367 lines
14 KiB
Rust
367 lines
14 KiB
Rust
use gc_arena::Collect;
|
|
use num_enum::TryFromPrimitive;
|
|
|
|
macro_rules! define_builtins {
|
|
($(($name:literal, $variant:ident, $arity:expr)),* $(,)?) => {
|
|
/// Builtin function registry.
|
|
/// Array index IS the PrimOp id. (name, arity) pairs.
|
|
pub const BUILTINS: &[(&str, u8)] = &[
|
|
$(($name, $arity),)*
|
|
];
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, Collect)]
|
|
#[repr(u8)]
|
|
#[collect(require_static)]
|
|
pub enum BuiltinId {
|
|
$($variant,)*
|
|
}
|
|
};
|
|
}
|
|
|
|
define_builtins! {
|
|
("abort", Abort, 1),
|
|
("__add", Add, 2),
|
|
("__addErrorContext", AddErrorContext, 2),
|
|
("__all", All, 2),
|
|
("__any", Any, 2),
|
|
("__appendContext", AppendContext, 2),
|
|
("__attrNames", AttrNames, 1),
|
|
("__attrValues", AttrValues, 1),
|
|
("baseNameOf", BaseNameOf, 1),
|
|
("__bitAnd", BitAnd, 2),
|
|
("__bitOr", BitOr, 2),
|
|
("__bitXor", BitXor, 2),
|
|
("break", Break, 1),
|
|
("__catAttrs", CatAttrs, 2),
|
|
("__ceil", Ceil, 1),
|
|
("__compareVersions", CompareVersions, 2),
|
|
("__concatLists", ConcatLists, 1),
|
|
("__concatMap", ConcatMap, 2),
|
|
("__concatStringsSep", ConcatStringsSep, 2),
|
|
("__convertHash", ConvertHash, 1),
|
|
("__deepSeq", DeepSeq, 2),
|
|
("derivation", Derivation, 1),
|
|
("derivationStrict", DerivationStrict, 1),
|
|
("dirOf", DirOf, 1),
|
|
("__div", Div, 2),
|
|
("__elem", Elem, 2),
|
|
("__elemAt", ElemAt, 2),
|
|
("fetchGit", FetchGit, 1),
|
|
("fetchMercurial", FetchMercurial, 1),
|
|
("fetchTarball", FetchTarball, 1),
|
|
("fetchTree", FetchTree, 1),
|
|
("__fetchurl", FetchUrl, 1),
|
|
("__filter", Filter, 2),
|
|
("__filterSource", FilterSource, 2),
|
|
("__findFile", FindFile, 2),
|
|
("__floor", Floor, 1),
|
|
("__foldl'", FoldlStrict, 3),
|
|
("__fromJSON", FromJSON, 1),
|
|
("fromTOML", FromTOML, 1),
|
|
("__functionArgs", FunctionArgs, 1),
|
|
("__genList", GenList, 2),
|
|
("__genericClosure", GenericClosure, 1),
|
|
("__getAttr", GetAttr, 2),
|
|
("__getContext", GetContext, 1),
|
|
("__getEnv", GetEnv, 1),
|
|
("__groupBy", GroupBy, 2),
|
|
("__hasAttr", HasAttr, 2),
|
|
("__hasContext", HasContext, 1),
|
|
("__hashFile", HashFile, 2),
|
|
("__hashString", HashString, 2),
|
|
("__head", Head, 1),
|
|
("import", Import, 1),
|
|
("__intersectAttrs", IntersectAttrs, 2),
|
|
("__isAttrs", IsAttrs, 1),
|
|
("__isBool", IsBool, 1),
|
|
("__isFloat", IsFloat, 1),
|
|
("__isFunction", IsFunction, 1),
|
|
("__isInt", IsInt, 1),
|
|
("__isList", IsList, 1),
|
|
("isNull", IsNull, 1),
|
|
("__isPath", IsPath, 1),
|
|
("__isString", IsString, 1),
|
|
("__length", Length, 1),
|
|
("__lessThan", LessThan, 2),
|
|
("__listToAttrs", ListToAttrs, 1),
|
|
("map", Map, 2),
|
|
("__mapAttrs", MapAttrs, 2),
|
|
("__match", Match, 2),
|
|
("__mul", Mul, 2),
|
|
("__parseDrvName", ParseDrvName, 1),
|
|
("__partition", Partition, 2),
|
|
("__path", Path, 1),
|
|
("__pathExists", PathExists, 1),
|
|
("placeholder", Placeholder, 1),
|
|
("__readDir", ReadDir, 1),
|
|
("__readFile", ReadFile, 1),
|
|
("__readFileType", ReadFileType, 1),
|
|
("removeAttrs", RemoveAttrs, 2),
|
|
("__replaceStrings", ReplaceStrings, 3),
|
|
("scopedImport", ScopedImport, 2),
|
|
("__seq", Seq, 2),
|
|
("__sort", Sort, 2),
|
|
("__split", Split, 2),
|
|
("__splitVersion", SplitVersion, 1),
|
|
("__storePath", StorePath, 1),
|
|
("__stringLength", StringLength, 1),
|
|
("__sub", Sub, 2),
|
|
("__substring", Substring, 3),
|
|
("__tail", Tail, 1),
|
|
("throw", Throw, 1),
|
|
("__toFile", ToFile, 2),
|
|
("__toJSON", ToJSON, 1),
|
|
("__toPath", ToPath, 1),
|
|
("toString", ToString, 1),
|
|
("__toXML", ToXML, 1),
|
|
("__trace", Trace, 2),
|
|
("__tryEval", TryEval, 1),
|
|
("__typeOf", TypeOf, 1),
|
|
("__unsafeDiscardStringContext", UnsafeDiscardStringContext, 1),
|
|
("__unsafeGetAttrPos", UnsafeGetAttrPos, 2),
|
|
("__warn", Warn, 2),
|
|
("__zipAttrsWith", ZipAttrsWith, 2),
|
|
}
|
|
|
|
#[repr(u8)]
|
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, TryFromPrimitive)]
|
|
pub enum PrimOpPhase {
|
|
Abort,
|
|
Add,
|
|
AddErrorContext,
|
|
All,
|
|
Any,
|
|
AppendContext,
|
|
AttrNames,
|
|
AttrValues,
|
|
BaseNameOf,
|
|
BitAnd,
|
|
BitOr,
|
|
BitXor,
|
|
Break,
|
|
CatAttrs,
|
|
Ceil,
|
|
CompareVersions,
|
|
ConcatLists,
|
|
ConcatMap,
|
|
ConcatStringsSep,
|
|
ConvertHash,
|
|
|
|
DeepSeq,
|
|
DeepSeqPush,
|
|
DeepSeqLoop,
|
|
|
|
Derivation,
|
|
DerivationStrict,
|
|
DirOf,
|
|
Div,
|
|
Elem,
|
|
ElemAt,
|
|
FetchGit,
|
|
FetchMercurial,
|
|
FetchTarball,
|
|
FetchTree,
|
|
FetchUrl,
|
|
|
|
FilterForceList,
|
|
FilterCallPred,
|
|
FilterCheck,
|
|
|
|
FilterSource,
|
|
FindFile,
|
|
Floor,
|
|
FoldlStrict,
|
|
FromJSON,
|
|
FromTOML,
|
|
FunctionArgs,
|
|
GenList,
|
|
GenericClosure,
|
|
GetAttr,
|
|
GetContext,
|
|
GetEnv,
|
|
GroupBy,
|
|
HasAttr,
|
|
HasContext,
|
|
HashFile,
|
|
HashString,
|
|
Head,
|
|
Import,
|
|
IntersectAttrs,
|
|
IsAttrs,
|
|
IsBool,
|
|
IsFloat,
|
|
IsFunction,
|
|
IsInt,
|
|
IsList,
|
|
IsNull,
|
|
IsPath,
|
|
IsString,
|
|
Length,
|
|
LessThan,
|
|
ListToAttrs,
|
|
Map,
|
|
MapAttrs,
|
|
Match,
|
|
Mul,
|
|
ParseDrvName,
|
|
Partition,
|
|
Path,
|
|
PathExists,
|
|
Placeholder,
|
|
ReadDir,
|
|
ReadFile,
|
|
ReadFileType,
|
|
RemoveAttrs,
|
|
ReplaceStrings,
|
|
ScopedImport,
|
|
Seq,
|
|
Sort,
|
|
Split,
|
|
SplitVersion,
|
|
StorePath,
|
|
StringLength,
|
|
Sub,
|
|
Substring,
|
|
Tail,
|
|
Throw,
|
|
ToFile,
|
|
ToJSON,
|
|
ToPath,
|
|
ToString,
|
|
ToXML,
|
|
Trace,
|
|
TryEval,
|
|
TypeOf,
|
|
UnsafeDiscardStringContext,
|
|
UnsafeGetAttrPos,
|
|
Warn,
|
|
ZipAttrsWith,
|
|
|
|
ForceResultShallow,
|
|
ForceResultShallowPush,
|
|
ForceResultShallowLoop,
|
|
ForceResultDeepFinish,
|
|
|
|
// TODO: split into separate enums
|
|
CallPattern,
|
|
|
|
Illegal,
|
|
}
|
|
|
|
impl BuiltinId {
|
|
#[inline(always)]
|
|
pub fn entry_phase(self) -> PrimOpPhase {
|
|
use BuiltinId::*;
|
|
match self {
|
|
Abort => PrimOpPhase::Abort,
|
|
Add => PrimOpPhase::Add,
|
|
AddErrorContext => PrimOpPhase::AddErrorContext,
|
|
All => PrimOpPhase::All,
|
|
Any => PrimOpPhase::Any,
|
|
AppendContext => PrimOpPhase::AppendContext,
|
|
AttrNames => PrimOpPhase::AttrNames,
|
|
AttrValues => PrimOpPhase::AttrValues,
|
|
BaseNameOf => PrimOpPhase::BaseNameOf,
|
|
BitAnd => PrimOpPhase::BitAnd,
|
|
BitOr => PrimOpPhase::BitOr,
|
|
BitXor => PrimOpPhase::BitXor,
|
|
Break => PrimOpPhase::Break,
|
|
CatAttrs => PrimOpPhase::CatAttrs,
|
|
Ceil => PrimOpPhase::Ceil,
|
|
CompareVersions => PrimOpPhase::CompareVersions,
|
|
ConcatLists => PrimOpPhase::ConcatLists,
|
|
ConcatMap => PrimOpPhase::ConcatMap,
|
|
ConcatStringsSep => PrimOpPhase::ConcatStringsSep,
|
|
ConvertHash => PrimOpPhase::ConvertHash,
|
|
DeepSeq => PrimOpPhase::DeepSeq,
|
|
Derivation => PrimOpPhase::Derivation,
|
|
DerivationStrict => PrimOpPhase::DerivationStrict,
|
|
DirOf => PrimOpPhase::DirOf,
|
|
Div => PrimOpPhase::Div,
|
|
Elem => PrimOpPhase::Elem,
|
|
ElemAt => PrimOpPhase::ElemAt,
|
|
FetchGit => PrimOpPhase::FetchGit,
|
|
FetchMercurial => PrimOpPhase::FetchMercurial,
|
|
FetchTarball => PrimOpPhase::FetchTarball,
|
|
FetchTree => PrimOpPhase::FetchTree,
|
|
FetchUrl => PrimOpPhase::FetchUrl,
|
|
Filter => PrimOpPhase::FilterForceList,
|
|
FilterSource => PrimOpPhase::FilterSource,
|
|
FindFile => PrimOpPhase::FindFile,
|
|
Floor => PrimOpPhase::Floor,
|
|
FoldlStrict => PrimOpPhase::FoldlStrict,
|
|
FromJSON => PrimOpPhase::FromJSON,
|
|
FromTOML => PrimOpPhase::FromTOML,
|
|
FunctionArgs => PrimOpPhase::FunctionArgs,
|
|
GenList => PrimOpPhase::GenList,
|
|
GenericClosure => PrimOpPhase::GenericClosure,
|
|
GetAttr => PrimOpPhase::GetAttr,
|
|
GetContext => PrimOpPhase::GetContext,
|
|
GetEnv => PrimOpPhase::GetEnv,
|
|
GroupBy => PrimOpPhase::GroupBy,
|
|
HasAttr => PrimOpPhase::HasAttr,
|
|
HasContext => PrimOpPhase::HasContext,
|
|
HashFile => PrimOpPhase::HashFile,
|
|
HashString => PrimOpPhase::HashString,
|
|
Head => PrimOpPhase::Head,
|
|
Import => PrimOpPhase::Import,
|
|
IntersectAttrs => PrimOpPhase::IntersectAttrs,
|
|
IsAttrs => PrimOpPhase::IsAttrs,
|
|
IsBool => PrimOpPhase::IsBool,
|
|
IsFloat => PrimOpPhase::IsFloat,
|
|
IsFunction => PrimOpPhase::IsFunction,
|
|
IsInt => PrimOpPhase::IsInt,
|
|
IsList => PrimOpPhase::IsList,
|
|
IsNull => PrimOpPhase::IsNull,
|
|
IsPath => PrimOpPhase::IsPath,
|
|
IsString => PrimOpPhase::IsString,
|
|
Length => PrimOpPhase::Length,
|
|
LessThan => PrimOpPhase::LessThan,
|
|
ListToAttrs => PrimOpPhase::ListToAttrs,
|
|
Map => PrimOpPhase::Map,
|
|
MapAttrs => PrimOpPhase::MapAttrs,
|
|
Match => PrimOpPhase::Match,
|
|
Mul => PrimOpPhase::Mul,
|
|
ParseDrvName => PrimOpPhase::ParseDrvName,
|
|
Partition => PrimOpPhase::Partition,
|
|
Path => PrimOpPhase::Path,
|
|
PathExists => PrimOpPhase::PathExists,
|
|
Placeholder => PrimOpPhase::Placeholder,
|
|
ReadDir => PrimOpPhase::ReadDir,
|
|
ReadFile => PrimOpPhase::ReadFile,
|
|
ReadFileType => PrimOpPhase::ReadFileType,
|
|
RemoveAttrs => PrimOpPhase::RemoveAttrs,
|
|
ReplaceStrings => PrimOpPhase::ReplaceStrings,
|
|
ScopedImport => PrimOpPhase::ScopedImport,
|
|
Seq => PrimOpPhase::Seq,
|
|
Sort => PrimOpPhase::Sort,
|
|
Split => PrimOpPhase::Split,
|
|
SplitVersion => PrimOpPhase::SplitVersion,
|
|
StorePath => PrimOpPhase::StorePath,
|
|
StringLength => PrimOpPhase::StringLength,
|
|
Sub => PrimOpPhase::Sub,
|
|
Substring => PrimOpPhase::Substring,
|
|
Tail => PrimOpPhase::Tail,
|
|
Throw => PrimOpPhase::Throw,
|
|
ToFile => PrimOpPhase::ToFile,
|
|
ToJSON => PrimOpPhase::ToJSON,
|
|
ToPath => PrimOpPhase::ToPath,
|
|
ToString => PrimOpPhase::ToString,
|
|
ToXML => PrimOpPhase::ToXML,
|
|
Trace => PrimOpPhase::Trace,
|
|
TryEval => PrimOpPhase::TryEval,
|
|
TypeOf => PrimOpPhase::TypeOf,
|
|
UnsafeDiscardStringContext => PrimOpPhase::UnsafeDiscardStringContext,
|
|
UnsafeGetAttrPos => PrimOpPhase::UnsafeGetAttrPos,
|
|
Warn => PrimOpPhase::Warn,
|
|
ZipAttrsWith => PrimOpPhase::ZipAttrsWith,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl PrimOpPhase {
|
|
pub fn ip(self) -> u32 {
|
|
self as u32 * 2
|
|
}
|
|
}
|