wip:
Add type system and refator for having only epression
This commit is contained in:
+16
-67
@@ -5,7 +5,7 @@
|
||||
|
||||
use std::fmt::{self, Write};
|
||||
|
||||
use crate::common::ast::{AstNode, AstNodeKind, Expr, Stmt};
|
||||
use crate::common::ast::{AstNode, Expr};
|
||||
|
||||
/// Configuration for pretty printing output
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -264,33 +264,7 @@ impl PrettyPrint for Expr {
|
||||
write!(f, "] }}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PrettyPrint for Stmt {
|
||||
fn pretty_print(&self, ctx: &PrettyContext, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
if ctx.should_truncate() {
|
||||
return write!(f, "{}...", ctx.indent());
|
||||
}
|
||||
|
||||
let indent = ctx.indent();
|
||||
|
||||
match self {
|
||||
Stmt::Expression { expression, .. } => {
|
||||
if ctx.config.compact {
|
||||
let expr_str =
|
||||
pretty_print_with_config(expression.as_ref(), &PrettyConfig::compact());
|
||||
write!(f, "{};", expr_str)
|
||||
} else {
|
||||
writeln!(f, "{}Expression {{", indent)?;
|
||||
write!(f, "{}expression: ", ctx.child_context(true).indent())?;
|
||||
expression.pretty_print(&ctx.child_context(true), f)?;
|
||||
writeln!(f)?;
|
||||
write!(f, "{}}}", indent)
|
||||
}
|
||||
}
|
||||
Stmt::Print { expression, .. } => {
|
||||
Expr::Print { expression } => {
|
||||
if ctx.config.compact {
|
||||
let expr_str =
|
||||
pretty_print_with_config(expression.as_ref(), &PrettyConfig::compact());
|
||||
@@ -303,7 +277,7 @@ impl PrettyPrint for Stmt {
|
||||
write!(f, "{}}}", indent)
|
||||
}
|
||||
}
|
||||
Stmt::VarDeclaration {
|
||||
Expr::VarDeclaration {
|
||||
name, initializer, ..
|
||||
} => {
|
||||
if ctx.config.compact {
|
||||
@@ -331,8 +305,7 @@ impl PrettyPrint for Stmt {
|
||||
write!(f, "{}}}", indent)
|
||||
}
|
||||
}
|
||||
|
||||
Stmt::Return { expression, .. } => {
|
||||
Expr::Return { expression, .. } => {
|
||||
if ctx.config.compact {
|
||||
let expr_str =
|
||||
pretty_print_with_config(expression.as_ref(), &PrettyConfig::compact());
|
||||
@@ -345,9 +318,7 @@ impl PrettyPrint for Stmt {
|
||||
write!(f, "{}}}", indent)
|
||||
}
|
||||
}
|
||||
Stmt::Block {
|
||||
statements, label, ..
|
||||
} => {
|
||||
Expr::Block { statements, label } => {
|
||||
if ctx.config.compact {
|
||||
write!(f, "{{ ... [{}] ({} statements) }}", label, statements.len())
|
||||
} else {
|
||||
@@ -366,12 +337,11 @@ impl PrettyPrint for Stmt {
|
||||
write!(f, "{}}}", indent)
|
||||
}
|
||||
}
|
||||
Stmt::If {
|
||||
Expr::If {
|
||||
condition,
|
||||
then_branch,
|
||||
elif_branch,
|
||||
elif_branches,
|
||||
else_branch,
|
||||
..
|
||||
} => {
|
||||
if ctx.config.compact {
|
||||
let cond_str =
|
||||
@@ -386,10 +356,10 @@ impl PrettyPrint for Stmt {
|
||||
then_branch.pretty_print(&ctx.child_context(false), f)?;
|
||||
writeln!(f, ",")?;
|
||||
|
||||
if !elif_branch.is_empty() {
|
||||
if !elif_branches.is_empty() {
|
||||
writeln!(f, "{}elif_branches: [", ctx.child_context(false).indent())?;
|
||||
for (i, (cond, branch)) in elif_branch.iter().enumerate() {
|
||||
let is_last = i == elif_branch.len() - 1;
|
||||
for (i, (cond, branch)) in elif_branches.iter().enumerate() {
|
||||
let is_last = i == elif_branches.len() - 1;
|
||||
let child_ctx = ctx.child_context(false).child_context(is_last);
|
||||
writeln!(f, "{}(", child_ctx.indent())?;
|
||||
write!(f, "{}condition: ", child_ctx.child_context(false).indent())?;
|
||||
@@ -421,21 +391,18 @@ impl PrettyPrint for Stmt {
|
||||
write!(f, "{}}}", indent)
|
||||
}
|
||||
}
|
||||
Stmt::While {
|
||||
condition, body, ..
|
||||
} => {
|
||||
Expr::While { condition, body } => {
|
||||
write!(f, "{}while (", ctx.child_context(true).indent())?;
|
||||
condition.pretty_print(&ctx.child_context(true), f)?;
|
||||
writeln!(f, ") {{")?;
|
||||
body.pretty_print(&ctx.child_context(true), f)?;
|
||||
writeln!(f, "{}}}", ctx.child_context(true).indent())
|
||||
}
|
||||
Stmt::For {
|
||||
Expr::For {
|
||||
variable,
|
||||
condition,
|
||||
increment,
|
||||
body,
|
||||
..
|
||||
} => {
|
||||
write!(f, "{}for (", ctx.child_context(true).indent())?;
|
||||
variable.pretty_print(&ctx.child_context(true), f)?;
|
||||
@@ -451,7 +418,7 @@ impl PrettyPrint for Stmt {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AstNodeKind + fmt::Debug + fmt::Display + PrettyPrint> PrettyPrint for AstNode<T> {
|
||||
impl PrettyPrint for AstNode {
|
||||
fn pretty_print(&self, ctx: &PrettyContext, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let indent = ctx.indent();
|
||||
|
||||
@@ -463,9 +430,9 @@ impl<T: AstNodeKind + fmt::Debug + fmt::Display + PrettyPrint> PrettyPrint for A
|
||||
if ctx.config.show_types {
|
||||
writeln!(
|
||||
f,
|
||||
"{}kind: {:?},",
|
||||
"{}type: {},",
|
||||
ctx.child_context(false).indent(),
|
||||
self.node.kind()
|
||||
self.return_type
|
||||
)?;
|
||||
}
|
||||
|
||||
@@ -519,25 +486,7 @@ impl PrettyPrintExt for Expr {
|
||||
}
|
||||
}
|
||||
|
||||
impl PrettyPrintExt for Stmt {
|
||||
fn pretty(&self) -> String {
|
||||
pretty_print(self)
|
||||
}
|
||||
|
||||
fn pretty_compact(&self) -> String {
|
||||
pretty_print_with_config(self, &PrettyConfig::compact())
|
||||
}
|
||||
|
||||
fn pretty_tree(&self) -> String {
|
||||
pretty_print_with_config(self, &PrettyConfig::tree())
|
||||
}
|
||||
|
||||
fn pretty_with_config(&self, config: &PrettyConfig) -> String {
|
||||
pretty_print_with_config(self, config)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AstNodeKind + fmt::Debug + fmt::Display + PrettyPrint> PrettyPrintExt for AstNode<T> {
|
||||
impl PrettyPrintExt for AstNode {
|
||||
fn pretty(&self) -> String {
|
||||
pretty_print(self)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user