diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lexer.c | 10 | ||||
-rw-r--r-- | src/main.c | 18 |
2 files changed, 23 insertions, 5 deletions
diff --git a/src/lexer.c b/src/lexer.c index 0aa26c1..e5bea2e 100644 --- a/src/lexer.c +++ b/src/lexer.c | |||
@@ -14,6 +14,7 @@ typedef enum TokenKind { | |||
14 | TOK_RCURLY, // } | 14 | TOK_RCURLY, // } |
15 | 15 | ||
16 | // Basic literals. | 16 | // Basic literals. |
17 | TOK_CHAR, | ||
17 | TOK_NUM_INT, | 18 | TOK_NUM_INT, |
18 | TOK_NUM_FLOAT, | 19 | TOK_NUM_FLOAT, |
19 | TOK_SYMBOL, | 20 | TOK_SYMBOL, |
@@ -82,6 +83,7 @@ Str token_str[] = { | |||
82 | [TOK_RCURLY] = cstr("RCURLY"), | 83 | [TOK_RCURLY] = cstr("RCURLY"), |
83 | 84 | ||
84 | // Basic literals. | 85 | // Basic literals. |
86 | [TOK_CHAR] = cstr("CHAR"), | ||
85 | [TOK_NUM_INT] = cstr("INUMBER"), | 87 | [TOK_NUM_INT] = cstr("INUMBER"), |
86 | [TOK_NUM_FLOAT] = cstr("FNUMBER"), | 88 | [TOK_NUM_FLOAT] = cstr("FNUMBER"), |
87 | [TOK_SYMBOL] = cstr("SYMBOL"), | 89 | [TOK_SYMBOL] = cstr("SYMBOL"), |
@@ -237,6 +239,7 @@ scan_is_valid_split(char c) { | |||
237 | case '"': | 239 | case '"': |
238 | case ' ': | 240 | case ' ': |
239 | case ',': | 241 | case ',': |
242 | case '\'': | ||
240 | case '\f': | 243 | case '\f': |
241 | case '\n': | 244 | case '\n': |
242 | case '\r': | 245 | case '\r': |
@@ -489,6 +492,13 @@ scan_token(Scanner *scanner) { | |||
489 | case ':': return emit_token(current, scanner, TOK_COLON); | 492 | case ':': return emit_token(current, scanner, TOK_COLON); |
490 | case '.': return emit_token(current, scanner, TOK_DOT); | 493 | case '.': return emit_token(current, scanner, TOK_DOT); |
491 | case '@': return emit_token(current, scanner, TOK_AT); | 494 | case '@': return emit_token(current, scanner, TOK_AT); |
495 | case '\'': { | ||
496 | c = scan_next(scanner); | ||
497 | if (scan_next(scanner) == '\'') { | ||
498 | return emit_token(current, scanner, TOK_CHAR); | ||
499 | } | ||
500 | return emit_token_err(¤t, cstr("mismatched char quote")); | ||
501 | }; | ||
492 | case '"': { | 502 | case '"': { |
493 | while (scan_has_next(scanner)) { | 503 | while (scan_has_next(scanner)) { |
494 | c = scan_next(scanner); | 504 | c = scan_next(scanner); |
@@ -254,6 +254,7 @@ ParseRule parse_rules[] = { | |||
254 | // Literals. | 254 | // Literals. |
255 | [TOK_STRING] = {parse_string, NULL, PREC_NONE}, | 255 | [TOK_STRING] = {parse_string, NULL, PREC_NONE}, |
256 | [TOK_SYMBOL] = {parse_symbol, NULL, PREC_NONE}, | 256 | [TOK_SYMBOL] = {parse_symbol, NULL, PREC_NONE}, |
257 | [TOK_CHAR] = {parse_number, NULL, PREC_NONE}, | ||
257 | [TOK_NUM_INT] = {parse_number, NULL, PREC_NONE}, | 258 | [TOK_NUM_INT] = {parse_number, NULL, PREC_NONE}, |
258 | [TOK_NUM_FLOAT] = {parse_number, NULL, PREC_NONE}, | 259 | [TOK_NUM_FLOAT] = {parse_number, NULL, PREC_NONE}, |
259 | [TOK_TRUE] = {parse_literal, NULL, PREC_NONE}, | 260 | [TOK_TRUE] = {parse_literal, NULL, PREC_NONE}, |
@@ -602,6 +603,11 @@ parse_number(Parser *parser) { | |||
602 | if (!node) return; | 603 | if (!node) return; |
603 | node->value.d = str_to_float(prev.val); | 604 | node->value.d = str_to_float(prev.val); |
604 | } break; | 605 | } break; |
606 | case TOK_CHAR: { | ||
607 | node = node_alloc(parser, NODE_NUM_INT, prev); | ||
608 | if (!node) return; | ||
609 | node->value.i = prev.val.mem[1]; | ||
610 | } break; | ||
605 | default: break; | 611 | default: break; |
606 | } | 612 | } |
607 | array_push(parser->nodes, node, parser->storage); | 613 | array_push(parser->nodes, node, parser->storage); |
@@ -700,10 +706,10 @@ graph_node(Node *node) { | |||
700 | case NODE_NUM_UINT: print("| Value: %x", node->value.u); break; | 706 | case NODE_NUM_UINT: print("| Value: %x", node->value.u); break; |
701 | case NODE_NUM_FLOAT: print("| Value: %f{2}", node->value.d); break; | 707 | case NODE_NUM_FLOAT: print("| Value: %f{2}", node->value.d); break; |
702 | case NODE_STRING: print("| Value: %s", node->value.str); break; | 708 | case NODE_STRING: print("| Value: %s", node->value.str); break; |
703 | case NODE_SYMBOL_IDX: | 709 | case NODE_SYMBOL_IDX: |
704 | case NODE_STRUCT: | 710 | case NODE_STRUCT: |
705 | case NODE_STRUCT_LIT: print("| Name: %s", node->value.sym); break; | 711 | case NODE_STRUCT_LIT: print("| Name: %s", node->value.sym); break; |
706 | case NODE_SYMBOL: | 712 | case NODE_SYMBOL: |
707 | case NODE_TYPE: { | 713 | case NODE_TYPE: { |
708 | if (node->is_ptr) { | 714 | if (node->is_ptr) { |
709 | print("| Name: @%s", node->value.sym); | 715 | print("| Name: @%s", node->value.sym); |
@@ -803,11 +809,14 @@ process_file(Str path) { | |||
803 | } | 809 | } |
804 | array_push(tokens, tok, &lexer_arena); | 810 | array_push(tokens, tok, &lexer_arena); |
805 | } | 811 | } |
806 | |||
807 | // Only proceed if there are no errors. | 812 | // Only proceed if there are no errors. |
808 | if (errors) { | 813 | if (errors) { |
809 | goto stop; | 814 | goto stop; |
810 | } | 815 | } |
816 | if (mode == PRINT_LEX) { | ||
817 | print_tokens(path, tokens); | ||
818 | goto stop; | ||
819 | } | ||
811 | 820 | ||
812 | // Parser. | 821 | // Parser. |
813 | Parser parser = { | 822 | Parser parser = { |
@@ -841,7 +850,6 @@ process_file(Str path) { | |||
841 | (void)root; | 850 | (void)root; |
842 | } | 851 | } |
843 | parse_consume(&parser, TOK_EOF, cstr("expected end of file")); | 852 | parse_consume(&parser, TOK_EOF, cstr("expected end of file")); |
844 | // print_tokens(path, tokens); // DEBUG | ||
845 | 853 | ||
846 | stop: | 854 | stop: |
847 | // Free up resources. | 855 | // Free up resources. |