diff --git a/ccodegen.cpp b/ccodegen.cpp index 90f1155..e98cfa5 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -132,8 +132,10 @@ gen_expr(Ast_Expr *ast){ } CASE(UNARY, Unary){ - gen("(%s", name(node->op)); + gen("("); + if(node->op != TK_PostIncrement && node->op != TK_PostDecrement) gen("%s", name(node->op)); gen_expr(node->expr); + if(node->op == TK_PostIncrement || node->op == TK_PostDecrement) gen("%s", name(node->op)); gen(")"); BREAK(); } @@ -388,7 +390,11 @@ gen_ast(Ast *ast){ BREAK(); } - invalid_default_case; + default: { + assert(is_flag_set(ast->flags, AST_EXPR)); + gen_expr((Ast_Expr *)ast); + gen(";"); + } } } diff --git a/main.cpp b/main.cpp index 869f3cd..b8e702a 100644 --- a/main.cpp +++ b/main.cpp @@ -77,11 +77,11 @@ Expr: @todo -[ ] - Make sure pointer arithmetic works +[ ] - We need ++ -- operators [ ] - Passing down program to compile through command line -[ ] - More for loop variations -[ ] - Write up on order independent declarations [ ] - Switch +[ ] - Arrays with size passed +[ ] - Some way to call foreign functions [ ] - Comma notation when declaring variables thing1, thing2: S32 [ ] - Array of inferred size @@ -95,6 +95,7 @@ Expr: [ ] - Order independent constants in structs [ ] - Fix recursive lambdas in structs [ ] - Fixing access to functions/structs, in C we cant have functons inside of structs / functions so we need to rewrite the tree +[ ] - Write up on order independent declarations [ ] - Casting to basic types by call S64(x) [ ] - Default values in structs??? Should compound stmts bring values from default values?? Maybe not? Whats the alternative @@ -105,6 +106,7 @@ Expr: [ ] - Constant arrays that evaluate fully at compile time [ ] - Rust like enum where you associate values(other structs) with keys [ ] - Compound that zeros values - .{} , Compound that assumes defaults from struct definition - {} +[ ] - Inject stack traces into the program @donzo [x] - We are parsing wrong here: (t.str=(&string_to_lex.str)[i]); @@ -113,11 +115,13 @@ Expr: [x] - More basic types [x] - Implementing required operations int128 [x] - Fix casting +[x] - More for loop variations [x] - Add basic support for floats [x] - Converting from U64 token to S64 Atom introduces unnanounced error (negates) - probably need big int [x] - Add basic setup for new type system [x] - Access through struct names to constants Arena.CONSTANT [x] - Enums +[x] - Make sure pointer arithmetic works [x] - Initial for loop [x] - Enum . access to values [x] - Character literal @@ -190,7 +194,7 @@ int main(){ assert(f); fprintf(f, "%.*s", (int)result.len, result.str); fclose(f); - system("clang.exe program.c -g -o program.exe && program.exe"); + system("clang.exe program.c -g -o program.exe"); #endif __debugbreak(); diff --git a/new_parse.cpp b/new_parse.cpp index 7d5a938..0de3a2e 100644 --- a/new_parse.cpp +++ b/new_parse.cpp @@ -263,8 +263,13 @@ parse_block(){ result = parse_init_stmt((Ast_Expr *)result); } - if(result) stmts.add(result); - else parsing_error(token, "Unexpected token [%s] while parsing statement", name(token->kind)); + if(result) { + result->flags = set_flag(result->flags, AST_STMT); + stmts.add(result); + } + else { + parsing_error(token, "Unexpected token [%s] while parsing statement", name(token->kind)); + } } } while(token_match(SAME_SCOPE)); @@ -320,6 +325,8 @@ binding_power(Binding binding, Token_Kind kind){ else invalid_codepath; Prefix: switch(kind){ + case TK_Increment: + case TK_Decrement: case TK_Pointer: case TK_Dereference: case TK_OpenBracket: @@ -361,6 +368,8 @@ binding_power(Binding binding, Token_Kind kind){ default: return {}; } Postfix: switch(kind){ + case TK_Increment: + case TK_Decrement: case TK_OpenBracket: case TK_OpenParen: return {21, -2}; @@ -387,6 +396,8 @@ parse_expr(S64 min_bp){ case TK_Add : left = ast_expr_unary(token, TK_Add, parse_expr(prefix_bp.right)); break; case TK_Not : left = ast_expr_unary(token, TK_Not, parse_expr(prefix_bp.right)); break; case TK_Neg : left = ast_expr_unary(token, TK_Neg, parse_expr(prefix_bp.right)); break; + case TK_Increment : left = ast_expr_unary(token, TK_Increment, parse_expr(prefix_bp.right)); break; + case TK_Decrement : left = ast_expr_unary(token, TK_Decrement, parse_expr(prefix_bp.right)); break; case TK_OpenBracket: { Ast_Array *result = ast_array(token, parse_expr(0)); @@ -482,7 +493,7 @@ parse_struct(Token *pos){ token_match(OPEN_SCOPE); do{ Token *token = token_get(); - + Ast_Named *named = parse_named(false); if(!named) parsing_error(token, "Failed to parse struct member"); named->flags = set_flag(named->flags, AST_AGGREGATE_CHILD); diff --git a/program.c b/program.c index ab27355..968a1a6 100644 --- a/program.c +++ b/program.c @@ -39,13 +39,19 @@ Bool is_numeric(U8 c){ } void entry(){ String string_to_lex = LIT("Identifier 2425525 Not_Number"); + Token token_array[32]; + U32 token_count; Token t; for(S64 i = 0;(i= '0 && c <= '9 return result entry :: () string_to_lex := "Identifier 2425525 Not_Number" + token_array: [32]Token + token_count: U32 t: Token for i := 0, i < string_to_lex.len, i+=1 if is_numeric(string_to_lex.str[i]) t.str = &string_to_lex.str[i] + t.len = i for is_numeric(string_to_lex.str[i]) i+=1 + t.len = i - t.len + token_array[token_count++] = t + token_count+=1 + diff --git a/typecheck.cpp b/typecheck.cpp index 747fcf3..bf2ae32 100644 --- a/typecheck.cpp +++ b/typecheck.cpp @@ -216,7 +216,8 @@ digit_count(const Value *a){ } function void -eval_unary(Token *pos, Token_Kind op, Value *a, bool is_const){ +eval_unary(Token *pos, Token_Kind op, Operand *operand){ + Value *a = &operand->value; Ast_Resolved_Type *type = a->type; if(!is_numeric(type)) parsing_error(pos, "Unary [%s] cant be applied to value of type %s", name(op), docname(type)); @@ -224,7 +225,10 @@ eval_unary(Token *pos, Token_Kind op, Value *a, bool is_const){ if(op == TK_Not) a->type = untyped_bool; - if(!is_const) + if(op == TK_Increment || op == TK_Decrement || op == TK_PostIncrement || op == TK_PostDecrement) + if(!operand->is_lvalue) parsing_error(pos, "Unary [%s] requires an assignable value(lvalue)"); + + if(!operand->is_const) return; BigInt result = {}; @@ -738,16 +742,15 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res case TK_Dereference:{ return operand_lvalue(type_pointer(value.type)); }break; - case TK_Neg:case TK_Not:case TK_Add:case TK_Sub:{ + default:{ Operand op = resolve_expr(node->expr); - eval_unary(node->pos, node->op, &op.value, op.is_const); + eval_unary(node->pos, node->op, &op); if(op.is_const){ rewrite_into_const(node, Ast_Unary, op.value); return operand_const_rvalue(op.value); } return operand_rvalue(op.value.type); }break; - invalid_default_case; return {}; } BREAK();