aboutsummaryrefslogtreecommitdiff
path: root/src/ast/mod.rs
blob: b3e7c368c12e7259819ed98269a8ba7697c86fdd (plain)
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
use crate::lexer::{Literal, TokenType};

pub mod parser;
pub mod printer;

#[derive(Debug, Eq, PartialEq)]
pub enum Stmt {
    Block(Vec<Stmt>),
    Expr(Expr),
    Val {
        ident: String,
        value: Expr,
    },
    Var {
        ident: String,
        value: Expr,
    },
    Assignment {
        ident: String,
        value: Expr,
    },
    Function {
        ident: String,
        arguments: Vec<FunctionArgument>,
        return_type: String,
        body: Vec<Stmt>,
    },
    If {
        condition: Expr,
        body: Vec<Stmt>,
    },
    For {
        binding: String,
        range: (Expr, Expr),
        body: Vec<Stmt>,
    },
    While {
        condition: Expr,
        body: Vec<Stmt>,
    },
    Return {
        value: Expr,
    },
}

#[derive(Debug, Eq, PartialEq)]
pub struct FunctionArgument {
    name: String,
    types: String,
}

#[derive(Debug, Eq, PartialEq)]
pub enum Expr {
    Literal(Literal),
    Variable(String),
    Grouping(Box<Expr>),
    Call {
        ident: String,
        arguments: Vec<Expr>,
    },
    Binary {
        operator: TokenType,
        lhs: Box<Expr>,
        rhs: Box<Expr>,
    },
    Unary {
        operator: TokenType,
        expr: Box<Expr>,
    },
}

pub trait AstVisitor<T = ()> {
    fn visit_stmt(&mut self, stmt: &Stmt) -> T;
    fn visit_expr(&mut self, expr: &Expr) -> T;
}