hence/hence/src/parser/ast.rs

53 lines
1.3 KiB
Rust
Raw Normal View History

2022-07-17 18:24:49 +00:00
use itertools::Itertools;
use std::fmt;
2022-07-17 18:24:49 +00:00
use crate::{arg, assembler};
2022-07-17 18:24:49 +00:00
2022-08-23 14:20:38 +00:00
#[derive(Debug, Clone)]
2022-07-17 18:24:49 +00:00
pub enum Node {
Comment(String),
Label(String),
Call { name: String, arg: Option<arg::Arg> },
MacroCall { name: String, args: Vec<arg::Arg> },
}
impl fmt::Display for Node {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2022-07-17 18:24:49 +00:00
match self {
Node::Comment(x) => write!(f, "; {}", x),
Node::Label(x) => write!(f, "{}:", x),
2022-07-17 18:24:49 +00:00
Node::Call { name, arg } => {
if let Some(a) = arg {
write!(f, "{} {}", name, a)
2022-07-17 18:24:49 +00:00
} else {
write!(f, "{}", name)
2022-07-17 18:24:49 +00:00
}
}
Node::MacroCall { name, args } => {
if args.is_empty() {
write!(f, ".{}", name)
2022-07-17 18:24:49 +00:00
} else {
write!(f, ".{} {}", name, args.iter().join(", "))
2022-07-17 18:24:49 +00:00
}
}
}
}
}
impl assembler::ToCode for Node {}
2022-08-28 10:31:56 +00:00
pub type Body = Vec<Node>;
2022-08-23 14:20:38 +00:00
#[derive(Debug, Clone)]
2022-07-17 18:24:49 +00:00
pub struct AST {
pub body: Vec<Node>,
}
impl fmt::Display for AST {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", itertools::free::join(&self.body, "\n"))
2022-07-17 18:24:49 +00:00
}
}
impl assembler::ToCode for AST {}