opslang_syn/
typedef.rs

1//! type definition of AST.
2
3#[derive(Debug, PartialEq)]
4pub struct Row<'a> {
5    pub breaks: Option<()>,
6    pub content: Option<ReservedControl<'a>>,
7    pub comment_trailing: Option<Comment<'a>>,
8}
9
10#[derive(Debug, PartialEq)]
11pub struct Comment<'a>(pub &'a str);
12
13/// reserved controls
14#[derive(Debug, PartialEq)]
15pub enum ReservedControl<'a> {
16    Call(Call<'a>),
17    WaitSec(WaitSec<'a>),
18    WaitUntil(WaitUntil<'a>),
19    CheckValue(CheckValue<'a>),
20    Command(Command<'a>),
21    Let(Let<'a>),
22    Get(Get<'a>),
23}
24
25#[derive(Debug, PartialEq)]
26pub struct VariablePath<'a> {
27    pub raw: &'a str,
28}
29
30#[derive(Debug, PartialEq)]
31pub struct FilePath<'a> {
32    pub full_name: &'a str,
33}
34
35#[derive(Debug, PartialEq)]
36pub struct CheckValue<'a> {
37    pub condition: Expr<'a>,
38}
39
40#[derive(Debug, PartialEq)]
41pub struct Command<'a> {
42    pub destinations: Vec<Destination<'a>>,
43    pub name: &'a str,
44    pub args: Vec<Expr<'a>>,
45}
46
47#[derive(Debug, PartialEq)]
48pub struct Destination<'a> {
49    pub component: &'a str,
50    pub exec_method: &'a str,
51}
52
53#[derive(Debug, PartialEq)]
54pub struct Call<'a> {
55    pub path: FilePath<'a>,
56}
57
58#[derive(Debug, PartialEq)]
59pub struct WaitSec<'a> {
60    pub sec: Expr<'a>,
61}
62
63#[derive(Debug, PartialEq)]
64pub struct WaitUntil<'a> {
65    pub condition: Expr<'a>,
66}
67
68#[derive(Debug, PartialEq)]
69pub struct WaitInc<'a> {
70    pub condition: Expr<'a>,
71}
72
73#[derive(Debug, PartialEq)]
74pub enum CompareOp<'a> {
75    BinOp(CompareBinOp<'a>),
76    In(CompareIn<'a>),
77}
78
79#[derive(Debug, PartialEq)]
80pub struct CompareBinOp<'a> {
81    pub method: CompareBinOpKind,
82    pub rhs: Expr<'a>,
83}
84
85#[derive(Debug, PartialEq)]
86pub enum CompareBinOpKind {
87    GreaterEq,
88    LessEq,
89    Greater,
90    Less,
91    NotEqual,
92    Equal,
93}
94
95#[derive(Debug, PartialEq)]
96pub struct CompareIn<'a> {
97    pub lo: Expr<'a>,
98    pub hi: Expr<'a>,
99}
100
101#[derive(Debug, PartialEq)]
102pub struct Ident<'a> {
103    pub raw: &'a str,
104}
105
106#[derive(Debug, PartialEq)]
107pub struct Let<'a> {
108    pub variable: Ident<'a>,
109    pub rhs: Expr<'a>,
110}
111
112#[derive(Debug, PartialEq)]
113pub struct Get<'a> {
114    pub variable: VariablePath<'a>,
115}
116
117/// an expression.
118///
119/// to implementer: you can use stack machine to express the evaluation of this tree structure.
120#[derive(Debug, PartialEq)]
121pub enum Expr<'a> {
122    Variable(VariablePath<'a>),
123    Literal(Literal<'a>),
124    UnOp(UnOpKind, Box<Self>),
125    BinOp(BinOpKind, Box<Self>, Box<Self>),
126    FunCall(Box<Self>, Vec<Self>),
127}
128
129#[derive(Debug, PartialEq)]
130pub enum Literal<'a> {
131    Array(Vec<Expr<'a>>),
132    String(&'a str),
133    Numeric(Numeric<'a>, Option<NumericSuffix>),
134}
135
136#[derive(Debug, PartialEq)]
137pub enum Numeric<'a> {
138    Integer(&'a str, IntegerPrefix),
139    Float(&'a str),
140}
141
142#[derive(Debug, PartialEq)]
143pub enum NumericSuffix {
144    Second,
145}
146
147#[derive(Debug, PartialEq)]
148pub enum IntegerPrefix {
149    Hexadecimal,
150    Decimal,
151    Octal,
152    Binary,
153}
154
155#[derive(Debug, PartialEq)]
156pub enum UnOpKind {
157    Neg,
158}
159
160#[derive(Debug, PartialEq)]
161pub enum BinOpKind {
162    Compare(CompareBinOpKind),
163    /// `a if b` means `b implies a`.
164    /// `a if b` will be represented as `Expr::BinOp(BinOpKind::If, a, b)`
165    If,
166    And,
167    Or,
168    In,
169    Mul,
170    Div,
171    Mod,
172    Add,
173    Sub,
174}