From 9db91607a01b83a245961bdfcb72fe1234be420f Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 18 Jun 2024 14:30:08 +0200 Subject: Add char 'c' literals --- Makefile | 1 - src/lexer.c | 10 ++++++++++ src/main.c | 18 +++++++++++++----- 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) $(BIN) $(SRC_BAD) graph-tokens: $(BIN) - # TODO: ... $(BIN) -pl $(SRC_BAD) graph-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 { TOK_RCURLY, // } // Basic literals. + TOK_CHAR, TOK_NUM_INT, TOK_NUM_FLOAT, TOK_SYMBOL, @@ -82,6 +83,7 @@ Str token_str[] = { [TOK_RCURLY] = cstr("RCURLY"), // Basic literals. + [TOK_CHAR] = cstr("CHAR"), [TOK_NUM_INT] = cstr("INUMBER"), [TOK_NUM_FLOAT] = cstr("FNUMBER"), [TOK_SYMBOL] = cstr("SYMBOL"), @@ -237,6 +239,7 @@ scan_is_valid_split(char c) { case '"': case ' ': case ',': + case '\'': case '\f': case '\n': case '\r': @@ -489,6 +492,13 @@ scan_token(Scanner *scanner) { case ':': return emit_token(current, scanner, TOK_COLON); case '.': return emit_token(current, scanner, TOK_DOT); case '@': return emit_token(current, scanner, TOK_AT); + case '\'': { + c = scan_next(scanner); + if (scan_next(scanner) == '\'') { + return emit_token(current, scanner, TOK_CHAR); + } + return emit_token_err(¤t, cstr("mismatched char quote")); + }; case '"': { while (scan_has_next(scanner)) { 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[] = { // Literals. [TOK_STRING] = {parse_string, NULL, PREC_NONE}, [TOK_SYMBOL] = {parse_symbol, NULL, PREC_NONE}, + [TOK_CHAR] = {parse_number, NULL, PREC_NONE}, [TOK_NUM_INT] = {parse_number, NULL, PREC_NONE}, [TOK_NUM_FLOAT] = {parse_number, NULL, PREC_NONE}, [TOK_TRUE] = {parse_literal, NULL, PREC_NONE}, @@ -602,6 +603,11 @@ parse_number(Parser *parser) { if (!node) return; node->value.d = str_to_float(prev.val); } break; + case TOK_CHAR: { + node = node_alloc(parser, NODE_NUM_INT, prev); + if (!node) return; + node->value.i = prev.val.mem[1]; + } break; default: break; } array_push(parser->nodes, node, parser->storage); @@ -700,10 +706,10 @@ graph_node(Node *node) { case NODE_NUM_UINT: print("| Value: %x", node->value.u); break; case NODE_NUM_FLOAT: print("| Value: %f{2}", node->value.d); break; case NODE_STRING: print("| Value: %s", node->value.str); break; - case NODE_SYMBOL_IDX: - case NODE_STRUCT: + case NODE_SYMBOL_IDX: + case NODE_STRUCT: case NODE_STRUCT_LIT: print("| Name: %s", node->value.sym); break; - case NODE_SYMBOL: + case NODE_SYMBOL: case NODE_TYPE: { if (node->is_ptr) { print("| Name: @%s", node->value.sym); @@ -803,11 +809,14 @@ process_file(Str path) { } array_push(tokens, tok, &lexer_arena); } - // Only proceed if there are no errors. if (errors) { goto stop; } + if (mode == PRINT_LEX) { + print_tokens(path, tokens); + goto stop; + } // Parser. Parser parser = { @@ -841,7 +850,6 @@ process_file(Str path) { (void)root; } parse_consume(&parser, TOK_EOF, cstr("expected end of file")); - // print_tokens(path, tokens); // DEBUG stop: // Free up resources. -- cgit v1.2.1