Cleanup, There is no decl anymore, Ast_Named
This commit is contained in:
@@ -197,12 +197,12 @@ gen_ast(Ast *ast){
|
|||||||
Ast_End();
|
Ast_End();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ast_Begin(AST_VAR, Ast_Decl){
|
Ast_Begin(AST_VAR, Ast_Var){
|
||||||
Sym *sym = resolved_get(node);
|
Sym *sym = resolved_get(node);
|
||||||
gen_simple_decl(sym->type, node->name);
|
gen_simple_decl(sym->type, node->name);
|
||||||
if(node->var.expr){
|
if(node->expr){
|
||||||
gen(" = ");
|
gen(" = ");
|
||||||
gen_expr(node->var.expr);
|
gen_expr(node->expr);
|
||||||
}
|
}
|
||||||
gen(";");
|
gen(";");
|
||||||
Ast_End();
|
Ast_End();
|
||||||
@@ -241,12 +241,12 @@ gen_ast(Ast *ast){
|
|||||||
Ast_End();
|
Ast_End();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ast_Begin(AST_CONST, Ast_Decl){
|
Ast_Begin(AST_CONST, Ast_Const){
|
||||||
Sym *sym = resolved_get(node);
|
Sym *sym = resolved_get(node);
|
||||||
|
|
||||||
if(sym->type->kind == TYPE_Lambda){
|
if(sym->type->kind == TYPE_Lambda){
|
||||||
if(node->var.expr->kind == AST_LAMBDA){
|
if(node->expr->kind == AST_LAMBDA){
|
||||||
Ast_Lambda *lambda = (Ast_Lambda *)node->var.expr;
|
Ast_Lambda *lambda = (Ast_Lambda *)node->expr;
|
||||||
gen("static ");
|
gen("static ");
|
||||||
gen_simple_decl(lambda->ret->resolved_type, node->name);
|
gen_simple_decl(lambda->ret->resolved_type, node->name);
|
||||||
gen("(");
|
gen("(");
|
||||||
@@ -265,7 +265,7 @@ gen_ast(Ast *ast){
|
|||||||
else{
|
else{
|
||||||
gen_simple_decl(sym->type, node->name);
|
gen_simple_decl(sym->type, node->name);
|
||||||
gen(" = ");
|
gen(" = ");
|
||||||
gen_expr(node->var.expr);
|
gen_expr(node->expr);
|
||||||
gen(";");
|
gen(";");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -316,6 +316,7 @@ function String
|
|||||||
compile_file(String filename){
|
compile_file(String filename){
|
||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
String filecontent = os_read_file(scratch, filename);
|
String filecontent = os_read_file(scratch, filename);
|
||||||
|
assert(filecontent.len);
|
||||||
String result = compile_string(filecontent, filename);
|
String result = compile_string(filecontent, filename);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
14
lambdas.kl
14
lambdas.kl
@@ -1,11 +1,19 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
@todo!!!
|
||||||
|
I think it might be better to try doing order indepent decls
|
||||||
|
as well as the tree transformations in the backend code
|
||||||
|
plus some cleanups before adding more complications to the code
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
if_stmt :: (cond: int): int
|
if_stmt :: (cond: int): int
|
||||||
CONSTANT :: 10
|
CONSTANT :: 10
|
||||||
|
thing := 10
|
||||||
if i := cond, cond + CONSTANT
|
if i := thing + cond, cond + CONSTANT
|
||||||
return i + CONSTANT
|
return i + CONSTANT
|
||||||
else if cond - CONSTANT
|
else if cond - CONSTANT
|
||||||
return cond - CONSTANT
|
return i - CONSTANT
|
||||||
else
|
else
|
||||||
return CONSTANT
|
return CONSTANT
|
||||||
|
|
||||||
|
|||||||
3
main.cpp
3
main.cpp
@@ -19,9 +19,10 @@ int main(){
|
|||||||
test_intern_table();
|
test_intern_table();
|
||||||
lex_test();
|
lex_test();
|
||||||
|
|
||||||
String result = compile_file("lambdas.kl"_s);
|
String result = compile_file("order_independent_globals.kl"_s);
|
||||||
printf("%s", result.str);
|
printf("%s", result.str);
|
||||||
|
|
||||||
|
compile_file("lambdas.kl"_s);
|
||||||
compile_file("globals.kl"_s);
|
compile_file("globals.kl"_s);
|
||||||
__debugbreak();
|
__debugbreak();
|
||||||
}
|
}
|
||||||
|
|||||||
57
new_ast.cpp
57
new_ast.cpp
@@ -113,6 +113,7 @@ struct Ast{
|
|||||||
bool is_stmt: 1;
|
bool is_stmt: 1;
|
||||||
bool is_expr: 1;
|
bool is_expr: 1;
|
||||||
bool is_decl: 1;
|
bool is_decl: 1;
|
||||||
|
bool is_named: 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Ast_Resolved_Type;
|
struct Ast_Resolved_Type;
|
||||||
@@ -209,19 +210,24 @@ struct Ast_Typespec:Ast{
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Ast_Decl:Ast{
|
struct Ast_Named:Ast{
|
||||||
Intern_String name;
|
Intern_String name;
|
||||||
union{
|
};
|
||||||
struct{
|
|
||||||
Ast_Typespec *typespec;
|
struct Ast_Var: Ast_Named{
|
||||||
Ast_Expr *expr;
|
Intern_String name;
|
||||||
}var;
|
Ast_Typespec *typespec;
|
||||||
};
|
Ast_Expr *expr;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Ast_Const: Ast_Named{
|
||||||
|
Intern_String name;
|
||||||
|
Ast_Expr *expr;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Ast_Package:Ast{
|
struct Ast_Package:Ast{
|
||||||
Intern_String name;
|
Intern_String name;
|
||||||
Array<Ast_Decl *> decls;
|
Array<Ast_Named *> decls;
|
||||||
};
|
};
|
||||||
|
|
||||||
function Ast_Typespec *ast_typespec_name(Token *pos, Intern_String name);
|
function Ast_Typespec *ast_typespec_name(Token *pos, Intern_String name);
|
||||||
@@ -398,31 +404,26 @@ ast_typespec_lambda(Token *pos, Ast_Lambda *lambda){
|
|||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Declarations
|
// Declarations
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
function Ast_Decl *
|
|
||||||
ast_decl_func(Token *pos, Intern_String name){
|
function Ast_Var *
|
||||||
AST_NEW(Decl, AST_LAMBDA, pos);
|
ast_var(Token *pos, Ast_Typespec *typespec, Intern_String name, Ast_Expr *expr){
|
||||||
|
AST_NEW(Var, AST_VAR, pos);
|
||||||
|
result->expr = expr;
|
||||||
|
result->typespec = typespec;
|
||||||
|
result->name = name;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function Ast_Const *
|
||||||
|
ast_const(Token *pos, Intern_String name, Ast_Expr *expr){
|
||||||
|
AST_NEW(Const, AST_CONST, pos);
|
||||||
|
result->expr = expr;
|
||||||
result->name = name;
|
result->name = name;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Ast_Decl *
|
|
||||||
ast_decl_var(Token *pos, Ast_Typespec *typespec, Intern_String name, Ast_Expr *expr){
|
|
||||||
AST_NEW(Decl, AST_VAR, pos);
|
|
||||||
result->var.expr = expr;
|
|
||||||
result->var.typespec = typespec;
|
|
||||||
result->name = name;
|
|
||||||
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 = AST_CONST;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function Ast_Package *
|
function Ast_Package *
|
||||||
ast_package(Token *pos, String name, Array<Ast_Decl *> decls){
|
ast_package(Token *pos, String name, Array<Ast_Named *> decls){
|
||||||
AST_NEW(Package, AST_PACKAGE, pos);
|
AST_NEW(Package, AST_PACKAGE, pos);
|
||||||
result->decls = decls.tight_copy(pctx->perm);
|
result->decls = decls.tight_copy(pctx->perm);
|
||||||
result->name = intern_string(&pctx->interns, name);
|
result->name = intern_string(&pctx->interns, name);
|
||||||
|
|||||||
@@ -177,7 +177,22 @@ parse_optional_type(){
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Ast_Decl *parse_decl(B32);
|
function Ast_Init *
|
||||||
|
parse_init_stmt(Ast_Expr *expr){
|
||||||
|
Token *token = token_get();
|
||||||
|
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(token_match(TK_Assign)){
|
||||||
|
Ast_Expr *value = parse_expr();
|
||||||
|
Ast_Init *result = ast_init(token, TK_Comma, (Ast_Atom *)expr, value);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
else not_implemented;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function Ast_Named *parse_named(B32);
|
||||||
function Ast_Block *
|
function Ast_Block *
|
||||||
parse_block(){
|
parse_block(){
|
||||||
Ast_Block *block = 0;
|
Ast_Block *block = 0;
|
||||||
@@ -195,24 +210,11 @@ 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_Init *init_val = 0;
|
Ast_Init *init_val = parse_init_stmt(expr);
|
||||||
|
if(init_val){
|
||||||
{
|
if(token_match(TK_Comma)) expr = parse_expr();
|
||||||
Token *token = token_get();
|
else expr = 0;
|
||||||
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(token_match(TK_Assign)){
|
|
||||||
Ast_Expr *value = parse_expr();
|
|
||||||
assert(expr->kind == AST_IDENT);
|
|
||||||
init_val = ast_init(token, TK_Comma, (Ast_Atom *)expr, value);
|
|
||||||
}
|
|
||||||
else not_implemented;
|
|
||||||
|
|
||||||
if(token_match(TK_Comma)){
|
|
||||||
expr = parse_expr();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ast_Block *block = parse_block();
|
Ast_Block *block = parse_block();
|
||||||
@@ -240,7 +242,7 @@ parse_block(){
|
|||||||
|
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
Ast_Decl *result = parse_decl(false);
|
Ast_Named *result = parse_named(false);
|
||||||
if(result) stmts.add(result);
|
if(result) stmts.add(result);
|
||||||
else parsing_error(token, "Unexpected token [%s] while parsing statement", token_kind_string(token->kind).str);
|
else parsing_error(token, "Unexpected token [%s] while parsing statement", token_kind_string(token->kind).str);
|
||||||
}
|
}
|
||||||
@@ -416,16 +418,16 @@ parse_assign_expr(){
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Ast_Decl *
|
function Ast_Named *
|
||||||
parse_decl(B32 is_global){
|
parse_named(B32 is_global){
|
||||||
Ast_Decl *result = 0;
|
Ast_Named *result = 0;
|
||||||
if(is_global) token_match(SAME_SCOPE);
|
if(is_global) token_match(SAME_SCOPE);
|
||||||
if(token_is(TK_Identifier)){
|
if(token_is(TK_Identifier)){
|
||||||
if(is_global && pctx->indent != 0) parsing_error(token_get(), "Top level declarations shouldn't be indented");
|
if(is_global && pctx->indent != 0) parsing_error(token_get(), "Top level declarations shouldn't be indented");
|
||||||
Token *name = token_next();
|
Token *name = token_next();
|
||||||
if(token_match(TK_DoubleColon)){ // Constant
|
if(token_match(TK_DoubleColon)){ // Constant
|
||||||
Ast_Expr *expr = parse_expr();
|
Ast_Expr *expr = parse_expr();
|
||||||
result = ast_decl_const(name, name->intern_val, expr);
|
result = ast_const(name, name->intern_val, expr);
|
||||||
}
|
}
|
||||||
else if(token_match(TK_Colon)){
|
else if(token_match(TK_Colon)){
|
||||||
Ast_Typespec *typespec = 0;
|
Ast_Typespec *typespec = 0;
|
||||||
@@ -434,7 +436,7 @@ parse_decl(B32 is_global){
|
|||||||
if(token_match(TK_Assign)) expr = parse_expr();
|
if(token_match(TK_Assign)) expr = parse_expr();
|
||||||
if(!expr && !typespec) parsing_error(name, "invalid declaration, no type or value");
|
if(!expr && !typespec) parsing_error(name, "invalid declaration, no type or value");
|
||||||
|
|
||||||
result = ast_decl_var(name, typespec, name->intern_val, expr);
|
result = ast_var(name, typespec, name->intern_val, expr);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
Token *token = token_get();
|
Token *token = token_get();
|
||||||
@@ -455,16 +457,19 @@ parse_file(){
|
|||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
|
|
||||||
//
|
//
|
||||||
// @note: pop the first token which which always should be an indentation token,
|
// @note: pop the first token with token_next() / token_expect()
|
||||||
// it updates the indent info on the parser, making sure that indentation on
|
// which always should be an indentation token,
|
||||||
|
// it updates the indent info on the parser,
|
||||||
|
// making sure that indentation on
|
||||||
// the first line is properly updated
|
// the first line is properly updated
|
||||||
//
|
//
|
||||||
Token *token = token_get();
|
Token *token = token_get();
|
||||||
Array<Ast_Decl *>decls = {scratch};
|
Array<Ast_Named *>decls = {scratch};
|
||||||
while(!token_is(TK_End)){
|
while(!token_is(TK_End)){
|
||||||
token_expect(SAME_SCOPE);
|
token_expect(SAME_SCOPE);
|
||||||
Ast_Decl *decl = parse_decl(true);
|
Ast_Named *decl = parse_named(true);
|
||||||
if(!decl) break;
|
if(!decl) break;
|
||||||
|
|
||||||
decls.add(decl);
|
decls.add(decl);
|
||||||
}
|
}
|
||||||
Ast_Package *result = ast_package(token, token->file, decls);
|
Ast_Package *result = ast_package(token, token->file, decls);
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ struct Operand{
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
global Ast_Decl empty_decl = {};
|
global Ast_Named empty_decl = {};
|
||||||
|
|
||||||
function void
|
function void
|
||||||
sym_insert(Sym *sym){
|
sym_insert(Sym *sym){
|
||||||
@@ -202,12 +202,12 @@ eval_stmt(Ast *ast, Ast_Resolved_Type *ret){
|
|||||||
Ast_End();
|
Ast_End();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ast_Begin(AST_VAR, Ast_Decl){
|
Ast_Begin(AST_VAR, Ast_Var){
|
||||||
eval_decl(node);
|
eval_decl(node);
|
||||||
Ast_End();
|
Ast_End();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ast_Begin(AST_CONST, Ast_Decl){
|
Ast_Begin(AST_CONST, Ast_Const){
|
||||||
eval_decl(node);
|
eval_decl(node);
|
||||||
Ast_End();
|
Ast_End();
|
||||||
}
|
}
|
||||||
@@ -226,8 +226,8 @@ eval_stmt(Ast *ast, Ast_Resolved_Type *ret){
|
|||||||
|
|
||||||
Ast_Begin(AST_IF, Ast_If){
|
Ast_Begin(AST_IF, Ast_If){
|
||||||
For(node->ifs){
|
For(node->ifs){
|
||||||
if(it[0]->expr) eval_expr(it[0]->expr);
|
|
||||||
if(it[0]->init) eval_stmt(it[0]->init, ret);
|
if(it[0]->init) eval_stmt(it[0]->init, ret);
|
||||||
|
if(it[0]->expr) eval_expr(it[0]->expr);
|
||||||
S64 scope_index = scope_push();
|
S64 scope_index = scope_push();
|
||||||
For_It(it[0]->block->stmts, jt){
|
For_It(it[0]->block->stmts, jt){
|
||||||
eval_stmt(jt[0], ret);
|
eval_stmt(jt[0], ret);
|
||||||
@@ -454,9 +454,9 @@ eval_decl(Ast *ast){
|
|||||||
Ast_End();
|
Ast_End();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ast_Begin(AST_VAR, Ast_Decl){
|
Ast_Begin(AST_VAR, Ast_Var){
|
||||||
Ast_Resolved_Type *type = eval_typespec(node->var.typespec);
|
Ast_Resolved_Type *type = eval_typespec(node->typespec);
|
||||||
Operand expr = node->var.expr ? eval_expr(node->var.expr, type) : Operand{};
|
Operand expr = node->expr ? eval_expr(node->expr, type) : Operand{};
|
||||||
Ast_Resolved_Type *resolved_type = resolve_type_pair(node->pos, type, expr.type);
|
Ast_Resolved_Type *resolved_type = resolve_type_pair(node->pos, type, expr.type);
|
||||||
|
|
||||||
Sym *sym = sym_new(SYM_Var, node->name, resolved_type, node);
|
Sym *sym = sym_new(SYM_Var, node->name, resolved_type, node);
|
||||||
@@ -464,13 +464,11 @@ eval_decl(Ast *ast){
|
|||||||
Ast_End();
|
Ast_End();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ast_Begin(AST_CONST, Ast_Decl){
|
Ast_Begin(AST_CONST, Ast_Const){
|
||||||
Ast_Resolved_Type *type = eval_typespec(node->var.typespec);
|
Operand expr = eval_expr(node->expr);
|
||||||
if(type && type->kind == TYPE_Pointer) parsing_error(node->pos, "Const cant be a pointer");
|
|
||||||
Operand expr = eval_expr(node->var.expr);
|
|
||||||
if(!expr.type) parsing_error(node->pos, "Constant value without expression");
|
if(!expr.type) parsing_error(node->pos, "Constant value without expression");
|
||||||
if(!expr.is_const) parsing_error(node->pos, "Value of constant variable is not a constant expression");
|
if(!expr.is_const) parsing_error(node->pos, "Value of constant variable is not a constant expression");
|
||||||
Ast_Resolved_Type *resolved_type = resolve_type_pair(node->pos, type, expr.type);
|
Ast_Resolved_Type *resolved_type = expr.type;
|
||||||
|
|
||||||
Sym *sym = sym_new(SYM_Const, node->name, resolved_type, node);
|
Sym *sym = sym_new(SYM_Const, node->name, resolved_type, node);
|
||||||
if(resolved_type == type_int) sym->int_val = expr.int_val;
|
if(resolved_type == type_int) sym->int_val = expr.int_val;
|
||||||
|
|||||||
4
order_independent_globals.kl
Normal file
4
order_independent_globals.kl
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
DEPENDENCE :: CONSTANT_VAL
|
||||||
|
CONSTANT_VAL :: 10
|
||||||
|
|
||||||
Reference in New Issue
Block a user