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:
@@ -23,10 +23,10 @@ import TreeSitterExtensions
|
||||
import TreeSitterP4
|
||||
|
||||
extension Statement: Compilable {
|
||||
public typealias T = EvaluatableStatement
|
||||
public typealias C = P4Statement
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(EvaluatableStatement, CompilerContext)> {
|
||||
) -> Result<P4Statement> {
|
||||
|
||||
if node.nodeType != "parserStatement" && node.nodeType != "statement" {
|
||||
return Result.Error(
|
||||
@@ -51,9 +51,9 @@ extension Statement: Compilable {
|
||||
withError:
|
||||
"Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))"))
|
||||
}
|
||||
switch parser.Compile(node: statement, withContext: context) {
|
||||
case Result.Ok(let (parsed, updated_context)):
|
||||
return .Ok((parsed, updated_context))
|
||||
switch parser.CompileStatement(node: statement, withContext: context) {
|
||||
case Result.Ok(let parsed):
|
||||
return .Ok(parsed)
|
||||
case Result.Error(let e):
|
||||
return .Error(
|
||||
ErrorWithLocation(
|
||||
@@ -62,11 +62,12 @@ extension Statement: Compilable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension LocalElements: Compilable {
|
||||
public typealias T = EvaluatableStatement
|
||||
public typealias C = P4Statement
|
||||
public static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(EvaluatableStatement, CompilerContext)> {
|
||||
) -> Result<P4Statement> {
|
||||
let localElementsParsers: [String: CompilableStatement.Type] = [
|
||||
"variableDeclaration": VariableDeclarationStatement.self
|
||||
]
|
||||
@@ -78,9 +79,9 @@ extension LocalElements: Compilable {
|
||||
withError: "Unparseable statement type (\(node.nodeType ?? "Unknown Statement Type"))"))
|
||||
}
|
||||
|
||||
switch parser.Compile(node: node, withContext: context) {
|
||||
case Result.Ok(let (parsed, parsed_updated_scopes)):
|
||||
return Result.Ok((parsed, parsed_updated_scopes))
|
||||
switch parser.CompileStatement(node: node, withContext: context) {
|
||||
case Result.Ok(let parsed):
|
||||
return Result.Ok(parsed)
|
||||
case Result.Error(let e):
|
||||
return Result.Error(Error(withMessage: "Failed to parse local element: \(e)"))
|
||||
}
|
||||
@@ -88,10 +89,10 @@ extension LocalElements: Compilable {
|
||||
}
|
||||
|
||||
extension ParserState: Compilable {
|
||||
public typealias T = ParserState
|
||||
public typealias C = ParserState
|
||||
public static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(ParserState, CompilerContext)> {
|
||||
) -> Result<ParserState> {
|
||||
var walker = Walker(node: node)
|
||||
|
||||
var current_node: Node? = .none
|
||||
@@ -107,7 +108,7 @@ extension ParserState: Compilable {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(ParserState, CompilerContext)>.Error(
|
||||
or: Result<ParserState>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing elements in parser state declaration")))
|
||||
@@ -125,7 +126,7 @@ extension ParserState: Compilable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(ParserState, CompilerContext)>.Error(
|
||||
or: Result<ParserState>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing elements in parser state declaration")))
|
||||
@@ -141,22 +142,20 @@ extension ParserState: Compilable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(ParserState, CompilerContext)>.Error(
|
||||
or: Result<ParserState>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Missing body of state declaration")
|
||||
))
|
||||
|
||||
var errors: (any Errorable)? = .none
|
||||
var current_context = context
|
||||
var parsed_s: [EvaluatableStatement] = Array()
|
||||
var parsed_s: [P4Statement] = Array()
|
||||
|
||||
if current_node!.nodeType == "parserStatements" {
|
||||
switch SpecialCompilers.Statements.Compile(
|
||||
node: current_node!, withContext: current_context)
|
||||
node: current_node!, withContext: context)
|
||||
{
|
||||
case .Ok(let (state_statements, updated_context)):
|
||||
case .Ok(let state_statements):
|
||||
parsed_s = state_statements
|
||||
current_context = updated_context
|
||||
case .Error(let error):
|
||||
errors =
|
||||
if let errors = errors {
|
||||
@@ -174,13 +173,13 @@ extension ParserState: Compilable {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(ParserState, CompilerContext)>.Error(
|
||||
or: Result<ParserState>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing transition statement of state declaration")))
|
||||
|
||||
return SpecialCompilers.TransitionStatement.Compile(
|
||||
node: current_node!, forState: state_identifier, withStatements: parsed_s,
|
||||
withContext: current_context)
|
||||
withContext: SpecialCompilers.Statements.effect(statements: parsed_s, context: context))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user