Start Evaluation

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-02-10 03:37:57 -05:00
parent 0dd5ce4be3
commit 3693bdc02d
15 changed files with 635 additions and 119 deletions
+24
View File
@@ -17,12 +17,27 @@
open class ProgramExecution: CustomStringConvertible {
public var scopes: Scopes = Scopes()
var error: Error?
public init() {}
open var description: String {
return "Runtime:\nScopes: \(scopes)"
}
public func hasError() -> Bool {
return self.error != nil
}
public func getError() -> Error? {
return self.error
}
public func setError(error: Error) -> ProgramExecution {
let npe = self
npe.error = error
return npe
}
}
@@ -96,6 +111,15 @@ public struct Scopes: CustomStringConvertible {
return s
}
public func evaluate(identifier: Identifier) -> Result<P4Value> {
for scope in scopes {
if let vari = scope.lookup(identifier: identifier) {
return .Ok(vari.value)
}
}
return .Error(Error(withMessage: "Cannot find \(identifier) in scope."))
}
public var count: Int {
get {
scopes.count
+7
View File
@@ -65,6 +65,12 @@ open class P4ValueBase<T: P4Type>: P4Value {
}
}
extension P4ValueBase: EvaluatableExpression {
public func evaluate(execution: ProgramExecution) -> Result<P4Value> {
return .Ok(self)
}
}
/// The type for a P4 struct
public struct P4Struct: P4Type {
public let name: String
@@ -122,6 +128,7 @@ public class P4BooleanValue: P4ValueBase<P4Boolean> {
}
}
/// A P4 int type
public struct P4Int: P4Type {
public static func create() -> any P4Type {
+1 -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) -> P4Value
func evaluate(execution: ProgramExecution) -> Result<P4Value>
}
public protocol EvaluatableParserStatement {
+7
View File
@@ -53,6 +53,13 @@ public enum Result<OKT>: Equatable {
}
return nil
}
public func map<T>(block: (OKT) -> Result<T>) -> Result<T> {
switch self {
case .Ok(let ok): return block(ok)
case .Error(let e): return .Error(e)
}
}
}
extension Result where OKT: CustomStringConvertible {