mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 07:33:01 +00:00 
			
		
		
		
	Move ast into parser. Map keywords to symbols instead of
strings.
This commit is contained in:
		| @@ -58,7 +58,6 @@ src/compiler/compile.h | |||||||
| set(CORE_SOURCES | set(CORE_SOURCES | ||||||
| src/core/abstract.c | src/core/abstract.c | ||||||
| src/core/array.c | src/core/array.c | ||||||
| src/core/ast.c |  | ||||||
| src/core/buffer.c | src/core/buffer.c | ||||||
| src/core/bytecode.c | src/core/bytecode.c | ||||||
| src/core/corelib.c | src/core/corelib.c | ||||||
| @@ -90,6 +89,7 @@ src/mainclient/linenoise.h | |||||||
| ) | ) | ||||||
|  |  | ||||||
| set(PARSER_SOURCES | set(PARSER_SOURCES | ||||||
|  | src/parser/ast.c | ||||||
| src/parser/parse.c | src/parser/parse.c | ||||||
| src/parser/strtod.c | src/parser/strtod.c | ||||||
| ) | ) | ||||||
|   | |||||||
| @@ -21,6 +21,7 @@ | |||||||
| */ | */ | ||||||
|  |  | ||||||
| #include <dst/dst.h> | #include <dst/dst.h> | ||||||
|  | #include <dst/dstparse.h> | ||||||
| #include <dst/dstcorelib.h> | #include <dst/dstcorelib.h> | ||||||
| #include "compile.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))); |     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 */ | /* Compile a single value */ | ||||||
| DstSlot dstc_value(DstFopts opts, Dst x) { | DstSlot dstc_value(DstFopts opts, Dst x) { | ||||||
|     DstSlot ret; |     DstSlot ret; | ||||||
| @@ -769,7 +781,7 @@ recur: | |||||||
|         case DST_SYMBOL: |         case DST_SYMBOL: | ||||||
|             { |             { | ||||||
|                 const uint8_t *sym = dst_unwrap_symbol(x); |                 const uint8_t *sym = dst_unwrap_symbol(x); | ||||||
|                 ret = dstc_resolve(opts.compiler, ast, sym); |                 ret = dstc_symbol(opts, ast, sym); | ||||||
|                 break; |                 break; | ||||||
|             } |             } | ||||||
|         case DST_TUPLE: |         case DST_TUPLE: | ||||||
|   | |||||||
| @@ -103,7 +103,6 @@ DstTable *dst_stl_env() { | |||||||
|         dst_lib_io(args); |         dst_lib_io(args); | ||||||
|         dst_lib_math(args); |         dst_lib_math(args); | ||||||
|         dst_lib_array(args); |         dst_lib_array(args); | ||||||
|         dst_lib_ast(args); |  | ||||||
|         dst_lib_tuple(args); |         dst_lib_tuple(args); | ||||||
|         dst_lib_buffer(args); |         dst_lib_buffer(args); | ||||||
|         dst_lib_parse(args); |         dst_lib_parse(args); | ||||||
|   | |||||||
| @@ -178,22 +178,22 @@ int dst_core_status(DstArgs args) { | |||||||
|     if (!dst_checktype(args.v[0], DST_FIBER)) return dst_throw(args, "expected fiber"); |     if (!dst_checktype(args.v[0], DST_FIBER)) return dst_throw(args, "expected fiber"); | ||||||
|     switch(dst_unwrap_fiber(args.v[0])->status) { |     switch(dst_unwrap_fiber(args.v[0])->status) { | ||||||
|         case DST_FIBER_PENDING: |         case DST_FIBER_PENDING: | ||||||
|             status = "pending"; |             status = ":pending"; | ||||||
|             break; |             break; | ||||||
|         case DST_FIBER_NEW: |         case DST_FIBER_NEW: | ||||||
|             status = "new"; |             status = ":new"; | ||||||
|             break; |             break; | ||||||
|         case DST_FIBER_ALIVE: |         case DST_FIBER_ALIVE: | ||||||
|             status = "alive"; |             status = ":alive"; | ||||||
|             break; |             break; | ||||||
|         case DST_FIBER_DEAD: |         case DST_FIBER_DEAD: | ||||||
|             status = "dead"; |             status = ":dead"; | ||||||
|             break; |             break; | ||||||
|         case DST_FIBER_ERROR: |         case DST_FIBER_ERROR: | ||||||
|             status = "error"; |             status = ":error"; | ||||||
|             break; |             break; | ||||||
|     } |     } | ||||||
|     return dst_return(args, dst_cstringv(status)); |     return dst_return(args, dst_csymbolv(status)); | ||||||
| } | } | ||||||
|  |  | ||||||
| int dst_core_put(DstArgs args) { | int dst_core_put(DstArgs args) { | ||||||
| @@ -220,7 +220,7 @@ int dst_core_type(DstArgs args) { | |||||||
|     if (dst_checktype(args.v[0], DST_ABSTRACT)) { |     if (dst_checktype(args.v[0], DST_ABSTRACT)) { | ||||||
|         return dst_return(args, dst_cstringv(dst_abstract_type(dst_unwrap_abstract(args.v[0]))->name)); |         return dst_return(args, dst_cstringv(dst_abstract_type(dst_unwrap_abstract(args.v[0]))->name)); | ||||||
|     } else { |     } 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])])); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -49,6 +49,10 @@ DstAbstractType dst_io_filetype = { | |||||||
| static int checkflags(const uint8_t *str, int32_t len) { | static int checkflags(const uint8_t *str, int32_t len) { | ||||||
|     int flags = 0; |     int flags = 0; | ||||||
|     int32_t i; |     int32_t i; | ||||||
|  |     if (len && str[0] == ':') { | ||||||
|  |         len--; | ||||||
|  |         str++; | ||||||
|  |     } | ||||||
|     if (!len || len > 3) return -1; |     if (!len || len > 3) return -1; | ||||||
|     switch (*str) { |     switch (*str) { | ||||||
|         default: |         default: | ||||||
|   | |||||||
| @@ -312,7 +312,7 @@ const uint8_t *dst_short_description(Dst x) { | |||||||
|                 dst_abstract_type(dst_unwrap_abstract(x))->name, |                 dst_abstract_type(dst_unwrap_abstract(x))->name, | ||||||
|                 dst_unwrap_abstract(x)); |                 dst_unwrap_abstract(x)); | ||||||
|     default: |     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)); |                 dst_unwrap_abstract(x)); | ||||||
|         return; |         return; | ||||||
|     default: |     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; |         break; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -34,22 +34,22 @@ const char dst_base64[65] = | |||||||
| /* The DST value types in order. These types can be used as | /* The DST value types in order. These types can be used as | ||||||
|  * mnemonics instead of a bit pattern for type checking */ |  * mnemonics instead of a bit pattern for type checking */ | ||||||
| const char *dst_type_names[16] = { | const char *dst_type_names[16] = { | ||||||
|     "nil", |     ":nil", | ||||||
|     "false", |     ":false", | ||||||
|     "true", |     ":true", | ||||||
|     "fiber", |     ":fiber", | ||||||
|     "integer", |     ":integer", | ||||||
|     "real", |     ":real", | ||||||
|     "string", |     ":string", | ||||||
|     "symbol", |     ":symbol", | ||||||
|     "array", |     ":array", | ||||||
|     "tuple", |     ":tuple", | ||||||
|     "table", |     ":table", | ||||||
|     "struct", |     ":struct", | ||||||
|     "buffer", |     ":buffer", | ||||||
|     "function", |     ":function", | ||||||
|     "cfunction", |     ":cfunction", | ||||||
|     "abstract" |     ":abstract" | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /* Calculate hash for string */ | /* Calculate hash for string */ | ||||||
|   | |||||||
| @@ -210,12 +210,6 @@ DstTable *dst_env_arg(DstArgs args); | |||||||
| /* STL */ | /* STL */ | ||||||
| DstTable *dst_stl_env(void); | 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 | #ifdef __cplusplus | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|   | |||||||
| @@ -95,7 +95,6 @@ int dst_core_hash(DstArgs args); | |||||||
| int dst_lib_io(DstArgs args); | int dst_lib_io(DstArgs args); | ||||||
| int dst_lib_math(DstArgs args); | int dst_lib_math(DstArgs args); | ||||||
| int dst_lib_array(DstArgs args); | int dst_lib_array(DstArgs args); | ||||||
| int dst_lib_ast(DstArgs args); |  | ||||||
| int dst_lib_tuple(DstArgs args); | int dst_lib_tuple(DstArgs args); | ||||||
| int dst_lib_buffer(DstArgs args); | int dst_lib_buffer(DstArgs args); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -29,6 +29,12 @@ extern "C" { | |||||||
|  |  | ||||||
| #include "dsttypes.h" | #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 DstParseState DstParseState; | ||||||
| typedef struct DstParser DstParser; | typedef struct DstParser DstParser; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -32,7 +32,7 @@ static int dst_ast_gcmark(void *p, size_t size) { | |||||||
| 
 | 
 | ||||||
| /* AST type */ | /* AST type */ | ||||||
| static DstAbstractType dst_ast_type = { | static DstAbstractType dst_ast_type = { | ||||||
|     "core.ast", |     "parse.ast", | ||||||
|     NULL, |     NULL, | ||||||
|     dst_ast_gcmark |     dst_ast_gcmark | ||||||
| }; | }; | ||||||
| @@ -183,52 +183,3 @@ Dst dst_ast_unwrap(Dst x) { | |||||||
|             return astunwrap_table(dst_unwrap_table(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; |  | ||||||
| } |  | ||||||
| @@ -314,12 +314,8 @@ static int tokenchar(DstParser *p, DstParseState *state, uint8_t c) { | |||||||
|                 p->error = "invalid utf-8 in symbol"; |                 p->error = "invalid utf-8 in symbol"; | ||||||
|                 return 0; |                 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 { |     } else { | ||||||
|         p->error = "empty symbol invalid"; |         p->error = "empty symbol invalid"; | ||||||
|         return 0; |         return 0; | ||||||
| @@ -643,12 +639,51 @@ static int cfun_produce(DstArgs args) { | |||||||
|     return dst_return(args, val); |     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[] = { | static const DstReg cfuns[] = { | ||||||
|     {"parser", cfun_parser}, |     {"parser", cfun_parser}, | ||||||
|     {"parser-produce", cfun_produce}, |     {"parser-produce", cfun_produce}, | ||||||
|     {"parser-consume", cfun_consume}, |     {"parser-consume", cfun_consume}, | ||||||
|     {"parser-error", cfun_error}, |     {"parser-error", cfun_error}, | ||||||
|     {"parser-status", cfun_status}, |     {"parser-status", cfun_status}, | ||||||
|  |     {"ast-unwrap", cfun_unwrap}, | ||||||
|  |     {"ast-unwrap1", cfun_unwrap1}, | ||||||
|  |     {"ast-wrap", cfun_wrap}, | ||||||
|  |     {"ast-node", cfun_node}, | ||||||
|     {NULL, NULL} |     {NULL, NULL} | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -124,7 +124,6 @@ | |||||||
| (assert (= ((outerfun nil 2)) 2) "inner closure 2") | (assert (= ((outerfun nil 2)) 2) "inner closure 2") | ||||||
| (assert (= ((outerfun false 3)) 3) "inner closure 3") | (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") | (assert (= '(1 2 3) (quote (1 2 3)) (tuple 1 2 3)) "quote shorthand") | ||||||
|  |  | ||||||
| ((fn [] | ((fn [] | ||||||
| @@ -164,7 +163,7 @@ | |||||||
| (def afiber-result (transfer afiber "world!")) | (def afiber-result (transfer afiber "world!")) | ||||||
|  |  | ||||||
| (assert (= afiber-result "hello, world!") "fiber error result") | (assert (= afiber-result "hello, world!") "fiber error result") | ||||||
| (assert (= (status afiber) "error") "fiber error status") | (assert (= (status afiber) :error) "fiber error status") | ||||||
|  |  | ||||||
| # yield tests | # yield tests | ||||||
|  |  | ||||||
| @@ -173,7 +172,7 @@ | |||||||
| (assert (= 1 (transfer t)) "initial transfer to new fiber") | (assert (= 1 (transfer t)) "initial transfer to new fiber") | ||||||
| (assert (= 2 (transfer t)) "second transfer to fiber") | (assert (= 2 (transfer t)) "second transfer to fiber") | ||||||
| (assert (= 3 (transfer t)) "return from 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 | # Var arg tests | ||||||
|  |  | ||||||
| @@ -203,7 +202,7 @@ | |||||||
|  |  | ||||||
| (def 🦊 :fox) | (def 🦊 :fox) | ||||||
| (def 🐮 :cow) | (def 🐮 :cow) | ||||||
| (assert (= (string "🐼" 🦊 🐮) "🐼foxcow") "emojis 🙉 :)") | (assert (= (string "🐼" 🦊 🐮) "🐼:fox:cow") "emojis 🙉 :)") | ||||||
| (assert (not= 🦊 :🦊) "utf8 strings are not symbols and vice versa") | (assert (not= 🦊 :🦊) "utf8 strings are not symbols and vice versa") | ||||||
|  |  | ||||||
| # Symbols with @ symbol | # Symbols with @ symbol | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Calvin Rose
					Calvin Rose