Skip to content

Commit

Permalink
Fix *= and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Jun 16, 2024
1 parent 6ddc900 commit ff35492
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
14 changes: 11 additions & 3 deletions Sources/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down
11 changes: 9 additions & 2 deletions Sources/kong.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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) {
Expand Down
43 changes: 22 additions & 21 deletions Sources/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ff35492

Please sign in to comment.