From ff0fcb80bc488e174c47ab203af95c94c3bb117d Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Tue, 9 Aug 2016 07:24:28 +0200 Subject: Blocks --- code.txt | 7 ++++--- parser.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/code.txt b/code.txt index 783d1b5..6359e4f 100644 --- a/code.txt +++ b/code.txt @@ -14,7 +14,8 @@ if (2 > 1) 1 - 1 - 1; if kaas doe(dingen) -else if andere(kaas) - doe(andere,dingen) -else +else if andere(kaas) { + doe(andere,dingen); +} else { print("rip"); +}; diff --git a/parser.c b/parser.c index 7ed64a6..fff3e07 100644 --- a/parser.c +++ b/parser.c @@ -341,14 +341,51 @@ static AST* parseterm(const char *source,int *reslen){ case TT_SYM: if(tok.len==1&&tok.str[0]=='{'){ - assert(NOT_IMPLEMENTED); + node=malloc(sizeof(AST)); + if(!node)outofmem(); + node->type=AST_BLOCK; + int sz=32; + node->b.len=0; + node->b.exprs=calloc(sz,sizeof(AST*)); + if(!node->b.exprs)outofmem(); + int len; + int cursor=0; + while(true){ + if(node->b.len==sz){ + sz*=2; + node->b.exprs=realloc(node->b.exprs,sz*sizeof(AST*)); + if(!node->b.exprs)outofmem(); + } + AST *n=parseexpr(source+cursor,&len,0,INT_MAX); + if(!n){ + ast_free(node); + return NULL; + } + 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; + } + cursor=src-source; + src=source+cursor; + tok=nexttoken(&src,false); + if(tok.type==TT_SYM&&tok.len==1&&tok.str[0]=='}'){ + source=src; + DBG(printtoken(stderr,tok,"block } ")); + break; + } + } } else if(tok.len==1&&tok.str[0]=='('){ int len; node=parseexpr(source,&len,0,INT_MAX); if(!node)return NULL; source+=len; Token aftertok=nexttoken(&source,false); - DBG(printtoken(stderr,aftertok,"braceclose")); + DBG(printtoken(stderr,aftertok,"parenclose")); if(aftertok.type!=TT_SYM||aftertok.len!=1||aftertok.str[0]!=')'){ ast_free(node); return NULL; @@ -622,9 +659,9 @@ static void ast_debug_(FILE *stream,const AST *ast,int indent){ break; case AST_IF: - fprintf(stream,"if ("); + fprintf(stream,"if "); ast_debug_(stream,ast->i.cond,indent); - fprintf(stream,")\n"); + fputc('\n',stream); indent++; INDENT ast_debug_(stream,ast->i.thenb,indent); -- cgit v1.2.3