]> git.frustrated-labs.net Git - so-test.git/commitdiff
feat: parse simple expressions
authorAlexander Goussas <[email protected]>
Thu, 31 Oct 2024 12:28:00 +0000 (07:28 -0500)
committerAlexander Goussas <[email protected]>
Thu, 31 Oct 2024 12:28:00 +0000 (07:28 -0500)
lexer.c
parser.c

diff --git a/lexer.c b/lexer.c
index 54e4f1a49236c5ec7a90f7167410da529d19b56c..bedc58f1a0b23a941c945bc308f9262d78711f49 100644 (file)
--- a/lexer.c
+++ b/lexer.c
@@ -116,7 +116,7 @@ so_token so_lexer_string(so_lexer *l)
 
     if (so_lexer_peek(l) != '"')
     {
-        fprintf(stderr, "Unterminated string literal at %d\n", l->current);
+        fprintf(stderr, "warning: unterminated string literal at %d\n", l->current);
         return (so_token){.lexeme = strdup("invalid"), .type = SO_TT_INVALID};
     }
 
index e902410280df91cb3976183a6f11a9cb596f9bca..b01276c9138b8f16b9f106325304c923d24a29c1 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -67,7 +67,7 @@ void so_parser_parse(so_parser *p)
             so_token_deinit(&p->current);
             return;
         default:
-            fprintf(stderr, "warning: invalid start of simple expression: %s\n", p->current.lexeme);
+            fprintf(stderr, "warning: invalid start of expression, expected use or call: %s\n", p->current.lexeme);
             so_token_deinit(&p->current);
         }
     }
@@ -97,7 +97,17 @@ void so_parse_call(so_parser *p)
     int nargs = 0;
 
     while (so_parser_advance(p).type != SO_TT_EOF && nargs < MAX_CALL_ARGS)
-        args[nargs++] = so_parser_parse_simple_expression(p);
+    {
+        so_expr *e = so_parser_parse_simple_expression(p);
+        if (e == NULL)
+        {
+            fprintf(stderr, "warning: expected either a string or a number\n");
+            so_token_deinit(&p->current);
+            continue;
+        }
+
+        args[nargs++] = e;
+    }
 
     if (!so_parser_expect(p, SO_TT_EOF))
     {
@@ -140,4 +150,20 @@ void so_parse_load(so_parser *p)
 
 so_expr *so_parser_parse_simple_expression(so_parser *p)
 {
+    so_expr *e = NULL;
+
+    if (p->current.type == SO_TT_STRING)
+    {
+        e = create_string_node(p->current.lexeme);
+        so_token_deinit(&p->current);
+    }
+
+    if (p->current.type == SO_TT_INTEGER)
+    {
+        int n = atoi(p->current.lexeme);
+        e = create_number_node(n);
+        so_token_deinit(&p->current);
+    }
+
+    return e;
 }