1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 01:37:19 +00:00

Add quoting with single quote

This commit is contained in:
bakpakin 2017-07-02 17:35:38 -04:00
parent 268bd5f954
commit 113c6cd6ad
3 changed files with 18 additions and 12 deletions

View File

@ -44,15 +44,13 @@ static GstParseState *parser_pop(GstParser * p) {
return p->data + --p->count; return p->data + --p->count;
} }
// /* Quote a value */ /* Quote a value */
// static GstValue quote(GstParser *p, GstValue x) { static GstValue quote(GstParser *p, GstValue x) {
// /* Load a quote form to get the string literal */ GstValue *tuple = gst_tuple_begin(p->vm, 2);
// GstValue *tuple; tuple[0] = gst_string_cvs(p->vm, "quote");
// tuple = gst_tuple_begin(p->vm, 2); tuple[1] = x;
// tuple[0] = gst_string_cv(p->vm, "quote"); return gst_wrap_tuple(gst_tuple_end(p->vm, tuple));
// tuple[1] = x; }
// return gst_wrap_tuple(gst_tuple_end(p->vm, tuple));
// }
/* Add a new, empty ParseState to the ParseStack. */ /* Add a new, empty ParseState to the ParseStack. */
static void parser_push(GstParser *p, ParseType type, uint8_t character) { static void parser_push(GstParser *p, ParseType type, uint8_t character) {
@ -68,6 +66,8 @@ static void parser_push(GstParser *p, ParseType type, uint8_t character) {
top = parser_peek(p); top = parser_peek(p);
if (!top) return; if (!top) return;
top->type = type; top->type = type;
top->quoteCount = p->quoteCount;
p->quoteCount = 0;
switch (type) { switch (type) {
case PTYPE_STRING: case PTYPE_STRING:
top->buf.string.state = STRING_STATE_BASE; top->buf.string.state = STRING_STATE_BASE;
@ -87,7 +87,10 @@ static void parser_push(GstParser *p, ParseType type, uint8_t character) {
/* Append a value to the top-most state in the Parser's stack. */ /* Append a value to the top-most state in the Parser's stack. */
static void parser_append(GstParser *p, GstValue x) { static void parser_append(GstParser *p, GstValue x) {
GstParseState *oldtop = parser_pop(p);
GstParseState *top = parser_peek(p); GstParseState *top = parser_peek(p);
while (oldtop->quoteCount--)
x = quote(p, x);
if (!top) { if (!top) {
p->value = x; p->value = x;
p->status = GST_PARSER_FULL; p->status = GST_PARSER_FULL;
@ -256,7 +259,6 @@ static int token_state(GstParser *p, uint8_t c) {
GstParseState *top = parser_peek(p); GstParseState *top = parser_peek(p);
GstBuffer *buf = top->buf.string.buffer; GstBuffer *buf = top->buf.string.buffer;
if (is_whitespace(c) || c == ')' || c == ']' || c == '}') { if (is_whitespace(c) || c == ')' || c == ']' || c == '}') {
parser_pop(p);
parser_append(p, build_token(p, buf)); parser_append(p, build_token(p, buf));
return !(c == ')' || c == ']' || c == '}'); return !(c == ')' || c == ']' || c == '}');
} else if (is_symbol_char(c)) { } else if (is_symbol_char(c)) {
@ -293,7 +295,6 @@ static int string_state(GstParser *p, uint8_t c) {
GstValue x; GstValue x;
x.type = GST_STRING; x.type = GST_STRING;
x.data.string = gst_buffer_to_string(p->vm, top->buf.string.buffer); x.data.string = gst_buffer_to_string(p->vm, top->buf.string.buffer);
parser_pop(p);
parser_append(p, x); parser_append(p, x);
} else { } else {
gst_buffer_push(p->vm, top->buf.string.buffer, c); gst_buffer_push(p->vm, top->buf.string.buffer, c);
@ -367,6 +368,10 @@ static int root_state(GstParser *p, uint8_t c) {
parser_push(p, PTYPE_STRING, c); parser_push(p, PTYPE_STRING, c);
return 1; return 1;
} }
if (c == '\'') {
p->quoteCount++;
return 1;
}
if (is_symbol_char(c)) { if (is_symbol_char(c)) {
parser_push(p, PTYPE_TOKEN, c); parser_push(p, PTYPE_TOKEN, c);
return 0; return 0;
@ -402,7 +407,6 @@ static int form_state(GstParser *p, uint8_t c) {
gst_table_put(p->vm, x.data.table, array->data[i], array->data[i + 1]); gst_table_put(p->vm, x.data.table, array->data[i], array->data[i + 1]);
} }
} }
parser_pop(p);
parser_append(p, x); parser_append(p, x);
return 1; return 1;
} }

View File

@ -45,6 +45,7 @@
(assert (= 0 (band 3 4)) "bit and") (assert (= 0 (band 3 4)) "bit and")
(assert (= "hello" :hello) "keyword syntax for strings") (assert (= "hello" :hello) "keyword syntax for strings")
(assert (= '(1 2 3) (quote (1 2 3)) (tuple 1 2 3)) "quote shorthand")
((fn [] ((fn []
(var accum 1) (var accum 1)

View File

@ -333,6 +333,7 @@ typedef enum ParseType {
/* Contain a parse state that goes on the parse stack */ /* Contain a parse state that goes on the parse stack */
struct GstParseState { struct GstParseState {
ParseType type; ParseType type;
uint32_t quoteCount;
union { union {
struct { struct {
uint8_t endDelimiter; uint8_t endDelimiter;