From: Michael D. Lowis Date: Thu, 14 Jun 2018 17:14:13 +0000 (-0400) Subject: fixed first pass at function parsing X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=99d085ee09fd3a512c7b873537c78e72e1783d9e;p=proto%2Fsclpl.git fixed first pass at function parsing --- diff --git a/example.src b/example.src index 1d63d64..583ac3e 100644 --- a/example.src +++ b/example.src @@ -22,5 +22,9 @@ var const_string string = "" # type type_intptrptr int** # type type_intref int& +fun main() int { +} + + diff --git a/source/lexer.l b/source/lexer.l index 659355b..f7cb952 100644 --- a/source/lexer.l +++ b/source/lexer.l @@ -33,8 +33,6 @@ NOSPACE [^ \t\r\n] [ \t\r\n] { /* whitespace is insignificant */ } #.*[\r\n] { /* skip line comments */ } -<> { return T_END_FILE; } - "package" { return T_PACKAGE; } "requires" { return T_REQUIRES; } "provides" { return T_PROVIDES; } @@ -115,6 +113,8 @@ false { return T_ID; } +<> { return T_END_FILE; } + . { return T_ERROR; } %% diff --git a/source/parser.c b/source/parser.c index 39ac254..859412b 100644 --- a/source/parser.c +++ b/source/parser.c @@ -103,11 +103,11 @@ static void provide_list(Parser* p) { static AST* definition_list(Parser* p) { while (!matches(p, T_END_FILE)) { TokType type = peek(p)->type; - if (accept(p, T_LET) || accept(p, T_VAR)) { + if (matches(p, T_LET) || matches(p, T_VAR)) { const_definition(p, (type == T_LET)); - } else if (accept(p, T_TYPE)) { + } else if (matches(p, T_TYPE)) { type_definition(p); - } else if (accept(p, T_FUN)) { + } else if (matches(p, T_FUN)) { func_definition(p); } else { error(p, "only definitions are allowed at the toplevel"); @@ -117,6 +117,8 @@ static AST* definition_list(Parser* p) { } static AST* const_definition(Parser* p, bool constant) { + if (!accept(p, T_LET) && !accept(p, T_VAR)) + error(p, "constant or variable definition expected"); Tok* id = expect_val(p, T_ID); Type* type = type_annotation(p); expect(p, T_ASSIGN); @@ -135,6 +137,13 @@ static AST* type_definition(Parser* p) { } static AST* func_definition(Parser* p) { + expect(p, T_FUN); + expect(p, T_ID); + expect(p, T_LPAR); + expect(p, T_RPAR); + type_expression(p); + expect(p, T_LBRACE); + expect(p, T_RBRACE); return NULL; }