summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-08-09 20:25:05 +0200
committertomsmeding <tom.smeding@gmail.com>2016-08-09 20:25:05 +0200
commitbd9d359d64a3c6a9fbc509a6e3be6f66c6dbe940 (patch)
tree26921e174379d575a5b52d8b0e6cfbff6402b125
parentff0fcb80bc488e174c47ab203af95c94c3bb117d (diff)
Better if syntax (better semicolons)
-rw-r--r--code.txt4
-rw-r--r--parser.c78
2 files changed, 60 insertions, 22 deletions
diff --git a/code.txt b/code.txt
index 6359e4f..ce95b45 100644
--- a/code.txt
+++ b/code.txt
@@ -13,9 +13,9 @@ if (2 > 1) 1 - 1 - 1;
1 ** 1 ** 1;
if kaas
- doe(dingen)
+ doe(dingen);
else if andere(kaas) {
doe(andere,dingen);
} else {
print("rip");
-};
+}
diff --git a/parser.c b/parser.c
index fff3e07..fa39168 100644
--- a/parser.c
+++ b/parser.c
@@ -36,6 +36,21 @@ static char hexencode(int n){
return n<10?n+'0':n+'a';
}
+static bool semicolon_needed_after(const AST *after){
+ DBGF("semicolon_needed_after(type %d) -> ",after->type);
+ switch(after->type){
+ case AST_BLOCK:
+ case AST_IF:
+ case AST_WHILE:
+ DBGF("false\n");
+ return false;
+
+ default:
+ DBGF("true\n");
+ return true;
+ }
+}
+
typedef enum Tokentype{
TT_NUM,
@@ -264,6 +279,15 @@ static AST* parseterm(const char *source,int *reslen){
return NULL;
}
source+=len;
+ if(semicolon_needed_after(thenbody)){
+ Token sctok=nexttoken(&source,false);
+ if(sctok.type!=TT_ENDSTMT){
+ ast_free(cond);
+ ast_free(thenbody);
+ return NULL;
+ }
+ DBG(printtoken(stderr,sctok,"afterthen"));
+ }
AST *elsebody=NULL;
const char *src=source;
Token elsetok=nexttoken(&src,false);
@@ -277,6 +301,16 @@ static AST* parseterm(const char *source,int *reslen){
return NULL;
}
source+=len;
+ if(semicolon_needed_after(elsebody)){
+ Token sctok=nexttoken(&source,false);
+ if(sctok.type!=TT_ENDSTMT){
+ ast_free(cond);
+ ast_free(thenbody);
+ ast_free(elsebody);
+ return NULL;
+ }
+ DBG(printtoken(stderr,sctok,"afterelse"));
+ }
}
node=malloc(sizeof(AST));
if(!node)outofmem();
@@ -363,19 +397,21 @@ static AST* parseterm(const char *source,int *reslen){
}
node->b.exprs[node->b.len++]=n;
cursor+=len;
- const char *src=source+cursor;
- Token tok=nexttoken(&src,false);
- DBG(printtoken(stderr,tok,"block ; "));
- if(tok.type!=TT_ENDSTMT){
- ast_free(node);
- return NULL;
+ if(semicolon_needed_after(n)){
+ const char *src=source+cursor;
+ Token sctok=nexttoken(&src,false);
+ DBG(printtoken(stderr,sctok,"block ; "));
+ if(sctok.type!=TT_ENDSTMT){
+ ast_free(node);
+ return NULL;
+ }
+ cursor=src-source;
}
- cursor=src-source;
- src=source+cursor;
- tok=nexttoken(&src,false);
- if(tok.type==TT_SYM&&tok.len==1&&tok.str[0]=='}'){
+ const char *src=source+cursor;
+ Token brtok=nexttoken(&src,false);
+ if(brtok.type==TT_SYM&&brtok.len==1&&brtok.str[0]=='}'){
source=src;
- DBG(printtoken(stderr,tok,"block } "));
+ DBG(printtoken(stderr,brtok,"block } "));
break;
}
}
@@ -570,17 +606,19 @@ AST* parse(const char *source,char **errmsg){
}
bl->b.exprs[bl->b.len++]=node;
cursor+=reslen;
+ if(semicolon_needed_after(node)){
+ const char *src=source+cursor;
+ Token tok=nexttoken(&src,false);
+ DBG(printtoken(stderr,tok,"parse "));
+ if(tok.type!=TT_ENDSTMT){
+ ast_free(bl);
+ *errmsg=reportparseerror(source);
+ return NULL;
+ }
+ cursor=src-source;
+ }
const char *src=source+cursor;
Token tok=nexttoken(&src,false);
- DBG(printtoken(stderr,tok,"parse "));
- if(tok.type!=TT_ENDSTMT){
- ast_free(bl);
- *errmsg=reportparseerror(source);
- return NULL;
- }
- cursor=src-source;
- src=source+cursor;
- tok=nexttoken(&src,false);
if(tok.type==TT_EOF)break;
}
return bl;