if-else chains

pull/1/head
cosmonaut 2021-04-29 15:26:30 -07:00
parent 708d4de0ae
commit c728dd6b8c
5 changed files with 33 additions and 8 deletions

View File

@ -23,7 +23,7 @@ find_package(LLVM)
include_directories(${CMAKE_SOURCE_DIR}) 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) FLEX_TARGET(Scanner generators/wraith.lex ${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c)
ADD_FLEX_BISON_DEPENDENCY(Scanner Parser) ADD_FLEX_BISON_DEPENDENCY(Scanner Parser)

View File

@ -45,10 +45,14 @@ struct Program
{ {
myStruct.Increment(); myStruct.Increment();
} }
else else if (myStruct.myInt > 10)
{ {
myStruct.Decrement(); myStruct.Decrement();
} }
else
{
myStruct.myInt = 4;
}
return myStruct.myInt; return myStruct.myInt;
} }

View File

@ -65,7 +65,9 @@ extern FILE *yyin;
%define parse.error verbose %define parse.error verbose
%left GREATER_THAN LESS_THAN
%left PLUS MINUS %left PLUS MINUS
%left STAR
%left BANG %left BANG
%left LEFT_PAREN RIGHT_PAREN %left LEFT_PAREN RIGHT_PAREN
@ -174,10 +176,14 @@ BinaryExpression : Expression PLUS Expression
{ {
$$ = MakeBinaryNode(LessThan, $1, $3); $$ = MakeBinaryNode(LessThan, $1, $3);
} }
| Expression GREATER_THAN Expression
{
$$ = MakeBinaryNode(GreaterThan, $1, $3);
}
Expression : PrimaryExpression Expression : BinaryExpression
| UnaryExpression | UnaryExpression
| BinaryExpression | PrimaryExpression
| HeapAllocation | HeapAllocation
; ;
@ -229,6 +235,10 @@ Conditional : IfStatement
{ {
$$ = MakeIfElseNode($1, $4); $$ = MakeIfElseNode($1, $4);
} }
| IfStatement ELSE Conditional
{
$$ = MakeIfElseNode($1, $3);
}
Statement : PartialStatement SEMICOLON Statement : PartialStatement SEMICOLON
| Conditional | Conditional

View File

@ -47,7 +47,8 @@ typedef enum
Add, Add,
Subtract, Subtract,
Multiply, Multiply,
LessThan LessThan,
GreaterThan
} BinaryOperator; } BinaryOperator;
typedef enum typedef enum

View File

@ -452,7 +452,10 @@ static LLVMValueRef CompileBinaryExpression(
/* FIXME: need type information for comparison */ /* FIXME: need type information for comparison */
case LessThan: 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; return NULL;
@ -670,10 +673,17 @@ static void CompileIfElseStatement(LLVMBuilderRef builder, LLVMValueRef function
LLVMPositionBuilderAtEnd(builder, elseBlock); LLVMPositionBuilderAtEnd(builder, elseBlock);
if (ifElseStatement->children[1]->syntaxKind == StatementSequence)
{
for (i = 0; i < ifElseStatement->children[1]->childCount; i += 1) for (i = 0; i < ifElseStatement->children[1]->childCount; i += 1)
{ {
CompileStatement(builder, function, ifElseStatement->children[1]->children[i]); CompileStatement(builder, function, ifElseStatement->children[1]->children[i]);
} }
}
else
{
CompileStatement(builder, function, ifElseStatement->children[1]);
}
LLVMBuildBr(builder, afterCond); LLVMBuildBr(builder, afterCond);