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

Add symbol function

This commit is contained in:
bakpakin 2017-07-03 11:21:10 -04:00
parent 0cf278994d
commit 961275116f
5 changed files with 27 additions and 25 deletions

View File

@ -38,13 +38,13 @@ $(GST_TARGET): $(GST_CLIENT_OBJECTS) $(GST_CORE_OBJECTS)
$(CC) $(CFLAGS) -o $@ -c $<
run: $(GST_TARGET)
./$(GST_TARGET)
@ ./$(GST_TARGET)
debug: $(GST_TARGET)
gdb ./$(GST_TARGET)
@ gdb ./$(GST_TARGET)
valgrind: $(GST_TARGET)
valgrind --leak-check=full -v ./$(GST_TARGET)
@ valgrind --leak-check=full -v ./$(GST_TARGET)
clean:
rm $(GST_TARGET) || true
@ -54,7 +54,7 @@ clean:
rm vgcore.* || true
test: $(GST_TARGET)
./$(GST_TARGET) gsttests/basic.gst
@ ./$(GST_TARGET) gsttests/basic.gst
valtest: $(GST_TARGET)
valgrind --leak-check=full -v ./$(GST_TARGET) gsttests/basic.gst

View File

@ -82,12 +82,11 @@ static char *gst_getline() {
}
/* Compile and run an ast */
static int debug_compile_and_run(Gst *vm, GstValue ast, GstValue last, int64_t flags) {
static int debug_compile_and_run(Gst *vm, GstValue ast, int64_t flags) {
GstCompiler c;
GstValue func;
/* Try to compile generated AST */
gst_compiler(&c, vm);
gst_env_putc(vm, vm->env, "_", last);
func = gst_wrap_function(gst_compiler_compile(&c, ast));
/* Check for compilation errors */
if (c.error.type != GST_NIL) {
@ -139,7 +138,7 @@ static int debug_run(Gst *vm, FILE *in, int64_t flags) {
printf_flags(flags, "31", "parse error: unexpected end of source%s\n", "");
break;
}
if (debug_compile_and_run(vm, gst_parse_consume(&p), vm->ret, flags)) {
if (debug_compile_and_run(vm, gst_parse_consume(&p), flags)) {
break;
}
}
@ -179,7 +178,8 @@ static int debug_repl(Gst *vm, uint64_t flags) {
printf_flags(flags, "31", "parse error: unexpected end of source%s\n", "");
continue;
}
if (!debug_compile_and_run(vm, gst_parse_consume(&p), vm->ret, flags)) {
gst_env_putc(vm, vm->env, "_", vm->ret);
if (!debug_compile_and_run(vm, gst_parse_consume(&p), flags)) {
printf_flags(flags, "36", "%s\n", (const char *) gst_description(vm, vm->ret));
}
}

View File

@ -161,14 +161,12 @@ static GstValue build_token(GstParser *p, GstBuffer *buf) {
if (buf->data[0] >= '0' && buf->data[0] <= '9') {
p_error(p, "symbols cannot start with digits");
x.type = GST_NIL;
} else if (buf->data[0] == ':' && buf->count >= 2) {
x.type = GST_STRING;
x.data.string = gst_string_b(p->vm, buf->data + 1, buf->count - 1);
} else {
if (buf->data[0] == ':' && buf->count >= 2) {
x.type = GST_STRING;
x.data.string = gst_string_b(p->vm, buf->data + 1, buf->count - 1);
} else {
x.type = GST_SYMBOL;
x.data.string = gst_buffer_to_string(p->vm, buf);
}
x.type = GST_SYMBOL;
x.data.string = gst_buffer_to_string(p->vm, buf);
}
}
return x;
@ -238,11 +236,6 @@ static int string_state(GstParser *p, uint8_t c) {
top->buf.string.count = 0;
top->buf.string.accum = 0;
return 1;
case 'u':
top->buf.string.state = STRING_STATE_ESCAPE_HEX;
top->buf.string.count = 0;
top->buf.string.accum = 0;
return 1;
default:
p_error(p, "unknown string escape sequence");
return 1;

View File

@ -443,6 +443,15 @@ int gst_stl_string(Gst *vm) {
gst_c_return(vm, gst_wrap_string(gst_string_end(vm, str)));
}
/* Create a symbol */
int gst_stl_symbol(Gst *vm) {
int ret = gst_stl_string(vm);
if (ret == GST_RETURN_OK) {
vm->ret.type = GST_SYMBOL;
}
return ret;
}
/* Create a thread */
int gst_stl_thread(Gst *vm) {
GstThread *t;
@ -598,8 +607,6 @@ int gst_stl_print(Gst *vm) {
uint32_t len = gst_string_length(string);
for (i = 0; i < len; ++i)
fputc(string[i], stdout);
if (j < count - 1)
fputc(' ', stdout);
}
fputc('\n', stdout);
return GST_RETURN_OK;
@ -1087,6 +1094,7 @@ static const GstModuleItem std_module[] = {
{"struct", gst_stl_struct},
{"buffer", gst_stl_buffer},
{"string", gst_stl_string},
{"symbol", gst_stl_symbol},
{"thread", gst_stl_thread},
{"status", gst_stl_status},
{"current", gst_stl_current},

View File

@ -6,11 +6,11 @@
(varset! num-tests-run (+ 1 num-tests-run))
(if x
(do
(print " \e[32m✔\e[0m" e)
(print " \e[32m✔\e[0m " e)
(varset! num-tests-passed (+ 1 num-tests-passed))
x)
(do
(print " \e[31m✘\e[0m" e)
(print " \e[31m✘\e[0m " e)
x))))
(assert (= 10 (+ 1 2 3 4)) "addition")
@ -46,6 +46,7 @@
(assert (= "hello" :hello) "keyword syntax for strings")
(assert (= '(1 2 3) (quote (1 2 3)) (tuple 1 2 3)) "quote shorthand")
(assert (= (symbol :banana) 'banana) "symbol function")
((fn []
(var accum 1)
@ -103,4 +104,4 @@
# report
(print num-tests-passed "of" num-tests-run "tests passed")
(print num-tests-passed " of " num-tests-run " tests passed")