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:
@@ -42,7 +42,7 @@ public struct Parameter: CustomStringConvertible, Equatable {
|
||||
if let param_direction = self.type.direction(),
|
||||
param_direction == Direction.In || param_direction == Direction.InOut
|
||||
{
|
||||
if !(argument.argument is EvaluatableLValueExpression) {
|
||||
if !(argument.argument is P4LValueExpression) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -125,9 +125,9 @@ public struct ArgumentList {
|
||||
|
||||
public struct Argument {
|
||||
public let index: Int
|
||||
public let argument: EvaluatableExpression
|
||||
public let argument: P4Expression
|
||||
|
||||
public init(_ argument: EvaluatableExpression, atIndex index: Int) {
|
||||
public init(_ argument: P4Expression, atIndex index: Int) {
|
||||
self.argument = argument
|
||||
self.index = index
|
||||
}
|
||||
|
||||
@@ -26,3 +26,124 @@ public typealias TypeTypeScope = Scope<P4Type>
|
||||
|
||||
/// Scopes that resolve type identifiers to their types.
|
||||
public typealias TypeTypeScopes = Scopes<P4Type>
|
||||
|
||||
/// Context for compilation
|
||||
///
|
||||
/// It contains (at least) three important pieces of information:
|
||||
/// 1. Instances: A ``VarTypeScopes`` that contains information about instantiated objects
|
||||
/// (and their types) in scope
|
||||
/// 1. Types: A ``TypeTypeScopes`` that contains information about declared types in scope.
|
||||
/// 1. Expected Type: In certain situations, to typecheck an element of a P4 program requires
|
||||
/// knowledge of an expected type. For instance, when compiling a return statement, the
|
||||
/// compiler must know the return type of the function to type check.
|
||||
public struct CompilerContext {
|
||||
public let instances: StaticVarValueScopes
|
||||
public let types: TypeTypeScopes
|
||||
public let externs: TypeTypeScopes
|
||||
public let ffis: [P4FFI]
|
||||
public let expected_type: P4QualifiedType?
|
||||
public let extern_context: Bool
|
||||
|
||||
public init() {
|
||||
instances = StaticVarValueScopes().enter()
|
||||
types = TypeTypeScopes().enter()
|
||||
externs = TypeTypeScopes().enter()
|
||||
expected_type = .none
|
||||
extern_context = false
|
||||
ffis = Array()
|
||||
}
|
||||
|
||||
public init(withInstances _instances: StaticVarValueScopes, withTypes _types: TypeTypeScopes) {
|
||||
instances = _instances
|
||||
types = _types
|
||||
externs = TypeTypeScopes().enter()
|
||||
expected_type = .none
|
||||
extern_context = false
|
||||
ffis = Array()
|
||||
}
|
||||
|
||||
public init(
|
||||
withInstances _instances: StaticVarValueScopes, withTypes _types: TypeTypeScopes,
|
||||
withExpectation expectation: P4QualifiedType?, withExtern extern: Bool,
|
||||
withExterns externs: TypeTypeScopes, withFFIs foreigns: [P4FFI]
|
||||
) {
|
||||
instances = _instances
|
||||
types = _types
|
||||
expected_type = expectation
|
||||
extern_context = extern
|
||||
self.externs = externs
|
||||
ffis = foreigns
|
||||
}
|
||||
|
||||
/// Update a compiler context
|
||||
///
|
||||
/// Create a new compiler context based on the current but new instances.
|
||||
///
|
||||
/// - Parameter instances: a ``VarTypeScopes`` with the updated instances for the newly created compiler context.
|
||||
/// - Returns: A new compiler context based on the current but new instances.
|
||||
public func update(newInstances instances: StaticVarValueScopes) -> CompilerContext {
|
||||
return CompilerContext(
|
||||
withInstances: instances, withTypes: self.types, withExpectation: self.expected_type,
|
||||
withExtern: self.extern_context, withExterns: self.externs, withFFIs: self.ffis)
|
||||
}
|
||||
|
||||
/// Update a compiler context
|
||||
///
|
||||
/// Create a new compiler context based on the current but new types.
|
||||
///
|
||||
/// - Parameter types: a ``TypeTypeScopes`` with the updated types for the newly created compiler context.
|
||||
/// - Returns: A new compiler context based on the current but new types.
|
||||
public func update(newTypes types: TypeTypeScopes) -> CompilerContext {
|
||||
return CompilerContext(
|
||||
withInstances: self.instances, withTypes: types, withExpectation: self.expected_type,
|
||||
withExtern: self.extern_context, withExterns: self.externs, withFFIs: self.ffis)
|
||||
}
|
||||
|
||||
/// Update a compiler context
|
||||
///
|
||||
/// Create a new compiler context based on the current but new expected type.
|
||||
///
|
||||
/// - Parameter expectation: a ``P4Type?`` to (re)set the type the compiler is expecting.
|
||||
/// - Returns: A new compiler context based on the current but new expected type.
|
||||
public func update(newExpectation expectation: P4QualifiedType?) -> CompilerContext {
|
||||
return CompilerContext(
|
||||
withInstances: self.instances, withTypes: self.types, withExpectation: expectation,
|
||||
withExtern: self.extern_context, withExterns: self.externs, withFFIs: self.ffis)
|
||||
}
|
||||
|
||||
/// Update a compiler context
|
||||
///
|
||||
/// Create a new compiler context based on the current but new extern context value.
|
||||
///
|
||||
/// - Parameter extern: a ``Bool`` to (re)set whether the compiler is compiling in an extern context.
|
||||
/// - Returns: A new compiler context based on the current but new extern context value.
|
||||
public func update(newExtern extern: Bool) -> CompilerContext {
|
||||
return CompilerContext(
|
||||
withInstances: self.instances, withTypes: self.types, withExpectation: self.expected_type,
|
||||
withExtern: extern, withExterns: self.externs, withFFIs: self.ffis)
|
||||
}
|
||||
|
||||
/// Update a compiler context
|
||||
///
|
||||
/// Create a new compiler context based on the current but new externs.
|
||||
///
|
||||
/// - Parameter externs: a ``TypeTypeScopes`` to (re)set the list of extern-al declarations.
|
||||
/// - Returns: A new compiler context based on the current but new list of external-al declarations.
|
||||
public func update(newExterns externs: TypeTypeScopes) -> CompilerContext {
|
||||
return CompilerContext(
|
||||
withInstances: self.instances, withTypes: self.types, withExpectation: self.expected_type,
|
||||
withExtern: self.extern_context, withExterns: externs, withFFIs: self.ffis)
|
||||
}
|
||||
|
||||
/// Update a compiler context
|
||||
///
|
||||
/// Create a new compiler context based on the current but new FFIs.
|
||||
///
|
||||
/// - Parameter foreigns: an array of ``P4FFI`` to (re)set the list of foreign functions.
|
||||
/// - Returns: A new compiler context based on the current but with a new list of foreign functions.
|
||||
public func update(newFFIs foreigns: [P4FFI]) -> CompilerContext {
|
||||
return CompilerContext(
|
||||
withInstances: self.instances, withTypes: self.types, withExpectation: self.expected_type,
|
||||
withExtern: self.extern_context, withExterns: externs, withFFIs: foreigns)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,6 +109,18 @@ public struct InterloperEvaluator: ProgramExecutionEvaluator {
|
||||
|
||||
return CanonicalExecuteStatements(statements, inExecution: execution) { statement, execution in
|
||||
let (cf, value) = statement.evaluate(execution: execution)
|
||||
|
||||
let (handled_cf, handled_value) =
|
||||
if let handler = handler {
|
||||
handler(cf, value)
|
||||
} else {
|
||||
(cf, value)
|
||||
}
|
||||
if hasDebugInterloper {
|
||||
debugger!(statement, handled_cf, handled_value)
|
||||
}
|
||||
return (handled_cf, handled_value)
|
||||
/*
|
||||
let (handled_cf, handled_value) =
|
||||
if let handler = handler {
|
||||
handler(cf, value)
|
||||
@@ -119,7 +131,7 @@ public struct InterloperEvaluator: ProgramExecutionEvaluator {
|
||||
if hasDebugInterloper {
|
||||
debugger!(statement, handled_cf, handled_value)
|
||||
}
|
||||
return (handled_cf, handled_value)
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,28 +15,16 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
public protocol EvaluatableExpression {
|
||||
/// Evaluate an expression for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the expression
|
||||
/// - Returns: The value of expression
|
||||
func evaluate(execution: ProgramExecution) -> (Result<P4Value>, ProgramExecution)
|
||||
func type() -> P4QualifiedType
|
||||
}
|
||||
|
||||
public protocol EvaluatableStatement {
|
||||
/// Evaluate a statement for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the parser statement
|
||||
/// - Returns: A tuple of
|
||||
/// 1. Whether this statement affects control flow.
|
||||
/// 2. An updated execution after evaluating the parser statement
|
||||
func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution)
|
||||
}
|
||||
|
||||
public protocol P4Type: CustomStringConvertible {
|
||||
func eq(rhs: any P4Type) -> Bool
|
||||
func def() -> P4DataValue?
|
||||
func instantiable() -> Bool
|
||||
}
|
||||
|
||||
extension P4Type {
|
||||
public func instantiable() -> Bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
public protocol P4DataValue: CustomStringConvertible {
|
||||
@@ -48,28 +36,6 @@ public protocol P4DataValue: CustomStringConvertible {
|
||||
func gte(rhs: P4DataValue) -> Bool
|
||||
}
|
||||
|
||||
public protocol EvaluatableLValueExpression: EvaluatableExpression {
|
||||
func set(
|
||||
to: P4Value, inScopes scopes: VarValueScopes, duringExecution execution: ProgramExecution
|
||||
) -> Result<(VarValueScopes, P4Value)>
|
||||
func check(to: EvaluatableExpression, inScopes scopes: StaticVarValueScopes) -> Result<()>
|
||||
}
|
||||
|
||||
public protocol ProgramExecutionEvaluator {
|
||||
func ExecuteStatements(
|
||||
_ statements: [EvaluatableStatement], inExecution execution: ProgramExecution,
|
||||
_ handler: ExecuteStatementResultHandlerT?
|
||||
) -> (ControlFlow, ProgramExecution)
|
||||
|
||||
func ExecuteStatements(
|
||||
_ statements: [EvaluatableStatement], inExecution execution: ProgramExecution
|
||||
) -> (ControlFlow, ProgramExecution)
|
||||
|
||||
func EvaluateExpression(
|
||||
_ expression: EvaluatableExpression, inExecution execution: ProgramExecution,
|
||||
) -> (Result<P4Value>, ProgramExecution)
|
||||
}
|
||||
|
||||
public protocol Errorable: CustomStringConvertible {
|
||||
func format(_ formatter: Formattable) -> String
|
||||
func format(_ formatter: Formattable, _ sc: SourceCode) -> String
|
||||
@@ -91,6 +57,64 @@ public protocol Formattable {
|
||||
func formatWithStyle(_ value: String, _ style: Style) -> String
|
||||
}
|
||||
|
||||
public protocol P4Statement {
|
||||
/// Evaluate a statement for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the parser statement
|
||||
/// - Returns: A tuple of
|
||||
/// 1. Whether this statement affects control flow.
|
||||
/// 2. An updated execution after evaluating the parser statement
|
||||
func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution)
|
||||
|
||||
func effect(context: CompilerContext) -> CompilerContext
|
||||
}
|
||||
|
||||
public protocol P4Expression {
|
||||
func effect(context: CompilerContext) -> CompilerContext
|
||||
func type() -> P4QualifiedType
|
||||
/// Evaluate an expression for a given execution
|
||||
/// - Parameters
|
||||
/// - execution: The execution context in which to evaluate the expression
|
||||
/// - Returns: The value of expression
|
||||
func evaluate(execution: ProgramExecution) -> (Result<P4Value>, ProgramExecution)
|
||||
}
|
||||
|
||||
extension P4Expression {
|
||||
func effect(context: CompilerContext) -> CompilerContext {
|
||||
return context
|
||||
}
|
||||
}
|
||||
|
||||
public protocol P4LValueExpression {
|
||||
func effect(context: CompilerContext) -> CompilerContext
|
||||
func type() -> P4QualifiedType
|
||||
func check(to: P4Expression, inScopes scopes: StaticVarValueScopes) -> Result<()>
|
||||
func set(
|
||||
to: P4Value, inScopes scopes: VarValueScopes, duringExecution execution: ProgramExecution
|
||||
) -> Result<(VarValueScopes, P4Value)>
|
||||
|
||||
}
|
||||
|
||||
/// TODO: Only generate these after going through the codegen!
|
||||
public typealias EvaluatableStatement = P4Statement
|
||||
public typealias EvaluatableLValueExpression = P4LValueExpression
|
||||
public typealias EvaluatableExpression = P4Expression
|
||||
|
||||
public protocol ProgramExecutionEvaluator {
|
||||
func ExecuteStatements(
|
||||
_ statements: [EvaluatableStatement], inExecution execution: ProgramExecution,
|
||||
_ handler: ExecuteStatementResultHandlerT?
|
||||
) -> (ControlFlow, ProgramExecution)
|
||||
|
||||
func ExecuteStatements(
|
||||
_ statements: [EvaluatableStatement], inExecution execution: ProgramExecution
|
||||
) -> (ControlFlow, ProgramExecution)
|
||||
|
||||
func EvaluateExpression(
|
||||
_ expression: EvaluatableExpression, inExecution execution: ProgramExecution,
|
||||
) -> (Result<P4Value>, ProgramExecution)
|
||||
}
|
||||
|
||||
extension ProgramExecutionEvaluator {
|
||||
public func ExecuteStatements(
|
||||
_ statements: [EvaluatableStatement], inExecution execution: ProgramExecution
|
||||
|
||||
@@ -106,3 +106,6 @@ public func Fold<T, A>(input: [T], initial: A, block: (T, A) -> A) -> A {
|
||||
|
||||
@freestanding(codeItem) public macro MustOr<E, N>(result: E, thing: E?, or: N) =
|
||||
#externalMacro(module: "Macros", type: "MustOr")
|
||||
|
||||
@attached(peer) public macro DeriveCompilableStatement() =
|
||||
#externalMacro(module: "Macros", type: "DeriveCompilableStatement")
|
||||
|
||||
Reference in New Issue
Block a user