@@ -27,3 +27,11 @@ $ swift package swift package --disable-sandbox preview-documentation --target
|
|||||||
```
|
```
|
||||||
|
|
||||||
For more information, see the [documentation for the Swift-DocC plugin](https://swiftlang.github.io/swift-docc-plugin/documentation/swiftdoccplugin/).
|
For more information, see the [documentation for the Swift-DocC plugin](https://swiftlang.github.io/swift-docc-plugin/documentation/swiftdoccplugin/).
|
||||||
|
|
||||||
|
#### Checking Format
|
||||||
|
|
||||||
|
To check the format:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ swift package plugin --allow-writing-to-package-directory swiftformat
|
||||||
|
```
|
||||||
@@ -16,62 +16,62 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
open class ProgramExecution: CustomStringConvertible {
|
open class ProgramExecution: CustomStringConvertible {
|
||||||
public var scopes: ValueScopes = ValueScopes()
|
public var scopes: ValueScopes = ValueScopes()
|
||||||
var error: Error?
|
var error: Error?
|
||||||
var debug: DebugLevel = DebugLevel.Error
|
var debug: DebugLevel = DebugLevel.Error
|
||||||
|
|
||||||
public init() {}
|
public init() {}
|
||||||
|
|
||||||
open var description: String {
|
open var description: String {
|
||||||
return "Runtime:\nScopes: \(scopes)"
|
return "Runtime:\nScopes: \(scopes)"
|
||||||
}
|
}
|
||||||
|
|
||||||
public func hasError() -> Bool {
|
public func hasError() -> Bool {
|
||||||
return self.error != nil
|
return self.error != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getError() -> Error? {
|
public func getError() -> Error? {
|
||||||
return self.error
|
return self.error
|
||||||
}
|
}
|
||||||
|
|
||||||
public func setError(error: Error) -> ProgramExecution {
|
public func setError(error: Error) -> ProgramExecution {
|
||||||
let npe = self
|
let npe = self
|
||||||
npe.error = error
|
npe.error = error
|
||||||
return npe
|
return npe
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getDebugLevel() -> DebugLevel {
|
public func getDebugLevel() -> DebugLevel {
|
||||||
return self.debug
|
return self.debug
|
||||||
}
|
}
|
||||||
|
|
||||||
public func setDebugLevel(_ dl: DebugLevel) -> ProgramExecution {
|
public func setDebugLevel(_ dl: DebugLevel) -> ProgramExecution {
|
||||||
let pe = self
|
let pe = self
|
||||||
pe.debug = dl
|
pe.debug = dl
|
||||||
return pe
|
return pe
|
||||||
}
|
}
|
||||||
|
|
||||||
open func isDone() -> Bool {
|
open func isDone() -> Bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
open func setDone() -> ProgramExecution {
|
open func setDone() -> ProgramExecution {
|
||||||
// For a bare ProgramExecution, setDone is a noop.
|
// For a bare ProgramExecution, setDone is a noop.
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
public func enter_scope() -> ProgramExecution {
|
public func enter_scope() -> ProgramExecution {
|
||||||
let new_pe = self
|
let new_pe = self
|
||||||
new_pe.scopes = self.scopes.enter()
|
new_pe.scopes = self.scopes.enter()
|
||||||
|
|
||||||
return new_pe
|
return new_pe
|
||||||
}
|
}
|
||||||
|
|
||||||
public func exit_scope() -> ProgramExecution {
|
public func exit_scope() -> ProgramExecution {
|
||||||
let new_pe = self
|
let new_pe = self
|
||||||
new_pe.scopes = self.scopes.exit()
|
new_pe.scopes = self.scopes.exit()
|
||||||
|
|
||||||
return new_pe
|
return new_pe
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,28 +16,28 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
public protocol EvaluatableExpression {
|
public protocol EvaluatableExpression {
|
||||||
/// Evaluate an expression for a given execution
|
/// Evaluate an expression for a given execution
|
||||||
/// - Parameters
|
/// - Parameters
|
||||||
/// - execution: The execution context in which to evaluate the expression
|
/// - execution: The execution context in which to evaluate the expression
|
||||||
/// - Returns: The value of expression
|
/// - Returns: The value of expression
|
||||||
func evaluate(execution: ProgramExecution) -> Result<P4Value>
|
func evaluate(execution: ProgramExecution) -> Result<P4Value>
|
||||||
func type() -> any P4Type
|
func type() -> any P4Type
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol EvaluatableStatement {
|
public protocol EvaluatableStatement {
|
||||||
/// Evaluate a statement for a given execution
|
/// Evaluate a statement for a given execution
|
||||||
/// - Parameters
|
/// - Parameters
|
||||||
/// - execution: The execution context in which to evaluate the parser statement
|
/// - execution: The execution context in which to evaluate the parser statement
|
||||||
/// - Returns: An updated execution after evaluating the parser statement
|
/// - Returns: An updated execution after evaluating the parser statement
|
||||||
func evaluate(execution: ProgramExecution) -> ProgramExecution
|
func evaluate(execution: ProgramExecution) -> ProgramExecution
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol P4Type: CustomStringConvertible {
|
public protocol P4Type: CustomStringConvertible {
|
||||||
static func create() -> any P4Type
|
static func create() -> any P4Type
|
||||||
func eq(rhs: any P4Type) -> Bool
|
func eq(rhs: any P4Type) -> Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol P4Value: CustomStringConvertible {
|
public protocol P4Value: CustomStringConvertible {
|
||||||
func type() -> any P4Type
|
func type() -> any P4Type
|
||||||
func eq(rhs: P4Value) -> Bool
|
func eq(rhs: P4Value) -> Bool
|
||||||
}
|
}
|
||||||
|
|||||||
+82
-88
@@ -16,112 +16,106 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
public struct Scope<T>: CustomStringConvertible {
|
public struct Scope<T>: CustomStringConvertible {
|
||||||
var symbols: Dictionary<Identifier, T> = Dictionary()
|
var symbols: [Identifier: T] = Dictionary()
|
||||||
public init() {}
|
public init() {}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
var result = String()
|
var result = String()
|
||||||
for (k,v) in symbols {
|
for (k, v) in symbols {
|
||||||
result += "\(k): \(v)\n"
|
result += "\(k): \(v)\n"
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
public var count: Int {
|
public var count: Int {
|
||||||
get {
|
symbols.count
|
||||||
symbols.count
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public func lookup(identifier: Identifier) -> T? {
|
public func lookup(identifier: Identifier) -> T? {
|
||||||
if let symbol = symbols[identifier] {
|
if let symbol = symbols[identifier] {
|
||||||
return symbol
|
return symbol
|
||||||
}
|
|
||||||
return .none
|
|
||||||
}
|
}
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
|
||||||
public func declare(identifier: Identifier, withValue value: T) -> Scope {
|
public func declare(identifier: Identifier, withValue value: T) -> Scope {
|
||||||
var s = self
|
var s = self
|
||||||
s.symbols[identifier] = value
|
s.symbols[identifier] = value
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct Scopes<T>: CustomStringConvertible {
|
public struct Scopes<T>: CustomStringConvertible {
|
||||||
var scopes: [Scope<T>] = Array()
|
var scopes: [Scope<T>] = Array()
|
||||||
|
|
||||||
public init() {}
|
public init() {}
|
||||||
|
|
||||||
init(withScopes scopes: [Scope<T>]) {
|
init(withScopes scopes: [Scope<T>]) {
|
||||||
self.scopes = scopes
|
self.scopes = scopes
|
||||||
|
}
|
||||||
|
|
||||||
|
public func enter() -> Scopes {
|
||||||
|
var new_scopes = scopes
|
||||||
|
new_scopes.append(Scope())
|
||||||
|
|
||||||
|
return Scopes(withScopes: new_scopes)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func exit() -> Scopes {
|
||||||
|
var old_scopes = scopes
|
||||||
|
_ = old_scopes.popLast()
|
||||||
|
return Scopes(withScopes: old_scopes)
|
||||||
|
}
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
var result = String()
|
||||||
|
for s in scopes {
|
||||||
|
result += "LexicalScope:\n\(s)\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
public func enter() -> Scopes {
|
return result
|
||||||
var new_scopes = scopes
|
}
|
||||||
new_scopes.append(Scope())
|
|
||||||
|
|
||||||
return Scopes(withScopes: new_scopes)
|
public var current: Scope<T>? {
|
||||||
|
scopes.last
|
||||||
|
}
|
||||||
|
|
||||||
|
public func set(identifier: Identifier, withValue value: T) -> Scopes {
|
||||||
|
var scopes = self.scopes
|
||||||
|
var scopes_to_read: [Scope<T>] = Array()
|
||||||
|
|
||||||
|
// Find the scope that has `identifier`
|
||||||
|
while let scope = scopes.popLast() {
|
||||||
|
if scope.lookup(identifier: identifier) != nil {
|
||||||
|
// Update that scope and add it to scopes
|
||||||
|
scopes.append(scope.declare(identifier: identifier, withValue: value))
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
// If there was no match, we'll put it back
|
||||||
|
scopes_to_read.append(scope)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return Scopes<T>(withScopes: (scopes + scopes_to_read))
|
||||||
|
}
|
||||||
|
|
||||||
public func exit() -> Scopes {
|
public func declare(identifier: Identifier, withValue value: T) -> Scopes {
|
||||||
var old_scopes = scopes
|
var s = self
|
||||||
_ = old_scopes.popLast()
|
if let scope = s.scopes.popLast() {
|
||||||
return Scopes(withScopes: old_scopes)
|
s.scopes.append(scope.declare(identifier: identifier, withValue: value))
|
||||||
}
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
public var description: String {
|
public func lookup(identifier: Identifier) -> Result<T> {
|
||||||
var result = String()
|
for scope in scopes {
|
||||||
for s in scopes {
|
if let vari = scope.lookup(identifier: identifier) {
|
||||||
result += "LexicalScope:\n\(s)\n"
|
return .Ok(vari)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
return .Error(Error(withMessage: "Cannot find \(identifier) in lexical scope."))
|
||||||
|
}
|
||||||
|
|
||||||
public var current: Scope<T>? {
|
public var count: Int {
|
||||||
get {
|
scopes.count
|
||||||
scopes.last
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public func set(identifier: Identifier, withValue value: T) -> Scopes {
|
|
||||||
var scopes = self.scopes
|
|
||||||
var scopes_to_read: [Scope<T>] = Array()
|
|
||||||
|
|
||||||
// Find the scope that has `identifier`
|
|
||||||
while let scope = scopes.popLast() {
|
|
||||||
if scope.lookup(identifier: identifier) != nil {
|
|
||||||
// Update that scope and add it to scopes
|
|
||||||
scopes.append(scope.declare(identifier: identifier, withValue: value))
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
// If there was no match, we'll put it back
|
|
||||||
scopes_to_read.append(scope)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Scopes<T>(withScopes: (scopes + scopes_to_read))
|
|
||||||
}
|
|
||||||
|
|
||||||
public func declare(identifier: Identifier, withValue value: T) -> Scopes {
|
|
||||||
var s = self
|
|
||||||
if let scope = s.scopes.popLast() {
|
|
||||||
s.scopes.append(scope.declare(identifier: identifier, withValue: value))
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
public func lookup(identifier: Identifier) -> Result<T> {
|
|
||||||
for scope in scopes {
|
|
||||||
if let vari = scope.lookup(identifier: identifier) {
|
|
||||||
return .Ok(vari)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return .Error(Error(withMessage: "Cannot find \(identifier) in lexical scope."))
|
|
||||||
}
|
|
||||||
|
|
||||||
public var count: Int {
|
|
||||||
get {
|
|
||||||
scopes.count
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+64
-64
@@ -20,87 +20,87 @@ import SwiftSyntax
|
|||||||
import SwiftSyntaxMacros
|
import SwiftSyntaxMacros
|
||||||
|
|
||||||
public struct UseOkResult: ExpressionMacro {
|
public struct UseOkResult: ExpressionMacro {
|
||||||
public static func expansion(
|
public static func expansion(
|
||||||
of node: some FreestandingMacroExpansionSyntax,
|
of node: some FreestandingMacroExpansionSyntax,
|
||||||
in context: some MacroExpansionContext
|
in context: some MacroExpansionContext
|
||||||
) throws -> ExprSyntax {
|
) throws -> ExprSyntax {
|
||||||
|
|
||||||
guard let argument = node.argumentList.first?.expression else {
|
guard let argument = node.argumentList.first?.expression else {
|
||||||
throw Require.Error.SyntaxError
|
throw Require.Error.SyntaxError
|
||||||
}
|
|
||||||
|
|
||||||
return """
|
|
||||||
{
|
|
||||||
switch \(argument) {
|
|
||||||
case Result.Ok(let __good): return __good
|
|
||||||
case Result.Error(let __error):
|
|
||||||
print("Unexpected result: \\(__error)")
|
|
||||||
throw Require.Error.UnexpectedResult
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return """
|
||||||
|
{
|
||||||
|
switch \(argument) {
|
||||||
|
case Result.Ok(let __good): return __good
|
||||||
|
case Result.Error(let __error):
|
||||||
|
print("Unexpected result: \\(__error)")
|
||||||
|
throw Require.Error.UnexpectedResult
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
"""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct Require {
|
public struct Require {
|
||||||
public enum Error: Swift.Error {
|
public enum Error: Swift.Error {
|
||||||
case UnexpectedResult
|
case UnexpectedResult
|
||||||
case SyntaxError
|
case SyntaxError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct RequireResult: ExpressionMacro {
|
public struct RequireResult: ExpressionMacro {
|
||||||
public static func expansion(
|
public static func expansion(
|
||||||
of node: some FreestandingMacroExpansionSyntax,
|
of node: some FreestandingMacroExpansionSyntax,
|
||||||
in context: some MacroExpansionContext
|
in context: some MacroExpansionContext
|
||||||
) throws -> ExprSyntax {
|
) throws -> ExprSyntax {
|
||||||
|
|
||||||
guard let argument = node.argumentList.first?.expression else {
|
guard let argument = node.argumentList.first?.expression else {
|
||||||
throw Require.Error.SyntaxError
|
throw Require.Error.SyntaxError
|
||||||
}
|
|
||||||
|
|
||||||
return """
|
|
||||||
{
|
|
||||||
switch \(argument) {
|
|
||||||
case Result.Ok(_): return true
|
|
||||||
case Result.Error(let __error):
|
|
||||||
print("Unexpected result: \\(__error)")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return """
|
||||||
|
{
|
||||||
|
switch \(argument) {
|
||||||
|
case Result.Ok(_): return true
|
||||||
|
case Result.Error(let __error):
|
||||||
|
print("Unexpected result: \\(__error)")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
"""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct RequireErrorResult: ExpressionMacro {
|
public struct RequireErrorResult: ExpressionMacro {
|
||||||
public static func expansion(
|
public static func expansion(
|
||||||
of node: some FreestandingMacroExpansionSyntax,
|
of node: some FreestandingMacroExpansionSyntax,
|
||||||
in context: some MacroExpansionContext
|
in context: some MacroExpansionContext
|
||||||
) throws -> ExprSyntax {
|
) throws -> ExprSyntax {
|
||||||
|
|
||||||
let arguments = node.argumentList.indices
|
let arguments = node.argumentList.indices
|
||||||
let expected_error = node.argumentList[arguments.startIndex].expression
|
let expected_error = node.argumentList[arguments.startIndex].expression
|
||||||
let error_producer = node.argumentList[arguments.index(after: arguments.startIndex)].expression
|
let error_producer = node.argumentList[arguments.index(after: arguments.startIndex)].expression
|
||||||
|
|
||||||
return ExprSyntax("""
|
return ExprSyntax(
|
||||||
{
|
"""
|
||||||
let __expected_error = \(expected_error)
|
{
|
||||||
let __actual_error = \(error_producer)
|
let __expected_error = \(expected_error)
|
||||||
if case Result.Error(__expected_error) = __actual_error {
|
let __actual_error = \(error_producer)
|
||||||
return true
|
if case Result.Error(__expected_error) = __actual_error {
|
||||||
} else {
|
return true
|
||||||
print("Expected Error: \\(__expected_error) but got Error: \\(__actual_error)")
|
} else {
|
||||||
return false
|
print("Expected Error: \\(__expected_error) but got Error: \\(__actual_error)")
|
||||||
}
|
return false
|
||||||
}()
|
}
|
||||||
""")
|
}()
|
||||||
}
|
""")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@main
|
@main
|
||||||
struct P4Macros: CompilerPlugin {
|
struct P4Macros: CompilerPlugin {
|
||||||
var providingMacros: [Macro.Type] = [
|
var providingMacros: [Macro.Type] = [
|
||||||
RequireResult.self, RequireErrorResult.self, UseOkResult.self,
|
RequireResult.self, RequireErrorResult.self, UseOkResult.self,
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,4 +19,4 @@ import Common
|
|||||||
|
|
||||||
public struct Instantiation {
|
public struct Instantiation {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ public class ParserState: Equatable, CustomStringConvertible, Comparable {
|
|||||||
public private(set) var next_state: ParserState?
|
public private(set) var next_state: ParserState?
|
||||||
|
|
||||||
public static func < (lhs: ParserState, rhs: ParserState) -> Bool {
|
public static func < (lhs: ParserState, rhs: ParserState) -> Bool {
|
||||||
// If lhs transitions to rhs, then return true. Otherwise, return false.
|
// If lhs transitions to rhs, then return true. Otherwise, return false.
|
||||||
|
|
||||||
// TODO!!
|
// TODO!!
|
||||||
return false
|
return false
|
||||||
@@ -141,7 +141,7 @@ public class ParserState: Equatable, CustomStringConvertible, Comparable {
|
|||||||
|
|
||||||
public func semantic_check(states: ParserStates) -> Bool {
|
public func semantic_check(states: ParserStates) -> Bool {
|
||||||
guard let transition = transition else {
|
guard let transition = transition else {
|
||||||
return self == accept || self == reject
|
return self == accept || self == reject
|
||||||
}
|
}
|
||||||
|
|
||||||
if let next_state_name = transition.next_state_name,
|
if let next_state_name = transition.next_state_name,
|
||||||
@@ -226,7 +226,6 @@ public struct ParserStates {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public struct Parser: P4Type {
|
public struct Parser: P4Type {
|
||||||
public var states: ParserStates
|
public var states: ParserStates
|
||||||
|
|
||||||
|
|||||||
@@ -22,26 +22,26 @@ public struct ExpressionStatement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public struct Program {
|
public struct Program {
|
||||||
public var types: [P4Type] = Array()
|
public var types: [P4Type] = Array()
|
||||||
|
|
||||||
/// Find the program's main parser
|
/// Find the program's main parser
|
||||||
///
|
///
|
||||||
/// Note: For now, the main parser is expected to be named main_parser.
|
/// Note: For now, the main parser is expected to be named main_parser.
|
||||||
public func starting_parser() -> Result<Parser> {
|
public func starting_parser() -> Result<Parser> {
|
||||||
return self.find_parser(withName: Identifier(name: "main_parser"))
|
return self.find_parser(withName: Identifier(name: "main_parser"))
|
||||||
|
}
|
||||||
|
|
||||||
|
public func find_parser(withName name: Identifier) -> Result<Parser> {
|
||||||
|
for type in self.types {
|
||||||
|
guard let parser = type as? Parser else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if parser.name == name {
|
||||||
|
return .Ok(parser)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return .Error(Error(withMessage: "Could not find parser named \(name)"))
|
||||||
|
}
|
||||||
|
|
||||||
public func find_parser(withName name: Identifier) -> Result<Parser> {
|
public init() {}
|
||||||
for type in self.types {
|
}
|
||||||
guard let parser = type as? Parser else {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if parser.name == name {
|
|
||||||
return .Ok(parser)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return .Error(Error(withMessage: "Could not find parser named \(name)"))
|
|
||||||
}
|
|
||||||
|
|
||||||
public init() {}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ protocol ParseableEvaluatableExpression {
|
|||||||
|
|
||||||
extension TypedIdentifier: ParseableEvaluatableExpression {
|
extension TypedIdentifier: ParseableEvaluatableExpression {
|
||||||
static func parse(
|
static func parse(
|
||||||
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree, withScopes scopes: LexicalScopes
|
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
|
||||||
|
withScopes scopes: LexicalScopes
|
||||||
) -> Result<EvaluatableExpression?> {
|
) -> Result<EvaluatableExpression?> {
|
||||||
|
|
||||||
guard
|
guard
|
||||||
@@ -61,7 +62,8 @@ extension TypedIdentifier: ParseableEvaluatableExpression {
|
|||||||
|
|
||||||
extension P4BooleanValue: ParseableEvaluatableExpression {
|
extension P4BooleanValue: ParseableEvaluatableExpression {
|
||||||
static func parse(
|
static func parse(
|
||||||
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree, withScopes scopes: LexicalScopes
|
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
|
||||||
|
withScopes scopes: LexicalScopes
|
||||||
) -> Result<EvaluatableExpression?> {
|
) -> Result<EvaluatableExpression?> {
|
||||||
|
|
||||||
guard
|
guard
|
||||||
@@ -101,7 +103,8 @@ extension P4BooleanValue: ParseableEvaluatableExpression {
|
|||||||
|
|
||||||
extension P4IntValue: ParseableEvaluatableExpression {
|
extension P4IntValue: ParseableEvaluatableExpression {
|
||||||
static func parse(
|
static func parse(
|
||||||
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree, withScopes scopes: LexicalScopes
|
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
|
||||||
|
withScopes scopes: LexicalScopes
|
||||||
) -> Result<EvaluatableExpression?> {
|
) -> Result<EvaluatableExpression?> {
|
||||||
|
|
||||||
guard
|
guard
|
||||||
@@ -132,7 +135,8 @@ extension P4IntValue: ParseableEvaluatableExpression {
|
|||||||
|
|
||||||
extension P4StringValue: ParseableEvaluatableExpression {
|
extension P4StringValue: ParseableEvaluatableExpression {
|
||||||
static func parse(
|
static func parse(
|
||||||
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree, withScopes scopes: LexicalScopes
|
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
|
||||||
|
withScopes scopes: LexicalScopes
|
||||||
) -> Result<EvaluatableExpression?> {
|
) -> Result<EvaluatableExpression?> {
|
||||||
|
|
||||||
guard
|
guard
|
||||||
@@ -202,4 +206,3 @@ extension ExpressionStatement: ParseableStatement {
|
|||||||
return Result.Ok((.none, scopes))
|
return Result.Ok((.none, scopes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,4 +18,4 @@
|
|||||||
import Common
|
import Common
|
||||||
|
|
||||||
public typealias LexicalScope = Scope<P4Type>
|
public typealias LexicalScope = Scope<P4Type>
|
||||||
public typealias LexicalScopes = Scopes<P4Type>
|
public typealias LexicalScopes = Scopes<P4Type>
|
||||||
|
|||||||
@@ -70,8 +70,10 @@ extension VariableDeclarationStatement: ParseableStatement {
|
|||||||
if rvalue.type().eq(rhs: declaration_p4_type) {
|
if rvalue.type().eq(rhs: declaration_p4_type) {
|
||||||
return Result.Ok(
|
return Result.Ok(
|
||||||
(
|
(
|
||||||
VariableDeclarationStatement(identifier: Identifier(name: variable_name), withInitializer: rvalue),
|
VariableDeclarationStatement(
|
||||||
scopes.declare(identifier: Identifier(name: variable_name), withValue: declaration_p4_type)
|
identifier: Identifier(name: variable_name), withInitializer: rvalue),
|
||||||
|
scopes.declare(
|
||||||
|
identifier: Identifier(name: variable_name), withValue: declaration_p4_type)
|
||||||
))
|
))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -51,4 +51,4 @@ public struct Types {
|
|||||||
}
|
}
|
||||||
return Result.Error(Error(withMessage: "Type name not recognized"))
|
return Result.Error(Error(withMessage: "Type name not recognized"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,7 +98,6 @@ public struct ParserStateSelectTransition: ParserStateInstance {
|
|||||||
program = statement.evaluate(execution: program)
|
program = statement.evaluate(execution: program)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
switch self.selector.evaluate(execution: program) {
|
switch self.selector.evaluate(execution: program) {
|
||||||
case .Ok(let selector_value):
|
case .Ok(let selector_value):
|
||||||
for (key, target) in zip(self.keys, self.states) {
|
for (key, target) in zip(self.keys, self.states) {
|
||||||
@@ -138,27 +137,33 @@ extension ParserState: Compilable {
|
|||||||
return .Ok(ParserStateNoTransition(currrent_state: state))
|
return .Ok(ParserStateNoTransition(currrent_state: state))
|
||||||
}
|
}
|
||||||
|
|
||||||
if state.direct_transition(),
|
if state.direct_transition(),
|
||||||
let transition_statement = state.transition {
|
let transition_statement = state.transition
|
||||||
|
{
|
||||||
return .Ok(
|
return .Ok(
|
||||||
ParserStateDirectTransition(
|
ParserStateDirectTransition(
|
||||||
currrent_state: state, next_state: current[transition_statement.next_state_name!]!))
|
currrent_state: state, next_state: current[transition_statement.next_state_name!]!))
|
||||||
}
|
}
|
||||||
|
|
||||||
if let transition_select_statement = state.transition,
|
if let transition_select_statement = state.transition,
|
||||||
let transition_select_expression = transition_select_statement.transition_expression {
|
let transition_select_expression = transition_select_statement.transition_expression
|
||||||
|
{
|
||||||
|
|
||||||
var keys: Array<any EvaluatableExpression> = Array()
|
var keys: [any EvaluatableExpression] = Array()
|
||||||
var states: Array<any ParserStateInstance> = Array()
|
var states: [any ParserStateInstance] = Array()
|
||||||
|
|
||||||
for kse in transition_select_expression.keyset_expressions {
|
for kse in transition_select_expression.keyset_expressions {
|
||||||
guard let next_state = current[kse.next_state_name] else {
|
guard let next_state = current[kse.next_state_name] else {
|
||||||
return .Error(Error(withMessage: "Cannot find \(kse.next_state_name) as transition target"))
|
return .Error(
|
||||||
|
Error(withMessage: "Cannot find \(kse.next_state_name) as transition target"))
|
||||||
}
|
}
|
||||||
keys.append(kse.key)
|
keys.append(kse.key)
|
||||||
states.append(next_state)
|
states.append(next_state)
|
||||||
}
|
}
|
||||||
return .Ok(ParserStateSelectTransition(keys: keys, states: states, selector: transition_select_expression.selector, currrent_state: state))
|
return .Ok(
|
||||||
|
ParserStateSelectTransition(
|
||||||
|
keys: keys, states: states, selector: transition_select_expression.selector,
|
||||||
|
currrent_state: state))
|
||||||
}
|
}
|
||||||
|
|
||||||
return .Error(Error(withMessage: "Invalid parser state: No meaningful transition"))
|
return .Error(Error(withMessage: "Invalid parser state: No meaningful transition"))
|
||||||
@@ -177,8 +182,8 @@ extension ParserStates: Compilable {
|
|||||||
// TODO: We assume that states are in transition-order!
|
// TODO: We assume that states are in transition-order!
|
||||||
for state in parser.states {
|
for state in parser.states {
|
||||||
switch ParserState.compile((state, compiled_states)) {
|
switch ParserState.compile((state, compiled_states)) {
|
||||||
case .Ok(let compiled): compiled_states[state.state_name] = compiled
|
case .Ok(let compiled): compiled_states[state.state_name] = compiled
|
||||||
case .Error(let e): return .Error(e)
|
case .Error(let e): return .Error(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,8 +15,8 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import P4Lang
|
|
||||||
import Common
|
import Common
|
||||||
|
import P4Lang
|
||||||
|
|
||||||
extension VariableDeclarationStatement: EvaluatableStatement {
|
extension VariableDeclarationStatement: EvaluatableStatement {
|
||||||
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
|
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
|
||||||
@@ -37,11 +37,11 @@ extension ExpressionStatement: EvaluatableStatement {
|
|||||||
|
|
||||||
// Variables are evaluatable because they can be looked up by identifiers.
|
// Variables are evaluatable because they can be looked up by identifiers.
|
||||||
extension TypedIdentifier: EvaluatableExpression {
|
extension TypedIdentifier: EvaluatableExpression {
|
||||||
public func type() -> any Common.P4Type {
|
public func type() -> any Common.P4Type {
|
||||||
return self.parsed_type
|
return self.parsed_type
|
||||||
}
|
}
|
||||||
|
|
||||||
public func evaluate(execution: Common.ProgramExecution) -> Result<P4Value> {
|
public func evaluate(execution: Common.ProgramExecution) -> Result<P4Value> {
|
||||||
return execution.scopes.lookup(identifier: self)
|
return execution.scopes.lookup(identifier: self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ protocol EvaluatableParserTransitionStatement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public protocol Execution {
|
public protocol Execution {
|
||||||
func execute() -> (ParserState, ProgramExecution)
|
func execute() -> (ParserState, ProgramExecution)
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol Compilable {
|
public protocol Compilable {
|
||||||
@@ -37,11 +37,11 @@ public protocol Compilable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public protocol ParserStateInstance {
|
public protocol ParserStateInstance {
|
||||||
func execute(program: ProgramExecution) -> (ParserStateInstance, ProgramExecution)
|
func execute(program: ProgramExecution) -> (ParserStateInstance, ProgramExecution)
|
||||||
func done() -> Bool
|
func done() -> Bool
|
||||||
func current() -> ParserState
|
func current() -> ParserState
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol ParserExecution {
|
public protocol ParserExecution {
|
||||||
func execute() -> (ParserState, ProgramExecution)
|
func execute() -> (ParserState, ProgramExecution)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ extension MutableTree {
|
|||||||
}
|
}
|
||||||
public func containsMissing(lang: Language) -> Bool {
|
public func containsMissing(lang: Language) -> Bool {
|
||||||
guard
|
guard
|
||||||
let parser_error_query = try? SwiftTreeSitter.Query(
|
let parser_error_query = try? SwiftTreeSitter.Query(
|
||||||
language: lang,
|
language: lang,
|
||||||
data: String(
|
data: String(
|
||||||
"(MISSING)"
|
"(MISSING)"
|
||||||
@@ -52,4 +52,4 @@ extension MutableTree {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user