1use std::rc::Rc;
15
16mod cache;
17mod debug;
18mod display;
19pub mod environment;
20#[macro_use]
21pub mod error;
22mod convert;
23pub mod pointer;
24mod runtime;
25pub mod scope;
26pub mod translate;
27mod vm;
28
29pub use environment::Environment;
30pub use error::Error;
31pub use vm::VM;
32
33use crate::ast::{CastType, Position};
34use pointer::OpPointer;
35use scope::Stack;
36
37#[derive(Debug, PartialEq, Clone)]
38pub enum Primitive {
39 Int(i64),
41 Float(f64),
42 Str(String),
43 Bool(bool),
44 Empty,
45}
46
47use Primitive::{Bool, Empty, Float, Int, Str};
48
49impl Value {
50 fn type_name(&self) -> &'static str {
51 match self {
52 P(Int(_)) => "Int",
53 P(Float(_)) => "Float",
54 P(Str(_)) => "String",
55 P(Bool(_)) => "Bool",
56 P(Empty) => "NULL",
57 C(List(_, _)) => "List",
58 C(Tuple(_, _)) => "Tuple",
59 F(_) => "Func",
60 M(_) => "Func",
61 T(_) => "Expression",
62 S(_) => "Symbol",
63 }
64 }
65}
66
67#[derive(Debug, PartialEq, Clone)]
68pub enum Composite {
69 List(Vec<Rc<Value>>, Vec<Position>),
70 Tuple(Vec<(String, Rc<Value>)>, Vec<(Position, Position)>),
71}
72
73use Composite::{List, Tuple};
74
75#[derive(Debug, PartialEq, Clone)]
76pub struct Func {
77 ptr: OpPointer,
78 bindings: Vec<String>,
79 snapshot: Stack,
80}
81
82#[derive(Debug, PartialEq, Clone)]
83pub struct Module {
84 ptr: OpPointer,
85 result_ptr: Option<usize>,
86 flds: Vec<(String, Rc<Value>)>,
87 flds_pos_list: Vec<(Position, Position)>,
88 pkg_ptr: Option<OpPointer>,
89}
90
91#[derive(Clone)]
92pub enum Value {
93 S(String),
95 P(Primitive),
97 C(Composite),
99 T(usize),
101 F(Func),
103 M(Module),
105}
106
107use Value::{C, F, M, P, S, T};
108
109#[derive(Debug, PartialEq, Clone)]
110pub enum Hook {
111 Map,
112 Include,
113 Filter,
114 Reduce,
115 Import,
116 Out,
117 Assert,
118 Convert,
119 Regex,
120 Range,
121 Trace(Position),
122}
123
124#[derive(Debug, PartialEq, Clone)]
125pub enum Op {
126 Bind, BindOver, Pop, NewScope(i32),
131 Add,
133 Sub,
134 Div,
135 Mul,
136 Mod,
137 Equal,
139 Gt,
140 Lt,
141 GtEq,
142 LtEq,
143 Not,
145 Val(Primitive),
147 Cast(CastType),
149 Sym(String),
151 DeRef(String),
153 InitTuple,
155 Field,
156 InitList,
157 Element,
158 Cp,
160 Bang,
162 Jump(i32),
163 JumpIfTrue(i32),
164 JumpIfFalse(i32),
165 SelectJump(i32),
166 And(i32),
167 Or(i32),
168 Index, SafeIndex, Exist,
172 Noop,
173 InitThunk(i32), Module(i32),
176 Func(i32),
177 Return,
178 FCall,
180 Typ,
182 Runtime(Hook),
184 Render,
185 PushSelf,
187 PopSelf,
188}
189
190use super::ir::Val;
191
192impl PartialEq for Value {
193 fn eq(&self, other: &Value) -> bool {
194 match (self, other) {
195 (P(left), P(right)) => left == right,
196 (C(List(left, _)), C(List(right, _))) => left == right,
197 (C(Tuple(left, _)), C(Tuple(right, _))) => {
198 if left.len() != right.len() {
199 return false;
200 }
201 for (ref lk, ref lv) in left.iter() {
202 let mut found = false;
203 for (ref rk, ref rv) in right.iter() {
204 if lk == rk {
205 found = true;
206 if lv != rv {
207 return false;
208 }
209 }
210 }
211 if !found {
212 return false;
213 }
214 }
215 true
216 }
217 (F(left), F(right)) => left == right,
218 (M(left), M(right)) => left == right,
219 (T(_), T(_)) | (S(_), S(_)) => false,
220 (_, _) => false,
221 }
222 }
223}