1extern crate alloc;
2
3use alloc::string::String;
4use core::fmt::{self, Display};
5
6use serde::{Deserialize, Serialize};
7
8mod meta;
9
10pub use meta::*;
11
12use crate::{data::Value, Label};
13
14#[must_use]
16#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
17pub enum ExitReason {
18 Normal,
20 Yield,
22 Check,
24 Panic,
26}
27
28impl ExitReason {
29 #[cfg(feature = "testing")]
31 pub fn success(self) {
32 assert_eq!(self, Self::Normal);
33 }
34}
35
36impl Display for ExitReason {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 match self {
39 Self::Normal => f.write_str("normal"),
40 Self::Yield => f.write_str("yield"),
41 Self::Check => f.write_str("check"),
42 Self::Panic => f.write_str("panic"),
43 }
44 }
45}
46
47#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
49pub enum Target {
50 Unresolved(Label),
52 Resolved(usize),
54}
55
56impl Target {
57 pub fn resolved(&self) -> Option<usize> {
59 match self {
60 Target::Resolved(i) => Some(*i),
61 _ => None,
62 }
63 }
64}
65
66impl Display for Target {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 match self {
69 Self::Unresolved(label) => write!(f, "<{label}>"),
70 Self::Resolved(addr) => write!(f, "{addr}"),
71 }
72 }
73}
74
75pub type Identifier = String;
77
78#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
80pub enum Instruction {
81 Const(Value),
84 Def(Identifier),
86 Get(Identifier),
88 Swap(usize), Dup(usize),
92 Pop,
94 Block,
97 End,
99 Jump(Target),
101 Branch(Target),
103 Next,
105 Last,
107 Call(Target),
109 ExtCall(usize, usize),
111 Return,
113 Exit(ExitReason),
115 Add,
118 Sub,
120 Not,
122 And,
124 Or,
126 Gt,
128 Lt,
130 Eq,
132 FactNew(Identifier),
135 FactKeySet(Identifier),
137 FactValueSet(Identifier),
139 StructNew(Identifier),
142 StructSet(Identifier),
144 StructGet(Identifier),
146 Publish,
149 Create,
151 Delete,
153 Update,
155 Emit,
157 Query,
159 FactCount(i64),
161 QueryStart,
163 QueryNext(Identifier),
165 Serialize,
167 Deserialize,
169 Meta(Meta),
171}
172
173impl Display for Instruction {
174 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175 match self {
176 Instruction::Const(v) => write!(f, "const {v}"),
177 Instruction::Def(ident) => write!(f, "def {ident}"),
178 Instruction::Get(ident) => write!(f, "get {ident}"),
179 Instruction::Swap(d) => write!(f, "swap {d}"),
180 Instruction::Dup(d) => write!(f, "dup {d}"),
181 Instruction::Pop => write!(f, "pop"),
182 Instruction::Block => write!(f, "block"),
183 Instruction::End => write!(f, "end"),
184 Instruction::Jump(t) => write!(f, "jump {t}"),
185 Instruction::Branch(t) => write!(f, "branch {t}"),
186 Instruction::Next => write!(f, "next"),
187 Instruction::Last => write!(f, "last"),
188 Instruction::Call(t) => write!(f, "call {t}"),
189 Instruction::ExtCall(module, proc) => write!(f, "extcall {module} {proc}"),
190 Instruction::Return => write!(f, "return"),
191 Instruction::Exit(reason) => write!(f, "exit {reason}"),
192 Instruction::Add => write!(f, "add"),
193 Instruction::Sub => write!(f, "sub"),
194 Instruction::Not => write!(f, "not"),
195 Instruction::And => write!(f, "and"),
196 Instruction::Or => write!(f, "or"),
197 Instruction::Gt => write!(f, "gt"),
198 Instruction::Lt => write!(f, "lt"),
199 Instruction::Eq => write!(f, "eq"),
200 Instruction::FactNew(ident) => write!(f, "fact.new {ident}"),
201 Instruction::FactKeySet(ident) => write!(f, "fact.kset {ident}"),
202 Instruction::FactValueSet(ident) => write!(f, "fact.vset {ident}"),
203 Instruction::StructNew(ident) => write!(f, "struct.new {ident}"),
204 Instruction::StructSet(ident) => write!(f, "struct.set {ident}"),
205 Instruction::StructGet(ident) => write!(f, "struct.get {ident}"),
206 Instruction::Publish => write!(f, "publish"),
207 Instruction::Create => write!(f, "create"),
208 Instruction::Delete => write!(f, "delete"),
209 Instruction::Update => write!(f, "update"),
210 Instruction::Emit => write!(f, "emit"),
211 Instruction::Query => write!(f, "query"),
212 Instruction::FactCount(limit) => write!(f, "fact.count {limit}"),
213 Instruction::QueryStart => write!(f, "query.start"),
214 Instruction::QueryNext(ident) => write!(f, "query.next {ident}"),
215 Instruction::Serialize => write!(f, "serialize"),
216 Instruction::Deserialize => write!(f, "deserialize"),
217 Instruction::Meta(m) => write!(f, "meta: {m}"),
218 }
219 }
220}