1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! The script parsers.

#![warn(missing_docs)]
#![deny(unsafe_code)]

#[cfg(feature = "parser")]
mod parser;
#[cfg(feature = "parser")]
pub use parser::*;

#[doc(no_inline)]
pub use ayaka_primitive::{RawValue, ValueType};

use serde::{Deserialize, Serialize};

/// A full script, a collection of expressions.
///
/// The last expression is the final value of the script.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Program(pub Vec<Expr>);

/// An expression.
///
/// Two expressions should be splited with `;`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Expr {
    /// A reference to a variable.
    Ref(Ref),
    /// A const value.
    Const(RawValue),
    /// A unary operation.
    Unary(UnaryOp, Box<Expr>),
    /// A binary operation.
    Binary(Box<Expr>, BinaryOp, Box<Expr>),
    /// A call to a function.
    Call(String, String, Vec<Expr>),
}

/// Unary operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum UnaryOp {
    /// `+`
    Positive,
    /// `-`
    Negative,
    /// `!`
    Not,
}

/// Binary operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BinaryOp {
    /// Value operations.
    Val(ValBinaryOp),
    /// Logical operations.
    Logic(LogicBinaryOp),
    /// Assignment.
    Assign,
    /// Inplace value operations.
    Inplace(ValBinaryOp),
}

/// Value binary operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ValBinaryOp {
    /// `+`
    Add,
    /// `-`
    Minus,
    /// `*`
    Mul,
    /// `/`
    Div,
    /// `%`
    Mod,
    /// `&`
    And,
    /// `|`
    Or,
    /// `^`
    Xor,
}

/// Logical operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LogicBinaryOp {
    /// `&&`
    And,
    /// `||`
    Or,
    /// `==`
    Eq,
    /// `!=`
    Neq,
    /// `<`
    Lt,
    /// `<=`
    Le,
    /// `>`
    Gt,
    /// `>=`
    Ge,
}

/// Reference of a variable.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Ref {
    /// A local variable.
    /// It is only accessible in current program.
    Var(String),
    /// A context variable.
    /// It is stored in the context.
    /// The variable name is prefixed with `$`.
    Ctx(String),
}