summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-08-09 07:24:28 +0200
committertomsmeding <tom.smeding@gmail.com>2016-08-09 07:24:28 +0200
commitff0fcb80bc488e174c47ab203af95c94c3bb117d (patch)
tree5b8260ce03031ab302149510fed5f16aea55ca31
parent77263f95bbbc9d8c91ab77726f02f445a1e3f367 (diff)
Blocks
-rw-r--r--code.txt7
-rw-r--r--parser.c45
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);