Work On Derived Types

1. Add support for field access
2. Add support for proper type checking of array access
3. Add tests for nested field and array access

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-16 08:31:16 -04:00
parent 8ee20fcf9f
commit 2fd5ecf8d6
10 changed files with 629 additions and 72 deletions
+102 -42
View File
@@ -27,6 +27,10 @@ public class Identifier: CustomStringConvertible, Equatable, Hashable {
self.name = name
}
public init(id: Identifier) {
self.name = id.name
}
public var description: String {
return "\(name)"
}
@@ -38,13 +42,18 @@ public class Identifier: CustomStringConvertible, Equatable, Hashable {
/// A P4 identifier
public class TypedIdentifier: Identifier {
public var parsed_type: P4Type
public var type: P4Type
public init(name: String, withType type: P4Type) {
self.parsed_type = type
self.type = type
super.init(name: name)
}
public init(id: Identifier, withType type: P4Type) {
self.type = type
super.init(id: id)
}
public override var description: String {
return "\(name)"
}
@@ -73,23 +82,61 @@ public class Variable: TypedIdentifier {
}
}
public typealias P4StructFieldIdentifier = TypedIdentifier
public struct P4StructFields: Sequence, CustomStringConvertible, Equatable {
public typealias Element = [P4StructFieldIdentifier].Iterator.Element
public typealias Iterator = [P4StructFieldIdentifier].Iterator
public func makeIterator() -> Iterator {
return self.fields.makeIterator()
}
let fields: [P4StructFieldIdentifier]
public init(_ fields: [P4StructFieldIdentifier]) {
self.fields = fields
}
public var description: String {
return self.fields.map { field in
field.name
}.joined(separator: ",")
}
public func get_field_type(_ field: Identifier) -> P4Type? {
if let found_field = self.fields.makeIterator().first(where: { current in
return current.name == field.name
}) {
return found_field.type
}
return .none
}
public func count() -> Int {
return self.fields.count
}
}
/// The type for a P4 struct
public struct P4Struct: P4Type {
public let name: String
// The type of the struct created is always anonymous.
public static func create() -> any P4Type {
return P4Struct()
public let name: Identifier
public let fields: P4StructFields
public init(withName name: Identifier, andFields fields: P4StructFields) {
self.name = name
self.fields = fields
}
public init(withName name: String) {
self.name = name
}
public init() {
self.name = ""
self.name = Identifier(name: "")
self.fields = P4StructFields([])
}
public var description: String {
return "Struct \(self.name)"
return "Struct \(self.name) with fields: \(self.fields)"
}
public func eq(rhs: P4Type) -> Bool {
@@ -101,21 +148,10 @@ public struct P4Struct: P4Type {
}
}
/// The field of a P4 struct
public struct P4StructField {
public let name: TypedIdentifier
public let type: P4Type
public init(withName name: TypedIdentifier, withType type: P4Type) {
self.name = name
self.type = type
}
}
/// An instance of a P4 struct
public class P4StructValue: P4Value {
public func type() -> any P4Type {
return P4Struct()
return self.stype
}
public func eq(rhs: any P4Value) -> Bool {
@@ -126,18 +162,37 @@ public class P4StructValue: P4Value {
return "Struct"
}
public let fields: [P4StructField]
public init(withFields fields: [P4StructField]) {
self.fields = fields
public let stype: P4Struct
public let values: [P4Value?]
public convenience init(withType type: P4Struct) {
self.init(withType: type, andInitializers: [])
}
public init(withType type: P4Struct, andInitializers initializers: [P4Value]) {
var values: [P4Value?] = Array(repeating: .none, count: type.fields.count())
for i in 0..<initializers.count {
values[i] = initializers[i]
}
self.values = values
self.stype = type
}
public func get(field: P4StructFieldIdentifier) -> P4Value? {
for field_idx in 0..<stype.fields.count() {
if stype.fields.fields[field_idx] == field {
return values[field_idx]
}
}
return .none
}
}
/// A P4 boolean type
public struct P4Boolean: P4Type {
public static func create() -> any P4Type {
return P4Boolean()
}
public init() {}
public var description: String {
return "Boolean"
}
@@ -174,9 +229,8 @@ public class P4BooleanValue: P4Value {
/// A P4 int type
public struct P4Int: P4Type {
public static func create() -> any P4Type {
return P4Int()
}
public init() {}
public var description: String {
return "Int"
}
@@ -216,10 +270,7 @@ public class P4IntValue: P4Value {
/// A P4 string type
public struct P4String: P4Type {
public static func create() -> any P4Type {
return P4String()
}
public init() {}
public var description: String {
return "String"
}
@@ -258,13 +309,20 @@ public class Packet {
/// A P4 array type
public struct P4Array: P4Type {
public static func create() -> any P4Type {
return P4Array()
public init(withValueType vtype: P4Type) {
self.vtype = vtype
}
let vtype: P4Type
public func value_type() -> P4Type {
return self.vtype
}
public var description: String {
return "Array"
}
public func eq(rhs: any P4Type) -> Bool {
return switch rhs {
case is P4Array: true
@@ -276,12 +334,14 @@ public struct P4Array: P4Type {
/// An instance of a P4 array
public class P4ArrayValue: P4Value {
public func type() -> any P4Type {
return P4Array()
return P4Array(withValueType: self.vtype)
}
let value: [EvaluatableExpression]
let vtype: P4Type
public init(withValue value: [EvaluatableExpression]) {
public init(withType type: P4Type, withValue value: [EvaluatableExpression]) {
self.vtype = type
self.value = value
}
+101 -3
View File
@@ -110,7 +110,7 @@ struct Expression {
let localElementsParsers: [CompilableExpression.Type] = [
P4BooleanValue.self, P4StringValue.self, P4IntValue.self, TypedIdentifier.self,
BinaryOperatorExpression.self, ArrayAccessExpression.self,
BinaryOperatorExpression.self, ArrayAccessExpression.self, FieldAccessExpression.self,
]
for le_parser in localElementsParsers {
@@ -307,7 +307,7 @@ extension BinaryOperatorExpression: CompilableExpression {
return .Ok(
BinaryOperatorExpression(
withEvaluator: ("Binary Equal", P4Boolean.create(), binary_equal_operator_evaluator),
withEvaluator: ("Binary Equal", P4Boolean(), binary_equal_operator_evaluator),
withLhs: left_hand_side, withRhs: right_hand_side))
}
}
@@ -370,12 +370,110 @@ extension ArrayAccessExpression: CompilableExpression {
return Result.Error(maybe_array_identifier.error()!)
}
let maybe_array_type = array_identifier.type()
guard let array_type = maybe_array_type as? P4Array else {
return Result.Error(
ErrorOnNode(
node: array_access_identifier_node,
withError: "\(array_identifier) does not name an array type")
)
}
let maybe_array_indexor = Expression.Compile(
node: array_access_indexor_node, withContext: context)
guard case Result.Ok(let array_indexor) = maybe_array_indexor else {
return Result.Error(maybe_array_indexor.error()!)
}
return .Ok(ArrayAccessExpression(withName: array_identifier, withIndexor: array_indexor))
return .Ok(
ArrayAccessExpression(
withName: array_identifier, withType: array_type, withIndexor: array_indexor))
}
}
extension FieldAccessExpression: CompilableExpression {
static func compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(any Common.EvaluatableExpression)?> {
let expression = node.child(at: 0)!
#SkipUnlessNodeType<Node, EvaluatableExpression?>(
node: expression, type: "fieldAccessExpression")
let field_access_expression_node = expression
var currentChildIdx = 0
var currentChildIdxSafe = 1
var currentChild: Node? = .none
// What is the "name" of the struct?
if field_access_expression_node.childCount < currentChildIdxSafe {
return Result.Error(
ErrorOnNode(node: node, withError: "Malformed field access expression"))
}
currentChild = expression.child(at: currentChildIdx)
#RequireNodeType<Node, EvaluatableExpression?>(
node: currentChild!, type: "expression",
nice_type_name: "struct identifier expression")
let struct_identifier_node = currentChild!
// Check for the .
currentChildIdx = currentChildIdx + 1
currentChildIdxSafe = currentChildIdxSafe + 1
if field_access_expression_node.childCount < currentChildIdxSafe {
return Result.Error(
ErrorOnNode(node: node, withError: "Missing . for field access expression")
)
}
// What is the field of the struct?
currentChildIdx = currentChildIdx + 1
currentChildIdxSafe = currentChildIdxSafe + 1
if field_access_expression_node.childCount < currentChildIdxSafe {
return Result.Error(
ErrorOnNode(node: node, withError: "Missing field name for field access expression")
)
}
currentChild = field_access_expression_node.child(at: currentChildIdx)
#RequireNodeType<Node, EvaluatableExpression?>(
node: currentChild!, type: "identifier",
nice_type_name: "field name")
let field_name_node = currentChild!
// Make sure that the identifier really identifies a struct.
let maybe_struct_identifier = Expression.Compile(
node: struct_identifier_node, withContext: context)
guard case Result.Ok(let struct_identifier) = maybe_struct_identifier else {
return Result.Error(maybe_struct_identifier.error()!)
}
guard let struct_type = struct_identifier.type() as? P4Struct else {
return .Error(
ErrorOnNode(
node: struct_identifier_node,
withError: "\(struct_identifier_node.text!) does not have struct type"))
}
let maybe_field_name = Identifier.Compile(
node: field_name_node, withContext: context)
guard case Result.Ok(let field_name) = maybe_field_name else {
return Result.Error(maybe_field_name.error()!)
}
// Make sure that the field is valid for the struct type.
let maybe_field_type = struct_type.fields.get_field_type(field_name)
guard let field_type = maybe_field_type else {
return .Error(
ErrorOnNode(
node: field_name_node,
withError: "\(field_name) is not a valid field for struct with type \(struct_type)"))
}
return .Ok(
FieldAccessExpression(
withStruct: struct_identifier,
withField: P4StructFieldIdentifier(id: field_name, withType: field_type)))
}
}
+4 -6
View File
@@ -199,14 +199,12 @@ extension VariableDeclarationStatement: CompilableStatement {
Error(withMessage: "Could not parse variable name"))
}
let maybe_parsed_rvalue = Expression.Compile(node: rvalue, withContext: context)
guard
case .Ok(let parsed_rvalue) = Expression.Compile(
node: rvalue, withContext: context)
case .Ok(let parsed_rvalue) = maybe_parsed_rvalue
else {
return Result.Error(
Error(
withMessage:
"Could not parse initial value expression in a variable declaration statement"))
return .Error(maybe_parsed_rvalue.error()!)
}
guard case .Ok(let declaration_p4_type) = Types.CompileBasicType(type: typeref.text!) else {
+3 -3
View File
@@ -24,19 +24,19 @@ import TreeSitterP4
extension P4Boolean: CompilableType {
public static func CompileType(type: String) -> Common.Result<(any Common.P4Type)?> {
return type == "bool" ? .Ok(P4Boolean.create()) : .Ok(.none)
return type == "bool" ? .Ok(P4Boolean()) : .Ok(.none)
}
}
extension P4Int: CompilableType {
public static func CompileType(type: String) -> Common.Result<(any Common.P4Type)?> {
return type == "int" ? .Ok(P4Int.create()) : .Ok(.none)
return type == "int" ? .Ok(P4Int()) : .Ok(.none)
}
}
extension P4String: CompilableType {
public static func CompileType(type: String) -> Common.Result<(any Common.P4Type)?> {
return type == "string" ? .Ok(P4String.create()) : .Ok(.none)
return type == "string" ? .Ok(P4String()) : .Ok(.none)
}
}
public struct Types {
+16 -1
View File
@@ -76,9 +76,24 @@ public struct BinaryOperatorExpression {
public struct ArrayAccessExpression {
public let indexor: EvaluatableExpression
public let name: EvaluatableExpression
public let type: P4Array
public init(withName name: EvaluatableExpression, withIndexor indexor: EvaluatableExpression) {
public init(
withName name: EvaluatableExpression, withType type: P4Array,
withIndexor indexor: EvaluatableExpression
) {
self.name = name
self.type = type
self.indexor = indexor
}
}
public struct FieldAccessExpression {
public let field: P4StructFieldIdentifier
public let strct: EvaluatableExpression
public init(withStruct strct: EvaluatableExpression, withField field: P4StructFieldIdentifier) {
self.strct = strct
self.field = field
}
}
+32 -2
View File
@@ -76,10 +76,16 @@ extension P4IntValue: EvaluatableExpression {
}
}
extension P4ArrayValue: EvaluatableExpression {
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
return .Ok(self)
}
}
// Variables are evaluatable because they can be looked up by identifiers.
extension TypedIdentifier: EvaluatableExpression {
public func type() -> any Common.P4Type {
return self.parsed_type
return self.type
}
public func evaluate(execution: Common.ProgramExecution) -> Result<P4Value> {
@@ -139,6 +145,30 @@ extension ArrayAccessExpression: EvaluatableExpression {
}
public func type() -> any Common.P4Type {
return P4Int.create()
return self.type.value_type()
}
}
extension FieldAccessExpression: EvaluatableExpression {
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
let maybe_struct = self.strct.evaluate(execution: execution)
guard case Result.Ok(let strct) = maybe_struct else {
return maybe_struct
}
guard let struct_strct = strct as? P4StructValue else {
return Result.Error(Error(withMessage: "\(strct) does not identify a struct"))
}
// TODO: Create a default value?
guard let value = struct_strct.get(field: self.field) else {
return .Error(Error(withMessage: "Missing value"))
}
return .Ok(value)
}
public func type() -> any Common.P4Type {
return self.field.type
}
}
+1 -1
View File
@@ -44,7 +44,7 @@ extension ConditionalStatement: EvaluatableStatement {
guard case .Ok(let evaluated_condition) = self.condition.evaluate(execution: execution) else {
return execution.setError(error: Error(withMessage: "Could not evaluate \(self.condition)"))
}
if !evaluated_condition.type().eq(rhs: P4Boolean.create()) {
if !evaluated_condition.type().eq(rhs: P4Boolean()) {
return execution.setError(error: Error(withMessage: "Condition expression is not a Boolean"))
}
if evaluated_condition.eq(rhs: P4BooleanValue.init(withValue: true)) {