mirror of
https://github.com/sigmasternchen/macrofuck
synced 2025-03-15 07:08:56 +00:00
feat: Add support to subscript assignments
This commit is contained in:
parent
c71a63e821
commit
a381d95ff1
8 changed files with 41 additions and 16 deletions
|
@ -39,11 +39,11 @@ struct statement* declaration_statement_new(struct statement* assignment) {
|
|||
}
|
||||
|
||||
|
||||
struct statement* assignment_statement_new(char* id, struct expression* expr) {
|
||||
struct statement* assignment_statement_new(struct expression* variable, struct expression* expr) {
|
||||
_new(stat, statement);
|
||||
stat->kind = ASSIGNMENT_STATEMENT;
|
||||
stat->assignment = (struct assignment_statement) {
|
||||
.id = id,
|
||||
.variable = variable,
|
||||
.value = expr,
|
||||
};
|
||||
return stat;
|
||||
|
|
|
@ -102,7 +102,7 @@ struct expression {
|
|||
};
|
||||
|
||||
struct assignment_statement {
|
||||
char* id;
|
||||
struct expression* variable;
|
||||
struct expression* value;
|
||||
};
|
||||
|
||||
|
@ -140,7 +140,7 @@ struct block* block_new(void);
|
|||
void block_add_statement(struct block*, struct statement*);
|
||||
|
||||
struct statement* declaration_statement_new(struct statement*);
|
||||
struct statement* assignment_statement_new(char*, struct expression*);
|
||||
struct statement* assignment_statement_new(struct expression*, struct expression*);
|
||||
struct statement* expr_statement_new(struct expression*);
|
||||
struct statement* if_statement_new(struct expression*, struct block*, struct block*);
|
||||
struct statement* while_statement_new(struct expression*, struct block*);
|
||||
|
|
|
@ -12,19 +12,25 @@
|
|||
|
||||
|
||||
static void codegen_assignment_statement(FILE* out, scope_t* scope, struct assignment_statement statement) {
|
||||
region_t* region = codegen_expr(out, scope, statement.value);
|
||||
region_t* var = scope_get(scope, statement.id);
|
||||
if (!var) {
|
||||
fprintf(stderr, "variable not found: %s\n", statement.id);
|
||||
panic("unknown variable");
|
||||
region_t* value = codegen_expr(out, scope, statement.value);
|
||||
region_t* var = codegen_expr(out, scope, statement.variable);
|
||||
|
||||
if (statement.variable->kind != VARIABLE) {
|
||||
fprintf(stderr, "assigment lhs is not a variable (%d)\n", statement.variable->kind);
|
||||
panic("not a variable");
|
||||
}
|
||||
|
||||
if (var->start != region->start) {
|
||||
if (var->start != value->start) {
|
||||
reset_region(var);
|
||||
copy(region, var);
|
||||
copy(value, var);
|
||||
}
|
||||
|
||||
region_used(region);
|
||||
region_used(value);
|
||||
|
||||
if (statement.variable->variable.is_offset) {
|
||||
// lhs is offset -> free ref
|
||||
scope_remove(scope, var);
|
||||
}
|
||||
}
|
||||
|
||||
// only used by decl_statement
|
||||
|
@ -61,8 +67,13 @@ static region_t* clone_region(FILE* out, scope_t* scope, region_t* original) {
|
|||
}
|
||||
|
||||
static void codegen_decl_statement(FILE* out, scope_t* scope, struct assignment_statement statement) {
|
||||
if (scope_get(scope, statement.id)) {
|
||||
fprintf(stderr, "variable exists: %s\n", statement.id);
|
||||
if (statement.variable->kind != VARIABLE || statement.variable->variable.is_offset) {
|
||||
fprintf(stderr, "decl lhs is not a variable (%d)\n", statement.variable->kind);
|
||||
panic("not a variable");
|
||||
}
|
||||
|
||||
if (scope_get(scope, statement.variable->variable.id)) {
|
||||
fprintf(stderr, "variable exists: %s\n", statement.variable->variable.id);
|
||||
panic("variable exists");
|
||||
}
|
||||
|
||||
|
@ -72,7 +83,7 @@ static void codegen_decl_statement(FILE* out, scope_t* scope, struct assignment_
|
|||
region = clone_region(out, scope, region);
|
||||
}
|
||||
|
||||
scope_existing(scope, region, statement.id);
|
||||
scope_existing(scope, region, statement.variable->variable.id);
|
||||
}
|
||||
|
||||
static void codegen_if_statement(FILE* out, scope_t* scope, struct if_statement statement) {
|
||||
|
|
|
@ -107,7 +107,7 @@ definition: VAR assignment
|
|||
}
|
||||
;
|
||||
|
||||
assignment: ID ASSIGNMENT expr
|
||||
assignment: variable ASSIGNMENT expr
|
||||
{
|
||||
$$ = assignment_statement_new($1, $3);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
var a = []{'f', 'o', 'o', '\n'};
|
||||
a[2] = 'O';
|
||||
|
||||
print(a);
|
|
@ -0,0 +1,3 @@
|
|||
>>>>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]<[-<<<<+>>>>>+<]>[-<+>]<[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]<[-<<<+>>>>+<]>[-<+>]<[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]<[-<<+>>>+<]>[-<+>]<[-]++++++++++>[-]<[-<+>>+<]>[-<+>]
|
||||
<[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<[-]>>>[-]<[-<<+>>>+<]>[-<+>]
|
||||
<<<<<.>.>.>.
|
4
compiler/test/cases/020g-assignment-to-str-subscript.in
Normal file
4
compiler/test/cases/020g-assignment-to-str-subscript.in
Normal file
|
@ -0,0 +1,4 @@
|
|||
var foo = "Hello, vorld!\n";
|
||||
foo[7] = 'W';
|
||||
|
||||
print(foo);
|
3
compiler/test/cases/020g-assignment-to-str-subscript.out
Normal file
3
compiler/test/cases/020g-assignment-to-str-subscript.out
Normal file
|
@ -0,0 +1,3 @@
|
|||
[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++>[-]++++++++++
|
||||
>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<<<<<<[-]>>>>>>>>[-]<[-<<<<<<<+>>>>>>>>+<]>[-<+>]
|
||||
<<<<<<<<<<<<<<<.>.>.>.>.>.>.>.>.>.>.>.>.>.
|
Loading…
Reference in a new issue