mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 05:10:55 +00:00
Support if without else.
This commit is contained in:
parent
cccea9c2ca
commit
903f730d51
@ -5,4 +5,5 @@ fib = { n in
|
||||
then 1
|
||||
else fib(n - 1) + fib(n - 2)
|
||||
}
|
||||
|
||||
main = { in fib(20) }
|
||||
|
7
examples/if.pork
Normal file
7
examples/if.pork
Normal file
@ -0,0 +1,7 @@
|
||||
check = { value in
|
||||
if value then 50
|
||||
}
|
||||
|
||||
main = { in
|
||||
value = check(100)
|
||||
}
|
@ -18,12 +18,17 @@ main = { in
|
||||
trueValue = true
|
||||
falseValue = false
|
||||
|
||||
check = { in
|
||||
if true then false else true
|
||||
}
|
||||
|
||||
[
|
||||
calculateSimpleResult,
|
||||
calculateComplexResult,
|
||||
multiplyResult,
|
||||
list,
|
||||
trueValue,
|
||||
falseValue
|
||||
falseValue,
|
||||
check()
|
||||
]
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user