60 lines
1.0 KiB
Rust
60 lines
1.0 KiB
Rust
|
|
use crate::{
|
||
|
|
compile::Ctx,
|
||
|
|
obj::Sym,
|
||
|
|
syn::{ast, span::*},
|
||
|
|
};
|
||
|
|
|
||
|
|
pub type Body = Vec<Stmt>;
|
||
|
|
|
||
|
|
#[derive(Debug, Clone)]
|
||
|
|
pub enum Stmt {
|
||
|
|
Eval(Expr),
|
||
|
|
Assign(Lhs, Expr),
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Clone)]
|
||
|
|
pub enum Lhs {
|
||
|
|
Sym(Sym),
|
||
|
|
Complex(Expr),
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Clone)]
|
||
|
|
pub enum Expr {
|
||
|
|
Call(Box<Expr>, Vec<Expr>),
|
||
|
|
Base(BaseExpr),
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Clone)]
|
||
|
|
pub enum BaseExprKind {
|
||
|
|
Num(i64),
|
||
|
|
Str(String),
|
||
|
|
Sym(Sym),
|
||
|
|
Ident(String),
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Clone)]
|
||
|
|
pub struct BaseExpr {
|
||
|
|
pub kind: BaseExprKind,
|
||
|
|
pub span: Span,
|
||
|
|
}
|
||
|
|
|
||
|
|
// FromAst and IntoAst are just so we can use .into_ast(ctx, text)
|
||
|
|
// some impls may not need ctx &mut ref - but probably needed for symbol resolution
|
||
|
|
|
||
|
|
pub trait FromAst<A> {
|
||
|
|
fn from_ast(other: A, ctx: &mut Ctx, text: &str) -> Self;
|
||
|
|
}
|
||
|
|
|
||
|
|
pub trait IntoIr<I> {
|
||
|
|
fn into_ir(self, ctx: &mut Ctx, text: &str) -> I;
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<A, I> IntoIr<I> for A
|
||
|
|
where
|
||
|
|
I: FromAst<A>,
|
||
|
|
{
|
||
|
|
fn into_ir(self, ctx: &mut Ctx, text: &str) -> I {
|
||
|
|
FromAst::from_ast(self, ctx, text)
|
||
|
|
}
|
||
|
|
}
|