1
0
mirror of https://github.com/janet-lang/janet synced 2025-08-07 22:43:47 +00:00

Add support for cuddled symbol shorthand.

This commit is contained in:
Calvin Rose 2020-03-31 20:39:02 -05:00
parent 5b6b9f1597
commit 2739605184
2 changed files with 23 additions and 7 deletions

View File

@ -1403,8 +1403,8 @@ static const JanetReg os_cfuns[] = {
{ {
"os/realpath", os_realpath, "os/realpath", os_realpath,
JDOC("(os/realpath path)\n\n" JDOC("(os/realpath path)\n\n"
"Get the absolute path for a given path, following ../, ./, and symlinks. " "Get the absolute path for a given path, following ../, ./, and symlinks. "
"Returns an absolute path as a string. Will raise an error on Windows.") "Returns an absolute path as a string. Will raise an error on Windows.")
}, },
#endif #endif
{NULL, NULL, NULL} {NULL, NULL, NULL}

View File

@ -112,6 +112,8 @@ struct JanetParseState {
Consumer consumer; Consumer consumer;
}; };
static int root(JanetParser *p, JanetParseState *state, uint8_t c);
/* Define a stack on the main parser struct */ /* Define a stack on the main parser struct */
#define DEF_PARSER_STACK(NAME, T, STACK, STACKCOUNT, STACKCAP) \ #define DEF_PARSER_STACK(NAME, T, STACK, STACKCOUNT, STACKCAP) \
static void NAME(JanetParser *p, T x) { \ static void NAME(JanetParser *p, T x) { \
@ -183,8 +185,12 @@ static void popstate(JanetParser *p, Janet val) {
(c == ',') ? "unquote" : (c == ',') ? "unquote" :
(c == ';') ? "splice" : (c == ';') ? "splice" :
(c == '|') ? "short-fn" : (c == '|') ? "short-fn" :
(c == '~') ? "quasiquote" : "<unknown>"; (c == '~') ? "quasiquote" : NULL;
t[0] = janet_csymbolv(which); if (!which) {
t[0] = p->args[--p->argcount];
} else {
t[0] = janet_csymbolv(which);
}
t[1] = val; t[1] = val;
/* Quote source mapping info */ /* Quote source mapping info */
janet_tuple_sm_line(t) = (int32_t) newtop->line; janet_tuple_sm_line(t) = (int32_t) newtop->line;
@ -320,6 +326,7 @@ static int tokenchar(JanetParser *p, JanetParseState *state, uint8_t c) {
Janet ret; Janet ret;
double numval; double numval;
int32_t blen; int32_t blen;
int prefix_symbol = 0;
if (is_symbol_char(c)) { if (is_symbol_char(c)) {
push_buf(p, (uint8_t) c); push_buf(p, (uint8_t) c);
if (c > 127) state->argn = 1; /* Use to indicate non ascii */ if (c > 127) state->argn = 1; /* Use to indicate non ascii */
@ -357,10 +364,20 @@ static int tokenchar(JanetParser *p, JanetParseState *state, uint8_t c) {
return 0; return 0;
} }
ret = janet_symbolv(p->buf, blen); ret = janet_symbolv(p->buf, blen);
prefix_symbol = c == '"' || c == '`' || c == '[' || c == '(' || c == '{';
} }
} }
p->bufcount = 0; p->bufcount = 0;
popstate(p, ret); if (prefix_symbol) {
push_arg(p, ret);
/* Set current state to a different state */
JanetParseState newState = {0};
newState.flags = PFLAG_READERMAC;
newState.consumer = root;
*state = newState;
} else {
popstate(p, ret);
}
return 0; return 0;
} }
@ -455,8 +472,6 @@ static int longstring(JanetParser *p, JanetParseState *state, uint8_t c) {
} }
} }
static int root(JanetParser *p, JanetParseState *state, uint8_t c);
static int atsign(JanetParser *p, JanetParseState *state, uint8_t c) { static int atsign(JanetParser *p, JanetParseState *state, uint8_t c) {
(void) state; (void) state;
p->statecount--; p->statecount--;
@ -927,6 +942,7 @@ static Janet janet_wrap_parse_state(JanetParseState *s, Janet *args,
type = (c == '\'') ? "quote" : type = (c == '\'') ? "quote" :
(c == ',') ? "unquote" : (c == ',') ? "unquote" :
(c == ';') ? "splice" : (c == ';') ? "splice" :
(c == '|') ? "short-fn" :
(c == '~') ? "quasiquote" : "<reader>"; (c == '~') ? "quasiquote" : "<reader>";
} else { } else {
type = "root"; type = "root";