Browse Source

Sol Part 34: When Life Gives You Language, Make Solade!

(should fix #4 )
master
Graham Northup 6 years ago
parent
commit
1073d60787
  1. 4
      .gitignore
  2. 9
      ast.h
  3. 2
      build.sh
  4. 67
      builtins.c
  5. 14
      object.c
  6. 76
      runtime.c
  7. 22
      sol.h
  8. 80
      state.c

4
.gitignore

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

9
ast.h

@ -184,6 +184,15 @@ typedef struct tag_stmt_node {
nd->binop->left->index->index = idx; \
nd->binop->right = val
#define BOOL_TO_INT(cond) ((cond)?1:0)
#define CALL_METHOD(state, obj, meth, args) ({\
sol_object_t *res;\
state->calling_type = obj->ops->tname;\
state->calling_meth = #meth;\
res = obj->ops->meth(state, args);\
state->calling_type = "(none)";\
state->calling_meth = "(none)";\
res;\
})
sol_object_t *sol_new_func(sol_state_t *, identlist_node *, stmt_node *, char *);
sol_object_t *sol_new_stmtnode(sol_state_t *, stmt_node *);

2
build.sh

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

67
builtins.c

@ -26,7 +26,9 @@ static char *_ftoa(double f) {
}
sol_object_t *sol_f_not_impl(sol_state_t *state, sol_object_t *args) {
return sol_set_error_string(state, "Undefined method");
char buffer[64];
snprintf(buffer, 64, "Undefined method (%s on %s)", state->calling_meth, state->calling_type);
return sol_set_error_string(state, buffer);
}
sol_object_t *sol_f_default_cmp(sol_state_t *state, sol_object_t *args) {
@ -46,7 +48,7 @@ sol_object_t *sol_f_default_tostring(sol_state_t *state, sol_object_t *args) {
}
sol_object_t *sol_f_default_repr(sol_state_t *state, sol_object_t *args) {
sol_object_t *obj = sol_list_get_index(state, args, 0), *res = obj->ops->tostring(state, args);
sol_object_t *obj = sol_list_get_index(state, args, 0), *res = CALL_METHOD(state, obj, tostring, args);
sol_obj_free(obj);
return res;
}
@ -60,21 +62,21 @@ sol_object_t *sol_f_no_op(sol_state_t *state, sol_object_t *args) {
sol_object_t *sol_f_toint(sol_state_t *state, sol_object_t *args) {
sol_object_t *obj = sol_list_get_index(state, args, 0);
sol_object_t *res = obj->ops->toint(state, args);
sol_object_t *res = CALL_METHOD(state, obj, toint, args);
sol_obj_free(obj);
return res;
}
sol_object_t *sol_f_tofloat(sol_state_t *state, sol_object_t *args) {
sol_object_t *obj = sol_list_get_index(state, args, 0);
sol_object_t *res = obj->ops->tofloat(state, args);
sol_object_t *res = CALL_METHOD(state, obj, tofloat, args);
sol_obj_free(obj);
return res;
}
sol_object_t *sol_f_tostring(sol_state_t *state, sol_object_t *args) {
sol_object_t *obj = sol_list_get_index(state, args, 0);
sol_object_t *res = obj->ops->tostring(state, args);
sol_object_t *res = CALL_METHOD(state, obj, tostring, args);
sol_obj_free(obj);
return res;
}
@ -84,7 +86,7 @@ sol_object_t *sol_f_try(sol_state_t *state, sol_object_t *args) {
sol_object_t *ls = sol_new_list(state), *one = sol_new_int(state, 1);
sol_object_t *res;
sol_list_insert(state, fargs, 0, func);
res = func->ops->call(state, fargs);
res = CALL_METHOD(state, func, call, fargs);
sol_obj_free(func);
sol_obj_free(fargs);
if(sol_has_error(state)) {
@ -452,24 +454,31 @@ sol_object_t *sol_f_debug_scopes(sol_state_t *state, sol_object_t *args) {
return sol_incref(state->scopes);
}
void _sol_freef_seq_iter(void *iter, size_t sz) {
dsl_free_seq_iter((dsl_seq_iter *) iter);
}
sol_object_t *sol_f_iter_str(sol_state_t *state, sol_object_t *args) {
sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
sol_object_t *index = sol_map_get_name(state, local, "idx"), *max = sol_map_get_name(state, local, "sz"), *res;
char temp[2] = {0, 0};
if(sol_is_none(state, index)) {
if(sol_is_none(state, index) || sol_is_none(state, max)) {
sol_obj_free(index);
index = sol_new_int(state, 0);
index = sol_new_buffer(state, (void *) 0, sizeof(void *), OWN_NONE, NULL, NULL);
sol_map_set_name(state, local, "idx", index);
sol_obj_free(max);
max = sol_new_int(state, strlen(obj->str));
sol_map_set_name(state, local, "sz", max);
}
if(index->ival >= strlen(obj->str)) {
if(((size_t) index->buffer) >= max->ival) {
sol_obj_free(index);
sol_obj_free(obj);
sol_obj_free(local);
return sol_incref(state->StopIteration);
}
temp[0] = obj->str[index->ival];
temp[0] = obj->str[((size_t) index->buffer)];
res = sol_new_string(state, temp);
index->ival++;
index->buffer = (void *) ((size_t) index->buffer + 1);
sol_obj_free(index);
sol_obj_free(local);
sol_obj_free(obj);
@ -481,18 +490,18 @@ sol_object_t *sol_f_iter_list(sol_state_t *state, sol_object_t *args) {
sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
if(sol_is_none(state, index)) {
sol_obj_free(index);
index = sol_new_int(state, 0);
index = sol_new_buffer(state, dsl_new_seq_iter(obj->seq), sizeof(dsl_seq_iter), OWN_CALLF, _sol_freef_seq_iter, NULL);
sol_map_set_name(state, local, "idx", index);
sol_obj_free(index);
}
if(index->ival >= sol_list_len(state, obj)) {
if(dsl_seq_iter_is_invalid(index->buffer)) {
sol_obj_free(index);
sol_obj_free(obj);
sol_obj_free(local);
return sol_incref(state->StopIteration);
}
res = sol_list_get_index(state, obj, index->ival);
index->ival++;
res = sol_incref(AS_OBJ(dsl_seq_iter_at(index->buffer)));
dsl_seq_iter_next(index->buffer);
sol_obj_free(local);
sol_obj_free(obj);
return res;
@ -503,18 +512,18 @@ sol_object_t *sol_f_iter_map(sol_state_t *state, sol_object_t *args) {
sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
if(sol_is_none(state, index)) {
sol_obj_free(index);
index = sol_new_int(state, 0);
index = sol_new_buffer(state, dsl_new_seq_iter(obj->seq), sizeof(dsl_seq_iter), OWN_CALLF, _sol_freef_seq_iter, NULL);
sol_map_set_name(state, local, "idx", index);
sol_obj_free(index);
}
if(index->ival >= dsl_seq_len(obj->seq)) {
if(dsl_seq_iter_is_invalid(index->buffer)) {
sol_obj_free(index);
sol_obj_free(obj);
sol_obj_free(local);
return sol_incref(state->StopIteration);
}
res = sol_incref(AS_OBJ(dsl_seq_get(obj->seq, index->ival))->key);
index->ival++;
res = sol_incref(AS_OBJ(dsl_seq_iter_at(index->buffer))->key);
dsl_seq_iter_next(index->buffer);
sol_obj_free(local);
sol_obj_free(obj);
return res;
@ -1127,7 +1136,7 @@ sol_object_t *sol_f_list_map(sol_state_t *state, sol_object_t *args) {
item = sol_list_get_index(state, list, idx);
sol_list_insert(state, fargs, 1, item);
sol_obj_free(item);
item = func->ops->call(state, fargs);
item = CALL_METHOD(state, func, call, fargs);
if(sol_has_error(state)) {
return list;
}
@ -1150,7 +1159,7 @@ sol_object_t *sol_f_list_filter(sol_state_t *state, sol_object_t *args) {
item = sol_list_get_index(state, list, idx);
sol_list_insert(state, fargs, 1, item);
sol_obj_free(item);
item = func->ops->call(state, fargs);
item = CALL_METHOD(state, func, call, fargs);
if(sol_has_error(state)) {
return list;
}
@ -1195,13 +1204,13 @@ sol_object_t *sol_f_map_index(sol_state_t *state, sol_object_t *args) {
newls = sol_new_list(state);
sol_list_insert(state, newls, 0, indexf);
sol_list_append(state, newls, args);
res = indexf->ops->call(state, newls);
res = CALL_METHOD(state, indexf, call, newls);
sol_obj_free(newls);
} else if(indexf->ops->index && indexf->ops->index != sol_f_not_impl) {
newls = sol_new_list(state);
sol_list_insert(state, newls, 0, indexf);
sol_list_insert(state, newls, 1, b);
res = indexf->ops->index(state, newls);
res = CALL_METHOD(state, indexf, index, newls);
sol_obj_free(newls);
}
}
@ -1221,7 +1230,7 @@ sol_object_t *sol_f_map_setindex(sol_state_t *state, sol_object_t *args) {
newls = sol_new_list(state);
sol_list_insert(state, newls, 0, setindexf);
sol_list_append(state, newls, args);
sol_obj_free(setindexf->ops->call(state, newls));
sol_obj_free(CALL_METHOD(state, setindexf, call, newls));
sol_obj_free(newls);
return sol_incref(state->None);
} else if(setindexf->ops->setindex && setindexf->ops->setindex != sol_f_not_impl) {
@ -1229,7 +1238,7 @@ sol_object_t *sol_f_map_setindex(sol_state_t *state, sol_object_t *args) {
sol_list_insert(state, newls, 0, setindexf);
sol_list_insert(state, newls, 1, b);
sol_list_insert(state, newls, 2, val);
sol_obj_free(setindexf->ops->index(state, newls));
sol_obj_free(CALL_METHOD(state, setindexf, index, newls));
sol_obj_free(newls);
return sol_incref(state->None);
}
@ -1249,7 +1258,7 @@ sol_object_t *sol_f_map_call(sol_state_t *state, sol_object_t *args) {
if(callf->ops->call) {
sol_list_insert(state, fargs, 0, callf);
sol_list_insert(state, fargs, 1, map);
res = callf->ops->call(state, fargs);
res = CALL_METHOD(state, callf, call, fargs);
}
}
sol_obj_free(map);
@ -1279,7 +1288,7 @@ sol_object_t *sol_f_map_tostring(sol_state_t *state, sol_object_t *args) {
fargs = sol_new_list(state);
sol_list_insert(state, fargs, 0, tostrf);
sol_list_insert(state, fargs, 1, map);
res = tostrf->ops->call(state, fargs);
res = CALL_METHOD(state, tostrf, call, fargs);
sol_obj_free(fargs);
} else {
res = sol_cast_repr(state, map);
@ -1298,7 +1307,7 @@ sol_object_t *sol_f_map_repr(sol_state_t *state, sol_object_t *args) {
fargs = sol_new_list(state);
sol_list_insert(state, fargs, 0, reprf);
sol_list_insert(state, fargs, 1, obj);
cur = reprf->ops->call(state, fargs);
cur = CALL_METHOD(state, reprf, call, fargs);
sol_obj_free(fargs);
sol_obj_free(obj);
sol_obj_free(reprf);

14
object.c

@ -1,4 +1,4 @@
#include "sol.h"
#include "ast.h" // For CALL_METHOD
#include <stdlib.h>
#include <string.h>
@ -73,7 +73,13 @@ void sol_init_object(sol_state_t *state, sol_object_t *obj) {
}
sol_object_t *sol_new_int(sol_state_t *state, long i) {
sol_object_t *res = sol_alloc_object(state);
sol_object_t *res;
#ifdef SOL_ICACHE
if(!state->icache_bypass && i >= SOL_ICACHE_MIN && i <= SOL_ICACHE_MAX) {
return sol_incref(state->icache[i - SOL_ICACHE_MIN]);
}
#endif
res = sol_alloc_object(state);
res->type = SOL_INTEGER;
res->ival = i;
res->ops = &(state->IntOps);
@ -311,7 +317,7 @@ sol_object_t *sol_map_mcell(sol_state_t *state, sol_object_t *map, sol_object_t
sol_list_insert(state, list, 1, state->None);
if(!dsl_seq_iter_is_invalid(iter)) do {
sol_list_set_index(state, list, 1, AS_OBJ(dsl_seq_iter_at(iter))->key);
cmp = key->ops->cmp(state, list);
cmp = CALL_METHOD(state, key, cmp, list);
icmp = sol_cast_int(state, cmp);
sol_obj_free(cmp);
if(icmp->ival == 0) {
@ -488,7 +494,7 @@ sol_object_t *sol_f_buffer_free(sol_state_t *state, sol_object_t *buf) {
break;
case OWN_CALLF:
buf->freef(buf->buffer, buf->sz);
if(buf->freef) buf->freef(buf->buffer, buf->sz);
break;
}
return buf;

76
runtime.c

@ -247,39 +247,39 @@ sol_object_t *sol_eval_inner(sol_state_t *state, expr_node *expr, jmp_buf jmp) {
sol_list_insert(state, list, 1, right);
switch(expr->binop->type) {
case OP_ADD:
res = left->ops->add(state, list);
res = CALL_METHOD(state, left, add, list);
break;
case OP_SUB:
res = left->ops->sub(state, list);
res = CALL_METHOD(state, left, sub, list);
break;
case OP_MUL:
res = left->ops->mul(state, list);
res = CALL_METHOD(state, left, mul, list);
break;
case OP_DIV:
res = left->ops->div(state, list);
res = CALL_METHOD(state, left, div, list);
break;
case OP_MOD:
res = left->ops->mod(state, list);
res = CALL_METHOD(state, left, mod, list);
break;
case OP_POW:
res = left->ops->pow(state, list);
res = CALL_METHOD(state, left, pow, list);
break;
case OP_BAND:
res = left->ops->band(state, list);
res = CALL_METHOD(state, left, band, list);
break;
case OP_BOR:
res = left->ops->bor(state, list);
res = CALL_METHOD(state, left, bor, list);
break;
case OP_BXOR:
res = left->ops->bxor(state, list);
res = CALL_METHOD(state, left, bxor, list);
break;
case OP_LAND:
@ -303,35 +303,59 @@ sol_object_t *sol_eval_inner(sol_state_t *state, expr_node *expr, jmp_buf jmp) {
break;
case OP_EQUAL:
res = sol_new_int(state, BOOL_TO_INT(sol_cast_int(state, left->ops->cmp(state, list))->ival == 0));
value = CALL_METHOD(state, left, cmp, list);
lint = sol_cast_int(state, value);
res = sol_new_int(state, BOOL_TO_INT(lint->ival == 0));
sol_obj_free(lint);
sol_obj_free(value);
break;
case OP_NEQUAL:
res = sol_new_int(state, BOOL_TO_INT(sol_cast_int(state, left->ops->cmp(state, list))->ival != 0));
value = CALL_METHOD(state, left, cmp, list);
lint = sol_cast_int(state, value);
res = sol_new_int(state, BOOL_TO_INT(lint->ival != 0));
sol_obj_free(lint);
sol_obj_free(value);
break;
case OP_LESS:
res = sol_new_int(state, BOOL_TO_INT(sol_cast_int(state, left->ops->cmp(state, list))->ival < 0));
value = CALL_METHOD(state, left, cmp, list);
lint = sol_cast_int(state, value);
res = sol_new_int(state, BOOL_TO_INT(lint->ival < 0));
sol_obj_free(lint);
sol_obj_free(value);
break;
case OP_GREATER:
res = sol_new_int(state, BOOL_TO_INT(sol_cast_int(state, left->ops->cmp(state, list))->ival > 0));
value = CALL_METHOD(state, left, cmp, list);
lint = sol_cast_int(state, value);
res = sol_new_int(state, BOOL_TO_INT(lint->ival > 0));
sol_obj_free(lint);
sol_obj_free(value);
break;
case OP_LESSEQ:
res = sol_new_int(state, BOOL_TO_INT(sol_cast_int(state, left->ops->cmp(state, list))->ival <= 0));
value = CALL_METHOD(state, left, cmp, list);
lint = sol_cast_int(state, value);
res = sol_new_int(state, BOOL_TO_INT(lint->ival <= 0));
sol_obj_free(lint);
sol_obj_free(value);
break;
case OP_GREATEREQ:
res = sol_new_int(state, BOOL_TO_INT(sol_cast_int(state, left->ops->cmp(state, list))->ival >= 0));
value = CALL_METHOD(state, left, cmp, list);
lint = sol_cast_int(state, value);
res = sol_new_int(state, BOOL_TO_INT(lint->ival >= 0));
sol_obj_free(lint);
sol_obj_free(value);
break;
case OP_LSHIFT:
res = left->ops->blsh(state, list);
res = CALL_METHOD(state, left, blsh, list);
break;
case OP_RSHIFT:
res = left->ops->brsh(state, list);
res = CALL_METHOD(state, left, brsh, list);
break;
}
sol_obj_free(list);
@ -351,12 +375,12 @@ sol_object_t *sol_eval_inner(sol_state_t *state, expr_node *expr, jmp_buf jmp) {
case OP_NEG:
right = sol_new_int(state, -1);
sol_list_insert(state, list, 1, right);
res = left->ops->mul(state, list);
res = CALL_METHOD(state, left, mul, list);
sol_obj_free(right);
break;
case OP_BNOT:
res = left->ops->bnot(state, list);
res = CALL_METHOD(state, left, bnot, list);
break;
case OP_LNOT:
@ -367,7 +391,7 @@ sol_object_t *sol_eval_inner(sol_state_t *state, expr_node *expr, jmp_buf jmp) {
break;
case OP_LEN:
res = left->ops->len(state, list);
res = CALL_METHOD(state, left, len, list);
break;
}
sol_obj_free(left);
@ -385,7 +409,7 @@ sol_object_t *sol_eval_inner(sol_state_t *state, expr_node *expr, jmp_buf jmp) {
ERR_CHECK(state);
sol_list_insert(state, list, 0, left);
sol_list_insert(state, list, 1, right);
res = left->ops->index(state, list);
res = CALL_METHOD(state, left, index, list);
sol_obj_free(left);
sol_obj_free(right);
sol_obj_free(list);
@ -405,7 +429,7 @@ sol_object_t *sol_eval_inner(sol_state_t *state, expr_node *expr, jmp_buf jmp) {
sol_list_insert(state, list, 0, left);
sol_list_insert(state, list, 1, right);
sol_list_insert(state, list, 2, value);
res = left->ops->setindex(state, list);
res = CALL_METHOD(state, left, setindex, list);
sol_obj_free(left);
sol_obj_free(right);
sol_obj_free(value);
@ -439,7 +463,7 @@ sol_object_t *sol_eval_inner(sol_state_t *state, expr_node *expr, jmp_buf jmp) {
ERR_CHECK(state);
cure = cure->next;
}
res = value->ops->call(state, list);
res = CALL_METHOD(state, value, call, list);
sol_obj_free(value);
sol_obj_free(list);
ERR_CHECK(state);
@ -532,7 +556,7 @@ void sol_exec(sol_state_t *state, stmt_node *stmt) {
if(value->ops->iter && value->ops->iter != sol_f_not_impl) {
list = sol_new_list(state);
sol_list_insert(state, list, 0, value);
iter = value->ops->iter(state, list);
iter = CALL_METHOD(state, value, iter, list);
sol_obj_free(list);
} else {
iter = value;
@ -545,7 +569,7 @@ void sol_exec(sol_state_t *state, stmt_node *stmt) {
sol_list_insert(state, list, 0, iter);
sol_list_insert(state, list, 1, value);
sol_list_insert(state, list, 2, sol_new_map(state));
item = iter->ops->call(state, list);
item = CALL_METHOD(state, iter, call, list);
while(item != state->StopIteration) {
sol_state_assign_l_name(state, stmt->iter->var, item);
sol_exec(state, stmt->iter->loop);
@ -554,7 +578,7 @@ void sol_exec(sol_state_t *state, stmt_node *stmt) {
item = sol_incref(state->StopIteration);
}
state->sflag = SF_NORMAL;
item = iter->ops->call(state, list);
item = CALL_METHOD(state, iter, call, list);
}
state->sflag = SF_NORMAL;
if(sol_has_error(state)) {

22
sol.h

@ -12,6 +12,18 @@
#define VERSION "0.1a1"
#define HEXVER 0x0001A01
#ifndef SOL_ICACHE_MIN
#define SOL_ICACHE_MIN -128
#endif
#ifndef SOL_ICACHE_MAX
#define SOL_ICACHE_MAX 256
#endif
#if !defined(SOL_ICACHE) && (SOL_ICACHE_MIN < SOL_ICACHE_MAX)
#define SOL_ICACHE
#endif
// Forward declarations:
struct sol_tag_object_t;
typedef struct sol_tag_object_t sol_object_t;
@ -160,6 +172,9 @@ typedef struct sol_tag_state_t {
sol_object_t *traceback; // The last stack of statement (nodes) in the last error, or NULL
sol_state_flag_t sflag; // Used to implement break/continue
sol_object_t *error; // Some arbitrary error descriptor, None if no error
sol_object_t *stdout; // Standard output stream object (for print())
sol_object_t *stdin; // Standard input stream object
sol_object_t *stderr; // Standard error stream object
sol_object_t *None;
sol_object_t *OutOfMemory;
sol_object_t *StopIteration;
@ -181,6 +196,12 @@ typedef struct sol_tag_state_t {
sol_object_t *modules;
sol_object_t *methods;
dsl_object_funcs obfuncs;
const char *calling_type;
const char *calling_meth;
#ifdef SOL_ICACHE
sol_object_t *icache[SOL_ICACHE_MAX - SOL_ICACHE_MIN + 1];
char icache_bypass;
#endif
} sol_state_t;
// state.c
@ -216,6 +237,7 @@ void sol_register_methods_name(sol_state_t *, char *, sol_object_t *);
sol_object_t *sol_get_methods(sol_state_t *, sol_object_t *);
sol_object_t *sol_get_methods_name(sol_state_t *, char *);
sol_object_t *sol_f_io_setindex(sol_state_t *, sol_object_t *);
sol_object_t *sol_get_stdin(sol_state_t *);
sol_object_t *sol_get_stdout(sol_state_t *);
sol_object_t *sol_get_stderr(sol_state_t *);

80
state.c

@ -5,6 +5,7 @@
int sol_state_init(sol_state_t *state) {
sol_object_t *globals, *mod, *meths;
sol_object_t *btype, *bsize, *bobj;
unsigned long i;
sol_mm_initialize(state);
@ -171,6 +172,17 @@ int sol_state_init(sol_state_t *state) {
state->obfuncs.destr = (dsl_destructor) sol_obj_free;
#endif
#ifdef SOL_ICACHE
state->icache_bypass = 1;
for(i = 0; i <= (SOL_ICACHE_MAX - SOL_ICACHE_MIN); i++) {
state->icache[i] = sol_new_int(state, ((long) i) + SOL_ICACHE_MIN);
}
state->icache_bypass = 0;
#endif
state->calling_type = "(none)";
state->calling_meth = "(none)";
state->error = state->None;
state->scopes = sol_new_list(state);
if(sol_has_error(state)) {
@ -187,6 +199,11 @@ int sol_state_init(sol_state_t *state) {
if(sol_has_error(state)) {
goto cleanup;
}
state->stdin = sol_new_stream(state, stdin, MODE_READ);
state->stdout = sol_new_stream(state, stdout, MODE_WRITE);
state->stderr = sol_new_stream(state, stderr, MODE_WRITE);
// I'm going to buffer all of these together because I can.
sol_map_set_name(state, globals, "OutOfMemory", state->OutOfMemory);
sol_map_set_name(state, globals, "StopIteration", state->StopIteration);
@ -217,6 +234,10 @@ int sol_state_init(sol_state_t *state) {
sol_map_set_name(state, mod, "scopes", sol_new_cfunc(state, sol_f_debug_scopes));
sol_map_set_name(state, mod, "version", sol_new_string(state, VERSION));
sol_map_set_name(state, mod, "hexversion", sol_new_int(state, HEXVER));
#ifdef SOL_ICACHE
sol_map_set_name(state, mod, "icache_min", sol_new_int(state, SOL_ICACHE_MIN));
sol_map_set_name(state, mod, "icache_max", sol_new_int(state, SOL_ICACHE_MAX));
#endif
sol_register_module_name(state, "debug", mod);
sol_obj_free(mod);
@ -368,6 +389,7 @@ int sol_state_init(sol_state_t *state) {
sol_map_set_name(state, mod, "stdout", sol_new_stream(state, stdout, MODE_WRITE));
sol_map_set_name(state, mod, "stderr", sol_new_stream(state, stderr, MODE_WRITE));
sol_map_set_name(state, mod, "open", sol_new_cfunc(state, sol_f_stream_open));
sol_map_set_name(state, mod, "__setindex", sol_new_cfunc(state, sol_f_io_setindex));
sol_register_module_name(state, "io", mod);
sol_obj_free(mod);
@ -442,7 +464,7 @@ sol_object_t *sol_state_resolve(sol_state_t *state, sol_object_t *key) {
sol_list_insert(state, args, 1, key);
while(!dsl_seq_iter_is_invalid(iter)) {
sol_list_set_index(state, args, 0, dsl_seq_iter_at(iter));
temp = ((sol_object_t *) dsl_seq_iter_at(iter))->ops->index(state, args);
temp = CALL_METHOD(state, ((sol_object_t *) dsl_seq_iter_at(iter)), index, args);
if(!sol_is_none(state, temp)) {
dsl_free_seq_iter(iter);
sol_obj_free(args);
@ -494,7 +516,7 @@ void sol_state_assign(sol_state_t *state, sol_object_t *key, sol_object_t *val)
sol_list_insert(state, args, 0, active);
sol_list_insert(state, args, 1, key);
sol_list_insert(state, args, 2, val);
sol_obj_free(active->ops->setindex(state, args));
sol_obj_free(CALL_METHOD(state, active, setindex, args));
sol_obj_free(args);
}
@ -523,7 +545,7 @@ void sol_state_assign_l(sol_state_t *state, sol_object_t *key, sol_object_t *val
sol_list_insert(state, args, 0, cur);
sol_list_insert(state, args, 1, key);
sol_list_insert(state, args, 2, val);
sol_obj_free(cur->ops->setindex(state, args));
sol_obj_free(CALL_METHOD(state, cur, setindex, args));
sol_obj_free(args);
}
@ -631,37 +653,41 @@ sol_object_t *sol_get_methods_name(sol_state_t *state, char *name) {
return sol_map_get_name(state, state->methods, name);
}
sol_object_t *sol_get_stdin(sol_state_t *state) {
sol_object_t *io = sol_state_resolve_name(state, "io");
sol_object_t *res = sol_map_get_name(state, io, "stdin");
sol_obj_free(io);
if(sol_is_none(state, res)) {
printf("WARNING: No io.stdin, returning a new ref\n");
return sol_new_stream(state, stdin, MODE_READ);
sol_object_t *sol_f_io_setindex(sol_state_t *state, sol_object_t *args) {
sol_object_t *name = sol_list_get_index(state, args, 1), *value = sol_list_get_index(state, args, 2);
sol_object_t *namestr = sol_cast_string(state, name), *io;
if(sol_string_eq(state, namestr, "stdin")) {
state->stdin = sol_incref(value);
goto done;
}
return res;
if(sol_string_eq(state, namestr, "stdout")) {
state->stdout = sol_incref(value);
goto done;
}
if(sol_string_eq(state, namestr, "stderr")) {
state->stderr = sol_incref(value);
goto done;
}
io = sol_list_get_index(state, args, 0);
sol_map_set(state, io, name, value);
sol_obj_free(io);
done:
sol_obj_free(namestr);
sol_obj_free(value);
sol_obj_free(name);
return sol_incref(state->None);
}
sol_object_t *sol_get_stdin(sol_state_t *state) {
return sol_incref(state->stdin);
}
sol_object_t *sol_get_stdout(sol_state_t *state) {
sol_object_t *io = sol_state_resolve_name(state, "io");
sol_object_t *res = sol_map_get_name(state, io, "stdout");
sol_obj_free(io);
if(sol_is_none(state, res)) {
printf("WARNING: No io.stdout, returning a new ref\n");
return sol_new_stream(state, stdout, MODE_WRITE);
}
return res;
return sol_incref(state->stdout);
}
sol_object_t *sol_get_stderr(sol_state_t *state) {
sol_object_t *io = sol_state_resolve_name(state, "io");
sol_object_t *res = sol_map_get_name(state, io, "stderr");
sol_obj_free(io);
if(sol_is_none(state, res)) {
printf("WARNING: No io.stderr, returning a new ref\n");
return sol_new_stream(state, stderr, MODE_WRITE);
}
return res;
return sol_incref(state->stderr);
}
void sol_ops_init(sol_ops_t *ops) {

Loading…
Cancel
Save