From ff3549245d8ddb785eacd8cb2248f19b2f374930 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Sun, 16 Jun 2024 11:53:28 +0200 Subject: [PATCH] Fix *= and friends --- Sources/compiler.c | 14 +++++++++++--- Sources/kong.c | 11 +++++++++-- Sources/tokenizer.c | 43 ++++++++++++++++++++++--------------------- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/Sources/compiler.c b/Sources/compiler.c index 2b86ca6..27153af 100644 --- a/Sources/compiler.c +++ b/Sources/compiler.c @@ -103,7 +103,7 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { variable result_var = allocate_variable(right_var.type); opcode o; - switch (e->kind) { + switch (e->binary.op) { case OPERATOR_MINUS: o.type = OPCODE_SUB; break; @@ -116,6 +116,10 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { case OPERATOR_MULTIPLY: o.type = OPCODE_MULTIPLY; break; + default: { + debug_context context = {0}; + error(context, "Unexpected operator"); + } } o.size = OP_SIZE(o, op_add); o.op_sub.right = right_var; @@ -151,7 +155,7 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { switch (left->kind) { case EXPRESSION_VARIABLE: { - if (e->kind != OPERATOR_ASSIGN) { + if (e->binary.op != OPERATOR_ASSIGN) { debug_context context = {0}; error(context, "operator can not initialize a variable"); } @@ -167,7 +171,7 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { variable member_var = emit_expression(code, parent, left->member.left); opcode o; - switch (e->kind) { + switch (e->binary.op) { case OPERATOR_ASSIGN: o.type = OPCODE_STORE_MEMBER; break; @@ -183,6 +187,10 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { case OPERATOR_MULTIPLY_ASSIGN: o.type = OPCODE_MULTIPLY_AND_STORE_MEMBER; break; + default: { + debug_context context = {0}; + error(context, "Unexpected operator"); + } } o.size = OP_SIZE(o, op_store_member); o.op_store_member.from = v; diff --git a/Sources/kong.c b/Sources/kong.c index b5ca92b..12f748d 100644 --- a/Sources/kong.c +++ b/Sources/kong.c @@ -119,12 +119,16 @@ void resolve_types_in_expression(statement *parent, expression *e) { e->type.type = bool_id; break; } - case OPERATOR_MULTIPLY: { + case OPERATOR_MULTIPLY: + case OPERATOR_MULTIPLY_ASSIGN: { type_id left_type = e->binary.left->type.type; type_id right_type = e->binary.right->type.type; if (left_type == right_type || (left_type == float4x4_id && right_type == float4_id)) { e->type = e->binary.right->type; } + else if (right_type == float_id && (left_type == float2_id || left_type == float3_id || left_type == float4_id)) { + e->type = e->binary.left->type; + } else { debug_context context = {0}; error(context, "Type mismatch %s vs %s", get_name(get_type(left_type)->name), get_name(get_type(right_type)->name)); @@ -135,7 +139,10 @@ void resolve_types_in_expression(statement *parent, expression *e) { case OPERATOR_PLUS: case OPERATOR_DIVIDE: case OPERATOR_MOD: - case OPERATOR_ASSIGN: { + case OPERATOR_ASSIGN: + case OPERATOR_DIVIDE_ASSIGN: + case OPERATOR_MINUS_ASSIGN: + case OPERATOR_PLUS_ASSIGN: { type_id left_type = e->binary.left->type.type; type_id right_type = e->binary.right->type.type; if (left_type != right_type) { diff --git a/Sources/tokenizer.c b/Sources/tokenizer.c index 4abbe96..903a239 100644 --- a/Sources/tokenizer.c +++ b/Sources/tokenizer.c @@ -378,7 +378,8 @@ tokens tokenize(const char *filename, const char *source) { } if (strcmp(long_op, "==") == 0 || strcmp(long_op, "!=") == 0 || strcmp(long_op, "<=") == 0 || strcmp(long_op, ">=") == 0 || - strcmp(long_op, "||") == 0 || strcmp(long_op, "&&") == 0 || strcmp(long_op, "->") == 0) { + strcmp(long_op, "||") == 0 || strcmp(long_op, "&&") == 0 || strcmp(long_op, "->") == 0 || strcmp(long_op, "-=") == 0 || + strcmp(long_op, "+=") == 0 || strcmp(long_op, "/=") == 0 || strcmp(long_op, "*=") == 0) { tokenizer_buffer_add(&buffer, ch); tokenizer_state_advance(&context, &state); } @@ -413,6 +414,26 @@ tokens tokenize(const char *filename, const char *source) { token.op = OPERATOR_LESS_EQUAL; tokens_add(&tokens, token); } + else if (tokenizer_buffer_equals(&buffer, "-=")) { + token token = token_create(TOKEN_OPERATOR, &state); + token.op = OPERATOR_MINUS_ASSIGN; + tokens_add(&tokens, token); + } + else if (tokenizer_buffer_equals(&buffer, "+=")) { + token token = token_create(TOKEN_OPERATOR, &state); + token.op = OPERATOR_PLUS_ASSIGN; + tokens_add(&tokens, token); + } + else if (tokenizer_buffer_equals(&buffer, "/=")) { + token token = token_create(TOKEN_OPERATOR, &state); + token.op = OPERATOR_DIVIDE_ASSIGN; + tokens_add(&tokens, token); + } + else if (tokenizer_buffer_equals(&buffer, "*=")) { + token token = token_create(TOKEN_OPERATOR, &state); + token.op = OPERATOR_MULTIPLY_ASSIGN; + tokens_add(&tokens, token); + } else if (tokenizer_buffer_equals(&buffer, "-")) { token token = token_create(TOKEN_OPERATOR, &state); token.op = OPERATOR_MINUS; @@ -453,26 +474,6 @@ tokens tokenize(const char *filename, const char *source) { token.op = OPERATOR_MOD; tokens_add(&tokens, token); } - else if (tokenizer_buffer_equals(&buffer, "-=")) { - token token = token_create(TOKEN_OPERATOR, &state); - token.op = OPERATOR_MINUS_ASSIGN; - tokens_add(&tokens, token); - } - else if (tokenizer_buffer_equals(&buffer, "+=")) { - token token = token_create(TOKEN_OPERATOR, &state); - token.op = OPERATOR_PLUS_ASSIGN; - tokens_add(&tokens, token); - } - else if (tokenizer_buffer_equals(&buffer, "/=")) { - token token = token_create(TOKEN_OPERATOR, &state); - token.op = OPERATOR_DIVIDE_ASSIGN; - tokens_add(&tokens, token); - } - else if (tokenizer_buffer_equals(&buffer, "*=")) { - token token = token_create(TOKEN_OPERATOR, &state); - token.op = OPERATOR_MULTIPLY_ASSIGN; - tokens_add(&tokens, token); - } else if (tokenizer_buffer_equals(&buffer, "=")) { token token = token_create(TOKEN_OPERATOR, &state); token.op = OPERATOR_ASSIGN;