@@ -16,62 +16,62 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
open class ProgramExecution: CustomStringConvertible {
|
||||
public var scopes: ValueScopes = ValueScopes()
|
||||
var error: Error?
|
||||
var debug: DebugLevel = DebugLevel.Error
|
||||
public var scopes: ValueScopes = ValueScopes()
|
||||
var error: Error?
|
||||
var debug: DebugLevel = DebugLevel.Error
|
||||
|
||||
public init() {}
|
||||
public init() {}
|
||||
|
||||
open var description: String {
|
||||
return "Runtime:\nScopes: \(scopes)"
|
||||
}
|
||||
open var description: String {
|
||||
return "Runtime:\nScopes: \(scopes)"
|
||||
}
|
||||
|
||||
public func hasError() -> Bool {
|
||||
return self.error != nil
|
||||
}
|
||||
public func hasError() -> Bool {
|
||||
return self.error != nil
|
||||
}
|
||||
|
||||
public func getError() -> Error? {
|
||||
return self.error
|
||||
}
|
||||
public func getError() -> Error? {
|
||||
return self.error
|
||||
}
|
||||
|
||||
public func setError(error: Error) -> ProgramExecution {
|
||||
let npe = self
|
||||
npe.error = error
|
||||
return npe
|
||||
}
|
||||
public func setError(error: Error) -> ProgramExecution {
|
||||
let npe = self
|
||||
npe.error = error
|
||||
return npe
|
||||
}
|
||||
|
||||
public func getDebugLevel() -> DebugLevel {
|
||||
return self.debug
|
||||
}
|
||||
public func getDebugLevel() -> DebugLevel {
|
||||
return self.debug
|
||||
}
|
||||
|
||||
public func setDebugLevel(_ dl: DebugLevel) -> ProgramExecution {
|
||||
let pe = self
|
||||
pe.debug = dl
|
||||
return pe
|
||||
}
|
||||
public func setDebugLevel(_ dl: DebugLevel) -> ProgramExecution {
|
||||
let pe = self
|
||||
pe.debug = dl
|
||||
return pe
|
||||
}
|
||||
|
||||
open func isDone() -> Bool {
|
||||
return false
|
||||
}
|
||||
open func isDone() -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
open func setDone() -> ProgramExecution {
|
||||
// For a bare ProgramExecution, setDone is a noop.
|
||||
return self
|
||||
}
|
||||
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()
|
||||
public func enter_scope() -> ProgramExecution {
|
||||
let new_pe = self
|
||||
new_pe.scopes = self.scopes.enter()
|
||||
|
||||
return new_pe
|
||||
}
|
||||
return new_pe
|
||||
}
|
||||
|
||||
public func exit_scope() -> ProgramExecution {
|
||||
let new_pe = self
|
||||
new_pe.scopes = self.scopes.exit()
|
||||
public func exit_scope() -> ProgramExecution {
|
||||
let new_pe = self
|
||||
new_pe.scopes = self.scopes.exit()
|
||||
|
||||
return new_pe
|
||||
}
|
||||
return new_pe
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -16,28 +16,28 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
public protocol EvaluatableExpression {
|
||||
/// Evaluate an expression for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the expression
|
||||
/// - Returns: The value of expression
|
||||
func evaluate(execution: ProgramExecution) -> Result<P4Value>
|
||||
func type() -> any P4Type
|
||||
/// Evaluate an expression for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the expression
|
||||
/// - Returns: The value of expression
|
||||
func evaluate(execution: ProgramExecution) -> Result<P4Value>
|
||||
func type() -> any P4Type
|
||||
}
|
||||
|
||||
public protocol EvaluatableStatement {
|
||||
/// Evaluate a statement for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the parser statement
|
||||
/// - Returns: An updated execution after evaluating the parser statement
|
||||
func evaluate(execution: ProgramExecution) -> ProgramExecution
|
||||
/// Evaluate a statement for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the parser statement
|
||||
/// - Returns: An updated execution after evaluating the parser statement
|
||||
func evaluate(execution: ProgramExecution) -> ProgramExecution
|
||||
}
|
||||
|
||||
public protocol P4Type: CustomStringConvertible {
|
||||
static func create() -> any P4Type
|
||||
func eq(rhs: any P4Type) -> Bool
|
||||
static func create() -> any P4Type
|
||||
func eq(rhs: any P4Type) -> Bool
|
||||
}
|
||||
|
||||
public protocol P4Value: CustomStringConvertible {
|
||||
func type() -> any P4Type
|
||||
func eq(rhs: P4Value) -> Bool
|
||||
func type() -> any P4Type
|
||||
func eq(rhs: P4Value) -> Bool
|
||||
}
|
||||
|
||||
+82
-88
@@ -16,112 +16,106 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
public struct Scope<T>: CustomStringConvertible {
|
||||
var symbols: Dictionary<Identifier, T> = Dictionary()
|
||||
public init() {}
|
||||
var symbols: [Identifier: T] = Dictionary()
|
||||
public init() {}
|
||||
|
||||
public var description: String {
|
||||
var result = String()
|
||||
for (k,v) in symbols {
|
||||
result += "\(k): \(v)\n"
|
||||
}
|
||||
return result
|
||||
public var description: String {
|
||||
var result = String()
|
||||
for (k, v) in symbols {
|
||||
result += "\(k): \(v)\n"
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
public var count: Int {
|
||||
get {
|
||||
symbols.count
|
||||
}
|
||||
}
|
||||
public var count: Int {
|
||||
symbols.count
|
||||
}
|
||||
|
||||
public func lookup(identifier: Identifier) -> T? {
|
||||
if let symbol = symbols[identifier] {
|
||||
return symbol
|
||||
}
|
||||
return .none
|
||||
public func lookup(identifier: Identifier) -> T? {
|
||||
if let symbol = symbols[identifier] {
|
||||
return symbol
|
||||
}
|
||||
return .none
|
||||
}
|
||||
|
||||
public func declare(identifier: Identifier, withValue value: T) -> Scope {
|
||||
var s = self
|
||||
s.symbols[identifier] = value
|
||||
return s
|
||||
}
|
||||
public func declare(identifier: Identifier, withValue value: T) -> Scope {
|
||||
var s = self
|
||||
s.symbols[identifier] = value
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
public struct Scopes<T>: CustomStringConvertible {
|
||||
var scopes: [Scope<T>] = Array()
|
||||
var scopes: [Scope<T>] = Array()
|
||||
|
||||
public init() {}
|
||||
public init() {}
|
||||
|
||||
init(withScopes scopes: [Scope<T>]) {
|
||||
self.scopes = scopes
|
||||
init(withScopes scopes: [Scope<T>]) {
|
||||
self.scopes = scopes
|
||||
}
|
||||
|
||||
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 {
|
||||
var result = String()
|
||||
for s in scopes {
|
||||
result += "LexicalScope:\n\(s)\n"
|
||||
}
|
||||
|
||||
public func enter() -> Scopes {
|
||||
var new_scopes = scopes
|
||||
new_scopes.append(Scope())
|
||||
return result
|
||||
}
|
||||
|
||||
return Scopes(withScopes: new_scopes)
|
||||
public var current: Scope<T>? {
|
||||
scopes.last
|
||||
}
|
||||
|
||||
public func set(identifier: Identifier, withValue value: T) -> Scopes {
|
||||
var scopes = self.scopes
|
||||
var scopes_to_read: [Scope<T>] = Array()
|
||||
|
||||
// Find the scope that has `identifier`
|
||||
while let scope = scopes.popLast() {
|
||||
if scope.lookup(identifier: identifier) != nil {
|
||||
// Update that scope and add it to scopes
|
||||
scopes.append(scope.declare(identifier: identifier, withValue: value))
|
||||
break
|
||||
} else {
|
||||
// If there was no match, we'll put it back
|
||||
scopes_to_read.append(scope)
|
||||
}
|
||||
}
|
||||
return Scopes<T>(withScopes: (scopes + scopes_to_read))
|
||||
}
|
||||
|
||||
public func exit() -> Scopes {
|
||||
var old_scopes = scopes
|
||||
_ = old_scopes.popLast()
|
||||
return Scopes(withScopes: old_scopes)
|
||||
public func declare(identifier: Identifier, withValue value: T) -> Scopes {
|
||||
var s = self
|
||||
if let scope = s.scopes.popLast() {
|
||||
s.scopes.append(scope.declare(identifier: identifier, withValue: value))
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
var result = String()
|
||||
for s in scopes {
|
||||
result += "LexicalScope:\n\(s)\n"
|
||||
}
|
||||
|
||||
return result
|
||||
public func lookup(identifier: Identifier) -> Result<T> {
|
||||
for scope in scopes {
|
||||
if let vari = scope.lookup(identifier: identifier) {
|
||||
return .Ok(vari)
|
||||
}
|
||||
}
|
||||
return .Error(Error(withMessage: "Cannot find \(identifier) in lexical scope."))
|
||||
}
|
||||
|
||||
public var current: Scope<T>? {
|
||||
get {
|
||||
scopes.last
|
||||
}
|
||||
}
|
||||
|
||||
public func set(identifier: Identifier, withValue value: T) -> Scopes {
|
||||
var scopes = self.scopes
|
||||
var scopes_to_read: [Scope<T>] = Array()
|
||||
|
||||
// Find the scope that has `identifier`
|
||||
while let scope = scopes.popLast() {
|
||||
if scope.lookup(identifier: identifier) != nil {
|
||||
// Update that scope and add it to scopes
|
||||
scopes.append(scope.declare(identifier: identifier, withValue: value))
|
||||
break
|
||||
} else {
|
||||
// If there was no match, we'll put it back
|
||||
scopes_to_read.append(scope)
|
||||
}
|
||||
}
|
||||
return Scopes<T>(withScopes: (scopes + scopes_to_read))
|
||||
}
|
||||
|
||||
public func declare(identifier: Identifier, withValue value: T) -> Scopes {
|
||||
var s = self
|
||||
if let scope = s.scopes.popLast() {
|
||||
s.scopes.append(scope.declare(identifier: identifier, withValue: value))
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
public func lookup(identifier: Identifier) -> Result<T> {
|
||||
for scope in scopes {
|
||||
if let vari = scope.lookup(identifier: identifier) {
|
||||
return .Ok(vari)
|
||||
}
|
||||
}
|
||||
return .Error(Error(withMessage: "Cannot find \(identifier) in lexical scope."))
|
||||
}
|
||||
|
||||
public var count: Int {
|
||||
get {
|
||||
scopes.count
|
||||
}
|
||||
}
|
||||
public var count: Int {
|
||||
scopes.count
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user