Skip to content

Commit

Permalink
Merge pull request #164 from d409f19/limited-exprs
Browse files Browse the repository at this point in the history
Disallow function calls and neighbourhoods in constant declarations
  • Loading branch information
NicEastvillage authored May 16, 2019
2 parents 0b3d759 + 02d8648 commit d359346
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
11 changes: 5 additions & 6 deletions compiler/src/main/kotlin/visitors/sanitychecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ class DimensionsError(ctx: SourceContext, description: String) : CompileError(ct
*/
class SanityChecker : BaseASTVisitor() {

var inAFunction = false
var dimensions: Int = 0
var worldHasEdge = false
private var inAFunction = false
private var inAState = false
private var numberOfStates = 0
private var dimensions: Int = 0
private var worldHasEdge = false

override fun visit(node: WorldNode) {
dimensions = node.dimensions.size
Expand Down Expand Up @@ -86,9 +88,6 @@ class SanityChecker : BaseASTVisitor() {
ErrorLogger += SanityError(node.ctx, "Become statements cannot be in functions")
}

var inAState = false
var numberOfStates = 0

override fun visit(node: StateDecl) {
inAState = true
super.visit(node)
Expand Down
16 changes: 15 additions & 1 deletion compiler/src/main/kotlin/visitors/typechecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ class TypeError(ctx: SourceContext, description: String) : CompileError(ctx, des
* Synthesizes types by moving them up the abstract syntax tree according to the type rules, and check that there is no violation of the type rules
*/
class TypeChecker(symbolTable: Table) : ScopedASTVisitor(symbolTable = symbolTable) {

private var expectedReturn: Type? = null
private var isOuterArray = true
// Constants are limited. Function calls and neighbourhoods are not allowed in limited expressions
private var inLimitedConstExpr = false

override fun visit(node: WorldNode) {
node.dimensions.forEach { visit(it) }
Expand Down Expand Up @@ -351,7 +354,12 @@ class TypeChecker(symbolTable: Table) : ScopedASTVisitor(symbolTable = symbolTab
node.setType(
when (decl) {
is StateDecl -> StateType
is NeighbourhoodDecl -> LocalNeighbourhoodType
is NeighbourhoodDecl -> {
if (inLimitedConstExpr) {
ErrorLogger += TypeError(node.ctx, "Neighbourhoods are not allowed in constant declarations.")
}
LocalNeighbourhoodType
}
is AssignStmt -> decl.expr.getType()
is ConstDecl -> decl.expr.getType()
is FunctionArgument -> decl.type
Expand Down Expand Up @@ -533,6 +541,10 @@ class TypeChecker(symbolTable: Table) : ScopedASTVisitor(symbolTable = symbolTab
override fun visit(node: FuncCallExpr) {
super.visit(node)

if (inLimitedConstExpr) {
ErrorLogger += TypeError(node.ctx, "Function calls are not allowed in constant declarations.")
}

val funcDecl = symbolTableSession.getSymbol(node.ident)

// If node is not a function, register error and continue type-checking
Expand Down Expand Up @@ -594,8 +606,10 @@ class TypeChecker(symbolTable: Table) : ScopedASTVisitor(symbolTable = symbolTab
}

override fun visit(node: ConstDecl) {
inLimitedConstExpr = true
super.visit(node)
node.type = node.expr.getType()
inLimitedConstExpr = false
}

override fun visit(node: AssignStmt) {
Expand Down
15 changes: 15 additions & 0 deletions compiler/src/main/resources/non-compiling-programs/const-fun.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
world {
size = 100 [wrap], 100 [wrap];
tickrate = 1;
cellsize = 5;
}

const c = 20 + foo(i)

state alive (0, 0, 0) {

}

function foo(int i) int {
return i + i;
}
15 changes: 15 additions & 0 deletions compiler/src/main/resources/non-compiling-programs/const-ngbh.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
world {
size = 100 [wrap], 100 [wrap];
tickrate = 1;
cellsize = 5;
}

neighbourhood nei {
(1, 0)
}

const c = alive == nei[0];

state alive (0, 0, 0) {

}

0 comments on commit d359346

Please sign in to comment.