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

Move ast into parser. Map keywords to symbols instead of

strings.
This commit is contained in:
Calvin Rose 2018-02-03 13:55:55 -05:00
parent 905ed9f871
commit 35ddc70888
13 changed files with 93 additions and 94 deletions

View File

@ -58,7 +58,6 @@ src/compiler/compile.h
set(CORE_SOURCES
src/core/abstract.c
src/core/array.c
src/core/ast.c
src/core/buffer.c
src/core/bytecode.c
src/core/corelib.c
@ -90,6 +89,7 @@ src/mainclient/linenoise.h
)
set(PARSER_SOURCES
src/parser/ast.c
src/parser/parse.c
src/parser/strtod.c
)

View File

@ -21,6 +21,7 @@
*/
#include <dst/dst.h>
#include <dst/dstparse.h>
#include <dst/dstcorelib.h>
#include "compile.h"
@ -745,6 +746,17 @@ static DstSlot dstc_tablector(DstFopts opts, DstAst *ast, Dst x, DstCFunction cf
return dstc_call(opts, ast, dstc_toslotskv(c, x), dstc_cslot(dst_wrap_cfunction(cfun)));
}
/* Compile a symbol */
DstSlot dstc_symbol(DstFopts opts, DstAst *ast, const uint8_t *sym) {
if (dst_string_length(sym) && sym[0] != ':') {
/* Non keyword */
return dstc_resolve(opts.compiler, ast, sym);
} else {
/* Keyword */
return dstc_cslot(dst_wrap_symbol(sym));
}
}
/* Compile a single value */
DstSlot dstc_value(DstFopts opts, Dst x) {
DstSlot ret;
@ -769,7 +781,7 @@ recur:
case DST_SYMBOL:
{
const uint8_t *sym = dst_unwrap_symbol(x);
ret = dstc_resolve(opts.compiler, ast, sym);
ret = dstc_symbol(opts, ast, sym);
break;
}
case DST_TUPLE:

View File

@ -103,7 +103,6 @@ DstTable *dst_stl_env() {
dst_lib_io(args);
dst_lib_math(args);
dst_lib_array(args);
dst_lib_ast(args);
dst_lib_tuple(args);
dst_lib_buffer(args);
dst_lib_parse(args);

View File

@ -178,22 +178,22 @@ int dst_core_status(DstArgs args) {
if (!dst_checktype(args.v[0], DST_FIBER)) return dst_throw(args, "expected fiber");
switch(dst_unwrap_fiber(args.v[0])->status) {
case DST_FIBER_PENDING:
status = "pending";
status = ":pending";
break;
case DST_FIBER_NEW:
status = "new";
status = ":new";
break;
case DST_FIBER_ALIVE:
status = "alive";
status = ":alive";
break;
case DST_FIBER_DEAD:
status = "dead";
status = ":dead";
break;
case DST_FIBER_ERROR:
status = "error";
status = ":error";
break;
}
return dst_return(args, dst_cstringv(status));
return dst_return(args, dst_csymbolv(status));
}
int dst_core_put(DstArgs args) {
@ -220,7 +220,7 @@ int dst_core_type(DstArgs args) {
if (dst_checktype(args.v[0], DST_ABSTRACT)) {
return dst_return(args, dst_cstringv(dst_abstract_type(dst_unwrap_abstract(args.v[0]))->name));
} else {
return dst_return(args, dst_cstringv(dst_type_names[dst_type(args.v[0])]));
return dst_return(args, dst_csymbolv(dst_type_names[dst_type(args.v[0])]));
}
}

View File

@ -49,6 +49,10 @@ DstAbstractType dst_io_filetype = {
static int checkflags(const uint8_t *str, int32_t len) {
int flags = 0;
int32_t i;
if (len && str[0] == ':') {
len--;
str++;
}
if (!len || len > 3) return -1;
switch (*str) {
default:

View File

@ -312,7 +312,7 @@ const uint8_t *dst_short_description(Dst x) {
dst_abstract_type(dst_unwrap_abstract(x))->name,
dst_unwrap_abstract(x));
default:
return string_description(dst_type_names[dst_type(x)], dst_unwrap_pointer(x));
return string_description(dst_type_names[dst_type(x)] + 1, dst_unwrap_pointer(x));
}
}
@ -350,7 +350,7 @@ void dst_short_description_b(DstBuffer *buffer, Dst x) {
dst_unwrap_abstract(x));
return;
default:
string_description_b(buffer, dst_type_names[dst_type(x)], dst_unwrap_pointer(x));
string_description_b(buffer, dst_type_names[dst_type(x)] + 1, dst_unwrap_pointer(x));
break;
}
}

View File

@ -34,22 +34,22 @@ const char dst_base64[65] =
/* The DST value types in order. These types can be used as
* mnemonics instead of a bit pattern for type checking */
const char *dst_type_names[16] = {
"nil",
"false",
"true",
"fiber",
"integer",
"real",
"string",
"symbol",
"array",
"tuple",
"table",
"struct",
"buffer",
"function",
"cfunction",
"abstract"
":nil",
":false",
":true",
":fiber",
":integer",
":real",
":string",
":symbol",
":array",
":tuple",
":table",
":struct",
":buffer",
":function",
":cfunction",
":abstract"
};
/* Calculate hash for string */

View File

@ -210,12 +210,6 @@ DstTable *dst_env_arg(DstArgs args);
/* STL */
DstTable *dst_stl_env(void);
/* AST */
Dst dst_ast_wrap(Dst x, int32_t start, int32_t end);
DstAst *dst_ast_node(Dst x);
Dst dst_ast_unwrap1(Dst x);
Dst dst_ast_unwrap(Dst x);
#ifdef __cplusplus
}
#endif

View File

@ -95,7 +95,6 @@ int dst_core_hash(DstArgs args);
int dst_lib_io(DstArgs args);
int dst_lib_math(DstArgs args);
int dst_lib_array(DstArgs args);
int dst_lib_ast(DstArgs args);
int dst_lib_tuple(DstArgs args);
int dst_lib_buffer(DstArgs args);

View File

@ -29,6 +29,12 @@ extern "C" {
#include "dsttypes.h"
/* AST */
Dst dst_ast_wrap(Dst x, int32_t start, int32_t end);
DstAst *dst_ast_node(Dst x);
Dst dst_ast_unwrap1(Dst x);
Dst dst_ast_unwrap(Dst x);
typedef struct DstParseState DstParseState;
typedef struct DstParser DstParser;

View File

@ -32,7 +32,7 @@ static int dst_ast_gcmark(void *p, size_t size) {
/* AST type */
static DstAbstractType dst_ast_type = {
"core.ast",
"parse.ast",
NULL,
dst_ast_gcmark
};
@ -183,52 +183,3 @@ Dst dst_ast_unwrap(Dst x) {
return astunwrap_table(dst_unwrap_table(x));
}
}
/* C Functions */
static int cfun_unwrap1(DstArgs args) {
if (args.n != 1) return dst_throw(args, "expected 1 argument");
return dst_return(args, dst_ast_unwrap1(args.v[0]));
}
static int cfun_unwrap(DstArgs args) {
if (args.n != 1) return dst_throw(args, "expected 1 argument");
return dst_return(args, dst_ast_unwrap(args.v[0]));
}
static int cfun_wrap(DstArgs args) {
if (args.n != 1) return dst_throw(args, "expected 1 argument");
return dst_return(args, dst_ast_wrap(args.v[0], -1, -1));
}
static int cfun_node(DstArgs args) {
DstAst *ast;
Dst *tup;
int32_t start, end;
if (args.n != 1) return dst_throw(args, "expected 1 argument");
ast = dst_ast_node(args.v[0]);
if (ast) {
start = ast->source_start;
end = ast->source_end;
} else {
start = -1;
end = -1;
}
tup = dst_tuple_begin(2);
tup[0] = dst_wrap_integer(start);
tup[1] = dst_wrap_integer(end);
return dst_return(args, dst_wrap_tuple(dst_tuple_end(tup)));
}
static const DstReg cfuns[] = {
{"ast-unwrap", cfun_unwrap},
{"ast-unwrap1", cfun_unwrap1},
{"ast-wrap", cfun_wrap},
{"ast-node", cfun_node},
{NULL, NULL}
};
int dst_lib_ast(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
return 0;
}

View File

@ -314,11 +314,7 @@ static int tokenchar(DstParser *p, DstParseState *state, uint8_t c) {
p->error = "invalid utf-8 in symbol";
return 0;
}
if (p->buf[0] == ':') {
ret = dst_stringv(p->buf + 1, blen - 1);
} else {
ret = dst_symbolv(p->buf, blen);
}
ret = dst_symbolv(p->buf, blen);
}
} else {
p->error = "empty symbol invalid";
@ -643,12 +639,51 @@ static int cfun_produce(DstArgs args) {
return dst_return(args, val);
}
/* AST */
static int cfun_unwrap1(DstArgs args) {
if (args.n != 1) return dst_throw(args, "expected 1 argument");
return dst_return(args, dst_ast_unwrap1(args.v[0]));
}
static int cfun_unwrap(DstArgs args) {
if (args.n != 1) return dst_throw(args, "expected 1 argument");
return dst_return(args, dst_ast_unwrap(args.v[0]));
}
static int cfun_wrap(DstArgs args) {
if (args.n != 1) return dst_throw(args, "expected 1 argument");
return dst_return(args, dst_ast_wrap(args.v[0], -1, -1));
}
static int cfun_node(DstArgs args) {
DstAst *ast;
Dst *tup;
int32_t start, end;
if (args.n != 1) return dst_throw(args, "expected 1 argument");
ast = dst_ast_node(args.v[0]);
if (ast) {
start = ast->source_start;
end = ast->source_end;
} else {
start = -1;
end = -1;
}
tup = dst_tuple_begin(2);
tup[0] = dst_wrap_integer(start);
tup[1] = dst_wrap_integer(end);
return dst_return(args, dst_wrap_tuple(dst_tuple_end(tup)));
}
static const DstReg cfuns[] = {
{"parser", cfun_parser},
{"parser-produce", cfun_produce},
{"parser-consume", cfun_consume},
{"parser-error", cfun_error},
{"parser-status", cfun_status},
{"ast-unwrap", cfun_unwrap},
{"ast-unwrap1", cfun_unwrap1},
{"ast-wrap", cfun_wrap},
{"ast-node", cfun_node},
{NULL, NULL}
};

View File

@ -124,7 +124,6 @@
(assert (= ((outerfun nil 2)) 2) "inner closure 2")
(assert (= ((outerfun false 3)) 3) "inner closure 3")
(assert (= "hello" :hello) "keyword syntax for strings")
(assert (= '(1 2 3) (quote (1 2 3)) (tuple 1 2 3)) "quote shorthand")
((fn []
@ -164,7 +163,7 @@
(def afiber-result (transfer afiber "world!"))
(assert (= afiber-result "hello, world!") "fiber error result")
(assert (= (status afiber) "error") "fiber error status")
(assert (= (status afiber) :error) "fiber error status")
# yield tests
@ -173,7 +172,7 @@
(assert (= 1 (transfer t)) "initial transfer to new fiber")
(assert (= 2 (transfer t)) "second transfer to fiber")
(assert (= 3 (transfer t)) "return from fiber")
(assert (= (status t) "dead") "finished fiber is dead")
(assert (= (status t) :dead) "finished fiber is dead")
# Var arg tests
@ -203,7 +202,7 @@
(def 🦊 :fox)
(def 🐮 :cow)
(assert (= (string "🐼" 🦊 🐮) "🐼foxcow") "emojis 🙉 :)")
(assert (= (string "🐼" 🦊 🐮) "🐼:fox:cow") "emojis 🙉 :)")
(assert (not= 🦊 :🦊) "utf8 strings are not symbols and vice versa")
# Symbols with @ symbol