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