Ast modified, Ast_Expr is not a union instead it uses inheritence
This commit is contained in:
153
new_ast.cpp
153
new_ast.cpp
@@ -73,7 +73,7 @@ enum Ast_Kind{
|
||||
AST_COMPOUND,
|
||||
|
||||
AST_LAMBDA,
|
||||
AST_LAMBDA_PARAM,
|
||||
AST_LAMBDA_ARG,
|
||||
AST_CONST,
|
||||
AST_VAR,
|
||||
|
||||
@@ -91,52 +91,61 @@ struct Ast{
|
||||
|
||||
struct Ast_Resolved_Type;
|
||||
struct Ast_Typespec;
|
||||
struct Ast_Expr:Ast{
|
||||
struct Ast_Expr:Ast{};
|
||||
|
||||
struct Ast_Atom: Ast_Expr{
|
||||
union{
|
||||
Intern_String intern_val;
|
||||
U64 int_val;
|
||||
struct{
|
||||
Token_Kind op;
|
||||
Ast_Expr *expr;
|
||||
}unary;
|
||||
struct{
|
||||
Token_Kind op;
|
||||
Ast_Expr *left;
|
||||
Ast_Expr *right;
|
||||
} binary;
|
||||
struct{
|
||||
Ast_Resolved_Type *type;
|
||||
Ast_Typespec *typespec;
|
||||
Array<Ast_Expr *> exprs;
|
||||
}compound;
|
||||
struct{
|
||||
Ast_Expr *name; // index | name
|
||||
Ast_Expr *index;
|
||||
Ast_Expr *item;
|
||||
}compound_item;
|
||||
struct{
|
||||
Ast_Expr *expr;
|
||||
Ast_Typespec *typespec;
|
||||
}cast;
|
||||
struct{
|
||||
Ast_Expr *expr;
|
||||
Ast_Expr *index;
|
||||
}index;
|
||||
struct{
|
||||
Intern_String name;
|
||||
Ast_Typespec *typespec;
|
||||
}lambda_param;
|
||||
};
|
||||
};
|
||||
|
||||
struct Ast_Compound_Item: Ast_Expr{
|
||||
Ast_Expr *name; // index | name
|
||||
Ast_Expr *index;
|
||||
Ast_Expr *item;
|
||||
};
|
||||
|
||||
struct Ast_Compound: Ast_Expr{
|
||||
Ast_Resolved_Type *type;
|
||||
Ast_Typespec *typespec;
|
||||
Array<Ast_Compound_Item *> exprs;
|
||||
};
|
||||
|
||||
struct Ast_Unary: Ast_Expr{
|
||||
Token_Kind op;
|
||||
Ast_Expr *expr;
|
||||
};
|
||||
|
||||
struct Ast_Cast: Ast_Expr{
|
||||
Ast_Expr *expr;
|
||||
Ast_Typespec *typespec;
|
||||
};
|
||||
|
||||
struct Ast_Index: Ast_Expr{
|
||||
Ast_Expr *expr;
|
||||
Ast_Expr *index;
|
||||
};
|
||||
|
||||
struct Ast_Binary: Ast_Expr{
|
||||
Token_Kind op;
|
||||
Ast_Expr *left;
|
||||
Ast_Expr *right;
|
||||
};
|
||||
|
||||
struct Ast_Block : Ast {
|
||||
Array<Ast *> stmts;
|
||||
};
|
||||
|
||||
struct Ast_Lambda_Arg: Ast_Expr{
|
||||
Intern_String name;
|
||||
Ast_Typespec *typespec;
|
||||
};
|
||||
|
||||
struct Ast_Lambda : Ast_Expr {
|
||||
Array<Ast_Expr *> params;
|
||||
Ast_Typespec *ret;
|
||||
Ast_Block *block;
|
||||
Array<Ast_Lambda_Arg *> args;
|
||||
Ast_Typespec *ret;
|
||||
Ast_Block *block;
|
||||
};
|
||||
|
||||
struct Ast_Resolved_Type;
|
||||
@@ -177,82 +186,82 @@ struct Ast_Package:Ast{
|
||||
result->pos = ipos; \
|
||||
result->id = ++pctx->unique_ids
|
||||
|
||||
function Ast_Expr *
|
||||
function Ast_Atom *
|
||||
ast_expr_string(Token *pos, Intern_String string){
|
||||
AST_NEW(Expr, AST_STR, pos);
|
||||
AST_NEW(Atom, AST_STR, pos);
|
||||
result->intern_val = string;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
function Ast_Atom *
|
||||
ast_expr_identifier(Token *pos, Intern_String string){
|
||||
AST_NEW(Expr, AST_IDENT, pos);
|
||||
AST_NEW(Atom, AST_IDENT, pos);
|
||||
result->intern_val = string;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
function Ast_Atom *
|
||||
ast_expr_integer(Token *pos, S64 integer){
|
||||
AST_NEW(Expr, AST_INT, pos);
|
||||
AST_NEW(Atom, AST_INT, pos);
|
||||
result->int_val = integer;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
|
||||
AST_NEW(Expr, AST_BINARY, op);
|
||||
result->binary.op = op->kind;
|
||||
result->binary.left = left;
|
||||
result->binary.right = right;
|
||||
AST_NEW(Binary, AST_BINARY, op);
|
||||
result->op = op->kind;
|
||||
result->left = left;
|
||||
result->right = right;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_compound(Token *pos, Ast_Typespec *typespec, Array<Ast_Expr *> exprs){
|
||||
AST_NEW(Expr, AST_COMPOUND, pos);
|
||||
result->compound.typespec = typespec;
|
||||
result->compound.exprs = exprs.tight_copy(pctx->perm);
|
||||
function Ast_Compound *
|
||||
ast_expr_compound(Token *pos, Ast_Typespec *typespec, Array<Ast_Compound_Item *> exprs){
|
||||
AST_NEW(Compound, AST_COMPOUND, pos);
|
||||
result->typespec = typespec;
|
||||
result->exprs = exprs.tight_copy(pctx->perm);
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
function Ast_Compound_Item *
|
||||
ast_expr_compound_item(Token *pos, Ast_Expr *index, Ast_Expr *name, Ast_Expr *item){
|
||||
AST_NEW(Expr, AST_COMPOUND_ITEM, pos);
|
||||
result->compound_item.name = name;
|
||||
result->compound_item.index = index;
|
||||
result->compound_item.item = item;
|
||||
AST_NEW(Compound_Item, AST_COMPOUND_ITEM, pos);
|
||||
result->name = name;
|
||||
result->index = index;
|
||||
result->item = item;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Typespec *typespec){
|
||||
AST_NEW(Expr, AST_CAST, pos);
|
||||
result->cast.expr = expr;
|
||||
result->cast.typespec = typespec;
|
||||
AST_NEW(Cast, AST_CAST, pos);
|
||||
result->expr = expr;
|
||||
result->typespec = typespec;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_unary(Token *pos, Token_Kind op, Ast_Expr *expr){
|
||||
AST_NEW(Expr, AST_UNARY, pos);
|
||||
result->unary.expr = expr;
|
||||
result->unary.op = op;
|
||||
AST_NEW(Unary, AST_UNARY, pos);
|
||||
result->expr = expr;
|
||||
result->op = op;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index){
|
||||
AST_NEW(Expr, AST_INDEX, pos);
|
||||
result->index.expr = expr;
|
||||
result->index.index = index;
|
||||
AST_NEW(Index, AST_INDEX, pos);
|
||||
result->expr = expr;
|
||||
result->index = index;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
function Ast_Lambda *
|
||||
ast_lambda(Token *pos, Array<Ast_Expr *> params, Ast_Typespec *ret){
|
||||
ast_lambda(Token *pos, Array<Ast_Lambda_Arg *> params, Ast_Typespec *ret){
|
||||
AST_NEW(Lambda, AST_LAMBDA, pos);
|
||||
result->params = params.tight_copy(pctx->perm);
|
||||
result->args = params.tight_copy(pctx->perm);
|
||||
result->ret = ret;
|
||||
return result;
|
||||
}
|
||||
@@ -263,11 +272,11 @@ ast_expr_lambda_empty(Token *pos){
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_lambda_param(Token *pos, Intern_String name, Ast_Typespec *typespec){
|
||||
AST_NEW(Expr, AST_LAMBDA_PARAM, pos);
|
||||
result->lambda_param.name = name;
|
||||
result->lambda_param.typespec = typespec;
|
||||
function Ast_Lambda_Arg *
|
||||
ast_expr_lambda_arg(Token *pos, Intern_String name, Ast_Typespec *typespec){
|
||||
AST_NEW(Lambda_Arg, AST_LAMBDA_ARG, pos);
|
||||
result->name = name;
|
||||
result->typespec = typespec;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user