1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-25 07:50:27 +00:00

Making some changes.

This commit is contained in:
bakpakin 2017-07-15 12:21:06 -04:00
parent 8c20b7229a
commit 57886db410
6 changed files with 66 additions and 7 deletions

View File

@ -15,6 +15,10 @@ GST_CORELIB=core/libgst.a
GST_INTERNAL_HEADERS=$(addprefix core/, cache.h)
GST_HEADERS=$(addprefix include/gst/, gst.h)
# Use gdb. On mac use lldb
DEBUGGER=lldb
CC=clang
all: $(GST_TARGET)
###################################
@ -42,7 +46,7 @@ run: $(GST_TARGET)
@ ./$(GST_TARGET)
debug: $(GST_TARGET)
@ gdb ./$(GST_TARGET)
@ $(DEBUGGER) ./$(GST_TARGET)
valgrind: $(GST_TARGET)
@ valgrind --leak-check=full -v ./$(GST_TARGET)

View File

@ -278,6 +278,16 @@ void gst_table_put(Gst *vm, GstTable *t, GstValue key, GstValue value) {
}
}
/* Clear a table */
void gst_table_clear(GstTable *t) {
uint32_t capacity = t->capacity;
uint32_t i;
GstValue *data = t->data;
for (i = 0; i < capacity; i += 2)
data[i].type = GST_NIL;
t->count = 0;
}
/* Find next key in an object. Returns nil if no next key. */
GstValue gst_table_next(GstTable *t, GstValue key) {
const GstValue *bucket, *end;

View File

@ -168,6 +168,25 @@ int gst_stl_not(Gst *vm) {
/* Core */
/****/
/* Empty a mutable datastructure */
int gst_stl_clear(Gst *vm) {
GstValue x = gst_arg(vm, 0);
switch (x.type) {
default:
gst_c_throwc(vm, "cannot get length");
case GST_ARRAY:
x.data.array->count = 0;
break;
case GST_BYTEBUFFER:
x.data.buffer->count = 0;
break;
case GST_TABLE:
gst_table_clear(x.data.table);
break;
}
gst_c_return(vm, x);
}
/* Get length of object */
int gst_stl_length(Gst *vm) {
uint32_t count = gst_count_args(vm);
@ -1037,12 +1056,12 @@ static const GstModuleItem std_module[] = {
{"parse-hasvalue", gst_stl_parser_hasvalue},
{"parse-charseq", gst_stl_parser_charseq},
{"parse-status", gst_stl_parser_status},
{"parse-status", gst_stl_parser_status},
{"parse", gst_stl_parse},
/* Compile */
{"compile", gst_stl_compile},
/* Other */
{"not", gst_stl_not},
{"clear", gst_stl_clear},
{"length", gst_stl_length},
{"hash", gst_stl_hash},
{"integer", gst_stl_to_int},

View File

@ -138,15 +138,14 @@ void gst_thread_endframe(Gst *vm, GstThread *thread) {
GstValue callee = gst_frame_callee(stack);
if (callee.type == GST_FUNCTION) {
GstFunction *fn = callee.data.function;
uint32_t locals = fn->def->locals;
gst_frame_pc(stack) = fn->def->byteCode;
if (fn->def->flags & GST_FUNCDEF_FLAG_VARARG) {
uint32_t arity = fn->def->arity;
gst_thread_tuplepack(vm, thread, arity);
} else {
uint32_t locals = fn->def->locals;
if (gst_frame_size(stack) < locals) {
gst_thread_pushnil(vm, thread, locals - gst_frame_size(stack));
}
}
if (gst_frame_size(stack) < locals) {
gst_thread_pushnil(vm, thread, locals - gst_frame_size(stack));
}
}
}

View File

@ -472,6 +472,7 @@ GstTable *gst_table(Gst *vm, uint32_t capacity);
GstValue gst_table_get(GstTable *t, GstValue key);
GstValue gst_table_remove(GstTable *t, GstValue key);
void gst_table_put(Gst *vm, GstTable *t, GstValue key, GstValue value);
void gst_table_clear(GstTable *t);
GstValue gst_table_next(GstTable *o, GstValue key);
/****/

26
libs/repl.gst Normal file
View File

@ -0,0 +1,26 @@
(var *sourcefile* stdin)
(var *compile* (fn [x]
(def ret (compile x))
(if (= :function (type ret))
ret
(error (string "compile error: " ret)))))
(var *read* (fn []
(def b (buffer))
(def p (parser))
(while (not (parse-hasvalue p))
(read *sourcefile* 1 b)
(if (= (length b) 0)
(error "unexpected end of source"))
(parse-charseq p b)
(clear b))
(parse-consume p)))
(def eval (fn [x]
(apply (*compile* x) 123)))
(def t (thread (fn []
(while true
(eval (*read*))))))
(print (tran t))