summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Mikkelsen <peter@pmikkelsen.com>2021-06-29 16:28:09 +0000
committerPeter Mikkelsen <peter@pmikkelsen.com>2021-06-29 16:28:09 +0000
commit8fdf0bc6e9770e2cea4d8e18299ff5a8a9aa0802 (patch)
tree34f90a0006bf6e06be587cd0d7a71aa8408b8c9c
parent02145f06ac007d730bc16930185fe18fa3e76c68 (diff)
Fix some parser errors, and accept clauses without a body
-rw-r--r--example.pl2
-rw-r--r--parser.c27
2 files changed, 17 insertions, 12 deletions
diff --git a/example.pl b/example.pl
index b0c6ebe..8037d1c 100644
--- 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
diff --git a/parser.c b/parser.c
index 459d048..4e88013 100644
--- a/parser.c
+++ b/parser.c
@@ -99,19 +99,19 @@ prologtext(void)
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 @@ Integer:
exits("lexer");
Functor:
+ if(peek == Beof){
+ Bgetrune(parsein);
+ return;
+ }
+
peek = Bgetrune(parsein);
if(peek == L'(')
lookahead.tag = FunctorTok;
@@ -556,6 +561,6 @@ match(int tag)
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