mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 21:21:33 +00:00
language: add boolean and/or operators, change negation syntax (#7)
* language: add boolean and/or operators, change negation syntax * examples: simplify row builder using arrays
This commit is contained in:
@ -92,7 +92,7 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
|
||||
override fun visitPrefixOperation(node: PrefixOperation): Any {
|
||||
val value = node.expression.visit(this)
|
||||
return when (node.op) {
|
||||
PrefixOperator.Negate -> {
|
||||
PrefixOperator.BooleanNot -> {
|
||||
if (value !is Boolean) {
|
||||
throw RuntimeException("Cannot negate a value which is not a boolean.")
|
||||
}
|
||||
@ -192,6 +192,14 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
|
||||
else -> {}
|
||||
}
|
||||
|
||||
if (left is Boolean && right is Boolean) {
|
||||
when (node.op) {
|
||||
InfixOperator.BooleanAnd -> return left && right
|
||||
InfixOperator.BooleanOr -> return left || right
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
if (left !is Number || right !is Number) {
|
||||
throw RuntimeException("Failed to evaluate infix operation, bad types.")
|
||||
}
|
||||
@ -311,7 +319,7 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
|
||||
InfixOperator.Minus -> subtract(convert(left), convert(right))
|
||||
InfixOperator.Multiply -> multiply(convert(left), convert(right))
|
||||
InfixOperator.Divide -> divide(convert(left), convert(right))
|
||||
InfixOperator.Equals, InfixOperator.NotEquals -> throw RuntimeException("Unable to handle operation $op")
|
||||
InfixOperator.Equals, InfixOperator.NotEquals, InfixOperator.BooleanAnd, InfixOperator.BooleanOr -> throw RuntimeException("Unable to handle operation $op")
|
||||
InfixOperator.BinaryAnd -> binaryAnd(convert(left), convert(right))
|
||||
InfixOperator.BinaryOr -> binaryOr(convert(left), convert(right))
|
||||
InfixOperator.BinaryExclusiveOr -> binaryExclusiveOr(convert(left), convert(right))
|
||||
@ -333,7 +341,7 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
|
||||
binaryNot: (T) -> T
|
||||
): Any {
|
||||
return when (op) {
|
||||
PrefixOperator.Negate -> throw RuntimeException("Unable to handle operation $op")
|
||||
PrefixOperator.BooleanNot -> throw RuntimeException("Unable to handle operation $op")
|
||||
PrefixOperator.UnaryPlus -> plus(convert(value))
|
||||
PrefixOperator.UnaryMinus -> minus(convert(value))
|
||||
PrefixOperator.BinaryNot -> binaryNot(convert(value))
|
||||
|
Reference in New Issue
Block a user