compiler, runtime, common: (Initial) Support For extern Declarations

Especially FFI

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-04-23 06:07:07 -04:00
parent 74fead1eba
commit f2bd53ce5f
17 changed files with 693 additions and 247 deletions
+30 -8
View File
@@ -477,33 +477,55 @@ extension FunctionCall: EvaluatableExpression {
execution: Common.ProgramExecution
) -> (Common.Result<P4Value>, ProgramExecution) {
guard let body = self.callee.body else {
return (
.Error(Error(withMessage: "No body for called function (\(self.callee.name))")), execution
)
let body_params:
Result<((ProgramExecution) -> (ControlFlow, ProgramExecution), ParameterList)> =
switch self.callee {
case (.some(let callee), .none):
switch callee.body {
case .some(let body):
.Ok(
(
{ (execution: ProgramExecution) -> (ControlFlow, ProgramExecution) in
return body.evaluate(execution: execution)
}, callee.params
))
case .none: .Error(Error(withMessage: "No body for called function (\(callee.name))"))
}
case (.none, .some(let callee)):
.Ok(
(
{ (execution: ProgramExecution) -> (ControlFlow, ProgramExecution) in
return callee.execute(execution: execution)
}, callee.parameters()
))
default: .Error(Error(withMessage: "No callee found for function call"))
}
guard case .Ok(let body) = body_params else {
return (.Error(body_params.error()!), execution)
}
let call_body: (ProgramExecution) -> (Result<P4Value>, ProgramExecution) = {
(execution: ProgramExecution) in
let (control_flow, updated_execution) = body.evaluate(execution: execution)
let (control_flow, updated_execution) = body.0(execution)
return switch control_flow {
case ControlFlow.Return(.some(let value)): (.Ok(value), updated_execution)
default:
(
.Error(
Error(withMessage: "No value returned from called function (\(self.callee.name))")),
Error(withMessage: "No value returned from called function")),
execution
)
}
}
return Call(
body: call_body, withArguments: self.arguments, withParameters: self.callee.params,
body: call_body, withArguments: self.arguments, withParameters: body.1,
inExecution: execution)
}
public func type() -> P4Type {
return self.callee.tipe
return P4Type(self.return_type)
}
}
+5 -12
View File
@@ -45,11 +45,8 @@ extension ParserStateDirectTransition: EvaluatableParserState {
) -> (any EvaluatableParserState, Common.ProgramExecution) {
var program = program.enter_scope()
let (control_flow, next_execution) = program.evaluator.ExecuteStatement(
statements,
handleResult: { (control_flow, execution) in
return (control_flow, execution)
}, inExecution: program)
let (control_flow, next_execution) = program.evaluator.ExecuteStatements(
statements, inExecution: program)
switch control_flow {
case .Next: program = next_execution
@@ -106,12 +103,8 @@ extension ParserStateSelectTransition: EvaluatableParserState {
) -> (any EvaluatableParserState, Common.ProgramExecution) {
var program = program.enter_scope()
let (control_flow, next_execution) = program.evaluator.ExecuteStatement(
statements,
handleResult: { (control_flow, execution) in
return (control_flow, execution)
}, inExecution: program)
let (control_flow, next_execution) = program.evaluator.ExecuteStatements(
statements, inExecution: program)
switch control_flow {
case .Next: program = next_execution
case .Error: return (reject, next_execution.exit_scope())
@@ -151,7 +144,7 @@ extension ParserStateSelectTransition: EvaluatableParserState {
extension Parser: LibraryCallable {
public typealias T = InstantiatedParserState
public func call(
execution: Common.ProgramExecution, arguments: P4Lang.ArgumentList
execution: Common.ProgramExecution, arguments: ArgumentList
) -> (P4Lang.InstantiatedParserState, Common.ProgramExecution) {
var execution = execution.enter_scope()