Browse Source

Sol Part 11: Ode to the Semicolon

master
Grissess 7 years ago
parent
commit
de533b0922
  1. 15
      builtins.c
  2. 2
      sol.h
  3. 3
      state.c
  4. 1
      subtest.sol
  5. 8
      test.sol

15
builtins.c

@ -249,35 +249,37 @@ sol_object_t *sol_f_range(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_exec(sol_state_t *state, sol_object_t *args) {
sol_object_t *prg = sol_list_get_index(state, args, 0), prgstr = sol_cast_string(state, prg);
sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
stmt_node *program;
program = sol_compile(prgstr->str);
if(!program) {
return sol_set_error_string(state, "Compilation failure"));
return sol_set_error_string(state, "Compilation failure");
}
// XXX should st_free(program);
sol_exec(state, program);
return sol_incref(state->None);
}
sol_object_t *sol_f_eval(sol_state_t *state, sol_object_t *args) {
sol_object_t *prg = sol_list_get_index(state, args, 0), prgstr = sol_cast_string(state, prg);
sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
stmt_node *program;
program = sol_compile(prgstr->str);
if(!program) {
return sol_set_error_string(state, "Compilation failure");
}
if(program->type != ST_EXPR) {
if(program->type != ST_LIST || program->stmtlist->stmt->type != ST_EXPR) {
return sol_set_error_string(state, "Not an expression");
}
// XXX should st_free(program);
return sol_eval(state, program->expr);
return sol_eval(state, program->stmtlist->stmt->expr);
}
sol_object_t *sol_f_execfile(sol_state_t *state, sol_object_t *args) {
sol_object_t *prg = sol_list_get_index(state, args, 0), prgstr = sol_cast_string(state, prg);
sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
stmt_node *program;
FILE *f = fopen(prgstr->str, "r");
char *s;
@ -303,6 +305,7 @@ sol_object_t *sol_f_execfile(sol_state_t *state, sol_object_t *args) {
if(!program) {
return sol_set_error_string(state, "Compilation failure");
}
// XXX should st_free(program);
sol_exec(state, program);
return sol_incref(state->None);

2
sol.h

@ -148,7 +148,7 @@ sol_object_t *sol_f_rawget(sol_state_t *, sol_object_t *);
sol_object_t *sol_f_rawset(sol_state_t *, sol_object_t *);
sol_object_t *sol_f_range(sol_state_t *, sol_object_t *);
sol_object_t *sol_f_exec(sol_state_t *, sol_object_t *);
sol_object_t *sol_f_eval(sol_state_t *, sol_object_t *;)
sol_object_t *sol_f_eval(sol_state_t *, sol_object_t *;);
sol_object_t *sol_f_execfile(sol_state_t *, sol_object_t *);
sol_object_t *sol_f_debug_getref(sol_state_t *, sol_object_t *);

3
state.c

@ -121,6 +121,9 @@ int sol_state_init(sol_state_t *state) {
sol_map_set_name(state, globals, "rawget", sol_new_cfunc(state, sol_f_rawget));
sol_map_set_name(state, globals, "rawset", sol_new_cfunc(state, sol_f_rawset));
sol_map_set_name(state, globals, "range", sol_new_cfunc(state, sol_f_range));
sol_map_set_name(state, globals, "exec", sol_new_cfunc(state, sol_f_exec));
sol_map_set_name(state, globals, "eval", sol_new_cfunc(state, sol_f_eval));
sol_map_set_name(state, globals, "execfile", sol_new_cfunc(state, sol_f_execfile));
sol_map_set_name(state, debug, "getref", sol_new_cfunc(state, sol_f_debug_getref));
sol_map_set_name(state, debug, "setref", sol_new_cfunc(state, sol_f_debug_setref));

1
subtest.sol

@ -0,0 +1 @@
print("Hello from subtest!")

8
test.sol

@ -170,3 +170,11 @@ print(l)
print('--- Map/filter chain')
print([1 2 3 4 5]:map(func (i) return i * 3 end):filter(func (i) return i & 1 end))
print('--- Exec/eval')
exec('print("Hello from exec!")')
print(eval('5 + 3'))
execfile('subtest.sol')
print('--- All done!')
Loading…
Cancel
Save