diff --git a/build.bat b/build.bat index 6c53d87..254ebe3 100644 --- a/build.bat +++ b/build.bat @@ -1,5 +1,7 @@ @echo off pushd %~dp0 -clang main.cpp -O0 -I.. -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib +rem @todo fix msvc +cl main.cpp -I.. user32.lib +rem clang main.cpp -O0 -I.. -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib popd diff --git a/lambdas.kl b/lambdas.kl index a8a2e45..73a7a76 100644 --- a/lambdas.kl +++ b/lambdas.kl @@ -1,25 +1,9 @@ -/* - thing // indent 2 == error - - -add_10 :: (size: int): int // scope 0 - add_20 :: (new_size: int): int // up scope 2 - return 20 // up scope 2 - // down scope -// down scope -// scope 0 -thing - - - - -*/ add_10 :: (size: int): int add_20 :: (new_size: int): int return 20 - result := size + 10 + constant :: 20; result := constant + 10 return result return_constant :: (): int diff --git a/main.obj b/main.obj new file mode 100644 index 0000000..c654b32 Binary files /dev/null and b/main.obj differ diff --git a/new_lex.cpp b/new_lex.cpp index caf234f..e15ed1f 100644 --- a/new_lex.cpp +++ b/new_lex.cpp @@ -291,7 +291,7 @@ lex_last_indent_token(Lex_Stream *s){ } function B32 -token_is_scope(Token *t){ +lex_is_scope(Token *t){ B32 result = t->kind == OPEN_SCOPE || t->kind == CLOSE_SCOPE || t->kind == SAME_SCOPE; return result; } @@ -303,7 +303,7 @@ lex__stream(Intern_Table *table, Array *array, Lex_Stream *s){ if(s->iter >= s->stream.len) // End of stream break; - // @note: for now the lexer is going to be a 2 stage process + // @note: the lexer is going to be a 2 stage process // first we tokenize the indentation and then proceed to tokenize // the good stuff @@ -326,6 +326,7 @@ lex__stream(Intern_Table *table, Array *array, Lex_Stream *s){ switch(lexc(s)){ case '\t': case ' ': lex_advance(s); t.indent++; break; case '\r': lex_advance(s); break; + case '/': { if(lexci(s,1) == '/'){ lex_advance(s); lex_advance(s); @@ -351,11 +352,34 @@ lex__stream(Intern_Table *table, Array *array, Lex_Stream *s){ } } } break; + + case ';' : { + Token semi = token_make(lexcp(s), s->file, s->line, s->line_begin); + Token *last = lex_last_indent_token(s); + semi.kind = SAME_SCOPE; + semi.indent = last->indent; + lex_advance(s); + array->add(semi); + } break; + case '\n':{ lex_advance(s); should_emit = true; t = token_make(lexcp(s), s->file, s->line, s->line_begin); } break; + + // @todo: add open and close brace handling as OPEN_SCOPE CLOSE_SCOPE + // when it comes to compound statements it's going to check for scopes + // and then it's going to specialize and look for brace string + + // case '{': { + // s->inside_brace_paren++; t.kind = TK_OpenBrace; + // } break; + // case '}': { + // s->inside_brace_paren--; + // t.kind = CLOSE_SCOPE; + // } break; + default:{ if(s->inside_brace_paren) should_emit = false; if(should_emit){ @@ -365,10 +389,11 @@ lex__stream(Intern_Table *table, Array *array, Lex_Stream *s){ array->add(t); s->indent_stack.add(array->last()); } + else if(t.indent < last->indent){ for(S64 i = s->indent_stack.len-1; i >= 0; i-=1){ auto it = s->indent_stack.data[i]; - assert(token_is_scope(it)); + assert(lex_is_scope(it)); if(it->indent == t.indent){ t.kind = SAME_SCOPE; array->add(t); @@ -422,10 +447,6 @@ lex__stream(Intern_Table *table, Array *array, Lex_Stream *s){ CASE3('+', TK_Add, TK_AddAssign, TK_Increment); CASE3('&', TK_BitAnd, TK_AndAssign, TK_And); CASE3('|', TK_BitOr, TK_OrAssign, TK_Or); - case ';': { - t.kind = TK_Semicolon; - }break; - case '.': { if(lexc(s) == '.' && lexci(s,1) == '.') { lex_advance(s); lex_advance(s); @@ -608,7 +629,7 @@ lex_stream(Allocator *token_string_arena, Allocator *map_allocator, String istre function void lex_test(){ Scratch scratch; - String test = "Keyword //R\n 18446744073709551616\n {}\n)(@?&+-;....->,:::/**/\"Thing\" Thingy" + String test = "Keyword //R\n 18446744073709551616\n {}\n)(@?&+-....->,:::/**/\"Thing\" Thingy" "\"Test_Meme\"+=-===42524 4294967295 18446744073709551615" "for if while switch :="_s; @@ -627,7 +648,7 @@ lex_test(){ Token_Kind kind[] = { SAME_SCOPE, TK_Keyword, OPEN_SCOPE, TK_Error, OPEN_SCOPE, TK_OpenBrace,TK_CloseBrace,CLOSE_SCOPE, CLOSE_SCOPE, SAME_SCOPE, TK_CloseParen,TK_OpenParen, - TK_At,TK_Question,TK_BitAnd,TK_Add,TK_Sub,TK_Semicolon, + TK_At,TK_Question,TK_BitAnd,TK_Add,TK_Sub, TK_ThreeDots, TK_Dot, TK_Arrow, TK_Comma, TK_DoubleColon, TK_Colon, TK_StringLit, TK_Identifier, TK_StringLit, TK_AddAssign, TK_SubAssign, TK_Equals, TK_Integer, TK_Integer, TK_Integer, @@ -636,7 +657,7 @@ lex_test(){ }; String strs[] = { ""_s, "Keyword"_s, ""_s, "18446744073709551616"_s, ""_s, "{"_s,"}"_s, ""_s, ""_s, ""_s, ")"_s, "("_s, - "@"_s,"?"_s,"&"_s,"+"_s,"-"_s,";"_s, + "@"_s,"?"_s,"&"_s,"+"_s,"-"_s, "..."_s,"."_s,"->"_s,","_s,"::"_s,":"_s, "Thing"_s,"Thingy"_s,"Test_Meme"_s, "+="_s,"-="_s, "=="_s,"42524"_s,"4294967295"_s,"18446744073709551615"_s, diff --git a/new_parse.cpp b/new_parse.cpp index de8506d..ba46168 100644 --- a/new_parse.cpp +++ b/new_parse.cpp @@ -35,10 +35,17 @@ token_get(S64 i = 0){ return result; } +function Token * +token_is_scope(){ + Token *token = token_get(); + if(lex_is_scope(token)) return token; + return 0; +} + function Token * token_next(){ Token *token = token_get(); - if(token_is_scope(token)) pctx->indent = token->indent; + if(lex_is_scope(token)) pctx->indent = token->indent; pctx->token_iter++; return token; } @@ -172,7 +179,7 @@ parse_block(){ Token *token = token_get(); if(token_match_keyword(keyword_return)){ AST_NEW(Return, AST_RETURN, token); - if(!token_is_scope(token_get())) result->expr = parse_expr(); + if(!token_is_scope()) result->expr = parse_expr(); stmts.add(result); } else{ diff --git a/new_resolve.cpp b/new_resolve.cpp index 6d96f33..8bc0477 100644 --- a/new_resolve.cpp +++ b/new_resolve.cpp @@ -222,13 +222,15 @@ function Operand eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type){ switch(ast->kind){ Ast_Begin(AST_INT, Ast_Atom){ - Operand result = {type_int, true, {.int_val=(S64)node->int_val}}; + Operand result = {type_int, true}; + result.int_val = node->int_val; return result; Ast_End(); } Ast_Begin(AST_STR, Ast_Atom){ - Operand result = {type_string, true, {.intern_val = node->intern_val}}; + Operand result = {type_string, true}; + result.intern_val = node->intern_val; return result; Ast_End(); }