Skip to content
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

Implement q() #5

Closed
dgkf opened this issue May 30, 2023 · 0 comments · Fixed by #12
Closed

Implement q() #5

dgkf opened this issue May 30, 2023 · 0 comments · Fixed by #12
Labels
meta-good first issue Good for newcomers theme-base Function implementations type-enhancement New feature or request

Comments

@dgkf
Copy link
Owner

dgkf commented May 30, 2023

The current implementation sorely lacks an obvious way to quit. Although you can break out of the repl with Ctrl+D, anyone who's booting up a repl for the first time is likely to use q().

To implement a quit functionality, we need to first implement a new signalling condition to signal a termination. This should be added to the possible R Conditions here:

R/src/lang.rs

Lines 34 to 37 in a902965

pub enum RSignal {
Condition(Cond),
Error(RError),
}

Next we need to implement a primitive q function. Primitives are callable symbols, and the dispatch to primitive internal calls is handled in this implementation of the Callable trait:

R/src/builtins.rs

Lines 897 to 905 in a902965

impl Callable for String {
fn call(&self, args: ExprList, env: &mut Environment) -> EvalResult {
if let Some(f) = primitive(self) {
return f(args, env);
}
(env.get(self.clone())?).call(args, env)
}
}

Which then calls into the primitive function to try to find an appropriate primitive internal call if one exists:

R/src/builtins.rs

Lines 771 to 777 in a902965

pub fn primitive(name: &str) -> Option<Box<dyn Fn(ExprList, &mut Environment) -> EvalResult>> {
match name {
"c" => Some(Box::new(primitive_c)),
"list" => Some(Box::new(primitive_list)),
_ => None,
}
}

A new primitive callback for q() would need to be added. For now, I wouldn't worry about even handling any arguments. It should just return the new RSignal to terminate the session.

And finally, the last step would to respond to this termination signal in the REPL handler:

R/src/r_repl/repl.rs

Lines 54 to 58 in a902965

let res = global_env.eval(expr);
match res {
Ok(val) => println!("{}", val),
Err(e) => println!("{}", e),
}

@dgkf dgkf added the meta-good first issue Good for newcomers label May 30, 2023
@dgkf dgkf added type-enhancement New feature or request theme-base Function implementations labels May 31, 2023
@armenic armenic mentioned this issue Jun 3, 2023
@dgkf dgkf closed this as completed in #12 Jun 4, 2023
dgkf pushed a commit that referenced this issue Aug 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meta-good first issue Good for newcomers theme-base Function implementations type-enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant