+50
-11
@@ -6,7 +6,7 @@ public struct LocalElement {
|
||||
|
||||
}
|
||||
|
||||
public struct ParserExecution {
|
||||
public class ParserExecution: ProgramExecution {
|
||||
public var state: ParserState
|
||||
|
||||
public init(_ state: ParserState) {
|
||||
@@ -14,7 +14,13 @@ public struct ParserExecution {
|
||||
}
|
||||
|
||||
public func transition(toNextState state: ParserState) -> ParserExecution {
|
||||
return ParserExecution(state)
|
||||
let next = self
|
||||
next.state = state
|
||||
return next
|
||||
}
|
||||
|
||||
public override var description: String {
|
||||
return "Execution: \(super.description)\nCurrent State: \(state)"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +32,7 @@ public protocol Expression {
|
||||
func evaluate(execution: ParserExecution) -> Value
|
||||
}
|
||||
|
||||
public protocol ParserStatement: Sendable {
|
||||
public protocol ParserStatement {
|
||||
/// Evaluate a statement for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the parser statement
|
||||
@@ -37,6 +43,20 @@ public protocol ParserStatement: Sendable {
|
||||
public struct ParserTransitionStatement: ParserStatement {
|
||||
public init() {}
|
||||
public func evaluate(execution: ParserExecution) -> ParserExecution {
|
||||
return execution
|
||||
}
|
||||
}
|
||||
|
||||
public struct VariableDeclarationStatement: ParserStatement {
|
||||
public var id: Identifier
|
||||
public init(withIdentifier id: Identifier) {
|
||||
self.id = id
|
||||
}
|
||||
|
||||
public func evaluate(execution: ParserExecution) -> ParserExecution {
|
||||
print("Evaluating!")
|
||||
execution.scopes.scopes[0].variables.append(Variable(name: id.name, withValue: id.value, isConstant: false))
|
||||
print("Execution: \(execution)")
|
||||
return execution
|
||||
}
|
||||
}
|
||||
@@ -48,32 +68,46 @@ public struct ExpressionStatement: ParserStatement {
|
||||
}
|
||||
}
|
||||
|
||||
public struct ParserState: Equatable, Sendable {
|
||||
public struct ParserState: Equatable, CustomStringConvertible {
|
||||
|
||||
public private(set) var state_name: String
|
||||
public private(set) var local_elements: [ParserStatement]
|
||||
public private(set) var statements: [ParserStatement]
|
||||
public private(set) var transition: ParserTransitionStatement?
|
||||
|
||||
public var description: String {
|
||||
return "Name: \(state_name)"
|
||||
}
|
||||
|
||||
public static func == (lhs: ParserState, rhs: ParserState) -> Bool {
|
||||
return lhs.state_name == rhs.state_name
|
||||
}
|
||||
|
||||
/// Construct a ParserState
|
||||
public init(name: String, withStatements statements: [ParserStatement]?, withTransition transitionStatement: ParserTransitionStatement) {
|
||||
public init(name: String, withLocalElements localElements: [ParserStatement]?, withStatements statements: [ParserStatement]?, withTransition transitionStatement: ParserTransitionStatement) {
|
||||
state_name = name
|
||||
transition = transitionStatement
|
||||
local_elements = localElements ?? Array()
|
||||
self.statements = statements ?? Array()
|
||||
}
|
||||
|
||||
func evaluate(execution: ParserExecution) -> ParserExecution {
|
||||
var currentExecution = execution
|
||||
|
||||
// First, evaluate the local elements.
|
||||
for local_element in local_elements {
|
||||
currentExecution = local_element.evaluate(execution: currentExecution)
|
||||
}
|
||||
|
||||
// Then, evaluate the statements.
|
||||
for statement in statements {
|
||||
currentExecution = statement.evaluate(execution: currentExecution)
|
||||
}
|
||||
|
||||
return if let transition = transition {
|
||||
execution.transition(toNextState: accept)
|
||||
currentExecution.transition(toNextState: accept)
|
||||
} else {
|
||||
execution.transition(toNextState: reject)
|
||||
currentExecution.transition(toNextState: reject)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,18 +117,19 @@ public struct ParserState: Equatable, Sendable {
|
||||
init(name: String) {
|
||||
state_name = name
|
||||
transition = .none
|
||||
local_elements = Array()
|
||||
statements = Array()
|
||||
}
|
||||
}
|
||||
|
||||
public struct ParserStates: Sendable {
|
||||
public struct ParserStates {
|
||||
public var states: [ParserState] = Array()
|
||||
}
|
||||
|
||||
public let accept: ParserState = ParserState(name: "accept")
|
||||
public let reject: ParserState = ParserState(name: "reject")
|
||||
nonisolated(unsafe) public let accept: ParserState = ParserState(name: "accept")
|
||||
nonisolated(unsafe) public let reject: ParserState = ParserState(name: "reject")
|
||||
|
||||
public struct Parser {
|
||||
public struct Parser: CustomStringConvertible {
|
||||
public var states: [ParserState] = Array()
|
||||
public var count: Int {
|
||||
states.count
|
||||
@@ -110,4 +145,8 @@ public struct Parser {
|
||||
}
|
||||
return .none
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
return "Parser"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,71 @@
|
||||
public struct Program {
|
||||
public class Identifier: CustomStringConvertible {
|
||||
var name: String
|
||||
var value: Value
|
||||
|
||||
public init(name: String, withValue value: Value) {
|
||||
self.name = name
|
||||
self.value = value
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
return "\(name) = \(value)"
|
||||
}
|
||||
}
|
||||
|
||||
public class Variable: Identifier {
|
||||
var constant: Bool
|
||||
|
||||
public init(name: String, withValue value: Value, isConstant constant: Bool) {
|
||||
self.constant = constant
|
||||
super.init(name: name, withValue: value)
|
||||
}
|
||||
|
||||
public override var description: String {
|
||||
return "\(super.description) \(constant ? "(constant)" : "")"
|
||||
}
|
||||
}
|
||||
|
||||
public struct Scope: CustomStringConvertible{
|
||||
var variables: [Variable] = Array()
|
||||
public init() {}
|
||||
|
||||
public var description: String {
|
||||
var result = String()
|
||||
for v in variables {
|
||||
result += "\(v)"
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
public struct Scopes: CustomStringConvertible {
|
||||
var scopes: [Scope] = Array()
|
||||
|
||||
public init() {}
|
||||
|
||||
public mutating func enter() {
|
||||
scopes.append(Scope())
|
||||
}
|
||||
|
||||
public mutating func exit() {
|
||||
let _ = scopes.popLast()
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
var result = String()
|
||||
for s in scopes {
|
||||
result += "Scope: \(s)\n"
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
public struct Program: CustomStringConvertible {
|
||||
public var parsers: [P4.Parser] = Array()
|
||||
public init() { }
|
||||
}
|
||||
public init() {}
|
||||
|
||||
public var description: String {
|
||||
return "Program"
|
||||
}
|
||||
}
|
||||
|
||||
+36
-35
@@ -1,40 +1,41 @@
|
||||
public struct Error {
|
||||
public private(set) var msg: String
|
||||
public class ProgramExecution: CustomStringConvertible {
|
||||
public var scopes: Scopes = Scopes()
|
||||
|
||||
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
|
||||
public var description: String {
|
||||
return "Runtime:\nScopes: \(scopes)"
|
||||
}
|
||||
}
|
||||
|
||||
//public struct ParserRuntime: ProgramRuntime {
|
||||
public class ParserRuntime: CustomStringConvertible {
|
||||
var execution: ParserExecution
|
||||
|
||||
init(execution: ParserExecution) {
|
||||
self.execution = execution
|
||||
}
|
||||
|
||||
public static func create(program: P4.Parser) -> Result<ParserRuntime> {
|
||||
// First, find the start state.
|
||||
guard let start_state = program.findStartState() else {
|
||||
return Result.Error(Error(withMessage: "Could not find the start state"))
|
||||
}
|
||||
return Result.Ok(P4.ParserRuntime(execution: P4.ParserExecution(start_state)))
|
||||
}
|
||||
|
||||
public func run(input: P4.Packet) -> Result<Nothing> {
|
||||
execution.scopes.enter()
|
||||
print("Execution: \(execution)")
|
||||
while execution.state != P4.accept && execution.state != P4.reject {
|
||||
execution = execution.state.evaluate(execution: execution)
|
||||
print("Execution: \(execution)")
|
||||
}
|
||||
return .Ok(Nothing())
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
//return "\(super.description)\nState: \(execution?.description ?? "N/A")\nError: \(error?.description ?? "None")"
|
||||
return "Runtime:\nExecution: \(execution)"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
public struct Error: Equatable {
|
||||
public private(set) var msg: String
|
||||
|
||||
public init(withMessage msg: String) {
|
||||
self.msg = msg
|
||||
}
|
||||
}
|
||||
|
||||
public struct Nothing: CustomStringConvertible {
|
||||
public var description: String {
|
||||
return "Nothing"
|
||||
}
|
||||
|
||||
public init() {}
|
||||
}
|
||||
|
||||
|
||||
public enum Result<T>: Equatable, CustomStringConvertible {
|
||||
case Ok(T)
|
||||
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 func error() -> Error? {
|
||||
if case Result.Error(let e) = self {
|
||||
return e
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
switch self {
|
||||
case Result.Error(let e):
|
||||
return e.msg
|
||||
case Result.Ok(let o):
|
||||
return "\(o)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@freestanding(expression) public macro RequireOkResult<T>(_: Result<T>) -> Bool =
|
||||
#externalMacro(module: "P4Macros", type: "RequireResult")
|
||||
@freestanding(expression) public macro RequireErrorResult<T>(_: Error, _: Result<T>) -> Bool =
|
||||
#externalMacro(module: "P4Macros", type: "RequireErrorResult")
|
||||
@freestanding(expression) public macro UseOkResult<T>(_: Result<T>) -> T =
|
||||
#externalMacro(module: "P4Macros", type: "UseOkResult")
|
||||
+16
-2
@@ -1,12 +1,26 @@
|
||||
// The Swift Programming Language
|
||||
// https://docs.swift.org/swift-book
|
||||
|
||||
public enum ValueType {
|
||||
public enum ValueType: CustomStringConvertible {
|
||||
case Boolean(Bool)
|
||||
|
||||
public var description: String {
|
||||
switch self {
|
||||
case ValueType.Boolean(let b):
|
||||
return "\(b) of Boolean"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct Value {
|
||||
public struct Value: CustomStringConvertible {
|
||||
public var value_type: ValueType
|
||||
|
||||
public init(withValue value: ValueType) {
|
||||
self.value_type = value
|
||||
}
|
||||
public var description: String {
|
||||
return "\(value_type)"
|
||||
}
|
||||
}
|
||||
|
||||
public class Packet {
|
||||
|
||||
Reference in New Issue
Block a user