Begin structs, add comment on multiline lambdas, add Ast flags
This commit is contained in:
57
new_ast.cpp
57
new_ast.cpp
@@ -100,13 +100,14 @@ enum Ast_Kind{
|
||||
AST_BLOCK,
|
||||
AST_LAMBDA,
|
||||
AST_LAMBDA_ARG,
|
||||
AST_STRUCT,
|
||||
AST_CONST,
|
||||
AST_VAR,
|
||||
};
|
||||
|
||||
AST_TYPESPEC_IDENT,
|
||||
AST_TYPESPEC_POINTER,
|
||||
AST_TYPESPEC_ARRAY,
|
||||
AST_TYPESPEC_LAMBDA
|
||||
typedef U32 Ast_Flag;
|
||||
enum{
|
||||
AST_EXPR = 1,
|
||||
};
|
||||
|
||||
struct Ast{
|
||||
@@ -115,11 +116,12 @@ struct Ast{
|
||||
|
||||
Ast_Kind kind;
|
||||
Ast *parent;
|
||||
Ast_Flag flags;
|
||||
// @todo?
|
||||
// bool is_atom: 1;
|
||||
// bool is_stmt: 1;
|
||||
// bool is_expr: 1;
|
||||
// bool is_decl: 1;
|
||||
// bool is_expr: 1;
|
||||
// bool is_named: 1;
|
||||
};
|
||||
|
||||
@@ -211,6 +213,12 @@ struct Ast_Array: Ast_Expr{
|
||||
Ast_Expr *expr;
|
||||
};
|
||||
|
||||
struct Ast_Struct: Ast{
|
||||
// Required to be Ast_Struct or Ast_Var or Ast_Const
|
||||
Array<Ast *> members;
|
||||
|
||||
};
|
||||
|
||||
struct Ast_Named:Ast{
|
||||
Intern_String name;
|
||||
};
|
||||
@@ -221,7 +229,7 @@ struct Ast_Var: Ast_Named{
|
||||
};
|
||||
|
||||
struct Ast_Const: Ast_Named{
|
||||
Ast_Expr *expr;
|
||||
Ast *value;
|
||||
};
|
||||
|
||||
struct Ast_Package:Ast{
|
||||
@@ -240,22 +248,25 @@ struct Ast_Package:Ast{
|
||||
result->id = ++pctx->unique_ids
|
||||
|
||||
function Ast_Atom *
|
||||
ast_str(Token *pos, Intern_String string){
|
||||
ast_str(Token *pos, Intern_String string, Ast_Flag flags = 0){
|
||||
AST_NEW(Atom, STR, pos);
|
||||
result->flags = flags;
|
||||
result->intern_val = string;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Atom *
|
||||
ast_ident(Token *pos, Intern_String string){
|
||||
ast_ident(Token *pos, Intern_String string, Ast_Flag flags = 0){
|
||||
AST_NEW(Atom, IDENT, pos);
|
||||
result->flags = flags;
|
||||
result->intern_val = string;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Atom *
|
||||
ast_int(Token *pos, S64 integer){
|
||||
ast_int(Token *pos, S64 integer, Ast_Flag flags){
|
||||
AST_NEW(Atom, INT, pos);
|
||||
result->flags = flags;
|
||||
result->int_val = integer;
|
||||
return result;
|
||||
}
|
||||
@@ -263,6 +274,7 @@ ast_int(Token *pos, S64 integer){
|
||||
function Ast_Expr *
|
||||
ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
|
||||
AST_NEW(Binary, BINARY, op);
|
||||
result->flags = AST_EXPR;
|
||||
result->op = op->kind;
|
||||
result->left = left;
|
||||
result->right = right;
|
||||
@@ -274,6 +286,7 @@ ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
|
||||
function Ast_Compound *
|
||||
ast_expr_compound(Token *pos, Ast_Expr *typespec, Array<Ast_Compound_Item *> exprs){
|
||||
AST_NEW(Compound, COMPOUND, pos);
|
||||
result->flags = AST_EXPR;
|
||||
result->typespec = typespec;
|
||||
result->exprs = exprs.tight_copy(pctx->perm);
|
||||
if(result->typespec) result->typespec->parent = result;
|
||||
@@ -284,6 +297,7 @@ ast_expr_compound(Token *pos, Ast_Expr *typespec, Array<Ast_Compound_Item *> exp
|
||||
function Ast_Compound_Item *
|
||||
ast_expr_compound_item(Token *pos, Ast_Expr *index, Ast_Atom *name, Ast_Expr *item){
|
||||
AST_NEW(Compound_Item, COMPOUND_ITEM, pos);
|
||||
result->flags = AST_EXPR;
|
||||
result->name = name;
|
||||
result->index = index;
|
||||
result->item = item;
|
||||
@@ -296,6 +310,7 @@ ast_expr_compound_item(Token *pos, Ast_Expr *index, Ast_Atom *name, Ast_Expr *it
|
||||
function Ast_Expr *
|
||||
ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Expr *typespec){
|
||||
AST_NEW(Cast, CAST, pos);
|
||||
result->flags = AST_EXPR;
|
||||
result->expr = expr;
|
||||
result->typespec = typespec;
|
||||
expr->parent = result;
|
||||
@@ -306,6 +321,7 @@ ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Expr *typespec){
|
||||
function Ast_Expr *
|
||||
ast_expr_unary(Token *pos, Token_Kind op, Ast_Expr *expr){
|
||||
AST_NEW(Unary, UNARY, pos);
|
||||
result->flags = AST_EXPR;
|
||||
result->expr = expr;
|
||||
result->op = op;
|
||||
expr->parent = result;
|
||||
@@ -315,6 +331,7 @@ ast_expr_unary(Token *pos, Token_Kind op, Ast_Expr *expr){
|
||||
function Ast_Expr *
|
||||
ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index){
|
||||
AST_NEW(Index, INDEX, pos);
|
||||
result->flags = AST_EXPR;
|
||||
result->expr = expr;
|
||||
result->index = index;
|
||||
expr->parent = result;
|
||||
@@ -325,6 +342,7 @@ ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index){
|
||||
function Ast_Lambda *
|
||||
ast_lambda(Token *pos, Array<Ast_Lambda_Arg *> params, Ast_Expr *ret, Ast_Block *block){
|
||||
AST_NEW(Lambda, LAMBDA, pos);
|
||||
result->flags = AST_EXPR;
|
||||
result->args = params.tight_copy(pctx->perm);
|
||||
result->block = block;
|
||||
result->ret = ret;
|
||||
@@ -339,6 +357,7 @@ ast_lambda(Token *pos, Array<Ast_Lambda_Arg *> params, Ast_Expr *ret, Ast_Block
|
||||
function Ast_Lambda_Arg *
|
||||
ast_expr_lambda_arg(Token *pos, Intern_String name, Ast_Expr *typespec){
|
||||
AST_NEW(Lambda_Arg, LAMBDA_ARG, pos);
|
||||
result->flags = AST_EXPR;
|
||||
result->name = name;
|
||||
result->typespec = typespec;
|
||||
result->typespec->parent = result;
|
||||
@@ -387,11 +406,23 @@ ast_init(Token *pos, Token_Kind op, Ast_Atom *ident, Ast_Expr *expr){
|
||||
function Ast_Array *
|
||||
ast_array(Token *pos, Ast_Expr *expr){
|
||||
AST_NEW(Array, ARRAY, pos);
|
||||
result->flags = AST_EXPR;
|
||||
result->expr = expr;
|
||||
result->expr->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Struct *
|
||||
ast_struct(Token *pos, Array<Ast *> members){
|
||||
AST_NEW(Struct, STRUCT, pos);
|
||||
result->members = members.tight_copy(pctx->perm);
|
||||
For(result->members) {
|
||||
assert(it[0]->kind == AST_VAR || it[0]->kind == AST_CONST || it[0]->kind == AST_STRUCT);
|
||||
it[0]->parent = result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Declarations
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -407,11 +438,13 @@ ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr){
|
||||
}
|
||||
|
||||
function Ast_Const *
|
||||
ast_const(Token *pos, Intern_String name, Ast_Expr *expr){
|
||||
ast_const(Token *pos, Intern_String name, Ast *value){
|
||||
AST_NEW(Const, CONST, pos);
|
||||
result->expr = expr;
|
||||
|
||||
assert(value->kind == AST_STRUCT || is_flag_set(value->flags, AST_EXPR));
|
||||
result->value = value;
|
||||
result->name = name;
|
||||
result->expr->parent = result;
|
||||
result->value->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user