chore: fmt
This commit is contained in:
@@ -4,17 +4,7 @@ use crate::ty::internal::{Const, Func};
|
|||||||
|
|
||||||
type Slice<T> = Box<[T]>;
|
type Slice<T> = Box<[T]>;
|
||||||
|
|
||||||
pub type ThunkIdx = usize;
|
|
||||||
pub type FuncIdx = usize;
|
|
||||||
pub type OpCodes = Slice<OpCode>;
|
pub type OpCodes = Slice<OpCode>;
|
||||||
pub type Consts = Slice<Const>;
|
|
||||||
pub type Thunks = Slice<Thunk>;
|
|
||||||
pub type Funcs = Slice<Func>;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Thunk {
|
|
||||||
pub opcodes: OpCodes,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum OpCode {
|
pub enum OpCode {
|
||||||
@@ -23,9 +13,9 @@ pub enum OpCode {
|
|||||||
/// load a dynamic var onto stack
|
/// load a dynamic var onto stack
|
||||||
LookUp { sym: EcoString },
|
LookUp { sym: EcoString },
|
||||||
/// load a thunk lazily onto stack
|
/// load a thunk lazily onto stack
|
||||||
LoadThunk { idx: ThunkIdx },
|
LoadThunk { idx: usize },
|
||||||
/// load a thunk onto stack and force its value
|
/// load a thunk onto stack and force its value
|
||||||
LoadValue { idx: ThunkIdx },
|
LoadValue { idx: usize },
|
||||||
/// force TOS to value
|
/// force TOS to value
|
||||||
ForceValue,
|
ForceValue,
|
||||||
|
|
||||||
@@ -33,7 +23,7 @@ pub enum OpCode {
|
|||||||
/// Example: __add 1 2 => [ LookUp("__add") Const(1) Const(2) Call(2) ]
|
/// Example: __add 1 2 => [ LookUp("__add") Const(1) Const(2) Call(2) ]
|
||||||
Call { arity: usize },
|
Call { arity: usize },
|
||||||
/// make a function
|
/// make a function
|
||||||
Func { idx: ThunkIdx },
|
Func { idx: usize },
|
||||||
|
|
||||||
/// consume 1 element, assert TOS is true
|
/// consume 1 element, assert TOS is true
|
||||||
Assert,
|
Assert,
|
||||||
@@ -107,6 +97,6 @@ pub enum UnOp {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Program {
|
pub struct Program {
|
||||||
pub top_level: OpCodes,
|
pub top_level: OpCodes,
|
||||||
pub thunks: Thunks,
|
pub thunks: Slice<OpCodes>,
|
||||||
pub funcs: Box<[Func]>
|
pub funcs: Slice<Func>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,9 +13,7 @@ pub fn compile(downgraded: ir::Downgraded) -> Program {
|
|||||||
thunks: downgraded
|
thunks: downgraded
|
||||||
.thunks
|
.thunks
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|thunk| Thunk {
|
.map(|thunk| Compiler::new().compile(thunk))
|
||||||
opcodes: Compiler::new().compile(thunk),
|
|
||||||
})
|
|
||||||
.collect(),
|
.collect(),
|
||||||
funcs: downgraded
|
funcs: downgraded
|
||||||
.funcs
|
.funcs
|
||||||
@@ -23,9 +21,9 @@ pub fn compile(downgraded: ir::Downgraded) -> Program {
|
|||||||
.map(|func| Func {
|
.map(|func| Func {
|
||||||
env: None,
|
env: None,
|
||||||
param: func.param.into(),
|
param: func.param.into(),
|
||||||
opcodes: Compiler::new().compile(*func.body)
|
opcodes: Compiler::new().compile(*func.body),
|
||||||
})
|
})
|
||||||
.collect()
|
.collect(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
21
src/ir.rs
21
src/ir.rs
@@ -3,11 +3,10 @@ use std::collections::HashMap;
|
|||||||
use ecow::EcoString;
|
use ecow::EcoString;
|
||||||
use rnix::ast::{self, Expr};
|
use rnix::ast::{self, Expr};
|
||||||
|
|
||||||
use crate::bytecode::{Consts, ThunkIdx, FuncIdx};
|
|
||||||
use crate::compile::*;
|
use crate::compile::*;
|
||||||
|
use crate::error::*;
|
||||||
#[cfg(feature = "jit")]
|
#[cfg(feature = "jit")]
|
||||||
use crate::jit::*;
|
use crate::jit::*;
|
||||||
use crate::error::*;
|
|
||||||
use crate::ty::internal as i;
|
use crate::ty::internal as i;
|
||||||
|
|
||||||
pub fn downgrade(expr: Expr) -> Result<Downgraded> {
|
pub fn downgrade(expr: Expr) -> Result<Downgraded> {
|
||||||
@@ -17,7 +16,7 @@ pub fn downgrade(expr: Expr) -> Result<Downgraded> {
|
|||||||
top_level: ir,
|
top_level: ir,
|
||||||
consts: ctx.consts.into(),
|
consts: ctx.consts.into(),
|
||||||
thunks: ctx.thunks.into(),
|
thunks: ctx.thunks.into(),
|
||||||
funcs: ctx.funcs.into()
|
funcs: ctx.funcs.into(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +114,7 @@ ir! {
|
|||||||
UnOp => { rhs: Box<Ir>, kind: UnOpKind },
|
UnOp => { rhs: Box<Ir>, kind: UnOpKind },
|
||||||
Select => { expr: Box<Ir>, attrpath: Vec<Attr>, default: Option<Box<Ir>> },
|
Select => { expr: Box<Ir>, attrpath: Vec<Attr>, default: Option<Box<Ir>> },
|
||||||
If => { cond: Box<Ir>, consq: Box<Ir>, alter: Box<Ir> },
|
If => { cond: Box<Ir>, consq: Box<Ir>, alter: Box<Ir> },
|
||||||
LoadFunc => { idx: FuncIdx },
|
LoadFunc => { idx: usize },
|
||||||
Call => { func: Box<Ir>, args: Vec<Ir> },
|
Call => { func: Box<Ir>, args: Vec<Ir> },
|
||||||
|
|
||||||
Let => { attrs: Attrs, expr: Box<Ir> },
|
Let => { attrs: Attrs, expr: Box<Ir> },
|
||||||
@@ -125,7 +124,7 @@ ir! {
|
|||||||
Const => { value: i::Const },
|
Const => { value: i::Const },
|
||||||
Var => { sym: EcoString },
|
Var => { sym: EcoString },
|
||||||
#[derive(Copy)]
|
#[derive(Copy)]
|
||||||
Thunk => { idx: ThunkIdx },
|
Thunk => { idx: usize },
|
||||||
Path => { expr: Box<Ir> },
|
Path => { expr: Box<Ir> },
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,9 +140,9 @@ pub struct DowngradeContext {
|
|||||||
|
|
||||||
pub struct Downgraded {
|
pub struct Downgraded {
|
||||||
pub top_level: Ir,
|
pub top_level: Ir,
|
||||||
pub consts: Consts,
|
pub consts: Box<[i::Const]>,
|
||||||
pub thunks: Box<[Ir]>,
|
pub thunks: Box<[Ir]>,
|
||||||
pub funcs: Box<[Func]>
|
pub funcs: Box<[Func]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DowngradeContext {
|
impl DowngradeContext {
|
||||||
@@ -174,9 +173,11 @@ impl Attrs {
|
|||||||
.get_mut(&ident)
|
.get_mut(&ident)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.downcast_mut()
|
.downcast_mut()
|
||||||
.ok_or_else(|| Error::DowngradeError(format!(
|
.ok_or_else(|| {
|
||||||
r#""{ident}" already exsists in this set"#
|
Error::DowngradeError(format!(
|
||||||
)))
|
r#""{ident}" already exsists in this set"#
|
||||||
|
))
|
||||||
|
})
|
||||||
.and_then(|attrs: &mut Attrs| attrs._insert(path, name, value))
|
.and_then(|attrs: &mut Attrs| attrs._insert(path, name, value))
|
||||||
} else {
|
} else {
|
||||||
let mut attrs = Attrs {
|
let mut attrs = Attrs {
|
||||||
|
|||||||
@@ -2,17 +2,17 @@ use ecow::EcoString;
|
|||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use rpds::HashTrieMap;
|
use rpds::HashTrieMap;
|
||||||
|
|
||||||
use crate::bytecode::{OpCodes, ThunkIdx};
|
use crate::bytecode::OpCodes;
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
|
use crate::ir;
|
||||||
use crate::ty::internal::Value;
|
use crate::ty::internal::Value;
|
||||||
use crate::vm::{CapturedEnv, VM};
|
use crate::vm::{CapturedEnv, VM};
|
||||||
use crate::ir;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Param {
|
pub enum Param {
|
||||||
Ident(EcoString),
|
Ident(EcoString),
|
||||||
Formals {
|
Formals {
|
||||||
formals: Vec<(EcoString, Option<ThunkIdx>)>,
|
formals: Vec<(EcoString, Option<usize>)>,
|
||||||
ellipsis: bool,
|
ellipsis: bool,
|
||||||
alias: Option<EcoString>,
|
alias: Option<EcoString>,
|
||||||
},
|
},
|
||||||
@@ -22,8 +22,18 @@ impl From<ir::Param> for Param {
|
|||||||
fn from(value: ir::Param) -> Self {
|
fn from(value: ir::Param) -> Self {
|
||||||
match value {
|
match value {
|
||||||
ir::Param::Ident(ident) => Param::Ident(ident),
|
ir::Param::Ident(ident) => Param::Ident(ident),
|
||||||
ir::Param::Formals { formals, ellipsis, alias } =>
|
ir::Param::Formals {
|
||||||
Param::Formals { formals: formals.into_iter().map(|(sym, default)| (sym, default.map(|default| default.idx))).collect(), ellipsis, alias }
|
formals,
|
||||||
|
ellipsis,
|
||||||
|
alias,
|
||||||
|
} => Param::Formals {
|
||||||
|
formals: formals
|
||||||
|
.into_iter()
|
||||||
|
.map(|(sym, default)| (sym, default.map(|default| default.idx)))
|
||||||
|
.collect(),
|
||||||
|
ellipsis,
|
||||||
|
alias,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,7 +42,7 @@ impl From<ir::Param> for Param {
|
|||||||
pub struct Func {
|
pub struct Func {
|
||||||
pub env: Option<CapturedEnv>,
|
pub env: Option<CapturedEnv>,
|
||||||
pub param: Param,
|
pub param: Param,
|
||||||
pub opcodes: OpCodes
|
pub opcodes: OpCodes,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Func {
|
impl Func {
|
||||||
|
|||||||
@@ -362,18 +362,14 @@ impl Value {
|
|||||||
|
|
||||||
pub fn select(&mut self, sym: Symbol) -> &mut Self {
|
pub fn select(&mut self, sym: Symbol) -> &mut Self {
|
||||||
if let Value::AttrSet(attrs) = self {
|
if let Value::AttrSet(attrs) = self {
|
||||||
let val = attrs
|
let val = attrs.select(sym.clone()).unwrap_or_else(|| {
|
||||||
.select(sym.clone())
|
Value::Catchable(c::Catchable::new(Some(format!("{sym:?} not found"))))
|
||||||
.unwrap_or_else(|| Value::Catchable(c::Catchable::new(Some(format!(
|
});
|
||||||
"{sym:?} not found"
|
|
||||||
)))));
|
|
||||||
*self = val;
|
*self = val;
|
||||||
} else if let Value::RecAttrSet(attrs) = self {
|
} else if let Value::RecAttrSet(attrs) = self {
|
||||||
let val = attrs
|
let val = attrs.select(sym.clone()).unwrap_or_else(|| {
|
||||||
.select(sym.clone())
|
Value::Catchable(c::Catchable::new(Some(format!("{sym:?} not found"))))
|
||||||
.unwrap_or_else(|| Value::Catchable(c::Catchable::new(Some(format!(
|
});
|
||||||
"{sym:?} not found"
|
|
||||||
)))));
|
|
||||||
*self = val;
|
*self = val;
|
||||||
} else {
|
} else {
|
||||||
*self = Value::Catchable(Catchable::new(Some(format!(
|
*self = Value::Catchable(Catchable::new(Some(format!(
|
||||||
|
|||||||
@@ -32,9 +32,10 @@ impl<const CAP: usize> Stack<CAP> {
|
|||||||
pub fn push(&mut self, item: Value) -> Result<()> {
|
pub fn push(&mut self, item: Value) -> Result<()> {
|
||||||
self.items
|
self.items
|
||||||
.get_mut(self.top)
|
.get_mut(self.top)
|
||||||
.map_or_else(|| Err(Error::EvalError("stack overflow".to_string())), |ok| {
|
.map_or_else(
|
||||||
Ok(ok)
|
|| Err(Error::EvalError("stack overflow".to_string())),
|
||||||
})?
|
|ok| Ok(ok),
|
||||||
|
)?
|
||||||
.write(item);
|
.write(item);
|
||||||
self.top += 1;
|
self.top += 1;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::builtins::env;
|
use crate::builtins::env;
|
||||||
use crate::bytecode::{self, BinOp, Funcs, OpCode, OpCodes, Program, Thunks, UnOp};
|
use crate::bytecode::{BinOp, OpCode, OpCodes, Program, UnOp};
|
||||||
use crate::error::*;
|
use crate::error::*;
|
||||||
use crate::ty::common::Symbol;
|
use crate::ty::common::Symbol;
|
||||||
use crate::ty::internal::*;
|
use crate::ty::internal::*;
|
||||||
@@ -17,14 +17,14 @@ pub fn run(prog: Program) -> Result<p::Value> {
|
|||||||
|
|
||||||
pub struct VM {
|
pub struct VM {
|
||||||
thunks: Box<[Thunk]>,
|
thunks: Box<[Thunk]>,
|
||||||
funcs: Funcs,
|
funcs: Box<[Func]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VM {
|
impl VM {
|
||||||
fn new(thunks: Thunks, funcs: Funcs) -> Self {
|
fn new(thunks: Box<[OpCodes]>, funcs: Box<[Func]>) -> Self {
|
||||||
let thunks = thunks
|
let thunks = thunks
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|bytecode::Thunk { opcodes }| Thunk::new(opcodes))
|
.map(|opcodes| Thunk::new(opcodes))
|
||||||
.collect();
|
.collect();
|
||||||
VM { thunks, funcs }
|
VM { thunks, funcs }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user