feat(ast): Add graph output
This commit is contained in:
+110
-1
@@ -1,4 +1,5 @@
|
||||
use crate::{diagnostic::span::Span, frontend::types::{TokenValue, TypeIdent}};
|
||||
use std::fmt;
|
||||
|
||||
pub struct CompileUnit {
|
||||
pub global_decls: Vec<GlobalDeclStmt>,
|
||||
@@ -109,4 +110,112 @@ pub struct Param {
|
||||
pub name: String,
|
||||
pub param_type: Type,
|
||||
pub span: Span,
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CompileUnit {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "CompileUnit")
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for GlobalDeclStmt {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
GlobalDeclStmt::VarDecl(_) => write!(f, "GlobalVarDecl"),
|
||||
GlobalDeclStmt::FuncDecl(_) => write!(f, "FuncDecl"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for VarDeclStmt {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "VarDecl")
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for VarDeclStmtValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{} {}", self.var_type, self.name)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FuncDeclStmt {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{} {}", self.return_type, self.name)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for BlockStmt {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Block")
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Statement {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Statement::Return(_) => write!(f, "ReturnStmt"),
|
||||
Statement::Block(_) => write!(f, "BlockStmt"),
|
||||
Statement::Expr(_) => write!(f, "ExprStmt"),
|
||||
Statement::VarDecl(_) => write!(f, "VarDeclStmt"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ReturnStmt {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "ReturnStmt")
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Expr {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ExprValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
ExprValue::IntLit(value) => write!(f, "IntLit({})", value),
|
||||
ExprValue::Var(name) => write!(f, "Var({})", name),
|
||||
ExprValue::BinaryOp { op, .. } => write!(f, "BinaryOp({})", op),
|
||||
ExprValue::FuncCall(name, _) => write!(f, "FuncCall({})", name),
|
||||
ExprValue::Assign { .. } => write!(f, "Assign"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Param {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{} {}", self.param_type, self.name)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for BinaryOp {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let op = match self {
|
||||
BinaryOp::Add => "+",
|
||||
BinaryOp::Sub => "-",
|
||||
BinaryOp::Mul => "*",
|
||||
BinaryOp::Div => "/",
|
||||
BinaryOp::Mod => "%",
|
||||
BinaryOp::Equal => "==",
|
||||
BinaryOp::NotEqual => "!=",
|
||||
BinaryOp::Less => "<",
|
||||
BinaryOp::LessEqual => "<=",
|
||||
BinaryOp::Greater => ">",
|
||||
BinaryOp::GreaterEqual => ">=",
|
||||
};
|
||||
write!(f, "{}", op)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Type {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Type::Int => write!(f, "int"),
|
||||
Type::Void => write!(f, "void"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user