bobascript_parser/
ast.rs

1use std::collections::HashMap;
2
3#[derive(Debug)]
4pub struct Ast(pub Vec<Box<Stmt>>, pub Option<Box<Expr>>);
5
6#[derive(Debug)]
7pub enum Stmt {
8  Function(String, Vec<String>, Box<Expr>),
9  Const(String, Box<Expr>),
10  Let(String, Option<Box<Expr>>),
11  Return(Option<Box<Expr>>),
12  Break(Option<Box<Expr>>),
13  Expression(Box<Expr>),
14}
15
16#[derive(Debug)]
17pub enum Expr {
18  Error,
19  /// Outputs the value of the contained [Expr] as a log.
20  Log(Box<Expr>),
21  Block(Vec<Box<Stmt>>, Option<Box<Expr>>),
22  /// The first [Expr] is the condition, the second the "true" block,
23  /// and the third the "false" block.
24  ///
25  /// Else-if statements are collapsed as if a standard if statement
26  /// was put inside of the else block.
27  If(
28    /* condition: */ Box<Expr>,
29    /* true branch: */ Box<Expr>,
30    /* false branch: */ Option<Box<Expr>>,
31  ),
32  /// While [Expr] is true, do [Stmt]s.
33  While(Box<Expr>, Vec<Box<Stmt>>),
34  Assign(Box<Expr>, AssignOp, Box<Expr>),
35  Binary(Box<Expr>, BinaryOp, Box<Expr>),
36  Unary(UnaryOp, Box<Expr>),
37  Property(Box<Expr>, String),
38  Index(Box<Expr>, Box<Expr>),
39  Call(Box<Expr>, Vec<Box<Expr>>),
40  Constant(Constant),
41}
42
43#[derive(Debug)]
44pub enum Constant {
45  True,
46  False,
47  Ident(Vec<String>, String),
48  Number(f64),
49  String(String),
50  Tuple(Vec<Box<Expr>>),
51  Record(HashMap<String, Box<Expr>>),
52}
53
54#[derive(Debug)]
55pub enum UnaryOp {
56  Negate,
57  Not,
58}
59
60#[derive(Debug)]
61pub enum BinaryOp {
62  Or,
63  And,
64  Equal,
65  NotEqual,
66  GreaterThan,
67  GreaterEqual,
68  LessThan,
69  LessEqual,
70  Add,
71  Subtract,
72  Multiply,
73  Divide,
74  Exponent,
75}
76
77#[derive(Debug)]
78pub enum AssignOp {
79  Assign,
80  AddAssign,
81  SubtractAssign,
82  MultiplyAssign,
83  DivideAssign,
84  ExponentAssign,
85  OrAssign,
86  AndAssign,
87}