shithub: pprolog

Download patch

ref: 8fdf0bc6e9770e2cea4d8e18299ff5a8a9aa0802
parent: 02145f06ac007d730bc16930185fe18fa3e76c68
author: Peter Mikkelsen <peter@pmikkelsen.com>
date: Tue Jun 29 12:28:09 EDT 2021

Fix some parser errors, and accept clauses without a body

--- a/example.pl
+++ b/example.pl
@@ -2,4 +2,4 @@
 
 math(A,B,C,D) :- D is A + B + C * A.
 
-true :- 1 = 1.
+true.
\ No newline at end of file
--- a/parser.c
+++ b/parser.c
@@ -99,19 +99,19 @@
 		return;
 
 	Term *t = fullterm(AtomTok, L".", nil);
-	if(t->tag != CompoundTerm || runestrcmp(t->text, L":-") != 0 || !(t->arity == 1 || t->arity == 2)){
-		print("Expected directive or clause as toplevel\n");
-		print("Got %S\n", prettyprint(t));
-		print("Tag=%d arity=%d text=\"%S\"\n", t->tag, t->arity, t->text);
-		syntaxerror("prologtext");
-	}
 
-	if(t->arity == 1){
-		/* A directive */
+	if(t->tag == CompoundTerm && runestrcmp(t->text, L":-") == 0 && t->arity == 1){
+		/* A Directive */
 		print("Got directive: %S\n", prettyprint(t));
+	}else if(t->tag == CompoundTerm && runestrcmp(t->text, L":-") == 0 && t->arity == 2){
+		/* A clause with a body */
+		print("Got clause with body: %S\n", prettyprint(t));
+	}else if(t->tag == AtomTerm || t->tag == CompoundTerm){
+		/* A clause without a body */
+		print("Got clause without body: %S\n", prettyprint(t));
 	}else{
-		/* A clause */
-		print("Got clause: %S\n", prettyprint(t));
+		print("Expected directive or clause as toplevel\n");
+		syntaxerror("prologtext");
 	}
 
 	if(lookahead.tag == AtomTok && runestrcmp(lookahead.text, L".") == 0)
@@ -537,6 +537,11 @@
 	exits("lexer");
 
 Functor:
+	if(peek == Beof){
+		Bgetrune(parsein);
+		return;
+	}
+
 	peek = Bgetrune(parsein);
 	if(peek == L'(')
 		lookahead.tag = FunctorTok;
@@ -556,6 +561,6 @@
 void
 syntaxerror(char *where)
 {
-	print("Syntax error: Unexpected %d token in %s\n", lookahead.tag, where);
+	print("Syntax error: Unexpected %d (%S) token in %s\n", lookahead.tag, lookahead.text, where);
 	exits("syntax error");
 }
\ No newline at end of file