Successful compile with new parser!

This commit is contained in:
Krzosa Karol
2022-05-26 14:49:50 +02:00
parent d9a80afa9e
commit c88b38cc44
3 changed files with 46 additions and 77 deletions

View File

@@ -38,7 +38,7 @@ token_get(S64 i = 0){
function Token *
token_next(){
Token *token = token_get();
if(token->kind == TK_NewLine) pctx->indent = token->indent;
if(token_is_scope(token)) pctx->indent = token->indent;
pctx->token_iter++;
return token;
}
@@ -81,42 +81,6 @@ token_expect(Token_Kind kind){
return 0;
}
// @note: right now we check if on downscope there is a end of file
// not sure if this is the right approach codewise but the fact
// that end of file is treated as end of scope feels intuitive
function Token *
token_is_scope(Token_Kind scope){
assert(scope == OPEN_SCOPE || scope == CLOSE_SCOPE || scope == SAME_SCOPE);
Token *token = token_get();
if(token->kind == TK_NewLine || token->kind == TK_End){
if (scope == OPEN_SCOPE && token->indent > pctx->indent) return token;
else if(scope == SAME_SCOPE && token->indent == pctx->indent) return token;
else if((scope == CLOSE_SCOPE) && ((token->indent < pctx->indent) || (token->kind == TK_End))) return token;
}
return 0;
}
function Token *
token_match_scope(Token_Kind scope){
Token *token = token_is_scope(scope);
if(token) return token_next();
return 0;
}
function Token *
token_expect_scope(Token_Kind scope){
assert(scope == OPEN_SCOPE || scope == CLOSE_SCOPE || scope == SAME_SCOPE);
Token *token = token_get();
if(token->kind == TK_NewLine || token->kind == TK_End){
if (scope == OPEN_SCOPE && token->indent > pctx->indent) return token_next();
else if(scope == SAME_SCOPE && token->indent == pctx->indent) return token_next();
else if((scope == CLOSE_SCOPE) && ((token->indent < pctx->indent) || (token->kind == TK_End))) return token_next();
else parsing_error(token, "Expected a scope of kind [%s]", token_kind_string(scope));
}
parsing_error(token, "Expected Scope[%s] got instead: [%s]", token_kind_string(scope).str, token_kind_string(token->kind).str);
return 0;
}
//-----------------------------------------------------------------------------
// Expression parsing
//-----------------------------------------------------------------------------
@@ -199,7 +163,7 @@ function Ast_Decl *parse_decl(B32);
function Ast_Block *
parse_block(){
Ast_Block *block = 0;
if(token_match_scope(OPEN_SCOPE)){
if(token_match(OPEN_SCOPE)){
Token *token_block = token_get();
Scratch scratch;
@@ -208,7 +172,7 @@ parse_block(){
Token *token = token_get();
if(token_match_keyword(keyword_return)){
AST_NEW(Return, AST_RETURN, token);
if(!token_is(TK_NewLine)) result->expr = parse_expr();
if(!token_is_scope(token_get())) result->expr = parse_expr();
stmts.add(result);
}
else{
@@ -216,8 +180,8 @@ parse_block(){
if(result) stmts.add(result);
else parsing_error(token, "Unexpected token while parsing statement");
}
} while(token_match_scope(SAME_SCOPE));
token_expect_scope(CLOSE_SCOPE);
} while(token_match(SAME_SCOPE));
token_expect(CLOSE_SCOPE);
block = ast_block(token_block, stmts);
}
return block;
@@ -391,6 +355,7 @@ parse_assign_expr(){
function Ast_Decl *
parse_decl(B32 is_global){
Ast_Decl *result = 0;
if(is_global) token_match(SAME_SCOPE);
if(token_is(TK_Identifier)){
if(is_global && pctx->indent != 0) parsing_error(token_get(), "Top level declarations shouldn't be indented");
Token *name = token_next();
@@ -424,10 +389,16 @@ parse_decl(B32 is_global){
function Ast_Package *
parse_file(){
Scratch scratch;
//
// @note: pop the first token which 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
//
Token *token = token_get();
Array<Ast_Decl *>decls = {scratch};
while(!token_is(TK_End)){
while(token_match(TK_NewLine));
token_expect(SAME_SCOPE);
Ast_Decl *decl = parse_decl(true);
if(!decl) break;
decls.add(decl);