Skip to content

Commit

Permalink
Alternative implementation of variables and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron K. Johnson committed Oct 15, 2024
1 parent d24d0b1 commit e99a40b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Makefile.mac
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC = clang
CFLAGS = -w -O3 -static -finline-functions
LDFLAGS = -lm -lportmidi
LDFLAGS = -lm -lportmidi -lsqlite3
LDDCLANG = -ldclang
OBJECTS = main.o
EXECUTABLE = dclang
Expand Down
10 changes: 10 additions & 0 deletions dclang.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,18 @@ DCLANG_PTR locals_keys[NUMLOCALS];
DCLANG_FLT locals_vals[NUMLOCALS * RETURN_STACK_SIZE];
DCLANG_PTR locals_base_idx;
// global variables
// track variables - mapping from name to index
char *var_keys[NUMVARS];
DCLANG_PTR var_vals[NUMVARS];
DCLANG_PTR var_map_idx;
// variable actual data stored here
DCLANG_FLT vars[NUMVARS];
DCLANG_PTR vars_idx;
// global constants
// track constants - mapping from name to index
char *const_keys[64];
DCLANG_FLT const_vals[64];
DCLANG_PTR const_idx;

// hashwords
char **hashwords;
Expand Down
30 changes: 30 additions & 0 deletions input_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ void compile_or_interpret(const char *token)
double d;
const struct primitive *pr = primitives;
DCLANG_LONG locals_idx = 0;
DCLANG_LONG const_search_idx = const_idx - 1;
DCLANG_LONG var_search_idx = var_map_idx - 1;

if (token == 0) {
return;
Expand Down Expand Up @@ -108,6 +110,34 @@ void compile_or_interpret(const char *token)
}
}

// Search for a constant
while ((const_search_idx >= 0) && (const_keys[const_search_idx] != 0)) {
if (strcmp(const_keys[const_search_idx], token) == 0) {
if (def_mode) {
prog[iptr].function.with_param = push;
prog[iptr++].param = (DCLANG_FLT) const_vals[const_search_idx];
} else {
push((DCLANG_FLT) const_vals[const_search_idx]);
}
return;
}
const_search_idx--;
}

// Search for a variable
while ((var_search_idx >= 0) && (var_keys[var_search_idx] != 0)) {
if (strcmp(var_keys[var_search_idx], token) == 0) {
if (def_mode) {
prog[iptr].function.with_param = push;
prog[iptr++].param = (DCLANG_FLT) var_vals[var_search_idx];
} else {
push((DCLANG_FLT) var_vals[var_search_idx]);
}
return;
}
var_search_idx--;
}

// Neither user word nor primitive word was found.
// OK, so next, try to convert to a number
d = strtod(token, &endPointer);
Expand Down
2 changes: 2 additions & 0 deletions primitives.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ void add_all_primitives()
add_primitive("exit", "Operating System", exitfunc);
// show defined words!
add_primitive("words", "Other", showdefined);
add_primitive("constants", "Other", showconsts);
add_primitive("variables", "Other", showvars);
};

void show_primitivesfunc()
Expand Down
58 changes: 24 additions & 34 deletions variable_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,43 +99,15 @@ void peekfunc()
/* implement constants */
void constantfunc()
{
startword();
prog[iptr].function.with_param = push;
prog[iptr++].param = dclang_pop();
endword();
}

void _variable_common()
{
// In reality, a variable is a named word that simply pushes
// an address to its associated memory
prog[iptr].function.with_param = push;
prog[iptr++].param = vars_idx++;
const_keys[const_idx] = get_token();
const_vals[const_idx++] = dclang_pop();
}

void variablefunc()
{
if (iptr < max_iptr)
{
// mark current position
DCLANG_PTR ret_iptr = iptr;
// jump to end (when this is called in the future)
iptr = return_stack[--return_stack_ptr];
return_stack[return_stack_ptr++] = ret_iptr;
startword();
_variable_common();
// part of endword here:
prog[iptr++].function.without_param = returnfunc;
max_iptr = iptr;
// restore function position
iptr = return_stack[--return_stack_ptr];
// restore return stack to new highest position
return_stack[return_stack_ptr++] = max_iptr;
} else {
startword();
_variable_common();
endword();
}
DCLANG_PTR next_var = vars_idx++;
var_keys[var_map_idx] = get_token();
var_vals[var_map_idx++] = next_var;
}

void allotfunc()
Expand Down Expand Up @@ -170,7 +142,25 @@ void herefunc()
push((DCLANG_PTR) vars_idx);
}

/* global hash space, a la 'redis', but in local memory */
// some helpers to show stuff

void showvars()
{
for (int x=0; x < var_map_idx; x++) {
printf("Variable %i: %s @ %li\n", x, var_keys[x], var_vals[x]);
}
}

void showconsts()
{
for (int x=0; x < const_idx; x++) {
printf("Constant %i: %s ==> %.19g\n", x, const_keys[x], const_vals[x]);
}
}

//////////////////////////////////////////////////////////
// global hash space, a la 'redis', but in local memory //
//////////////////////////////////////////////////////////

void _add_key(char *key){
if (hashwords_cnt > hashwords_size)
Expand Down

0 comments on commit e99a40b

Please # to comment.