fix bug by adding count to all types.
This commit is contained in:
parent
28f94b2ee1
commit
3eb174aedf
2
makefile
2
makefile
|
@ -6,7 +6,7 @@
|
||||||
# make uninstall
|
# make uninstall
|
||||||
|
|
||||||
## C compiler
|
## C compiler
|
||||||
CC=gcc # much faster compilation than gcc
|
CC=tcc # much faster compilation than gcc
|
||||||
COMPILER_FLAGS=#-g3 -Wall -Wextra -Wconversion -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-sign-conversion -fsanitize=undefined
|
COMPILER_FLAGS=#-g3 -Wall -Wextra -Wconversion -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-sign-conversion -fsanitize=undefined
|
||||||
# exclude: -fsanitize-trap, because I'm using an old version of gcc and couldn't bother getting a new one.
|
# exclude: -fsanitize-trap, because I'm using an old version of gcc and couldn't bother getting a new one.
|
||||||
## ^ from <https://nullprogram.com/blog/2023/04/29/>
|
## ^ from <https://nullprogram.com/blog/2023/04/29/>
|
||||||
|
|
38
src/mumble.c
38
src/mumble.c
|
@ -38,6 +38,7 @@ lispval* lispval_num(double x)
|
||||||
{
|
{
|
||||||
lispval* v = malloc(sizeof(lispval));
|
lispval* v = malloc(sizeof(lispval));
|
||||||
v->type = LISPVAL_NUM;
|
v->type = LISPVAL_NUM;
|
||||||
|
v->count = 0;
|
||||||
v->num = x;
|
v->num = x;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
@ -46,6 +47,7 @@ lispval* lispval_err(char* message)
|
||||||
{
|
{
|
||||||
lispval* v = malloc(sizeof(lispval));
|
lispval* v = malloc(sizeof(lispval));
|
||||||
v->type = LISPVAL_ERR;
|
v->type = LISPVAL_ERR;
|
||||||
|
v->count = 0;
|
||||||
v->err = malloc(strlen(message) + 1);
|
v->err = malloc(strlen(message) + 1);
|
||||||
strcpy(v->err, message);
|
strcpy(v->err, message);
|
||||||
return v;
|
return v;
|
||||||
|
@ -55,6 +57,7 @@ lispval* lispval_sym(char* symbol)
|
||||||
{
|
{
|
||||||
lispval* v = malloc(sizeof(lispval));
|
lispval* v = malloc(sizeof(lispval));
|
||||||
v->type = LISPVAL_SYM;
|
v->type = LISPVAL_SYM;
|
||||||
|
v->count = 0;
|
||||||
v->sym = malloc(strlen(symbol) + 1);
|
v->sym = malloc(strlen(symbol) + 1);
|
||||||
strcpy(v->sym, symbol);
|
strcpy(v->sym, symbol);
|
||||||
return v;
|
return v;
|
||||||
|
@ -267,29 +270,44 @@ void print_ast(mpc_ast_t* ast, int indent_level)
|
||||||
lispval* clone_lispval(lispval* old)
|
lispval* clone_lispval(lispval* old)
|
||||||
{
|
{
|
||||||
lispval* new;
|
lispval* new;
|
||||||
|
print_lispval_tree(old, 0);
|
||||||
|
printf("\nCloning lispval of type %d\n", old->type);
|
||||||
switch(old->type){
|
switch(old->type){
|
||||||
case LISPVAL_NUM:
|
case LISPVAL_NUM:
|
||||||
|
printf("\n1");
|
||||||
|
printf("\nnum: %f", old->num);
|
||||||
|
print_lispval_tree(old, 0);
|
||||||
new = lispval_num(old->num);
|
new = lispval_num(old->num);
|
||||||
|
printf("\nAssigned new");
|
||||||
|
printf("\n count: %i", old->count);
|
||||||
break;
|
break;
|
||||||
case LISPVAL_ERR:
|
case LISPVAL_ERR:
|
||||||
|
printf("2");
|
||||||
new = lispval_err(old->err);
|
new = lispval_err(old->err);
|
||||||
break;
|
break;
|
||||||
case LISPVAL_SYM:
|
case LISPVAL_SYM:
|
||||||
|
printf("3");
|
||||||
new = lispval_sym(old->sym);
|
new = lispval_sym(old->sym);
|
||||||
break;
|
break;
|
||||||
case LISPVAL_SEXPR:
|
case LISPVAL_SEXPR:
|
||||||
|
printf("4");
|
||||||
new = lispval_sexpr();
|
new = lispval_sexpr();
|
||||||
break;
|
break;
|
||||||
case LISPVAL_QEXPR:
|
case LISPVAL_QEXPR:
|
||||||
|
printf("\n5");
|
||||||
new = lispval_qexpr();
|
new = lispval_qexpr();
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
return lispval_err("Error: Cloning element of unknown type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < old->count; i++) {
|
printf("\n6");
|
||||||
lispval* temp_child = old->cell[i];
|
if(old->count > 0){
|
||||||
lispval* child = clone_lispval(temp_child);
|
for (int i = 0; i < old->count; i++) {
|
||||||
lispval_append_child(new, child);
|
lispval* temp_child = old->cell[i];
|
||||||
|
lispval* child = clone_lispval(temp_child);
|
||||||
|
lispval_append_child(new, child);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
@ -319,13 +337,17 @@ lispval* take_lispval(lispval* v, int i)
|
||||||
// Operations
|
// Operations
|
||||||
// Ops for q-expressions
|
// Ops for q-expressions
|
||||||
lispval* builtin_head(lispval* v){
|
lispval* builtin_head(lispval* v){
|
||||||
|
printf("Entering builtin_head with v->count = %d and v->cell[0]->type = %d\n", v->count, v->cell[0]->type);
|
||||||
// head { 1 2 3 }
|
// head { 1 2 3 }
|
||||||
// But actually, that gets processd into head ({ 1 2 3 }), hence the v->cell[0]->cell[0];
|
// But actually, that gets processd into head ({ 1 2 3 }), hence the v->cell[0]->cell[0];
|
||||||
LISPVAL_ASSERT(v->count ==1, "Error: function head passed too many arguments");
|
LISPVAL_ASSERT(v->count == 1, "Error: function head passed too many arguments");
|
||||||
LISPVAL_ASSERT(v->cell[0]->type == LISPVAL_QEXPR, "Error: Argument passed to head is not a q-expr, i.e., a bracketed list.");
|
LISPVAL_ASSERT(v->cell[0]->type == LISPVAL_QEXPR, "Error: Argument passed to head is not a q-expr, i.e., a bracketed list.");
|
||||||
LISPVAL_ASSERT(v->cell[0]->count != 0, "Error: Argument passed to head is {}");
|
LISPVAL_ASSERT(v->cell[0]->count != 0, "Error: Argument passed to head is {}");
|
||||||
|
printf("Passed assertions, v->cell[0]->count = %d\n", v->cell[0]->count);
|
||||||
// print_lispval_parenthesis(v);
|
// print_lispval_parenthesis(v);
|
||||||
lispval* result = clone_lispval(v->cell[0]->cell[0]);
|
print_lispval_parenthesis(v->cell[0]);
|
||||||
|
lispval* result = clone_lispval(v->cell[0]);
|
||||||
|
printf("Cloned lispval, result->type = %d\n", result->type);
|
||||||
// lispval* result = pop_lispval(v->cell[0], 0);
|
// lispval* result = pop_lispval(v->cell[0], 0);
|
||||||
// ^ also possible
|
// ^ also possible
|
||||||
// A bit unclear. Pop seems like it would depend on the size of the array. clone depends on the sie of head.
|
// A bit unclear. Pop seems like it would depend on the size of the array. clone depends on the sie of head.
|
||||||
|
@ -470,6 +492,8 @@ lispval* builtin_functions(char* func, lispval* v)
|
||||||
// Evaluate the lispval
|
// Evaluate the lispval
|
||||||
lispval* evaluate_lispval(lispval* l)
|
lispval* evaluate_lispval(lispval* l)
|
||||||
{
|
{
|
||||||
|
// Check if this is an s-expression
|
||||||
|
if(l->type != LISPVAL_SEXPR) return l;
|
||||||
// Evaluate the children if needed
|
// Evaluate the children if needed
|
||||||
for (int i = 0; i < l->count; i++) {
|
for (int i = 0; i < l->count; i++) {
|
||||||
if (l->cell[i]->type == LISPVAL_SEXPR) {
|
if (l->cell[i]->type == LISPVAL_SEXPR) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user