diff options
-rw-r--r-- | code.txt | 4 | ||||
-rw-r--r-- | parser.c | 78 |
2 files changed, 60 insertions, 22 deletions
@@ -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"); -}; +} @@ -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; |