Browse Source

Sol Part 33: Garbage In, Garbage Out!

master
Graham Northup 6 years ago
parent
commit
3c1da53911
  1. 4
      .gitignore
  2. 2
      ast.h
  3. 4
      astprint.c
  4. 2
      build.sh
  5. 142
      builtins.c
  6. 42
      dump.sol
  7. 104
      gc.c
  8. 71
      gcstat.py
  9. 1884
      lex.yy.c
  10. 4
      object.c
  11. 8339
      parser.output
  12. 4707
      parser.tab.c
  13. 162
      parser.tab.h
  14. 3
      parser.y
  15. 4
      runtime.c
  16. 19
      sol.h
  17. 5
      solrun.c
  18. 16
      state.c
  19. 1
      test_monty.sol
  20. 2
      tokenizer.lex

4
.gitignore

@ -3,3 +3,7 @@ sol
stdout
.submodule_stamp
*.orig
*.swp
*.swo
gclog.txt
gcstat.txt

2
ast.h

@ -26,7 +26,7 @@ typedef struct {
};
} lit_node;
typedef enum {OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD, OP_POW, OP_BAND, OP_BOR, OP_BXOR, OP_LAND, OP_LOR, OP_EQUAL, OP_LESS, OP_GREATER, OP_LESSEQ, OP_GREATEREQ, OP_LSHIFT, OP_RSHIFT} binop_t;
typedef enum {OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD, OP_POW, OP_BAND, OP_BOR, OP_BXOR, OP_LAND, OP_LOR, OP_EQUAL, OP_NEQUAL, OP_LESS, OP_GREATER, OP_LESSEQ, OP_GREATEREQ, OP_LSHIFT, OP_RSHIFT} binop_t;
typedef struct {
binop_t type;
expr_node *left;

4
astprint.c

@ -189,6 +189,10 @@ void prex(sol_state_t *state, expr_node *node, int lev) {
case OP_EQUAL:
prlev(state, lev, "Op: ==");
break;
case OP_NEQUAL:
prlev(state, lev, "Op: !=");
break;
case OP_LESS:
prlev(state, lev, "Op: <");

2
build.sh

@ -4,7 +4,7 @@ if [ ! -f .submodule_stamp ]; then
fi
if [ -z "$CFLAGS" ]; then
CFLAGS=-g
CFLAGS="-g"
fi
gcc -c $CFLAGS dsl/seq.c

142
builtins.c

@ -538,8 +538,8 @@ sol_object_t *sol_f_singlet_tostring(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_int_add(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, a->ival + b->ival);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
sol_object_t *res = sol_new_int(state, a->ival + bint->ival);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -550,8 +550,8 @@ sol_object_t *sol_f_int_add(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_int_sub(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, a->ival - b->ival);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
sol_object_t *res = sol_new_int(state, a->ival - bint->ival);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -562,8 +562,8 @@ sol_object_t *sol_f_int_sub(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_int_mul(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, a->ival * b->ival);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
sol_object_t *res = sol_new_int(state, a->ival * bint->ival);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -574,8 +574,8 @@ sol_object_t *sol_f_int_mul(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_int_div(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, a->ival / b->ival);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
sol_object_t *res = sol_new_int(state, a->ival / bint->ival);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -586,8 +586,8 @@ sol_object_t *sol_f_int_div(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_int_mod(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, a->ival % b->ival);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
sol_object_t *res = sol_new_int(state, a->ival % bint->ival);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -598,8 +598,8 @@ sol_object_t *sol_f_int_mod(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_int_pow(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, (long) pow((double) a->ival, b->ival));
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
sol_object_t *res = sol_new_int(state, (long) pow((double) a->ival, bint->ival));
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -610,8 +610,8 @@ sol_object_t *sol_f_int_pow(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_int_band(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, a->ival & b->ival);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
sol_object_t *res = sol_new_int(state, a->ival & bint->ival);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -622,8 +622,8 @@ sol_object_t *sol_f_int_band(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_int_bor(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, a->ival | b->ival);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
sol_object_t *res = sol_new_int(state, a->ival | bint->ival);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -634,8 +634,8 @@ sol_object_t *sol_f_int_bor(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_int_bxor(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, a->ival ^ b->ival);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
sol_object_t *res = sol_new_int(state, a->ival ^ bint->ival);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -646,8 +646,8 @@ sol_object_t *sol_f_int_bxor(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_int_blsh(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, a->ival << b->ival);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
sol_object_t *res = sol_new_int(state, a->ival << bint->ival);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -658,8 +658,8 @@ sol_object_t *sol_f_int_blsh(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_int_brsh(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, a->ival >> b->ival);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
sol_object_t *res = sol_new_int(state, a->ival >> bint->ival);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -677,10 +677,11 @@ sol_object_t *sol_f_int_bnot(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_int_cmp(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, a->ival == b->ival ? 0 : (a->ival < b->ival ? -1 : 1));
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
sol_object_t *res = sol_new_int(state, a->ival == bint->ival ? 0 : (a->ival < bint->ival ? -1 : 1));
sol_obj_free(a);
sol_obj_free(b);
sol_obj_free(bint);
return res;
}
@ -705,8 +706,8 @@ sol_object_t *sol_f_int_tostring(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_float_add(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_float(state, a->fval + b->fval);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bflt = sol_cast_float(state, b);
sol_object_t *res = sol_new_float(state, a->fval + bflt->fval);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -717,8 +718,8 @@ sol_object_t *sol_f_float_add(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_float_sub(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_float(state, a->fval - b->fval);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bflt = sol_cast_float(state, b);
sol_object_t *res = sol_new_float(state, a->fval - bflt->fval);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -729,8 +730,8 @@ sol_object_t *sol_f_float_sub(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_float_mul(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_float(state, a->fval * b->fval);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bflt = sol_cast_float(state, b);
sol_object_t *res = sol_new_float(state, a->fval * bflt->fval);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -741,8 +742,8 @@ sol_object_t *sol_f_float_mul(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_float_div(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_float(state, a->fval / b->fval);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bflt = sol_cast_float(state, b);
sol_object_t *res = sol_new_float(state, a->fval / bflt->fval);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -753,8 +754,8 @@ sol_object_t *sol_f_float_div(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_float_pow(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_float(state, pow(a->fval, b->fval));
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bflt = sol_cast_float(state, b);
sol_object_t *res = sol_new_float(state, pow(a->fval, bflt->fval));
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -765,11 +766,12 @@ sol_object_t *sol_f_float_pow(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_float_cmp(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, a->fval == b->fval ? 0 : (a->fval < b->fval ? -1 : 1));
sol_obj_free(a);
sol_obj_free(b);
return res;
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bflt = sol_cast_float(state, b);
sol_object_t *res = sol_new_int(state, a->fval==bflt->fval? 0 : (a->fval<bflt->fval? -1 : 1));
sol_obj_free(a);
sol_obj_free(b);
sol_obj_free(bflt);
return res;
}
sol_object_t *sol_f_float_toint(sol_state_t *state, sol_object_t *args) {
@ -793,8 +795,8 @@ sol_object_t *sol_f_float_tostring(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_str_add(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_string(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_string_concat(state, a, b);
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bstr = sol_cast_string(state, b);
sol_object_t *res = sol_string_concat(state, a, bstr);
sol_obj_free(a);
sol_obj_free(b);
if(sol_has_error(state)) {
@ -805,8 +807,8 @@ sol_object_t *sol_f_str_add(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_str_mul(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
int n = strlen(a->str) * b->ival + 1;
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
int n = strlen(a->str) * bint->ival + 1;
char *s = malloc(n);
int i;
s[0] = '\0';
@ -816,6 +818,7 @@ sol_object_t *sol_f_str_mul(sol_state_t *state, sol_object_t *args) {
sol_object_t *res = sol_new_string(state, s);
sol_obj_free(a);
sol_obj_free(b);
sol_obj_free(bint);
free(s);
if(sol_has_error(state)) {
sol_obj_free(res);
@ -825,11 +828,12 @@ sol_object_t *sol_f_str_mul(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_str_cmp(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_string(state, sol_list_get_index(state, args, 1));
sol_object_t *res = sol_new_int(state, strcmp(a->str, b->str));
sol_obj_free(a);
sol_obj_free(b);
return res;
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bstr = sol_cast_string(state, b);
sol_object_t *res = sol_new_int(state, strcmp(a->str, bstr->str));
sol_obj_free(a);
sol_obj_free(b);
sol_obj_free(bstr);
return res;
}
sol_object_t *sol_f_str_len(sol_state_t *state, sol_object_t *args) {
@ -990,11 +994,12 @@ sol_object_t *sol_f_list_add(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_list_mul(sol_state_t *state, sol_object_t *args) {
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1)), *ls;
sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b), *ls;
int i;
if(sol_has_error(state)) {
sol_obj_free(a);
sol_obj_free(b);
sol_obj_free(bint);
return sol_incref(state->None);
}
ls = sol_new_list(state);
@ -1003,6 +1008,7 @@ sol_object_t *sol_f_list_mul(sol_state_t *state, sol_object_t *args) {
if(sol_has_error(state)) {
sol_obj_free(a);
sol_obj_free(b);
sol_obj_free(bint);
return sol_incref(state->None);
}
}
@ -1027,11 +1033,12 @@ sol_object_t *sol_f_list_index(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_list_setindex(sol_state_t *state, sol_object_t *args) {
sol_object_t *ls = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args , 1));
sol_object_t *ls = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args ,1), *bint = sol_cast_int(state, b);
sol_object_t *val = sol_list_get_index(state, args, 2);
sol_list_set_index(state, ls, b->ival, val);
sol_list_set_index(state, ls, bint->ival, val);
sol_obj_free(ls);
sol_obj_free(b);
sol_obj_free(bint);
sol_obj_free(val);
return sol_incref(state->None);
}
@ -1084,24 +1091,31 @@ sol_object_t *sol_f_list_copy(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_list_insert(sol_state_t *state, sol_object_t *args) {
sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_cast_int(state, sol_list_get_index(state, args, 1)), *obj = sol_list_get_index(state, args, 2);
sol_list_insert(state, list, idx->ival, obj);
sol_obj_free(list);
return sol_incref(state->None);
sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_list_get_index(state, args, 1), *iidx = sol_cast_int(state, idx), *obj = sol_list_get_index(state, args, 2);
sol_list_insert(state, list, iidx->ival, obj);
sol_obj_free(list);
sol_obj_free(idx);
sol_obj_free(iidx);
sol_obj_free(obj);
return sol_incref(state->None);
}
sol_object_t *sol_f_list_remove(sol_state_t *state, sol_object_t *args) {
sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_cast_int(state, sol_list_get_index(state, args, 1));
sol_list_remove(state, list, idx->ival);
sol_obj_free(list);
return sol_incref(state->None);
sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_list_get_index(state, args, 1), *iidx = sol_cast_int(state, idx);
sol_object_t *res = sol_list_remove(state, list, iidx->ival);
sol_obj_free(list);
sol_obj_free(idx);
sol_obj_free(iidx);
return res;
}
sol_object_t *sol_f_list_truncate(sol_state_t *state, sol_object_t *args) {
sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_list_get_index(state, args, 1);
sol_object_t *res = sol_list_truncate(state, list, idx->ival);
sol_obj_free(list);
return res;
sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_list_get_index(state, args, 1), *iidx = sol_cast_int(state, idx);
sol_object_t *res = sol_list_truncate(state, list, iidx->ival);
sol_obj_free(list);
sol_obj_free(idx);
sol_obj_free(iidx);
return res;
}
sol_object_t *sol_f_list_map(sol_state_t *state, sol_object_t *args) {
@ -2304,7 +2318,7 @@ sol_object_t *sol_f_buffer_fromstring(sol_state_t *state, sol_object_t *args) {
sol_object_t *sol_f_buffer_fromobject(sol_state_t *state, sol_object_t *args) {
sol_object_t *obj = sol_list_get_index(state, args, 0);
sol_object_t *buf = sol_new_buffer(state, obj, sizeof(sol_object_t), OWN_CALLF, (sol_freefunc_t) sol_obj_free, (sol_movefunc_t) sol_obj_acquire);
sol_object_t *buf = sol_new_buffer(state, obj, sizeof(sol_object_t), OWN_CALLF, (sol_freefunc_t) state->obfuncs.destr, (sol_movefunc_t) state->obfuncs.copy);
//Keep ref to obj so buf remains alive
return buf;
}

42
dump.sol

@ -0,0 +1,42 @@
func dump(obj, indent)
if None == indent then
indent = 0
seen = {}
end
io.stdout:write(" "*indent)
if type(obj) == "list" then
buf = buffer.fromobject(obj)
addr = buf:address()
if None != seen[obj] then
print("...("+addr+")")
return
end
seen[obj] = 1
print("[")
for elem in obj do
dump(elem, indent+2)
end
print(" "*indent+"] =("+addr+")")
return
end
if type(obj) == "map" then
buf = buffer.fromobject(obj)
addr = buf:address()
if None != seen[obj] then
print("...("+addr+")")
return
end
seen[obj] = 1
print("{")
for key in obj do
io.stdout:write(" "*(indent+2))
prepr(key, ":")
dump(obj[key], indent+4)
end
print(" "*indent+"} =("+addr+")")
return
end
prepr(obj)
end
dump.closure.seen = {}

104
gc.c

@ -1,11 +1,9 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "sol.h"
#ifdef DEBUG_GC
#else
sol_object_t *sol_alloc_object(sol_state_t *state) {
sol_object_t *_sol_gc_alloc_object(sol_state_t *state) {
sol_object_t *res = malloc(sizeof(sol_object_t));
if(!res) {
sol_set_error(state, state->OutOfMemory);
@ -16,11 +14,7 @@ sol_object_t *sol_alloc_object(sol_state_t *state) {
return sol_incref(res);
}
sol_object_t *sol_obj_acquire(sol_object_t *obj) {
return sol_incref(obj);
}
void sol_obj_free(sol_object_t *obj) {
void _sol_gc_obj_free(sol_object_t *obj) {
if(!obj) {
printf("WARNING: Attempt to free NULL\n");
return;
@ -34,6 +28,93 @@ void sol_obj_free(sol_object_t *obj) {
}
}
sol_object_t *sol_obj_acquire(sol_object_t *obj) {
return sol_incref(obj);
}
#ifdef DEBUG_GC
static FILE *gclog = NULL;
static int gcrefcnt = 0;
static char gctime[64];
/*
char *prtime() {
time_t t;
struct tm t2;
time(&t);
localtime_r(&t, &t2);
strftime(gctime, 64, "%Y/%m/%d %T", &t2);
return gctime;
}
*/
char *prtime() {return "";}
void sol_mm_initialize(sol_state_t *state) {
if(gclog) {
fprintf(gclog, " === Reopened at %s ===\n", prtime());
} else {
gclog = fopen("gclog.txt", "a");
fprintf(gclog, "=== Opened at %s ===\n", prtime());
}
gcrefcnt++;
}
void sol_mm_finalize(sol_state_t *state) {
gcrefcnt--;
fprintf(gclog, "=== Closed at %s ===\n", prtime());
if(gcrefcnt <= 0) {
fflush(gclog);
fclose(gclog);
gclog = NULL;
}
}
sol_object_t *_int_sol_alloc_object(const char *func, sol_state_t *state) {
fprintf(gclog, "%s\t%s\tALLOC\n", prtime(), func);
return _sol_gc_alloc_object(state);
}
sol_object_t *_int_sol_incref(const char *func, sol_object_t *obj) {
int oldref = obj->refcnt++;
fprintf(gclog, "%s\t%s\tINCREF\t%s\t%p\t%d\t->\t%d\n", prtime(), func, obj->ops->tname, obj, oldref, obj->refcnt);
return obj;
}
void _int_sol_obj_free(const char *func, sol_object_t *obj) {
fprintf(gclog, "%s\t%s\tDECREF\t%s\t%p\t%d\t->\t%d\n", prtime(), func, obj->ops->tname, obj, obj->refcnt, obj->refcnt - 1);
_sol_gc_obj_free(obj);
}
void sol_obj_release(sol_object_t *obj) {
fprintf(gclog, "%s\t\tFREE\t%s\t%p\n", prtime(), obj->ops->tname, obj);
if(obj->ops->free) obj->ops->free(NULL, obj);
free(obj);
}
sol_object_t *_sol_gc_dsl_copier(sol_object_t *obj) {
fprintf(gclog, "%s\t<dsl>\tINCREF\t%s\t%p\t%d\t->\t%d\n", prtime(), obj->ops->tname, obj, obj->refcnt, ++obj->refcnt);
return obj;
}
void _sol_gc_dsl_destructor(sol_object_t *obj) {
fprintf(gclog, "%s\t<dsl>\tDECREF\t%s\t%p\t%d\t->\t%d\n", prtime(), obj->ops->tname, obj, obj->refcnt, obj->refcnt - 1);
_sol_gc_obj_free(obj);
}
#else
sol_object_t *sol_alloc_object(sol_state_t *state) {
return _sol_gc_alloc_object(state);
}
void sol_obj_free(sol_object_t *obj) {
_sol_gc_obj_free(obj);
}
void sol_obj_release(sol_object_t *obj) {
if(obj->ops->free) {
obj->ops->free(NULL, obj);
@ -41,4 +122,7 @@ void sol_obj_release(sol_object_t *obj) {
free(obj);
}
void sol_mm_initialize(sol_state_t *state) {}
void sol_mm_finalize(sol_state_t *state) {}
#endif

71
gcstat.py

@ -0,0 +1,71 @@
import sys
from collections import defaultdict
f = open('gclog.txt', 'r')
incs = defaultdict(lambda: 0)
decs = defaultdict(lambda: 0)
refs = defaultdict(lambda: 0)
seenrefs = {}
types = {}
lineno = 0
for line in f:
lineno += 1
if lineno % 10000 == 0:
print 'Processing line', lineno
if line[0] == '=':
continue
parts = line.split('\t')
if parts[2] == 'INCREF':
incs[parts[1]] += 1
refs[parts[4]] += 1
types[parts[4]] = parts[3]
seenrefs[parts[4]] = parts[7]
elif parts[2] == 'DECREF':
decs[parts[1]] += 1
refs[parts[4]] -= 1
types[parts[4]] = parts[3]
seenrefs[parts[4]] = parts[7]
incpairs = incs.items()
decpairs = decs.items()
refpairs = refs.items()
incpairs.sort(key=lambda it: it[1], reverse=True)
decpairs.sort(key=lambda it: it[1], reverse=True)
refpairs.sort(key=lambda it: it[1], reverse=True)
for k in seenrefs:
seenrefs[k] = int(seenrefs[k])
totincs = sum((i[1] for i in incpairs))
totdecs = sum((i[1] for i in decpairs))
totliving = sum((i[1] for i in refpairs))
totsol = sum(seenrefs.itervalues())
out = open('gcstat.txt', 'w')
sys.stdout = out
print '=== Totals ==='
print '= Increfs:', totincs
print '= Decrefs:', totdecs
print '= Diff:', totincs - totdecs
print '= Living (our estimate):', totliving
print '= Living (according to Sol):', totsol
print '=== Functions, sorted by increments ==='
print '= %-30s%-8s%-8s'%('name', 'incs', 'decs')
for func, inccnt in incpairs:
print '%-32s%-8d%-8d'%(func, inccnt, decs[func])
print '=== Functions, sorted by decrements ==='
print '= %-30s%-8s%-8s'%('name', 'decs', 'incs')
for func, deccnt in decpairs:
print '%-32s%-8d%-8d'%(func, deccnt, incs[func])
print '=== Objects alive at cleanup ==='
print '= %-14s%-16s%-8s%-8s'%('addr', 'type', 'refs', 'solrefs')
for addr, refcnt in refpairs:
print '%-16s%-16s%-8d%-8d'%(addr, types[addr], refcnt, seenrefs[addr])

1884
lex.yy.c
File diff suppressed because it is too large
View File

4
object.c

@ -330,7 +330,7 @@ sol_object_t *sol_map_mcell(sol_state_t *state, sol_object_t *map, sol_object_t
int sol_map_has(sol_state_t *state, sol_object_t *map, sol_object_t *key) {
sol_object_t *mcell = sol_map_mcell(state, map, key);
int res = sol_is_none(state, mcell);
sol_obj_free(mcell);
if(sol_is_none(state, mcell)) sol_decref(mcell);
return res;
}
@ -637,4 +637,4 @@ sol_object_t *sol_f_stream_free(sol_state_t *state, sol_object_t *stream) {
//printf("IO: Closing open file\n");
fclose(stream->stream);
return stream;
}
}

8339
parser.output
File diff suppressed because it is too large
View File

4707
parser.tab.c
File diff suppressed because it is too large
View File

162
parser.tab.h

@ -1,8 +1,8 @@
/* A Bison parser, made by GNU Bison 2.7.12-4996. */
/* A Bison parser, made by GNU Bison 3.0.2. */
/* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc.
Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -32,7 +32,7 @@
#ifndef YY_YY_PARSER_TAB_H_INCLUDED
# define YY_YY_PARSER_TAB_H_INCLUDED
/* Enabling traces. */
/* Debug traces. */
#ifndef YYDEBUG
# define YYDEBUG 1
#endif
@ -40,105 +40,95 @@
extern int yydebug;
#endif
/* Tokens. */
/* Token type. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
IF = 258,
THEN = 259,
ELSE = 260,
WHILE = 261,
FOR = 262,
IN = 263,
DO = 264,
FUNC = 265,
RETURN = 266,
BREAK = 267,
CONTINUE = 268,
END = 269,
NONE = 270,
IDENT = 271,
INT = 272,
FLOAT = 273,
STRING = 274,
PLUS = 275,
MINUS = 276,
STAR = 277,
SLASH = 278,
PERCENT = 279,
DSTAR = 280,
BAND = 281,
BOR = 282,
BXOR = 283,
BNOT = 284,
LAND = 285,
LOR = 286,
LNOT = 287,
ASSIGN = 288,
ASSIGNPLUS = 289,
ASSIGNMINUS = 290,
ASSIGNSTAR = 291,
ASSIGNSLASH = 292,
ASSIGNDSTAR = 293,
ASSIGNBAND = 294,
ASSIGNBOR = 295,
ASSIGNBXOR = 296,
EQUAL = 297,
LESS = 298,
GREATER = 299,
LESSEQ = 300,
GREATEREQ = 301,
RSHIFT = 302,
LSHIFT = 303,
LBRACE = 304,
RBRACE = 305,
LPAREN = 306,
RPAREN = 307,
LBRACKET = 308,
RBRACKET = 309,
DOT = 310,
COLON = 311,
SEMICOLON = 312,
COMMA = 313,
POUND = 314
};
enum yytokentype
{
IF = 258,
THEN = 259,
ELSE = 260,
WHILE = 261,
FOR = 262,
IN = 263,
DO = 264,
FUNC = 265,
RETURN = 266,
BREAK = 267,
CONTINUE = 268,
END = 269,
NONE = 270,
IDENT = 271,
INT = 272,
FLOAT = 273,
STRING = 274,
PLUS = 275,
MINUS = 276,
STAR = 277,
SLASH = 278,
PERCENT = 279,
DSTAR = 280,
BAND = 281,
BOR = 282,
BXOR = 283,
BNOT = 284,
LAND = 285,
LOR = 286,
LNOT = 287,
ASSIGN = 288,
ASSIGNPLUS = 289,
ASSIGNMINUS = 290,
ASSIGNSTAR = 291,
ASSIGNSLASH = 292,
ASSIGNDSTAR = 293,
ASSIGNBAND = 294,
ASSIGNBOR = 295,
ASSIGNBXOR = 296,
EQUAL = 297,
NEQUAL = 298,
LESS = 299,
GREATER = 300,
LESSEQ = 301,
GREATEREQ = 302,
RSHIFT = 303,
LSHIFT = 304,
LBRACE = 305,
RBRACE = 306,
LPAREN = 307,
RPAREN = 308,
LBRACKET = 309,
RBRACKET = 310,
DOT = 311,
COLON = 312,
SEMICOLON = 313,
COMMA = 314,
POUND = 315
};
#endif
/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef int YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif
/* Location type. */
#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
typedef struct YYLTYPE {
int first_line;
int first_column;
int last_line;
int last_column;
} YYLTYPE;
# define yyltype YYLTYPE /* obsolescent; will be withdrawn */
typedef struct YYLTYPE YYLTYPE;
struct YYLTYPE
{
int first_line;
int first_column;
int last_line;
int last_column;
};
# define YYLTYPE_IS_DECLARED 1
# define YYLTYPE_IS_TRIVIAL 1
#endif
#ifdef YYPARSE_PARAM
#if defined __STDC__ || defined __cplusplus
int yyparse (void *YYPARSE_PARAM);
#else
int yyparse ();
#endif
#else /* ! YYPARSE_PARAM */
#if defined __STDC__ || defined __cplusplus
int yyparse (stmt_node **program);
#else
int yyparse ();
#endif
#endif /* ! YYPARSE_PARAM */
#endif /* !YY_YY_PARSER_TAB_H_INCLUDED */

3
parser.y

@ -19,7 +19,7 @@
%token INT FLOAT STRING
%token PLUS MINUS STAR SLASH PERCENT DSTAR BAND BOR BXOR BNOT LAND LOR LNOT
%token ASSIGN ASSIGNPLUS ASSIGNMINUS ASSIGNSTAR ASSIGNSLASH ASSIGNDSTAR ASSIGNBAND ASSIGNBOR ASSIGNBXOR
%token EQUAL LESS GREATER LESSEQ GREATEREQ RSHIFT LSHIFT
%token EQUAL NEQUAL LESS GREATER LESSEQ GREATEREQ RSHIFT LSHIFT
%token LBRACE RBRACE LPAREN RPAREN LBRACKET RBRACKET DOT COLON SEMICOLON COMMA POUND
%parse-param {stmt_node **program}
@ -258,6 +258,7 @@ ulogic_expr:
rel_expr:
term_expr EQUAL rel_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_EQUAL; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
| term_expr NEQUAL rel_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_NEQUAL; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
| term_expr LESS rel_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_LESS; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
| term_expr GREATER rel_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_GREATER; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
| term_expr LESSEQ rel_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_LESSEQ; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }

4
runtime.c

@ -306,6 +306,10 @@ sol_object_t *sol_eval_inner(sol_state_t *state, expr_node *expr, jmp_buf jmp) {
res = sol_new_int(state, BOOL_TO_INT(sol_cast_int(state, left->ops->cmp(state, list))->ival == 0));
break;
case OP_NEQUAL:
res = sol_new_int(state, BOOL_TO_INT(sol_cast_int(state, left->ops->cmp(state, list))->ival != 0));
break;
case OP_LESS:
res = sol_new_int(state, BOOL_TO_INT(sol_cast_int(state, left->ops->cmp(state, list))->ival < 0));
break;

19
sol.h

@ -9,8 +9,8 @@
#include <stdarg.h>
#include "dsl/dsl.h"
#define VERSION "0.1a0"
#define HEXVER 0x0001A00
#define VERSION "0.1a1"
#define HEXVER 0x0001A01
// Forward declarations:
struct sol_tag_object_t;
@ -181,9 +181,6 @@ typedef struct sol_tag_state_t {
sol_object_t *modules;
sol_object_t *methods;
dsl_object_funcs obfuncs;
#ifdef DEBUG_GC
dsl_seq *objects;
#endif
} sol_state_t;
// state.c
@ -506,14 +503,23 @@ sol_object_t *sol_util_call(sol_state_t *, sol_object_t *, int *, int, ...);
sol_object_t *_int_sol_incref(const char *, sol_object_t *);
void _int_sol_obj_free(const char *, sol_object_t *);
sol_object_t *_sol_gc_dsl_copier(sol_object_t *);
void _sol_gc_dsl_destructor(sol_object_t *);
sol_object_t *_int_sol_alloc_object(const char *, sol_state_t *);
#define sol_incref(obj) (_int_sol_incref(__func__, (obj)))
#define sol_obj_free(obj) (_int_sol_obj_free(__func__, (obj)))
#define sol_alloc_object(state) (_int_sol_alloc_object(__func__, (state)))
#else
#define sol_incref(obj) (++((obj)->refcnt), obj)
void sol_obj_free(sol_object_t *);
sol_object_t *sol_alloc_object(sol_state_t *);
#endif
#define sol_decref(obj) (--((obj)->refcnt))
@ -521,8 +527,7 @@ void sol_obj_free(sol_object_t *);
sol_object_t *sol_obj_acquire(sol_object_t *);
void sol_obj_release(sol_object_t *);
sol_object_t *sol_alloc_object(sol_state_t *);
void sol_mm_initialize(sol_state_t *);
void sol_mm_finalize(sol_state_t *);
#define AS_OBJ(x) ((sol_object_t *) (x))

5
solrun.c

@ -55,6 +55,11 @@ int main(int argc, char **argv) {
ob_print(state.error);
printf("\n");
}
if(state.ret) {
printf("Toplevel return: ");
ob_print(state.ret);
printf("\n");
}
//st_free(program);
sol_state_cleanup(&state);
return 0;

16
state.c

@ -6,6 +6,8 @@ int sol_state_init(sol_state_t *state) {
sol_object_t *globals, *mod, *meths;
sol_object_t *btype, *bsize, *bobj;
sol_mm_initialize(state);
state->None = NULL;
state->OutOfMemory = NULL;
state->scopes = NULL;
@ -14,6 +16,12 @@ int sol_state_init(sol_state_t *state) {
state->ret = NULL;
state->sflag = SF_NORMAL;
#ifdef DEBUG_GC
// This is necessary for DEBUG_GC's early allocation; it gets overwritten,
// unfortunately, during sol_ops_init below, so it's duplicated.
state->SingletOps.tname = "singlet";
#endif
// If any of the following fail, some very weird things are happening.
if(!(state->None = sol_new_singlet(state, "None"))) {
goto cleanup;
@ -155,8 +163,13 @@ int sol_state_init(sol_state_t *state) {
state->StreamOps.free = sol_f_stream_free;
state->StreamOps.tostring = sol_f_stream_tostring;
#ifdef DEBUG_GC
state->obfuncs.copy = (dsl_copier) _sol_gc_dsl_copier;
state->obfuncs.destr = (dsl_destructor) _sol_gc_dsl_destructor;
#else
state->obfuncs.copy = (dsl_copier) sol_obj_acquire;
state->obfuncs.destr = (dsl_destructor) sol_obj_free;
#endif
state->error = state->None;
state->scopes = sol_new_list(state);
@ -246,6 +259,7 @@ int sol_state_init(sol_state_t *state) {
sol_map_set_name(state, mod, "OP_LAND", sol_new_int(state, OP_LAND));
sol_map_set_name(state, mod, "OP_LOR", sol_new_int(state, OP_LOR));
sol_map_set_name(state, mod, "OP_EQUAL", sol_new_int(state, OP_EQUAL));
sol_map_set_name(state, mod, "OP_NEQUAL", sol_new_int(state, OP_NEQUAL));
sol_map_set_name(state, mod, "OP_LESS", sol_new_int(state, OP_LESS));
sol_map_set_name(state, mod, "OP_GREATER", sol_new_int(state, OP_GREATER));
sol_map_set_name(state, mod, "OP_LESSEQ", sol_new_int(state, OP_LESSEQ));
@ -417,6 +431,7 @@ void sol_state_cleanup(sol_state_t *state) {
}
sol_obj_free(state->modules);
sol_obj_free(state->methods);
sol_mm_finalize(state);
}
sol_object_t *sol_state_resolve(sol_state_t *state, sol_object_t *key) {
@ -443,6 +458,7 @@ sol_object_t *sol_state_resolve(sol_state_t *state, sol_object_t *key) {
if(!sol_is_none(state, temp)) {
return temp;
}
// sol_obj_free(temp);
return sol_incref(state->None);
}

1
test_monty.sol

@ -8,3 +8,4 @@ print('Resulting statement:')
ast.print(stmt)
print('---Running results begin here---')
stmt()
io.stdin:read(io.LINE)

2
tokenizer.lex

@ -164,6 +164,8 @@ None { return NONE; }
"==" { return EQUAL; }
"!=" { return NEQUAL; }
"<" { return LESS; }
">" { return GREATER; }

Loading…
Cancel
Save