2024-01-21 05:32:18 -06:00
|
|
|
//! Implementations of AST nodes and traits
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
mod display {
|
|
|
|
//! Implements [Display] for [AST](super::super) Types
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
use super::*;
|
2024-04-18 20:47:28 -05:00
|
|
|
use format::{delimiters::*, *};
|
2024-04-13 03:33:26 -05:00
|
|
|
use std::{
|
|
|
|
borrow::Borrow,
|
|
|
|
fmt::{Display, Write},
|
|
|
|
};
|
2024-04-18 20:47:28 -05:00
|
|
|
|
|
|
|
fn separate<I: Display, W: Write>(
|
|
|
|
iterable: impl IntoIterator<Item = I>,
|
|
|
|
sep: &'static str,
|
|
|
|
) -> impl FnOnce(W) -> std::fmt::Result {
|
|
|
|
move |mut f| {
|
|
|
|
for (idx, item) in iterable.into_iter().enumerate() {
|
2024-01-21 05:32:18 -06:00
|
|
|
if idx > 0 {
|
2024-04-18 20:47:28 -05:00
|
|
|
f.write_str(sep)?;
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
write!(f, "{item}")?;
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Mutability {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Mutability::Not => Ok(()),
|
|
|
|
Mutability::Mut => "mut ".fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Visibility {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Visibility::Private => Ok(()),
|
|
|
|
Visibility::Public => "pub ".fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-18 20:47:28 -05:00
|
|
|
impl Display for Literal {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Literal::Bool(v) => v.fmt(f),
|
2024-07-26 05:51:20 -05:00
|
|
|
Literal::Char(v) => write!(f, "'{}'", v.escape_debug()),
|
2024-04-18 20:47:28 -05:00
|
|
|
Literal::Int(v) => v.fmt(f),
|
2024-09-19 14:00:22 -05:00
|
|
|
Literal::Float(v) => write!(f, "{:?}", f64::from_bits(*v)),
|
2024-07-26 05:51:20 -05:00
|
|
|
Literal::String(v) => write!(f, "\"{}\"", v.escape_debug()),
|
2024-04-18 20:47:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for File {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-16 23:45:54 -05:00
|
|
|
separate(&self.items, "\n\n")(f)
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 23:31:49 -06:00
|
|
|
impl Display for Attrs {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { meta } = self;
|
|
|
|
if meta.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
"#".fmt(f)?;
|
2024-04-18 20:47:28 -05:00
|
|
|
separate(meta, ", ")(&mut f.delimit(INLINE_SQUARE))?;
|
2024-02-27 23:31:49 -06:00
|
|
|
"\n".fmt(f)
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-02-27 23:31:49 -06:00
|
|
|
impl Display for Meta {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { name, kind } = self;
|
|
|
|
write!(f, "{name}{kind}")
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-02-27 23:31:49 -06:00
|
|
|
impl Display for MetaKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
MetaKind::Plain => Ok(()),
|
|
|
|
MetaKind::Equals(v) => write!(f, " = {v}"),
|
2024-04-18 20:47:28 -05:00
|
|
|
MetaKind::Func(args) => separate(args, ", ")(f.delimit(INLINE_PARENS)),
|
2024-02-27 23:31:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Item {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-02-27 23:31:49 -06:00
|
|
|
let Self { extents: _, attrs, vis, kind } = self;
|
|
|
|
attrs.fmt(f)?;
|
|
|
|
vis.fmt(f)?;
|
2024-04-18 20:47:28 -05:00
|
|
|
kind.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for ItemKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-02-27 20:49:02 -06:00
|
|
|
ItemKind::Alias(v) => v.fmt(f),
|
2024-01-21 05:32:18 -06:00
|
|
|
ItemKind::Const(v) => v.fmt(f),
|
|
|
|
ItemKind::Static(v) => v.fmt(f),
|
|
|
|
ItemKind::Module(v) => v.fmt(f),
|
|
|
|
ItemKind::Function(v) => v.fmt(f),
|
|
|
|
ItemKind::Struct(v) => v.fmt(f),
|
|
|
|
ItemKind::Enum(v) => v.fmt(f),
|
|
|
|
ItemKind::Impl(v) => v.fmt(f),
|
2024-04-20 14:51:54 -05:00
|
|
|
ItemKind::Use(v) => v.fmt(f),
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-02-27 20:49:02 -06:00
|
|
|
impl Display for Alias {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { to, from } = self;
|
|
|
|
match from {
|
|
|
|
Some(from) => write!(f, "type {to} = {from};"),
|
|
|
|
None => write!(f, "type {to};"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Const {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { name, ty, init } = self;
|
|
|
|
write!(f, "const {name}: {ty} = {init}")
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Static {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { mutable, name, ty, init } = self;
|
|
|
|
write!(f, "static {mutable}{name}: {ty} = {init}")
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Module {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { name, kind } = self;
|
|
|
|
write!(f, "mod {name}{kind}")
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for ModuleKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
ModuleKind::Inline(items) => {
|
|
|
|
' '.fmt(f)?;
|
2024-04-18 20:47:28 -05:00
|
|
|
write!(f.delimit(BRACES), "{items}")
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
ModuleKind::Outline => ';'.fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Function {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-16 20:31:23 -05:00
|
|
|
let Self { name, sign: sign @ TyFn { args, rety }, bind, body } = self;
|
|
|
|
let types = match **args {
|
|
|
|
TyKind::Tuple(TyTuple { ref types }) => types.as_slice(),
|
|
|
|
TyKind::Empty => Default::default(),
|
|
|
|
_ => {
|
|
|
|
write!(f, "Invalid function signature: {sign}")?;
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
debug_assert_eq!(bind.len(), types.len());
|
2024-01-21 05:32:18 -06:00
|
|
|
write!(f, "fn {name} ")?;
|
2024-04-18 20:47:28 -05:00
|
|
|
{
|
|
|
|
let mut f = f.delimit(INLINE_PARENS);
|
|
|
|
for (idx, (arg, ty)) in bind.iter().zip(types.iter()).enumerate() {
|
|
|
|
if idx != 0 {
|
|
|
|
f.write_str(", ")?;
|
2024-04-16 20:31:23 -05:00
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
write!(f, "{arg}: {ty}")?;
|
|
|
|
}
|
|
|
|
}
|
2024-01-21 05:32:18 -06:00
|
|
|
if let Some(rety) = rety {
|
|
|
|
write!(f, " -> {rety}")?;
|
|
|
|
}
|
|
|
|
match body {
|
|
|
|
Some(body) => write!(f, " {body}"),
|
|
|
|
None => ';'.fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Param {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-16 20:31:23 -05:00
|
|
|
let Self { mutability, name } = self;
|
|
|
|
write!(f, "{mutability}{name}")
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Struct {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { name, kind } = self;
|
|
|
|
write!(f, "struct {name}{kind}")
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for StructKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
StructKind::Empty => ';'.fmt(f),
|
2024-04-18 20:47:28 -05:00
|
|
|
StructKind::Tuple(v) => separate(v, ", ")(f.delimit(INLINE_PARENS)),
|
|
|
|
StructKind::Struct(v) => separate(v, ",\n")(f.delimit(SPACED_BRACES)),
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for StructMember {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { vis, name, ty } = self;
|
|
|
|
write!(f, "{vis}{name}: {ty}")
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Enum {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { name, kind } = self;
|
|
|
|
write!(f, "enum {name}{kind}")
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for EnumKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-04-01 04:18:31 -05:00
|
|
|
EnumKind::NoVariants => ';'.fmt(f),
|
2024-04-18 20:47:28 -05:00
|
|
|
EnumKind::Variants(v) => separate(v, ",\n")(f.delimit(SPACED_BRACES)),
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Variant {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { name, kind } = self;
|
|
|
|
write!(f, "{name}{kind}")
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for VariantKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-02-26 15:59:15 -06:00
|
|
|
VariantKind::Plain => Ok(()),
|
2024-04-01 04:18:31 -05:00
|
|
|
VariantKind::CLike(n) => write!(f, " = {n}"),
|
2024-04-16 20:40:02 -05:00
|
|
|
VariantKind::Tuple(v) => v.fmt(f),
|
2024-04-18 20:47:28 -05:00
|
|
|
VariantKind::Struct(v) => separate(v, ", ")(f.delimit(INLINE_BRACES)),
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Impl {
|
2024-04-14 18:01:30 -05:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { target, body } = self;
|
|
|
|
write!(f, "impl {target} ")?;
|
2024-04-18 20:47:28 -05:00
|
|
|
write!(f.delimit(BRACES), "{body}")
|
2024-04-14 18:01:30 -05:00
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-04-14 18:01:30 -05:00
|
|
|
impl Display for ImplKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
ImplKind::Type(t) => t.fmt(f),
|
|
|
|
ImplKind::Trait { impl_trait, for_type } => {
|
|
|
|
write!(f, "{impl_trait} for {for_type}")
|
|
|
|
}
|
|
|
|
}
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-20 14:51:54 -05:00
|
|
|
impl Display for Use {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-21 18:57:46 -05:00
|
|
|
let Self { absolute, tree } = self;
|
2024-04-29 16:19:52 -05:00
|
|
|
f.write_str(if *absolute { "use ::" } else { "use " })?;
|
|
|
|
write!(f, "{tree};")
|
2024-04-20 14:51:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for UseTree {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-04-21 18:57:46 -05:00
|
|
|
UseTree::Tree(tree) => separate(tree, ", ")(f.delimit(INLINE_BRACES)),
|
|
|
|
UseTree::Path(path, rest) => write!(f, "{path}::{rest}"),
|
2024-04-20 14:51:54 -05:00
|
|
|
UseTree::Alias(path, name) => write!(f, "{path} as {name}"),
|
2024-04-21 18:57:46 -05:00
|
|
|
UseTree::Name(name) => write!(f, "{name}"),
|
2024-04-20 14:51:54 -05:00
|
|
|
UseTree::Glob => write!(f, "*"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Ty {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-14 23:10:02 -05:00
|
|
|
self.kind.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-04-14 23:10:02 -05:00
|
|
|
impl Display for TyKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-01-21 05:32:18 -06:00
|
|
|
TyKind::Never => "!".fmt(f),
|
|
|
|
TyKind::Empty => "()".fmt(f),
|
|
|
|
TyKind::Path(v) => v.fmt(f),
|
2024-07-20 18:22:50 -05:00
|
|
|
TyKind::Array(v) => v.fmt(f),
|
|
|
|
TyKind::Slice(v) => v.fmt(f),
|
2024-01-21 05:32:18 -06:00
|
|
|
TyKind::Tuple(v) => v.fmt(f),
|
|
|
|
TyKind::Ref(v) => v.fmt(f),
|
|
|
|
TyKind::Fn(v) => v.fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-07-20 18:22:50 -05:00
|
|
|
impl Display for TyArray {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { ty, count } = self;
|
|
|
|
write!(f, "[{ty}; {count}]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for TySlice {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { ty } = self;
|
|
|
|
write!(f, "[{ty}]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for TyTuple {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-18 20:47:28 -05:00
|
|
|
separate(&self.types, ", ")(f.delimit(INLINE_PARENS))
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for TyRef {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-16 20:35:27 -05:00
|
|
|
let &Self { count, mutable, ref to } = self;
|
|
|
|
for _ in 0..count {
|
2024-01-21 05:32:18 -06:00
|
|
|
f.write_char('&')?;
|
|
|
|
}
|
2024-04-16 20:35:27 -05:00
|
|
|
write!(f, "{mutable}{to}")
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for TyFn {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { args, rety } = self;
|
|
|
|
write!(f, "fn {args}")?;
|
|
|
|
match rety {
|
|
|
|
Some(v) => write!(f, " -> {v}"),
|
|
|
|
None => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-18 20:47:28 -05:00
|
|
|
impl Display for Path {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { absolute, parts } = self;
|
|
|
|
if *absolute {
|
|
|
|
"::".fmt(f)?;
|
|
|
|
}
|
|
|
|
separate(parts, "::")(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for PathPart {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
PathPart::SuperKw => "super".fmt(f),
|
|
|
|
PathPart::SelfKw => "self".fmt(f),
|
2024-07-17 15:05:52 -05:00
|
|
|
PathPart::SelfTy => "Self".fmt(f),
|
2024-04-18 20:47:28 -05:00
|
|
|
PathPart::Ident(id) => id.fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Stmt {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Stmt { extents: _, kind, semi } = self;
|
2024-04-18 20:47:28 -05:00
|
|
|
write!(f, "{kind}{semi}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for StmtKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-01-21 05:32:18 -06:00
|
|
|
StmtKind::Empty => Ok(()),
|
|
|
|
StmtKind::Item(v) => v.fmt(f),
|
|
|
|
StmtKind::Expr(v) => v.fmt(f),
|
2024-04-18 20:47:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Semi {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-01-21 05:32:18 -06:00
|
|
|
Semi::Terminated => ';'.fmt(f),
|
|
|
|
Semi::Unterminated => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Expr {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-13 03:33:26 -05:00
|
|
|
self.kind.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-04-13 03:33:26 -05:00
|
|
|
impl Display for ExprKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-04-18 20:47:28 -05:00
|
|
|
ExprKind::Empty => "()".fmt(f),
|
2025-01-29 03:56:19 -06:00
|
|
|
ExprKind::Quote(v) => v.fmt(f),
|
2024-07-30 18:02:09 -05:00
|
|
|
ExprKind::Let(v) => v.fmt(f),
|
2024-01-21 05:32:18 -06:00
|
|
|
ExprKind::Assign(v) => v.fmt(f),
|
2024-05-19 14:31:30 -05:00
|
|
|
ExprKind::Modify(v) => v.fmt(f),
|
2024-01-21 05:32:18 -06:00
|
|
|
ExprKind::Binary(v) => v.fmt(f),
|
|
|
|
ExprKind::Unary(v) => v.fmt(f),
|
2024-07-26 05:26:08 -05:00
|
|
|
ExprKind::Cast(v) => v.fmt(f),
|
2024-05-19 13:55:28 -05:00
|
|
|
ExprKind::Member(v) => v.fmt(f),
|
2024-01-21 05:32:18 -06:00
|
|
|
ExprKind::Index(v) => v.fmt(f),
|
2024-04-20 15:02:16 -05:00
|
|
|
ExprKind::Structor(v) => v.fmt(f),
|
2024-01-21 05:32:18 -06:00
|
|
|
ExprKind::Path(v) => v.fmt(f),
|
|
|
|
ExprKind::Literal(v) => v.fmt(f),
|
|
|
|
ExprKind::Array(v) => v.fmt(f),
|
|
|
|
ExprKind::ArrayRep(v) => v.fmt(f),
|
|
|
|
ExprKind::AddrOf(v) => v.fmt(f),
|
|
|
|
ExprKind::Block(v) => v.fmt(f),
|
|
|
|
ExprKind::Group(v) => v.fmt(f),
|
|
|
|
ExprKind::Tuple(v) => v.fmt(f),
|
|
|
|
ExprKind::While(v) => v.fmt(f),
|
|
|
|
ExprKind::If(v) => v.fmt(f),
|
|
|
|
ExprKind::For(v) => v.fmt(f),
|
2024-07-30 22:31:39 -05:00
|
|
|
ExprKind::Break(v) => v.fmt(f),
|
|
|
|
ExprKind::Return(v) => v.fmt(f),
|
2024-07-30 19:42:28 -05:00
|
|
|
ExprKind::Continue => "continue".fmt(f),
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2025-01-29 03:56:19 -06:00
|
|
|
impl Display for Quote {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { quote } = self;
|
|
|
|
write!(f, "`{quote}`")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 02:35:41 -05:00
|
|
|
impl Display for Let {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-07-31 03:19:20 -05:00
|
|
|
let Self { mutable, name, ty, init } = self;
|
2024-07-31 02:35:41 -05:00
|
|
|
write!(f, "let {mutable}{name}")?;
|
|
|
|
if let Some(value) = ty {
|
|
|
|
write!(f, ": {value}")?;
|
|
|
|
}
|
|
|
|
if let Some(value) = init {
|
|
|
|
write!(f, " = {value}")?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Assign {
|
2024-05-19 14:31:30 -05:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { parts } = self;
|
|
|
|
write!(f, "{} = {}", parts.0, parts.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Modify {
|
2024-01-21 05:32:18 -06:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-13 03:33:26 -05:00
|
|
|
let Self { kind, parts } = self;
|
|
|
|
write!(f, "{} {kind} {}", parts.0, parts.1)
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-05-19 14:31:30 -05:00
|
|
|
impl Display for ModifyKind {
|
2024-01-21 05:32:18 -06:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-05-19 14:31:30 -05:00
|
|
|
ModifyKind::Mul => "*=",
|
|
|
|
ModifyKind::Div => "/=",
|
|
|
|
ModifyKind::Rem => "%=",
|
|
|
|
ModifyKind::Add => "+=",
|
|
|
|
ModifyKind::Sub => "-=",
|
|
|
|
ModifyKind::And => "&=",
|
|
|
|
ModifyKind::Or => "|=",
|
|
|
|
ModifyKind::Xor => "^=",
|
|
|
|
ModifyKind::Shl => "<<=",
|
|
|
|
ModifyKind::Shr => ">>=",
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Binary {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-13 03:33:26 -05:00
|
|
|
let Self { kind, parts } = self;
|
|
|
|
let (head, tail) = parts.borrow();
|
|
|
|
match kind {
|
|
|
|
BinaryKind::Call => write!(f, "{head}{tail}"),
|
|
|
|
_ => write!(f, "{head} {kind} {tail}"),
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for BinaryKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
BinaryKind::Lt => "<",
|
|
|
|
BinaryKind::LtEq => "<=",
|
|
|
|
BinaryKind::Equal => "==",
|
|
|
|
BinaryKind::NotEq => "!=",
|
|
|
|
BinaryKind::GtEq => ">=",
|
|
|
|
BinaryKind::Gt => ">",
|
|
|
|
BinaryKind::RangeExc => "..",
|
|
|
|
BinaryKind::RangeInc => "..=",
|
|
|
|
BinaryKind::LogAnd => "&&",
|
|
|
|
BinaryKind::LogOr => "||",
|
|
|
|
BinaryKind::LogXor => "^^",
|
|
|
|
BinaryKind::BitAnd => "&",
|
|
|
|
BinaryKind::BitOr => "|",
|
|
|
|
BinaryKind::BitXor => "^",
|
|
|
|
BinaryKind::Shl => "<<",
|
|
|
|
BinaryKind::Shr => ">>",
|
|
|
|
BinaryKind::Add => "+",
|
|
|
|
BinaryKind::Sub => "-",
|
|
|
|
BinaryKind::Mul => "*",
|
|
|
|
BinaryKind::Div => "/",
|
|
|
|
BinaryKind::Rem => "%",
|
2024-04-13 03:33:26 -05:00
|
|
|
BinaryKind::Call => "()",
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Unary {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-13 03:33:26 -05:00
|
|
|
let Self { kind, tail } = self;
|
|
|
|
write!(f, "{kind}{tail}")
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for UnaryKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-07-30 18:21:25 -05:00
|
|
|
UnaryKind::Loop => "loop ",
|
2024-01-21 05:32:18 -06:00
|
|
|
UnaryKind::Deref => "*",
|
|
|
|
UnaryKind::Neg => "-",
|
|
|
|
UnaryKind::Not => "!",
|
|
|
|
UnaryKind::At => "@",
|
|
|
|
UnaryKind::Tilde => "~",
|
|
|
|
}
|
|
|
|
.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-07-26 05:26:08 -05:00
|
|
|
impl Display for Cast {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { head, ty } = self;
|
|
|
|
write!(f, "{head} as {ty}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-19 13:55:28 -05:00
|
|
|
impl Display for Member {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { head, kind } = self;
|
|
|
|
write!(f, "{head}.{kind}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for MemberKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
MemberKind::Call(name, args) => write!(f, "{name}{args}"),
|
|
|
|
MemberKind::Struct(name) => write!(f, "{name}"),
|
|
|
|
MemberKind::Tuple(name) => write!(f, "{name}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Index {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { head, indices } = self;
|
|
|
|
write!(f, "{head}")?;
|
2024-04-18 20:47:28 -05:00
|
|
|
separate(indices, ", ")(f.delimit(INLINE_SQUARE))
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-04-20 15:02:16 -05:00
|
|
|
impl Display for Structor {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { to, init } = self;
|
|
|
|
write!(f, "{to}: ")?;
|
|
|
|
separate(init, ", ")(f.delimit(INLINE_BRACES))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Fielder {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { name, init } = self;
|
|
|
|
write!(f, "{name}")?;
|
|
|
|
if let Some(init) = init {
|
|
|
|
write!(f, ": {init}")?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Array {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-18 20:47:28 -05:00
|
|
|
separate(&self.values, ", ")(f.delimit(INLINE_SQUARE))
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for ArrayRep {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { value, repeat } = self;
|
|
|
|
write!(f, "[{value}; {repeat}]")
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for AddrOf {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2025-01-29 03:31:24 -06:00
|
|
|
let Self { mutable, expr } = self;
|
|
|
|
write!(f, "&{mutable}{expr}")
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Block {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-07-30 20:40:22 -05:00
|
|
|
let Self { stmts } = self;
|
|
|
|
|
|
|
|
match stmts.as_slice() {
|
|
|
|
[] => "{}".fmt(f),
|
|
|
|
stmts => separate(stmts, "\n")(f.delimit(BRACES)),
|
|
|
|
}
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Group {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "({})", self.expr)
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
|
|
|
impl Display for Tuple {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-07-30 20:40:22 -05:00
|
|
|
let Self { exprs } = self;
|
|
|
|
|
|
|
|
match exprs.as_slice() {
|
|
|
|
[] => write!(f, "()"),
|
|
|
|
[expr] => write!(f, "({expr},)"),
|
|
|
|
exprs => separate(exprs, ", ")(f.delimit(INLINE_PARENS)),
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for While {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { cond, pass, fail } = self;
|
|
|
|
write!(f, "while {cond} {pass}{fail}")
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for If {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { cond, pass, fail } = self;
|
|
|
|
write!(f, "if {cond} {pass}{fail}")
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for For {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let Self { bind, cond, pass, fail } = self;
|
|
|
|
write!(f, "for {bind} in {cond} {pass}{fail}")
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:47:28 -05:00
|
|
|
|
2024-01-21 05:32:18 -06:00
|
|
|
impl Display for Else {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match &self.body {
|
|
|
|
Some(body) => write!(f, " else {body}"),
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-30 22:31:39 -05:00
|
|
|
|
|
|
|
impl Display for Break {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "break")?;
|
|
|
|
match &self.body {
|
|
|
|
Some(body) => write!(f, " {body}"),
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Return {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "return")?;
|
|
|
|
match &self.body {
|
|
|
|
Some(body) => write!(f, " {body}"),
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
mod convert {
|
|
|
|
//! Converts between major enums and enum variants
|
|
|
|
use super::*;
|
|
|
|
|
2024-04-14 18:01:30 -05:00
|
|
|
impl<T: AsRef<str>> From<T> for PathPart {
|
|
|
|
fn from(value: T) -> Self {
|
|
|
|
match value.as_ref() {
|
2024-04-22 20:52:12 -05:00
|
|
|
"self" => PathPart::SelfKw,
|
2024-04-14 18:01:30 -05:00
|
|
|
"super" => PathPart::SuperKw,
|
|
|
|
ident => PathPart::Ident(ident.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-21 05:32:18 -06:00
|
|
|
|
|
|
|
macro impl_from ($(impl From for $T:ty {$($from:ty => $to:expr),*$(,)?})*) {$($(
|
|
|
|
impl From<$from> for $T {
|
|
|
|
fn from(value: $from) -> Self {
|
|
|
|
$to(value.into()) // Uses *tuple constructor*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<Box<$from>> for $T {
|
|
|
|
fn from(value: Box<$from>) -> Self {
|
|
|
|
$to((*value).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*)*}
|
|
|
|
|
|
|
|
impl_from! {
|
|
|
|
impl From for ItemKind {
|
2024-02-27 20:49:02 -06:00
|
|
|
Alias => ItemKind::Alias,
|
2024-01-21 05:32:18 -06:00
|
|
|
Const => ItemKind::Const,
|
|
|
|
Static => ItemKind::Static,
|
|
|
|
Module => ItemKind::Module,
|
|
|
|
Function => ItemKind::Function,
|
|
|
|
Struct => ItemKind::Struct,
|
|
|
|
Enum => ItemKind::Enum,
|
|
|
|
Impl => ItemKind::Impl,
|
2024-04-20 14:51:54 -05:00
|
|
|
Use => ItemKind::Use,
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
impl From for StructKind {
|
|
|
|
Vec<Ty> => StructKind::Tuple,
|
|
|
|
// TODO: Struct members in struct
|
|
|
|
}
|
|
|
|
impl From for EnumKind {
|
|
|
|
Vec<Variant> => EnumKind::Variants,
|
|
|
|
}
|
|
|
|
impl From for VariantKind {
|
2024-02-26 15:59:15 -06:00
|
|
|
u128 => VariantKind::CLike,
|
2024-04-16 20:35:27 -05:00
|
|
|
Ty => VariantKind::Tuple,
|
2024-01-21 05:32:18 -06:00
|
|
|
// TODO: enum struct variants
|
|
|
|
}
|
|
|
|
impl From for TyKind {
|
|
|
|
Path => TyKind::Path,
|
|
|
|
TyTuple => TyKind::Tuple,
|
|
|
|
TyRef => TyKind::Ref,
|
|
|
|
TyFn => TyKind::Fn,
|
|
|
|
}
|
|
|
|
impl From for StmtKind {
|
|
|
|
Item => StmtKind::Item,
|
2024-02-27 22:49:14 -06:00
|
|
|
Expr => StmtKind::Expr,
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
impl From for ExprKind {
|
2024-07-30 18:02:09 -05:00
|
|
|
Let => ExprKind::Let,
|
2025-01-29 03:56:19 -06:00
|
|
|
Quote => ExprKind::Quote,
|
2024-01-21 05:32:18 -06:00
|
|
|
Assign => ExprKind::Assign,
|
2024-05-19 14:31:30 -05:00
|
|
|
Modify => ExprKind::Modify,
|
2024-01-21 05:32:18 -06:00
|
|
|
Binary => ExprKind::Binary,
|
|
|
|
Unary => ExprKind::Unary,
|
2024-07-26 05:26:08 -05:00
|
|
|
Cast => ExprKind::Cast,
|
2024-05-19 13:55:28 -05:00
|
|
|
Member => ExprKind::Member,
|
2024-01-21 05:32:18 -06:00
|
|
|
Index => ExprKind::Index,
|
|
|
|
Path => ExprKind::Path,
|
|
|
|
Literal => ExprKind::Literal,
|
|
|
|
Array => ExprKind::Array,
|
|
|
|
ArrayRep => ExprKind::ArrayRep,
|
|
|
|
AddrOf => ExprKind::AddrOf,
|
|
|
|
Block => ExprKind::Block,
|
|
|
|
Group => ExprKind::Group,
|
|
|
|
Tuple => ExprKind::Tuple,
|
|
|
|
While => ExprKind::While,
|
|
|
|
If => ExprKind::If,
|
|
|
|
For => ExprKind::For,
|
2024-07-30 22:31:39 -05:00
|
|
|
Break => ExprKind::Break,
|
|
|
|
Return => ExprKind::Return,
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
impl From for Literal {
|
|
|
|
bool => Literal::Bool,
|
|
|
|
char => Literal::Char,
|
|
|
|
u128 => Literal::Int,
|
2024-04-13 03:33:26 -05:00
|
|
|
String => Literal::String,
|
2024-01-21 05:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Option<Expr>> for Else {
|
|
|
|
fn from(value: Option<Expr>) -> Self {
|
|
|
|
Self { body: value.map(Into::into) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<Expr> for Else {
|
|
|
|
fn from(value: Expr) -> Self {
|
|
|
|
Self { body: Some(value.into()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 21:04:30 -05:00
|
|
|
|
|
|
|
mod path {
|
|
|
|
//! Utils for [Path]
|
2024-05-19 14:41:31 -05:00
|
|
|
use crate::{ast::Path, PathPart, Sym};
|
2024-04-22 21:04:30 -05:00
|
|
|
|
|
|
|
impl Path {
|
|
|
|
/// Appends a [PathPart] to this [Path]
|
|
|
|
pub fn push(&mut self, part: PathPart) {
|
|
|
|
self.parts.push(part);
|
|
|
|
}
|
|
|
|
/// Removes a [PathPart] from this [Path]
|
|
|
|
pub fn pop(&mut self) -> Option<PathPart> {
|
|
|
|
self.parts.pop()
|
|
|
|
}
|
|
|
|
/// Concatenates `self::other`. If `other` is an absolute [Path],
|
|
|
|
/// this replaces `self` with `other`
|
2024-04-29 16:19:52 -05:00
|
|
|
pub fn concat(mut self, other: &Self) -> Self {
|
2024-04-22 21:04:30 -05:00
|
|
|
if other.absolute {
|
2024-04-29 16:19:52 -05:00
|
|
|
other.clone()
|
2024-04-22 21:04:30 -05:00
|
|
|
} else {
|
2024-04-29 16:19:52 -05:00
|
|
|
self.parts.extend(other.parts.iter().cloned());
|
|
|
|
self
|
2024-04-22 21:04:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl PathPart {
|
2024-05-19 14:41:31 -05:00
|
|
|
pub fn from_sym(ident: Sym) -> Self {
|
2024-04-22 21:04:30 -05:00
|
|
|
Self::Ident(ident)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|