Initial Array Access Implementation

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-13 09:41:04 -04:00
parent 1982fda677
commit 0b4db34177
10 changed files with 266 additions and 11 deletions
+12 -1
View File
@@ -17,10 +17,17 @@
open class ProgramExecution: CustomStringConvertible {
public var scopes: ValueScopes = ValueScopes()
let initialValues: ValueScopes?
var error: Error?
var debug: DebugLevel = DebugLevel.Error
public init() {}
public init() {
initialValues = .none
}
public init(withGlobalValues values: ValueScopes) {
initialValues = values
}
open var description: String {
return "Runtime:\nScopes: \(scopes)"
@@ -80,6 +87,10 @@ open class ProgramExecution: CustomStringConvertible {
new_pe.scopes = new_scopes
return new_pe
}
public func initial_values() -> ValueScopes? {
return self.initialValues
}
}
public typealias ValueScope = Scope<P4Value>
+51 -1
View File
@@ -198,8 +198,12 @@ public class P4IntValue: P4Value {
public init(withValue value: Int) {
self.value = value
}
public func access() -> Int {
return self.value
}
public func eq(rhs: P4Value) -> Bool {
print("Int value equal.")
guard let int_rhs = rhs as? P4IntValue else {
return false
}
@@ -251,3 +255,49 @@ public class P4StringValue: P4Value {
public class Packet {
public init() {}
}
/// A P4 array type
public struct P4Array: P4Type {
public static func create() -> any P4Type {
return P4Array()
}
public var description: String {
return "Array"
}
public func eq(rhs: any P4Type) -> Bool {
return switch rhs {
case is P4Array: true
default: false
}
}
}
/// An instance of a P4 array
public class P4ArrayValue: P4Value {
public func type() -> any P4Type {
return P4Array()
}
let value: Array<EvaluatableExpression>
public init(withValue value: Array<EvaluatableExpression>) {
self.value = value
}
public func access(_ index: Int) -> EvaluatableExpression {
return self.value[index]
}
public func eq(rhs: P4Value) -> Bool {
guard let _ = rhs as? P4ArrayValue else {
return false
}
// TODO!!
return true
}
public var description: String {
"\(self.value) of \(self.type()) type"
}
}
+5 -5
View File
@@ -16,10 +16,10 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
public struct Scope<T>: CustomStringConvertible, Sequence {
public typealias Element = T
public typealias Iterator = Dictionary<Identifier, T>.Values.Iterator
public typealias Element = (key: Identifier, value: T)
public typealias Iterator = Dictionary<Identifier, T>.Iterator
public func makeIterator() -> Iterator {
self.symbols.values.makeIterator()
self.symbols.makeIterator()
}
var symbols: [Identifier: T] = Dictionary()
@@ -123,8 +123,8 @@ public struct Scopes<T>: CustomStringConvertible, Sequence {
return .Error(Error(withMessage: "Cannot find \(identifier) in lexical scope."))
}
public typealias Element = T
public typealias Iterator = Dictionary<Identifier, T>.Values.Iterator
public typealias Element = (key: Identifier, value: T)
public typealias Iterator = Dictionary<Identifier, T>.Iterator
public func makeIterator() -> Iterator {
scopes.last?.makeIterator() ?? Scope<T>().makeIterator()
}