aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2024-06-18 14:30:08 +0200
committerBad Diode <bd@badd10de.dev>2024-06-18 14:30:08 +0200
commit9db91607a01b83a245961bdfcb72fe1234be420f (patch)
tree78d59a6e92161821382a40d23e31407ec6d8e1ea
parent37f703d1252d39047ab71ef15c62855e44de897b (diff)
downloadbdl-9db91607a01b83a245961bdfcb72fe1234be420f.tar.gz
bdl-9db91607a01b83a245961bdfcb72fe1234be420f.zip
Add char 'c' literals
-rw-r--r--Makefile1
-rw-r--r--src/lexer.c10
-rw-r--r--src/main.c18
3 files changed, 23 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index ecdb672..fabc1c7 100644
--- a/Makefile
+++ b/Makefile
@@ -56,7 +56,6 @@ run: $(BIN)
56 $(BIN) $(SRC_BAD) 56 $(BIN) $(SRC_BAD)
57 57
58graph-tokens: $(BIN) 58graph-tokens: $(BIN)
59 # TODO: ...
60 $(BIN) -pl $(SRC_BAD) 59 $(BIN) -pl $(SRC_BAD)
61 60
62graph-parse: $(BIN) 61graph-parse: $(BIN)
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(&current, 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);
diff --git a/src/main.c b/src/main.c
index 3454587..83477d8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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
846stop: 854stop:
847 // Free up resources. 855 // Free up resources.