Parsing for stmt
This commit is contained in:
1
main.cpp
1
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
|
[ ] - Default values in structs??? Should compound stmts bring values from default values?? Maybe not? Whats the alternative
|
||||||
[ ] - Write up on order independent declarations
|
[ ] - Write up on order independent declarations
|
||||||
[ ] - Switch
|
[ ] - Switch
|
||||||
|
[ ] - Pass statement
|
||||||
[ ] - lvalue, rvalue concept so we cant assign value to some arbitrary weird expression
|
[ ] - lvalue, rvalue concept so we cant assign value to some arbitrary weird expression
|
||||||
[ ] - More basic types
|
[ ] - More basic types
|
||||||
[ ] - Array of inferred size
|
[ ] - Array of inferred size
|
||||||
|
|||||||
34
new_ast.cpp
34
new_ast.cpp
@@ -93,7 +93,7 @@ enum Ast_Kind: U32{
|
|||||||
|
|
||||||
AST_POINTER,
|
AST_POINTER,
|
||||||
AST_ARRAY,
|
AST_ARRAY,
|
||||||
AST_INIT,
|
AST_FOR,
|
||||||
AST_IF,
|
AST_IF,
|
||||||
AST_IF_NODE,
|
AST_IF_NODE,
|
||||||
AST_RETURN,
|
AST_RETURN,
|
||||||
@@ -188,6 +188,15 @@ struct Ast_If: Ast{
|
|||||||
Array<Ast_If_Node *> ifs;
|
Array<Ast_If_Node *> 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{
|
struct Ast_Lambda_Arg: Ast_Expr{
|
||||||
Intern_String name;
|
Intern_String name;
|
||||||
Ast_Expr *typespec;
|
Ast_Expr *typespec;
|
||||||
@@ -387,6 +396,20 @@ ast_if(Token *pos, Array<Ast_If_Node *> ifs){
|
|||||||
return result;
|
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 *
|
function Ast_Return *
|
||||||
ast_return(Token *pos, Ast_Expr *expr){
|
ast_return(Token *pos, Ast_Expr *expr){
|
||||||
AST_NEW(Return, RETURN, pos, AST_STMT);
|
AST_NEW(Return, RETURN, pos, AST_STMT);
|
||||||
@@ -399,14 +422,17 @@ ast_return(Token *pos, Ast_Expr *expr){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Ast_If_Node *
|
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);
|
AST_NEW(If_Node, IF_NODE, pos, AST_STMT);
|
||||||
result->block = block;
|
result->block = block;
|
||||||
result->expr = expr;
|
result->expr = expr;
|
||||||
result->init = init;
|
result->init = (Ast_Binary *)init;
|
||||||
if(result->block) result->block->parent = result;
|
if(result->block) result->block->parent = result;
|
||||||
if(result->expr) result->expr->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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -135,14 +135,14 @@ Compound literals
|
|||||||
*/
|
*/
|
||||||
function Ast_Expr *parse_expr(S64 rbp = 0);
|
function Ast_Expr *parse_expr(S64 rbp = 0);
|
||||||
|
|
||||||
function Ast_Binary *
|
function Ast_Expr *
|
||||||
parse_init_stmt(Ast_Expr *expr){
|
parse_init_stmt(Ast_Expr *expr){
|
||||||
Token *token = token_get();
|
Token *token = token_get();
|
||||||
if(token_match(TK_Colon)){
|
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(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)){
|
if(token_match(TK_Assign)){
|
||||||
Ast_Expr *value = parse_expr();
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
else not_implemented;
|
else not_implemented;
|
||||||
@@ -210,17 +210,35 @@ parse_block(){
|
|||||||
Ast_Return *return_stmt = ast_return(token, expr);
|
Ast_Return *return_stmt = ast_return(token, expr);
|
||||||
stmts.add(return_stmt);
|
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)){
|
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_Binary *init_val = parse_init_stmt(expr);
|
Ast_Expr *init_val = parse_init_stmt(expr);
|
||||||
if(init_val){
|
if(init_val){
|
||||||
if(token_match(TK_Comma)) expr = parse_expr();
|
if(token_match(TK_Comma)) expr = parse_expr();
|
||||||
else expr = 0;
|
else expr = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ast_Block *block = parse_block();
|
Ast_Block *if_block = parse_block();
|
||||||
Ast_If_Node *if_node = ast_if_node(token, init_val, expr, block);
|
Ast_If_Node *if_node = ast_if_node(token, init_val, expr, if_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)){
|
||||||
@@ -228,13 +246,13 @@ parse_block(){
|
|||||||
token = token_next();
|
token = token_next();
|
||||||
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 *else_if_block = parse_block();
|
||||||
Ast_If_Node *if_node = ast_if_node(token, 0, expr, block);
|
Ast_If_Node *if_node = ast_if_node(token, 0, expr, else_if_block);
|
||||||
if_nodes.add(if_node);
|
if_nodes.add(if_node);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
Ast_Block *block = parse_block();
|
Ast_Block *else_block = parse_block();
|
||||||
Ast_If_Node *if_node = ast_if_node(token, 0, 0, block);
|
Ast_If_Node *if_node = ast_if_node(token, 0, 0, else_block);
|
||||||
if_nodes.add(if_node);
|
if_nodes.add(if_node);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user