Broken structure now gets through syntax evaluator.

This commit is contained in:
2024-03-08 18:39:13 -07:00
parent 77c4277625
commit b0cc2fc26b
12 changed files with 389 additions and 50 deletions

View File

@@ -1,4 +1,4 @@
use crate::syntax::ast::{ConstantType, Expression, Program, Statement, TopLevel, Value};
use crate::syntax::ast::{ConstantType, Expression, Program, Statement, TopLevel, Type, Value};
use crate::util::pretty::{derived_display, Allocator};
use pretty::{DocAllocator, DocBuilder};
@@ -21,7 +21,7 @@ impl TopLevel {
pub fn pretty<'a>(&self, allocator: &'a Allocator<'a>) -> DocBuilder<'a, Allocator<'a>> {
match self {
TopLevel::Statement(stmt) => stmt.pretty(allocator),
TopLevel::Function(name, arg_names, body) => allocator
TopLevel::Function(name, args, rettype, body) => allocator
.text("function")
.append(allocator.space())
.append(
@@ -32,13 +32,61 @@ impl TopLevel {
.append(
allocator
.intersperse(
arg_names.iter().map(|x| allocator.text(x.to_string())),
args.iter().map(|(x, t)| {
allocator.text(x.to_string()).append(
t.as_ref()
.map(|t| {
allocator
.text(":")
.append(allocator.space())
.append(t.pretty(allocator))
})
.unwrap_or_else(|| allocator.nil()),
)
}),
allocator.text(","),
)
.parens(),
)
.append(
rettype
.as_ref()
.map(|rettype| {
allocator
.space()
.append(allocator.text("->"))
.append(allocator.space())
.append(rettype.pretty(allocator))
})
.unwrap_or_else(|| allocator.nil()),
)
.append(allocator.space())
.append(body.pretty(allocator)),
TopLevel::Structure(_, name, fields) => allocator
.text("struct")
.append(allocator.space())
.append(
name.as_ref()
.map(|x| allocator.text(x.to_string()))
.unwrap_or_else(|| allocator.nil()),
)
.append(allocator.space())
.append(allocator.text("{"))
.append(allocator.hardline())
.append(
allocator
.concat(fields.iter().map(|(name, ty)| {
allocator
.text(name.to_string())
.append(allocator.text(":"))
.append(allocator.space())
.append(ty.pretty(allocator))
.append(allocator.text(";"))
.append(allocator.hardline())
}))
.nest(2),
)
.append(allocator.text("}")),
}
}
}
@@ -65,7 +113,28 @@ impl Expression {
pub fn pretty<'a>(&self, allocator: &'a Allocator<'a>) -> DocBuilder<'a, Allocator<'a>> {
match self {
Expression::Value(_, val) => val.pretty(allocator),
Expression::Constructor(_, name, fields) => allocator
.text(name.to_string())
.append(allocator.space())
.append(
allocator
.concat(fields.iter().map(|(n, e)| {
allocator
.text(n.to_string())
.append(allocator.text(":"))
.append(allocator.space())
.append(e.pretty(allocator))
.append(allocator.text(";"))
.append(allocator.hardline())
}))
.nest(2)
.braces(),
),
Expression::Reference(_, var) => allocator.text(var.to_string()),
Expression::FieldRef(_, val, field) => val
.pretty(allocator)
.append(allocator.text("."))
.append(allocator.text(field.to_string())),
Expression::Cast(_, t, e) => allocator
.text(t.clone())
.angles()
@@ -151,6 +220,42 @@ fn type_suffix(x: &Option<ConstantType>) -> &'static str {
}
}
impl Type {
pub fn pretty<'a>(&self, allocator: &'a Allocator<'a>) -> DocBuilder<'a, Allocator<'a>> {
match self {
Type::Named(x) => allocator.text(x.to_string()),
Type::Struct(name, fields) => allocator
.text("struct")
.append(allocator.space())
.append(
name.as_ref()
.map(|x| allocator.text(x.to_string()))
.unwrap_or_else(|| allocator.nil()),
)
.append(allocator.intersperse(
fields.iter().map(|(name, ty)| {
allocator
.text(
name.as_ref()
.map(|x| x.to_string())
.unwrap_or_else(|| "_".to_string()),
)
.append(allocator.text(":"))
.append(allocator.space())
.append(
ty.as_ref()
.map(|x| x.pretty(allocator))
.unwrap_or_else(|| allocator.text("_")),
)
.append(allocator.text(";"))
}),
allocator.hardline(),
))
.braces(),
}
}
}
derived_display!(Program);
derived_display!(TopLevel);
derived_display!(Statement);