Add init statement to if
This commit is contained in:
@@ -208,8 +208,20 @@ gen_ast(Ast *ast){
|
|||||||
Ast_End();
|
Ast_End();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ast_Begin(AST_INIT, Ast_Init){
|
||||||
|
Sym *sym = resolved_get(node);
|
||||||
|
gen_simple_decl(sym->type, node->ident->intern_val);
|
||||||
|
if(node->expr){
|
||||||
|
gen(" = ");
|
||||||
|
gen_expr(node->expr);
|
||||||
|
}
|
||||||
|
gen(";");
|
||||||
|
Ast_End();
|
||||||
|
}
|
||||||
|
|
||||||
Ast_Begin(AST_IF, Ast_If){
|
Ast_Begin(AST_IF, Ast_If){
|
||||||
For(node->ifs){
|
For(node->ifs){
|
||||||
|
if(it[0]->init) gen_ast(it[0]->init);
|
||||||
if(node->ifs.is_first(it)){
|
if(node->ifs.is_first(it)){
|
||||||
genln("if(");
|
genln("if(");
|
||||||
gen_expr(it[0]->expr);
|
gen_expr(it[0]->expr);
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
if_stmt :: (cond: int): int
|
if_stmt :: (cond: int): int
|
||||||
CONSTANT :: 10
|
CONSTANT :: 10
|
||||||
|
|
||||||
if cond + CONSTANT
|
if i := cond, cond + CONSTANT
|
||||||
return cond + CONSTANT
|
return i + CONSTANT
|
||||||
else if cond - CONSTANT
|
else if cond - CONSTANT
|
||||||
return cond - CONSTANT
|
return cond - CONSTANT
|
||||||
else
|
else
|
||||||
|
|||||||
22
new_ast.cpp
22
new_ast.cpp
@@ -89,6 +89,7 @@ enum Ast_Kind{
|
|||||||
AST_COMPOUND_ITEM,
|
AST_COMPOUND_ITEM,
|
||||||
AST_COMPOUND,
|
AST_COMPOUND,
|
||||||
|
|
||||||
|
AST_INIT,
|
||||||
AST_IF,
|
AST_IF,
|
||||||
AST_IF_NODE,
|
AST_IF_NODE,
|
||||||
AST_RETURN,
|
AST_RETURN,
|
||||||
@@ -167,9 +168,16 @@ struct Ast_Return: Ast{
|
|||||||
Ast_Expr *expr;
|
Ast_Expr *expr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Ast_Init: Ast{
|
||||||
|
Token_Kind op;
|
||||||
|
Ast_Atom *ident;
|
||||||
|
Ast_Expr *expr;
|
||||||
|
};
|
||||||
|
|
||||||
struct Ast_If_Node: Ast{
|
struct Ast_If_Node: Ast{
|
||||||
Ast_Expr *expr ;
|
Ast_Expr *expr ;
|
||||||
Ast_Block *block;
|
Ast_Block *block;
|
||||||
|
Ast_Init *init;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Ast_If: Ast{
|
struct Ast_If: Ast{
|
||||||
@@ -338,9 +346,19 @@ ast_if(Token *pos, Array<Ast_If_Node *> ifs){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Ast_If_Node *
|
function Ast_If_Node *
|
||||||
ast_if_node(Token *pos, Ast_Expr *expr, Ast_Block *block){
|
ast_if_node(Token *pos, Ast_Init *init, Ast_Expr *expr, Ast_Block *block){
|
||||||
AST_NEW(If_Node, AST_IF_NODE, pos);
|
AST_NEW(If_Node, AST_IF_NODE, pos);
|
||||||
result->block = block;
|
result->block = block;
|
||||||
|
result->expr = expr;
|
||||||
|
result->init = init;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function Ast_Init *
|
||||||
|
ast_init(Token *pos, Token_Kind op, Ast_Atom *ident, Ast_Expr *expr){
|
||||||
|
AST_NEW(Init, AST_INIT, pos);
|
||||||
|
result->op = op;
|
||||||
|
result->ident = ident;
|
||||||
result->expr = expr;
|
result->expr = expr;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -196,8 +196,27 @@ parse_block(){
|
|||||||
else if(token_match_keyword(keyword_if)){
|
else if(token_match_keyword(keyword_if)){
|
||||||
Array<Ast_If_Node *> if_nodes = {scratch};
|
Array<Ast_If_Node *> if_nodes = {scratch};
|
||||||
Ast_Expr *expr = parse_expr();
|
Ast_Expr *expr = parse_expr();
|
||||||
|
Ast_Init *init_val = 0;
|
||||||
|
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
assert(expr->kind == AST_IDENT);
|
||||||
|
init_val = ast_init(token, TK_Comma, (Ast_Atom *)expr, value);
|
||||||
|
}
|
||||||
|
else not_implemented;
|
||||||
|
|
||||||
|
if(token_match(TK_Comma)){
|
||||||
|
expr = parse_expr();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ast_Block *block = parse_block();
|
Ast_Block *block = parse_block();
|
||||||
Ast_If_Node *if_node = ast_if_node(token, expr, block);
|
Ast_If_Node *if_node = ast_if_node(token, init_val, expr, block);
|
||||||
if_nodes.add(if_node);
|
if_nodes.add(if_node);
|
||||||
|
|
||||||
while(token_is(SAME_SCOPE) && token_is_keyword(keyword_else, 1)){
|
while(token_is(SAME_SCOPE) && token_is_keyword(keyword_else, 1)){
|
||||||
@@ -206,12 +225,12 @@ parse_block(){
|
|||||||
if(token_match_keyword(keyword_if)){
|
if(token_match_keyword(keyword_if)){
|
||||||
Ast_Expr *expr = parse_expr();
|
Ast_Expr *expr = parse_expr();
|
||||||
Ast_Block *block = parse_block();
|
Ast_Block *block = parse_block();
|
||||||
Ast_If_Node *if_node = ast_if_node(token, expr, block);
|
Ast_If_Node *if_node = ast_if_node(token, 0, expr, block);
|
||||||
if_nodes.add(if_node);
|
if_nodes.add(if_node);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
Ast_Block *block = parse_block();
|
Ast_Block *block = parse_block();
|
||||||
Ast_If_Node *if_node = ast_if_node(token, 0, block);
|
Ast_If_Node *if_node = ast_if_node(token, 0, 0, block);
|
||||||
if_nodes.add(if_node);
|
if_nodes.add(if_node);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -212,12 +212,27 @@ eval_stmt(Ast *ast, Ast_Resolved_Type *ret){
|
|||||||
Ast_End();
|
Ast_End();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ast_Begin(AST_INIT, Ast_Init){
|
||||||
|
switch(node->op){
|
||||||
|
case TK_Comma:{
|
||||||
|
Operand op = eval_expr(node->expr);
|
||||||
|
Sym *sym = sym_new(SYM_Var, node->ident->intern_val, op.type, node);
|
||||||
|
sym_insert(sym);
|
||||||
|
}break;
|
||||||
|
invalid_default_case;
|
||||||
|
}
|
||||||
|
Ast_End();
|
||||||
|
}
|
||||||
|
|
||||||
Ast_Begin(AST_IF, Ast_If){
|
Ast_Begin(AST_IF, Ast_If){
|
||||||
For(node->ifs){
|
For(node->ifs){
|
||||||
if(it[0]->expr) eval_expr(it[0]->expr);
|
if(it[0]->expr) eval_expr(it[0]->expr);
|
||||||
|
if(it[0]->init) eval_stmt(it[0]->init, ret);
|
||||||
|
S64 scope_index = scope_push();
|
||||||
For_It(it[0]->block->stmts, jt){
|
For_It(it[0]->block->stmts, jt){
|
||||||
eval_stmt(jt[0], ret);
|
eval_stmt(jt[0], ret);
|
||||||
}
|
}
|
||||||
|
scope_pop(scope_index);
|
||||||
}
|
}
|
||||||
Ast_End();
|
Ast_End();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user