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 <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-04 07:29:51 -04:00
parent a84e778e79
commit 7c660b2b0c
30 changed files with 264 additions and 264 deletions
@@ -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
}
+3 -3
View File
@@ -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())
}
}
+2 -2
View File
@@ -16,10 +16,10 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// A scope that resolves variable identifiers to their types.
public typealias VarTypeScope = Scope<P4Type>
public typealias VarTypeScope = Scope<P4QualifiedType>
/// Scopes that resolve variable identifiers to their types.
public typealias VarTypeScopes = Scopes<P4Type>
public typealias VarTypeScopes = Scopes<P4QualifiedType>
/// A scope that resolves type identifiers to their types.
public typealias TypeTypeScope = Scope<P4DataType>
+16 -16
View File
@@ -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
}
+1 -1
View File
@@ -17,6 +17,6 @@
public protocol P4FFI {
func execute(execution: ProgramExecution) -> (ControlFlow, ProgramExecution)
func type() -> P4Type
func type() -> P4QualifiedType
func parameters() -> ParameterList
}
+40 -40
View File
@@ -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<Direction>.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<P4Value> {
@@ -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
}
+1 -1
View File
@@ -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<P4Value>, ProgramExecution)
func type() -> P4Type
func type() -> P4QualifiedType
}
public protocol EvaluatableStatement {
+1 -1
View File
@@ -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
))
+3 -3
View File
@@ -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)
+10 -10
View File
@@ -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 {
+16 -16
View File
@@ -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<BinaryOperatorChecker>.none,
"Binary Equal", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.none,
binary_equal_operator_evaluator
),
"binaryLessThanOperatorExpression": (
"Binary Less Than", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
"Binary Less Than", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.none,
binary_lt_operator_evaluator
),
"binaryLessThanEqualOperatorExpression": (
"Binary Less Than Or Equal", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
"Binary Less Than Or Equal", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.none,
binary_lte_operator_evaluator
),
"binaryGreaterThanOperatorExpression": (
"Binary Greater Than", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
"Binary Greater Than", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.none,
binary_gt_operator_evaluator
),
"binaryGreaterThanEqualOperatorExpression": (
"Binary Greater Than Or Equal", P4Type(P4Boolean()), Optional<BinaryOperatorChecker>.none,
"Binary Greater Than Or Equal", P4QualifiedType(P4Boolean()), Optional<BinaryOperatorChecker>.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<ParameterList>.some(callee.params)
case (.none, .some(let callee)):
Optional<ParameterList>.some((callee.ffi!.type().dataType() as! FunctionDeclaration).params)
Optional<ParameterList>.some((callee.ffi!.type().baseType() as! FunctionDeclaration).params)
default: Optional<ParameterList>.none
}
+1 -1
View File
@@ -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(
+2 -2
View File
@@ -68,13 +68,13 @@ extension P4Struct: CompilableType {
public struct Types {
static func CompileType(
type: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Result<P4Type> {
) -> Result<P4QualifiedType> {
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)
}
+5 -5
View File
@@ -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
+5 -5
View File
@@ -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()
}
}
+5 -5
View File
@@ -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<Parser> {
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 {
+2 -2
View File
@@ -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
}
+13 -13
View File
@@ -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)
}
}
+4 -4
View File
@@ -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() {
+1 -1
View File
@@ -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"))
+17 -17
View File
@@ -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))
@@ -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)
+6 -6
View File
@@ -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
}
+35 -35
View File
@@ -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<P4TableHitMissValue, Control>.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<P4TableHitMissValue, Control>.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<P4TableHitMissValue, Control>.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<P4TableHitMissValue, Control>.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<P4TableHitMissValue, Control>.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),
])))
+6 -6
View File
@@ -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?))
}
+6 -6
View File
@@ -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 {
+10 -10
View File
@@ -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()))
}
+38 -38
View File
@@ -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(
+2 -2
View File
@@ -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)
+1 -1
View File
@@ -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(