1
0
Fork 0

TOKEN_QUOTE

This commit is contained in:
Alex Kotov 2023-05-05 11:44:33 +04:00
parent b1c9f77b85
commit 21f54b6a8a
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 22 additions and 0 deletions

View file

@ -14,6 +14,7 @@ const char *State_to_str(const enum State state)
case STATE_SQUARE_CLOSE: return "STATE_SQUARE_CLOSE";
case STATE_CURLY_OPEN: return "STATE_CURLY_OPEN";
case STATE_CURLY_CLOSE: return "STATE_CURLY_CLOSE";
case STATE_QUOTE: return "STATE_QUOTE";
case STATE_IDENT: return "STATE_IDENT";
case STATE_NUM: return "STATE_NUM";
}
@ -29,6 +30,7 @@ const char *TokenType_to_str(const enum TokenType token_type)
case TOKEN_SQUARE_CLOSE: return "TOKEN_SQUARE_CLOSE";
case TOKEN_CURLY_OPEN: return "TOKEN_CURLY_OPEN";
case TOKEN_CURLY_CLOSE: return "TOKEN_CURLY_CLOSE";
case TOKEN_QUOTE: return "TOKEN_QUOTE";
case TOKEN_IDENT: return "TOKEN_IDENT";
case TOKEN_NUM: return "TOKEN_NUM";
}
@ -59,6 +61,7 @@ State_to_token_type(const enum State state, enum TokenType *const token_type)
case STATE_SQUARE_CLOSE: *token_type = TOKEN_SQUARE_CLOSE; break;
case STATE_CURLY_OPEN: *token_type = TOKEN_CURLY_OPEN; break;
case STATE_CURLY_CLOSE: *token_type = TOKEN_CURLY_CLOSE; break;
case STATE_QUOTE: *token_type = TOKEN_QUOTE; break;
case STATE_IDENT: *token_type = TOKEN_IDENT; break;
case STATE_NUM: *token_type = TOKEN_NUM; break;
default: return false;

View file

@ -12,6 +12,7 @@ enum State {
STATE_SQUARE_CLOSE,
STATE_CURLY_OPEN,
STATE_CURLY_CLOSE,
STATE_QUOTE,
STATE_IDENT,
STATE_NUM,
};
@ -23,6 +24,7 @@ enum TokenType {
TOKEN_SQUARE_CLOSE,
TOKEN_CURLY_OPEN,
TOKEN_CURLY_CLOSE,
TOKEN_QUOTE,
TOKEN_IDENT,
TOKEN_NUM,
};

View file

@ -59,6 +59,9 @@ void lex(const char chr)
} else if (chr == '}') {
state = STATE_CURLY_CLOSE;
buffer_add(chr);
} else if (chr == '\'') {
state = STATE_QUOTE;
buffer_add(chr);
} else if (is_space(chr)) {
state = STATE_WHITESPACE;
buffer_add(chr);
@ -103,6 +106,11 @@ void lex(const char chr)
buffer_clean();
state = STATE_CURLY_CLOSE;
buffer_add(chr);
} else if (chr == '\'') {
token_add(state, buffer);
buffer_clean();
state = STATE_QUOTE;
buffer_add(chr);
} else if (is_space(chr)) {
buffer_add(chr);
} else if (is_ident_head(chr)) {
@ -125,6 +133,7 @@ void lex(const char chr)
case STATE_SQUARE_CLOSE:
case STATE_CURLY_OPEN:
case STATE_CURLY_CLOSE:
case STATE_QUOTE:
if (chr == '(') {
token_add(state, buffer);
buffer_clean();
@ -155,6 +164,11 @@ void lex(const char chr)
buffer_clean();
state = STATE_CURLY_CLOSE;
buffer_add(chr);
} else if (chr == '\'') {
token_add(state, buffer);
buffer_clean();
state = STATE_QUOTE;
buffer_add(chr);
} else if (is_space(chr)) {
token_add(state, buffer);
buffer_clean();

View file

@ -33,6 +33,9 @@ struct Object *expr()
case TOKEN_SQUARE_OPEN:
case TOKEN_CURLY_OPEN:
return parens();
case TOKEN_QUOTE:
tokens_pop();
return Object_build_list(2, Object_new_symbol("quote"), expr());
case TOKEN_IDENT:
{
struct Object *const object = Object_new_symbol(tokens_top()->val);