Refactor Runtime

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-02-19 22:50:19 -05:00
parent 3693bdc02d
commit d9c8c5aeb0
17 changed files with 897 additions and 439 deletions
+83 -7
View File
@@ -17,7 +17,8 @@
open class ProgramExecution: CustomStringConvertible {
public var scopes: Scopes = Scopes()
var error: Error?
var error: Error?
var debug: DebugLevel = DebugLevel.Error
public init() {}
@@ -38,10 +39,44 @@ open class ProgramExecution: CustomStringConvertible {
npe.error = error
return npe
}
public func getDebugLevel() -> DebugLevel {
return self.debug
}
public func setDebugLevel(_ dl: DebugLevel) -> ProgramExecution {
let pe = self
pe.debug = dl
return pe
}
open func isDone() -> Bool {
return false
}
open func setDone() -> ProgramExecution {
// For a bare ProgramExecution, setDone is a noop.
return self
}
public func enter_scope() -> ProgramExecution {
let new_pe = self
new_pe.scopes = self.scopes.enter()
return new_pe
}
public func exit_scope() -> ProgramExecution {
let new_pe = self
new_pe.scopes = self.scopes.exit()
return new_pe
}
}
public struct Scope: CustomStringConvertible{
public struct Scope: CustomStringConvertible, Equatable {
var variables: [Variable] = Array()
public init() {}
@@ -59,6 +94,26 @@ public struct Scope: CustomStringConvertible{
}
}
public func set(identifier: Identifier, value: P4Value) -> Scope? {
var updated = false
var updated_scope: [Variable] = Array()
for v in variables {
if v == identifier && v.value_type.type().eq(rhs: value.type()) {
updated = true
updated_scope.append(Variable(name: v.name, withValue: value, isConstant: false))
} else {
updated_scope.append(v)
}
}
var new_scope = Scope()
new_scope.variables = updated_scope
return if updated {
new_scope
} else {
.none
}
}
public func lookup(identifier: Identifier) -> Variable? {
for v in variables {
if v == identifier {
@@ -75,17 +130,26 @@ public struct Scope: CustomStringConvertible{
}
}
public struct Scopes: CustomStringConvertible {
public struct Scopes: CustomStringConvertible, Equatable {
var scopes: [Scope] = Array()
public init() {}
public mutating func enter() {
scopes.append(Scope())
init(withScopes scopes: [Scope]) {
self.scopes = scopes
}
public mutating func exit() {
let _ = scopes.popLast()
public func enter() -> Scopes {
var new_scopes = scopes
new_scopes.append(Scope())
return Scopes(withScopes: new_scopes)
}
public func exit() -> Scopes {
var old_scopes = scopes
_ = old_scopes.popLast()
return Scopes(withScopes: old_scopes)
}
public var description: String {
@@ -125,4 +189,16 @@ public struct Scopes: CustomStringConvertible {
scopes.count
}
}
public func set(identifier: Identifier, value: P4Value) -> Scopes {
var new_scopes: [Scope] = Array()
for scope in self.scopes {
if let updated_scope = scope.set(identifier: identifier, value: value) {
new_scopes.append(updated_scope)
} else {
new_scopes.append(scope)
}
}
return Scopes(withScopes: new_scopes)
}
}
+53 -17
View File
@@ -54,15 +54,20 @@ public class Variable: Identifier {
/// A base for all instances of P4 types
open class P4ValueBase<T: P4Type>: P4Value {
public init() {}
public func type() -> P4Type {
return T.create()
}
public func eq(rhs: P4Value) -> Bool {
return false
}
public var description: String {
"Value of \(self.type()) type"
}
}
extension P4ValueBase: EvaluatableExpression {
@@ -85,6 +90,17 @@ public struct P4Struct: P4Type {
public init() {
self.name = ""
}
public var description: String {
return "Struct \(self.name)"
}
public func eq(rhs: P4Type) -> Bool {
return if let struct_rhs = rhs as? P4Struct {
struct_rhs.name == self.name
} else {
false
}
}
}
/// The field of a P4 struct
@@ -111,6 +127,15 @@ public struct P4Boolean: P4Type {
public static func create() -> any P4Type {
return P4Boolean()
}
public var description: String {
return "Boolean"
}
public func eq(rhs: P4Type) -> Bool {
return switch rhs {
case is P4Boolean: true
default: false
}
}
}
/// An instance of a P4 boolean
@@ -126,14 +151,26 @@ public class P4BooleanValue: P4ValueBase<P4Boolean> {
}
return self.value == bool_rhs.value
}
}
public override var description: String {
"\(self.value ? "true" : "false") of \(self.type()) type"
}
}
/// A P4 int type
public struct P4Int: P4Type {
public static func create() -> any P4Type {
return P4Int()
}
public var description: String {
return "Int"
}
public func eq(rhs: P4Type) -> Bool {
return switch rhs {
case is P4Int: true
default: false
}
}
}
/// An instance of a P4 integer
@@ -148,6 +185,9 @@ public class P4IntValue: P4ValueBase<P4Int> {
}
return self.value == int_rhs.value
}
public override var description: String {
"\(self.value) of \(self.type()) type"
}
}
/// A P4 string type
@@ -155,6 +195,15 @@ public struct P4String: P4Type {
public static func create() -> any P4Type {
return P4String()
}
public var description: String {
return "String"
}
public func eq(rhs: P4Type) -> Bool {
return switch rhs {
case is P4String: true
default: false
}
}
}
/// An instance of a P4 string
public class P4StringValue: P4ValueBase<P4String> {
@@ -168,22 +217,9 @@ public class P4StringValue: P4ValueBase<P4String> {
}
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 override var description: String {
"\(self.value) of \(self.type()) type"
}
}
+3 -2
View File
@@ -31,11 +31,12 @@ public protocol EvaluatableParserStatement {
func evaluate(execution: ProgramExecution) -> ProgramExecution
}
public protocol P4Type {
public protocol P4Type: CustomStringConvertible {
static func create() -> P4Type
func eq(rhs: P4Type) -> Bool
}
public protocol P4Value {
public protocol P4Value: CustomStringConvertible {
func type() -> P4Type
func eq(rhs: P4Value) -> Bool
}
+46
View File
@@ -15,6 +15,52 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
public enum DebugLevel {
case Trace
case Verbose
case Debug
case Error
func value() -> UInt8 {
return switch self {
case DebugLevel.Trace: 3
case DebugLevel.Verbose: 2
case DebugLevel.Debug: 1
case DebugLevel.Error: 0
}
}
public func isTrace() -> Bool {
if self.value() >= DebugLevel.Trace.value() {
return true
}
return false
}
public func isVerbose() -> Bool {
if self.value() >= DebugLevel.Verbose.value() {
return true
}
return false
}
public func isDebug() -> Bool {
if self.value() >= DebugLevel.Debug.value() {
return true
}
return false
}
public func isError() -> Bool {
return true
}
public var description: String {
return switch self {
case DebugLevel.Trace: "Trace"
case DebugLevel.Verbose: "Verbose"
case DebugLevel.Debug: "Debug"
case DebugLevel.Error: "Error"
}
}
}
public struct Error: Equatable {
public private(set) var msg: String