mirror of
				https://github.com/GayPizzaSpecifications/pork.git
				synced 2025-11-03 17:39:38 +00:00 
			
		
		
		
	Support if without else.
This commit is contained in:
		@ -5,4 +5,5 @@ fib = { n in
 | 
				
			|||||||
    then 1
 | 
					    then 1
 | 
				
			||||||
  else fib(n - 1) + fib(n - 2)
 | 
					  else fib(n - 1) + fib(n - 2)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
main = { in fib(20) }
 | 
					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
 | 
					  trueValue = true
 | 
				
			||||||
  falseValue = false
 | 
					  falseValue = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  check = { in
 | 
				
			||||||
 | 
					    if true then false else true
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  [
 | 
					  [
 | 
				
			||||||
    calculateSimpleResult,
 | 
					    calculateSimpleResult,
 | 
				
			||||||
    calculateComplexResult,
 | 
					    calculateComplexResult,
 | 
				
			||||||
    multiplyResult,
 | 
					    multiplyResult,
 | 
				
			||||||
    list,
 | 
					    list,
 | 
				
			||||||
    trueValue,
 | 
					    trueValue,
 | 
				
			||||||
    falseValue
 | 
					    falseValue,
 | 
				
			||||||
 | 
					    check()
 | 
				
			||||||
  ]
 | 
					  ]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -3,7 +3,7 @@ package gay.pizza.pork.ast
 | 
				
			|||||||
class If(
 | 
					class If(
 | 
				
			||||||
  val condition: Expression,
 | 
					  val condition: Expression,
 | 
				
			||||||
  val thenExpression: Expression,
 | 
					  val thenExpression: Expression,
 | 
				
			||||||
  val elseExpression: Expression
 | 
					  val elseExpression: Expression? = null
 | 
				
			||||||
) : Expression {
 | 
					) : Expression {
 | 
				
			||||||
  override val type: NodeType = NodeType.If
 | 
					  override val type: NodeType = NodeType.If
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -44,8 +44,10 @@ class Printer(private val buffer: StringBuilder) : Visitor<Unit> {
 | 
				
			|||||||
    visit(node.condition)
 | 
					    visit(node.condition)
 | 
				
			||||||
    append(" then ")
 | 
					    append(" then ")
 | 
				
			||||||
    visit(node.thenExpression)
 | 
					    visit(node.thenExpression)
 | 
				
			||||||
    append(" else ")
 | 
					    if (node.elseExpression != null) {
 | 
				
			||||||
    visit(node.elseExpression)
 | 
					      append(" else ")
 | 
				
			||||||
 | 
					      visit(node.elseExpression)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  override fun visitSymbol(node: Symbol) {
 | 
					  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) {
 | 
					    return if (condition == true) {
 | 
				
			||||||
      visit(node.thenExpression)
 | 
					      visit(node.thenExpression)
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      visit(node.elseExpression)
 | 
					      if (node.elseExpression != null) {
 | 
				
			||||||
 | 
					        visit(node.elseExpression)
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        None
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  override fun visitSymbol(node: Symbol): Any {
 | 
					  override fun visitSymbol(node: Symbol): Any {
 | 
				
			||||||
    return Unit
 | 
					    return None
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  override fun visitLambda(node: Lambda): CallableFunction {
 | 
					  override fun visitLambda(node: Lambda): CallableFunction {
 | 
				
			||||||
@ -43,7 +47,7 @@ class PorkEvaluator(root: Scope) : Visitor<Any> {
 | 
				
			|||||||
        for (expression in node.expressions) {
 | 
					        for (expression in node.expressions) {
 | 
				
			||||||
          value = visit(expression)
 | 
					          value = visit(expression)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        value ?: Unit
 | 
					        value ?: None
 | 
				
			||||||
      } finally {
 | 
					      } finally {
 | 
				
			||||||
        currentScope = currentScope.leave()
 | 
					        currentScope = currentScope.leave()
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@ -88,6 +92,6 @@ class PorkEvaluator(root: Scope) : Visitor<Any> {
 | 
				
			|||||||
    for (expression in node.expressions) {
 | 
					    for (expression in node.expressions) {
 | 
				
			||||||
      value = visit(expression)
 | 
					      value = visit(expression)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return value ?: Unit
 | 
					    return value ?: None
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -18,8 +18,11 @@ class PorkParser(val source: PeekableSource<Token>) {
 | 
				
			|||||||
    val condition = readExpression()
 | 
					    val condition = readExpression()
 | 
				
			||||||
    expect(TokenType.Then)
 | 
					    expect(TokenType.Then)
 | 
				
			||||||
    val thenExpression = readExpression()
 | 
					    val thenExpression = readExpression()
 | 
				
			||||||
    expect(TokenType.Else)
 | 
					    var elseExpression: Expression? = null
 | 
				
			||||||
    val elseExpression = readExpression()
 | 
					    if (peekType(TokenType.Else)) {
 | 
				
			||||||
 | 
					      expect(TokenType.Else)
 | 
				
			||||||
 | 
					      elseExpression = readExpression()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    return If(condition, thenExpression, elseExpression)
 | 
					    return If(condition, thenExpression, elseExpression)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user