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"
|
#include "ast.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned* i;
|
unsigned* next_free_cell_index;
|
||||||
Array memory;
|
Array memory;
|
||||||
} Memory;
|
} Memory;
|
||||||
|
|
||||||
struct Cell {
|
|
||||||
char* name;
|
|
||||||
unsigned index;
|
|
||||||
struct Cell* next;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
struct Cell* next;
|
|
||||||
} Scope;
|
|
||||||
|
|
||||||
Memory new_memory() {
|
Memory new_memory() {
|
||||||
unsigned size = 1024;
|
unsigned size = 1024;
|
||||||
unsigned* i = (unsigned*)malloc(sizeof(unsigned));
|
unsigned* next_free_cell_index = (unsigned*)calloc(1, sizeof(unsigned));
|
||||||
*i = 0;
|
|
||||||
int* mem = (int*)malloc(sizeof(int) * size);
|
int* mem = (int*)malloc(sizeof(int) * size);
|
||||||
return (Memory){
|
return (Memory){
|
||||||
.i = i,
|
.next_free_cell_index = next_free_cell_index,
|
||||||
.memory = {
|
.memory = {
|
||||||
.length = size,
|
.length = size,
|
||||||
.elements = mem,
|
.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() {
|
Scope new_scope() {
|
||||||
return (Scope) { .next = NULL };
|
return (Scope) { .next = NULL };
|
||||||
}
|
}
|
||||||
|
|
||||||
int lookup_scope(char* name, Scope scope) {
|
int* lookup(char* name, Scope scope) {
|
||||||
struct Cell* cell = scope.next;
|
struct Cell* cell = scope.next;
|
||||||
while (cell != NULL) {
|
while (cell != NULL) {
|
||||||
if (strcmp(cell->name, name) == 0) {
|
if (strcmp(cell->name, name) == 0) {
|
||||||
return cell->index;
|
return cell->memory_cell;
|
||||||
} else {
|
} else {
|
||||||
cell = cell->next;
|
cell = cell->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Scope insert(char* name, int n, Memory memory, Scope scope) {
|
Scope insert(char* name, int value, Memory memory, Scope scope) {
|
||||||
if (*memory.i + 1 < memory.memory.length) {
|
if (*memory.next_free_cell_index + 1 < memory.memory.length) {
|
||||||
((int*)(memory.memory.elements))[*memory.i] = n;
|
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));
|
struct Cell* cell = (struct Cell*)malloc(sizeof(struct Cell));
|
||||||
*cell = (struct Cell) {
|
*cell = (struct Cell) {
|
||||||
.name = name,
|
.name = name,
|
||||||
.index = *memory.i,
|
.memory_cell = memory_cell,
|
||||||
.next = scope.next,
|
.next = scope.next,
|
||||||
};
|
};
|
||||||
++*memory.i;
|
|
||||||
Scope new_scope = (Scope) {.next = cell};
|
Scope new_scope = (Scope) {.next = cell};
|
||||||
return new_scope;
|
return new_scope;
|
||||||
}
|
}
|
||||||
|
|
@ -66,24 +66,6 @@ Scope insert(char* name, int n, Memory memory, Scope scope) {
|
||||||
exit(1);
|
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) {
|
int eval_expr(Expr expr, Memory memory, Scope scope) {
|
||||||
switch (expr.tag) {
|
switch (expr.tag) {
|
||||||
default:
|
default:
|
||||||
|
|
@ -91,7 +73,7 @@ int eval_expr(Expr expr, Memory memory, Scope scope) {
|
||||||
return expr.data.integer;
|
return expr.data.integer;
|
||||||
}
|
}
|
||||||
case VARIABLE: {
|
case VARIABLE: {
|
||||||
int* result = lookup(expr.data.variable, memory, scope);
|
int* result = lookup(expr.data.variable, scope);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
fprintf(stderr, "Error: variable not found '%s'\n", expr.data.variable);
|
fprintf(stderr, "Error: variable not found '%s'\n", expr.data.variable);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
@ -138,7 +120,7 @@ Scope interpret_stmt(Stmt stmt, Memory memory, Scope scope) {
|
||||||
switch (stmt.tag) {
|
switch (stmt.tag) {
|
||||||
case SET: {
|
case SET: {
|
||||||
int result = eval_expr(stmt.data.Set.expr, memory, scope);
|
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) {
|
if (index != NULL) {
|
||||||
*index = result;
|
*index = result;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue