Skip to content
Snippets Groups Projects
Select Git revision
  • 1daf87a621f1beb6efcb1e4644ea526646f9147f
  • master default protected
  • usb
  • fresk
4 results

bare6.rs

Blame
  • ast.rs 2.13 KiB
    // AST
    
    use nom_locate::LocatedSpan;
    
    pub type Span<'a> = LocatedSpan<&'a str>;
    
    #[derive(Debug, Clone, Copy, PartialEq)]
    pub enum Op {
        Eq,
        Neq,
        And,
        Or,
        Add,
        Sub,
        Mul,
        Div,
        Pow,
        Not,
    }
    
    type SpanOp<'a> = (Span<'a>, Op);
    
    #[derive(Debug, Clone, PartialEq)]
    pub enum Expr<'a> {
        Num(i32),
        Bool(bool),
        Par(Box<SpanExpr<'a>>),
        Id(String),
        Fn(String, Vec<SpanExpr<'a>>),
        BinOp(Op, Box<SpanExpr<'a>>, Box<SpanExpr<'a>>),
        UnaryOp(Op, Box<SpanExpr<'a>>),
    }
    
    pub type SpanExpr<'a> = (Span<'a>, Expr<'a>);
    
    #[derive(Debug, PartialEq, Clone)]
    pub enum Cmd<'a> {
        // let <mut> id : <& <mut>>Type = expr
        Let(SpanMut<'a>, String, SpanType<'a>, SpanExpr<'a>),
        // id = expr
        Assign(SpanExpr<'a>, SpanExpr<'a>),
        // /// if predicate do-this, and optionally do-that)
        // If(Expr, Block, Option<Block>),
        // /// while predicate do-this
        // While(Expr, Block),
        // Return(Expr),
    }
    
    pub type SpanCmd<'a> = (Span<'a>, Cmd<'a>);
    
    #[derive(Debug, PartialEq, Clone)]
    pub enum Mutability {
        Imm,
        Mut,
    }
    
    pub type SpanMut<'a> = (Span<'a>, Mutability);
    
    // #[derive(Debug, PartialEq, Clone)]
    // pub enum Cmd {
    //     /// let <mut> id : <& <mut>>Type = expr
    //     Let(Mutability, String, Type, Expr),
    //     /// id = expr
    //     Assign(Expr, Expr),
    //     /// if predicate do-this, and optionally do-that)
    //     If(Expr, Block, Option<Block>),
    //     /// while predicate do-this
    //     While(Expr, Block),
    //     Return(Expr),
    // }
    
    pub type SpanBlock<'a> = (Span<'a>, Vec<SpanCmd<'a>>);
    
    #[derive(Debug, PartialEq, Clone)]
    pub enum Type<'a> {
        I32,
        Bool,
        Unit,
        Mut(Box<SpanType<'a>>),
        Ref(Box<SpanType<'a>>),
        // no structs
    }
    
    pub type SpanType<'a> = (Span<'a>, Type<'a>);
    
    // #[derive(Debug, PartialEq, Clone)]
    // pub enum TypeDecl {
    //     Struct(String, Vec<(String, Type)>),
    // }
    
    // #[derive(Debug, PartialEq, Clone)]
    // pub struct Function {
    //     pub sig: (String, Vec<(String, Type)>, Type),
    //     pub body: Block,
    // }
    
    // #[derive(Debug, PartialEq, Clone)]
    // pub enum Item {
    //     TypeDecl(TypeDecl),
    //     Function(Function),
    // }
    
    // pub type Prog = Vec<Item>;