@@ -175,7 +175,8 @@ public class P4StructValue: P4Value {
|
||||
return self.stype
|
||||
}
|
||||
|
||||
func bin_op_impl(lhs: P4StructValue, rhs: P4StructValue, op: (P4Value?, P4Value?) -> Bool) -> Bool {
|
||||
func bin_op_impl(lhs: P4StructValue, rhs: P4StructValue, op: (P4Value?, P4Value?) -> Bool) -> Bool
|
||||
{
|
||||
if lhs.stype.fields.count() != rhs.stype.fields.count() {
|
||||
// If there are a different number of fields, then we cannot
|
||||
// possibly be equal.
|
||||
@@ -386,28 +387,28 @@ public class P4BooleanValue: P4Value {
|
||||
guard let bool_rhs = rhs as? P4BooleanValue else {
|
||||
return false
|
||||
}
|
||||
return (self.value ? 1 : 0 ) < (bool_rhs.value ? 1 : 0)
|
||||
return (self.value ? 1 : 0) < (bool_rhs.value ? 1 : 0)
|
||||
}
|
||||
|
||||
public func lte(rhs: P4Value) -> Bool {
|
||||
guard let bool_rhs = rhs as? P4BooleanValue else {
|
||||
return false
|
||||
}
|
||||
return (self.value ? 1 : 0 ) <= (bool_rhs.value ? 1 : 0)
|
||||
return (self.value ? 1 : 0) <= (bool_rhs.value ? 1 : 0)
|
||||
}
|
||||
|
||||
public func gt(rhs: P4Value) -> Bool {
|
||||
guard let bool_rhs = rhs as? P4BooleanValue else {
|
||||
return false
|
||||
}
|
||||
return (self.value ? 1 : 0 ) > (bool_rhs.value ? 1 : 0)
|
||||
return (self.value ? 1 : 0) > (bool_rhs.value ? 1 : 0)
|
||||
}
|
||||
|
||||
public func gte(rhs: P4Value) -> Bool {
|
||||
guard let bool_rhs = rhs as? P4BooleanValue else {
|
||||
return false
|
||||
}
|
||||
return (self.value ? 1 : 0 ) >= (bool_rhs.value ? 1 : 0)
|
||||
return (self.value ? 1 : 0) >= (bool_rhs.value ? 1 : 0)
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
|
||||
@@ -133,7 +133,8 @@ struct Expression {
|
||||
node
|
||||
}
|
||||
|
||||
#RequireNodeType<Node, EvaluatableExpression>(node: expression_node, type: "expression", nice_type_name: "expression")
|
||||
#RequireNodeType<Node, EvaluatableExpression>(
|
||||
node: expression_node, type: "expression", nice_type_name: "expression")
|
||||
|
||||
expression_node = expression_node.child(at: 0)!
|
||||
#RequireNodesType<Node, EvaluatableExpression>(
|
||||
@@ -180,7 +181,8 @@ struct LValue {
|
||||
node
|
||||
}
|
||||
|
||||
#RequireNodeType<Node, EvaluatableExpression>(node: expression_node, type: "expression", nice_type_name: "expression")
|
||||
#RequireNodeType<Node, EvaluatableExpression>(
|
||||
node: expression_node, type: "expression", nice_type_name: "expression")
|
||||
|
||||
expression_node = expression_node.child(at: 0)!
|
||||
#RequireNodesType<Node, EvaluatableExpression>(
|
||||
@@ -381,20 +383,42 @@ extension BinaryOperatorExpression: CompilableExpression {
|
||||
}
|
||||
|
||||
let evaluators = [
|
||||
"binaryEqualOperatorExpression": ("Binary Equal", P4Boolean(), Optional<BinaryOperatorChecker>.none, binary_equal_operator_evaluator),
|
||||
"binaryLessThanOperatorExpression": ("Binary Less Than", P4Boolean(), Optional<BinaryOperatorChecker>.none, binary_lt_operator_evaluator),
|
||||
"binaryLessThanEqualOperatorExpression": ("Binary Less Than Or Equal", P4Boolean(), Optional<BinaryOperatorChecker>.none, binary_lte_operator_evaluator),
|
||||
"binaryGreaterThanOperatorExpression": ("Binary Greater Than", P4Boolean(), Optional<BinaryOperatorChecker>.none, binary_gt_operator_evaluator),
|
||||
"binaryGreaterThanEqualOperatorExpression": ("Binary Greater Than Or Equal", P4Boolean(), Optional<BinaryOperatorChecker>.none, binary_gte_operator_evaluator),
|
||||
"binaryAndOperatorExpression": ("Binary Or", P4Boolean(), binary_and_or_operator_checker, binary_and_operator_evaluator),
|
||||
"binaryOrOperatorExpression": ("Binary And", P4Boolean(), binary_and_or_operator_checker, binary_or_operator_evaluator),
|
||||
"binaryEqualOperatorExpression": (
|
||||
"Binary Equal", P4Boolean(), Optional<BinaryOperatorChecker>.none,
|
||||
binary_equal_operator_evaluator
|
||||
),
|
||||
"binaryLessThanOperatorExpression": (
|
||||
"Binary Less Than", P4Boolean(), Optional<BinaryOperatorChecker>.none,
|
||||
binary_lt_operator_evaluator
|
||||
),
|
||||
"binaryLessThanEqualOperatorExpression": (
|
||||
"Binary Less Than Or Equal", P4Boolean(), Optional<BinaryOperatorChecker>.none,
|
||||
binary_lte_operator_evaluator
|
||||
),
|
||||
"binaryGreaterThanOperatorExpression": (
|
||||
"Binary Greater Than", P4Boolean(), Optional<BinaryOperatorChecker>.none,
|
||||
binary_gt_operator_evaluator
|
||||
),
|
||||
"binaryGreaterThanEqualOperatorExpression": (
|
||||
"Binary Greater Than Or Equal", P4Boolean(), Optional<BinaryOperatorChecker>.none,
|
||||
binary_gte_operator_evaluator
|
||||
),
|
||||
"binaryAndOperatorExpression": (
|
||||
"Binary Or", P4Boolean(), binary_and_or_operator_checker, binary_and_operator_evaluator
|
||||
),
|
||||
"binaryOrOperatorExpression": (
|
||||
"Binary And", P4Boolean(), binary_and_or_operator_checker, binary_or_operator_evaluator
|
||||
),
|
||||
]
|
||||
|
||||
guard let selected_evaluator = evaluators[binary_operator_expression_node.nodeType!] else {
|
||||
return Result.Error(Error(withMessage: "No evaluator for \(binary_operator_expression_node.nodeType!)"))
|
||||
return Result.Error(
|
||||
Error(withMessage: "No evaluator for \(binary_operator_expression_node.nodeType!)"))
|
||||
}
|
||||
|
||||
if let checker = selected_evaluator.2, case .Error(let e) = checker(left_hand_side, right_hand_side) {
|
||||
if let checker = selected_evaluator.2,
|
||||
case .Error(let e) = checker(left_hand_side, right_hand_side)
|
||||
{
|
||||
return Result.Error(e)
|
||||
}
|
||||
|
||||
|
||||
@@ -123,9 +123,13 @@ public func binary_gte_operator_evaluator(left: P4Value, right: P4Value) -> P4Va
|
||||
}
|
||||
}
|
||||
|
||||
public typealias BinaryOperatorChecker = (EvaluatableExpression, EvaluatableExpression) -> Result<()>
|
||||
public typealias BinaryOperatorChecker = (EvaluatableExpression, EvaluatableExpression) -> Result<
|
||||
()
|
||||
>
|
||||
|
||||
public func binary_and_or_operator_checker(left: EvaluatableExpression, right: EvaluatableExpression) -> Result<()> {
|
||||
public func binary_and_or_operator_checker(
|
||||
left: EvaluatableExpression, right: EvaluatableExpression
|
||||
) -> Result<()> {
|
||||
// Check that both are Boolean-typed things!
|
||||
if !(left.type().eq(rhs: P4Boolean()) && right.type().eq(rhs: P4Boolean())) {
|
||||
return .Error(Error(withMessage: "And/Or on operands with non-bool type is not allowed"))
|
||||
|
||||
Reference in New Issue
Block a user