From 7c660b2b0c2e83c5542e7954126fa104c2d64ae3 Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Mon, 4 May 2026 07:29:51 -0400 Subject: [PATCH] compiler, runtime: Refactor P4Type to P4QualifiedType Also, refer to the different pieces of the qualified type as qualifiers and not attributes. Signed-off-by: Will Hawkins --- Snippets/use-program-instanceswithtypes.swift | 4 +- Sources/Common/Call.swift | 6 +- Sources/Common/Compiler.swift | 4 +- Sources/Common/DataTypes.swift | 32 ++++---- Sources/Common/FFI.swift | 2 +- Sources/Common/P4Types.swift | 80 +++++++++---------- Sources/Common/Protocols.swift | 2 +- Sources/P4Compiler/Common.swift | 2 +- Sources/P4Compiler/Compiler.swift | 6 +- Sources/P4Compiler/Declarations.swift | 20 ++--- Sources/P4Compiler/Expression.swift | 32 ++++---- Sources/P4Compiler/Statement.swift | 2 +- Sources/P4Compiler/Types.swift | 4 +- Sources/P4Lang/Declarations.swift | 10 +-- Sources/P4Lang/Expressions.swift | 10 +-- Sources/P4Lang/Program.swift | 10 +-- Sources/P4Lang/Types.swift | 4 +- Sources/P4Runtime/Expressions.swift | 26 +++--- Sources/P4Runtime/Parser.swift | 8 +- Sources/P4Runtime/Statements.swift | 2 +- Tests/p4rseTests/ArrayTests.swift | 34 ++++---- .../BinaryOperatorTests/Struct.swift | 20 ++--- Tests/p4rseTests/ControlTests/Compile.swift | 12 +-- Tests/p4rseTests/ControlTests/Runtime.swift | 70 ++++++++-------- Tests/p4rseTests/ExternDeclarations.swift | 12 +-- Tests/p4rseTests/ParserCompilerTests.swift | 12 +-- Tests/p4rseTests/ScopeTests.swift | 20 ++--- Tests/p4rseTests/StructTests.swift | 76 +++++++++--------- Tests/p4rseTests/TypeTests.swift | 4 +- Tests/p4rseTests/ValueTypeParserTests.swift | 2 +- 30 files changed, 264 insertions(+), 264 deletions(-) diff --git a/Snippets/use-program-instanceswithtypes.swift b/Snippets/use-program-instanceswithtypes.swift index a494019..802538a 100644 --- a/Snippets/use-program-instanceswithtypes.swift +++ b/Snippets/use-program-instanceswithtypes.swift @@ -39,8 +39,8 @@ let p4_program_with_control_decl = """ // snippet.include -let flter = { (tipe: P4Type) -> Bool in - switch tipe.dataType(){ +let flter = { (tipe: P4QualifiedType) -> Bool in + switch tipe.baseType(){ case let c as Control: c.name == "simple" default: false } diff --git a/Sources/Common/Call.swift b/Sources/Common/Call.swift index f365200..706f877 100644 --- a/Sources/Common/Call.swift +++ b/Sources/Common/Call.swift @@ -21,10 +21,10 @@ public struct Parameter: CustomStringConvertible, Equatable { } public var name: Identifier - public var type: P4Type + public var type: P4QualifiedType public init( - identifier: Identifier, withType type: P4Type + identifier: Identifier, withType type: P4QualifiedType ) { self.name = identifier self.type = type @@ -46,7 +46,7 @@ public struct Parameter: CustomStringConvertible, Equatable { return false } } - return arg_type.dataType().eq(rhs: self.type.dataType()) + return arg_type.baseType().eq(rhs: self.type.baseType()) } } diff --git a/Sources/Common/Compiler.swift b/Sources/Common/Compiler.swift index c9f43ea..797b880 100644 --- a/Sources/Common/Compiler.swift +++ b/Sources/Common/Compiler.swift @@ -16,10 +16,10 @@ // along with this program. If not, see . /// A scope that resolves variable identifiers to their types. -public typealias VarTypeScope = Scope +public typealias VarTypeScope = Scope /// Scopes that resolve variable identifiers to their types. -public typealias VarTypeScopes = Scopes +public typealias VarTypeScopes = Scopes /// A scope that resolves type identifiers to their types. public typealias TypeTypeScope = Scope diff --git a/Sources/Common/DataTypes.swift b/Sources/Common/DataTypes.swift index 2844584..abb4a8c 100644 --- a/Sources/Common/DataTypes.swift +++ b/Sources/Common/DataTypes.swift @@ -50,14 +50,14 @@ public class Identifier: CustomStringConvertible, Comparable, Hashable { /// A P4 identifier public class TypedIdentifier: Identifier { - public var type: P4Type + public var type: P4QualifiedType - public init(name: String, withType type: P4Type) { + public init(name: String, withType type: P4QualifiedType) { self.type = type super.init(name: name) } - public init(id: Identifier, withType type: P4Type) { + public init(id: Identifier, withType type: P4QualifiedType) { self.type = type super.init(id: id) } @@ -107,7 +107,7 @@ public struct P4StructFields: Sequence, CustomStringConvertible, Equatable { }.joined(separator: ",") } - public func get_field_type(_ field: Identifier) -> P4Type? { + public func get_field_type(_ field: Identifier) -> P4QualifiedType? { if let found_field = self.fields.makeIterator().first(where: { current in return current.name == field.name }) { @@ -566,13 +566,13 @@ public class Packet { /// A P4 array type public struct P4Array: P4DataType { - public init(withValueType vtype: P4Type) { + public init(withValueType vtype: P4QualifiedType) { self.vtype = vtype } - let vtype: P4Type + let vtype: P4QualifiedType - public func value_type() -> P4Type { + public func value_type() -> P4QualifiedType { return self.vtype } @@ -599,9 +599,9 @@ public class P4ArrayValue: P4DataValue { } let value: [P4Value] - let vtype: P4Type + let vtype: P4QualifiedType - public init(withType type: P4Type, withValue value: [P4Value]) { + public init(withType type: P4QualifiedType, withValue value: [P4Value]) { self.vtype = type self.value = value } @@ -664,13 +664,13 @@ public class P4ArrayValue: P4DataValue { /// A P4 set type public struct P4Set: P4DataType { - public init(withSetType stype: P4Type) { + public init(withSetType stype: P4QualifiedType) { self.stype = stype } - let stype: P4Type + let stype: P4QualifiedType - public func set_type() -> P4Type { + public func set_type() -> P4QualifiedType { return self.stype } @@ -681,13 +681,13 @@ public struct P4Set: P4DataType { public func eq(rhs: any P4DataType) -> Bool { return switch rhs { // If rhs is a set type, then they are the same if the types in the set are the same. - case let srhs as P4Set: srhs.eq(rhs: self.stype.dataType()) + case let srhs as P4Set: srhs.eq(rhs: self.stype.baseType()) default: false } } public func def() -> P4DataValue { - return P4SetValue(withValue: P4Value(self.stype.dataType().def(), self.stype)) + return P4SetValue(withValue: P4Value(self.stype.baseType().def(), self.stype)) } } @@ -748,9 +748,9 @@ public class P4SetDefaultValue: P4DataValue { return P4Set(withSetType: self.stype) } - let stype: P4Type + let stype: P4QualifiedType - public init(withType type: P4Type) { + public init(withType type: P4QualifiedType) { self.stype = type } diff --git a/Sources/Common/FFI.swift b/Sources/Common/FFI.swift index 821c780..02c0f11 100644 --- a/Sources/Common/FFI.swift +++ b/Sources/Common/FFI.swift @@ -17,6 +17,6 @@ public protocol P4FFI { func execute(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) - func type() -> P4Type + func type() -> P4QualifiedType func parameters() -> ParameterList } diff --git a/Sources/Common/P4Types.swift b/Sources/Common/P4Types.swift index 9c7dc82..29babb9 100644 --- a/Sources/Common/P4Types.swift +++ b/Sources/Common/P4Types.swift @@ -52,27 +52,27 @@ public enum Direction: Equatable, CustomStringConvertible { } } -public enum P4TypeAttribute: Equatable { +public enum P4TypeQualifier: Equatable { case Direction(Direction) case Readonly // Not yet used -- here to keep Swift warnings at bay } -public struct P4TypeAttributes: CustomStringConvertible { - let _attributes: [P4TypeAttribute] +public struct P4TypeQualifiers: CustomStringConvertible { + let _qualifiers: [P4TypeQualifier] - public init(_ attributes: [P4TypeAttribute]) { - self._attributes = attributes + public init(_ qualifiers: [P4TypeQualifier]) { + self._qualifiers = qualifiers } public func direction() -> Direction? { - let result = _attributes.firstIndex { attribute in + let result = _qualifiers.firstIndex { attribute in return switch attribute { case .Direction(_): true default: false } } return result.flatMap { index in - return switch _attributes[index] { + return switch _qualifiers[index] { case .Direction(let d): d default: Optional.none } @@ -80,7 +80,7 @@ public struct P4TypeAttributes: CustomStringConvertible { } public func readOnly() -> Bool { - return _attributes.contains { attribute in + return _qualifiers.contains { attribute in return switch attribute { case .Readonly: true default: false @@ -88,45 +88,45 @@ public struct P4TypeAttributes: CustomStringConvertible { } } - public func update(removeAttribute attributeToRemove: P4TypeAttribute) -> P4TypeAttributes { - var new_attributes = self._attributes + public func update(removeAttribute attributeToRemove: P4TypeQualifier) -> P4TypeQualifiers { + var new_attributes = self._qualifiers new_attributes.removeAll { item in return item == attributeToRemove } - return P4TypeAttributes(new_attributes) + return P4TypeQualifiers(new_attributes) } - public func update(addAttribute attributeToAdd: P4TypeAttribute) -> P4TypeAttributes { - return P4TypeAttributes(self._attributes + [attributeToAdd]) + public func update(addAttribute attributeToAdd: P4TypeQualifier) -> P4TypeQualifiers { + return P4TypeQualifiers(self._qualifiers + [attributeToAdd]) } public var description: String { - return self._attributes.map { attribute in - return "\(attribute)" + return self._qualifiers.map { qualifier in + return "\(qualifier)" }.joined(separator: ",") } - public static func ReadOnly() -> P4TypeAttributes { - return P4TypeAttributes([P4TypeAttribute.Readonly]) + public static func ReadOnly() -> P4TypeQualifiers { + return P4TypeQualifiers([P4TypeQualifier.Readonly]) } } -public struct P4Type: CustomStringConvertible { - let _attributes: P4TypeAttributes - let _data_type: P4DataType +public struct P4QualifiedType: CustomStringConvertible { + let _attributes: P4TypeQualifiers + let base_type: P4DataType - public init(_ type: P4DataType, _ attributes: P4TypeAttributes = P4TypeAttributes([])) { + public init(_ base_type: P4DataType, _ attributes: P4TypeQualifiers = P4TypeQualifiers([])) { self._attributes = attributes - self._data_type = type + self.base_type = base_type } - public func update(removeAttribute attribute: P4TypeAttribute) -> P4Type { - return P4Type(self._data_type, self._attributes.update(removeAttribute: attribute)) + public func update(removeAttribute attribute: P4TypeQualifier) -> P4QualifiedType { + return P4QualifiedType(self.base_type, self._attributes.update(removeAttribute: attribute)) } - public func update(addAttribute attribute: P4TypeAttribute) -> P4Type { - return P4Type(self._data_type, self._attributes.update(addAttribute: attribute)) + public func update(addAttribute attribute: P4TypeQualifier) -> P4QualifiedType { + return P4QualifiedType(self.base_type, self._attributes.update(addAttribute: attribute)) } public func direction() -> Direction? { @@ -137,17 +137,17 @@ public struct P4Type: CustomStringConvertible { return self._attributes.readOnly() } - public func dataType() -> P4DataType { - return self._data_type + public func baseType() -> P4DataType { + return self.base_type } public func def() -> P4Value { - return P4Value(self._data_type.def(), self) + return P4Value(self.base_type.def(), self) } - public func eq(_ rhs: P4Type) -> Bool { + public func eq(_ rhs: P4QualifiedType) -> Bool { return self.direction() == rhs.direction() && self.readOnly() == self.readOnly() - && self.dataType().eq(rhs: rhs.dataType()) + && self.baseType().eq(rhs: rhs.baseType()) } public func assignable() -> TypeCheckResults { @@ -163,8 +163,8 @@ public struct P4Type: CustomStringConvertible { return TypeCheckResults.Ok } - public func assignableFromType(_ rhs: P4Type) -> TypeCheckResults { - if !self.dataType().eq(rhs: rhs.dataType()) { + public func assignableFromType(_ rhs: P4QualifiedType) -> TypeCheckResults { + if !self.baseType().eq(rhs: rhs.baseType()) { return TypeCheckResults.IncompatibleTypes } @@ -181,8 +181,8 @@ public struct P4Type: CustomStringConvertible { return TypeCheckResults.Ok } - public static func ReadOnly(_ type: P4DataType) -> P4Type { - return P4Type(type, P4TypeAttributes.ReadOnly()) + public static func ReadOnly(_ type: P4DataType) -> P4QualifiedType { + return P4QualifiedType(type, P4TypeQualifiers.ReadOnly()) } public var description: String { @@ -190,17 +190,17 @@ public struct P4Type: CustomStringConvertible { if !attributes_description.isEmpty { attributes_description += " " } - return "\(attributes_description)\(self._data_type)" + return "\(attributes_description)\(self.base_type)" } } public struct P4Value: CustomStringConvertible { let _value: P4DataValue - let _type: P4Type + let _type: P4QualifiedType - public init(_ value: P4DataValue, _ type: P4Type? = .none) { + public init(_ value: P4DataValue, _ type: P4QualifiedType? = .none) { self._value = value - self._type = type != nil ? type! : P4Type(value.type()) + self._type = type != nil ? type! : P4QualifiedType(value.type()) } public func update(withNewValue value: P4DataValue) -> Result { @@ -212,7 +212,7 @@ public struct P4Value: CustomStringConvertible { return "Value: \(self._value) of type \(self._type)" } - public func type() -> P4Type { + public func type() -> P4QualifiedType { return self._type } diff --git a/Sources/Common/Protocols.swift b/Sources/Common/Protocols.swift index 41a22b2..272d37b 100644 --- a/Sources/Common/Protocols.swift +++ b/Sources/Common/Protocols.swift @@ -21,7 +21,7 @@ public protocol EvaluatableExpression { /// - execution: The execution context in which to evaluate the expression /// - Returns: The value of expression func evaluate(execution: ProgramExecution) -> (Result, ProgramExecution) - func type() -> P4Type + func type() -> P4QualifiedType } public protocol EvaluatableStatement { diff --git a/Sources/P4Compiler/Common.swift b/Sources/P4Compiler/Common.swift index 707525f..b6cc1c5 100644 --- a/Sources/P4Compiler/Common.swift +++ b/Sources/P4Compiler/Common.swift @@ -229,7 +229,7 @@ extension Parameter: Compilable { Parameter( identifier: parameter_name, withType: direction != nil - ? parameter_type.update(addAttribute: P4TypeAttribute.Direction(direction!)) + ? parameter_type.update(addAttribute: P4TypeQualifier.Direction(direction!)) : parameter_type), context )) diff --git a/Sources/P4Compiler/Compiler.swift b/Sources/P4Compiler/Compiler.swift index 8afaf30..1321cf5 100644 --- a/Sources/P4Compiler/Compiler.swift +++ b/Sources/P4Compiler/Compiler.swift @@ -54,7 +54,7 @@ public struct CompilerContext { let types: TypeTypeScopes let externs: TypeTypeScopes let ffis: [P4FFI] - let expected_type: P4Type? + let expected_type: P4QualifiedType? let extern_context: Bool public init() { @@ -77,7 +77,7 @@ public struct CompilerContext { public init( withInstances _instances: VarTypeScopes, withTypes _types: TypeTypeScopes, - withExpectation expectation: P4Type?, withExtern extern: Bool, + withExpectation expectation: P4QualifiedType?, withExtern extern: Bool, withExterns externs: TypeTypeScopes, withFFIs foreigns: [P4FFI] ) { instances = _instances @@ -118,7 +118,7 @@ public struct CompilerContext { /// /// - Parameter expectation: a ``P4Type?`` to (re)set the type the compiler is expecting. /// - Returns: A new compiler context based on the current but new expected type. - public func update(newExpectation expectation: P4Type?) -> CompilerContext { + public func update(newExpectation expectation: P4QualifiedType?) -> CompilerContext { return CompilerContext( withInstances: self.instances, withTypes: self.types, withExpectation: expectation, withExtern: self.extern_context, withExterns: self.externs, withFFIs: self.ffis) diff --git a/Sources/P4Compiler/Declarations.swift b/Sources/P4Compiler/Declarations.swift index f8d604f..25e9799 100644 --- a/Sources/P4Compiler/Declarations.swift +++ b/Sources/P4Compiler/Declarations.swift @@ -128,7 +128,7 @@ extension FunctionDeclaration: CompilableDeclaration { let function_declaration = Declaration( TypedIdentifier( id: function_name, - withType: P4Type( + withType: P4QualifiedType( FunctionDeclaration( named: function_name, ofType: function_type, withParameters: function_parameters, withBody: function_body)))) @@ -144,7 +144,7 @@ extension FunctionDeclaration: CompilableDeclaration { ? context : context.update( newTypes: context.types.declare( - identifier: function_name, withValue: function_declaration.identifier.type.dataType()) + identifier: function_name, withValue: function_declaration.identifier.type.baseType()) ) )) } @@ -189,7 +189,7 @@ extension P4Struct: CompilableDeclaration { let struc = Declaration( TypedIdentifier( id: struct_identifier, - withType: P4Type(P4Struct(withName: struct_identifier, andFields: P4StructFields([]))))) + withType: P4QualifiedType(P4Struct(withName: struct_identifier, andFields: P4StructFields([]))))) return Result.Ok( ( struc, @@ -197,7 +197,7 @@ extension P4Struct: CompilableDeclaration { ? context : context.update( newTypes: context.types.declare( - identifier: struct_identifier, withValue: struc.identifier.type.dataType())) + identifier: struct_identifier, withValue: struc.identifier.type.baseType())) )) } @@ -234,7 +234,7 @@ extension P4Struct: CompilableDeclaration { let declared_struct = Declaration( TypedIdentifier( id: struct_identifier, - withType: P4Type( + withType: P4QualifiedType( P4Struct( withName: struct_identifier, andFields: P4StructFields(parsed_fields))))) return .Ok( @@ -244,7 +244,7 @@ extension P4Struct: CompilableDeclaration { ? current_context : current_context.update( newTypes: current_context.types.declare( - identifier: struct_identifier, withValue: declared_struct.identifier.type.dataType())) + identifier: struct_identifier, withValue: declared_struct.identifier.type.baseType())) )) } } @@ -366,7 +366,7 @@ extension P4Lang.Parser: CompilableDeclaration { { case Result.Ok((let parser, let updated_context)): let parser_declaration = Declaration( - TypedIdentifier(id: parser.name, withType: P4Type(parser))) + TypedIdentifier(id: parser.name, withType: P4QualifiedType(parser))) // Create a new context with the name of the parser that was just compiled in scope. return .Ok( ( @@ -518,7 +518,7 @@ extension Control: CompilableDeclaration { Declaration( TypedIdentifier( id: control_name, - withType: P4Type( + withType: P4QualifiedType( Control( named: control_name, withParameters: control_parameters, withTable: tables[0], withActions: Actions(withActions: actions), withApply: apply)))) @@ -753,7 +753,7 @@ extension TableActionsProperty: Compilable { switch context.types.lookup(identifier: listed_action) { case .Ok(let maybe_action): if maybe_action.eq(rhs: Action()) { - return .Ok(TypedIdentifier(id: listed_action, withType: P4Type(maybe_action))) + return .Ok(TypedIdentifier(id: listed_action, withType: P4QualifiedType(maybe_action))) } return .Error( ErrorOnNode(node: node, withError: "\(listed_action) does not name an action")) @@ -923,7 +923,7 @@ extension ExternDeclaration: CompilableDeclaration { // with the matching "stuff". let found_ffi = context.ffis.first { ffi in - ffi.type().dataType().eq(rhs: declared.identifier.type.dataType()) + ffi.type().baseType().eq(rhs: declared.identifier.type.baseType()) } guard let found_ffi = found_ffi else { diff --git a/Sources/P4Compiler/Expression.swift b/Sources/P4Compiler/Expression.swift index c0160f5..625a8f9 100644 --- a/Sources/P4Compiler/Expression.swift +++ b/Sources/P4Compiler/Expression.swift @@ -392,49 +392,49 @@ extension BinaryOperatorExpression: CompilableExpression { return Result.Error(maybe_right_hand_side.error()!) } - let evaluators: [String: (String, P4Type, BinaryOperatorChecker?, BinaryOperatorEvaluator)] = [ + let evaluators: [String: (String, P4QualifiedType, BinaryOperatorChecker?, BinaryOperatorEvaluator)] = [ "binaryEqualOperatorExpression": ( - "Binary Equal", P4Type(P4Boolean()), Optional.none, + "Binary Equal", P4QualifiedType(P4Boolean()), Optional.none, binary_equal_operator_evaluator ), "binaryLessThanOperatorExpression": ( - "Binary Less Than", P4Type(P4Boolean()), Optional.none, + "Binary Less Than", P4QualifiedType(P4Boolean()), Optional.none, binary_lt_operator_evaluator ), "binaryLessThanEqualOperatorExpression": ( - "Binary Less Than Or Equal", P4Type(P4Boolean()), Optional.none, + "Binary Less Than Or Equal", P4QualifiedType(P4Boolean()), Optional.none, binary_lte_operator_evaluator ), "binaryGreaterThanOperatorExpression": ( - "Binary Greater Than", P4Type(P4Boolean()), Optional.none, + "Binary Greater Than", P4QualifiedType(P4Boolean()), Optional.none, binary_gt_operator_evaluator ), "binaryGreaterThanEqualOperatorExpression": ( - "Binary Greater Than Or Equal", P4Type(P4Boolean()), Optional.none, + "Binary Greater Than Or Equal", P4QualifiedType(P4Boolean()), Optional.none, binary_gte_operator_evaluator ), "binaryAndOperatorExpression": ( - "Binary Or", P4Type(P4Boolean()), binary_and_or_operator_checker, + "Binary Or", P4QualifiedType(P4Boolean()), binary_and_or_operator_checker, binary_and_operator_evaluator ), "binaryOrOperatorExpression": ( - "Binary And", P4Type(P4Boolean()), binary_and_or_operator_checker, + "Binary And", P4QualifiedType(P4Boolean()), binary_and_or_operator_checker, binary_or_operator_evaluator ), "binaryAddOperatorExpression": ( - "Binary Add", P4Type(P4Int()), binary_int_math_operator_checker, + "Binary Add", P4QualifiedType(P4Int()), binary_int_math_operator_checker, binary_add_operator_evaluator ), "binarySubtractOperatorExpression": ( - "Binary Subtract", P4Type(P4Int()), binary_int_math_operator_checker, + "Binary Subtract", P4QualifiedType(P4Int()), binary_int_math_operator_checker, binary_subtract_operator_evaluator ), "binaryMultiplyOperatorExpression": ( - "Binary Multiply", P4Type(P4Int()), binary_int_math_operator_checker, + "Binary Multiply", P4QualifiedType(P4Int()), binary_int_math_operator_checker, binary_multiply_operator_evaluator ), "binaryDivideOperatorExpression": ( - "Binary Divide", P4Type(P4Int()), binary_int_math_operator_checker, + "Binary Divide", P4QualifiedType(P4Int()), binary_int_math_operator_checker, binary_divide_operator_evaluator ), ] @@ -510,7 +510,7 @@ extension ArrayAccessExpression: CompilableExpression { } let maybe_array_type = array_identifier.type() - guard let array_type = maybe_array_type.dataType() as? P4Array else { + guard let array_type = maybe_array_type.baseType() as? P4Array else { return Result.Error( ErrorOnNode( node: array_access_identifier_node, @@ -581,7 +581,7 @@ extension FieldAccessExpression: CompilableExpression { 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().dataType() as? P4Struct else { + guard let struct_type = struct_identifier.type().baseType() as? P4Struct else { return .Error( ErrorOnNode( node: struct_identifier_node, @@ -690,7 +690,7 @@ extension FunctionCall: CompilableExpression { switch context.externs.lookup(identifier: callee_name) { case .Ok(let callee as Declaration): // Now, make sure that it is a function declaration! - switch callee.identifier.type.dataType() { + switch callee.identifier.type.baseType() { case is FunctionDeclaration: Result.Ok((.none, callee)) default: .Error(ErrorOnNode(node: current_node!, withError: "\(callee_name) is not a function")) @@ -725,7 +725,7 @@ extension FunctionCall: CompilableExpression { switch callee { case (.some(let callee), .none): Optional.some(callee.params) case (.none, .some(let callee)): - Optional.some((callee.ffi!.type().dataType() as! FunctionDeclaration).params) + Optional.some((callee.ffi!.type().baseType() as! FunctionDeclaration).params) default: Optional.none } diff --git a/Sources/P4Compiler/Statement.swift b/Sources/P4Compiler/Statement.swift index 5be841e..87c42a1 100644 --- a/Sources/P4Compiler/Statement.swift +++ b/Sources/P4Compiler/Statement.swift @@ -312,7 +312,7 @@ extension ReturnStatement: CompilableStatement { return switch Expression.Compile(node: expression_node, withContext: context) { case .Ok(let result): - if result.type().dataType().eq(rhs: context.expected_type!.dataType()) { + if result.type().baseType().eq(rhs: context.expected_type!.baseType()) { .Ok((ReturnStatement(result), context)) } else { .Error( diff --git a/Sources/P4Compiler/Types.swift b/Sources/P4Compiler/Types.swift index aec0c27..685ae72 100644 --- a/Sources/P4Compiler/Types.swift +++ b/Sources/P4Compiler/Types.swift @@ -68,13 +68,13 @@ extension P4Struct: CompilableType { public struct Types { static func CompileType( type: SwiftTreeSitter.Node, withContext context: CompilerContext - ) -> Result { + ) -> Result { let type_parsers: [CompilableType.Type] = [ P4Boolean.self, P4Int.self, P4String.self, P4Struct.self, ] for type_parser in type_parsers { switch type_parser.CompileType(type: type, withContext: context) { - case .Ok(.some(let type)): return .Ok(P4Type(type)) + case .Ok(.some(let type)): return .Ok(P4QualifiedType(type)) case .Ok(.none): continue case .Error(let e): return .Error(e) } diff --git a/Sources/P4Lang/Declarations.swift b/Sources/P4Lang/Declarations.swift index ef01350..be2294f 100644 --- a/Sources/P4Lang/Declarations.swift +++ b/Sources/P4Lang/Declarations.swift @@ -37,7 +37,7 @@ public struct Declaration: P4DataType { public func eq(rhs: any Common.P4DataType) -> Bool { return switch rhs { case let rrhs as Declaration: - self.identifier.type.dataType().eq(rhs: rrhs.identifier.type.dataType()) + self.identifier.type.baseType().eq(rhs: rrhs.identifier.type.baseType()) && self.extern == rrhs.extern default: false } @@ -45,7 +45,7 @@ public struct Declaration: P4DataType { public func def() -> any Common.P4DataValue { /// TODO: Is a default of the extern'd type the right way to go? - return self.identifier.type.dataType().def() + return self.identifier.type.baseType().def() } public func type() -> any Common.P4DataType { @@ -109,7 +109,7 @@ public struct FunctionDeclaration: P4DataType, P4DataValue { public func def() -> any Common.P4DataValue { return FunctionDeclaration( - named: Identifier(name: ""), ofType: P4Type(P4Boolean()), + named: Identifier(name: ""), ofType: P4QualifiedType(P4Boolean()), withParameters: ParameterList([]), withBody: .none) } @@ -121,10 +121,10 @@ public struct FunctionDeclaration: P4DataType, P4DataValue { public var body: BlockStatement? public var params: ParameterList public var name: Identifier - public var tipe: P4Type + public var tipe: P4QualifiedType public init( - named name: Identifier, ofType type: P4Type, withParameters parameters: ParameterList, + named name: Identifier, ofType type: P4QualifiedType, withParameters parameters: ParameterList, withBody body: BlockStatement? ) { self.name = name diff --git a/Sources/P4Lang/Expressions.swift b/Sources/P4Lang/Expressions.swift index 6029037..9d1611d 100644 --- a/Sources/P4Lang/Expressions.swift +++ b/Sources/P4Lang/Expressions.swift @@ -24,8 +24,8 @@ public struct KeysetExpression { self.key = key } - public func compatible(type: P4Type) -> Result<()> { - if let key_type = self.key.type().dataType() as? P4Set { + public func compatible(type: P4QualifiedType) -> Result<()> { + if let key_type = self.key.type().baseType() as? P4Set { if !key_type.set_type().eq(type) { return .Error( Error( @@ -84,7 +84,7 @@ public struct SelectExpression { } } -public typealias NamedBinaryOperatorEvaluator = (String, P4Type, (P4Value, P4Value) -> P4DataValue) +public typealias NamedBinaryOperatorEvaluator = (String, P4QualifiedType, (P4Value, P4Value) -> P4DataValue) public typealias BinaryOperatorEvaluator = (P4Value, P4Value) -> P4DataValue public struct BinaryOperatorExpression { @@ -135,13 +135,13 @@ public struct FunctionCall { public init(_ callee: FunctionDeclaration, withArguments arguments: ArgumentList) { self.callee = (callee, .none) self.arguments = arguments - self.return_type = callee.tipe.dataType() + self.return_type = callee.tipe.baseType() } public init(_ callee: P4FFI, withArguments arguments: ArgumentList) { self.callee = (.none, callee) self.arguments = arguments /// ASSUME: That the FFI has been checked and the type is always a function declaration. - self.return_type = (callee.type().dataType() as! FunctionDeclaration).tipe.dataType() + self.return_type = (callee.type().baseType() as! FunctionDeclaration).tipe.baseType() } } diff --git a/Sources/P4Lang/Program.swift b/Sources/P4Lang/Program.swift index aaa4072..6d78a23 100644 --- a/Sources/P4Lang/Program.swift +++ b/Sources/P4Lang/Program.swift @@ -28,15 +28,15 @@ public struct ExpressionStatement { public struct Program { public var types: [P4DataType] = Array() public var externs: [P4DataType] = Array() - public var instances: [P4Type] = Array() + public var instances: [P4QualifiedType] = Array() /// Type of closure for filtering results from ``Program/InstancesWithTypes(_:)`` - public typealias TypeFilter = (P4Type) -> Bool + public typealias TypeFilter = (P4QualifiedType) -> Bool /// Type of closure for filtering results from ``Program/TypesWithTypes(_:)`` public typealias DataTypeFilter = (P4DataType) -> Bool /// Retrieve global instances in the compiled P4 program. - public func InstancesWithTypes() -> [P4Type] { + public func InstancesWithTypes() -> [P4QualifiedType] { return self.instances } @@ -52,7 +52,7 @@ public struct Program { /// /// @Snippet(path: "use-program-instanceswithtypes", slice: "include") /// - public func InstancesWithTypes(_ filter: TypeFilter) -> [P4Type] { + public func InstancesWithTypes(_ filter: TypeFilter) -> [P4QualifiedType] { return self.instances.filter { instance in filter(instance) } @@ -113,7 +113,7 @@ public struct Program { public func find_parser(withName name: Identifier) -> Result { for instance in self.instances { - guard let parser = instance.dataType() as? Parser else { + guard let parser = instance.baseType() as? Parser else { continue } if parser.name == name { diff --git a/Sources/P4Lang/Types.swift b/Sources/P4Lang/Types.swift index 15ce50a..d4ce6b3 100644 --- a/Sources/P4Lang/Types.swift +++ b/Sources/P4Lang/Types.swift @@ -19,9 +19,9 @@ import Common public struct AttributedP4Type { public let type: P4DataType - public let attributes: P4Type + public let attributes: P4QualifiedType - public init(_ type: P4DataType, _ attributes: P4Type) { + public init(_ type: P4DataType, _ attributes: P4QualifiedType) { self.type = type self.attributes = attributes } diff --git a/Sources/P4Runtime/Expressions.swift b/Sources/P4Runtime/Expressions.swift index f899729..8909ba9 100644 --- a/Sources/P4Runtime/Expressions.swift +++ b/Sources/P4Runtime/Expressions.swift @@ -23,8 +23,8 @@ extension SelectCaseExpression: EvaluatableExpression { return (execution.scopes.lookup(identifier: next_state_identifier), execution) } - public func type() -> P4Type { - return P4Type(ParserState()) + public func type() -> P4QualifiedType { + return P4QualifiedType(ParserState()) } } @@ -49,14 +49,14 @@ extension SelectExpression: EvaluatableExpression { } } - public func type() -> P4Type { - return P4Type(ParserState()) + public func type() -> P4QualifiedType { + return P4QualifiedType(ParserState()) } } // Variables are evaluatable because they can be looked up by identifiers. extension TypedIdentifier: EvaluatableExpression { - public func type() -> P4Type { + public func type() -> P4QualifiedType { return self.type } @@ -192,7 +192,7 @@ public func binary_and_or_operator_checker( left: EvaluatableExpression, right: EvaluatableExpression ) -> Result<()> { // Check that both are Boolean-typed things! - if !(left.type().dataType().eq(rhs: P4Boolean()) && right.type().dataType().eq(rhs: P4Boolean())) + if !(left.type().baseType().eq(rhs: P4Boolean()) && right.type().baseType().eq(rhs: P4Boolean())) { return .Error(Error(withMessage: "And/Or on operands with non-bool type is not allowed")) } @@ -203,7 +203,7 @@ public func binary_int_math_operator_checker( left: EvaluatableExpression, right: EvaluatableExpression ) -> Result<()> { // Check that both are int-typed things! - if !(left.type().dataType().eq(rhs: P4Int()) && right.type().dataType().eq(rhs: P4Int())) { + if !(left.type().baseType().eq(rhs: P4Int()) && right.type().baseType().eq(rhs: P4Int())) { return .Error( Error(withMessage: "Mathematical operation on operands with non-int type is not allowed")) } @@ -230,7 +230,7 @@ extension BinaryOperatorExpression: EvaluatableExpression { return (.Ok(P4Value(self.evaluator.2(evaluated_left, evaluated_right))), updated_execution) } - public func type() -> P4Type { + public func type() -> P4QualifiedType { return self.evaluator.1 } } @@ -264,7 +264,7 @@ extension ArrayAccessExpression: EvaluatableExpression { return (.Ok(accessed), updated_execution) } - public func type() -> P4Type { + public func type() -> P4QualifiedType { return self.type.value_type() } } @@ -375,7 +375,7 @@ extension FieldAccessExpression: EvaluatableExpression { return (.Ok(value), updated_execution) } - public func type() -> P4Type { + public func type() -> P4QualifiedType { return self.field.type } } @@ -467,7 +467,7 @@ extension KeysetExpression: EvaluatableExpression { return execution.evaluator.EvaluateExpression(self.key, inExecution: execution) } - public func type() -> P4Type { + public func type() -> P4QualifiedType { return self.key.type() } } @@ -524,8 +524,8 @@ extension FunctionCall: EvaluatableExpression { inExecution: execution) } - public func type() -> P4Type { - return P4Type(self.return_type) + public func type() -> P4QualifiedType { + return P4QualifiedType(self.return_type) } } diff --git a/Sources/P4Runtime/Parser.swift b/Sources/P4Runtime/Parser.swift index 281bd0f..9dbc763 100644 --- a/Sources/P4Runtime/Parser.swift +++ b/Sources/P4Runtime/Parser.swift @@ -62,7 +62,7 @@ extension ParserStateDirectTransition: EvaluatableParserState { let res = program.scopes.lookup(identifier: get_next_state()) if case .Ok(let value) = res { - if value.type().dataType().eq(rhs: self) { + if value.type().baseType().eq(rhs: self) { return (value.dataValue() as! EvaluatableParserState, program.exit_scope()) } } @@ -119,7 +119,7 @@ extension ParserStateSelectTransition: EvaluatableParserState { //switch self.selectExpression.evaluate(execution: program) { switch program.evaluator.EvaluateExpression(self.selectExpression, inExecution: program) { case (.Ok(let value), let program): - if value.type().dataType().eq(rhs: self) { + if value.type().baseType().eq(rhs: self) { return (value.dataValue() as! EvaluatableParserState, program.exit_scope()) } else { return ( @@ -150,10 +150,10 @@ extension Parser: LibraryCallable { execution = execution.declare( identifier: AsInstantiatedParserState(accept.state()).state, - withValue: P4Value(accept, P4Type.ReadOnly(accept.type()))) + withValue: P4Value(accept, P4QualifiedType.ReadOnly(accept.type()))) execution = execution.declare( identifier: AsInstantiatedParserState(reject.state()).state, - withValue: P4Value(reject, P4Type.ReadOnly(reject.type()))) + withValue: P4Value(reject, P4QualifiedType.ReadOnly(reject.type()))) // Add initial values to the global scope for (name, value) in execution.getGlobalValues() { diff --git a/Sources/P4Runtime/Statements.swift b/Sources/P4Runtime/Statements.swift index 4749346..6063c33 100644 --- a/Sources/P4Runtime/Statements.swift +++ b/Sources/P4Runtime/Statements.swift @@ -69,7 +69,7 @@ extension ConditionalStatement: EvaluatableStatement { ) } - if !evaluated_condition.type().dataType().eq(rhs: P4Boolean()) { + if !evaluated_condition.type().baseType().eq(rhs: P4Boolean()) { return ( ControlFlow.Error, execution.setError(error: Error(withMessage: "Condition expression is not a Boolean")) diff --git a/Tests/p4rseTests/ArrayTests.swift b/Tests/p4rseTests/ArrayTests.swift index 1790d61..45ac2ff 100644 --- a/Tests/p4rseTests/ArrayTests.swift +++ b/Tests/p4rseTests/ArrayTests.swift @@ -40,11 +40,11 @@ import TreeSitterP4 }; """ var test_declarations = VarTypeScopes().enter() - test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int())))) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int())))) var test_values = VarValueScopes().enter() test_values = test_values.declare( identifier: Identifier(name: "ta"), - withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [ + withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [ P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)) ]))) let program = try #UseOkResult( @@ -68,7 +68,7 @@ import TreeSitterP4 }; """ var test_declarations = VarTypeScopes().enter() - test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Int())) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Int())) #expect( #RequireErrorResult( Error( @@ -91,12 +91,12 @@ import TreeSitterP4 }; """ var test_declarations = VarTypeScopes().enter() - test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int())))) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int())))) var test_values = VarValueScopes().enter() test_values = test_values.declare( identifier: Identifier(name: "ta"), - withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [ + withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [ P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)) ]))) let program = try #UseOkResult( @@ -119,11 +119,11 @@ import TreeSitterP4 }; """ var test_declarations = VarTypeScopes().enter() - test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int())))) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int())))) var test_values = VarValueScopes().enter() test_values = test_values.declare( identifier: Identifier(name: "ta"), - withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [ + withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [ P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)), ]))) let program = try #UseOkResult( @@ -146,11 +146,11 @@ import TreeSitterP4 }; """ var test_declarations = VarTypeScopes().enter() - test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int())))) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int())))) var test_values = VarValueScopes().enter() test_values = test_values.declare( identifier: Identifier(name: "ta"), - withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [ + withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [ P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)), ]))) let program = try #UseOkResult( @@ -174,16 +174,16 @@ import TreeSitterP4 }; """ var test_declarations = VarTypeScopes().enter() - test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Array(withValueType: P4Type(P4Int())))))) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int())))))) var test_values = VarValueScopes().enter() let nested = P4Value(P4ArrayValue( - withType: P4Type(P4Int()), + withType: P4QualifiedType(P4Int()), withValue: [P4Value(P4IntValue(withValue: 5)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))])) test_values = test_values.declare( identifier: Identifier(name: "ta"), - withValue: P4Value(P4ArrayValue(withType: P4Type(P4Array(withValueType: P4Type(P4Int()))), withValue: [nested]))) + withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), withValue: [nested]))) let program = try #UseOkResult( Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) @@ -207,11 +207,11 @@ import TreeSitterP4 }; """ var test_declarations = VarTypeScopes().enter() - test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int())))) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int())))) var test_values = VarValueScopes().enter() test_values = test_values.declare( identifier: Identifier(name: "ta"), - withValue: P4Value(P4ArrayValue(withType: P4Type(P4Int()), withValue: [ + withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Int()), withValue: [ P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3)), ]))) let program = try #UseOkResult( @@ -236,16 +236,16 @@ import TreeSitterP4 }; """ var test_declarations = VarTypeScopes().enter() - test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Array(withValueType: P4Type(P4Int())))))) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int())))))) var test_values = VarValueScopes().enter() let nested = P4Value(P4ArrayValue( - withType: P4Type(P4Int()), + withType: P4QualifiedType(P4Int()), withValue: [P4Value(P4IntValue(withValue: 1)), P4Value(P4IntValue(withValue: 2)), P4Value(P4IntValue(withValue: 3))])) test_values = test_values.declare( identifier: Identifier(name: "ta"), - withValue: P4Value(P4ArrayValue(withType: P4Type(P4Array(withValueType: P4Type(P4Int()))), withValue: [nested]))) + withValue: P4Value(P4ArrayValue(withType: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), withValue: [nested]))) let program = try #UseOkResult( Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) diff --git a/Tests/p4rseTests/BinaryOperatorTests/Struct.swift b/Tests/p4rseTests/BinaryOperatorTests/Struct.swift index e7c12a6..4dcf9b0 100644 --- a/Tests/p4rseTests/BinaryOperatorTests/Struct.swift +++ b/Tests/p4rseTests/BinaryOperatorTests/Struct.swift @@ -42,8 +42,8 @@ import TreeSitterP4 """ let test_declarations = VarTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) var test_types = TypeTypeScopes().enter() let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) @@ -75,8 +75,8 @@ import TreeSitterP4 """ let test_declarations = VarTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) var test_types = TypeTypeScopes().enter() let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) @@ -109,8 +109,8 @@ import TreeSitterP4 """ let test_declarations = VarTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) var test_types = TypeTypeScopes().enter() let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) @@ -146,8 +146,8 @@ import TreeSitterP4 """ let test_declarations = VarTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) var test_types = TypeTypeScopes().enter() let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) @@ -183,8 +183,8 @@ import TreeSitterP4 """ let test_declarations = VarTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) var test_types = TypeTypeScopes().enter() let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) diff --git a/Tests/p4rseTests/ControlTests/Compile.swift b/Tests/p4rseTests/ControlTests/Compile.swift index 140d3be..713aeef 100644 --- a/Tests/p4rseTests/ControlTests/Compile.swift +++ b/Tests/p4rseTests/ControlTests/Compile.swift @@ -41,8 +41,8 @@ import P4Lang } }; """ - let x = { (tipe: P4Type) -> Bool in - switch tipe.dataType() { + let x = { (tipe: P4QualifiedType) -> Bool in + switch tipe.baseType() { case let c as Control: c.name == "simple" default: false } @@ -79,8 +79,8 @@ import P4Lang }; """ - let filter = { (tipe: P4Type) -> Bool in - switch tipe.dataType() { + let filter = { (tipe: P4QualifiedType) -> Bool in + switch tipe.baseType() { case let c as Control: c.name == "simple" || c.name == "complex" default: false } @@ -106,8 +106,8 @@ import P4Lang } }; """ - let x = { (tipe: P4Type) -> Bool in - switch tipe.dataType() { + let x = { (tipe: P4QualifiedType) -> Bool in + switch tipe.baseType() { case let c as Control: c.name == "simple" default: false } diff --git a/Tests/p4rseTests/ControlTests/Runtime.swift b/Tests/p4rseTests/ControlTests/Runtime.swift index d861005..2b9afcb 100644 --- a/Tests/p4rseTests/ControlTests/Runtime.swift +++ b/Tests/p4rseTests/ControlTests/Runtime.swift @@ -53,24 +53,24 @@ import TreeSitterP4 let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) // Pull the control out of the compiled program. - let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in - switch tipe.dataType() { + let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in + switch tipe.baseType() { case let c as Control: c.name == "simple" default: false } } - var control = ((controls[0].dataType() as P4DataType) as! Control) + var control = ((controls[0].baseType() as P4DataType) as! Control) // Add entries to the table. control = control.updateTable( addEntry: ( P4Value(P4BooleanValue(withValue: true)), - TypedIdentifier(name: "a", withType: P4Type(Action())) + TypedIdentifier(name: "a", withType: P4QualifiedType(Action())) ) ).updateTable( addEntry: ( P4Value(P4BooleanValue(withValue: false)), - TypedIdentifier(name: "b", withType: P4Type(Action())) + TypedIdentifier(name: "b", withType: P4QualifiedType(Action())) )) // Set a variable in the global scope for the inout first parameter. @@ -79,14 +79,14 @@ import TreeSitterP4 identifier: Identifier(name: "result_arg"), withValue: P4Value( P4IntValue(withValue: 0), - P4Type(P4Int()))) + P4QualifiedType(P4Int()))) let runtime = try #UseOkResult( P4Runtime.Runtime.create(control: control, withGlobalValues: global_values)) let (hit_miss, updated_execution) = try #UseOkResult(runtime.run( withArguments: ArgumentList([ - Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0), + Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0), Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 1), ]))) @@ -124,24 +124,24 @@ import TreeSitterP4 let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) // Pull the control out of the compiled program. - let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in - switch tipe.dataType() { + let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in + switch tipe.baseType() { case let c as Control: c.name == "simple" default: false } } - var control = ((controls[0].dataType() as P4DataType) as! Control) + var control = ((controls[0].baseType() as P4DataType) as! Control) // Add entries to the table. control = control.updateTable( addEntry: ( P4Value(P4BooleanValue(withValue: true)), - TypedIdentifier(name: "a", withType: P4Type(Action())) + TypedIdentifier(name: "a", withType: P4QualifiedType(Action())) ) ).updateTable( addEntry: ( P4Value(P4BooleanValue(withValue: false)), - TypedIdentifier(name: "b", withType: P4Type(Action())) + TypedIdentifier(name: "b", withType: P4QualifiedType(Action())) )) // Set a variable in the global scope for the inout first parameter. @@ -150,14 +150,14 @@ import TreeSitterP4 identifier: Identifier(name: "result_arg"), withValue: P4Value( P4IntValue(withValue: 0), - P4Type(P4Int()))) + P4QualifiedType(P4Int()))) let runtime = try #UseOkResult( P4Runtime.Runtime.create(control: control, withGlobalValues: global_values)) let (hit_miss, updated_execution) = try #UseOkResult(runtime.run( withArguments: ArgumentList([ - Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0), + Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0), Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1), ]))) @@ -195,24 +195,24 @@ import TreeSitterP4 let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) // Pull the control out of the compiled program. - let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in - switch tipe.dataType() { + let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in + switch tipe.baseType() { case let c as Control: c.name == "simple" default: false } } - var control = ((controls[0].dataType() as P4DataType) as! Control) + var control = ((controls[0].baseType() as P4DataType) as! Control) // Add entries to the table. control = control.updateTable( addEntry: ( P4Value(P4IntValue(withValue: 5)), - TypedIdentifier(name: "a", withType: P4Type(Action())) + TypedIdentifier(name: "a", withType: P4QualifiedType(Action())) ) ).updateTable( addEntry: ( P4Value(P4IntValue(withValue: 2)), - TypedIdentifier(name: "b", withType: P4Type(Action())) + TypedIdentifier(name: "b", withType: P4QualifiedType(Action())) )) // Set a variable in the global scope for the inout first parameter. @@ -221,14 +221,14 @@ import TreeSitterP4 identifier: Identifier(name: "result_arg"), withValue: P4Value( P4IntValue(withValue: 0), - P4Type(P4Int()))) + P4QualifiedType(P4Int()))) let runtime = try #UseOkResult( P4Runtime.Runtime.create(control: control, withGlobalValues: global_values)) let (hit_miss, updated_execution) = try #UseOkResult(runtime.run( withArguments: ArgumentList([ - Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0), + Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0), Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 1), ]))) @@ -265,24 +265,24 @@ import TreeSitterP4 let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) // Pull the control out of the compiled program. - let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in - switch tipe.dataType() { + let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in + switch tipe.baseType() { case let c as Control: c.name == "simple" default: false } } - var control = ((controls[0].dataType() as P4DataType) as! Control) + var control = ((controls[0].baseType() as P4DataType) as! Control) // Add entries to the table. control = control.updateTable( addEntry: ( P4Value(P4IntValue(withValue: 1)), - TypedIdentifier(name: "a", withType: P4Type(Action())) + TypedIdentifier(name: "a", withType: P4QualifiedType(Action())) ) ).updateTable( addEntry: ( P4Value(P4IntValue(withValue: 2)), - TypedIdentifier(name: "b", withType: P4Type(Action())) + TypedIdentifier(name: "b", withType: P4QualifiedType(Action())) )) // Set a variable in the global scope for the inout first parameter. @@ -291,14 +291,14 @@ import TreeSitterP4 identifier: Identifier(name: "result_arg"), withValue: P4Value( P4IntValue(withValue: 0), - P4Type(P4Int()))) + P4QualifiedType(P4Int()))) let runtime = try #UseOkResult( P4Runtime.Runtime.create(control: control, withGlobalValues: global_values)) let (hit_miss, updated_execution) = try #UseOkResult(runtime.run( withArguments: ArgumentList([ - Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0), + Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0), Argument(P4Value(P4IntValue(withValue: 3)), atIndex: 1), ]))) @@ -336,24 +336,24 @@ import TreeSitterP4 let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) // Pull the control out of the compiled program. - let controls = program.InstancesWithTypes() { (tipe: P4Type) -> Bool in - switch tipe.dataType() { + let controls = program.InstancesWithTypes() { (tipe: P4QualifiedType) -> Bool in + switch tipe.baseType() { case let c as Control: c.name == "simple" default: false } } - var control = ((controls[0].dataType() as P4DataType) as! Control) + var control = ((controls[0].baseType() as P4DataType) as! Control) // Add entries to the table. control = control.updateTable( addEntry: ( P4Value(P4BooleanValue(withValue: true)), - TypedIdentifier(name: "a", withType: P4Type(Action())) + TypedIdentifier(name: "a", withType: P4QualifiedType(Action())) ) ).updateTable( addEntry: ( P4Value(P4IntValue(withValue: 5)), - TypedIdentifier(name: "b", withType: P4Type(Action())) + TypedIdentifier(name: "b", withType: P4QualifiedType(Action())) )) // Set a variable in the global scope for the inout first parameter. @@ -362,14 +362,14 @@ import TreeSitterP4 identifier: Identifier(name: "result_arg"), withValue: P4Value( P4IntValue(withValue: 0), - P4Type(P4Int()))) + P4QualifiedType(P4Int()))) let runtime = try #UseOkResult( P4Runtime.Runtime.create(control: control, withGlobalValues: global_values)) let (hit_miss, updated_execution) = try #UseOkResult(runtime.run( withArguments: ArgumentList([ - Argument(TypedIdentifier(name: "result_arg", withType: P4Type(P4Int())), atIndex: 0), + Argument(TypedIdentifier(name: "result_arg", withType: P4QualifiedType(P4Int())), atIndex: 0), Argument(P4Value(P4BooleanValue(withValue: false)), atIndex: 1), Argument(P4Value(P4IntValue(withValue: 5)), atIndex: 2), ]))) diff --git a/Tests/p4rseTests/ExternDeclarations.swift b/Tests/p4rseTests/ExternDeclarations.swift index b883d7e..9830570 100644 --- a/Tests/p4rseTests/ExternDeclarations.swift +++ b/Tests/p4rseTests/ExternDeclarations.swift @@ -38,10 +38,10 @@ public struct Return5: P4FFI { return ParameterList() } - public func type() -> Common.P4Type { - return P4Type( + public func type() -> Common.P4QualifiedType { + return P4QualifiedType( FunctionDeclaration( - named: Identifier(name: "externally"), ofType: P4Type(P4Int()), + named: Identifier(name: "externally"), ofType: P4QualifiedType(P4Int()), withParameters: ParameterList(), withBody: .none?)) } @@ -59,10 +59,10 @@ public struct Return6: P4FFI { return ParameterList() } - public func type() -> Common.P4Type { - return P4Type( + public func type() -> Common.P4QualifiedType { + return P4QualifiedType( FunctionDeclaration( - named: Identifier(name: "externally"), ofType: P4Type(P4Int()), + named: Identifier(name: "externally"), ofType: P4QualifiedType(P4Int()), withParameters: ParameterList(), withBody: .none?)) } diff --git a/Tests/p4rseTests/ParserCompilerTests.swift b/Tests/p4rseTests/ParserCompilerTests.swift index afbc00e..d9be3d3 100644 --- a/Tests/p4rseTests/ParserCompilerTests.swift +++ b/Tests/p4rseTests/ParserCompilerTests.swift @@ -118,7 +118,7 @@ import P4Lang #expect(parameters.parameters.count == 1) #expect(parameters.parameters[0].name == Identifier(name: "pmtr")) - #expect(parameters.parameters[0].type.dataType().eq(rhs: P4Boolean())) + #expect(parameters.parameters[0].type.baseType().eq(rhs: P4Boolean())) } @Test func test_simple_compiler_parser_with_multiple_parameters() async throws { @@ -138,10 +138,10 @@ import P4Lang #expect(parameters.parameters.count == 2) #expect(parameters.parameters[0].name == Identifier(name: "pmtr")) - #expect(parameters.parameters[0].type.dataType().eq(rhs: P4Boolean())) + #expect(parameters.parameters[0].type.baseType().eq(rhs: P4Boolean())) #expect(parameters.parameters[1].name == Identifier(name: "smtr")) - #expect(parameters.parameters[1].type.dataType().eq(rhs: P4String())) + #expect(parameters.parameters[1].type.baseType().eq(rhs: P4String())) } @Test func test_simple_compiler_parser_with_multiple_parameters2() async throws { @@ -161,13 +161,13 @@ import P4Lang #expect(parameters.parameters.count == 3) #expect(parameters.parameters[0].name == Identifier(name: "pmtr")) - #expect(parameters.parameters[0].type.dataType().eq(rhs: P4Boolean())) + #expect(parameters.parameters[0].type.baseType().eq(rhs: P4Boolean())) #expect(parameters.parameters[1].name == Identifier(name: "smtr")) - #expect(parameters.parameters[1].type.dataType().eq(rhs: P4String())) + #expect(parameters.parameters[1].type.baseType().eq(rhs: P4String())) #expect(parameters.parameters[2].name == Identifier(name: "imtr")) - #expect(parameters.parameters[2].type.dataType().eq(rhs: P4Int())) + #expect(parameters.parameters[2].type.baseType().eq(rhs: P4Int())) } @Test func test_simple_compiler_parser_use_parameters() async throws { diff --git a/Tests/p4rseTests/ScopeTests.swift b/Tests/p4rseTests/ScopeTests.swift index 48fc355..bca1c5b 100644 --- a/Tests/p4rseTests/ScopeTests.swift +++ b/Tests/p4rseTests/ScopeTests.swift @@ -28,39 +28,39 @@ import TreeSitterP4 @Test func test_scope() async throws { let s = VarTypeScope() - let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4Type(P4Int())) + let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4QualifiedType(P4Int())) let found_first = try! #require(s2.lookup(identifier: Identifier(name: "first"))) - #expect(found_first.dataType().eq(rhs: P4Int())) + #expect(found_first.baseType().eq(rhs: P4Int())) #expect(s2.count == 1) } @Test func test_scope_no_set() async throws { var ss = VarTypeScopes().enter() - ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4Type(P4Int())) + ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4QualifiedType(P4Int())) ss = ss.enter() - ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Type(P4Boolean())) + ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4QualifiedType(P4Boolean())) let found_first = try! #UseOkResult(ss.lookup(identifier: Identifier(name: "first"))) let found_second = try! #UseOkResult(ss.lookup(identifier: Identifier(name: "second"))) - #expect(found_first.dataType().eq(rhs: P4Int())) - #expect(found_second.dataType().eq(rhs: P4Boolean())) + #expect(found_first.baseType().eq(rhs: P4Int())) + #expect(found_second.baseType().eq(rhs: P4Boolean())) } @Test func test_scope_set() async throws { var ss = VarTypeScopes().enter() let id = Identifier(name: "first") - let id_type = P4Type(P4Int()) + let id_type = P4QualifiedType(P4Int()) ss = ss.declare(identifier: id, withValue: id_type) ss = ss.enter() - ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Type(P4Boolean())) + ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4QualifiedType(P4Boolean())) // Change the value of `first`. - ss = ss.set(identifier: id, withValue: P4Type(P4String())) + ss = ss.set(identifier: id, withValue: P4QualifiedType(P4String())) // Verify the change! let found = try! #UseOkResult(ss.lookup(identifier: id)) - #expect(found.dataType().eq(rhs: P4String())) + #expect(found.baseType().eq(rhs: P4String())) } diff --git a/Tests/p4rseTests/StructTests.swift b/Tests/p4rseTests/StructTests.swift index 90451bc..5e2dd89 100644 --- a/Tests/p4rseTests/StructTests.swift +++ b/Tests/p4rseTests/StructTests.swift @@ -41,11 +41,11 @@ import TreeSitterP4 """ var test_declarations = VarTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) - test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type)) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type)) var test_values = VarValueScopes().enter() test_values = test_values.declare( @@ -78,8 +78,8 @@ import TreeSitterP4 """ var test_types = TypeTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) @@ -108,8 +108,8 @@ import TreeSitterP4 """ var test_types = TypeTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) @@ -135,11 +135,11 @@ import TreeSitterP4 """ var test_declarations = VarTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) - test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type)) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type)) var test_values = VarValueScopes().enter() test_values = test_values.declare( @@ -170,11 +170,11 @@ import TreeSitterP4 """ var test_declarations = VarTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) - test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type)) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type)) var test_values = VarValueScopes().enter() test_values = test_values.declare( @@ -204,11 +204,11 @@ import TreeSitterP4 """ var test_declarations = VarTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) - test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type)) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type)) var test_values = VarValueScopes().enter() test_values = test_values.declare( @@ -240,15 +240,15 @@ import TreeSitterP4 var test_declarations = VarTypeScopes().enter() let ty_fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields) - let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(ty_struct_type))]) + let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))]) let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields) - test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type)) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type)) var test_values = VarValueScopes().enter() @@ -286,11 +286,11 @@ import TreeSitterP4 """ var test_declarations = VarTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) - test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type)) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type)) var test_values = VarValueScopes().enter() test_values = test_values.declare( @@ -318,11 +318,11 @@ import TreeSitterP4 """ var test_declarations = VarTypeScopes().enter() let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) - test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(struct_type)) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type)) #expect( #RequireErrorResult( @@ -349,15 +349,15 @@ import TreeSitterP4 var test_declarations = VarTypeScopes().enter() let ty_fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields) - let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(ty_struct_type))]) + let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))]) let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields) - test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type)) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type)) var test_values = VarValueScopes().enter() @@ -397,15 +397,15 @@ import TreeSitterP4 var test_declarations = VarTypeScopes().enter() let ty_fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields) - let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(ty_struct_type))]) + let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))]) let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields) - test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type)) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type)) var test_values = VarValueScopes().enter() @@ -444,15 +444,15 @@ import TreeSitterP4 var test_declarations = VarTypeScopes().enter() let ty_fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields) - let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4Type(ty_struct_type))]) + let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))]) let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields) - test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4Type(ts_struct_type)) + test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type)) #expect( #RequireErrorResult( diff --git a/Tests/p4rseTests/TypeTests.swift b/Tests/p4rseTests/TypeTests.swift index 1eab21f..e07ad34 100644 --- a/Tests/p4rseTests/TypeTests.swift +++ b/Tests/p4rseTests/TypeTests.swift @@ -26,8 +26,8 @@ import TreeSitterP4 @Test func test_simple_struct() async throws { let fields = P4StructFields([ - P4StructFieldIdentifier(name: "yesno", withType: P4Type(P4Boolean())), - P4StructFieldIdentifier(name: "count", withType: P4Type(P4Int())), + P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())), + P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())), ]) let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields) diff --git a/Tests/p4rseTests/ValueTypeParserTests.swift b/Tests/p4rseTests/ValueTypeParserTests.swift index dd6364f..cd8dbae 100644 --- a/Tests/p4rseTests/ValueTypeParserTests.swift +++ b/Tests/p4rseTests/ValueTypeParserTests.swift @@ -253,7 +253,7 @@ import TreeSitterP4 }; """ var test_types = VarTypeScopes().enter() - test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Type(P4Array(withValueType: P4Type(P4Int())))) + test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int())))) #expect( #RequireErrorResult( Error(