Work on lambda body

This commit is contained in:
Krzosa Karol
2022-05-24 23:35:49 +02:00
parent 52910d5606
commit 9dc2e1588d
5 changed files with 97 additions and 18 deletions

View File

@@ -7,6 +7,10 @@
//
Intern_String keyword_struct;
Intern_String keyword_union;
Intern_String keyword_return;
Intern_String keyword_if;
Intern_String keyword_else;
Intern_String keyword_for;
Intern_String keyword_cast;
Intern_String keyword_enum;
@@ -15,7 +19,7 @@ Intern_String intern_int;
Intern_String intern_str;
Intern_String intern_unsigned;
const U64 Parse_Ctx_ID = 115151;
struct Sym;
struct Parse_Ctx:Lexer{
Allocator *perm; // Stores: AST, tokens, interns
Allocator *heap;
@@ -23,6 +27,7 @@ struct Parse_Ctx:Lexer{
U64 unique_ids;
Map global_syms;
Map type_map;
Array<Sym *> local_syms;
Token empty_token;
S64 indent;
@@ -39,9 +44,13 @@ struct Parse_Ctx:Lexer{
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_struct= intern("struct"_s);
keyword_union = intern("union"_s);
keyword_cast = intern("cast"_s);
keyword_return = intern("return"_s);
keyword_if = intern("if"_s);
keyword_else = intern("else"_s);
keyword_for = intern("for"_s);
keyword_enum = intern_string(&interns, "enum"_s);
interns.first_keyword = keyword_struct.str;
interns.last_keyword = keyword_enum.str;
@@ -72,6 +81,8 @@ enum Ast_Kind{
AST_COMPOUND_ITEM,
AST_COMPOUND,
AST_RETURN,
AST_BLOCK,
AST_LAMBDA,
AST_LAMBDA_ARG,
AST_CONST,
@@ -101,6 +112,7 @@ struct Ast_Atom: Ast_Expr{
};
struct Ast_Compound_Item: Ast_Expr{
// @todo: Ast_Expr to Ast_Atom
Ast_Expr *name; // index | name
Ast_Expr *index;
Ast_Expr *item;
@@ -137,6 +149,10 @@ struct Ast_Block : Ast {
Array<Ast *> stmts;
};
struct Ast_Return: Ast{
Ast_Expr *expr;
};
struct Ast_Lambda_Arg: Ast_Expr{
Intern_String name;
Ast_Typespec *typespec;
@@ -177,6 +193,7 @@ struct Ast_Package:Ast{
Array<Ast_Decl *> decls;
};
function Ast_Typespec *ast_typespec_name(Token *pos, Intern_String name);
//-----------------------------------------------------------------------------
// AST Constructors beginning with expressions
//-----------------------------------------------------------------------------
@@ -262,6 +279,9 @@ ast_lambda(Token *pos, Array<Ast_Lambda_Arg *> params, Ast_Typespec *ret, Ast_Bl
AST_NEW(Lambda, AST_LAMBDA, pos);
result->args = params.tight_copy(pctx->perm);
result->ret = ret;
if(!ret){
result->ret = ast_typespec_name(0, intern_void);
}
return result;
}
@@ -279,6 +299,13 @@ ast_expr_lambda_arg(Token *pos, Intern_String name, Ast_Typespec *typespec){
return result;
}
function Ast_Block *
ast_block(Token *pos, Array<Ast *> stmts){
AST_NEW(Block, AST_BLOCK, pos);
result->stmts = stmts.tight_copy(pctx->perm);
return result;
}
//-----------------------------------------------------------------------------
// Typespecs
//-----------------------------------------------------------------------------

View File

@@ -47,6 +47,8 @@ enum Token_Kind{
TK_Dot,
TK_NewLine,
TK_NewUpScope,
TK_NewDownScope,
TK_Colon,
TK_Assign,
@@ -523,6 +525,17 @@ lex_restream(Lexer *lexer, String istream, String file){
lexer->tokens.clear();
lexer->token_iter = 0;
lex__stream(&lexer->interns, &lexer->tokens, &lexer->stream);
S32 indent = 0;
For(lexer->tokens){
if(it->kind == TK_NewLine){
if(it->indent > indent)
it->kind = TK_NewUpScope;
if(it->indent < indent)
it->kind = TK_NewDownScope;
indent = it->indent;
}
}
}
function Lexer
@@ -640,17 +653,19 @@ token_kind_string(Token_Kind kind){
case TK_LeftShift: return "<<"_s;
case TK_RightShift: return ">>"_s;
case TK_Arrow: return "->"_s;
case TK_NewLine: return "NewLine"_s;
case TK_NewLine: return "New_Line"_s;
case TK_ExprSizeof: return "sizeof"_s;
case TK_DocComment: return "DocComment"_s;
case TK_DocComment: return "Doc_Comment"_s;
case TK_Comment: return "Comment"_s;
case TK_Identifier: return "Identifier"_s;
case TK_StringLit: return "StringLit"_s;
case TK_StringLit: return "String_Lit"_s;
case TK_Character: return "Character"_s;
case TK_Error: return "Error"_s;
case TK_Float: return "Float"_s;
case TK_Integer: return "Int"_s;
case TK_Keyword: return "Keyword"_s;
case TK_NewUpScope: return "New_Up_Scope"_s;
case TK_NewDownScope: return "New_Down_Scope"_s;
default: invalid_codepath; return "<Undefined>"_s;
}
}

View File

@@ -58,7 +58,7 @@ parsing_error(Token *token, const char *str, ...){
// @Note(Krzosa): Print nice error message
printf("\nError: %s", string.str);
if(token){
printf(" %s:%d\n", token->file.str, (S32)token->line);
printf(" %s:%d\n", token->file.str, (S32)token->line + 1);
// @Note(Krzosa): Print error line
{
@@ -165,15 +165,35 @@ parse_optional_type(){
return result;
}
function Ast_Block *
parse_block(){
Ast_Block *block = 0;
if(token_match(TK_NewUpScope)){
Token *token_block = token_get();
// function Ast_Block *
// parse_block(){
// Ast_Block *result = 0;
// if(token_match(TK_NewScope)){
Scratch scratch;
Array<Ast *> stmts = {scratch};
do{
Token *token = token_get();
if(token_match_keyword(keyword_return)){
AST_NEW(Return, AST_RETURN, token);
result->expr = parse_expr();
stmts.add(result);
}
else{
// @todo
// Probably want to rewrite parse decls to allow for
// calling from other places, dont want to error messages
// to suffer though!!!
parsing_error(token, "Unexpected token while parsing statement");
}
} while(token_match(TK_NewLine));
token_expect(TK_NewDownScope);
// }
// return result;
// }
block = ast_block(token_block, stmts);
}
return block;
}
function Ast_Lambda *
@@ -198,7 +218,7 @@ parse_lambda(Token *token, B32 is_typespec = false){ // @Todo(Krzosa): is_typesp
token_expect(TK_CloseParen);
Ast_Typespec *ret = parse_optional_type();
Ast_Block *block = 0;
Ast_Block *block = parse_block();
Ast_Lambda *result = ast_lambda(token, params, ret, block);
return result;
}
@@ -392,7 +412,10 @@ parse_decl(){
Token *token = token_get();
parsing_error(token, "Unexpected token: [%s] when parsing a declaration", token_kind_string(token->kind).str);
}
}
else if(!token_is(TK_End)){
Token *token = token_get();
parsing_error(token, "Unexpected token: [%s] when parsing a declaration", token_kind_string(token->kind).str);
}
return result;
}

View File

@@ -28,6 +28,8 @@ struct Operand{
};
};
global Ast_Decl empty_decl = {};
function void
sym_insert(Sym *sym){
U64 hash = hash_string(sym->name.s);
@@ -55,7 +57,6 @@ sym_new(Sym_Kind kind, Intern_String name, Ast_Resolved_Type *type, Ast_Decl *de
return result;
}
global Ast_Decl empty_decl = {};
function void
sym_insert_builtin_type(String name, Ast_Resolved_Type *type){
Intern_String string = intern_string(&pctx->interns, name);
@@ -224,6 +225,16 @@ eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type){
Ast_Begin(AST_LAMBDA, Ast_Lambda){
Ast_Resolved_Type *type = eval_typespec(ast_typespec_lambda(0, node));
// @todo: typecheck the function
// enter scope -
// push local syms etc.
// Make sure return type is matching function return type
// quit scope
For(node->block->stmts){
}
return {type, true};
Ast_End();
}

View File

@@ -20,6 +20,9 @@ max :: (a: int, b: int)
{ and } - treated as new line scope and end of new line scope
*/
arena_push :: (size: int)
return size + 10
//-----------------------------------------------------------------------------
// Function types
//-----------------------------------------------------------------------------