Added lambda expressions, lambda types, no body yet
This commit is contained in:
152
new_ast.cpp
152
new_ast.cpp
@@ -20,21 +20,14 @@ struct Parse_Ctx:Lexer{
|
||||
Allocator *perm; // Stores: AST, tokens, interns
|
||||
Allocator *heap;
|
||||
|
||||
U64 unique_ids;
|
||||
Map global_syms;
|
||||
Map type_map;
|
||||
|
||||
Token empty_token;
|
||||
S64 indent;
|
||||
S64 pt[256]; // precedence table
|
||||
|
||||
void init(Allocator *perm_allocator, Allocator *heap_allocator){
|
||||
const S64 addp = 1;
|
||||
const S64 mulp = 2;
|
||||
pt[TK_Add] = addp;
|
||||
pt[TK_Sub] = addp;
|
||||
pt[TK_Div] = mulp;
|
||||
pt[TK_Mul] = mulp;
|
||||
|
||||
perm = perm_allocator;
|
||||
heap = heap_allocator;
|
||||
|
||||
@@ -61,30 +54,33 @@ thread_local Parse_Ctx *pctx;
|
||||
// AST
|
||||
//-----------------------------------------------------------------------------
|
||||
enum Ast_Kind{
|
||||
AK_None,
|
||||
AST_NONE,
|
||||
|
||||
AK_Package,
|
||||
AST_PACKAGE,
|
||||
|
||||
AK_Expr_Str,
|
||||
AK_Expr_Int,
|
||||
AK_Expr_Cast,
|
||||
AK_Expr_Ident,
|
||||
AK_Expr_Binary,
|
||||
AK_Expr_CompoundItem,
|
||||
AK_Expr_Compound,
|
||||
AST_STR,
|
||||
AST_INT,
|
||||
AST_CAST,
|
||||
AST_IDENT,
|
||||
AST_INDEX,
|
||||
AST_UNARY,
|
||||
AST_BINARY,
|
||||
AST_COMPOUND_ITEM,
|
||||
AST_COMPOUND,
|
||||
|
||||
AK_Decl_Func,
|
||||
AK_Decl_Func_Arg,
|
||||
AK_Decl_Const,
|
||||
AK_Decl_Var,
|
||||
AST_LAMBDA,
|
||||
AST_LAMBDA_PARAM,
|
||||
AST_CONST,
|
||||
AST_VAR,
|
||||
|
||||
AK_Typespec_Ident,
|
||||
AK_Typespec_Pointer,
|
||||
AK_Typespec_Array,
|
||||
AK_Typespec_Func
|
||||
AST_TYPESPEC_IDENT,
|
||||
AST_TYPESPEC_POINTER,
|
||||
AST_TYPESPEC_ARRAY,
|
||||
AST_TYPESPEC_LAMBDA
|
||||
};
|
||||
|
||||
struct Ast{
|
||||
U64 id;
|
||||
Ast_Kind kind;
|
||||
Token *pos;
|
||||
};
|
||||
@@ -94,6 +90,11 @@ struct Ast_Expr:Ast{
|
||||
union{
|
||||
Intern_String intern_val;
|
||||
U64 int_val;
|
||||
struct{
|
||||
Token_Kind op;
|
||||
Ast_Expr *expr;
|
||||
}unary;
|
||||
|
||||
struct{
|
||||
Token_Kind op;
|
||||
Ast_Expr *left;
|
||||
@@ -112,21 +113,31 @@ struct Ast_Expr:Ast{
|
||||
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_Lambda : Ast_Expr {
|
||||
Array<Ast_Expr *> params;
|
||||
Ast_Typespec *ret;
|
||||
};
|
||||
|
||||
struct Ast_Typespec:Ast{
|
||||
union{
|
||||
Ast_Typespec *base;
|
||||
Intern_String name;
|
||||
Ast_Typespec *base;
|
||||
Intern_String name;
|
||||
struct{
|
||||
Ast_Typespec *base;
|
||||
Ast_Expr *expr;
|
||||
}arr;
|
||||
struct{
|
||||
Ast_Typespec *ret;
|
||||
Array<Ast_Typespec*> args;
|
||||
}func;
|
||||
Ast_Lambda *lambda;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -153,34 +164,35 @@ struct Ast_Package:Ast{
|
||||
// AST Constructors beginning with expressions
|
||||
//-----------------------------------------------------------------------------
|
||||
#define AST_NEW(T,ikind,ipos) \
|
||||
Ast_##T *result = exp_alloc_type(pctx->perm, Ast_##T); \
|
||||
Ast_##T *result = exp_alloc_type(pctx->perm, Ast_##T); \
|
||||
result->kind = ikind; \
|
||||
result->pos = ipos
|
||||
result->pos = ipos; \
|
||||
result->id = ++pctx->unique_ids
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_string(Token *pos, Intern_String string){
|
||||
AST_NEW(Expr, AK_Expr_Str, pos);
|
||||
AST_NEW(Expr, AST_STR, pos);
|
||||
result->intern_val = string;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_identifier(Token *pos, Intern_String string){
|
||||
AST_NEW(Expr, AK_Expr_Ident, pos);
|
||||
AST_NEW(Expr, AST_IDENT, pos);
|
||||
result->intern_val = string;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_integer(Token *pos, S64 integer){
|
||||
AST_NEW(Expr, AK_Expr_Int, pos);
|
||||
AST_NEW(Expr, 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, AK_Expr_Binary, op);
|
||||
AST_NEW(Expr, AST_BINARY, op);
|
||||
result->binary.op = op->kind;
|
||||
result->binary.left = left;
|
||||
result->binary.right = right;
|
||||
@@ -189,7 +201,7 @@ ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_compound(Token *pos, Ast_Typespec *typespec, Array<Ast_Expr *> exprs){
|
||||
AST_NEW(Expr, AK_Expr_Compound, pos);
|
||||
AST_NEW(Expr, AST_COMPOUND, pos);
|
||||
result->compound.typespec = typespec;
|
||||
result->compound.exprs = exprs.tight_copy(pctx->perm);
|
||||
return result;
|
||||
@@ -197,7 +209,7 @@ ast_expr_compound(Token *pos, Ast_Typespec *typespec, Array<Ast_Expr *> exprs){
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_compound_item(Token *pos, Ast_Expr *index, Ast_Expr *name, Ast_Expr *item){
|
||||
AST_NEW(Expr, AK_Expr_CompoundItem, pos);
|
||||
AST_NEW(Expr, AST_COMPOUND_ITEM, pos);
|
||||
result->compound_item.name = name;
|
||||
result->compound_item.index = index;
|
||||
result->compound_item.item = item;
|
||||
@@ -206,42 +218,80 @@ ast_expr_compound_item(Token *pos, Ast_Expr *index, Ast_Expr *name, Ast_Expr *it
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Typespec *typespec){
|
||||
AST_NEW(Expr, AK_Expr_Cast, pos);
|
||||
AST_NEW(Expr, AST_CAST, pos);
|
||||
result->cast.expr = expr;
|
||||
result->cast.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;
|
||||
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;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
function Ast_Lambda *
|
||||
ast_lambda(Token *pos, Array<Ast_Expr *> params, Ast_Typespec *ret){
|
||||
AST_NEW(Lambda, AST_LAMBDA, pos);
|
||||
result->params = params.tight_copy(pctx->perm);
|
||||
result->ret = ret;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_lambda_empty(Token *pos){
|
||||
AST_NEW(Expr, AST_LAMBDA, 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;
|
||||
return result;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Typespecs
|
||||
//-----------------------------------------------------------------------------
|
||||
function Ast_Typespec *
|
||||
ast_typespec_name(Token *pos, Intern_String name){
|
||||
AST_NEW(Typespec, AK_Typespec_Ident, pos);
|
||||
AST_NEW(Typespec, AST_TYPESPEC_IDENT, pos);
|
||||
result->name = name;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Typespec *
|
||||
ast_typespec_pointer(Token *pos, Ast_Typespec *base){
|
||||
AST_NEW(Typespec, AK_Typespec_Pointer, pos);
|
||||
AST_NEW(Typespec, AST_TYPESPEC_POINTER, pos);
|
||||
result->base = base;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Typespec *
|
||||
ast_typespec_array(Token *pos, Ast_Typespec *base, Ast_Expr *expr){
|
||||
AST_NEW(Typespec, AK_Typespec_Array, pos);
|
||||
AST_NEW(Typespec, AST_TYPESPEC_ARRAY, pos);
|
||||
result->arr.base = base;
|
||||
result->arr.expr = expr;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Typespec *
|
||||
ast_typespec_func(Token *pos, Ast_Typespec *ret, Array<Ast_Typespec*> args){
|
||||
AST_NEW(Typespec, AK_Typespec_Func, pos);
|
||||
result->func.ret = ret;
|
||||
result->func.args = args.tight_copy(pctx->perm);
|
||||
ast_typespec_lambda(Token *pos, Ast_Lambda *lambda){
|
||||
AST_NEW(Typespec, AST_TYPESPEC_LAMBDA, pos);
|
||||
result->lambda = lambda;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -250,14 +300,14 @@ ast_typespec_func(Token *pos, Ast_Typespec *ret, Array<Ast_Typespec*> args){
|
||||
//-----------------------------------------------------------------------------
|
||||
function Ast_Decl *
|
||||
ast_decl_func(Token *pos, Intern_String name){
|
||||
AST_NEW(Decl, AK_Decl_Func, pos);
|
||||
AST_NEW(Decl, AST_LAMBDA, pos);
|
||||
result->name = name;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Decl *
|
||||
ast_decl_var(Token *pos, Ast_Typespec *typespec, Intern_String name, Ast_Expr *expr){
|
||||
AST_NEW(Decl, AK_Decl_Var, pos);
|
||||
AST_NEW(Decl, AST_VAR, pos);
|
||||
result->var.expr = expr;
|
||||
result->var.typespec = typespec;
|
||||
result->name = name;
|
||||
@@ -267,13 +317,13 @@ ast_decl_var(Token *pos, Ast_Typespec *typespec, Intern_String name, Ast_Expr *e
|
||||
function Ast_Decl *
|
||||
ast_decl_const(Token *pos, Intern_String name, Ast_Expr *expr){
|
||||
Ast_Decl *result = ast_decl_var(pos, 0, name, expr);
|
||||
result->kind = AK_Decl_Const;
|
||||
result->kind = AST_CONST;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Package *
|
||||
ast_package(Token *pos, String name, Array<Ast_Decl *> decls){
|
||||
AST_NEW(Package, AK_Package, pos);
|
||||
AST_NEW(Package, AST_PACKAGE, pos);
|
||||
result->decls = decls.tight_copy(pctx->perm);
|
||||
result->name = intern_string(&pctx->interns, name);
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user