-
Notifications
You must be signed in to change notification settings - Fork 61
feat(compiler): support registered host-await builtins for natural function call syntax #667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ use crate::lexer::Span; | |
| use crate::rvm::instructions::{BuiltinCallParams, FunctionCallParams}; | ||
| use crate::rvm::Instruction; | ||
| use crate::utils::get_path_string; | ||
| use crate::value::Value; | ||
| use alloc::{format, string::ToString, vec::Vec}; | ||
|
|
||
| enum CallTarget { | ||
|
|
@@ -127,21 +128,50 @@ impl<'a> Compiler<'a> { | |
| self.emit_instruction(Instruction::BuiltinCall { params_index }, &span); | ||
| } | ||
| CallTarget::HostAwait { .. } => { | ||
| if arg_regs.len() != 2 { | ||
| return Err(CompilerError::General { | ||
| message: format!( | ||
| "__builtin_host_await expects 2 arguments, got {}", | ||
| arg_regs.len() | ||
| ), | ||
| let (arg_reg, id_reg) = if original_fcn_path == "__builtin_host_await" { | ||
| // Explicit __builtin_host_await(arg, id) — 2 arguments | ||
| if arg_regs.len() != 2 { | ||
| return Err(CompilerError::General { | ||
| message: format!( | ||
| "__builtin_host_await expects 2 arguments, got {}", | ||
| arg_regs.len() | ||
| ), | ||
| } | ||
| .at(&span)); | ||
| } | ||
| .at(&span)); | ||
| } | ||
| (arg_regs[0], arg_regs[1]) | ||
| } else { | ||
| // Registered host-awaitable builtin — identifier is the function name | ||
| if arg_regs.len() != 1 { | ||
| return Err(CompilerError::General { | ||
| message: format!( | ||
| "host-awaitable builtin '{}' expects exactly 1 argument, got {}", | ||
| original_fcn_path, | ||
| arg_regs.len() | ||
| ), | ||
| } | ||
| .at(&span)); | ||
| } | ||
| let id_reg = self.alloc_register(); | ||
| let literal_idx = self.add_literal(Value::String(original_fcn_path.into())); | ||
| self.emit_instruction( | ||
| Instruction::Load { | ||
| dest: id_reg, | ||
| literal_idx, | ||
| }, | ||
| &span, | ||
| ); | ||
| // HostAwait carries a single arg register; registered builtins | ||
| // are restricted to arg_count == 1 at registration time, so | ||
| // arg_regs[0] is the only argument. | ||
| (arg_regs[0], id_reg) | ||
| }; | ||
|
|
||
| self.emit_instruction( | ||
| Instruction::HostAwait { | ||
| dest, | ||
| arg: arg_regs[0], | ||
| id: arg_regs[1], | ||
| arg: arg_reg, | ||
| id: id_reg, | ||
| }, | ||
| &span, | ||
| ); | ||
|
|
@@ -192,6 +222,13 @@ impl<'a> Compiler<'a> { | |
| }); | ||
| } | ||
|
|
||
| // Check registered host-awaitable builtins | ||
| if let Some(&arg_count) = self.host_await_builtins.get(original_fcn_path) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to detect shadowing here. |
||
| return Ok(CallTarget::HostAwait { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it might be clear to distinguish between the builtin and the custom registered builtins via a new variant say RegisteredHostAwait. That way the rest of the code doesn't need to depend on the builtin's name (__builtin_host_await) to distinguish between builtin and registered. |
||
| expected_args: Some(arg_count), | ||
| }); | ||
| } | ||
|
|
||
| if self.is_user_defined_function(full_fcn_path) { | ||
| let rule_index = self.get_or_assign_rule_index(full_fcn_path)?; | ||
| let expected_args = self | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -181,8 +181,20 @@ impl<'a> Compiler<'a> { | |
| pub fn compile_from_policy( | ||
| policy: &CompiledPolicy, | ||
| entry_points: &[&str], | ||
| ) -> Result<Arc<Program>> { | ||
| Self::compile_from_policy_with_host_await(policy, entry_points, &[]) | ||
| } | ||
|
|
||
| /// Compile from a CompiledPolicy to RVM Program with registered host-awaitable builtins. | ||
| pub fn compile_from_policy_with_host_await( | ||
| policy: &CompiledPolicy, | ||
| entry_points: &[&str], | ||
| host_await_builtins: &[(&str, usize)], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scenarios to define behavior for:
|
||
| ) -> Result<Arc<Program>> { | ||
| let mut compiler = Compiler::with_policy(policy); | ||
| for &(name, arg_count) in host_await_builtins { | ||
| compiler.register_host_await_builtin(name, arg_count)?; | ||
| } | ||
| compiler.current_rule_path = "".to_string(); | ||
| let rules = policy.get_rules(); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to define behavior when registered host await builtin shadows a user write policy rule (both regular and function)