some execute refactor
This commit is contained in:
parent
9132dd2d66
commit
40b17625e2
1 changed files with 24 additions and 42 deletions
|
|
@ -5,27 +5,16 @@
|
|||
#include "ast.h"
|
||||
|
||||
typedef struct {
|
||||
unsigned* i;
|
||||
unsigned* next_free_cell_index;
|
||||
Array memory;
|
||||
} Memory;
|
||||
|
||||
struct Cell {
|
||||
char* name;
|
||||
unsigned index;
|
||||
struct Cell* next;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
struct Cell* next;
|
||||
} Scope;
|
||||
|
||||
Memory new_memory() {
|
||||
unsigned size = 1024;
|
||||
unsigned* i = (unsigned*)malloc(sizeof(unsigned));
|
||||
*i = 0;
|
||||
unsigned* next_free_cell_index = (unsigned*)calloc(1, sizeof(unsigned));
|
||||
int* mem = (int*)malloc(sizeof(int) * size);
|
||||
return (Memory){
|
||||
.i = i,
|
||||
.next_free_cell_index = next_free_cell_index,
|
||||
.memory = {
|
||||
.length = size,
|
||||
.elements = mem,
|
||||
|
|
@ -33,32 +22,43 @@ Memory new_memory() {
|
|||
};
|
||||
}
|
||||
|
||||
struct Cell {
|
||||
char* name;
|
||||
int* memory_cell;
|
||||
struct Cell* next;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
struct Cell* next;
|
||||
} Scope;
|
||||
|
||||
Scope new_scope() {
|
||||
return (Scope) { .next = NULL };
|
||||
}
|
||||
|
||||
int lookup_scope(char* name, Scope scope) {
|
||||
int* lookup(char* name, Scope scope) {
|
||||
struct Cell* cell = scope.next;
|
||||
while (cell != NULL) {
|
||||
if (strcmp(cell->name, name) == 0) {
|
||||
return cell->index;
|
||||
return cell->memory_cell;
|
||||
} else {
|
||||
cell = cell->next;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Scope insert(char* name, int n, Memory memory, Scope scope) {
|
||||
if (*memory.i + 1 < memory.memory.length) {
|
||||
((int*)(memory.memory.elements))[*memory.i] = n;
|
||||
Scope insert(char* name, int value, Memory memory, Scope scope) {
|
||||
if (*memory.next_free_cell_index + 1 < memory.memory.length) {
|
||||
int* memory_cell = &((int*)(memory.memory.elements))[*memory.next_free_cell_index];
|
||||
++*memory.next_free_cell_index;
|
||||
*memory_cell = value;
|
||||
struct Cell* cell = (struct Cell*)malloc(sizeof(struct Cell));
|
||||
*cell = (struct Cell) {
|
||||
.name = name,
|
||||
.index = *memory.i,
|
||||
.memory_cell = memory_cell,
|
||||
.next = scope.next,
|
||||
};
|
||||
++*memory.i;
|
||||
Scope new_scope = (Scope) {.next = cell};
|
||||
return new_scope;
|
||||
}
|
||||
|
|
@ -66,24 +66,6 @@ Scope insert(char* name, int n, Memory memory, Scope scope) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
int* lookup_mem(int index, Memory mem) {
|
||||
if (index >= 0 && (unsigned)index < *(mem.i)) {
|
||||
return &((int*)(mem.memory.elements))[index];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int* lookup(char* name, Memory memory, Scope scope) {
|
||||
int index = lookup_scope(name, scope);
|
||||
if (index >= 0) {
|
||||
int* result = lookup_mem(index, memory);
|
||||
if (result != NULL) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int eval_expr(Expr expr, Memory memory, Scope scope) {
|
||||
switch (expr.tag) {
|
||||
default:
|
||||
|
|
@ -91,7 +73,7 @@ int eval_expr(Expr expr, Memory memory, Scope scope) {
|
|||
return expr.data.integer;
|
||||
}
|
||||
case VARIABLE: {
|
||||
int* result = lookup(expr.data.variable, memory, scope);
|
||||
int* result = lookup(expr.data.variable, scope);
|
||||
if (result == NULL) {
|
||||
fprintf(stderr, "Error: variable not found '%s'\n", expr.data.variable);
|
||||
exit(1);
|
||||
|
|
@ -138,7 +120,7 @@ Scope interpret_stmt(Stmt stmt, Memory memory, Scope scope) {
|
|||
switch (stmt.tag) {
|
||||
case SET: {
|
||||
int result = eval_expr(stmt.data.Set.expr, memory, scope);
|
||||
int* index = lookup(stmt.data.Set.name, memory, scope);
|
||||
int* index = lookup(stmt.data.Set.name, scope);
|
||||
if (index != NULL) {
|
||||
*index = result;
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue