From 0cc0cb5cfbff5ccec58e6296c94e3138f0fd76b1 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 19 May 2024 14:41:31 -0500 Subject: [PATCH] conlang: Remove "Identifier" node It never carried any extra information, and got in the way everywhere it was used. --- compiler/cl-ast/src/ast.rs | 47 +++++++++---------- compiler/cl-ast/src/ast_impl.rs | 15 +----- compiler/cl-ast/src/ast_visitor/fold.rs | 45 ++++++++---------- compiler/cl-ast/src/ast_visitor/visit.rs | 42 ++++++++--------- compiler/cl-ast/src/desugar/path_absoluter.rs | 4 +- compiler/cl-interpret/src/interpret.rs | 12 ++--- compiler/cl-interpret/src/lib.rs | 12 ++--- compiler/cl-parser/src/inliner.rs | 3 +- compiler/cl-parser/src/parser.rs | 4 +- compiler/cl-repl/examples/yaml.rs | 5 +- compiler/cl-typeck/src/name_collector.rs | 20 ++++---- compiler/cl-typeck/src/node.rs | 20 ++++---- compiler/cl-typeck/src/project.rs | 12 ++--- compiler/cl-typeck/src/type_resolver.rs | 8 ++-- compiler/cl-typeck/src/use_importer.rs | 5 +- 15 files changed, 115 insertions(+), 139 deletions(-) diff --git a/compiler/cl-ast/src/ast.rs b/compiler/cl-ast/src/ast.rs index b73e7a3..7d03601 100644 --- a/compiler/cl-ast/src/ast.rs +++ b/compiler/cl-ast/src/ast.rs @@ -29,11 +29,6 @@ pub enum Visibility { Public, } -// TODO: Capture token? -/// A name -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct Identifier(pub Sym); - /// A [Literal]: 0x42, 1e123, 2.4, "Hello" #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub enum Literal { @@ -58,7 +53,7 @@ pub struct Attrs { /// A metadata decorator #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Meta { - pub name: Identifier, + pub name: Sym, pub kind: MetaKind, } @@ -108,14 +103,14 @@ pub enum ItemKind { /// An alias to another [Ty] #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Alias { - pub to: Identifier, + pub to: Sym, pub from: Option>, } /// A compile-time constant #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Const { - pub name: Identifier, + pub name: Sym, pub ty: Box, pub init: Box, } @@ -124,7 +119,7 @@ pub struct Const { #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Static { pub mutable: Mutability, - pub name: Identifier, + pub name: Sym, pub ty: Box, pub init: Box, } @@ -132,7 +127,7 @@ pub struct Static { /// An ordered collection of [Items](Item) #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Module { - pub name: Identifier, + pub name: Sym, pub kind: ModuleKind, } @@ -146,7 +141,7 @@ pub enum ModuleKind { /// Code, and the interface to that code #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Function { - pub name: Identifier, + pub name: Sym, pub sign: TyFn, pub bind: Vec, pub body: Option, @@ -156,13 +151,13 @@ pub struct Function { #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Param { pub mutability: Mutability, - pub name: Identifier, + pub name: Sym, } /// A user-defined product type #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Struct { - pub name: Identifier, + pub name: Sym, pub kind: StructKind, } @@ -174,18 +169,18 @@ pub enum StructKind { Struct(Vec), } -/// The [Visibility], [Identifier], and [Ty]pe of a single [Struct] member +/// The [Visibility], [Sym], and [Ty]pe of a single [Struct] member #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct StructMember { pub vis: Visibility, - pub name: Identifier, + pub name: Sym, pub ty: Ty, } /// A user-defined sum type #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Enum { - pub name: Identifier, + pub name: Sym, pub kind: EnumKind, } @@ -200,7 +195,7 @@ pub enum EnumKind { /// A single [Enum] variant #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Variant { - pub name: Identifier, + pub name: Sym, pub kind: VariantKind, } @@ -239,8 +234,8 @@ pub struct Use { pub enum UseTree { Tree(Vec), Path(PathPart, Box), - Alias(Identifier, Identifier), - Name(Identifier), + Alias(Sym, Sym), + Name(Sym), Glob, } @@ -297,7 +292,7 @@ pub struct Path { pub enum PathPart { SuperKw, SelfKw, - Ident(Identifier), + Ident(Sym), } /// An abstract statement, and associated metadata @@ -328,7 +323,7 @@ pub enum Semi { #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Let { pub mutable: Mutability, - pub name: Identifier, + pub name: Sym, pub ty: Option>, pub init: Option>, } @@ -483,8 +478,8 @@ pub struct Member { /// The kind of [Member] access #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub enum MemberKind { - Call(Identifier, Tuple), - Struct(Identifier), + Call(Sym, Tuple), + Struct(Sym), Tuple(Literal), } @@ -502,10 +497,10 @@ pub struct Structor { pub init: Vec, } -/// A [Struct field initializer] expression: [Identifier] (`=` [Expr])? +/// A [Struct field initializer] expression: [Sym] (`=` [Expr])? #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Fielder { - pub name: Identifier, + pub name: Sym, pub init: Option>, } @@ -574,7 +569,7 @@ pub struct If { /// A [For] expression: `for` Pattern `in` [`Expr`] [`Block`] [`Else`]? #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct For { - pub bind: Identifier, // TODO: Patterns? + pub bind: Sym, // TODO: Patterns? pub cond: Box, pub pass: Box, pub fail: Else, diff --git a/compiler/cl-ast/src/ast_impl.rs b/compiler/cl-ast/src/ast_impl.rs index 05cd25e..cab7b6f 100644 --- a/compiler/cl-ast/src/ast_impl.rs +++ b/compiler/cl-ast/src/ast_impl.rs @@ -44,12 +44,6 @@ mod display { } } - impl Display for Identifier { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) - } - } - impl Display for Literal { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -691,11 +685,6 @@ mod convert { //! Converts between major enums and enum variants use super::*; - impl> From for Identifier { - fn from(value: T) -> Self { - Identifier(value.as_ref().into()) - } - } impl> From for PathPart { fn from(value: T) -> Self { match value.as_ref() { @@ -799,7 +788,7 @@ mod convert { mod path { //! Utils for [Path] - use crate::{ast::Path, Identifier, PathPart}; + use crate::{ast::Path, PathPart, Sym}; impl Path { /// Appends a [PathPart] to this [Path] @@ -822,7 +811,7 @@ mod path { } } impl PathPart { - pub fn from_ident(ident: Identifier) -> Self { + pub fn from_sym(ident: Sym) -> Self { Self::Ident(ident) } } diff --git a/compiler/cl-ast/src/ast_visitor/fold.rs b/compiler/cl-ast/src/ast_visitor/fold.rs index 3a68371..3ffe982 100644 --- a/compiler/cl-ast/src/ast_visitor/fold.rs +++ b/compiler/cl-ast/src/ast_visitor/fold.rs @@ -22,7 +22,7 @@ pub trait Fold { fn fold_visibility(&mut self, visibility: Visibility) -> Visibility { visibility } - fn fold_identifier(&mut self, ident: Identifier) -> Identifier { + fn fold_sym(&mut self, ident: Sym) -> Sym { ident } fn fold_literal(&mut self, lit: Literal) -> Literal { @@ -50,7 +50,7 @@ pub trait Fold { } fn fold_meta(&mut self, m: Meta) -> Meta { let Meta { name, kind } = m; - Meta { name: self.fold_identifier(name), kind: self.fold_meta_kind(kind) } + Meta { name: self.fold_sym(name), kind: self.fold_meta_kind(kind) } } fn fold_meta_kind(&mut self, kind: MetaKind) -> MetaKind { or_fold_meta_kind(self, kind) @@ -69,12 +69,12 @@ pub trait Fold { } fn fold_alias(&mut self, a: Alias) -> Alias { let Alias { to, from } = a; - Alias { to: self.fold_identifier(to), from: from.map(|from| Box::new(self.fold_ty(*from))) } + Alias { to: self.fold_sym(to), from: from.map(|from| Box::new(self.fold_ty(*from))) } } fn fold_const(&mut self, c: Const) -> Const { let Const { name, ty, init } = c; Const { - name: self.fold_identifier(name), + name: self.fold_sym(name), ty: Box::new(self.fold_ty(*ty)), init: Box::new(self.fold_expr(*init)), } @@ -83,14 +83,14 @@ pub trait Fold { let Static { mutable, name, ty, init } = s; Static { mutable: self.fold_mutability(mutable), - name: self.fold_identifier(name), + name: self.fold_sym(name), ty: Box::new(self.fold_ty(*ty)), init: Box::new(self.fold_expr(*init)), } } fn fold_module(&mut self, m: Module) -> Module { let Module { name, kind } = m; - Module { name: self.fold_identifier(name), kind: self.fold_module_kind(kind) } + Module { name: self.fold_sym(name), kind: self.fold_module_kind(kind) } } fn fold_module_kind(&mut self, m: ModuleKind) -> ModuleKind { match m { @@ -101,7 +101,7 @@ pub trait Fold { fn fold_function(&mut self, f: Function) -> Function { let Function { name, sign, bind, body } = f; Function { - name: self.fold_identifier(name), + name: self.fold_sym(name), sign: self.fold_ty_fn(sign), bind: bind.into_iter().map(|p| self.fold_param(p)).collect(), body: body.map(|b| self.fold_block(b)), @@ -109,11 +109,11 @@ pub trait Fold { } fn fold_param(&mut self, p: Param) -> Param { let Param { mutability, name } = p; - Param { mutability: self.fold_mutability(mutability), name: self.fold_identifier(name) } + Param { mutability: self.fold_mutability(mutability), name: self.fold_sym(name) } } fn fold_struct(&mut self, s: Struct) -> Struct { let Struct { name, kind } = s; - Struct { name: self.fold_identifier(name), kind: self.fold_struct_kind(kind) } + Struct { name: self.fold_sym(name), kind: self.fold_struct_kind(kind) } } fn fold_struct_kind(&mut self, kind: StructKind) -> StructKind { match kind { @@ -132,13 +132,13 @@ pub trait Fold { let StructMember { vis, name, ty } = m; StructMember { vis: self.fold_visibility(vis), - name: self.fold_identifier(name), + name: self.fold_sym(name), ty: self.fold_ty(ty), } } fn fold_enum(&mut self, e: Enum) -> Enum { let Enum { name, kind } = e; - Enum { name: self.fold_identifier(name), kind: self.fold_enum_kind(kind) } + Enum { name: self.fold_sym(name), kind: self.fold_enum_kind(kind) } } fn fold_enum_kind(&mut self, kind: EnumKind) -> EnumKind { or_fold_enum_kind(self, kind) @@ -146,7 +146,7 @@ pub trait Fold { fn fold_variant(&mut self, v: Variant) -> Variant { let Variant { name, kind } = v; - Variant { name: self.fold_identifier(name), kind: self.fold_variant_kind(kind) } + Variant { name: self.fold_sym(name), kind: self.fold_variant_kind(kind) } } fn fold_variant_kind(&mut self, kind: VariantKind) -> VariantKind { or_fold_variant_kind(self, kind) @@ -200,7 +200,7 @@ pub trait Fold { match p { PathPart::SuperKw => PathPart::SuperKw, PathPart::SelfKw => PathPart::SelfKw, - PathPart::Ident(i) => PathPart::Ident(self.fold_identifier(i)), + PathPart::Ident(i) => PathPart::Ident(self.fold_sym(i)), } } fn fold_stmt(&mut self, s: Stmt) -> Stmt { @@ -221,7 +221,7 @@ pub trait Fold { let Let { mutable, name, ty, init } = l; Let { mutable: self.fold_mutability(mutable), - name: self.fold_identifier(name), + name: self.fold_sym(name), ty: ty.map(|t| Box::new(self.fold_ty(*t))), init: init.map(|e| Box::new(self.fold_expr(*e))), } @@ -292,10 +292,7 @@ pub trait Fold { fn fold_fielder(&mut self, f: Fielder) -> Fielder { let Fielder { name, init } = f; - Fielder { - name: self.fold_identifier(name), - init: init.map(|e| Box::new(self.fold_expr(*e))), - } + Fielder { name: self.fold_sym(name), init: init.map(|e| Box::new(self.fold_expr(*e))) } } fn fold_array(&mut self, a: Array) -> Array { let Array { values } = a; @@ -351,7 +348,7 @@ pub trait Fold { fn fold_for(&mut self, f: For) -> For { let For { bind, cond, pass, fail } = f; For { - bind: self.fold_identifier(bind), + bind: self.fold_sym(bind), cond: Box::new(self.fold_expr(*cond)), pass: Box::new(self.fold_block(*pass)), fail: self.fold_else(fail), @@ -489,10 +486,8 @@ pub fn or_fold_use_tree(folder: &mut F, tree: UseTree) -> UseT folder.fold_path_part(path), Box::new(folder.fold_use_tree(*rest)), ), - UseTree::Alias(path, name) => { - UseTree::Alias(folder.fold_identifier(path), folder.fold_identifier(name)) - } - UseTree::Name(name) => UseTree::Name(folder.fold_identifier(name)), + UseTree::Alias(path, name) => UseTree::Alias(folder.fold_sym(path), folder.fold_sym(name)), + UseTree::Name(name) => UseTree::Name(folder.fold_sym(name)), UseTree::Glob => UseTree::Glob, } } @@ -553,9 +548,9 @@ pub fn or_fold_expr_kind(folder: &mut F, kind: ExprKind) -> Ex pub fn or_fold_member_kind(folder: &mut F, kind: MemberKind) -> MemberKind { match kind { MemberKind::Call(name, args) => { - MemberKind::Call(folder.fold_identifier(name), folder.fold_tuple(args)) + MemberKind::Call(folder.fold_sym(name), folder.fold_tuple(args)) } - MemberKind::Struct(name) => MemberKind::Struct(folder.fold_identifier(name)), + MemberKind::Struct(name) => MemberKind::Struct(folder.fold_sym(name)), MemberKind::Tuple(name) => MemberKind::Tuple(folder.fold_literal(name)), } } diff --git a/compiler/cl-ast/src/ast_visitor/visit.rs b/compiler/cl-ast/src/ast_visitor/visit.rs index f0487c4..eaeb188 100644 --- a/compiler/cl-ast/src/ast_visitor/visit.rs +++ b/compiler/cl-ast/src/ast_visitor/visit.rs @@ -16,7 +16,7 @@ pub trait Visit<'a>: Sized { fn visit_span(&mut self, _extents: &'a Span) {} fn visit_mutability(&mut self, _mutable: &'a Mutability) {} fn visit_visibility(&mut self, _vis: &'a Visibility) {} - fn visit_identifier(&mut self, _name: &'a Identifier) {} + fn visit_sym(&mut self, _name: &'a Sym) {} fn visit_literal(&mut self, l: &'a Literal) { or_visit_literal(self, l) } @@ -34,7 +34,7 @@ pub trait Visit<'a>: Sized { } fn visit_meta(&mut self, m: &'a Meta) { let Meta { name, kind } = m; - self.visit_identifier(name); + self.visit_sym(name); self.visit_meta_kind(kind); } fn visit_meta_kind(&mut self, kind: &'a MetaKind) { @@ -52,27 +52,27 @@ pub trait Visit<'a>: Sized { } fn visit_alias(&mut self, a: &'a Alias) { let Alias { to, from } = a; - self.visit_identifier(to); + self.visit_sym(to); if let Some(t) = from { self.visit_ty(t) } } fn visit_const(&mut self, c: &'a Const) { let Const { name, ty, init } = c; - self.visit_identifier(name); + self.visit_sym(name); self.visit_ty(ty); self.visit_expr(init); } fn visit_static(&mut self, s: &'a Static) { let Static { mutable, name, ty, init } = s; self.visit_mutability(mutable); - self.visit_identifier(name); + self.visit_sym(name); self.visit_ty(ty); self.visit_expr(init); } fn visit_module(&mut self, m: &'a Module) { let Module { name, kind } = m; - self.visit_identifier(name); + self.visit_sym(name); self.visit_module_kind(kind); } fn visit_module_kind(&mut self, kind: &'a ModuleKind) { @@ -80,7 +80,7 @@ pub trait Visit<'a>: Sized { } fn visit_function(&mut self, f: &'a Function) { let Function { name, sign, bind, body } = f; - self.visit_identifier(name); + self.visit_sym(name); self.visit_ty_fn(sign); bind.iter().for_each(|p| self.visit_param(p)); if let Some(b) = body { @@ -90,11 +90,11 @@ pub trait Visit<'a>: Sized { fn visit_param(&mut self, p: &'a Param) { let Param { mutability, name } = p; self.visit_mutability(mutability); - self.visit_identifier(name); + self.visit_sym(name); } fn visit_struct(&mut self, s: &'a Struct) { let Struct { name, kind } = s; - self.visit_identifier(name); + self.visit_sym(name); self.visit_struct_kind(kind); } fn visit_struct_kind(&mut self, kind: &'a StructKind) { @@ -103,12 +103,12 @@ pub trait Visit<'a>: Sized { fn visit_struct_member(&mut self, m: &'a StructMember) { let StructMember { vis, name, ty } = m; self.visit_visibility(vis); - self.visit_identifier(name); + self.visit_sym(name); self.visit_ty(ty); } fn visit_enum(&mut self, e: &'a Enum) { let Enum { name, kind } = e; - self.visit_identifier(name); + self.visit_sym(name); self.visit_enum_kind(kind); } fn visit_enum_kind(&mut self, kind: &'a EnumKind) { @@ -116,7 +116,7 @@ pub trait Visit<'a>: Sized { } fn visit_variant(&mut self, v: &'a Variant) { let Variant { name, kind } = v; - self.visit_identifier(name); + self.visit_sym(name); self.visit_variant_kind(kind); } fn visit_variant_kind(&mut self, kind: &'a VariantKind) { @@ -169,7 +169,7 @@ pub trait Visit<'a>: Sized { match p { PathPart::SuperKw => {} PathPart::SelfKw => {} - PathPart::Ident(i) => self.visit_identifier(i), + PathPart::Ident(i) => self.visit_sym(i), } } fn visit_stmt(&mut self, s: &'a Stmt) { @@ -185,7 +185,7 @@ pub trait Visit<'a>: Sized { fn visit_let(&mut self, l: &'a Let) { let Let { mutable, name, ty, init } = l; self.visit_mutability(mutable); - self.visit_identifier(name); + self.visit_sym(name); if let Some(ty) = ty { self.visit_ty(ty); } @@ -249,7 +249,7 @@ pub trait Visit<'a>: Sized { } fn visit_fielder(&mut self, f: &'a Fielder) { let Fielder { name, init } = f; - self.visit_identifier(name); + self.visit_sym(name); if let Some(init) = init { self.visit_expr(init); } @@ -298,7 +298,7 @@ pub trait Visit<'a>: Sized { } fn visit_for(&mut self, f: &'a For) { let For { bind, cond, pass, fail } = f; - self.visit_identifier(bind); + self.visit_sym(bind); self.visit_expr(cond); self.visit_block(pass); self.visit_else(fail); @@ -408,11 +408,11 @@ pub fn or_visit_use_tree<'a, V: Visit<'a>>(visitor: &mut V, tree: &'a UseTree) { visitor.visit_use_tree(rest) } UseTree::Alias(path, name) => { - visitor.visit_identifier(path); - visitor.visit_identifier(name); + visitor.visit_sym(path); + visitor.visit_sym(name); } UseTree::Name(name) => { - visitor.visit_identifier(name); + visitor.visit_sym(name); } UseTree::Glob => {} } @@ -469,10 +469,10 @@ pub fn or_visit_expr_kind<'a, V: Visit<'a>>(visitor: &mut V, e: &'a ExprKind) { pub fn or_visit_member_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a MemberKind) { match kind { MemberKind::Call(field, args) => { - visitor.visit_identifier(field); + visitor.visit_sym(field); visitor.visit_tuple(args); } - MemberKind::Struct(field) => visitor.visit_identifier(field), + MemberKind::Struct(field) => visitor.visit_sym(field), MemberKind::Tuple(field) => visitor.visit_literal(field), } } diff --git a/compiler/cl-ast/src/desugar/path_absoluter.rs b/compiler/cl-ast/src/desugar/path_absoluter.rs index 6176eb3..9dbe10a 100644 --- a/compiler/cl-ast/src/desugar/path_absoluter.rs +++ b/compiler/cl-ast/src/desugar/path_absoluter.rs @@ -24,9 +24,9 @@ impl Default for NormalizePaths { impl Fold for NormalizePaths { fn fold_module(&mut self, m: Module) -> Module { let Module { name, kind } = m; - self.path.push(PathPart::Ident(name.clone())); + self.path.push(PathPart::Ident(name)); - let (name, kind) = (self.fold_identifier(name), self.fold_module_kind(kind)); + let (name, kind) = (self.fold_sym(name), self.fold_module_kind(kind)); self.path.pop(); Module { name, kind } diff --git a/compiler/cl-interpret/src/interpret.rs b/compiler/cl-interpret/src/interpret.rs index 51cae1c..543a553 100644 --- a/compiler/cl-interpret/src/interpret.rs +++ b/compiler/cl-interpret/src/interpret.rs @@ -110,7 +110,7 @@ impl Interpret for Stmt { } impl Interpret for Let { fn interpret(&self, env: &mut Environment) -> IResult { - let Let { mutable: _, name: Identifier(name), ty: _, init } = self; + let Let { mutable: _, name, ty: _, init } = self; let init = init.as_ref().map(|i| i.interpret(env)).transpose()?; env.insert(*name, init); Ok(ConValue::Empty) @@ -162,7 +162,7 @@ fn evaluate_place_expr<'e>( match parts.last().expect("parts should not be empty") { PathPart::SuperKw => Err(Error::NotAssignable), PathPart::SelfKw => todo!("Assignment to `self`"), - PathPart::Ident(Identifier(s)) => env.get_mut(*s).map(|v| (v, *s)), + PathPart::Ident(s) => env.get_mut(*s).map(|v| (v, *s)), } } ExprKind::Index(_) => todo!("Assignment to an index operation"), @@ -340,7 +340,7 @@ impl Interpret for Member { for arg in &args.exprs { values.push(arg.interpret(env)?); } - env.call(name.0, &values) + env.call(*name, &values) } _ => Err(Error::TypeError)?, } @@ -368,7 +368,7 @@ impl Interpret for Path { if parts.len() == 1 { match parts.last().expect("parts should not be empty") { PathPart::SuperKw | PathPart::SelfKw => todo!("Path navigation"), - PathPart::Ident(Identifier(name)) => env.get(*name), + PathPart::Ident(name) => env.get(*name), } } else { todo!("Path navigation!") @@ -413,7 +413,7 @@ impl Interpret for AddrOf { match expr.as_ref() { ExprKind::Index(_) => todo!("AddrOf array index"), // ExprKind::Path(Path { absolute: false, parts }) => match parts.as_slice() { - // [PathPart::Ident(Identifier(id))] => env.get_ref(id), + // [PathPart::Ident(id)] => env.get_ref(id), // _ => todo!("Path traversal in addrof"), // }, ExprKind::Path(_) => todo!("Path traversal in addrof"), @@ -492,7 +492,7 @@ impl Interpret for If { } impl Interpret for For { fn interpret(&self, env: &mut Environment) -> IResult { - let Self { bind: Identifier(name), cond, pass, fail } = self; + let Self { bind: name, cond, pass, fail } = self; // TODO: A better iterator model let mut bounds = match cond.interpret(env)? { ConValue::RangeExc(a, b) => a..=b, diff --git a/compiler/cl-interpret/src/lib.rs b/compiler/cl-interpret/src/lib.rs index 54cfacd..284d8b1 100644 --- a/compiler/cl-interpret/src/lib.rs +++ b/compiler/cl-interpret/src/lib.rs @@ -311,7 +311,7 @@ pub mod function { //! Represents a block of code which lives inside the Interpreter use super::{Callable, ConValue, Environment, Error, IResult, Interpret}; - use cl_ast::{Function as FnDecl, Identifier, Param, Sym}; + use cl_ast::{Function as FnDecl, Param, Sym}; use std::rc::Rc; /// Represents a block of code which persists inside the Interpreter #[derive(Clone, Debug)] @@ -333,11 +333,11 @@ pub mod function { impl Callable for Function { fn name(&self) -> Sym { - let FnDecl { name: Identifier(name), .. } = *self.decl; + let FnDecl { name, .. } = *self.decl; name } fn call(&self, env: &mut Environment, args: &[ConValue]) -> IResult { - let FnDecl { name: Identifier(name), bind, body, sign: _ } = &*self.decl; + let FnDecl { name, bind, body, sign: _ } = &*self.decl; // Check arg mapping if args.len() != bind.len() { return Err(Error::ArgNumber { want: bind.len(), got: args.len() }); @@ -347,7 +347,7 @@ pub mod function { }; // TODO: completely refactor data storage let mut frame = env.frame("fn args"); - for (Param { mutability: _, name: Identifier(name) }, value) in bind.iter().zip(args) { + for (Param { mutability: _, name }, value) in bind.iter().zip(args) { frame.insert(*name, Some(value.clone())); } match body.interpret(&mut frame) { @@ -370,7 +370,7 @@ pub mod env { temp_type_impl::ConValue, BuiltIn, Callable, Interpret, }; - use cl_ast::{Function as FnDecl, Identifier, Sym}; + use cl_ast::{Function as FnDecl, Sym}; use std::{ collections::HashMap, fmt::Display, @@ -475,7 +475,7 @@ pub mod env { } /// A convenience function for registering a [FnDecl] as a [Function] pub fn insert_fn(&mut self, decl: &FnDecl) { - let FnDecl { name: Identifier(name), .. } = decl; + let FnDecl { name, .. } = decl; let (name, function) = (name, Some(Function::new(decl).into())); if let Some((frame, _)) = self.frames.last_mut() { frame.insert(*name, function); diff --git a/compiler/cl-parser/src/inliner.rs b/compiler/cl-parser/src/inliner.rs index e4913b0..c0f6a6e 100644 --- a/compiler/cl-parser/src/inliner.rs +++ b/compiler/cl-parser/src/inliner.rs @@ -64,8 +64,7 @@ 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 sym = name.0; - self.path.push(&*sym); // cd ./name + self.path.push(&*name); // cd ./name let kind = self.fold_module_kind(kind); diff --git a/compiler/cl-parser/src/parser.rs b/compiler/cl-parser/src/parser.rs index bc42ae2..c19a7cf 100644 --- a/compiler/cl-parser/src/parser.rs +++ b/compiler/cl-parser/src/parser.rs @@ -769,8 +769,8 @@ impl<'t> Parser<'t> { self.consume_peeked(); Ok(out) } - /// [Identifier] = [`Identifier`](TokenKind::Identifier) - pub fn identifier(&mut self) -> PResult { + /// [Sym] = [`Identifier`](TokenKind::Identifier) + pub fn identifier(&mut self) -> PResult { let tok = self.match_type(TokenKind::Identifier, Parsing::Identifier)?; match tok.data() { TokenData::String(ident) => Ok(ident.into()), diff --git a/compiler/cl-repl/examples/yaml.rs b/compiler/cl-repl/examples/yaml.rs index 64c80a9..eb92cdb 100644 --- a/compiler/cl-repl/examples/yaml.rs +++ b/compiler/cl-repl/examples/yaml.rs @@ -588,10 +588,9 @@ pub mod yamlify { y.value(format_args!("\"{self}\"")); } } - impl Yamlify for Identifier { + impl Yamlify for Sym { fn yaml(&self, y: &mut Yamler) { - let Self(name) = self; - y.value(name); + y.value(self); } } impl Yamlify for Param { diff --git a/compiler/cl-typeck/src/name_collector.rs b/compiler/cl-typeck/src/name_collector.rs index d10ceef..f1c358d 100644 --- a/compiler/cl-typeck/src/name_collector.rs +++ b/compiler/cl-typeck/src/name_collector.rs @@ -51,7 +51,7 @@ impl<'prj, 'a> Visit<'a> for NameCollector<'prj, 'a> { } fn visit_module(&mut self, m: &'a Module) { let Self { prj, parent, path, retval: _ } = self; - let Module { name: Identifier(name), kind } = m; + let Module { name, kind } = m; let def = Def { module: Mod::new(*parent), @@ -60,14 +60,14 @@ impl<'prj, 'a> Visit<'a> for NameCollector<'prj, 'a> { }; let id = prj.pool.insert(def); prj[*parent].module.insert_type(*name, id); - self.path.push(PathPart::Ident(Identifier(*name))); + self.path.push(PathPart::Ident(*name)); self.with_parent(id, kind, Self::visit_module_kind); self.path.pop(); self.retval = Some(id); } fn visit_alias(&mut self, a: &'a Alias) { let Self { prj, parent, path, retval: _ } = self; - let Alias { to: Identifier(name), from: _ } = a; + let Alias { to: name, from: _ } = a; let def = Def { module: Mod::new(*parent), @@ -81,7 +81,7 @@ impl<'prj, 'a> Visit<'a> for NameCollector<'prj, 'a> { } fn visit_enum(&mut self, e: &'a Enum) { let Self { prj, parent, path, retval: _ } = self; - let Enum { name: Identifier(name), kind } = e; + let Enum { name, kind } = e; let def = Def { module: Mod::new(*parent), @@ -96,7 +96,7 @@ impl<'prj, 'a> Visit<'a> for NameCollector<'prj, 'a> { } fn visit_variant(&mut self, v: &'a Variant) { let Self { path, prj, parent, retval: _ } = self; - let Variant { name: Identifier(name), kind } = v; + let Variant { name, kind } = v; let def = Def { module: Mod::new(*parent), @@ -111,7 +111,7 @@ impl<'prj, 'a> Visit<'a> for NameCollector<'prj, 'a> { } fn visit_struct(&mut self, s: &'a Struct) { let Self { prj, parent, path, retval: _ } = self; - let Struct { name: Identifier(name), kind } = s; + let Struct { name, kind } = s; let def = Def { module: Mod::new(*parent), @@ -126,7 +126,7 @@ impl<'prj, 'a> Visit<'a> for NameCollector<'prj, 'a> { } fn visit_const(&mut self, c: &'a Const) { let Self { prj, parent, path, retval: _ } = self; - let Const { name: Identifier(name), ty: _, init } = c; + let Const { name, ty: _, init } = c; let def = Def { module: Mod::new(*parent), @@ -141,7 +141,7 @@ impl<'prj, 'a> Visit<'a> for NameCollector<'prj, 'a> { } fn visit_static(&mut self, s: &'a Static) { let Self { prj, parent, path, retval: _ } = self; - let Static { name: Identifier(name), mutable: _, ty: _, init } = s; + let Static { name, mutable: _, ty: _, init } = s; let def = Def { module: Mod::new(*parent), @@ -156,7 +156,7 @@ impl<'prj, 'a> Visit<'a> for NameCollector<'prj, 'a> { } fn visit_function(&mut self, f: &'a Function) { let Self { prj, parent, path, retval: _ } = self; - let Function { name: Identifier(name), body, .. } = f; + let Function { name, body, .. } = f; let def = Def { module: Mod::new(*parent), @@ -201,7 +201,7 @@ impl<'prj, 'a> Visit<'a> for NameCollector<'prj, 'a> { } fn visit_let(&mut self, l: &'a Let) { let Self { prj, parent, path, retval: _ } = self; - let Let { name: Identifier(name), init, .. } = l; + let Let { name, init, .. } = l; let def = Def { module: Mod::new(*parent), kind: DefKind::Undecided, diff --git a/compiler/cl-typeck/src/node.rs b/compiler/cl-typeck/src/node.rs index 7f907b1..e390ea9 100644 --- a/compiler/cl-typeck/src/node.rs +++ b/compiler/cl-typeck/src/node.rs @@ -47,15 +47,15 @@ impl<'a> NodeSource<'a> { pub fn name(&self) -> Option { match self { NodeSource::Root => None, - NodeSource::Module(v) => Some(v.name.0), - NodeSource::Alias(v) => Some(v.to.0), - NodeSource::Enum(v) => Some(v.name.0), - NodeSource::Variant(v) => Some(v.name.0), - NodeSource::Struct(v) => Some(v.name.0), - NodeSource::Const(v) => Some(v.name.0), - NodeSource::Static(v) => Some(v.name.0), - NodeSource::Function(v) => Some(v.name.0), - NodeSource::Local(l) => Some(l.name.0), + NodeSource::Module(v) => Some(v.name), + NodeSource::Alias(v) => Some(v.to), + NodeSource::Enum(v) => Some(v.name), + NodeSource::Variant(v) => Some(v.name), + NodeSource::Struct(v) => Some(v.name), + NodeSource::Const(v) => Some(v.name), + NodeSource::Static(v) => Some(v.name), + NodeSource::Function(v) => Some(v.name), + NodeSource::Local(l) => Some(l.name), NodeSource::Impl(_) | NodeSource::Use(_) | NodeSource::Ty(_) => None, } } @@ -166,7 +166,7 @@ pub mod sorcerer { impl<'a> Visit<'a> for NodeSorcerer<'a> { fn visit_module(&mut self, m: &'a Module) { let Module { name, kind } = m; - self.path.push(PathPart::Ident(name.clone())); + self.path.push(PathPart::Ident(*name)); self.visit_module_kind(kind); self.path.pop(); } diff --git a/compiler/cl-typeck/src/project.rs b/compiler/cl-typeck/src/project.rs index 432b9ad..3f4b7ef 100644 --- a/compiler/cl-typeck/src/project.rs +++ b/compiler/cl-typeck/src/project.rs @@ -6,7 +6,7 @@ use crate::{ node::{Node, NodeSource}, path::Path, }; -use cl_ast::{Identifier, PathPart}; +use cl_ast::PathPart; use cl_structures::deprecated_intern_pool::Pool; use std::{ collections::HashMap, @@ -85,11 +85,11 @@ impl<'a> Project<'a> { } match path.as_ref() { [] => Some((Some(within), None, path)), - [PathPart::Ident(Identifier(name))] => { + [PathPart::Ident(name)] => { let (ty, val) = self[within].module.get(*name); Some((ty, val, path.pop_front()?)) } - [PathPart::Ident(Identifier(name)), ..] => { + [PathPart::Ident(name), ..] => { let ty = self[within].module.get_type(*name)?; self.get(path.pop_front()?, ty) } @@ -108,7 +108,7 @@ impl<'a> Project<'a> { match front { PathPart::SelfKw => self.get_type(path.pop_front()?, within), PathPart::SuperKw => self.get_type(path.pop_front()?, module.parent?), - PathPart::Ident(Identifier(name)) => match module.types.get(name) { + PathPart::Ident(name) => match module.types.get(name) { Some(&submodule) => self.get_type(path.pop_front()?, submodule), None => Some((within, path)), }, @@ -120,7 +120,7 @@ impl<'a> Project<'a> { pub fn get_value<'p>(&self, path: Path<'p>, within: DefID) -> Option<(DefID, Path<'p>)> { match path.front()? { - PathPart::Ident(Identifier(name)) => Some(( + PathPart::Ident(name) => Some(( self[within].module.values.get(name).copied()?, path.pop_front()?, )), @@ -280,7 +280,7 @@ pub mod evaluate { .parent_of(parent) .ok_or_else(|| "Attempt to get super of root".into()), PathPart::SelfKw => Ok(parent), - PathPart::Ident(Identifier(name)) => name.evaluate(prj, parent), + PathPart::Ident(name) => name.evaluate(prj, parent), } } } diff --git a/compiler/cl-typeck/src/type_resolver.rs b/compiler/cl-typeck/src/type_resolver.rs index 6d6ca45..65f2d5e 100644 --- a/compiler/cl-typeck/src/type_resolver.rs +++ b/compiler/cl-typeck/src/type_resolver.rs @@ -34,7 +34,7 @@ pub fn resolve(prj: &mut Prj, id: DefID) -> Result<(), &'static str> { eprintln!("Resolver: \x1b[32mEvaluating\x1b[0m \"\x1b[36m{kind} {name}\x1b[0m\" (`{id:?}`)"); - for Meta { name: Identifier(name), kind } in meta { + for Meta { name, kind } in meta { if let ("intrinsic", MetaKind::Equals(Literal::String(s))) = (&**name, kind) { prj[id].kind = DefKind::Type(TypeKind::Intrinsic( s.parse().map_err(|_| "Failed to parse intrinsic")?, @@ -85,7 +85,7 @@ impl<'a> TypeResolvable<'a> for &'a Meta { #[allow(unused_variables)] fn resolve_type(self, prj: &mut Prj<'a>, id: DefID) -> Result { - let Meta { name: Identifier(name), kind } = self; + let Meta { name, kind } = self; match (name.as_ref(), kind) { ("intrinsic", MetaKind::Equals(Literal::String(intrinsic))) => Ok(DefKind::Type( TypeKind::Intrinsic(intrinsic.parse().map_err(|_| "unknown intrinsic type")?), @@ -134,7 +134,7 @@ impl<'a> TypeResolvable<'a> for &'a Enum { return Ok(DefKind::Type(TypeKind::Adt(Adt::FieldlessEnum))); }; let mut fields = vec![]; - for Variant { name: Identifier(name), kind: _ } in v { + for Variant { name, kind: _ } in v { let id = prj[id].module.get_type(*name); fields.push((*name, id)) } @@ -201,7 +201,7 @@ impl<'a> TypeResolvable<'a> for &'a StructMember { fn resolve_type(self, prj: &mut Prj<'a>, id: DefID) -> Result { let parent = prj.parent_of(id).unwrap_or(id); - let StructMember { name: Identifier(name), vis, ty } = self; + let StructMember { name, vis, ty } = self; let ty = ty .evaluate(prj, parent) diff --git a/compiler/cl-typeck/src/use_importer.rs b/compiler/cl-typeck/src/use_importer.rs index d89a2b7..aaca810 100644 --- a/compiler/cl-typeck/src/use_importer.rs +++ b/compiler/cl-typeck/src/use_importer.rs @@ -58,7 +58,7 @@ impl<'a> Project<'a> { } UseTree::Name(name) => self.visit_use_leaf(name, parent, c)?, - UseTree::Alias(Identifier(from), Identifier(to)) => { + UseTree::Alias(from, to) => { self.visit_use_alias(from, to, parent, c)? } UseTree::Glob => self.visit_use_glob(parent, c)?, @@ -70,8 +70,7 @@ impl<'a> Project<'a> { Ok(()) } - pub fn visit_use_leaf(&mut self, part: &'a Identifier, parent: DefID, c: DefID) -> UseResult { - let Identifier(name) = part; + pub fn visit_use_leaf(&mut self, name: &'a Sym, parent: DefID, c: DefID) -> UseResult { self.visit_use_alias(name, name, parent, c) }