compiler, runtime: Begin Runtime Refactor
Ultimately, the goal is to completely separate the compilation from the runtime to make it possible to have the interpreter/evaluator be "just another" entity that can perform meaningful work when given a parsed GP4 program. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -22,33 +22,61 @@ import SwiftTreeSitter
|
||||
import TreeSitterExtensions
|
||||
import TreeSitterP4
|
||||
|
||||
extension Declaration: CompilableDeclaration {
|
||||
extension Declaration: CompilableStatement {
|
||||
public static func CompileStatement(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<P4Statement> {
|
||||
return switch Compile(node: node, withContext: context) {
|
||||
case .Ok(let res): .Ok(res)
|
||||
case .Error(let e): .Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Declaration: Compilable {
|
||||
public typealias C = Declaration
|
||||
public static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(Declaration, CompilerContext)?> {
|
||||
) -> Result<Declaration> {
|
||||
|
||||
let declaration_compilers: [String: CompilableDeclaration.Type] = [
|
||||
// Be kind to our user -- if we are at a declaration node, dive into it!
|
||||
let declaration_node =
|
||||
if node.nodeType == "declaration" {
|
||||
node.child(at: 0)!
|
||||
} else {
|
||||
node
|
||||
}
|
||||
|
||||
let declaration_compilers: [String: any Compilable<Declaration>.Type] = [
|
||||
"function_declaration": FunctionDeclaration.self,
|
||||
"control_declaration": Control.self,
|
||||
"type_declaration": P4Struct.self,
|
||||
"parserDeclaration": Parser.self,
|
||||
/// ASSUME: Type declarations are struct declarations.
|
||||
"extern_declaration": ExternDeclaration.self,
|
||||
]
|
||||
|
||||
guard let declaration_compiler = declaration_compilers[node.nodeType!] else {
|
||||
return .Ok(.none)
|
||||
guard let declaration_compiler = declaration_compilers[declaration_node.nodeType!] else {
|
||||
return .Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Could not find parser for declaration \(declaration_node.nodeType!)"))
|
||||
}
|
||||
|
||||
return declaration_compiler.Compile(node: node, withContext: context)
|
||||
return declaration_compiler.Compile(node: declaration_node, withContext: context).map {
|
||||
result in
|
||||
.Ok(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension FunctionDeclaration: CompilableDeclaration {
|
||||
extension FunctionDeclaration: Compilable {
|
||||
public typealias C = Declaration
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(Declaration, CompilerContext)?> {
|
||||
) -> Common.Result<Declaration> {
|
||||
let function_declaration_node = node
|
||||
#RequireNodeType<Node, (ParameterList, CompilerContext)>(
|
||||
#RequireNodeType<Node, ParameterList>(
|
||||
node: function_declaration_node, type: "function_declaration",
|
||||
nice_type_name: "Function Declaration")
|
||||
|
||||
@@ -59,7 +87,7 @@ extension FunctionDeclaration: CompilableDeclaration {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: function_declaration_node.toSourceLocation(),
|
||||
withError: "Missing function declaration component")))
|
||||
@@ -72,7 +100,7 @@ extension FunctionDeclaration: CompilableDeclaration {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: function_declaration_node.toSourceLocation(),
|
||||
withError: "Missing function declaration component")))
|
||||
@@ -85,17 +113,16 @@ extension FunctionDeclaration: CompilableDeclaration {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: function_declaration_node.toSourceLocation(),
|
||||
withError: "Missing function declaration component")))
|
||||
|
||||
let maybe_function_parameters = ParameterList.Compile(node: current_node!, withContext: context)
|
||||
guard case .Ok((let function_parameters, let updated_context)) = maybe_function_parameters
|
||||
guard case .Ok(let function_parameters) = maybe_function_parameters
|
||||
else {
|
||||
return .Error(maybe_function_parameters.error()!)
|
||||
}
|
||||
context = updated_context
|
||||
|
||||
var function_body: BlockStatement? = .none
|
||||
|
||||
@@ -113,10 +140,10 @@ extension FunctionDeclaration: CompilableDeclaration {
|
||||
withContext: context.update(newInstances: function_scope).update(
|
||||
newExpectation: function_type))
|
||||
|
||||
guard case .Ok((let parsed_function_body, _)) = maybe_function_body else {
|
||||
guard case .Ok(let parsed_function_body) = maybe_function_body else {
|
||||
return .Error(maybe_function_body.error()!)
|
||||
}
|
||||
function_body = (parsed_function_body as! BlockStatement)
|
||||
function_body = parsed_function_body
|
||||
} else {
|
||||
|
||||
// If we are in an extern context, no body is okay!
|
||||
@@ -142,33 +169,29 @@ extension FunctionDeclaration: CompilableDeclaration {
|
||||
// And, do not update the context if we are compiling in an
|
||||
// extern context -- the wrapping extern declaration will take care of that.
|
||||
return .Ok(
|
||||
(
|
||||
function_declaration,
|
||||
context.extern_context
|
||||
? context
|
||||
: context.update(
|
||||
newTypes: context.types.declare(
|
||||
identifier: function_name, withValue: function_declaration.identifier.type.baseType())
|
||||
)
|
||||
))
|
||||
function_declaration,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension P4Struct: CompilableDeclaration {
|
||||
extension P4Struct: Compilable {
|
||||
public typealias C = Declaration
|
||||
static public func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(Declaration, CompilerContext)?> {
|
||||
) -> Result<Declaration> {
|
||||
let struct_declaration_node = node.child(at: 0)!
|
||||
var walker = Walker(node: struct_declaration_node)
|
||||
var currentNode: Node? = .none
|
||||
|
||||
#SkipUnlessNodeType(node: struct_declaration_node, type: "struct_declaration")
|
||||
#RequireNodeType<Node, Result<Declaration>>(
|
||||
node: struct_declaration_node, type: "struct_declaration",
|
||||
nice_type_name: "struct declaration")
|
||||
|
||||
// Skip the keyword struct
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: currentNode, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: struct_declaration_node.toSourceLocation(),
|
||||
withError: "Missing function declaration component")))
|
||||
@@ -185,7 +208,7 @@ extension P4Struct: CompilableDeclaration {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: currentNode, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: struct_declaration_node.toSourceLocation(),
|
||||
withError: "Missing function declaration component")))
|
||||
@@ -197,15 +220,7 @@ extension P4Struct: CompilableDeclaration {
|
||||
id: struct_identifier,
|
||||
withType: P4QualifiedType(
|
||||
P4Struct(withName: struct_identifier, andFields: P4StructFields([])))))
|
||||
return Result.Ok(
|
||||
(
|
||||
struc,
|
||||
context.extern_context
|
||||
? context
|
||||
: context.update(
|
||||
newTypes: context.types.declare(
|
||||
identifier: struct_identifier, withValue: struc.identifier.type.baseType()))
|
||||
))
|
||||
return Result.Ok(struc)
|
||||
}
|
||||
|
||||
var parse_errs: (any Errorable)? = .none
|
||||
@@ -217,13 +232,12 @@ extension P4Struct: CompilableDeclaration {
|
||||
switch VariableDeclarationStatement.Compile(
|
||||
node: declaration_field, withContext: current_context)
|
||||
{
|
||||
case .Ok((let declaration, let updated_context)):
|
||||
case .Ok(let declaration):
|
||||
let variable_declaration = declaration as! VariableDeclarationStatement
|
||||
parsed_fields.append(
|
||||
P4StructFieldIdentifier(
|
||||
id: variable_declaration.identifier, withType: variable_declaration.initializer.type()
|
||||
))
|
||||
current_context = updated_context
|
||||
case .Error(let e):
|
||||
parse_errs =
|
||||
if let e = parse_errs {
|
||||
@@ -245,24 +259,19 @@ extension P4Struct: CompilableDeclaration {
|
||||
withType: P4QualifiedType(
|
||||
P4Struct(
|
||||
withName: struct_identifier, andFields: P4StructFields(parsed_fields)))))
|
||||
return .Ok(
|
||||
(
|
||||
declared_struct,
|
||||
current_context.extern_context
|
||||
? current_context
|
||||
: current_context.update(
|
||||
newTypes: current_context.types.declare(
|
||||
identifier: struct_identifier, withValue: declared_struct.identifier.type.baseType()))
|
||||
))
|
||||
return .Ok(declared_struct)
|
||||
}
|
||||
}
|
||||
|
||||
extension P4Lang.Parser: CompilableDeclaration {
|
||||
extension P4Lang.Parser: Compilable {
|
||||
public typealias C = Declaration
|
||||
|
||||
public static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(Declaration, CompilerContext)?> {
|
||||
) -> Result<Declaration> {
|
||||
let parser_node = node
|
||||
#SkipUnlessNodeType<Node>(node: parser_node, type: "parserDeclaration")
|
||||
#RequireNodeType<Node, Result<Declaration>>(
|
||||
node: parser_node, type: "parserDeclaration", nice_type_name: "parser declaration")
|
||||
var current_context = context
|
||||
|
||||
var walker = Walker(node: parser_node)
|
||||
@@ -270,7 +279,7 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: parser_node.toSourceLocation(),
|
||||
withError: "Missing elements of parser declaration")))
|
||||
@@ -294,7 +303,7 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
|
||||
#MustOr(
|
||||
result: type_node_child, thing: type_node_walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: parser_node.toSourceLocation(),
|
||||
withError: "Missing elements of parser type in parser declaration")))
|
||||
@@ -310,7 +319,7 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
type_node_walker.next()
|
||||
#MustOr(
|
||||
result: type_node_child, thing: type_node_walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: type_node_child!.toSourceLocation(),
|
||||
withError: "Missing name in parser type declaration")))
|
||||
@@ -324,15 +333,14 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
type_node_walker.next()
|
||||
#MustOr(
|
||||
result: type_node_child, thing: type_node_walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: type_node_child!.toSourceLocation(),
|
||||
withError: "Missing parser parameters")))
|
||||
|
||||
switch ParameterList.Compile(node: type_node_child!, withContext: current_context) {
|
||||
case .Ok(let (parsed_parameter_list, updated_context)):
|
||||
case .Ok(let parsed_parameter_list):
|
||||
parameter_list = parsed_parameter_list
|
||||
current_context = updated_context
|
||||
case .Error(let e):
|
||||
return .Error(e)
|
||||
}
|
||||
@@ -348,7 +356,7 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: parser_node.toSourceLocation(),
|
||||
withError: "Missing parser declaration component")))
|
||||
@@ -356,7 +364,7 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: parser_node.toSourceLocation(),
|
||||
withError: "Missing elements of parser declaration")))
|
||||
@@ -371,7 +379,7 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: parser_node.toSourceLocation(),
|
||||
withError: "Missing body of parser declaration")))
|
||||
@@ -381,7 +389,7 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
}
|
||||
|
||||
/// TODO: Handle extern parsers.
|
||||
switch SpecialCompilers.Compile(
|
||||
switch SpecialCompilers.CompileParserBody(
|
||||
withName: parser_name!, withParameters: parameter_list, node: current_node!,
|
||||
withContext: current_context)
|
||||
{
|
||||
@@ -390,24 +398,21 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
TypedIdentifier(id: parser.name, withType: P4QualifiedType(parser)))
|
||||
// Create a new context with the name of the parser that was just compiled in scope.
|
||||
return .Ok(
|
||||
(
|
||||
parser_declaration,
|
||||
context.extern_context
|
||||
? context
|
||||
: updated_context.update(
|
||||
newTypes: updated_context.types.declare(identifier: parser.name, withValue: parser))
|
||||
))
|
||||
parser_declaration,
|
||||
)
|
||||
case Result.Error(let error): return .Error(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Control: CompilableDeclaration {
|
||||
extension Control: Compilable {
|
||||
public typealias C = Declaration
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(Declaration, CompilerContext)?> {
|
||||
) -> Common.Result<Declaration> {
|
||||
|
||||
#SkipUnlessNodeType<Node>(node: node, type: "control_declaration")
|
||||
#RequireNodeType<Node, Result<Declaration>>(
|
||||
node: node, type: "control_declaration", nice_type_name: "control declaration")
|
||||
|
||||
var walker = Walker(node: node)
|
||||
var current_node: Node? = .none
|
||||
@@ -417,7 +422,7 @@ extension Control: CompilableDeclaration {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing control declaration component")))
|
||||
@@ -433,18 +438,17 @@ extension Control: CompilableDeclaration {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing control declaration component")))
|
||||
|
||||
let maybe_control_parameters = ParameterList.Compile(
|
||||
node: current_node!, withContext: local_context)
|
||||
guard case .Ok((let control_parameters, let updated_context)) = maybe_control_parameters
|
||||
guard case .Ok(let control_parameters) = maybe_control_parameters
|
||||
else {
|
||||
return .Error(maybe_control_parameters.error()!)
|
||||
}
|
||||
local_context = updated_context
|
||||
|
||||
// Before continuing, make sure to put the parameters into context.
|
||||
var control_scope = local_context.instances.enter()
|
||||
@@ -459,7 +463,7 @@ extension Control: CompilableDeclaration {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
or: Result<Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing control declaration component")))
|
||||
@@ -476,12 +480,11 @@ extension Control: CompilableDeclaration {
|
||||
let maybe_action_declaration = Action.Compile(
|
||||
node: current_node, withContext: local_context)
|
||||
guard
|
||||
case .Ok((let action_declaration, let updated_context)) = maybe_action_declaration
|
||||
case .Ok(let action_declaration) = maybe_action_declaration
|
||||
else {
|
||||
return .Error(maybe_action_declaration.error()!)
|
||||
}
|
||||
actions.append(action_declaration)
|
||||
local_context = updated_context
|
||||
// Now, add the declaration into the context.
|
||||
local_context = local_context.update(
|
||||
newTypes: local_context.types.declare(
|
||||
@@ -490,22 +493,20 @@ extension Control: CompilableDeclaration {
|
||||
let maybe_table_declaration = Table.Compile(
|
||||
node: current_node, withContext: local_context)
|
||||
guard
|
||||
case .Ok((let table_declaration, let updated_context)) = maybe_table_declaration
|
||||
case .Ok(let table_declaration) = maybe_table_declaration
|
||||
else {
|
||||
return .Error(maybe_table_declaration.error()!)
|
||||
}
|
||||
tables.append(table_declaration)
|
||||
local_context = updated_context
|
||||
} else if current_node.nodeType == "apply_statement" {
|
||||
// When we see an apply, that is it for the actions and the tables.
|
||||
let maybe_apply_statement = ApplyStatement.Compile(
|
||||
node: current_node, withContext: local_context)
|
||||
guard
|
||||
case .Ok((let apply_statement, let updated_context)) = maybe_apply_statement
|
||||
case .Ok(let apply_statement) = maybe_apply_statement
|
||||
else {
|
||||
return .Error(maybe_apply_statement.error()!)
|
||||
}
|
||||
local_context = updated_context
|
||||
apply = (apply_statement as! ApplyStatement)
|
||||
|
||||
// The apply is the last thing in a control declaration.
|
||||
@@ -555,24 +556,16 @@ extension Control: CompilableDeclaration {
|
||||
|
||||
// Don't forget to add the newly declared Control to the instance that we were given
|
||||
// (and not the one that we entered to do the parsing of this Control).
|
||||
return .Ok(
|
||||
(
|
||||
declared_control,
|
||||
context.extern_context
|
||||
? context
|
||||
: context.update(
|
||||
newTypes: context.types.declare(
|
||||
identifier: control_name, withValue: control))
|
||||
))
|
||||
return .Ok(declared_control)
|
||||
}
|
||||
}
|
||||
|
||||
extension Action: Compilable {
|
||||
public typealias T = Action
|
||||
public typealias C = Action
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(Action, CompilerContext)> {
|
||||
#RequireNodeType<Node, (P4Type, CompilerContext)>(
|
||||
) -> Result<Action> {
|
||||
#RequireNodeType<Node, P4Type>(
|
||||
node: node, type: "action_declaration", nice_type_name: "Action Declaration")
|
||||
|
||||
var walker = Walker(node: node)
|
||||
@@ -584,7 +577,7 @@ extension Action: Compilable {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Action, CompilerContext)>.Error(
|
||||
or: Result<Action>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"
|
||||
))
|
||||
@@ -601,7 +594,7 @@ extension Action: Compilable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Action, CompilerContext)>.Error(
|
||||
or: Result<Action>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"
|
||||
))
|
||||
@@ -609,11 +602,10 @@ extension Action: Compilable {
|
||||
|
||||
let maybe_action_parameters = ParameterList.Compile(
|
||||
node: current_node!, withContext: current_context)
|
||||
guard case .Ok((let action_parameters, let updated_context)) = maybe_action_parameters
|
||||
guard case .Ok(let action_parameters) = maybe_action_parameters
|
||||
else {
|
||||
return .Error(maybe_action_parameters.error()!)
|
||||
}
|
||||
current_context = updated_context
|
||||
|
||||
// Check whether the parameters are in the proper order.
|
||||
let remaining_parameters = action_parameters.parameters.drop(while: {
|
||||
@@ -629,7 +621,7 @@ extension Action: Compilable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Action, CompilerContext)>.Error(
|
||||
or: Result<Action>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"
|
||||
))
|
||||
@@ -644,29 +636,26 @@ extension Action: Compilable {
|
||||
|
||||
let maybe_action_body = BlockStatement.Compile(
|
||||
node: current_node!, withContext: context.update(newInstances: function_scope))
|
||||
guard case .Ok((let action_body, _)) = maybe_action_body else {
|
||||
guard case .Ok(let action_body) = maybe_action_body else {
|
||||
return .Error(maybe_action_body.error()!)
|
||||
}
|
||||
|
||||
/// TODO: Actions cannot contain switches!
|
||||
|
||||
return .Ok(
|
||||
(
|
||||
Action(
|
||||
named: action_name, withParameters: action_parameters,
|
||||
withBody: (action_body as! BlockStatement)),
|
||||
current_context
|
||||
))
|
||||
Action(
|
||||
named: action_name, withParameters: action_parameters,
|
||||
withBody: (action_body as! BlockStatement)))
|
||||
}
|
||||
}
|
||||
|
||||
extension TableKeyEntry: Compilable {
|
||||
public typealias T = TableKeyEntry
|
||||
public typealias C = TableKeyEntry
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(TableKeyEntry, CompilerContext)> {
|
||||
) -> Result<TableKeyEntry> {
|
||||
|
||||
#RequireNodeType<Node, (P4Type, CompilerContext)>(
|
||||
#RequireNodeType<Node, TableKeyEntry>(
|
||||
node: node, type: "table_key_entry", nice_type_name: "Table Key Entry")
|
||||
|
||||
var walker = Walker(node: node)
|
||||
@@ -677,7 +666,7 @@ extension TableKeyEntry: Compilable {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(TableKeyEntry, CompilerContext)>.Error(
|
||||
or: Result<TableKeyEntry>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing table key entry declaration component")))
|
||||
@@ -693,31 +682,31 @@ extension TableKeyEntry: Compilable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(TableKeyEntry, CompilerContext)>.Error(
|
||||
or: Result<TableKeyEntry>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing table key entry declaration component")))
|
||||
|
||||
let maybe_match_type = TableKeyMatchType.Compile(
|
||||
node: current_node!, withContext: current_context)
|
||||
guard case .Ok((let match_type, _)) = maybe_match_type else {
|
||||
guard case .Ok(let match_type) = maybe_match_type else {
|
||||
return .Error(maybe_match_type.error()!)
|
||||
}
|
||||
|
||||
return .Ok((TableKeyEntry(keyset_expression as! KeysetExpression, match_type), current_context))
|
||||
return .Ok(TableKeyEntry(keyset_expression as! KeysetExpression, match_type))
|
||||
}
|
||||
}
|
||||
|
||||
extension TableKeyMatchType: Compilable {
|
||||
public typealias T = TableKeyMatchType
|
||||
public typealias C = TableKeyMatchType
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(TableKeyMatchType, CompilerContext)> {
|
||||
#RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>(
|
||||
) -> Result<TableKeyMatchType> {
|
||||
#RequireNodeType<Node, TableKeyMatchType>(
|
||||
node: node, type: "table_key_match_type", nice_type_name: "Table Key Match Type")
|
||||
|
||||
if node.text! == "exact" {
|
||||
return .Ok((TableKeyMatchType.Exact, context))
|
||||
return .Ok(TableKeyMatchType.Exact)
|
||||
}
|
||||
return .Error(
|
||||
ErrorWithLocation(
|
||||
@@ -727,11 +716,11 @@ extension TableKeyMatchType: Compilable {
|
||||
}
|
||||
|
||||
extension TableKeys: Compilable {
|
||||
public typealias T = TableKeys
|
||||
public typealias C = TableKeys
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(TableKeys, CompilerContext)> {
|
||||
#RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>(
|
||||
) -> Result<TableKeys> {
|
||||
#RequireNodeType<Node, TableKeyMatchType>(
|
||||
node: node, type: "table_keys", nice_type_name: "Table Keys")
|
||||
|
||||
var walker = Walker(node: node)
|
||||
@@ -746,7 +735,7 @@ extension TableKeys: Compilable {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(TableKeys, CompilerContext)>.Error(
|
||||
or: Result<TableKeys>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing table keys declaration component in control declaration"))
|
||||
@@ -754,7 +743,7 @@ extension TableKeys: Compilable {
|
||||
|
||||
let (keys, errors) = walker.try_map(n: node.childCount - 1, onlyNamed: true) { current_node in
|
||||
return switch TableKeyEntry.Compile(node: current_node, withContext: context) {
|
||||
case .Ok((let keyset_expression, _)): .Ok(keyset_expression)
|
||||
case .Ok(let keyset_expression): .Ok(keyset_expression)
|
||||
case .Error(let e): .Error(e)
|
||||
}
|
||||
}
|
||||
@@ -769,16 +758,16 @@ extension TableKeys: Compilable {
|
||||
}.joined(separator: ";"))))
|
||||
}
|
||||
|
||||
return .Ok((TableKeys(withEntries: keys), context))
|
||||
return .Ok(TableKeys(withEntries: keys))
|
||||
}
|
||||
}
|
||||
|
||||
extension TableActionsProperty: Compilable {
|
||||
public typealias T = TableActionsProperty
|
||||
public typealias C = TableActionsProperty
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(TableActionsProperty, CompilerContext)> {
|
||||
#RequireNodeType<Node, (TableActionsProperty, CompilerContext)>(
|
||||
) -> Result<TableActionsProperty> {
|
||||
#RequireNodeType<Node, TableActionsProperty>(
|
||||
node: node, type: "table_actions", nice_type_name: "Table Actions")
|
||||
|
||||
var walker = Walker(node: node)
|
||||
@@ -793,7 +782,7 @@ extension TableActionsProperty: Compilable {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(TableActionsProperty, CompilerContext)>.Error(
|
||||
or: Result<TableActionsProperty>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing table actions declaration component in control declaration"))
|
||||
@@ -828,16 +817,16 @@ extension TableActionsProperty: Compilable {
|
||||
}.joined(separator: ";"))))
|
||||
}
|
||||
|
||||
return .Ok((TableActionsProperty(actions), context))
|
||||
return .Ok(TableActionsProperty(actions))
|
||||
}
|
||||
}
|
||||
extension TablePropertyList: Compilable {
|
||||
public typealias T = TablePropertyList
|
||||
public typealias C = TablePropertyList
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(TablePropertyList, CompilerContext)> {
|
||||
) -> Result<TablePropertyList> {
|
||||
|
||||
#RequireNodeType<Node, (P4Type, CompilerContext)>(
|
||||
#RequireNodeType<Node, TablePropertyList>(
|
||||
node: node, type: "table_property_list", nice_type_name: "Table Property List")
|
||||
|
||||
var current_context = context
|
||||
@@ -849,15 +838,13 @@ extension TablePropertyList: Compilable {
|
||||
node.enumerateNamedChildren { child in
|
||||
if child.nodeType == "table_keys" {
|
||||
switch TableKeys.Compile(node: child, withContext: current_context) {
|
||||
case .Ok((let table_key, let updated_context)):
|
||||
current_context = updated_context
|
||||
case .Ok(let table_key):
|
||||
keys.append(table_key)
|
||||
case .Error(let e): errors.append(e)
|
||||
}
|
||||
} else if child.nodeType == "table_actions" {
|
||||
switch TableActionsProperty.Compile(node: child, withContext: current_context) {
|
||||
case .Ok((let table_action_property, let updated_context)):
|
||||
current_context = updated_context
|
||||
case .Ok(let table_action_property):
|
||||
actions.append(table_action_property)
|
||||
case .Error(let e): errors.append(e)
|
||||
}
|
||||
@@ -900,19 +887,19 @@ extension TablePropertyList: Compilable {
|
||||
actions.append(TableActionsProperty())
|
||||
}
|
||||
|
||||
return .Ok((TablePropertyList(withActions: actions[0], withKeys: keys[0]), current_context))
|
||||
return .Ok(TablePropertyList(withActions: actions[0], withKeys: keys[0]))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
extension Table: Compilable {
|
||||
public typealias T = Table
|
||||
public typealias C = Table
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(Table, CompilerContext)> {
|
||||
) -> Result<Table> {
|
||||
|
||||
let table_declaration_node = node
|
||||
#RequireNodeType<Node, (P4Type, CompilerContext)>(
|
||||
#RequireNodeType<Node, Table>(
|
||||
node: table_declaration_node, type: "table_declaration", nice_type_name: "Table Declaration")
|
||||
|
||||
var walker = Walker(node: table_declaration_node)
|
||||
@@ -924,7 +911,7 @@ extension Table: Compilable {
|
||||
walker.next() // Skip the XXX?
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Table, CompilerContext)>.Error(
|
||||
or: Result<Table>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component")
|
||||
))
|
||||
@@ -942,46 +929,43 @@ extension Table: Compilable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Table, CompilerContext)>.Error(
|
||||
or: Result<Table>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component")
|
||||
))
|
||||
|
||||
let maybe_table_property_list = TablePropertyList.Compile(
|
||||
node: current_node!, withContext: current_context)
|
||||
guard case .Ok((let table_property_list, _)) = maybe_table_property_list else {
|
||||
guard case .Ok(let table_property_list) = maybe_table_property_list else {
|
||||
return Result.Error(maybe_table_property_list.error()!)
|
||||
}
|
||||
|
||||
return .Ok(
|
||||
(Table(withName: table_name, withPropertyList: table_property_list), current_context))
|
||||
return .Ok(Table(withName: table_name, withPropertyList: table_property_list))
|
||||
}
|
||||
}
|
||||
|
||||
extension ExternDeclaration: CompilableDeclaration {
|
||||
extension ExternDeclaration: Compilable {
|
||||
public typealias C = Declaration
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(Declaration, CompilerContext)?> {
|
||||
) -> Result<Declaration> {
|
||||
let extern_declaration_node = node
|
||||
#RequireNodeType<Node, (Declaration, CompilerContext)>(
|
||||
#RequireNodeType<Node, Declaration>(
|
||||
node: extern_declaration_node, type: "extern_declaration",
|
||||
nice_type_name: "Extern Declaration")
|
||||
|
||||
let declaration_node = extern_declaration_node.child(at: 1)!
|
||||
#RequireNodeType<Node, (Declaration, CompilerContext)>(
|
||||
#RequireNodeType<Node, Declaration>(
|
||||
node: declaration_node, type: "declaration", nice_type_name: "Declaration")
|
||||
let declarationed_node = declaration_node.child(at: 0)!
|
||||
|
||||
let maybe_declared = Declaration.Compile(
|
||||
node: declarationed_node, withContext: context.update(newExtern: true))
|
||||
|
||||
guard case .Ok(let maybe_declared) = maybe_declared else {
|
||||
guard case .Ok(let declared) = maybe_declared else {
|
||||
return .Error(maybe_declared.error()!)
|
||||
}
|
||||
|
||||
guard case .some((let declared, _)) = maybe_declared else {
|
||||
return .Ok(.none)
|
||||
}
|
||||
|
||||
// Before we are okay with this declaration, it must already be registered as an extern
|
||||
// with the matching "stuff".
|
||||
|
||||
@@ -1000,11 +984,7 @@ extension ExternDeclaration: CompilableDeclaration {
|
||||
let extern_declaration = Declaration(extern: declared, ffi: found_ffi)
|
||||
|
||||
return .Ok(
|
||||
(
|
||||
extern_declaration,
|
||||
context.update(
|
||||
newExterns: context.externs.declare(
|
||||
identifier: declared.identifier, withValue: extern_declaration))
|
||||
))
|
||||
extern_declaration,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user