Browse Source

Sol Part 10: Now Approximately 20% Cooler!

master
Grissess 7 years ago
parent
commit
38a800f31d
  1. 62
      builtins.c
  2. 27
      cdata.c
  3. 6
      cdata.h
  4. 3
      sol.h

62
builtins.c

@ -2,7 +2,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "sol.h"
#include "ast.h"
// XXX hardcoded buffer sizes
@ -248,6 +248,66 @@ sol_object_t *sol_f_range(sol_state_t *state, sol_object_t *args) {
return res;
}
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);
stmt_node *program;
program = sol_compile(prgstr->str);
if(!program) {
return sol_set_error_string(state, "Compilation failure"));
}
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);
stmt_node *program;
program = sol_compile(prgstr->str);
if(!program) {
return sol_set_error_string(state, "Compilation failure");
}
if(program->type != ST_EXPR) {
return sol_set_error_string(state, "Not an expression");
}
return sol_eval(state, program->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);
stmt_node *program;
FILE *f = fopen(prgstr->str, "r");
char *s;
long sz;
if(!f) {
return sol_set_error_string(state, "File open failure");
}
fseek(f, 0, SEEK_END);
sz = ftell(f);
fseek(f, 0, SEEK_SET);
s = malloc(sz);
if(!s) {
fclose(f);
return sol_set_error_string(state, "File memory allocation failure");
}
fread(s, sz, 1, f);
fclose(f);
program = sol_compile(s);
free(s);
if(!program) {
return sol_set_error_string(state, "Compilation failure");
}
sol_exec(state, program);
return sol_incref(state->None);
}
sol_object_t *sol_f_debug_getref(sol_state_t *state, sol_object_t *args) {
sol_object_t *obj = sol_list_get_index(state, args, 0);
sol_object_t *res = sol_new_int(state, obj->refcnt - 2); // NB: We grabbed a reference, and there's one in the arglist, so account for them.

27
cdata.c

@ -48,6 +48,24 @@ void sol_cstruct_add_member_name(sol_state_t *state, sol_object_t *specs, char *
sol_obj_free(spec);
}
void sol_cstruct_add_pointer(sol_state_t *state, sol_object_t *specs, sol_object_t *key, sol_object_t *specs, int offset) {
sol_object_t *spec = sol_new_cstruct_spec(state, SOL_CS_MEMBER);
AS(spec->data, sol_memspec_t)->memtype = SOL_PTR;
AS(spec->data, sol_memspec_t)->offset = offset;
AS(spec->data, sol_memspec_t)->specs = specs;
sol_map_set(state, spec, key, spec);
sol_obj_free(spec);
}
void sol_cstruct_add_pointer_name(sol_state_t *state, sol_object_t *specs, char *name, sol_object_t *specs, int offset) {
sol_object_t *spec = sol_new_cstruct_spec(state, SOL_CS_MEMBER);
AS(spec->data, sol_memspec_t)->memtype = SOL_PTR;
AS(spec->data, sol_memspec_t)->offset = offset;
AS(spec->data, sol_memspec_t)->specs = specs;
sol_map_set_name(state, spec, name, spec);
sol_obj_free(spec);
}
void sol_cstruct_add_func(sol_state_t *state, sol_object_t *specs, sol_object_t *key, sol_cfunc_t cfunc) {
sol_object_t *spec = sol_new_cstruct_spec(state, SOL_CS_CFUNC);
AS(spec->data, sol_memspec_t)->cfunc = cfunc;
@ -145,6 +163,10 @@ sol_object_t *sol_f_cstruct_index(sol_state_t *state, sol_object_t *args) {
case SOL_CFUNC:
res = sol_new_cfunc(state, AT(cstruct->data, sol_cfunc_t, spec->offset));
break;
case SOL_PTR:
res = sol_new_cstruct(state, AT(cstruct->data, void *, spec->offset), spec->specs);
break;
}
break;
@ -231,6 +253,11 @@ sol_object_t *sol_f_cstruct_setindex(sol_state_t *state, sol_object_t *args) {
case SOL_CFUNC:
return sol_set_error_string(state, "Can't assign CFunc members");
break;
case SOL_PTR:
if(!sol_is_cdata(val) || spec->specs != AS(val->data, sol_cstruct_t)->specs)
return sol_set_error_string(state, "Invalid type for PTR assignment");
AS(cstruct->data, void *, spec->offset) = AS(val->data, sol_cstruct_t)->data;
}
break;

6
cdata.h

@ -18,7 +18,8 @@ typedef enum {
SOL_DOUBLE,
SOL_CHAR,
SOL_CSTR,
SOL_CFUNC
SOL_CFUNC,
SOL_PTR // Don't use this in add_member--use add_pointer
} sol_memtype_t;
typedef enum {
@ -32,6 +33,7 @@ typedef struct {
struct {
sol_memtype_t memtype;
int offset;
sol_object_t *specs;
};
sol_cfunc_t cfunc;
};
@ -45,6 +47,8 @@ typedef struct {
sol_object_t *sol_new_cstruct_specs(sol_state_t *);
void sol_cstruct_add_member(sol_state_t *, sol_object_t *, sol_object_t *, sol_memtype_t, int);
void sol_cstruct_add_member_name(sol_state_t *, sol_object_t *, char *, sol_memtype_t, int);
void sol_cstruct_add_pointer(sol_state_t *, sol_object_t *, sol_object_t *, sol_object_t *, int);
void sol_cstruct_add_pointer_name(sol_state_t *, sol_object_t *, char *, sol_object_t *, int);
void sol_cstruct_add_func(sol_state_t *, sol_object_t *, sol_object_t *, sol_cfunc_t);
void sol_cstruct_add_func_name(sol_state_t *, sol_object_t *, char *, sol_cfunc_t);

3
sol.h

@ -147,6 +147,9 @@ sol_object_t *sol_f_print(sol_state_t *, sol_object_t *);
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_execfile(sol_state_t *, sol_object_t *);
sol_object_t *sol_f_debug_getref(sol_state_t *, sol_object_t *);
sol_object_t *sol_f_debug_setref(sol_state_t *, sol_object_t *);

Loading…
Cancel
Save