@@ -75,5 +75,4 @@ public struct CompilerContext {
|
|||||||
return CompilerContext(withNames: self.names, withTypes: types)
|
return CompilerContext(withNames: self.names, withTypes: types)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -17,9 +17,9 @@
|
|||||||
|
|
||||||
import Common
|
import Common
|
||||||
import P4Lang
|
import P4Lang
|
||||||
|
import P4Runtime
|
||||||
import SwiftTreeSitter
|
import SwiftTreeSitter
|
||||||
import TreeSitterP4
|
import TreeSitterP4
|
||||||
import P4Runtime
|
|
||||||
|
|
||||||
protocol CompilableExpression {
|
protocol CompilableExpression {
|
||||||
static func compile(
|
static func compile(
|
||||||
@@ -109,7 +109,8 @@ struct Expression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let localElementsParsers: [CompilableExpression.Type] = [
|
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 {
|
for le_parser in localElementsParsers {
|
||||||
@@ -250,7 +251,8 @@ extension BinaryOperatorExpression: CompilableExpression {
|
|||||||
) -> Result<(EvaluatableExpression)?> {
|
) -> Result<(EvaluatableExpression)?> {
|
||||||
let expression = node.child(at: 0)!
|
let expression = node.child(at: 0)!
|
||||||
|
|
||||||
#SkipUnlessNodeType<Node, EvaluatableExpression?>(node: expression, type: "binaryOperatorExpression")
|
#SkipUnlessNodeType<Node, EvaluatableExpression?>(
|
||||||
|
node: expression, type: "binaryOperatorExpression")
|
||||||
|
|
||||||
var currentChildIdx = 0
|
var currentChildIdx = 0
|
||||||
var currentChildIdxSafe = 1
|
var currentChildIdxSafe = 1
|
||||||
@@ -263,7 +265,9 @@ extension BinaryOperatorExpression: CompilableExpression {
|
|||||||
currentChild = expression.child(at: currentChildIdx)
|
currentChild = expression.child(at: currentChildIdx)
|
||||||
|
|
||||||
let binary_operator_expression_node = currentChild!
|
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 {
|
if binary_operator_expression_node.childCount < currentChildIdxSafe {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
@@ -276,16 +280,17 @@ extension BinaryOperatorExpression: CompilableExpression {
|
|||||||
currentChildIdxSafe = currentChildIdxSafe + 1
|
currentChildIdxSafe = currentChildIdxSafe + 1
|
||||||
if binary_operator_expression_node.childCount < currentChildIdxSafe {
|
if binary_operator_expression_node.childCount < currentChildIdxSafe {
|
||||||
return Result.Error(
|
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)
|
currentChild = binary_operator_expression_node.child(at: currentChildIdx)
|
||||||
|
|
||||||
|
|
||||||
currentChildIdx = currentChildIdx + 1
|
currentChildIdx = currentChildIdx + 1
|
||||||
currentChildIdxSafe = currentChildIdxSafe + 1
|
currentChildIdxSafe = currentChildIdxSafe + 1
|
||||||
if binary_operator_expression_node.childCount < currentChildIdxSafe {
|
if binary_operator_expression_node.childCount < currentChildIdxSafe {
|
||||||
return Result.Error(
|
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)
|
currentChild = binary_operator_expression_node.child(at: currentChildIdx)
|
||||||
let right_hand_side_raw = currentChild!
|
let right_hand_side_raw = currentChild!
|
||||||
|
|||||||
@@ -54,7 +54,8 @@ extension ParserAssignmentStatement: CompilableStatement {
|
|||||||
guard case .Ok(let lvalue_identifier) = maybe_parsed_lvalue else {
|
guard case .Ok(let lvalue_identifier) = maybe_parsed_lvalue else {
|
||||||
return Result.Error(maybe_parsed_lvalue.error()!)
|
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(
|
return Result.Error(
|
||||||
ErrorOnNode(
|
ErrorOnNode(
|
||||||
node: lvalue_node,
|
node: lvalue_node,
|
||||||
|
|||||||
@@ -168,7 +168,8 @@ public struct Program {
|
|||||||
{
|
{
|
||||||
case Result.Ok((let parser, let updated_context)):
|
case Result.Ok((let parser, let updated_context)):
|
||||||
// Create a new context with the name of the parser that was just compiled in scope.
|
// 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)
|
case Result.Error(let error): errors.append(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -220,7 +220,8 @@ extension VariableDeclarationStatement: CompilableStatement {
|
|||||||
VariableDeclarationStatement(
|
VariableDeclarationStatement(
|
||||||
identifier: parsed_variablename, withInitializer: parsed_rvalue),
|
identifier: parsed_variablename, withInitializer: parsed_rvalue),
|
||||||
// Context with updated names to include the newly declared name.
|
// Context with updated names to include the newly declared name.
|
||||||
context.update(newNames: context.names.declare(
|
context.update(
|
||||||
|
newNames: context.names.declare(
|
||||||
identifier: parsed_variablename, withValue: declaration_p4_type))
|
identifier: parsed_variablename, withValue: declaration_p4_type))
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user