Initial Array Access Implementation
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -113,3 +113,32 @@ extension BinaryOperatorExpression: EvaluatableExpression {
|
||||
return self.evaluator.1
|
||||
}
|
||||
}
|
||||
|
||||
extension ArrayAccessExpression: EvaluatableExpression {
|
||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
||||
let maybe_name = self.name.evaluate(execution: execution)
|
||||
guard case Result.Ok(let name) = maybe_name else {
|
||||
return maybe_name
|
||||
}
|
||||
|
||||
let maybe_indexor = self.indexor.evaluate(execution: execution)
|
||||
guard case Result.Ok(let indexor) = maybe_indexor else {
|
||||
return maybe_indexor
|
||||
}
|
||||
|
||||
guard let indexor_int = indexor as? P4IntValue else {
|
||||
return Result.Error(Error(withMessage: "\(indexor) cannot index an array"))
|
||||
}
|
||||
|
||||
guard let array = name as? P4ArrayValue else {
|
||||
return Result.Error(Error(withMessage: "\(name) does not name an array"))
|
||||
}
|
||||
let accessed = array.access(indexor_int.access())
|
||||
|
||||
return accessed.evaluate(execution: execution)
|
||||
}
|
||||
|
||||
public func type() -> any Common.P4Type {
|
||||
return P4Int.create()
|
||||
}
|
||||
}
|
||||
@@ -120,6 +120,13 @@ extension Parser: ParserExecution {
|
||||
execution = execution.declare(identifier: accept.state().state, withValue: accept)
|
||||
execution = execution.declare(identifier: reject.state().state, withValue: reject)
|
||||
|
||||
// Add initial values to the global scope
|
||||
if let initial = execution.initial_values() {
|
||||
for (name, value) in initial {
|
||||
execution = execution.declare(identifier: name, withValue: value)
|
||||
}
|
||||
}
|
||||
|
||||
// First, add every state to the scope!
|
||||
for state in self.states.states {
|
||||
execution = execution.declare(identifier: state.state, withValue: state)
|
||||
|
||||
@@ -22,16 +22,27 @@ import P4Lang
|
||||
public struct ParserRuntime: CustomStringConvertible {
|
||||
public var parser: Parser
|
||||
|
||||
let initialValues: ValueScopes?
|
||||
|
||||
init(parser: Parser) {
|
||||
self.parser = parser
|
||||
self.initialValues = .none
|
||||
}
|
||||
|
||||
init(parser: Parser, withInitialValues initial: ValueScopes?) {
|
||||
self.parser = parser
|
||||
self.initialValues = initial
|
||||
}
|
||||
|
||||
/// Create a parser runtime from a P4 program
|
||||
public static func create(program: P4Lang.Program) -> Result<ParserRuntime> {
|
||||
return ParserRuntime.create(program: program, withInitialValues: .none)
|
||||
}
|
||||
|
||||
public static func create(program: P4Lang.Program, withInitialValues initial: ValueScopes?) -> Result<ParserRuntime> {
|
||||
return switch program.starting_parser() {
|
||||
case .Ok(let parser):
|
||||
.Ok(P4Runtime.ParserRuntime(parser: parser))
|
||||
.Ok(P4Runtime.ParserRuntime(parser: parser, withInitialValues: initial))
|
||||
case .Error(let error): .Error(error)
|
||||
}
|
||||
}
|
||||
@@ -39,7 +50,13 @@ public struct ParserRuntime: CustomStringConvertible {
|
||||
/// Run the P4 parser on a given packet
|
||||
public func run() -> Result<(ParserState, ProgramExecution)> {
|
||||
|
||||
let (end_state, execution) = parser.execute(execution: ProgramExecution())
|
||||
let pe = if let initial = initialValues {
|
||||
ProgramExecution(withGlobalValues: initial)
|
||||
} else {
|
||||
ProgramExecution()
|
||||
}
|
||||
|
||||
let (end_state, execution) = parser.execute(execution: pe)
|
||||
if let error = execution.getError() {
|
||||
return .Error(error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user