Refactor Type System

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-02-06 07:46:18 -05:00
parent bc700509c1
commit c3fdfb62e8
11 changed files with 395 additions and 199 deletions
+146 -60
View File
@@ -15,85 +15,171 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// A P4 identifier
public class Identifier: CustomStringConvertible, Equatable {
var name: String
var name: String
public init(name: String) {
self.name = name
}
public init(name: String) {
self.name = name
}
public var description: String {
return "\(name)"
}
public var description: String {
return "\(name)"
}
public static func ==(lhs: Identifier, rhs: Identifier) -> Bool {
return lhs.name == rhs.name
}
public static func == (lhs: Identifier, rhs: Identifier) -> Bool {
return lhs.name == rhs.name
}
}
/// A P4 variable
public class Variable: Identifier {
var constant: Bool
var value: ValueType
var constant: Bool
var value: P4Value
public init(name: String, withValue value: ValueType, isConstant constant: Bool) {
self.constant = constant
self.value = value
super.init(name: name)
}
public init(name: String, withValue value: P4Value, isConstant constant: Bool) {
self.constant = constant
self.value = value
super.init(name: name)
}
public override var description: String {
return "\(super.description) = \(value) \(constant ? "(constant)" : "")"
}
public override var description: String {
return "\(super.description) = \(value) \(constant ? "(constant)" : "")"
}
public var value_type: ValueType {
get {
value
}
}
public var value_type: P4Value {
value
}
}
public enum ValueType: CustomStringConvertible, Equatable {
case Boolean(Bool)
case Int(Int)
case String(String)
/// A base for all instances of P4 types
open class P4ValueBase<T: P4Type>: P4Value {
public var description: String {
switch self {
case ValueType.Boolean(let b):
return "\(b) of Boolean"
case ValueType.Int(let i):
return "\(i) of Int"
case ValueType.String(let s):
return "\(s) of String"
}
}
public static func==(lhs: ValueType, rhs: ValueType) -> Bool {
switch (lhs,rhs) {
case (ValueType.Boolean(let lhsb), ValueType.Boolean(let rhsb)):
return lhsb == rhsb
case (ValueType.String(let lhsb), ValueType.String(let rhsb)):
return lhsb == rhsb
case (ValueType.Int(let lhsb), ValueType.Int(let rhsb)):
return lhsb == rhsb
default: return false
}
}
public init() {}
public func type() -> P4Type {
return T.create()
}
public func eq(rhs: P4Value) -> Bool {
return false
}
}
public struct Value: CustomStringConvertible {
public var value_type: ValueType
/// The type for a P4 struct
public struct P4Struct: P4Type {
public let name: String
// The type of the struct created is always anonymous.
public static func create() -> any P4Type {
return P4Struct()
}
public init(withValue value: ValueType) {
self.value_type = value
public init(withName name: String) {
self.name = name
}
public init() {
self.name = ""
}
}
/// The field of a P4 struct
public struct P4StructField {
public let name: Identifier
public let type: P4Type
public init(withName name: Identifier, withType type: P4Type) {
self.name = name
self.type = type
}
}
/// An instance of a P4 struct
public class P4StructValue: P4ValueBase<P4Struct> {
public let fields: [P4StructField]
public init(withFields fields: [P4StructField]) {
self.fields = fields
}
}
/// A P4 boolean type
public struct P4Boolean: P4Type {
public static func create() -> any P4Type {
return P4Boolean()
}
}
/// An instance of a P4 boolean
public class P4BooleanValue: P4ValueBase<P4Boolean> {
let value: Bool
public init(withValue value: Bool) {
self.value = value
}
public override func eq(rhs: P4Value) -> Bool {
guard let bool_rhs = rhs as? P4BooleanValue else {
return false
}
public var description: String {
return "\(value_type)"
return self.value == bool_rhs.value
}
}
/// A P4 int type
public struct P4Int: P4Type {
public static func create() -> any P4Type {
return P4Int()
}
}
/// An instance of a P4 integer
public class P4IntValue: P4ValueBase<P4Int> {
let value: Int
public init(withValue value: Int) {
self.value = value
}
public override func eq(rhs: P4Value) -> Bool {
guard let int_rhs = rhs as? P4IntValue else {
return false
}
return self.value == int_rhs.value
}
}
/// A P4 string type
public struct P4String: P4Type {
public static func create() -> any P4Type {
return P4String()
}
}
/// An instance of a P4 string
public class P4StringValue: P4ValueBase<P4String> {
let value: String
public init(withValue value: String) {
self.value = value
}
public override func eq(rhs: P4Value) -> Bool {
guard let string_rhs = rhs as? P4StringValue else {
return false
}
return self.value == string_rhs.value
}
}
/// A P4 value (with a type)
public struct Value: CustomStringConvertible, Equatable {
public var type: P4Type
public var value: P4Value
public init(withValue value: P4Value, andType type: P4Type) {
self.value = value
self.type = type
}
public var description: String {
return "\(self.value) of \(self.type)"
}
public static func == (lhs: Value, rhs: Value) -> Bool {
return lhs.value.eq(rhs: rhs.value)
}
}
public class Packet {
public init() {}
}
public init() {}
}
+9 -1
View File
@@ -20,7 +20,7 @@ public protocol EvaluatableExpression {
/// - Parameters
/// - execution: The execution context in which to evaluate the expression
/// - Returns: The value of expression
func evaluate(execution: ProgramExecution) -> ValueType
func evaluate(execution: ProgramExecution) -> P4Value
}
public protocol EvaluatableParserStatement {
@@ -31,3 +31,11 @@ public protocol EvaluatableParserStatement {
func evaluate(execution: ProgramExecution) -> ProgramExecution
}
public protocol P4Type {
static func create() -> P4Type
}
public protocol P4Value {
func type() -> P4Type
func eq(rhs: P4Value) -> Bool
}