]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
first attempt at if statement parsing
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 25 Jun 2018 18:33:13 +0000 (14:33 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 25 Jun 2018 18:33:13 +0000 (14:33 -0400)
example.src
source/lexer.l
source/parser.c
source/sclpl.h

index c6f51e66133817c62ffc61252db836b75ad5cdda..f1885fd333a2b3cc2b778f75be481bfff56c2a4b 100644 (file)
@@ -36,6 +36,11 @@ fun main(args string[]) int {
     foo();
     bar(1);
     baz(1,2);
+#    if (123) {}
+#    if 123 {}
+#    if (123) {} else {}
+#    if (123) {} else if {}
+#    if (123) {} else if {} else {}
     return bar;
 }
 
index 22dc4e9cb95d04f63872faca8982e696b58badb9..ce19730461d5052fa1b847cea4969a6c5a77ab6c 100644 (file)
@@ -44,6 +44,8 @@ NOSPACE [^ \t\r\n]
 "struct"  { return T_STRUCT;   }
 "union"   { return T_UNION;    }
 "return"  { return T_RETURN;   }
+"if"      { return T_IF;       }
+"else"    { return T_ELSE;     }
 
 "("    { return '(';    }
 ")"    { return ')';    }
index ac282d2ab907c323918aea8e46e1fb954cbe37ef..16fc84a612cc9b06ab0aedd811ba5e244f010da6 100644 (file)
@@ -13,6 +13,8 @@ static AST* expression_block(Parser* p);
 static AST* statement(Parser* p);
 static AST* expression(Parser* p);
 static AST* definition(Parser* p, bool constant);
+static AST* return_statement(Parser* p);
+static AST* if_statement(Parser* p);
 static AST* func_expr_list(Parser* p);
 
 static AST* const_expression(Parser* p);
@@ -221,10 +223,10 @@ static AST* statement(Parser* p) {
     } else {
         if (matches(p, T_LET) || matches(p, T_VAR)) {
             definition(p, (peek(p)->type == T_LET));
-        } else if (accept(p, T_RETURN)) {
-            expression(p);
-            expect(p, ';');
-//        } else if (matches(p, T_IF)) {
+        } else if (matches(p, T_RETURN)) {
+            return_statement(p);
+        } else if (matches(p, T_IF)) {
+            if_statement(p);
 //        } else if (matches(p, T_WHILE)) {
 //        } else if (matches(p, T_FOR)) {
 //        } else if (matches(p, T_DO)) {
@@ -264,6 +266,27 @@ static AST* definition(Parser* p, bool constant) {
     return NULL;
 }
 
+static AST* return_statement(Parser* p) {
+    expect(p, T_RETURN);
+    expression(p);
+    expect(p, ';');
+    return NULL;
+}
+
+static AST* if_statement(Parser* p) {
+    expect(p, T_IF);
+    expression(p);
+    expression_block(p);
+    if (accept(p, T_ELSE)) {
+        if (matches(p, T_IF)) {
+            if_statement(p);
+        } else {
+            expression_block(p);
+        }
+    }
+    return NULL;
+}
+
 static AST* func_expr_list(Parser* p) {
     expect(p, '(');
     if (!matches(p, ')')) {
index aa3cea8948aab419fbf2fe3fdc4ef511209f8564..069e116d3376a9e025a5f941c8add20ce30ff911 100644 (file)
@@ -42,7 +42,7 @@ void vec_set(vec_t* vec, size_t index, void* data);
 typedef enum {
     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_RETURN,
+    T_UNION, T_RETURN, T_IF, T_ELSE,
     T_ID, T_CHAR, T_INT, T_FLOAT, T_BOOL, T_STRING,
 } TokType;