mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-16 11:21:32 +00:00
Support if without else.
This commit is contained in:
@ -3,7 +3,7 @@ package gay.pizza.pork.ast
|
||||
class If(
|
||||
val condition: Expression,
|
||||
val thenExpression: Expression,
|
||||
val elseExpression: Expression
|
||||
val elseExpression: Expression? = null
|
||||
) : Expression {
|
||||
override val type: NodeType = NodeType.If
|
||||
}
|
||||
|
@ -44,8 +44,10 @@ class Printer(private val buffer: StringBuilder) : Visitor<Unit> {
|
||||
visit(node.condition)
|
||||
append(" then ")
|
||||
visit(node.thenExpression)
|
||||
append(" else ")
|
||||
visit(node.elseExpression)
|
||||
if (node.elseExpression != null) {
|
||||
append(" else ")
|
||||
visit(node.elseExpression)
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitSymbol(node: Symbol) {
|
||||
|
3
src/main/kotlin/gay/pizza/pork/eval/None.kt
Normal file
3
src/main/kotlin/gay/pizza/pork/eval/None.kt
Normal file
@ -0,0 +1,3 @@
|
||||
package gay.pizza.pork.eval
|
||||
|
||||
data object None
|
@ -24,12 +24,16 @@ class PorkEvaluator(root: Scope) : Visitor<Any> {
|
||||
return if (condition == true) {
|
||||
visit(node.thenExpression)
|
||||
} else {
|
||||
visit(node.elseExpression)
|
||||
if (node.elseExpression != null) {
|
||||
visit(node.elseExpression)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitSymbol(node: Symbol): Any {
|
||||
return Unit
|
||||
return None
|
||||
}
|
||||
|
||||
override fun visitLambda(node: Lambda): CallableFunction {
|
||||
@ -43,7 +47,7 @@ class PorkEvaluator(root: Scope) : Visitor<Any> {
|
||||
for (expression in node.expressions) {
|
||||
value = visit(expression)
|
||||
}
|
||||
value ?: Unit
|
||||
value ?: None
|
||||
} finally {
|
||||
currentScope = currentScope.leave()
|
||||
}
|
||||
@ -88,6 +92,6 @@ class PorkEvaluator(root: Scope) : Visitor<Any> {
|
||||
for (expression in node.expressions) {
|
||||
value = visit(expression)
|
||||
}
|
||||
return value ?: Unit
|
||||
return value ?: None
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,11 @@ class PorkParser(val source: PeekableSource<Token>) {
|
||||
val condition = readExpression()
|
||||
expect(TokenType.Then)
|
||||
val thenExpression = readExpression()
|
||||
expect(TokenType.Else)
|
||||
val elseExpression = readExpression()
|
||||
var elseExpression: Expression? = null
|
||||
if (peekType(TokenType.Else)) {
|
||||
expect(TokenType.Else)
|
||||
elseExpression = readExpression()
|
||||
}
|
||||
return If(condition, thenExpression, elseExpression)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user