cl-ast: Clean up "to", "extents", Module."kind"

This commit is contained in:
John 2025-03-14 00:52:43 -05:00
parent 11c8daaed0
commit 33e13425a9
13 changed files with 95 additions and 99 deletions

View File

@ -62,7 +62,7 @@ pub enum MetaKind {
/// Anything that can appear at the top level of a [File]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Item {
pub extents: Span,
pub span: Span,
pub attrs: Attrs,
pub vis: Visibility,
pub kind: ItemKind,
@ -96,13 +96,13 @@ pub enum ItemKind {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Module {
pub name: Sym,
pub kind: Option<File>,
pub file: Option<File>,
}
/// An alias to another [Ty]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Alias {
pub to: Sym,
pub name: Sym,
pub from: Option<Box<Ty>>,
}
@ -212,7 +212,7 @@ pub enum UseTree {
/// A type expression
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Ty {
pub extents: Span,
pub span: Span,
pub kind: TyKind,
}
@ -283,7 +283,7 @@ pub enum PathPart {
/// An abstract statement, and associated metadata
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Stmt {
pub extents: Span,
pub span: Span,
pub kind: StmtKind,
pub semi: Semi,
}
@ -306,7 +306,7 @@ pub enum Semi {
/// An expression, the beating heart of the language
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Expr {
pub extents: Span,
pub span: Span,
pub kind: ExprKind,
}

View File

@ -93,7 +93,7 @@ mod display {
impl Display for Item {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { extents: _, attrs, vis, kind } = self;
let Self { span: _, attrs, vis, kind } = self;
attrs.fmt(f)?;
vis.fmt(f)?;
kind.fmt(f)
@ -118,10 +118,10 @@ mod display {
impl Display for Alias {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { to, from } = self;
let Self { name, from } = self;
match from {
Some(from) => write!(f, "type {to} = {from};"),
None => write!(f, "type {to};"),
Some(from) => write!(f, "type {name} = {from};"),
None => write!(f, "type {name};"),
}
}
}
@ -142,9 +142,9 @@ mod display {
impl Display for Module {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self;
let Self { name, file } = self;
write!(f, "mod {name}")?;
match kind {
match file {
Some(items) => {
' '.fmt(f)?;
write!(f.delimit(BRACES), "{items}")
@ -365,7 +365,7 @@ mod display {
impl Display for Stmt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Stmt { extents: _, kind, semi } = self;
let Stmt { span: _, kind, semi } = self;
write!(f, "{kind}{semi}")
}
}
@ -939,7 +939,7 @@ mod path {
/// Checks whether this path refers to the sinkhole identifier, `_`
pub fn is_sinkhole(&self) -> bool {
if let [PathPart::Ident(id)] = self.parts.as_slice() {
if let "_" = id.to_ref(){
if let "_" = id.to_ref() {
return true;
}
}

View File

@ -13,8 +13,8 @@ use cl_structures::span::Span;
///
/// For all other nodes, traversal is *explicit*.
pub trait Fold {
fn fold_span(&mut self, extents: Span) -> Span {
extents
fn fold_span(&mut self, span: Span) -> Span {
span
}
fn fold_mutability(&mut self, mutability: Mutability) -> Mutability {
mutability
@ -59,9 +59,9 @@ pub trait Fold {
or_fold_meta_kind(self, kind)
}
fn fold_item(&mut self, i: Item) -> Item {
let Item { extents, attrs, vis, kind } = i;
let Item { span, attrs, vis, kind } = i;
Item {
extents: self.fold_span(extents),
span: self.fold_span(span),
attrs: self.fold_attrs(attrs),
vis: self.fold_visibility(vis),
kind: self.fold_item_kind(kind),
@ -71,8 +71,8 @@ pub trait Fold {
or_fold_item_kind(self, kind)
}
fn fold_alias(&mut self, a: Alias) -> Alias {
let Alias { to, from } = a;
Alias { to: self.fold_sym(to), from: from.map(|from| Box::new(self.fold_ty(*from))) }
let Alias { name, from } = a;
Alias { name: self.fold_sym(name), from: from.map(|from| Box::new(self.fold_ty(*from))) }
}
fn fold_const(&mut self, c: Const) -> Const {
let Const { name, ty, init } = c;
@ -92,8 +92,8 @@ pub trait Fold {
}
}
fn fold_module(&mut self, m: Module) -> Module {
let Module { name, kind } = m;
Module { name: self.fold_sym(name), kind: kind.map(|v| self.fold_file(v)) }
let Module { name, file } = m;
Module { name: self.fold_sym(name), file: file.map(|v| self.fold_file(v)) }
}
fn fold_function(&mut self, f: Function) -> Function {
let Function { name, sign, bind, body } = f;
@ -159,8 +159,8 @@ pub trait Fold {
or_fold_use_tree(self, tree)
}
fn fold_ty(&mut self, t: Ty) -> Ty {
let Ty { extents, kind } = t;
Ty { extents: self.fold_span(extents), kind: self.fold_ty_kind(kind) }
let Ty { span, kind } = t;
Ty { span: self.fold_span(span), kind: self.fold_ty_kind(kind) }
}
fn fold_ty_kind(&mut self, kind: TyKind) -> TyKind {
or_fold_ty_kind(self, kind)
@ -206,9 +206,9 @@ pub trait Fold {
}
}
fn fold_stmt(&mut self, s: Stmt) -> Stmt {
let Stmt { extents, kind, semi } = s;
let Stmt { span, kind, semi } = s;
Stmt {
extents: self.fold_span(extents),
span: self.fold_span(span),
kind: self.fold_stmt_kind(kind),
semi: self.fold_semi(semi),
}
@ -220,8 +220,8 @@ pub trait Fold {
s
}
fn fold_expr(&mut self, e: Expr) -> Expr {
let Expr { extents, kind } = e;
Expr { extents: self.fold_span(extents), kind: self.fold_expr_kind(kind) }
let Expr { span, kind } = e;
Expr { span: self.fold_span(span), kind: self.fold_expr_kind(kind) }
}
fn fold_expr_kind(&mut self, kind: ExprKind) -> ExprKind {
or_fold_expr_kind(self, kind)

View File

@ -13,7 +13,7 @@ use cl_structures::span::Span;
///
/// For all other nodes, traversal is *explicit*.
pub trait Visit<'a>: Sized {
fn visit_span(&mut self, _extents: &'a Span) {}
fn visit_span(&mut self, _span: &'a Span) {}
fn visit_mutability(&mut self, _mutable: &'a Mutability) {}
fn visit_visibility(&mut self, _vis: &'a Visibility) {}
fn visit_sym(&mut self, _name: &'a Sym) {}
@ -42,8 +42,8 @@ pub trait Visit<'a>: Sized {
or_visit_meta_kind(self, kind)
}
fn visit_item(&mut self, i: &'a Item) {
let Item { extents, attrs, vis, kind } = i;
self.visit_span(extents);
let Item { span, attrs, vis, kind } = i;
self.visit_span(span);
self.visit_attrs(attrs);
self.visit_visibility(vis);
self.visit_item_kind(kind);
@ -52,8 +52,8 @@ pub trait Visit<'a>: Sized {
or_visit_item_kind(self, kind)
}
fn visit_alias(&mut self, a: &'a Alias) {
let Alias { to, from } = a;
self.visit_sym(to);
let Alias { name, from } = a;
self.visit_sym(name);
if let Some(t) = from {
self.visit_ty(t)
}
@ -72,9 +72,9 @@ pub trait Visit<'a>: Sized {
self.visit_expr(init);
}
fn visit_module(&mut self, m: &'a Module) {
let Module { name, kind } = m;
let Module { name, file } = m;
self.visit_sym(name);
if let Some(f) = kind {
if let Some(f) = file {
self.visit_file(f)
}
}
@ -132,8 +132,8 @@ pub trait Visit<'a>: Sized {
or_visit_use_tree(self, tree)
}
fn visit_ty(&mut self, t: &'a Ty) {
let Ty { extents, kind } = t;
self.visit_span(extents);
let Ty { span, kind } = t;
self.visit_span(span);
self.visit_ty_kind(kind);
}
fn visit_ty_kind(&mut self, kind: &'a TyKind) {
@ -176,8 +176,8 @@ pub trait Visit<'a>: Sized {
}
}
fn visit_stmt(&mut self, s: &'a Stmt) {
let Stmt { extents, kind, semi } = s;
self.visit_span(extents);
let Stmt { span, kind, semi } = s;
self.visit_span(span);
self.visit_stmt_kind(kind);
self.visit_semi(semi);
}
@ -186,8 +186,8 @@ pub trait Visit<'a>: Sized {
}
fn visit_semi(&mut self, _s: &'a Semi) {}
fn visit_expr(&mut self, e: &'a Expr) {
let Expr { extents, kind } = e;
self.visit_span(extents);
let Expr { span, kind } = e;
self.visit_span(span);
self.visit_expr_kind(kind)
}
fn visit_expr_kind(&mut self, e: &'a ExprKind) {

View File

@ -23,14 +23,14 @@ impl Default for NormalizePaths {
impl Fold for NormalizePaths {
fn fold_module(&mut self, m: Module) -> Module {
let Module { name, kind } = m;
let Module { name, file } = m;
self.path.push(PathPart::Ident(name));
let name = self.fold_sym(name);
let kind = kind.map(|f| self.fold_file(f));
let file = file.map(|f| self.fold_file(f));
self.path.pop();
Module { name, kind }
Module { name, file }
}
fn fold_path(&mut self, p: Path) -> Path {

View File

@ -10,26 +10,26 @@ pub struct WhileElseDesugar;
impl Fold for WhileElseDesugar {
fn fold_expr(&mut self, e: Expr) -> Expr {
let Expr { extents, kind } = e;
let kind = desugar_while(extents, kind);
Expr { extents: self.fold_span(extents), kind: self.fold_expr_kind(kind) }
let Expr { span, kind } = e;
let kind = desugar_while(span, kind);
Expr { span: self.fold_span(span), kind: self.fold_expr_kind(kind) }
}
}
/// Desugars while(-else) expressions into loop-if-else-break expressions
fn desugar_while(extents: Span, kind: ExprKind) -> ExprKind {
fn desugar_while(span: Span, kind: ExprKind) -> ExprKind {
match kind {
// work backwards: fail -> break -> if -> loop
ExprKind::While(While { cond, pass, fail: Else { body } }) => {
// Preserve the else-expression's extents, if present, or use the parent's extents
let fail_span = body.as_ref().map(|body| body.extents).unwrap_or(extents);
let break_expr = Expr { extents: fail_span, kind: ExprKind::Break(Break { body }) };
// Preserve the else-expression's span, if present, or use the parent's span
let fail_span = body.as_ref().map(|body| body.span).unwrap_or(span);
let break_expr = Expr { span: fail_span, kind: ExprKind::Break(Break { body }) };
let loop_body = If { cond, pass, fail: Else { body: Some(Box::new(break_expr)) } };
let loop_body = ExprKind::If(loop_body);
ExprKind::Unary(Unary {
kind: UnaryKind::Loop,
tail: Box::new(Expr { extents, kind: loop_body }),
tail: Box::new(Expr { span, kind: loop_body }),
})
}
_ => kind,

View File

@ -87,9 +87,9 @@ impl Interpret for Static {
impl Interpret for Module {
// TODO: Keep modules around somehow, rather than putting them on the stack
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Self { name, kind } = self;
let Self { name, file } = self;
env.push_frame(name.to_ref(), Default::default());
let out = match kind {
let out = match file {
Some(file) => file.interpret(env),
None => {
eprintln!("Module {name} specified, but not imported.");
@ -128,7 +128,7 @@ impl Interpret for Struct {
.into(),
rety: Some(
Ty {
extents: cl_structures::span::Span::dummy(),
span: cl_structures::span::Span::dummy(),
kind: TyKind::Path(Path::from(*name)),
}
.into(),
@ -172,7 +172,7 @@ impl Interpret for Enum {
}
impl Interpret for Impl {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Self { target: ImplKind::Type(Ty { extents, kind: TyKind::Path(name) }), body } = self
let Self { target: ImplKind::Type(Ty { span, kind: TyKind::Path(name) }), body } = self
else {
eprintln!("TODO: impl X for Ty");
return Ok(ConValue::Empty);
@ -184,7 +184,7 @@ impl Interpret for Impl {
.pop_frame()
.expect("Environment frames must be balanced");
match assignment::addrof_path(env, name.parts.as_slice())
.map_err(|err| err.with_span(*extents))?
.map_err(|err| err.with_span(*span))?
{
Some(ConValue::Module(m)) => m.extend(frame),
Some(other) => eprintln!("TODO: impl for {other}"),
@ -263,13 +263,13 @@ impl Interpret for UseTree {
impl Interpret for Stmt {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Self { extents, kind, semi } = self;
let Self { span, kind, semi } = self;
let out = match kind {
StmtKind::Empty => Ok(ConValue::Empty),
StmtKind::Item(stmt) => stmt.interpret(env),
StmtKind::Expr(stmt) => stmt.interpret(env),
}
.map_err(|err| err.with_span(*extents))?;
.map_err(|err| err.with_span(*span))?;
Ok(match semi {
Semi::Terminated => ConValue::Empty,
Semi::Unterminated => out,
@ -280,8 +280,8 @@ impl Interpret for Stmt {
impl Interpret for Expr {
#[inline]
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Self { extents, kind } = self;
kind.interpret(env).map_err(|err| err.with_span(*extents))
let Self { span, kind } = self;
kind.interpret(env).map_err(|err| err.with_span(*span))
}
}

View File

@ -63,13 +63,13 @@ impl ModuleInliner {
impl Fold for ModuleInliner {
/// Traverses down the module tree, entering ever nested directories
fn fold_module(&mut self, m: Module) -> Module {
let Module { name, kind } = m;
let Module { name, file } = m;
self.path.push(&*name); // cd ./name
let kind = self.fold_module_kind(kind);
let file = self.fold_module_kind(file);
self.path.pop(); // cd ..
Module { name, kind }
Module { name, file }
}
}

View File

@ -320,7 +320,7 @@ impl Parse<'_> for Item {
attrs: Attrs::parse(p)?,
vis: Visibility::parse(p)?,
kind: ItemKind::parse(p)?,
extents: Span(start, p.loc()),
span: Span(start, p.loc()),
})
}
}
@ -352,7 +352,7 @@ impl Parse<'_> for Alias {
p.consume_peeked();
let out = Ok(Alias {
to: Sym::parse(p)?,
name: Sym::parse(p)?,
from: if p.match_type(TokenKind::Eq, P).is_ok() {
Some(Ty::parse(p)?.into())
} else {
@ -416,7 +416,7 @@ impl Parse<'_> for Module {
Ok(Module {
name: Sym::parse(p)?,
kind: {
file: {
const P: Parsing = Parsing::ModuleKind;
let inline = delim(Parse::parse, CURLIES, P);
@ -636,7 +636,7 @@ impl Parse<'_> for ImplKind {
Err(Error {
reason: ExpectedParsing { want: Parsing::Path },
while_parsing: P,
loc: target.extents.head,
loc: target.span.head,
})?
}
}
@ -696,7 +696,7 @@ impl Parse<'_> for Ty {
/// See also: [TyKind::parse]
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
let start = p.loc();
Ok(Ty { kind: TyKind::parse(p)?, extents: Span(start, p.loc()) })
Ok(Ty { kind: TyKind::parse(p)?, span: Span(start, p.loc()) })
}
}
@ -868,7 +868,7 @@ impl Parse<'_> for Stmt {
Ok(_) => Semi::Terminated,
_ => Semi::Unterminated,
},
extents: Span(start, p.loc()),
span: Span(start, p.loc()),
})
}
}
@ -984,7 +984,7 @@ impl Parse<'_> for AddrOf {
mutable: Mutability::parse(p)?,
expr: Expr::parse(p)?.into(),
}),
extents: Span(start, p.loc()),
span: Span(start, p.loc()),
}
.into(),
})

View File

@ -41,7 +41,7 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
Unary { kind, tail: expr(p, after)?.into() }.into()
}
},
extents: Span(start, p.loc()),
span: Span(start, p.loc()),
};
fn from_postfix(op: TokenKind) -> Option<Precedence> {
@ -80,10 +80,7 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
kind: BinaryKind::Call,
parts: (
head,
Expr {
kind: Tuple { exprs }.into(),
extents: Span(start, p.loc()),
},
Expr { kind: Tuple { exprs }.into(), span: Span(start, p.loc()) },
)
.into(),
}
@ -105,7 +102,7 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
}
_ => Err(p.error(Unexpected(op), parsing))?,
},
extents: Span(start, p.loc()),
span: Span(start, p.loc()),
};
continue;
}
@ -120,7 +117,7 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
let tail = expr(p, after)?;
head = Expr {
kind: Binary { kind, parts: (head, tail).into() }.into(),
extents: Span(start, p.loc()),
span: Span(start, p.loc()),
};
continue;
}
@ -135,7 +132,7 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
let tail = expr(p, after)?;
head = Expr {
kind: Modify { kind, parts: (head, tail).into() }.into(),
extents: Span(start, p.loc()),
span: Span(start, p.loc()),
};
continue;
}
@ -152,7 +149,7 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
let tail = expr(p, after)?;
head = Expr {
kind: Assign { parts: (head, tail).into() }.into(),
extents: Span(start, p.loc()),
span: Span(start, p.loc()),
};
continue;
@ -166,8 +163,7 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
p.consume_peeked();
let ty = Ty::parse(p)?;
head =
Expr { kind: Cast { head: head.into(), ty }.into(), extents: Span(start, p.loc()) };
head = Expr { kind: Cast { head: head.into(), ty }.into(), span: Span(start, p.loc()) };
continue;
}

View File

@ -193,7 +193,7 @@ pub mod yamlify {
impl Yamlify for Item {
fn yaml(&self, y: &mut Yamler) {
let Self { extents: _, attrs, vis, kind } = self;
let Self { span: _, attrs, vis, kind } = self;
y.key("Item").yaml(attrs).yaml(vis).yaml(kind);
}
}
@ -214,8 +214,8 @@ pub mod yamlify {
}
impl Yamlify for Alias {
fn yaml(&self, y: &mut Yamler) {
let Self { to, from } = self;
y.key("Alias").pair("to", to).pair("from", from);
let Self { name, from } = self;
y.key("Alias").pair("to", name).pair("from", from);
}
}
impl Yamlify for Const {
@ -235,8 +235,8 @@ pub mod yamlify {
}
impl Yamlify for Module {
fn yaml(&self, y: &mut Yamler) {
let Self { name, kind } = self;
y.key("Module").pair("name", name).yaml(kind);
let Self { name, file } = self;
y.key("Module").pair("name", name).yaml(file);
}
}
impl Yamlify for Function {
@ -333,7 +333,7 @@ pub mod yamlify {
}
impl Yamlify for Stmt {
fn yaml(&self, y: &mut Yamler) {
let Self { extents: _, kind, semi } = self;
let Self { span: _, kind, semi } = self;
y.key("Stmt").yaml(kind).yaml(semi);
}
}
@ -355,7 +355,7 @@ pub mod yamlify {
}
impl Yamlify for Expr {
fn yaml(&self, y: &mut Yamler) {
let Self { extents: _, kind } = self;
let Self { span: _, kind } = self;
y.yaml(kind);
}
}
@ -628,7 +628,7 @@ pub mod yamlify {
}
impl Yamlify for Ty {
fn yaml(&self, y: &mut Yamler) {
let Self { extents: _, kind } = self;
let Self { span: _, kind } = self;
y.key("Ty").yaml(kind);
}
}

View File

@ -25,7 +25,7 @@ impl Source<'_> {
match self {
Source::Root => None,
Source::Module(v) => Some(v.name),
Source::Alias(v) => Some(v.to),
Source::Alias(v) => Some(v.name),
Source::Enum(v) => Some(v.name),
Source::Variant(v) => Some(v.name),
Source::Struct(v) => Some(v.name),

View File

@ -33,7 +33,7 @@ impl<'t, 'a> Populator<'t, 'a> {
impl<'a> Visit<'a> for Populator<'_, 'a> {
fn visit_item(&mut self, i: &'a cl_ast::Item) {
let cl_ast::Item { extents, attrs, vis, kind } = i;
let cl_ast::Item { span, attrs, vis, kind } = i;
// TODO: this, better, better.
let entry_kind = match kind {
ItemKind::Alias(_) => NodeKind::Type,
@ -50,10 +50,10 @@ impl<'a> Visit<'a> for Populator<'_, 'a> {
};
let mut entry = self.new_entry(entry_kind);
entry.inner.set_span(*extents);
entry.inner.set_span(*span);
entry.inner.set_meta(&attrs.meta);
entry.visit_span(extents);
entry.visit_span(span);
entry.visit_attrs(attrs);
entry.visit_visibility(vis);
entry.visit_item_kind(kind);
@ -64,9 +64,9 @@ impl<'a> Visit<'a> for Populator<'_, 'a> {
}
fn visit_alias(&mut self, a: &'a cl_ast::Alias) {
let cl_ast::Alias { to, from } = a;
let cl_ast::Alias { name, from } = a;
self.inner.set_source(Source::Alias(a));
self.set_name(*to);
self.set_name(*name);
if let Some(t) = from {
self.visit_ty(t)
@ -93,11 +93,11 @@ impl<'a> Visit<'a> for Populator<'_, 'a> {
}
fn visit_module(&mut self, m: &'a cl_ast::Module) {
let cl_ast::Module { name, kind } = m;
let cl_ast::Module { name, file } = m;
self.inner.set_source(Source::Module(m));
self.set_name(*name);
if let Some(file) = kind {
if let Some(file) = file {
self.visit_file(file);
}
}