diff options
-rw-r--r-- | ast.c | 21 | ||||
-rw-r--r-- | ast.h | 3 | ||||
-rw-r--r-- | com.c | 4 | ||||
-rw-r--r-- | g.peg | 2 | ||||
-rw-r--r-- | test.bþ | 5 |
5 files changed, 23 insertions, 12 deletions
@@ -57,6 +57,15 @@ AstNode astnode_new_symbol(char *s) { }; } +AstNode astnode_new_string(char *s) { + return (AstNode){ + .ty =AST_STRING, + .as = { + .str = s + } + }; +} + void astnode_disp(AstNode *a) { switch (a->ty) { case AST_NUM: @@ -70,20 +79,18 @@ void astnode_disp(AstNode *a) { } printf(")"); break; - case AST_SYMBOL:; + case AST_SYMBOL: + case AST_STRING: printf("%s:%s ",ast_ty_to_str(a->ty), a->as.str); break; + } } const char* ty_names[] = { - "list", "num", "symbol" + "list", "num", "symbol", "string" }; const char *ast_ty_to_str(AstTy ty) { - if (ty >= AST_TY_LAST || ty < 0) { - return "???"; - } else { - return ty_names[ty]; - } + return ty_names[ty]; } @@ -8,8 +8,8 @@ typedef enum { AST_LIST, AST_NUM, AST_SYMBOL, + AST_STRING, } AstTy; -#define AST_TY_LAST AST_SYMBOL+1 struct _astnode; typedef struct _astnode AstNode; @@ -37,6 +37,7 @@ void astnode_append(AstNode *l, AstNode val); AstNode astnode_new_num(int n); AstNode astnode_new_list(); AstNode astnode_new_symbol(char *s); +AstNode astnode_new_string(char *s); void astnode_disp(AstNode *a); @@ -11,6 +11,10 @@ static void compile_node(Chunk *ch, AstNode a) { printf("can't compile symbol\n"); exit(1); break; + case AST_STRING: + printf("can't compile strings yet\n"); + exit(1); + break; case AST_NUM: chunk_wbc(ch, OP_LOADK); chunk_wbc(ch, chunk_wconst(ch, VAL_NUM(a.as.num))); @@ -25,6 +25,7 @@ expr <- l:list { $$ = l; } / n:number { $$ = n; } / s:symbol { $$ = s; } + / t:string { $$ = t; } list <- { $$ = astnode_new_list(); } '(' @@ -34,6 +35,7 @@ list <- { $$ = astnode_new_list(); } number <- < [0-9]+ > (! ident_char) _ { $$ = astnode_new_num(atoi($1)); } symbol <- < ident_char+ > _ { $$ = astnode_new_symbol(strdup($1)); } +string <- '"' < [^"]+ > '"' { $$ = astnode_new_string(strdup($1)); } ident_char <- [-_a-zA-Z'+*0-9] _ <- [ \t\n]* @@ -1,4 +1 @@ -(do - (set a 4) - (set b 5) - (print (+ a b))) +(+ 6 (* 9 25)) |