use fusevm::{Chunk, VMResult, Value, VM};
use indexmap::IndexMap;
use std::cell::RefCell;
use std::rc::Rc;
pub mod ops {
pub const GETVAR: u16 = 1;
pub const GETFUN: u16 = 2;
pub const SETVAR: u16 = 3;
pub const SETSUPER: u16 = 4;
pub const MKARGS: u16 = 5;
pub const CALL: u16 = 6;
pub const MKCLOSURE: u16 = 7;
pub const CONST_DBL: u16 = 8;
pub const CONST_INT: u16 = 9;
pub const CONST_STR: u16 = 10;
pub const CONST_LGL: u16 = 11;
pub const CONST_NULL: u16 = 12;
pub const CONST_NA: u16 = 13;
pub const DOTS: u16 = 14;
pub const BINOP: u16 = 20;
pub const UNOP: u16 = 21;
pub const SPECIAL: u16 = 22;
pub const INDEX: u16 = 30;
pub const INDEX2: u16 = 31;
pub const DOLLAR: u16 = 32;
pub const INDEX_SET: u16 = 33;
pub const INDEX2_SET: u16 = 34;
pub const DOLLAR_SET: u16 = 35;
pub const REPLACE: u16 = 36;
pub const TRUTHY: u16 = 40;
pub const SIG_BREAK: u16 = 41;
pub const SIG_NEXT: u16 = 42;
pub const SIG_RETURN: u16 = 43;
pub const SEQ_LEN: u16 = 44;
pub const SEQ_ELEM: u16 = 45;
pub const AUTOPRINT: u16 = 46;
pub const IS_FALSE: u16 = 47;
pub const IS_TRUE: u16 = 48;
pub const MISSING: u16 = 49;
pub const NULL_INVISIBLE: u16 = 50;
}
pub struct EnvData {
pub vars: IndexMap<String, Value>,
pub parent: Option<Env>,
}
pub type Env = Rc<RefCell<EnvData>>;
fn new_env(parent: Option<Env>) -> Env {
Rc::new(RefCell::new(EnvData {
vars: IndexMap::new(),
parent,
}))
}
#[derive(Clone)]
pub struct ClosureDef {
pub params: Vec<String>,
pub chunk: Chunk,
}
#[derive(Debug, Clone)]
pub enum Signal {
Break,
Next,
Return(Value),
}
#[derive(Clone)]
pub struct RObj {
pub data: RData,
pub attrs: IndexMap<String, Value>,
}
#[derive(Clone)]
pub enum RData {
Null,
Lgl(Vec<Option<bool>>),
Int(Vec<Option<i64>>),
Dbl(Vec<Option<f64>>),
Str(Vec<Option<String>>),
List(Vec<Value>),
Closure {
id: usize,
env: Env,
},
Builtin(String),
Environment(Env),
Args(Vec<(Option<String>, Value)>),
}
pub fn type_rank(d: &RData) -> u8 {
match d {
RData::Null => 0,
RData::Lgl(_) => 1,
RData::Int(_) => 2,
RData::Dbl(_) => 3,
RData::Str(_) => 4,
_ => 5,
}
}
pub struct Frame {
pub env: Env,
pub args: Vec<(Option<String>, Value)>,
pub fun_name: Option<String>,
pub dispatch: Option<(String, Vec<String>)>,
}
pub struct RHost {
heap: Vec<RObj>,
pub global: Env,
pub frames: Vec<Frame>,
pub closures: Vec<ClosureDef>,
pub error: Option<String>,
pub signal: Option<Signal>,
pub visible: bool,
pub echo: bool,
null: Option<Value>,
}
thread_local! {
static HOST: RefCell<RHost> = RefCell::new(RHost::new());
}
pub fn with_host<R>(f: impl FnOnce(&mut RHost) -> R) -> R {
HOST.with(|h| f(&mut h.borrow_mut()))
}
pub fn reset_host() {
with_host(|h| *h = RHost::new());
}
impl Default for RHost {
fn default() -> Self {
Self::new()
}
}
impl RHost {
pub fn new() -> Self {
let global = new_env(None);
RHost {
heap: Vec::new(),
global: global.clone(),
frames: vec![Frame {
env: global,
args: Vec::new(),
fun_name: None,
dispatch: None,
}],
closures: Vec::new(),
error: None,
signal: None,
visible: true,
echo: true,
null: None,
}
}
pub fn load_closures(&mut self, closures: Vec<ClosureDef>) {
self.closures = closures;
}
pub fn alloc(&mut self, data: RData) -> Value {
self.alloc_with(data, IndexMap::new())
}
pub fn alloc_with(&mut self, data: RData, attrs: IndexMap<String, Value>) -> Value {
self.heap.push(RObj { data, attrs });
Value::Obj((self.heap.len() - 1) as u32)
}
pub fn get(&self, v: &Value) -> Option<&RObj> {
match v {
Value::Obj(i) => self.heap.get(*i as usize),
_ => None,
}
}
fn get_mut(&mut self, v: &Value) -> Option<&mut RObj> {
match v {
Value::Obj(i) => self.heap.get_mut(*i as usize),
_ => None,
}
}
pub fn data_of(&self, v: &Value) -> RData {
self.get(v).map(|o| o.data.clone()).unwrap_or(RData::Null)
}
pub fn attrs_of(&self, v: &Value) -> IndexMap<String, Value> {
self.get(v).map(|o| o.attrs.clone()).unwrap_or_default()
}
pub fn attr(&self, v: &Value, name: &str) -> Option<Value> {
self.get(v).and_then(|o| o.attrs.get(name).cloned())
}
pub fn set_attr(&mut self, v: &Value, name: &str, val: Value) {
let is_null = matches!(self.data_of(&val), RData::Null);
if let Some(o) = self.get_mut(v) {
if is_null {
o.attrs.shift_remove(name);
} else {
o.attrs.insert(name.to_string(), val);
}
}
}
pub fn null(&mut self) -> Value {
if let Some(v) = &self.null {
return v.clone();
}
let v = self.alloc(RData::Null);
self.null = Some(v.clone());
v
}
pub fn dbl(&mut self, xs: Vec<Option<f64>>) -> Value {
self.alloc(RData::Dbl(xs))
}
pub fn int(&mut self, xs: Vec<Option<i64>>) -> Value {
self.alloc(RData::Int(xs))
}
pub fn lgl(&mut self, xs: Vec<Option<bool>>) -> Value {
self.alloc(RData::Lgl(xs))
}
pub fn str_vec(&mut self, xs: Vec<Option<String>>) -> Value {
self.alloc(RData::Str(xs))
}
pub fn list(&mut self, xs: Vec<Value>) -> Value {
self.alloc(RData::List(xs))
}
pub fn scalar_dbl(&mut self, x: f64) -> Value {
self.dbl(vec![Some(x)])
}
pub fn scalar_int(&mut self, x: i64) -> Value {
self.int(vec![Some(x)])
}
pub fn scalar_lgl(&mut self, x: bool) -> Value {
self.lgl(vec![Some(x)])
}
pub fn scalar_str(&mut self, x: impl Into<String>) -> Value {
self.str_vec(vec![Some(x.into())])
}
pub fn length(&self, v: &Value) -> usize {
match self.get(v).map(|o| &o.data) {
Some(RData::Null) | None => 0,
Some(RData::Lgl(x)) => x.len(),
Some(RData::Int(x)) => x.len(),
Some(RData::Dbl(x)) => x.len(),
Some(RData::Str(x)) => x.len(),
Some(RData::List(x)) => x.len(),
Some(RData::Args(x)) => x.len(),
Some(RData::Environment(e)) => e.borrow().vars.len(),
_ => 1,
}
}
pub fn as_lgl(&self, v: &Value) -> Vec<Option<bool>> {
match self.get(v).map(|o| &o.data) {
Some(RData::Lgl(x)) => x.clone(),
Some(RData::Int(x)) => x.iter().map(|e| e.map(|n| n != 0)).collect(),
Some(RData::Dbl(x)) => x.iter().map(|e| e.map(|n| n != 0.0)).collect(),
Some(RData::Str(x)) => x
.iter()
.map(|e| match e.as_deref() {
Some("TRUE") | Some("true") | Some("T") => Some(true),
Some("FALSE") | Some("false") | Some("F") => Some(false),
_ => None,
})
.collect(),
Some(RData::List(x)) => x.iter().flat_map(|e| self.as_lgl(e)).collect(),
_ => Vec::new(),
}
}
pub fn as_int(&self, v: &Value) -> Vec<Option<i64>> {
match self.get(v).map(|o| &o.data) {
Some(RData::Lgl(x)) => x.iter().map(|e| e.map(|b| b as i64)).collect(),
Some(RData::Int(x)) => x.clone(),
Some(RData::Dbl(x)) => x
.iter()
.map(|e| e.and_then(|n| n.is_finite().then_some(n.trunc() as i64)))
.collect(),
Some(RData::Str(x)) => x
.iter()
.map(|e| e.as_ref().and_then(|s| s.trim().parse::<f64>().ok()))
.map(|o| o.map(|n| n.trunc() as i64))
.collect(),
Some(RData::List(x)) => x.iter().flat_map(|e| self.as_int(e)).collect(),
_ => Vec::new(),
}
}
pub fn as_dbl(&self, v: &Value) -> Vec<Option<f64>> {
match self.get(v).map(|o| &o.data) {
Some(RData::Lgl(x)) => x.iter().map(|e| e.map(|b| b as i64 as f64)).collect(),
Some(RData::Int(x)) => x.iter().map(|e| e.map(|n| n as f64)).collect(),
Some(RData::Dbl(x)) => x.clone(),
Some(RData::Str(x)) => x
.iter()
.map(|e| e.as_ref().and_then(|s| parse_num(s.trim())))
.collect(),
Some(RData::List(x)) => x.iter().flat_map(|e| self.as_dbl(e)).collect(),
_ => Vec::new(),
}
}
pub fn as_str(&self, v: &Value) -> Vec<Option<String>> {
match self.get(v).map(|o| &o.data) {
Some(RData::Lgl(x)) => x
.iter()
.map(|e| e.map(|b| if b { "TRUE" } else { "FALSE" }.to_string()))
.collect(),
Some(RData::Int(x)) => x.iter().map(|e| e.map(|n| n.to_string())).collect(),
Some(RData::Dbl(x)) => x.iter().map(|e| e.map(format_dbl)).collect(),
Some(RData::Str(x)) => x.clone(),
Some(RData::List(x)) => x.iter().flat_map(|e| self.as_str(e)).collect(),
_ => Vec::new(),
}
}
pub fn str1(&self, v: &Value) -> Option<String> {
self.as_str(v).into_iter().next().flatten()
}
pub fn num1(&self, v: &Value) -> Option<f64> {
self.as_dbl(v).into_iter().next().flatten()
}
pub fn lgl1(&self, v: &Value) -> Option<bool> {
self.as_lgl(v).into_iter().next().flatten()
}
pub fn elements(&mut self, v: &Value) -> Vec<Value> {
match self.data_of(v) {
RData::List(xs) => xs,
RData::Null => Vec::new(),
_ => (0..self.length(v)).map(|i| self.element_at(v, i)).collect(),
}
}
pub fn element_at(&mut self, v: &Value, i: usize) -> Value {
match self.data_of(v) {
RData::Lgl(x) => {
let e = x.get(i).cloned().flatten();
self.lgl(vec![e])
}
RData::Int(x) => {
let e = x.get(i).cloned().flatten();
self.int(vec![e])
}
RData::Dbl(x) => {
let e = x.get(i).cloned().flatten();
self.dbl(vec![e])
}
RData::Str(x) => {
let e = x.get(i).cloned().flatten();
self.str_vec(vec![e])
}
RData::List(x) => x.get(i).cloned().unwrap_or_else(|| {
let n = self.null.clone();
n.unwrap_or(Value::Undef)
}),
_ => self.null(),
}
}
pub fn names(&self, v: &Value) -> Vec<Option<String>> {
match self.attr(v, "names") {
Some(n) => self.as_str(&n),
None => Vec::new(),
}
}
pub fn is_null(&self, v: &Value) -> bool {
matches!(self.get(v).map(|o| &o.data), Some(RData::Null) | None)
}
pub fn is_function(&self, v: &Value) -> bool {
matches!(
self.get(v).map(|o| &o.data),
Some(RData::Closure { .. }) | Some(RData::Builtin(_))
)
}
pub fn class_of(&self, v: &Value) -> Vec<String> {
if let Some(c) = self.attr(v, "class") {
let cs: Vec<String> = self.as_str(&c).into_iter().flatten().collect();
if !cs.is_empty() {
return cs;
}
}
if self.attr(v, "dim").map(|d| self.length(&d)) == Some(2) {
return vec!["matrix".into(), "array".into()];
}
vec![match self.get(v).map(|o| &o.data) {
Some(RData::Null) | None => "NULL",
Some(RData::Lgl(_)) => "logical",
Some(RData::Int(_)) => "integer",
Some(RData::Dbl(_)) => "numeric",
Some(RData::Str(_)) => "character",
Some(RData::List(_)) => "list",
Some(RData::Closure { .. }) | Some(RData::Builtin(_)) => "function",
Some(RData::Environment(_)) => "environment",
Some(RData::Args(_)) => "list",
}
.to_string()]
}
pub fn type_of(&self, v: &Value) -> &'static str {
match self.get(v).map(|o| &o.data) {
Some(RData::Null) | None => "NULL",
Some(RData::Lgl(_)) => "logical",
Some(RData::Int(_)) => "integer",
Some(RData::Dbl(_)) => "double",
Some(RData::Str(_)) => "character",
Some(RData::List(_)) | Some(RData::Args(_)) => "list",
Some(RData::Closure { .. }) => "closure",
Some(RData::Builtin(_)) => "builtin",
Some(RData::Environment(_)) => "environment",
}
}
pub fn env(&self) -> Env {
self.frames
.last()
.map(|f| f.env.clone())
.unwrap_or_else(|| self.global.clone())
}
pub fn lookup(&self, name: &str) -> Option<Value> {
let mut e = Some(self.env());
while let Some(cur) = e {
if let Some(v) = cur.borrow().vars.get(name) {
return Some(v.clone());
}
e = cur.borrow().parent.clone();
}
None
}
pub fn lookup_function(&self, name: &str) -> Option<Value> {
let mut e = Some(self.env());
while let Some(cur) = e {
let hit = cur.borrow().vars.get(name).cloned();
if let Some(v) = hit {
if self.is_function(&v) {
return Some(v);
}
}
e = cur.borrow().parent.clone();
}
None
}
pub fn set_var(&mut self, name: &str, val: Value) {
self.env().borrow_mut().vars.insert(name.to_string(), val);
}
pub fn set_super(&mut self, name: &str, val: Value) {
let mut e = self.env().borrow().parent.clone();
while let Some(cur) = e {
let has = cur.borrow().vars.contains_key(name);
if has {
cur.borrow_mut().vars.insert(name.to_string(), val);
return;
}
e = cur.borrow().parent.clone();
}
self.global.borrow_mut().vars.insert(name.to_string(), val);
}
pub fn exists(&self, name: &str) -> bool {
self.lookup(name).is_some()
}
pub fn dots(&self) -> Vec<(Option<String>, Value)> {
match self.lookup("...").map(|v| self.data_of(&v)) {
Some(RData::Args(a)) => a,
_ => Vec::new(),
}
}
pub fn fail<T>(&mut self, msg: impl Into<String>) -> Option<T> {
if self.error.is_none() {
self.error = Some(msg.into());
}
None
}
pub fn take_error(&mut self) -> Option<String> {
self.error.take()
}
}
fn parse_num(s: &str) -> Option<f64> {
match s {
"Inf" => Some(f64::INFINITY),
"-Inf" => Some(f64::NEG_INFINITY),
"NaN" => Some(f64::NAN),
_ => s.parse().ok(),
}
}
pub fn fixed_decimals(x: f64) -> usize {
if !x.is_finite() || (x == x.trunc() && x.abs() < 1e15) {
return 0;
}
let mag = x.abs().log10().floor() as i32;
let d = (6 - mag).clamp(0, 15) as usize;
let s = format!("{x:.d$}");
let trimmed = s.trim_end_matches('0');
match trimmed.split_once('.') {
Some((_, frac)) => frac.len(),
None => 0,
}
}
pub fn sci_decimals(x: f64) -> usize {
if !x.is_finite() || x == 0.0 {
return 0;
}
let mag = x.abs().log10().floor() as i32;
let mant = x / 10f64.powi(mag);
let s = format!("{mant:.6}");
let trimmed = s.trim_end_matches('0');
match trimmed.split_once('.') {
Some((_, frac)) => frac.len(),
None => 0,
}
}
pub fn render_fixed(x: f64, decimals: usize) -> String {
if x.is_nan() {
return "NaN".into();
}
if x.is_infinite() {
return if x > 0.0 { "Inf" } else { "-Inf" }.into();
}
format!("{x:.decimals$}")
}
pub fn render_sci(x: f64, decimals: usize) -> String {
if !x.is_finite() {
return render_fixed(x, 0);
}
if x == 0.0 {
return format!("{:.*}e+00", decimals, 0.0);
}
let mut mag = x.abs().log10().floor() as i32;
let mut mant = x / 10f64.powi(mag);
if format!("{mant:.decimals$}")
.trim_start_matches('-')
.starts_with("10")
{
mant /= 10.0;
mag += 1;
}
format!(
"{mant:.decimals$}e{}{:02}",
if mag < 0 { '-' } else { '+' },
mag.abs()
)
}
pub fn format_dbl(x: f64) -> String {
let fixed = render_fixed(x, fixed_decimals(x));
if !x.is_finite() {
return fixed;
}
let sci = render_sci(x, sci_decimals(x));
if sci.chars().count() < fixed.chars().count() {
sci
} else {
fixed
}
}
pub fn run_chunk(chunk: Chunk) -> Result<Value, String> {
let mut vm = VM::new(chunk);
crate::builtins::install(&mut vm);
vm.enable_tracing_jit();
let outcome = vm.run();
if let Some(e) = with_host(|h| h.take_error()) {
return Err(e);
}
match outcome {
VMResult::Ok(v) => Ok(v),
VMResult::Halted => Ok(vm.stack.last().cloned().unwrap_or(Value::Undef)),
VMResult::Error(e) => Err(e),
}
}
pub fn run_main(chunk: Chunk) -> Result<Value, String> {
let r = run_chunk(chunk);
with_host(|h| h.signal = None);
r
}
pub fn call_value(
f: &Value,
args: Vec<(Option<String>, Value)>,
fun_name: Option<String>,
) -> Result<Value, String> {
match with_host(|h| h.data_of(f)) {
RData::Builtin(name) => crate::builtins::call_primitive(&name, args),
RData::Closure { id, env } => call_closure(id, env, args, fun_name),
_ => Err("attempt to apply non-function".into()),
}
}
pub const MAX_DEPTH: usize = 5000;
pub fn call_closure(
id: usize,
env: Env,
args: Vec<(Option<String>, Value)>,
fun_name: Option<String>,
) -> Result<Value, String> {
let def = with_host(|h| h.closures.get(id).cloned())
.ok_or_else(|| format!("internal: unknown closure #{id}"))?;
if with_host(|h| h.frames.len()) >= MAX_DEPTH {
return Err("evaluation nested too deeply: infinite recursion?".into());
}
let frame_env = new_env(Some(env));
let bindings = match_args(&def.params, &args)?;
{
let mut e = frame_env.borrow_mut();
for (k, v) in bindings {
e.vars.insert(k, v);
}
}
with_host(|h| {
h.frames.push(Frame {
env: frame_env,
args: args.clone(),
fun_name,
dispatch: None,
})
});
let out = run_chunk(def.chunk.clone());
with_host(|h| {
h.frames.pop();
});
let out = out?;
let sig = with_host(|h| h.signal.take());
match sig {
Some(Signal::Return(v)) => Ok(v),
Some(other) => {
with_host(|h| h.signal = Some(other));
Ok(out)
}
None => Ok(out),
}
}
pub fn match_args(
params: &[String],
args: &[(Option<String>, Value)],
) -> Result<Vec<(String, Value)>, String> {
let mut bound: Vec<Option<Value>> = vec![None; params.len()];
let mut used = vec![false; args.len()];
let dots_at = params.iter().position(|p| p == "...");
for (ai, (tag, val)) in args.iter().enumerate() {
let Some(tag) = tag else { continue };
if let Some(pi) = params.iter().position(|p| p == tag) {
if bound[pi].is_some() {
return Err(format!(
"formal argument \"{tag}\" matched by multiple actual arguments"
));
}
bound[pi] = Some(val.clone());
used[ai] = true;
}
}
let partial_limit = dots_at.unwrap_or(params.len());
for (ai, (tag, val)) in args.iter().enumerate() {
if used[ai] {
continue;
}
let Some(tag) = tag else { continue };
let hits: Vec<usize> = (0..partial_limit)
.filter(|&pi| bound[pi].is_none() && params[pi].starts_with(tag.as_str()))
.collect();
match hits.len() {
0 => {}
1 => {
bound[hits[0]] = Some(val.clone());
used[ai] = true;
}
_ => return Err(format!("argument {tag} matches multiple formal arguments")),
}
}
let mut pi = 0usize;
for (ai, (tag, val)) in args.iter().enumerate() {
if used[ai] || tag.is_some() {
continue;
}
while pi < params.len() && (bound[pi].is_some() || params[pi] == "...") {
if params[pi] == "..." {
break;
}
pi += 1;
}
if pi >= params.len() || params[pi] == "..." {
break;
}
bound[pi] = Some(val.clone());
used[ai] = true;
pi += 1;
}
let mut out = Vec::new();
for (i, p) in params.iter().enumerate() {
if p == "..." {
continue;
}
if let Some(v) = &bound[i] {
out.push((p.clone(), v.clone()));
}
}
let rest: Vec<(Option<String>, Value)> = args
.iter()
.enumerate()
.filter(|(i, _)| !used[*i])
.map(|(_, a)| a.clone())
.collect();
if dots_at.is_some() {
let dots = with_host(|h| h.alloc(RData::Args(rest)));
out.push(("...".to_string(), dots));
} else if let Some((tag, _)) = rest.first() {
return Err(match tag {
Some(t) => format!("unused argument ({t} = ...)"),
None => "unused arguments".to_string(),
});
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn format_dbl_matches_r_seven_significant_digits() {
assert_eq!(format_dbl(1.0), "1");
assert_eq!(format_dbl(1.5), "1.5");
assert_eq!(format_dbl(1.0 / 3.0), "0.3333333");
assert_eq!(format_dbl(123.456), "123.456");
assert_eq!(format_dbl(f64::INFINITY), "Inf");
assert_eq!(format_dbl(f64::NAN), "NaN");
}
#[test]
fn exact_tags_beat_position() {
reset_host();
let (a, b) = with_host(|h| (h.scalar_dbl(1.0), h.scalar_dbl(2.0)));
let bound = match_args(
&["x".into(), "y".into()],
&[(Some("y".into()), a.clone()), (None, b.clone())],
)
.unwrap();
assert_eq!(bound[0].0, "x");
assert_eq!(bound[0].1, b);
assert_eq!(bound[1].0, "y");
assert_eq!(bound[1].1, a);
}
#[test]
fn partial_tags_match_unique_prefixes_only() {
reset_host();
let v = with_host(|h| h.scalar_dbl(1.0));
let ok = match_args(&["verbose".into()], &[(Some("verb".into()), v.clone())]).unwrap();
assert_eq!(ok[0].0, "verbose");
let ambiguous = match_args(
&["value".into(), "verbose".into()],
&[(Some("v".into()), v.clone())],
);
assert!(ambiguous.is_err());
}
#[test]
fn extra_arguments_need_dots() {
reset_host();
let v = with_host(|h| h.scalar_dbl(1.0));
assert!(match_args(&["x".into()], &[(None, v.clone()), (None, v.clone())]).is_err());
let with_dots =
match_args(&["x".into(), "...".into()], &[(None, v.clone()), (None, v)]).unwrap();
assert_eq!(with_dots.len(), 2);
assert_eq!(with_dots[1].0, "...");
}
}