compiler, runtime, testing, common: Centralize Execution/Evaluation

Centralize the execution of statements and evaluation of expressions
so that the user can specify a debugging "callback" after every
execution/evaluation.

The callback can be used for myriad things, but it seems likely that
it will be useful for implementing:
1. Testing
2. Debugger

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-04-20 05:48:09 -04:00
parent c6f086f67f
commit d33066c543
13 changed files with 346 additions and 115 deletions
+19 -8
View File
@@ -29,22 +29,22 @@ public struct ParserRuntime: CustomStringConvertible {
self.initialValues = .none
}
init(parser: Parser, withInitialValues initial: VarValueScopes?) {
init(parser: Parser, withGlobalValues initial: VarValueScopes?) {
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)
return ParserRuntime.create(program: program, withGlobalValues: .none)
}
public static func create(
program: P4Lang.Program, withInitialValues initial: VarValueScopes?
program: P4Lang.Program, withGlobalValues initial: VarValueScopes?
) -> Result<ParserRuntime> {
return switch program.starting_parser() {
case .Ok(let parser):
.Ok(P4Runtime.ParserRuntime(parser: parser, withInitialValues: initial))
.Ok(P4Runtime.ParserRuntime(parser: parser, withGlobalValues: initial))
case .Error(let error): .Error(error)
}
}
@@ -52,12 +52,10 @@ public struct ParserRuntime: CustomStringConvertible {
/// Run a P4 parser with no arguments
public func run() -> Result<(ParserState, ProgramExecution)> {
return self.run(withArguments: ArgumentList([]))
}
/// Run the P4 parser on a given packet
public func run(withArguments arguments: ArgumentList) -> Result<(ParserState, ProgramExecution)>
{
public func run(withArguments arguments: ArgumentList) -> Result<(ParserState, ProgramExecution)> {
let pe =
if let initial = initialValues {
ProgramExecution(withGlobalValues: initial)
@@ -65,6 +63,19 @@ public struct ParserRuntime: CustomStringConvertible {
ProgramExecution()
}
return self.run(withArguments: arguments, inExecution: pe)
}
/// Run the P4 parser on a given packet
public func run(withArguments arguments: ArgumentList, inExecution pe: ProgramExecution) -> Result<(ParserState, ProgramExecution)>
{
let pe = if let globals = initialValues {
pe.setGlobalValues(globals)
} else {
pe
}
let (end_state, execution) = parser.call(execution: pe, arguments: arguments)
if let error = execution.getError() {
return .Error(error)