refactor: type

This commit is contained in:
2025-05-03 20:33:59 +08:00
parent 3f0cb2c2fa
commit 4a310ff317
23 changed files with 475 additions and 309 deletions

View File

@@ -2,7 +2,7 @@ use std::hash::Hash;
use ecow::EcoString;
use crate::value::Const;
use crate::ty::internal::{Const, Param};
type Slice<T> = Box<[T]>;
@@ -12,17 +12,13 @@ pub type SymIdx = usize;
pub type OpCodes = Slice<OpCode>;
pub type Consts = Slice<Const>;
pub type Thunks = Slice<Thunk>;
pub type Args = Slice<Arg>;
#[derive(Debug, Clone)]
pub struct Thunk {
pub opcodes: OpCodes,
}
#[derive(Debug, Clone, Hash)]
pub enum Arg {}
#[derive(Debug, Clone, Hash)]
#[derive(Debug, Clone)]
pub enum OpCode {
/// load a constant onto stack
Const { value: Const },
@@ -34,17 +30,25 @@ pub enum OpCode {
LoadValue { idx: ThunkIdx },
/// force TOS to value
ForceValue,
/// [ .. func args @ .. ] consume (`arity` + 2) elements, call `func` with args` of length `arity`
/// Example: __add 1 2 => [ LookUp("__add") Const(1) Const(2) Call(2) ]
Call { arity: usize },
/// return a value
Ret,
/// make a function
Func { param: Param, length: usize },
/// consume 1 element, assert TOS is true
Assert,
/// jump forward
Jmp { step: usize },
/// [ .. cond ] consume 1 element, if `cond`` is true, then jump forward
JmpIfTrue { step: usize },
/// [ .. cond ] consume 1 element, if `cond` is false, then jump forward
JmpIfFalse { step: usize },
/// push an empty attribute set onto stack
AttrSet,
/// push an empty recursive attribute set onto stack
@@ -53,10 +57,15 @@ pub enum OpCode {
PushStaticAttr { name: EcoString },
/// [ .. set name value ] consume 2 elements, push a dynamic kv pair (`name`, `value`) in to `set`
PushDynamicAttr,
/// push an empty list onto stack
List,
/// [ .. list elem ] consume 1 element, push `elem` into `list`
PushElem,
/// convert the string as TOS to a path
Path,
/// [ .. a b ] consume 2 elements, perform a string concatenation `a` + `b`
ConcatString,
/// [ .. a b ] consume 2 elements, perform a binary operation `a` `op` `b`
@@ -69,23 +78,19 @@ pub enum OpCode {
HasDynamicAttr,
/// [ .. set ] select `sym` from `set`
Select { sym: EcoString },
/// TODO:
SelectWithDefault { sym: EcoString },
/// TODO:
SelectOrEmpty { sym: EcoString },
/// TODO:
/// [ .. set default ] select `sym` from `set` or `default`
SelectOrDefault { sym: EcoString },
/// [ .. set sym ] select `sym` from `set`
SelectDynamic,
/// TODO:
SelectDynamicOrEmpty,
/// TODO:
SelectDynamicWithDefault,
/// [ .. set sym default ] select `sym` from `set` or `default`
SelectDynamicOrDefault,
/// enter the environment of the attribute set at TOS
EnterEnv,
/// exit current envrironment
LeaveEnv,
/// return a value
Ret,
/// no operation
/// no operation, used as termporary placeholder
NoOp,
}