diff --git a/main.cpp b/main.cpp index 5b24722..f49ee9d 100644 --- a/main.cpp +++ b/main.cpp @@ -46,6 +46,7 @@ For now I don't thing it should be overloadable. [ ] - Default values in structs??? Should compound stmts bring values from default values?? Maybe not? Whats the alternative [ ] - Write up on order independent declarations [ ] - Switch +[ ] - Pass statement [ ] - lvalue, rvalue concept so we cant assign value to some arbitrary weird expression [ ] - More basic types [ ] - Array of inferred size diff --git a/new_ast.cpp b/new_ast.cpp index 2e5ffe5..e637f28 100644 --- a/new_ast.cpp +++ b/new_ast.cpp @@ -93,7 +93,7 @@ enum Ast_Kind: U32{ AST_POINTER, AST_ARRAY, - AST_INIT, + AST_FOR, AST_IF, AST_IF_NODE, AST_RETURN, @@ -188,6 +188,15 @@ struct Ast_If: Ast{ Array ifs; }; +struct Ast_Pass: Ast{}; // @todo + +struct Ast_For: Ast{ + Ast_Expr *init; + Ast_Expr *cond; + Ast_Expr *iter; + Ast_Block *block; +}; + struct Ast_Lambda_Arg: Ast_Expr{ Intern_String name; Ast_Expr *typespec; @@ -387,6 +396,20 @@ ast_if(Token *pos, Array ifs){ return result; } +function Ast_For * +ast_for(Token *pos, Ast_Expr *init, Ast_Expr *cond, Ast_Expr *iter, Ast_Block *block){ + AST_NEW(For, FOR, pos, AST_STMT); + result->init = init; + result->cond = cond; + result->iter = iter; + result->block = block; + if(result->init) result->init->parent = result; + if(result->cond) result->cond->parent = result; + if(result->iter) result->iter->parent = result; + result->block->parent = result; + return result; +} + function Ast_Return * ast_return(Token *pos, Ast_Expr *expr){ AST_NEW(Return, RETURN, pos, AST_STMT); @@ -399,14 +422,17 @@ ast_return(Token *pos, Ast_Expr *expr){ } function Ast_If_Node * -ast_if_node(Token *pos, Ast_Binary *init, Ast_Expr *expr, Ast_Block *block){ +ast_if_node(Token *pos, Ast_Expr *init, Ast_Expr *expr, Ast_Block *block){ AST_NEW(If_Node, IF_NODE, pos, AST_STMT); result->block = block; result->expr = expr; - result->init = init; + result->init = (Ast_Binary *)init; if(result->block) result->block->parent = result; if(result->expr) result->expr->parent = result; - if(result->init) result->init->parent = result; + if(result->init) { + assert(init->kind == AST_BINARY); + result->init->parent = result; + } return result; } diff --git a/new_parse.cpp b/new_parse.cpp index cc74674..cfee774 100644 --- a/new_parse.cpp +++ b/new_parse.cpp @@ -135,14 +135,14 @@ Compound literals */ function Ast_Expr *parse_expr(S64 rbp = 0); -function Ast_Binary * +function Ast_Expr * parse_init_stmt(Ast_Expr *expr){ Token *token = token_get(); if(token_match(TK_Colon)){ if(expr->kind != AST_IDENT) parsing_error(token, "Failed to parse init stmt, expected left of [:] to be a token of kind [Identifier]"); if(token_match(TK_Assign)){ Ast_Expr *value = parse_expr(); - Ast_Binary *result = (Ast_Binary *)ast_expr_binary((Ast_Atom *)expr, value, token); + Ast_Expr *result = ast_expr_binary((Ast_Atom *)expr, value, token); return result; } else not_implemented; @@ -210,17 +210,35 @@ parse_block(){ Ast_Return *return_stmt = ast_return(token, expr); stmts.add(return_stmt); } + + else if(token_match_keyword(keyword_for)){ + Ast_Expr *expr_first = parse_expr(); + Ast_Expr *init = parse_init_stmt(expr_first); + Ast_Expr *actual_init = init ? init : expr_first; + + Ast_Expr *cond = 0; + Ast_Expr *iter = 0; + if(token_match(TK_Comma)){ + cond = parse_expr(); + if(token_match(TK_Comma)){ + iter = parse_expr(); + } + } + + stmts.add(ast_for(token, actual_init, cond, iter, parse_block())); + } + else if(token_match_keyword(keyword_if)){ Array if_nodes = {scratch}; Ast_Expr *expr = parse_expr(); - Ast_Binary *init_val = parse_init_stmt(expr); + Ast_Expr *init_val = parse_init_stmt(expr); if(init_val){ if(token_match(TK_Comma)) expr = parse_expr(); else expr = 0; } - Ast_Block *block = parse_block(); - Ast_If_Node *if_node = ast_if_node(token, init_val, expr, block); + Ast_Block *if_block = parse_block(); + Ast_If_Node *if_node = ast_if_node(token, init_val, expr, if_block); if_nodes.add(if_node); while(token_is(SAME_SCOPE) && token_is_keyword(keyword_else, 1)){ @@ -228,13 +246,13 @@ parse_block(){ token = token_next(); if(token_match_keyword(keyword_if)){ Ast_Expr *expr = parse_expr(); - Ast_Block *block = parse_block(); - Ast_If_Node *if_node = ast_if_node(token, 0, expr, block); + Ast_Block *else_if_block = parse_block(); + Ast_If_Node *if_node = ast_if_node(token, 0, expr, else_if_block); if_nodes.add(if_node); } else{ - Ast_Block *block = parse_block(); - Ast_If_Node *if_node = ast_if_node(token, 0, 0, block); + Ast_Block *else_block = parse_block(); + Ast_If_Node *if_node = ast_if_node(token, 0, 0, else_block); if_nodes.add(if_node); break; } diff --git a/order2.kl b/order2.kl index dedb8db..6bb3efb 100644 --- a/order2.kl +++ b/order2.kl @@ -43,3 +43,20 @@ pointer := &with_type deref := *pointer +/* +for + pass +for i:=0, i < 10, i+=1 + pass +for array + pass +for it in array + pass +for it,i in array + pass +for i in 0..10 + pass + + + +*/ \ No newline at end of file