Starting from scratch on smaller scale, typechecking global and constant variables, compound expressions for arrays
This commit is contained in:
102
new_ast.cpp
102
new_ast.cpp
@@ -5,20 +5,29 @@
|
||||
// Parser::ast_arena - arena for asts
|
||||
// Lexer::interns::string_allocator - arena for interns
|
||||
//
|
||||
Intern_String keyword_const;
|
||||
Intern_String keyword_struct;
|
||||
Intern_String keyword_union;
|
||||
Intern_String keyword_cast;
|
||||
Intern_String keyword_enum;
|
||||
|
||||
Intern_String intern_void;
|
||||
Intern_String intern_int;
|
||||
Intern_String intern_str;
|
||||
Intern_String intern_unsigned;
|
||||
|
||||
const U64 Parse_Ctx_ID = 115151;
|
||||
struct Parse_Ctx:Lexer{
|
||||
Arena ast_arena;
|
||||
Allocator *perm; // Stores: AST, tokens, interns
|
||||
Allocator *heap;
|
||||
|
||||
Map global_syms;
|
||||
Map type_map;
|
||||
|
||||
Token empty_token;
|
||||
S64 indent;
|
||||
S64 pt[256]; // precedence table
|
||||
Map type_map;
|
||||
|
||||
void init(){
|
||||
void init(Allocator *perm_allocator, Allocator *heap_allocator){
|
||||
const S64 addp = 1;
|
||||
const S64 mulp = 2;
|
||||
pt[TK_Add] = addp;
|
||||
@@ -26,17 +35,28 @@ struct Parse_Ctx:Lexer{
|
||||
pt[TK_Div] = mulp;
|
||||
pt[TK_Mul] = mulp;
|
||||
|
||||
arena_init(&ast_arena, "AST Arena"_s);
|
||||
lex_init(this);
|
||||
keyword_const = intern_string(&interns, "const"_s);
|
||||
perm = perm_allocator;
|
||||
heap = heap_allocator;
|
||||
|
||||
global_syms = {heap};
|
||||
type_map = {heap};
|
||||
|
||||
lex_init(perm, heap, this);
|
||||
keyword_struct= intern_string(&interns, "struct"_s);
|
||||
keyword_union = intern_string(&interns, "union"_s);
|
||||
keyword_cast = intern_string(&interns, "cast"_s);
|
||||
keyword_enum = intern_string(&interns, "enum"_s);
|
||||
interns.first_keyword = keyword_const.str;
|
||||
interns.first_keyword = keyword_struct.str;
|
||||
interns.last_keyword = keyword_enum.str;
|
||||
|
||||
intern_void = intern_string(&interns, "void"_s);
|
||||
intern_int = intern_string(&interns, "int"_s);
|
||||
intern_str = intern_string(&interns, "String"_s);
|
||||
intern_unsigned = intern_string(&interns, "unsigned"_s);
|
||||
}
|
||||
};
|
||||
|
||||
thread_local Parse_Ctx *pctx;
|
||||
//-----------------------------------------------------------------------------
|
||||
// AST
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -47,8 +67,11 @@ enum Ast_Kind{
|
||||
|
||||
AK_Expr_Str,
|
||||
AK_Expr_Int,
|
||||
AK_Expr_Cast,
|
||||
AK_Expr_Ident,
|
||||
AK_Expr_Binary,
|
||||
AK_Expr_CompoundItem,
|
||||
AK_Expr_Compound,
|
||||
|
||||
AK_Decl_Func,
|
||||
AK_Decl_Func_Arg,
|
||||
@@ -66,6 +89,7 @@ struct Ast{
|
||||
Token *pos;
|
||||
};
|
||||
|
||||
struct Ast_Typespec;
|
||||
struct Ast_Expr:Ast{
|
||||
union{
|
||||
Intern_String intern_val;
|
||||
@@ -75,6 +99,19 @@ struct Ast_Expr:Ast{
|
||||
Ast_Expr *left;
|
||||
Ast_Expr *right;
|
||||
} binary;
|
||||
struct{
|
||||
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;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -98,7 +135,6 @@ struct Ast_Decl:Ast{
|
||||
union{
|
||||
struct{
|
||||
Ast_Typespec *typespec;
|
||||
Intern_String name;
|
||||
Ast_Expr *expr;
|
||||
}var;
|
||||
struct{
|
||||
@@ -117,8 +153,7 @@ struct Ast_Package:Ast{
|
||||
// AST Constructors beginning with expressions
|
||||
//-----------------------------------------------------------------------------
|
||||
#define AST_NEW(T,ikind,ipos) \
|
||||
Get_Ctx(Parse_Ctx); \
|
||||
Ast_##T *result = exp_alloc_type(&ctx->ast_arena, Ast_##T); \
|
||||
Ast_##T *result = exp_alloc_type(pctx->perm, Ast_##T); \
|
||||
result->kind = ikind; \
|
||||
result->pos = ipos
|
||||
|
||||
@@ -152,6 +187,31 @@ ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_compound(Token *pos, Ast_Typespec *typespec, Array<Ast_Expr *> exprs){
|
||||
AST_NEW(Expr, AK_Expr_Compound, pos);
|
||||
result->compound.typespec = typespec;
|
||||
result->compound.exprs = exprs.tight_copy(pctx->perm);
|
||||
return result;
|
||||
}
|
||||
|
||||
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);
|
||||
result->compound_item.name = name;
|
||||
result->compound_item.index = index;
|
||||
result->compound_item.item = item;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Typespec *typespec){
|
||||
AST_NEW(Expr, AK_Expr_Cast, pos);
|
||||
result->cast.expr = expr;
|
||||
result->cast.typespec = typespec;
|
||||
return result;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Typespecs
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -177,6 +237,14 @@ ast_typespec_array(Token *pos, Ast_Typespec *base, Ast_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);
|
||||
return result;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Declarations
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -196,9 +264,17 @@ ast_decl_var(Token *pos, Ast_Typespec *typespec, Intern_String name, Ast_Expr *e
|
||||
return result;
|
||||
}
|
||||
|
||||
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;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Package *
|
||||
ast_package(Token *pos, String name){
|
||||
ast_package(Token *pos, String name, Array<Ast_Decl *> decls){
|
||||
AST_NEW(Package, AK_Package, pos);
|
||||
result->name = intern_string(&ctx->interns, name);
|
||||
result->decls = decls.tight_copy(pctx->perm);
|
||||
result->name = intern_string(&pctx->interns, name);
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user