@@ -75,5 +75,4 @@ public struct CompilerContext {
|
||||
return CompilerContext(withNames: self.names, withTypes: types)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -17,9 +17,9 @@
|
||||
|
||||
import Common
|
||||
import P4Lang
|
||||
import P4Runtime
|
||||
import SwiftTreeSitter
|
||||
import TreeSitterP4
|
||||
import P4Runtime
|
||||
|
||||
protocol CompilableExpression {
|
||||
static func compile(
|
||||
@@ -109,7 +109,8 @@ struct Expression {
|
||||
}
|
||||
|
||||
let localElementsParsers: [CompilableExpression.Type] = [
|
||||
P4BooleanValue.self, P4StringValue.self, P4IntValue.self, TypedIdentifier.self, BinaryOperatorExpression.self
|
||||
P4BooleanValue.self, P4StringValue.self, P4IntValue.self, TypedIdentifier.self,
|
||||
BinaryOperatorExpression.self,
|
||||
]
|
||||
|
||||
for le_parser in localElementsParsers {
|
||||
@@ -250,7 +251,8 @@ extension BinaryOperatorExpression: CompilableExpression {
|
||||
) -> Result<(EvaluatableExpression)?> {
|
||||
let expression = node.child(at: 0)!
|
||||
|
||||
#SkipUnlessNodeType<Node, EvaluatableExpression?>(node: expression, type: "binaryOperatorExpression")
|
||||
#SkipUnlessNodeType<Node, EvaluatableExpression?>(
|
||||
node: expression, type: "binaryOperatorExpression")
|
||||
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
@@ -263,7 +265,9 @@ extension BinaryOperatorExpression: CompilableExpression {
|
||||
currentChild = expression.child(at: currentChildIdx)
|
||||
|
||||
let binary_operator_expression_node = currentChild!
|
||||
#RequireNodesType<Node, EvaluatableExpression?>(nodes: binary_operator_expression_node, type: ["binaryEqualOperatorExpression"], nice_type_names: ["binary equal operator"])
|
||||
#RequireNodesType<Node, EvaluatableExpression?>(
|
||||
nodes: binary_operator_expression_node, type: ["binaryEqualOperatorExpression"],
|
||||
nice_type_names: ["binary equal operator"])
|
||||
|
||||
if binary_operator_expression_node.childCount < currentChildIdxSafe {
|
||||
return Result.Error(
|
||||
@@ -276,16 +280,17 @@ extension BinaryOperatorExpression: CompilableExpression {
|
||||
currentChildIdxSafe = currentChildIdxSafe + 1
|
||||
if binary_operator_expression_node.childCount < currentChildIdxSafe {
|
||||
return Result.Error(
|
||||
ErrorOnNode(node: node, withError: "Missing binary operator for binary operator expression"))
|
||||
ErrorOnNode(node: node, withError: "Missing binary operator for binary operator expression")
|
||||
)
|
||||
}
|
||||
currentChild = binary_operator_expression_node.child(at: currentChildIdx)
|
||||
|
||||
|
||||
currentChildIdx = currentChildIdx + 1
|
||||
currentChildIdxSafe = currentChildIdxSafe + 1
|
||||
if binary_operator_expression_node.childCount < currentChildIdxSafe {
|
||||
return Result.Error(
|
||||
ErrorOnNode(node: node, withError: "Missing binary operator for binary operator expression"))
|
||||
ErrorOnNode(node: node, withError: "Missing binary operator for binary operator expression")
|
||||
)
|
||||
}
|
||||
currentChild = binary_operator_expression_node.child(at: currentChildIdx)
|
||||
let right_hand_side_raw = currentChild!
|
||||
|
||||
@@ -54,7 +54,8 @@ extension ParserAssignmentStatement: CompilableStatement {
|
||||
guard case .Ok(let lvalue_identifier) = maybe_parsed_lvalue else {
|
||||
return Result.Error(maybe_parsed_lvalue.error()!)
|
||||
}
|
||||
guard case Result.Ok(let lvalue_type) = context.names.lookup(identifier: lvalue_identifier) else {
|
||||
guard case Result.Ok(let lvalue_type) = context.names.lookup(identifier: lvalue_identifier)
|
||||
else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: lvalue_node,
|
||||
|
||||
@@ -168,7 +168,8 @@ public struct Program {
|
||||
{
|
||||
case Result.Ok((let parser, let updated_context)):
|
||||
// Create a new context with the name of the parser that was just compiled in scope.
|
||||
compilation_context = compilation_context.update(newNames: updated_context.names.declare(identifier: parser.name, withValue: parser))
|
||||
compilation_context = compilation_context.update(
|
||||
newNames: updated_context.names.declare(identifier: parser.name, withValue: parser))
|
||||
case Result.Error(let error): errors.append(error)
|
||||
}
|
||||
|
||||
|
||||
@@ -220,8 +220,9 @@ extension VariableDeclarationStatement: CompilableStatement {
|
||||
VariableDeclarationStatement(
|
||||
identifier: parsed_variablename, withInitializer: parsed_rvalue),
|
||||
// Context with updated names to include the newly declared name.
|
||||
context.update(newNames: context.names.declare(
|
||||
identifier: parsed_variablename, withValue: declaration_p4_type))
|
||||
context.update(
|
||||
newNames: context.names.declare(
|
||||
identifier: parsed_variablename, withValue: declaration_p4_type))
|
||||
))
|
||||
|
||||
} else {
|
||||
|
||||
@@ -95,21 +95,21 @@ public func binary_equal_operator_evaluator(left: P4Value, right: P4Value) -> P4
|
||||
}
|
||||
|
||||
extension BinaryOperatorExpression: EvaluatableExpression {
|
||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
||||
let maybe_evaluated_left = self.left.evaluate(execution: execution)
|
||||
guard case Result.Ok(let evaluated_left) = maybe_evaluated_left else {
|
||||
return maybe_evaluated_left
|
||||
}
|
||||
|
||||
let maybe_evaluated_right = self.right.evaluate(execution: execution)
|
||||
guard case Result.Ok(let evaluated_right) = maybe_evaluated_right else {
|
||||
return maybe_evaluated_right
|
||||
}
|
||||
|
||||
return Result.Ok(self.evaluator.2(evaluated_left, evaluated_right))
|
||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
||||
let maybe_evaluated_left = self.left.evaluate(execution: execution)
|
||||
guard case Result.Ok(let evaluated_left) = maybe_evaluated_left else {
|
||||
return maybe_evaluated_left
|
||||
}
|
||||
|
||||
public func type() -> any Common.P4Type {
|
||||
return self.evaluator.1
|
||||
let maybe_evaluated_right = self.right.evaluate(execution: execution)
|
||||
guard case Result.Ok(let evaluated_right) = maybe_evaluated_right else {
|
||||
return maybe_evaluated_right
|
||||
}
|
||||
|
||||
return Result.Ok(self.evaluator.2(evaluated_left, evaluated_right))
|
||||
}
|
||||
|
||||
public func type() -> any Common.P4Type {
|
||||
return self.evaluator.1
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user