add ast display impl
This commit is contained in:
parent
e2213a627b
commit
f4dbadced9
2 changed files with 66 additions and 2 deletions
64
src/ast.rs
64
src/ast.rs
|
|
@ -1,4 +1,6 @@
|
||||||
use crate::{symbol::Symbol, val::Val};
|
use std::fmt;
|
||||||
|
|
||||||
|
use crate::symbol::Symbol;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub enum Ident {
|
pub enum Ident {
|
||||||
|
|
@ -8,6 +10,15 @@ pub enum Ident {
|
||||||
Return(Symbol),
|
Return(Symbol),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Ident {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Ident::Local(name) => write!(f, "{name}"),
|
||||||
|
Ident::Return(name) => write!(f, "^{name}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub enum BinOp {
|
pub enum BinOp {
|
||||||
Add,
|
Add,
|
||||||
|
|
@ -21,6 +32,23 @@ pub enum BinOp {
|
||||||
Le,
|
Le,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for BinOp {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let str = match self {
|
||||||
|
BinOp::Add => "+",
|
||||||
|
BinOp::Sub => "-",
|
||||||
|
BinOp::Mul => "*",
|
||||||
|
BinOp::Div => "/",
|
||||||
|
BinOp::Mod => "%",
|
||||||
|
BinOp::Pow => "**",
|
||||||
|
BinOp::Eq => "=",
|
||||||
|
BinOp::Lt => "<",
|
||||||
|
BinOp::Le => "<=",
|
||||||
|
};
|
||||||
|
write!(f, "{str}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: great display repr for this for testing (yes it's allowed)
|
// TODO: great display repr for this for testing (yes it's allowed)
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Ast {
|
pub enum Ast {
|
||||||
|
|
@ -45,3 +73,37 @@ pub enum Ast {
|
||||||
/// `(println bla; 3) #=> 3`
|
/// `(println bla; 3) #=> 3`
|
||||||
Block(Vec<Ast>),
|
Block(Vec<Ast>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Ast {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Ast::Var(ident) => write!(f, "{ident}"),
|
||||||
|
Ast::Set(ident, ast) => write!(f, "(set! {ident} {ast})"),
|
||||||
|
Ast::Fn {
|
||||||
|
inputs,
|
||||||
|
outputs,
|
||||||
|
body,
|
||||||
|
} => {
|
||||||
|
let body: Vec<_> = body.iter().map(|stmt| format!("{stmt}")).collect();
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"(fn ([{}] [{}]) {})",
|
||||||
|
inputs.join(" "),
|
||||||
|
outputs.join(" "),
|
||||||
|
body.join(" ")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Ast::Map(body) => {
|
||||||
|
let body: Vec<_> = body.iter().map(|stmt| format!("{stmt}")).collect();
|
||||||
|
write!(f, "{{{}}}", body.join(" "))
|
||||||
|
}
|
||||||
|
Ast::App(func, x) => write!(f, "({func} {x})"),
|
||||||
|
Ast::BinOp(lhs, op, rhs) => write!(f, "({op} {lhs} {rhs})"),
|
||||||
|
Ast::Num(n) => write!(f, "{n}"),
|
||||||
|
Ast::Block(body) => {
|
||||||
|
let body: Vec<_> = body.iter().map(|stmt| format!("{stmt}")).collect();
|
||||||
|
write!(f, "({})", body.join(" "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,5 +11,7 @@ mod val;
|
||||||
fn main() {
|
fn main() {
|
||||||
let src = &env::args().nth(1).expect("give me an argument now");
|
let src = &env::args().nth(1).expect("give me an argument now");
|
||||||
let res = parse(src);
|
let res = parse(src);
|
||||||
println!("{res:#?}");
|
for stmt in res {
|
||||||
|
println!("{stmt}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue