-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.c
80 lines (69 loc) · 1.98 KB
/
scope.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <memory.h>
#include <stdlib.h>
#include <string.h>
#include "scope.h"
#include "array-util.h"
static const Variant undefined = {.type = TYPE_UNDEF};
Scope* create_scope()
{
Scope* ret = malloc(sizeof(Scope));
ret->capacity = 5;
ret->size = 0;
ret->vars = calloc(ret->capacity, sizeof(*ret->vars));
ret->parent = NULL;
return ret;
}
void destroy_scope(Scope* scope)
{
for (size_t i = 0; i < scope->size; ++i) {
free(scope->vars[i].name);
free_var(scope->vars[i].value);
}
free(scope->vars);
free(scope);
}
Variant lookup(Runtime* R, char* str)
{
return lookupWithFlags(R, str, 0);
}
Variant lookupWithFlags(Runtime* R, char* str, int flags)
{
Variable* vars = R->scope->vars;
for (size_t i = 0; i < R->scope->size; ++i) {
if (strcmp(str, vars[i].name) == 0 && (flags == vars[i].flags || vars[i].flags & flags)) {
return vars[i].value;
}
}
return undefined;
}
static void try_vars_resize(Scope* scope)
{
try_resize(&scope->capacity, scope->size, (void*)&scope->vars,
sizeof(*scope->vars), die);
}
Variable* set_var(Runtime* R, char* str, Variant var, int flags)
{
Variable* vars = R->scope->vars;
// existing variable
for (size_t i = 0; i < R->scope->size; ++i) {
if (strcmp(str, vars[i].name) == 0 && (flags == vars[i].flags || vars[i].flags & flags)) {
if (flags & VAR_FLAG_CONST) {
runtimeerror(R, "Cannot redeclare constant");
return NULL;
}
free_var(vars[i].value);
vars[i].value = cpy_var(var);
if (vars[i].flags != flags) {
runtimeerror(R, "Cannot reassign flags");
return NULL;
}
return &vars[i];
}
}
try_vars_resize(R->scope);
const size_t idx = R->scope->size++;
vars[idx].name = strdup(str);
vars[idx].value = cpy_var(var);
vars[idx].flags = flags;
return &vars[idx];
}