Make Formatter Happy

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-13 08:27:41 -04:00
parent d323434787
commit 4c63e7f92a
7 changed files with 43 additions and 36 deletions
+3 -4
View File
@@ -58,7 +58,7 @@ public struct CompilerContext {
/// Update a compiler context /// Update a compiler context
/// ///
/// Create a new compiler context based on the current with the same types and new names. /// Create a new compiler context based on the current with the same types and new names.
/// ///
/// - Parameter names: a ``LexicalScopes`` with the updated names for the newly created compiler context. /// - Parameter names: a ``LexicalScopes`` with the updated names for the newly created compiler context.
/// - Returns: A new compiler context based on the current with the same types and new names. /// - Returns: A new compiler context based on the current with the same types and new names.
public func update(newNames names: LexicalScopes) -> CompilerContext { public func update(newNames names: LexicalScopes) -> CompilerContext {
@@ -68,12 +68,11 @@ public struct CompilerContext {
/// Update a compiler context /// Update a compiler context
/// ///
/// Create a new compiler context based on the current with the same names and new types. /// Create a new compiler context based on the current with the same names and new types.
/// ///
/// - Parameter types: a ``LexicalScopes`` with the updated types for the newly created compiler context. /// - Parameter types: a ``LexicalScopes`` with the updated types for the newly created compiler context.
/// - Returns: A new compiler context based on the current with the same names and new types. /// - Returns: A new compiler context based on the current with the same names and new types.
public func update(newTypes types: LexicalScopes) -> CompilerContext { public func update(newTypes types: LexicalScopes) -> CompilerContext {
return CompilerContext(withNames: self.names, withTypes: types) return CompilerContext(withNames: self.names, withTypes: types)
} }
}
}
+17 -12
View File
@@ -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(
@@ -272,20 +276,21 @@ extension BinaryOperatorExpression: CompilableExpression {
currentChild = binary_operator_expression_node.child(at: currentChildIdx) currentChild = binary_operator_expression_node.child(at: currentChildIdx)
let left_hand_side_raw = currentChild! let left_hand_side_raw = currentChild!
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)
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!
@@ -305,4 +310,4 @@ extension BinaryOperatorExpression: CompilableExpression {
withEvaluator: ("Binary Equal", P4Boolean.create(), binary_equal_operator_evaluator), withEvaluator: ("Binary Equal", P4Boolean.create(), binary_equal_operator_evaluator),
withLhs: left_hand_side, withRhs: right_hand_side)) withLhs: left_hand_side, withRhs: right_hand_side))
} }
} }
+2 -1
View File
@@ -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,
+2 -1
View File
@@ -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)
} }
+3 -2
View File
@@ -220,8 +220,9 @@ 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(
identifier: parsed_variablename, withValue: declaration_p4_type)) newNames: context.names.declare(
identifier: parsed_variablename, withValue: declaration_p4_type))
)) ))
} else { } else {
+1 -1
View File
@@ -71,4 +71,4 @@ public struct BinaryOperatorExpression {
self.left = lhs self.left = lhs
self.right = rhs self.right = rhs
} }
} }
+15 -15
View File
@@ -95,21 +95,21 @@ public func binary_equal_operator_evaluator(left: P4Value, right: P4Value) -> P4
} }
extension BinaryOperatorExpression: EvaluatableExpression { extension BinaryOperatorExpression: EvaluatableExpression {
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> { public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
let maybe_evaluated_left = self.left.evaluate(execution: execution) let maybe_evaluated_left = self.left.evaluate(execution: execution)
guard case Result.Ok(let evaluated_left) = maybe_evaluated_left else { guard case Result.Ok(let evaluated_left) = maybe_evaluated_left else {
return maybe_evaluated_left 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 type() -> any Common.P4Type { let maybe_evaluated_right = self.right.evaluate(execution: execution)
return self.evaluator.1 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
}
}