Refactor Library And Start Runtime

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-01-20 07:10:58 -05:00
parent 45a8baa323
commit 4bec71dcf4
11 changed files with 472 additions and 65 deletions
+40
View File
@@ -0,0 +1,40 @@
public struct Error {
public private(set) var msg: String
public init(withMessage msg: String) {
self.msg = msg
}
}
public enum Result: Equatable {
case Ok
case Error(Error)
public static func == (lhs: Result, rhs: Result) -> Bool {
switch (lhs, rhs) {
case (Ok, Ok):
return true
case (Error(let le), Error(let re)):
return le.msg == re.msg
default:
return false
}
}
}
public struct ParserRuntime {
public init() {}
public func run(program: P4.Parser, input: P4.Packet) -> Result {
// First, find the start state.
guard var start_state = program.findStartState() else {
return Result.Error(Error(withMessage: "Could not find the start state"))
}
var execution = P4.ParserExecution(start_state)
while execution.state != P4.accept && execution.state != P4.reject {
execution = execution.state.evaluate(execution: execution)
}
return Result.Ok
}
}