feat: stash
This commit is contained in:
@@ -1,7 +1,21 @@
|
||||
use rustyline::error::ReadlineError;
|
||||
use rustyline::{DefaultEditor, Result};
|
||||
|
||||
use nixjit::*;
|
||||
use nixjit::compile::compile;
|
||||
use nixjit::ir::downgrade;
|
||||
use nixjit::vm::run;
|
||||
|
||||
macro_rules! unwrap {
|
||||
($e:expr) => {
|
||||
match $e {
|
||||
Ok(ok) => ok,
|
||||
Err(err) => {
|
||||
println!("{err}");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut rl = DefaultEditor::new()?;
|
||||
@@ -12,8 +26,11 @@ fn main() -> Result<()> {
|
||||
if expr.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let prog = compile(expr.as_str()).unwrap();
|
||||
println!("{}", run(prog).unwrap());
|
||||
let downgraded = unwrap!(downgrade(
|
||||
rnix::Root::parse(expr.as_str()).tree().expr().unwrap()
|
||||
));
|
||||
let prog = compile(downgraded);
|
||||
println!("{}", unwrap!(run(prog)));
|
||||
rl.add_history_entry(expr.as_str())?;
|
||||
}
|
||||
Err(ReadlineError::Interrupted) => {
|
||||
@@ -1,8 +1,7 @@
|
||||
use crate::bytecode::*;
|
||||
use crate::ir;
|
||||
use crate::ty::internal::Const;
|
||||
|
||||
use super::ir;
|
||||
|
||||
pub struct Compiler {
|
||||
opcodes: Vec<OpCode>,
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
mod compile;
|
||||
mod ir;
|
||||
|
||||
pub fn compile(expr: &str) -> anyhow::Result<crate::bytecode::Program> {
|
||||
let root = rnix::Root::parse(expr);
|
||||
if !root.errors().is_empty() {
|
||||
return Err(anyhow::anyhow!(
|
||||
root.errors().iter().map(|err| err.to_string()).join(";")
|
||||
));
|
||||
}
|
||||
assert!(root.errors().is_empty());
|
||||
let expr = root.tree().expr().unwrap();
|
||||
let ir = ir::downgrade(expr)?;
|
||||
Ok(compile::compile(ir))
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
pub trait Downcast<T: Sized>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
fn downcast_ref(&self) -> Option<&T>;
|
||||
fn downcast_mut(&mut self) -> Option<&mut T>;
|
||||
fn downcast(self) -> Result<T, Self>;
|
||||
}
|
||||
13
src/error.rs
Normal file
13
src/error.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use thiserror::Error;
|
||||
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("error occurred during downgrade stage: {0}")]
|
||||
DowngradeError(String),
|
||||
#[error("error occurred during evaluation stage: {0}")]
|
||||
EvalError(String),
|
||||
#[error("unknown error")]
|
||||
Unknown,
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use ecow::EcoString;
|
||||
use rnix::ast::{self, Expr};
|
||||
|
||||
use crate::bytecode::{ConstIdx, Consts, ThunkIdx};
|
||||
use crate::compile::*;
|
||||
use crate::error::*;
|
||||
use crate::ty::internal as i;
|
||||
|
||||
use super::compile::*;
|
||||
|
||||
pub fn downgrade(expr: Expr) -> Result<Downgraded> {
|
||||
let mut state = DowngradeState::new();
|
||||
let ir = expr.downgrade(&mut state)?;
|
||||
@@ -19,7 +18,15 @@ pub fn downgrade(expr: Expr) -> Result<Downgraded> {
|
||||
})
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
trait Downcast<T: Sized>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
fn downcast_ref(&self) -> Option<&T>;
|
||||
fn downcast_mut(&mut self) -> Option<&mut T>;
|
||||
fn downcast(self) -> core::result::Result<T, Self>;
|
||||
}
|
||||
|
||||
macro_rules! ir {
|
||||
(
|
||||
$(
|
||||
@@ -30,8 +37,6 @@ macro_rules! ir {
|
||||
)
|
||||
,*$(,)?
|
||||
) => {
|
||||
use crate::downcast::Downcast;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Ir {
|
||||
$(
|
||||
@@ -73,16 +78,6 @@ macro_rules! ir {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Ir> for $ty {
|
||||
type Error = anyhow::Error;
|
||||
fn try_from(value: Ir) -> Result<Self> {
|
||||
match value {
|
||||
Ir::$ty(value) => Ok(value),
|
||||
_ => Err(anyhow!("")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Downcast<$ty> for Ir {
|
||||
fn downcast_ref(&self) -> Option<&$ty> {
|
||||
match self {
|
||||
@@ -132,12 +127,6 @@ ir! {
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct DynamicAttrPair(pub Ir, pub Ir);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DowngradeError {
|
||||
errno: u16,
|
||||
text: String,
|
||||
}
|
||||
|
||||
pub struct DowngradeState {
|
||||
thunks: Vec<Ir>,
|
||||
consts: Vec<i::Const>,
|
||||
@@ -180,7 +169,9 @@ impl Attrs {
|
||||
.get_mut(&ident)
|
||||
.unwrap()
|
||||
.downcast_mut()
|
||||
.ok_or(anyhow!(r#""{ident}" already exsists in this set"#))
|
||||
.ok_or(Error::DowngradeError(format!(
|
||||
r#""{ident}" already exsists in this set"#
|
||||
)))
|
||||
.and_then(|attrs: &mut Attrs| attrs._insert(path, name, value))
|
||||
} else {
|
||||
let mut attrs = Attrs {
|
||||
@@ -218,7 +209,9 @@ impl Attrs {
|
||||
match name {
|
||||
Attr::Str(ident) => {
|
||||
if self.stcs.get(&ident).is_some() {
|
||||
return Err(anyhow!(r#""{ident}" already exsists in this set"#));
|
||||
return Err(Error::DowngradeError(format!(
|
||||
r#""{ident}" already exsists in this set"#
|
||||
)));
|
||||
}
|
||||
self.stcs.insert(ident, value);
|
||||
}
|
||||
@@ -349,7 +342,7 @@ impl Downgrade for Expr {
|
||||
match self {
|
||||
Expr::Apply(apply) => apply.downgrade(state),
|
||||
Expr::Assert(assert) => assert.downgrade(state),
|
||||
Expr::Error(error) => return Err(anyhow!(error.to_string())),
|
||||
Expr::Error(error) => return Err(Error::DowngradeError(error.to_string())),
|
||||
Expr::IfElse(ifelse) => ifelse.downgrade(state),
|
||||
Expr::Select(select) => select.downgrade(state),
|
||||
Expr::Str(str) => str.downgrade(state),
|
||||
@@ -675,7 +668,11 @@ fn downgrade_inherit(
|
||||
for attr in inherit.attrs() {
|
||||
let ident: EcoString = match downgrade_attr(attr, state)? {
|
||||
Attr::Str(ident) => ident.to_string().into(),
|
||||
_ => return Err(anyhow!("dynamic attributes not allowed in inherit")),
|
||||
_ => {
|
||||
return Err(Error::DowngradeError(
|
||||
"dynamic attributes not allowed in inherit".to_string(),
|
||||
));
|
||||
}
|
||||
};
|
||||
let expr = from.map_or_else(
|
||||
|| Var { sym: ident.clone() }.ir().ok(),
|
||||
@@ -694,29 +691,31 @@ fn downgrade_inherit(
|
||||
}
|
||||
|
||||
fn downgrade_attr(attr: ast::Attr, state: &mut DowngradeState) -> Result<Attr> {
|
||||
use ast::Attr::*;
|
||||
use ast::InterpolPart::*;
|
||||
match attr {
|
||||
ast::Attr::Ident(ident) => Ok(Attr::Str(ident.to_string().into())),
|
||||
ast::Attr::Str(string) => {
|
||||
Ident(ident) => Ok(Attr::Str(ident.to_string().into())),
|
||||
Str(string) => {
|
||||
let parts = string.normalized_parts();
|
||||
if parts.len() == 1 {
|
||||
let ast::InterpolPart::Literal(ident) = parts.into_iter().next().unwrap() else {
|
||||
unreachable!()
|
||||
};
|
||||
Ok(Attr::Str(ident.into()))
|
||||
match parts.into_iter().next().unwrap() {
|
||||
Literal(ident) => Ok(Attr::Str(ident.into())),
|
||||
Interpolation(interpol) => {
|
||||
Ok(Attr::Dynamic(interpol.expr().unwrap().downgrade(state)?))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let parts = parts
|
||||
.into_iter()
|
||||
.map(|part| match part {
|
||||
ast::InterpolPart::Literal(lit) => Const { value: lit.into() }.ir().ok(),
|
||||
ast::InterpolPart::Interpolation(interpol) => {
|
||||
interpol.expr().unwrap().downgrade(state)
|
||||
}
|
||||
Literal(lit) => Const { value: lit.into() }.ir().ok(),
|
||||
Interpolation(interpol) => interpol.expr().unwrap().downgrade(state),
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
Ok(Attr::Strs(ConcatStrings { parts }))
|
||||
}
|
||||
}
|
||||
ast::Attr::Dynamic(dynamic) => Ok(Attr::Dynamic(dynamic.expr().unwrap().downgrade(state)?)),
|
||||
Dynamic(dynamic) => Ok(Attr::Dynamic(dynamic.expr().unwrap().downgrade(state)?)),
|
||||
}
|
||||
}
|
||||
|
||||
5
src/jit/codegen.rs
Normal file
5
src/jit/codegen.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
use super::JITContext;
|
||||
|
||||
pub trait CodeGen {
|
||||
fn codegen(self, ctx: JITContext);
|
||||
}
|
||||
@@ -1,21 +1,27 @@
|
||||
use inkwell::OptimizationLevel;
|
||||
use inkwell::builder::Builder;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::module::Module;
|
||||
use inkwell::OptimizationLevel;
|
||||
use inkwell::execution_engine::ExecutionEngine;
|
||||
use inkwell::module::Module;
|
||||
|
||||
pub struct JIT<'ctx> {
|
||||
mod codegen;
|
||||
|
||||
pub use codegen::CodeGen;
|
||||
|
||||
pub struct JITContext<'ctx> {
|
||||
context: &'ctx Context,
|
||||
module: Module<'ctx>,
|
||||
builder: Builder<'ctx>,
|
||||
builder: Builder<'ctx>,
|
||||
execution_engine: ExecutionEngine<'ctx>,
|
||||
}
|
||||
|
||||
impl<'ctx> JIT<'ctx> {
|
||||
pub fn new(context: &Context) -> JIT {
|
||||
impl<'ctx> JITContext<'ctx> {
|
||||
pub fn new(context: &Context) -> JITContext {
|
||||
let module = context.create_module("nixjit");
|
||||
JIT {
|
||||
execution_engine: module.create_jit_execution_engine(OptimizationLevel::None).unwrap(),
|
||||
JITContext {
|
||||
execution_engine: module
|
||||
.create_jit_execution_engine(OptimizationLevel::None)
|
||||
.unwrap(),
|
||||
builder: context.create_builder(),
|
||||
context,
|
||||
module,
|
||||
|
||||
14
src/lib.rs
14
src/lib.rs
@@ -2,12 +2,14 @@
|
||||
|
||||
mod builtins;
|
||||
mod bytecode;
|
||||
mod compile;
|
||||
mod downcast;
|
||||
mod ty;
|
||||
mod vm;
|
||||
mod jit;
|
||||
|
||||
pub use compile::compile;
|
||||
pub mod compile;
|
||||
pub mod error;
|
||||
pub mod ir;
|
||||
#[cfg(feature = "jit")]
|
||||
pub mod jit;
|
||||
#[cfg(feature = "vm")]
|
||||
pub mod vm;
|
||||
|
||||
pub use ty::public::Value;
|
||||
pub use vm::run;
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
use std::cell::RefCell;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use derive_more::Constructor;
|
||||
use rpds::HashTrieMapSync;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::vm::VM;
|
||||
|
||||
use super::super::common::Symbol;
|
||||
use super::super::public as p;
|
||||
|
||||
use super::{ToPublic, Value};
|
||||
use crate::vm::VM;
|
||||
|
||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||
pub struct AttrSet {
|
||||
@@ -28,7 +28,7 @@ impl AttrSet {
|
||||
}
|
||||
|
||||
pub fn push_attr(&mut self, sym: Symbol, val: Value) {
|
||||
if self.data.get(&sym).is_some() {
|
||||
if let Some(_) = self.data.get_mut(&sym) {
|
||||
todo!()
|
||||
}
|
||||
self.data.insert_mut(sym, val);
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
use anyhow::Error;
|
||||
use derive_more::{IsVariant, Unwrap};
|
||||
|
||||
use ecow::EcoString;
|
||||
|
||||
#[derive(Debug, Clone, IsVariant, Unwrap)]
|
||||
@@ -47,49 +45,6 @@ impl From<&str> for Const {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a Const> for &'a bool {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &'a Const) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
Const::Bool(b) => Ok(b),
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a> TryFrom<&'a Const> for &'a i64 {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &'a Const) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
Const::Int(int) => Ok(int),
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a Const> for &'a f64 {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &'a Const) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
Const::Float(float) => Ok(float),
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a Const> for &'a str {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &'a Const) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
Const::String(string) => Ok(string),
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Const {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
use Const::*;
|
||||
|
||||
@@ -3,8 +3,8 @@ use itertools::Itertools;
|
||||
use rpds::HashTrieMap;
|
||||
|
||||
use crate::bytecode::OpCodes;
|
||||
use crate::error::Result;
|
||||
use crate::ty::internal::{Thunk, Value};
|
||||
|
||||
use crate::vm::{CapturedEnv, VM};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -33,7 +33,7 @@ impl Func {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call(self, vm: &VM, arg: Value) -> Value {
|
||||
pub fn call(self, vm: &VM, arg: Value) -> Result<Value> {
|
||||
use Param::*;
|
||||
|
||||
let env = self.env.released();
|
||||
@@ -71,7 +71,7 @@ impl Func {
|
||||
}
|
||||
}
|
||||
|
||||
vm.eval(self.opcodes, env).unwrap()
|
||||
vm.eval(self.opcodes, env)
|
||||
}
|
||||
|
||||
pub fn push_ident_param(&mut self, param: EcoString) {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use anyhow::Result;
|
||||
use derive_more::Constructor;
|
||||
use rpds::VectorSync;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::ty::public as p;
|
||||
use crate::vm::VM;
|
||||
|
||||
use super::{ToPublic, Value};
|
||||
use crate::vm::VM;
|
||||
|
||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||
pub struct List {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use std::cell::RefCell;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use derive_more::{IsVariant, Unwrap};
|
||||
|
||||
use super::common as c;
|
||||
@@ -11,6 +10,7 @@ use super::public as p;
|
||||
use c::Symbol;
|
||||
|
||||
use crate::bytecode::OpCodes;
|
||||
use crate::error::*;
|
||||
use crate::vm::{Env, VM};
|
||||
|
||||
mod attrset;
|
||||
@@ -65,9 +65,10 @@ impl Thunk {
|
||||
match &*self.thunk.borrow() {
|
||||
_Thunk::Value(value) => return Ok(value.as_ref().clone()),
|
||||
_Thunk::SuspendedFrom(from) => {
|
||||
return Err(anyhow!(
|
||||
"already suspended from {from:p} (infinite recursion encountered)"
|
||||
));
|
||||
return Err(Error::EvalError(format!(
|
||||
"thunk {:p} already suspended from {from:p} (infinite recursion encountered)",
|
||||
self as *const Thunk
|
||||
)));
|
||||
}
|
||||
_Thunk::Code(_) => (),
|
||||
}
|
||||
@@ -77,9 +78,7 @@ impl Thunk {
|
||||
_Thunk::SuspendedFrom(self as *const Thunk),
|
||||
)
|
||||
.unwrap_code();
|
||||
let value = vm
|
||||
.eval(opcodes, self.env.borrow().clone().unwrap())
|
||||
.unwrap();
|
||||
let value = vm.eval(opcodes, self.env.borrow().clone().unwrap())?;
|
||||
let _ = std::mem::replace(
|
||||
&mut *self.thunk.borrow_mut(),
|
||||
_Thunk::Value(value.clone().into()),
|
||||
@@ -199,9 +198,9 @@ impl Value {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call(self, vm: &VM, args: Vec<Value>) -> Value {
|
||||
pub fn call(self, vm: &VM, args: Vec<Value>) -> Result<Value> {
|
||||
use Value::*;
|
||||
match self {
|
||||
Ok(match self {
|
||||
PrimOp(func) => func.call(vm, args),
|
||||
PartialPrimOp(func) => func.call(vm, args),
|
||||
mut func @ Value::Func(_) => {
|
||||
@@ -209,12 +208,12 @@ impl Value {
|
||||
while let Some(arg) = iter.next() {
|
||||
func = match func {
|
||||
PrimOp(func) => {
|
||||
return func.call(vm, [arg].into_iter().chain(iter).collect());
|
||||
return Ok(func.call(vm, [arg].into_iter().chain(iter).collect()));
|
||||
}
|
||||
PartialPrimOp(func) => {
|
||||
return func.call(vm, [arg].into_iter().chain(iter).collect());
|
||||
return Ok(func.call(vm, [arg].into_iter().chain(iter).collect()));
|
||||
}
|
||||
Func(func) => func.call(vm, arg),
|
||||
Func(func) => func.call(vm, arg)?,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
@@ -222,7 +221,7 @@ impl Value {
|
||||
}
|
||||
x @ Catchable(_) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn not(self) -> Value {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use std::fmt::{Display, Formatter, Result as FmtResult};
|
||||
|
||||
use anyhow::Error;
|
||||
use derive_more::{IsVariant, Unwrap};
|
||||
use ecow::EcoString;
|
||||
|
||||
use crate::error::Error;
|
||||
|
||||
use super::super::internal as i;
|
||||
|
||||
#[derive(Debug, Clone, IsVariant, Unwrap)]
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use std::mem::{MaybeUninit, size_of, transmute};
|
||||
use std::mem::{MaybeUninit, replace, size_of, transmute};
|
||||
use std::ops::Deref;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
|
||||
use crate::error::*;
|
||||
use crate::ty::internal::Value;
|
||||
|
||||
pub const STACK_SIZE: usize = 8 * 1024 / size_of::<Value>();
|
||||
@@ -12,6 +11,12 @@ pub struct Stack<const CAP: usize> {
|
||||
top: usize,
|
||||
}
|
||||
|
||||
macro_rules! into {
|
||||
($e:expr) => {
|
||||
unsafe { transmute($e) }
|
||||
};
|
||||
}
|
||||
|
||||
impl<const CAP: usize> Stack<CAP> {
|
||||
pub fn new() -> Self {
|
||||
Stack {
|
||||
@@ -27,51 +32,34 @@ impl<const CAP: usize> Stack<CAP> {
|
||||
pub fn push(&mut self, item: Value) -> Result<()> {
|
||||
self.items
|
||||
.get_mut(self.top)
|
||||
.map_or(Err(anyhow!("stack overflow")), |ok| Ok(ok))?
|
||||
.map_or(Err(Error::EvalError("stack overflow".to_string())), |ok| {
|
||||
Ok(ok)
|
||||
})?
|
||||
.write(item);
|
||||
self.top += 1;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn pop(&mut self) -> Result<Value> {
|
||||
pub fn pop(&mut self) -> Value {
|
||||
self.top -= 1;
|
||||
let item = self
|
||||
.items
|
||||
.get_mut(self.top)
|
||||
.map_or(Err(anyhow!("stack empty")), |ok| Ok(ok))?;
|
||||
unsafe { Ok(std::mem::replace(item, MaybeUninit::uninit()).assume_init()) }
|
||||
let item = self.items.get_mut(self.top).unwrap();
|
||||
|
||||
unsafe { replace(item, MaybeUninit::uninit()).assume_init() }
|
||||
}
|
||||
|
||||
pub fn tos(&self) -> Result<&Value> {
|
||||
if self.top == 0 {
|
||||
Err(anyhow!(""))
|
||||
panic!("stack empty")
|
||||
} else {
|
||||
unsafe { Ok(transmute(self.items.get(self.top - 1).unwrap())) }
|
||||
Ok(into!(&self.items[self.top - 1]))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tos_mut(&mut self) -> Result<&mut Value> {
|
||||
if self.top == 0 {
|
||||
Err(anyhow!(""))
|
||||
panic!("stack empty")
|
||||
} else {
|
||||
unsafe { Ok(transmute(self.items.get_mut(self.top - 1).unwrap())) }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_tos(&self, func: impl Fn(&Value)) -> Result<()> {
|
||||
if self.top != 0 {
|
||||
Err(anyhow!(""))
|
||||
} else {
|
||||
unsafe { func(transmute(self.items.get(self.top - 1).unwrap())) }
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub fn with_tos_mut(&mut self, func: impl Fn(&mut Value)) -> Result<()> {
|
||||
if self.top != 0 {
|
||||
Err(anyhow!(""))
|
||||
} else {
|
||||
unsafe { func(transmute(self.items.get_mut(self.top - 1).unwrap())) }
|
||||
Ok(())
|
||||
Ok(into!(&mut self.items[self.top - 1]))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,7 +67,7 @@ impl<const CAP: usize> Stack<CAP> {
|
||||
impl<const CAP: usize> Deref for Stack<CAP> {
|
||||
type Target = [Value];
|
||||
fn deref(&self) -> &Self::Target {
|
||||
unsafe { transmute(&self.items[0..self.top]) }
|
||||
into!(&self.items[0..self.top])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ use ecow::EcoString;
|
||||
use rpds::{ht_map_sync, vector_sync};
|
||||
|
||||
use crate::compile::compile;
|
||||
use crate::ir::downgrade;
|
||||
use crate::ty::common::Symbol;
|
||||
use crate::ty::public::*;
|
||||
|
||||
@@ -9,7 +10,8 @@ use super::vm::run;
|
||||
|
||||
#[inline]
|
||||
fn test_expr(expr: &str, expected: Value) {
|
||||
let prog = compile(expr).unwrap();
|
||||
let downgraded = downgrade(rnix::Root::parse(expr).tree().expr().unwrap()).unwrap();
|
||||
let prog = compile(downgraded);
|
||||
dbg!(&prog);
|
||||
assert_eq!(run(prog).unwrap(), expected);
|
||||
}
|
||||
|
||||
55
src/vm/vm.rs
55
src/vm/vm.rs
@@ -1,9 +1,8 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
|
||||
use crate::builtins::env;
|
||||
use crate::bytecode::{self, BinOp, OpCode, OpCodes, Program, Thunks, UnOp};
|
||||
use crate::error::*;
|
||||
use crate::ty::common::Symbol;
|
||||
use crate::ty::internal::*;
|
||||
use crate::ty::public as p;
|
||||
@@ -44,10 +43,8 @@ impl VM {
|
||||
}
|
||||
assert_eq!(stack.len(), 1);
|
||||
let mut ret = stack.pop();
|
||||
if let Ok(ref mut value) = ret {
|
||||
value.force(self)?;
|
||||
}
|
||||
ret
|
||||
ret.force(self)?;
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -58,7 +55,7 @@ impl VM {
|
||||
env: Arc<Env>,
|
||||
) -> Result<usize> {
|
||||
match opcode {
|
||||
OpCode::Illegal => return Err(anyhow!("illegal opcode")),
|
||||
OpCode::Illegal => panic!("illegal opcode"),
|
||||
OpCode::Const { value } => stack.push(Value::Const(value))?,
|
||||
OpCode::LoadThunk { idx } => {
|
||||
self.thunks[idx].capture(env);
|
||||
@@ -72,23 +69,23 @@ impl VM {
|
||||
}
|
||||
OpCode::Jmp { step } => return Ok(step),
|
||||
OpCode::JmpIfTrue { step } => {
|
||||
if let Value::Const(Const::Bool(true)) = stack.pop()? {
|
||||
if let Value::Const(Const::Bool(true)) = stack.pop() {
|
||||
return Ok(step);
|
||||
}
|
||||
}
|
||||
OpCode::JmpIfFalse { step } => {
|
||||
if let Value::Const(Const::Bool(false)) = stack.pop()? {
|
||||
if let Value::Const(Const::Bool(false)) = stack.pop() {
|
||||
return Ok(step);
|
||||
}
|
||||
}
|
||||
OpCode::Call { arity } => {
|
||||
let mut args = Vec::with_capacity(arity);
|
||||
for _ in 0..arity {
|
||||
args.insert(0, stack.pop()?);
|
||||
args.insert(0, stack.pop());
|
||||
}
|
||||
let mut func = stack.pop()?;
|
||||
let mut func = stack.pop();
|
||||
func.force(self)?;
|
||||
stack.push(func.call(self, args))?;
|
||||
stack.push(func.call(self, args)?)?;
|
||||
}
|
||||
OpCode::Func { idx } => {
|
||||
stack.push(Value::Func(Func::new(
|
||||
@@ -125,7 +122,7 @@ impl VM {
|
||||
}
|
||||
OpCode::UnOp { op } => {
|
||||
use UnOp::*;
|
||||
let mut value = stack.pop()?;
|
||||
let mut value = stack.pop();
|
||||
value.force(self)?;
|
||||
stack.push(match op {
|
||||
Not => value.not(),
|
||||
@@ -133,8 +130,8 @@ impl VM {
|
||||
}
|
||||
OpCode::BinOp { op } => {
|
||||
use BinOp::*;
|
||||
let mut rhs = stack.pop()?;
|
||||
let mut lhs = stack.pop()?;
|
||||
let mut rhs = stack.pop();
|
||||
let mut lhs = stack.pop();
|
||||
lhs.force(self)?;
|
||||
rhs.force(self)?;
|
||||
stack.push(match op {
|
||||
@@ -147,7 +144,7 @@ impl VM {
|
||||
})?;
|
||||
}
|
||||
OpCode::ConcatString => {
|
||||
let mut rhs = stack.pop()?;
|
||||
let mut rhs = stack.pop();
|
||||
rhs.force(self)?;
|
||||
stack.tos_mut()?.concat_string(rhs);
|
||||
}
|
||||
@@ -158,7 +155,7 @@ impl VM {
|
||||
stack.push(Value::List(List::empty()))?;
|
||||
}
|
||||
OpCode::PushElem => {
|
||||
let elem = stack.pop()?;
|
||||
let elem = stack.pop();
|
||||
stack.tos_mut()?.push(elem);
|
||||
}
|
||||
OpCode::AttrSet => {
|
||||
@@ -169,13 +166,13 @@ impl VM {
|
||||
stack.push(Value::RecAttrSet(RecAttrSet::new(new)))?;
|
||||
}
|
||||
OpCode::PushStaticAttr { name } => {
|
||||
let val = stack.pop()?;
|
||||
let val = stack.pop();
|
||||
stack.tos_mut()?.push_attr(Symbol::new(name), val);
|
||||
}
|
||||
OpCode::PushDynamicAttr => {
|
||||
let val = stack.pop()?;
|
||||
let mut sym = stack.pop().unwrap();
|
||||
sym.coerce_to_string();
|
||||
let val = stack.pop();
|
||||
let mut sym = stack.pop();
|
||||
sym.force(self)?.coerce_to_string();
|
||||
let sym = sym.unwrap_const().unwrap_string().into();
|
||||
stack.tos_mut()?.push_attr(sym, val);
|
||||
}
|
||||
@@ -183,22 +180,22 @@ impl VM {
|
||||
stack.tos_mut()?.force(self)?.select(Symbol::new(sym));
|
||||
}
|
||||
OpCode::SelectOrDefault { sym } => {
|
||||
let default = stack.pop()?;
|
||||
let default = stack.pop();
|
||||
stack
|
||||
.tos_mut()?
|
||||
.force(self)?
|
||||
.select_with_default(Symbol::new(sym), default);
|
||||
}
|
||||
OpCode::SelectDynamic => {
|
||||
let mut val = stack.pop().unwrap();
|
||||
let mut val = stack.pop();
|
||||
val.force(self)?;
|
||||
val.coerce_to_string();
|
||||
let sym = val.unwrap_const().unwrap_string().into();
|
||||
stack.tos_mut()?.force(self)?.select(sym);
|
||||
}
|
||||
OpCode::SelectDynamicOrDefault => {
|
||||
let default = stack.pop()?;
|
||||
let mut val = stack.pop().unwrap();
|
||||
let default = stack.pop();
|
||||
let mut val = stack.pop();
|
||||
val.force(self)?;
|
||||
val.coerce_to_string();
|
||||
let sym = val.unwrap_const().unwrap_string().into();
|
||||
@@ -211,7 +208,7 @@ impl VM {
|
||||
stack.tos_mut()?.force(self)?.has_attr(Symbol::new(sym));
|
||||
}
|
||||
OpCode::HasDynamicAttr => {
|
||||
let mut val = stack.pop().unwrap();
|
||||
let mut val = stack.pop();
|
||||
val.coerce_to_string();
|
||||
let sym = val.unwrap_const().unwrap_string().into();
|
||||
stack.tos_mut()?.force(self)?.has_attr(sym);
|
||||
@@ -219,10 +216,10 @@ impl VM {
|
||||
OpCode::LookUp { sym } => {
|
||||
stack.push(
|
||||
env.lookup(Symbol::new(sym.clone()))
|
||||
.ok_or(anyhow!(r#""{sym}" not found"#))?,
|
||||
.ok_or(Error::EvalError(format!(r#""{sym}" not found"#)))?,
|
||||
)?;
|
||||
}
|
||||
OpCode::EnterEnv => match stack.pop()? {
|
||||
OpCode::EnterEnv => match stack.pop() {
|
||||
Value::AttrSet(attrs) => env.enter(attrs.into_inner()),
|
||||
Value::RecAttrSet(attrs) => env.enter(attrs.into_inner()),
|
||||
_ => unreachable!(),
|
||||
@@ -231,7 +228,7 @@ impl VM {
|
||||
env.leave();
|
||||
}
|
||||
OpCode::Assert => {
|
||||
if !stack.pop()?.unwrap_const().unwrap_bool() {
|
||||
if !stack.pop().unwrap_const().unwrap_bool() {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user