Added unary ++ --, prefix and postfix, working on running a program

This commit is contained in:
Krzosa Karol
2022-06-07 09:05:02 +02:00
parent d3da979d64
commit c5b82c0532
6 changed files with 51 additions and 15 deletions

View File

@@ -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(";");
}
}
}

View File

@@ -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();

View File

@@ -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);

View File

@@ -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<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);
}
}
}

View File

@@ -3,19 +3,25 @@ Token :: struct
str: *U8
len: S64
is_numeric :: (c: U8): Bool
result := c >= '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

View File

@@ -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();