Blame view

src/Rust/ast.rs 1.45 KB
Ulrich Schoepp committed
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
#[derive(Debug, Copy, Clone)]
pub enum Binop {
    Add,
    Sub,
    Mul,
    Div,
    Lt,
    StrictAnd,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type<'a> {
    Int,
    Void,
    Bool,
    Arr(Box<Type<'a>>),
    Class(&'a str),
}

#[derive(Debug)]
pub struct Prg<'a> {
    pub main_class: &'a str,
    pub main_throws_ioexception: bool,
    pub main_body: Stm<'a>,
    pub classes: Vec<ClassDecl<'a>>,
}

#[derive(Debug)]
pub struct ClassDecl<'a> {
    pub name: &'a str,
    pub fields: Vec<(&'a str, Type<'a>)>,
    pub methods: Vec<MethodDecl<'a>>,
}

#[derive(Debug)]
pub struct MethodDecl<'a> {
    pub name: &'a str,
    pub ret_type: Type<'a>,
    pub parameters: Vec<(&'a str, Type<'a>)>,
    pub throws_ioexception: bool,
    pub locals: Vec<(&'a str, Type<'a>)>,
    pub body: Stm<'a>,
    pub ret: Exp<'a>,
}

#[derive(Debug)]
pub enum Exp<'a> {
    Id(&'a str),
    Number(i32),
    Op(Box<Exp<'a>>, Binop, Box<Exp<'a>>),
    Invoke(Box<Exp<'a>>, &'a str, Vec<Exp<'a>>),
    ArrayGet(Box<Exp<'a>>, Box<Exp<'a>>),
    ArrayLength(Box<Exp<'a>>),
    True,
    False,
    This,
    New(&'a str),
    NewIntArray(Box<Exp<'a>>),
    Neg(Box<Exp<'a>>),
    Read()
}

#[derive(Debug)]
pub enum Stm<'a> {
    Assignment(&'a str, Box<Exp<'a>>),
    ArrayAssignment(&'a str, Box<Exp<'a>>, Box<Exp<'a>>),
    If(Box<Exp<'a>>, Box<Stm<'a>>, Box<Stm<'a>>),
    While(Box<Exp<'a>>, Box<Stm<'a>>),
    Write(Box<Exp<'a>>),
    Println(Box<Exp<'a>>),
    Seq(Vec<Stm<'a>>),
}