@@ -72,7 +72,6 @@ public class Variable: TypedIdentifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// The type for a P4 struct
|
/// The type for a P4 struct
|
||||||
public struct P4Struct: P4Type {
|
public struct P4Struct: P4Type {
|
||||||
|
|
||||||
@@ -247,7 +246,6 @@ public class P4StringValue: P4Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class Packet {
|
public class Packet {
|
||||||
public init() {}
|
public init() {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,7 +141,9 @@ extension Result: CustomStringConvertible {
|
|||||||
#externalMacro(module: "Macros", type: "UseOkResult")
|
#externalMacro(module: "Macros", type: "UseOkResult")
|
||||||
@freestanding(expression) public macro UseErrorResult<T>(_: Result<T>) -> Error =
|
@freestanding(expression) public macro UseErrorResult<T>(_: Result<T>) -> Error =
|
||||||
#externalMacro(module: "Macros", type: "UseErrorResult")
|
#externalMacro(module: "Macros", type: "UseErrorResult")
|
||||||
@freestanding(codeItem) public macro RequireNodeType<N, T>(node: N, type: String, nice_type_name: String) =
|
@freestanding(codeItem) public macro RequireNodeType<N, T>(
|
||||||
|
node: N, type: String, nice_type_name: String
|
||||||
|
) =
|
||||||
#externalMacro(module: "Macros", type: "RequireNodeType")
|
#externalMacro(module: "Macros", type: "RequireNodeType")
|
||||||
@freestanding(codeItem) public macro RequireNodesType<N, T>(
|
@freestanding(codeItem) public macro RequireNodesType<N, T>(
|
||||||
nodes: N, type: [String], nice_type_names: [String]
|
nodes: N, type: [String], nice_type_names: [String]
|
||||||
|
|||||||
@@ -184,8 +184,9 @@ extension SelectExpression: CompilableExpression {
|
|||||||
var kses: [KeysetExpression] = Array()
|
var kses: [KeysetExpression] = Array()
|
||||||
var kses_errors: [Error] = Array()
|
var kses_errors: [Error] = Array()
|
||||||
|
|
||||||
select_body_node.enumerateNamedChildren() { current_node in
|
select_body_node.enumerateNamedChildren { current_node in
|
||||||
let maybe_parsed_kse = KeysetExpression.compile(node: current_node, inTree: tree, withScopes: scopes)
|
let maybe_parsed_kse = KeysetExpression.compile(
|
||||||
|
node: current_node, inTree: tree, withScopes: scopes)
|
||||||
if case .Ok(let parsed_kse) = maybe_parsed_kse {
|
if case .Ok(let parsed_kse) = maybe_parsed_kse {
|
||||||
kses.append(parsed_kse as! KeysetExpression)
|
kses.append(parsed_kse as! KeysetExpression)
|
||||||
} else {
|
} else {
|
||||||
@@ -202,8 +203,8 @@ extension SelectExpression: CompilableExpression {
|
|||||||
}.joined(separator: ";\n"))))
|
}.joined(separator: ";\n"))))
|
||||||
}
|
}
|
||||||
return .Ok(
|
return .Ok(
|
||||||
SelectExpression(withSelector: selector, withKeysetExpressions: kses),
|
SelectExpression(withSelector: selector, withKeysetExpressions: kses),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,38 +212,37 @@ extension KeysetExpression: CompilableExpression {
|
|||||||
static func compile(
|
static func compile(
|
||||||
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
||||||
) -> Result<EvaluatableExpression?> {
|
) -> Result<EvaluatableExpression?> {
|
||||||
if node.nodeType != "selectCase" {
|
if node.nodeType != "selectCase" {
|
||||||
return Result.Error(Error(withMessage: "Expected select case not found"))
|
return Result.Error(Error(withMessage: "Expected select case not found"))
|
||||||
}
|
|
||||||
|
|
||||||
guard let keysetexpression_node = node.child(at: 0),
|
|
||||||
keysetexpression_node.nodeType == "keysetExpression"
|
|
||||||
else {
|
|
||||||
return Result.Error(Error(withMessage: "Missing keyset expression in select case"))
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let targetstate_node = node.child(at: 2),
|
|
||||||
targetstate_node.nodeType == "identifier"
|
|
||||||
else {
|
|
||||||
return Result.Error(Error(withMessage: "Missing target state in select case"))
|
|
||||||
}
|
|
||||||
|
|
||||||
let maybe_parsed_keysetexpression = Expression.Compile(
|
|
||||||
node: keysetexpression_node, inTree: tree, withScopes: scopes)
|
|
||||||
guard case Result.Ok(let keysetexpression) = maybe_parsed_keysetexpression else {
|
|
||||||
return Result.Error(maybe_parsed_keysetexpression.error()!)
|
|
||||||
}
|
|
||||||
|
|
||||||
let maybe_parsed_targetstate = Identifier.Compile(
|
|
||||||
node: targetstate_node, inTree: tree, withScopes: scopes)
|
|
||||||
guard case .Ok(let targetstate) = maybe_parsed_targetstate else {
|
|
||||||
return Result.Error(maybe_parsed_targetstate.error()!)
|
|
||||||
}
|
|
||||||
|
|
||||||
return .Ok(
|
|
||||||
KeysetExpression(
|
|
||||||
withKey: keysetexpression, withNextState: targetstate)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
guard let keysetexpression_node = node.child(at: 0),
|
||||||
|
keysetexpression_node.nodeType == "keysetExpression"
|
||||||
|
else {
|
||||||
|
return Result.Error(Error(withMessage: "Missing keyset expression in select case"))
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let targetstate_node = node.child(at: 2),
|
||||||
|
targetstate_node.nodeType == "identifier"
|
||||||
|
else {
|
||||||
|
return Result.Error(Error(withMessage: "Missing target state in select case"))
|
||||||
|
}
|
||||||
|
|
||||||
|
let maybe_parsed_keysetexpression = Expression.Compile(
|
||||||
|
node: keysetexpression_node, inTree: tree, withScopes: scopes)
|
||||||
|
guard case Result.Ok(let keysetexpression) = maybe_parsed_keysetexpression else {
|
||||||
|
return Result.Error(maybe_parsed_keysetexpression.error()!)
|
||||||
|
}
|
||||||
|
|
||||||
|
let maybe_parsed_targetstate = Identifier.Compile(
|
||||||
|
node: targetstate_node, inTree: tree, withScopes: scopes)
|
||||||
|
guard case .Ok(let targetstate) = maybe_parsed_targetstate else {
|
||||||
|
return Result.Error(maybe_parsed_targetstate.error()!)
|
||||||
|
}
|
||||||
|
|
||||||
|
return .Ok(
|
||||||
|
KeysetExpression(
|
||||||
|
withKey: keysetexpression, withNextState: targetstate)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -147,15 +147,22 @@ public struct Parser {
|
|||||||
node: Node, inTree tree: MutableTree, withScope scopes: LexicalScopes
|
node: Node, inTree tree: MutableTree, withScope scopes: LexicalScopes
|
||||||
) -> Result<(ParserTransitionStatement, LexicalScopes)> {
|
) -> Result<(ParserTransitionStatement, LexicalScopes)> {
|
||||||
|
|
||||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "parserTransitionStatement", nice_type_name: "parser transition statement")
|
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
|
||||||
|
node: node, type: "parserTransitionStatement", nice_type_name: "parser transition statement"
|
||||||
|
)
|
||||||
|
|
||||||
guard let tse_node = node.child(at: 1),
|
guard let tse_node = node.child(at: 1),
|
||||||
tse_node.nodeType! == "transitionSelectionExpression" else {
|
tse_node.nodeType! == "transitionSelectionExpression"
|
||||||
return .Error(ErrorOnNode(node: node, withError: "Could not find transition select expression"))
|
else {
|
||||||
|
return .Error(
|
||||||
|
ErrorOnNode(node: node, withError: "Could not find transition select expression"))
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let next_node = tse_node.child(at: 0) else {
|
guard let next_node = tse_node.child(at: 0) else {
|
||||||
return .Error(ErrorOnNode(node: node, withError: "Could not find the next token in a transition selection expression"))
|
return .Error(
|
||||||
|
ErrorOnNode(
|
||||||
|
node: node,
|
||||||
|
withError: "Could not find the next token in a transition selection expression"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the next node is an identifier, we have the simple form ...
|
// If the next node is an identifier, we have the simple form ...
|
||||||
@@ -179,7 +186,8 @@ public struct Parser {
|
|||||||
switch SelectExpression.compile(node: next_node, inTree: tree, withScopes: scopes)
|
switch SelectExpression.compile(node: next_node, inTree: tree, withScopes: scopes)
|
||||||
{
|
{
|
||||||
case .Ok(let tse):
|
case .Ok(let tse):
|
||||||
.Ok((ParserTransitionStatement(withTransitionExpression: tse! as! SelectExpression), scopes))
|
.Ok(
|
||||||
|
(ParserTransitionStatement(withTransitionExpression: tse! as! SelectExpression), scopes))
|
||||||
case .Error(let e): .Error(e)
|
case .Error(let e): .Error(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ extension BlockStatement: CompilableStatement {
|
|||||||
public static func Compile(
|
public static func Compile(
|
||||||
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
||||||
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
||||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "blockStatement", nice_type_name: "block statement")
|
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
|
||||||
|
node: node, type: "blockStatement", nice_type_name: "block statement")
|
||||||
|
|
||||||
var currentChildIdx = 0
|
var currentChildIdx = 0
|
||||||
var currentChildIdxSafe = 1
|
var currentChildIdxSafe = 1
|
||||||
@@ -91,7 +92,8 @@ extension ConditionalStatement: CompilableStatement {
|
|||||||
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
||||||
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
||||||
|
|
||||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "conditionalStatement", nice_type_name: "conditional statement")
|
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
|
||||||
|
node: node, type: "conditionalStatement", nice_type_name: "conditional statement")
|
||||||
|
|
||||||
let maybe_condition_expression = node.child(at: 2)
|
let maybe_condition_expression = node.child(at: 2)
|
||||||
guard let condition_expression = maybe_condition_expression,
|
guard let condition_expression = maybe_condition_expression,
|
||||||
@@ -158,7 +160,8 @@ extension VariableDeclarationStatement: CompilableStatement {
|
|||||||
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
||||||
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
||||||
|
|
||||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "variableDeclaration", nice_type_name: "variable declaration statement")
|
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
|
||||||
|
node: node, type: "variableDeclaration", nice_type_name: "variable declaration statement")
|
||||||
|
|
||||||
let maybe_typeref = node.child(at: 0)
|
let maybe_typeref = node.child(at: 0)
|
||||||
guard let typeref = maybe_typeref,
|
guard let typeref = maybe_typeref,
|
||||||
@@ -235,7 +238,8 @@ extension ExpressionStatement: CompilableStatement {
|
|||||||
public static func Compile(
|
public static func Compile(
|
||||||
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
||||||
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
||||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "expressionStatement", nice_type_name: "expression statement")
|
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
|
||||||
|
node: node, type: "expressionStatement", nice_type_name: "expression statement")
|
||||||
|
|
||||||
let _ = node.child(at: 0)
|
let _ = node.child(at: 0)
|
||||||
|
|
||||||
|
|||||||
@@ -51,27 +51,27 @@ extension SelectExpression: EvaluatableExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension P4StringValue: EvaluatableExpression {
|
extension P4StringValue: EvaluatableExpression {
|
||||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
||||||
return .Ok(self)
|
return .Ok(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension P4BooleanValue: EvaluatableExpression {
|
extension P4BooleanValue: EvaluatableExpression {
|
||||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
||||||
return .Ok(self)
|
return .Ok(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension P4StructValue: EvaluatableExpression {
|
extension P4StructValue: EvaluatableExpression {
|
||||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
||||||
return .Ok(self)
|
return .Ok(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension P4IntValue: EvaluatableExpression {
|
extension P4IntValue: EvaluatableExpression {
|
||||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
||||||
return .Ok(self)
|
return .Ok(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variables are evaluatable because they can be looked up by identifiers.
|
// Variables are evaluatable because they can be looked up by identifiers.
|
||||||
@@ -83,4 +83,4 @@ extension TypedIdentifier: EvaluatableExpression {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,9 +46,7 @@ public struct ParserStateDirectTransition: ParserStateInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
get {
|
return "Instance of \(currrent_state)"
|
||||||
return "Instance of \(currrent_state)"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public let currrent_state: ParserState
|
public let currrent_state: ParserState
|
||||||
@@ -71,7 +69,7 @@ public struct ParserStateDirectTransition: ParserStateInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
program = program.setError(error: res.error()!).exit_scope()
|
program = program.setError(error: res.error()!).exit_scope()
|
||||||
|
|
||||||
return (self, program.exit_scope())
|
return (self, program.exit_scope())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,9 +97,7 @@ public struct ParserStateNoTransition: ParserStateInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
get {
|
return "Instance of \(currrent_state)"
|
||||||
return "Instance of \(currrent_state)"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public let currrent_state: ParserState
|
public let currrent_state: ParserState
|
||||||
@@ -134,9 +130,7 @@ public struct ParserStateSelectTransition: ParserStateInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
get {
|
return "Instance of \(currrent_state)"
|
||||||
return "Instance of \(currrent_state)"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func execute(
|
public func execute(
|
||||||
@@ -203,7 +197,7 @@ extension ParserStates: Compilable {
|
|||||||
public typealias ToCompile = ParserStates
|
public typealias ToCompile = ParserStates
|
||||||
public typealias Compiled = (ParserStateInstance, [ParserStateInstance])
|
public typealias Compiled = (ParserStateInstance, [ParserStateInstance])
|
||||||
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
||||||
var compiled_states:[ParserStateInstance] = Array()
|
var compiled_states: [ParserStateInstance] = Array()
|
||||||
|
|
||||||
compiled_states.append(ParserStateNoTransition(currrent_state: accept))
|
compiled_states.append(ParserStateNoTransition(currrent_state: accept))
|
||||||
compiled_states.append(ParserStateNoTransition(currrent_state: reject))
|
compiled_states.append(ParserStateNoTransition(currrent_state: reject))
|
||||||
@@ -213,7 +207,7 @@ 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) {
|
switch ParserState.compile(state) {
|
||||||
case .Ok(let compiled):
|
case .Ok(let compiled):
|
||||||
if compiled.state().state == Identifier(name: "start") {
|
if compiled.state().state == Identifier(name: "start") {
|
||||||
start_state = compiled
|
start_state = compiled
|
||||||
}
|
}
|
||||||
@@ -252,7 +246,8 @@ extension ParserInstance: Compilable {
|
|||||||
|
|
||||||
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
||||||
return switch ParserStates.compile(parser.states) {
|
return switch ParserStates.compile(parser.states) {
|
||||||
case .Ok(let (start_state, states)): Result.Ok(ParserInstance(start: start_state, states: states))
|
case .Ok(let (start_state, states)):
|
||||||
|
Result.Ok(ParserInstance(start: start_state, states: states))
|
||||||
case .Error(let e): Result.Error(e)
|
case .Error(let e): Result.Error(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,4 +66,4 @@ extension ExpressionStatement: EvaluatableStatement {
|
|||||||
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
|
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
|
||||||
return execution
|
return execution
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user