From c728dd6b8c07510b846ed72b3793f23d1ed27ffe Mon Sep 17 00:00:00 2001
From: cosmonaut <evan@moonside.games>
Date: Thu, 29 Apr 2021 15:26:30 -0700
Subject: [PATCH] if-else chains

---
 CMakeLists.txt      |  2 +-
 example.w           |  6 +++++-
 generators/wraith.y | 14 ++++++++++++--
 src/ast.h           |  3 ++-
 src/codegen.c       | 16 +++++++++++++---
 5 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 51630b6..56d5ca6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,7 +23,7 @@ find_package(LLVM)
 
 include_directories(${CMAKE_SOURCE_DIR})
 
-BISON_TARGET(Parser generators/wraith.y ${CMAKE_CURRENT_BINARY_DIR}/y.tab.c COMPILE_FLAGS "-d -v -t")
+BISON_TARGET(Parser generators/wraith.y ${CMAKE_CURRENT_BINARY_DIR}/y.tab.c COMPILE_FLAGS "-d -v -t -Wcounterexamples")
 FLEX_TARGET(Scanner generators/wraith.lex ${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c)
 
 ADD_FLEX_BISON_DEPENDENCY(Scanner Parser)
diff --git a/example.w b/example.w
index 2248179..51b8942 100644
--- a/example.w
+++ b/example.w
@@ -45,10 +45,14 @@ struct Program
         {
             myStruct.Increment();
         }
-        else
+        else if (myStruct.myInt > 10)
         {
             myStruct.Decrement();
         }
+        else
+        {
+            myStruct.myInt = 4;
+        }
 
         return myStruct.myInt;
     }
diff --git a/generators/wraith.y b/generators/wraith.y
index 6d7e861..831bb76 100644
--- a/generators/wraith.y
+++ b/generators/wraith.y
@@ -65,7 +65,9 @@ extern FILE *yyin;
 
 %define parse.error verbose
 
+%left GREATER_THAN LESS_THAN
 %left PLUS MINUS
+%left STAR
 %left BANG
 %left LEFT_PAREN RIGHT_PAREN
 
@@ -174,10 +176,14 @@ BinaryExpression        : Expression PLUS Expression
                         {
                             $$ = MakeBinaryNode(LessThan, $1, $3);
                         }
+                        | Expression GREATER_THAN Expression
+                        {
+                            $$ = MakeBinaryNode(GreaterThan, $1, $3);
+                        }
 
-Expression              : PrimaryExpression
+Expression              : BinaryExpression
                         | UnaryExpression
-                        | BinaryExpression
+                        | PrimaryExpression
                         | HeapAllocation
                         ;
 
@@ -229,6 +235,10 @@ Conditional             : IfStatement
                         {
                             $$ = MakeIfElseNode($1, $4);
                         }
+                        | IfStatement ELSE Conditional
+                        {
+                            $$ = MakeIfElseNode($1, $3);
+                        }
 
 Statement               : PartialStatement SEMICOLON
                         | Conditional
diff --git a/src/ast.h b/src/ast.h
index 9a37da6..ac65995 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -47,7 +47,8 @@ typedef enum
     Add,
     Subtract,
     Multiply,
-    LessThan
+    LessThan,
+    GreaterThan
 } BinaryOperator;
 
 typedef enum
diff --git a/src/codegen.c b/src/codegen.c
index c9d95f3..4719317 100644
--- a/src/codegen.c
+++ b/src/codegen.c
@@ -452,7 +452,10 @@ static LLVMValueRef CompileBinaryExpression(
 
         /* FIXME: need type information for comparison */
         case LessThan:
-            return LLVMBuildICmp(builder, LLVMIntSLT, left, right, "compareResult");
+            return LLVMBuildICmp(builder, LLVMIntSLT, left, right, "lessThanResult");
+
+        case GreaterThan:
+            return LLVMBuildICmp(builder, LLVMIntSGT, left, right, "greaterThanResult");
     }
 
     return NULL;
@@ -670,9 +673,16 @@ static void CompileIfElseStatement(LLVMBuilderRef builder, LLVMValueRef function
 
     LLVMPositionBuilderAtEnd(builder, elseBlock);
 
-    for (i = 0; i < ifElseStatement->children[1]->childCount; i += 1)
+    if (ifElseStatement->children[1]->syntaxKind == StatementSequence)
     {
-        CompileStatement(builder, function, ifElseStatement->children[1]->children[i]);
+        for (i = 0; i < ifElseStatement->children[1]->childCount; i += 1)
+        {
+            CompileStatement(builder, function, ifElseStatement->children[1]->children[i]);
+        }
+    }
+    else
+    {
+        CompileStatement(builder, function, ifElseStatement->children[1]);
     }
 
     LLVMBuildBr(builder, afterCond);