ayaka_script/
lib.rs

1//! The script parsers.
2
3#![warn(missing_docs)]
4#![deny(unsafe_code)]
5
6#[cfg(feature = "parser")]
7mod parser;
8#[cfg(feature = "parser")]
9pub use parser::*;
10
11#[doc(no_inline)]
12pub use ayaka_primitive::{RawValue, ValueType};
13
14use serde::{Deserialize, Serialize};
15
16/// A full script, a collection of expressions.
17///
18/// The last expression is the final value of the script.
19#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
20pub struct Program(pub Vec<Expr>);
21
22/// An expression.
23///
24/// Two expressions should be splited with `;`.
25#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
26pub enum Expr {
27    /// A reference to a variable.
28    Ref(Ref),
29    /// A const value.
30    Const(RawValue),
31    /// A unary operation.
32    Unary(UnaryOp, Box<Expr>),
33    /// A binary operation.
34    Binary(Box<Expr>, BinaryOp, Box<Expr>),
35    /// A call to a function.
36    Call(String, String, Vec<Expr>),
37}
38
39/// Unary operations.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
41pub enum UnaryOp {
42    /// `+`
43    Positive,
44    /// `-`
45    Negative,
46    /// `!`
47    Not,
48}
49
50/// Binary operations.
51#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
52pub enum BinaryOp {
53    /// Value operations.
54    Val(ValBinaryOp),
55    /// Logical operations.
56    Logic(LogicBinaryOp),
57    /// Assignment.
58    Assign,
59    /// Inplace value operations.
60    Inplace(ValBinaryOp),
61}
62
63/// Value binary operations.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
65pub enum ValBinaryOp {
66    /// `+`
67    Add,
68    /// `-`
69    Minus,
70    /// `*`
71    Mul,
72    /// `/`
73    Div,
74    /// `%`
75    Mod,
76    /// `&`
77    And,
78    /// `|`
79    Or,
80    /// `^`
81    Xor,
82}
83
84/// Logical operations.
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
86pub enum LogicBinaryOp {
87    /// `&&`
88    And,
89    /// `||`
90    Or,
91    /// `==`
92    Eq,
93    /// `!=`
94    Neq,
95    /// `<`
96    Lt,
97    /// `<=`
98    Le,
99    /// `>`
100    Gt,
101    /// `>=`
102    Ge,
103}
104
105/// Reference of a variable.
106#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
107pub enum Ref {
108    /// A local variable.
109    /// It is only accessible in current program.
110    Var(String),
111    /// A context variable.
112    /// It is stored in the context.
113    /// The variable name is prefixed with `$`.
114    Ctx(String),
115}