sieve/compiler/grammar/expr/
mod.rs1use std::sync::Arc;
25
26use serde::{Deserialize, Serialize};
27
28use crate::compiler::{Number, VariableType};
29
30pub mod parser;
31pub mod tokenizer;
32
33#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
34pub(crate) enum Expression {
35 Variable(VariableType),
36 Constant(Constant),
37 BinaryOperator(BinaryOperator),
38 UnaryOperator(UnaryOperator),
39 JmpIf { val: bool, pos: u32 },
40 Function { id: u32, num_args: u32 },
41 ArrayAccess,
42 ArrayBuild(u32),
43}
44
45#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
46pub(crate) enum Constant {
47 Integer(i64),
48 Float(f64),
49 String(Arc<String>),
50}
51
52impl Eq for Constant {}
53
54impl From<Number> for Constant {
55 fn from(value: Number) -> Self {
56 match value {
57 Number::Integer(i) => Constant::Integer(i),
58 Number::Float(f) => Constant::Float(f),
59 }
60 }
61}
62
63impl From<String> for Constant {
64 fn from(value: String) -> Self {
65 Constant::String(value.into())
66 }
67}
68
69#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
70pub(crate) enum BinaryOperator {
71 Add,
72 Subtract,
73 Multiply,
74 Divide,
75
76 And,
77 Or,
78 Xor,
79
80 Eq,
81 Ne,
82 Lt,
83 Le,
84 Gt,
85 Ge,
86}
87
88#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
89pub(crate) enum UnaryOperator {
90 Not,
91 Minus,
92}
93
94#[derive(Debug, PartialEq, Eq, Clone)]
95pub(crate) enum Token {
96 Variable(VariableType),
97 Function {
98 name: String,
99 id: u32,
100 num_args: u32,
101 },
102 Number(Number),
103 String(String),
104 BinaryOperator(BinaryOperator),
105 UnaryOperator(UnaryOperator),
106 OpenParen,
107 CloseParen,
108 OpenBracket,
109 CloseBracket,
110 Comma,
111}