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
+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"))