From 3cdb549edcd9f73828512a0c62352998e8d3b1e1 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 18 Jun 2018 11:46:00 -0400 Subject: [PATCH] don't define tokens for real characters. just use them directly --- example.src | 3 --- source/lexer.l | 28 ++++++++++++-------------- source/parser.c | 53 ++++++++++++++++++++++++------------------------- source/pprint.c | 24 +++++++++++----------- source/sclpl.h | 6 +++--- 5 files changed, 54 insertions(+), 60 deletions(-) diff --git a/example.src b/example.src index b099a28..2875d24 100644 --- a/example.src +++ b/example.src @@ -29,6 +29,3 @@ type type_union = union { fun main() int { } - - - diff --git a/source/lexer.l b/source/lexer.l index 472c82b..2bb3501 100644 --- a/source/lexer.l +++ b/source/lexer.l @@ -42,21 +42,19 @@ NOSPACE [^ \t\r\n] "struct" { return T_STRUCT; } "union" { return T_UNION; } -"if" { return T_IF; } -"else" { return T_ELSE; } -"(" { return T_LPAR; } -")" { return T_RPAR; } -"[" { return T_LBRACK; } -"]" { return T_RBRACK; } -"{" { return T_LBRACE; } -"}" { return T_RBRACE; } -"," { return T_COMMA; } -"'" { return T_SQUOTE; } -":" { return T_COLON; } -"&" { return T_AMP; } -"=" { return T_ASSIGN; } -";" { return T_SEMI; } -"*" { return T_MUL; } +"(" { return '('; } +")" { return ')'; } +"[" { return '['; } +"]" { return ']'; } +"{" { return '{'; } +"}" { return '}'; } +"," { return ','; } +"'" { return '\''; } +":" { return ':'; } +"&" { return '&'; } +"=" { return '='; } +";" { return ';'; } +"*" { return '*'; } \\. { Value.character = yytext[1]; return T_CHAR; } \\space { Value.character = ' '; return T_CHAR; } diff --git a/source/parser.c b/source/parser.c index c7f646b..99b2b0b 100644 --- a/source/parser.c +++ b/source/parser.c @@ -81,20 +81,20 @@ AST* toplevel(Parser* p) { static void require_list(Parser* p) { accept(p, T_REQUIRES); - expect(p, T_LPAR); - while (!matches(p, T_RPAR)) { + expect(p, '('); + while (!matches(p, ')')) { expect(p, T_STRING); } - expect(p, T_RPAR); + expect(p, ')'); } static void provide_list(Parser* p) { accept(p, T_PROVIDES); - expect(p, T_LPAR); - while (!matches(p, T_RPAR)) { + expect(p, '('); + while (!matches(p, ')')) { expect(p, T_ID); } - expect(p, T_RPAR); + expect(p, ')'); } static AST* definition_list(Parser* p) { @@ -107,7 +107,6 @@ static AST* definition_list(Parser* p) { } else if (matches(p, T_FUN)) { func_definition(p); } else { - printf("%d\n", peek(p)->type); error(p, "only definitions are allowed at the toplevel"); } } @@ -119,37 +118,37 @@ static AST* const_definition(Parser* p, bool constant) { error(p, "constant or variable definition expected"); expect(p, T_ID); type_expression(p); - expect(p, T_ASSIGN); + expect(p, '='); const_expression(p); - expect(p, T_SEMI); + expect(p, ';'); return NULL; } static AST* type_definition(Parser* p) { expect(p, T_TYPE); expect(p, T_ID); - expect(p, T_ASSIGN); + expect(p, '='); type_expression(p); - expect(p, T_SEMI); + expect(p, ';'); return NULL; } static AST* func_definition(Parser* p) { expect(p, T_FUN); expect(p, T_ID); - expect(p, T_LPAR); - expect(p, T_RPAR); + expect(p, '('); + expect(p, ')'); type_expression(p); - expect(p, T_LBRACE); - expect(p, T_RBRACE); + expect(p, '{'); + expect(p, '}'); return NULL; } static AST* const_expression(Parser* p) { AST* expr = NULL; - if (accept(p, T_LPAR)) { + if (accept(p, '(')) { expr = const_expression(p); - expect(p, T_RPAR); + expect(p, ')'); } else if (matches(p, T_ID)) { expr = identifier(p); } else { @@ -168,26 +167,26 @@ static AST* type_expression(Parser* p) { } else { expect(p, T_ID); } - while (matches(p, T_MUL) || matches(p, T_LBRACK)) { - if (matches(p, T_LBRACK)) { - expect(p, T_LBRACK); - expect(p, T_RBRACK); + while (matches(p, '*') || matches(p, '[')) { + if (matches(p, '[')) { + expect(p, '['); + expect(p, ']'); } else { - expect(p, T_MUL); + expect(p, '*'); } } return NULL; } static AST* struct_fields(Parser* p) { - expect(p, T_LBRACE); + expect(p, '{'); do { expect(p, T_ID); - expect(p, T_ASSIGN); + expect(p, '='); type_expression(p); - expect(p, T_SEMI); - } while(!matches(p, T_RBRACE)); - expect(p, T_RBRACE); + expect(p, ';'); + } while(!matches(p, '}')); + expect(p, '}'); return NULL; } diff --git a/source/pprint.c b/source/pprint.c index ac01cea..ee85b8a 100644 --- a/source/pprint.c +++ b/source/pprint.c @@ -12,18 +12,18 @@ static const char* token_type_to_string(TokType type) { case T_INT: return "T_INT"; case T_FLOAT: return "T_FLOAT"; case T_BOOL: return "T_BOOL"; - case T_LBRACE: return "T_LBRACE"; - case T_RBRACE: return "T_RBRACE"; - case T_LBRACK: return "T_LBRACK"; - case T_RBRACK: return "T_RBRACK"; - case T_LPAR: return "T_LPAR"; - case T_RPAR: return "T_RPAR"; - case T_COMMA: return "T_COMMA"; case T_ID: return "T_ID"; - case T_COLON: return "T_COLON"; - case T_AMP: return "T_AMP"; - case T_SQUOTE: return "T_SQUOTE"; - case T_DQUOTE: return "T_DQUOTE"; + case '{': return "T_LBRACE"; + case '}': return "T_RBRACE"; + case '[': return "T_LBRACK"; + case ']': return "T_RBRACK"; + case '(': return "T_LPAR"; + case ')': return "T_RPAR"; + case ',': return "T_COMMA"; + case ':': return "T_COLON"; + case '&': return "T_AMP"; + case '\'': return "T_SQUOTE"; + case '"': return "T_DQUOTE"; case T_END_FILE: return "T_END_FILE"; default: return "???"; } @@ -70,7 +70,7 @@ void pprint_token(FILE* file, Tok* token, bool print_loc) fprintf(file, "%zu:", token->col); } pprint_token_type(file, token); - if (token->type < T_LBRACE) { + if (token->type > 256) { fprintf(file, ":"); pprint_token_value(file, token); } diff --git a/source/sclpl.h b/source/sclpl.h index 4ecd42a..02c129a 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -40,12 +40,12 @@ void vec_set(vec_t* vec, size_t index, void* data); /* Token Types *****************************************************************************/ typedef enum { - T_NONE, T_ERROR, T_END_FILE, + T_NONE = 0, T_ERROR = 256, T_END_FILE, T_PACKAGE, T_REQUIRES, T_PROVIDES, T_LET, T_VAR, T_FUN, T_TYPE, T_STRUCT, T_UNION, T_ID, T_CHAR, T_INT, T_FLOAT, T_BOOL, T_STRING, - T_LBRACE, T_RBRACE, T_LBRACK, T_RBRACK, T_LPAR, T_RPAR, T_COMMA, T_SQUOTE, - T_DQUOTE, T_COLON, T_AMP, T_IF, T_ELSE, T_ASSIGN, T_SEMI, T_MUL +// T_LBRACE, T_RBRACE, T_LBRACK, T_RBRACK, T_LPAR, T_RPAR, T_COMMA, T_SQUOTE, +// T_DQUOTE, T_COLON, T_AMP, T_IF, T_ELSE, T_ASSIGN, T_SEMI, T_MUL } TokType; typedef struct { -- 2.54.0