if-else chains

generics
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})
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)

View File

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

View File

@ -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

View File

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

View File

@ -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,10 +673,17 @@ static void CompileIfElseStatement(LLVMBuilderRef builder, LLVMValueRef function
LLVMPositionBuilderAtEnd(builder, elseBlock);
if (ifElseStatement->children[1]->syntaxKind == StatementSequence)
{
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);