Compare commits

..

1 Commits

Author SHA1 Message Date
3cb85c7f42 toki: pona 2025-02-19 04:04:40 -06:00
89 changed files with 3747 additions and 10244 deletions

3
.gitignore vendored
View File

@ -6,8 +6,5 @@
**/Cargo.lock
target
# Symbol table dump?
typeck-table.ron
# Pest files generated by Grammatical
*.p*st

View File

@ -2,7 +2,6 @@
members = [
"compiler/cl-repl",
"compiler/cl-typeck",
"compiler/cl-embed",
"compiler/cl-interpret",
"compiler/cl-structures",
"compiler/cl-token",
@ -15,9 +14,9 @@ resolver = "2"
[workspace.package]
repository = "https://git.soft.fish/j/Conlang"
version = "0.0.10"
version = "0.0.9"
authors = ["John Breaux <j@soft.fish>"]
edition = "2024"
edition = "2021"
license = "MIT"
publish = ["soft-fish"]

View File

@ -31,10 +31,19 @@ pub enum Visibility {
Public,
}
/// A [Literal]: 0x42, 1e123, 2.4, "Hello"
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Literal {
Bool(bool),
Char(char),
Int(u128),
Float(u64),
String(String),
}
/// A list of [Item]s
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct File {
pub name: &'static str,
pub items: Vec<Item>,
}
@ -63,7 +72,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 span: Span,
pub extents: Span,
pub attrs: Attrs,
pub vis: Visibility,
pub kind: ItemKind,
@ -93,23 +102,10 @@ pub enum ItemKind {
Use(Use),
}
/// A list of type variables to introduce
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct Generics {
pub vars: Vec<Sym>,
}
/// An ordered collection of [Items](Item)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Module {
pub name: Sym,
pub file: Option<File>,
}
/// An alias to another [Ty]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Alias {
pub name: Sym,
pub to: Sym,
pub from: Option<Box<Ty>>,
}
@ -130,21 +126,40 @@ pub struct Static {
pub init: Box<Expr>,
}
/// An ordered collection of [Items](Item)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Module {
pub name: Sym,
pub kind: ModuleKind,
}
/// The contents of a [Module], if they're in the same file
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ModuleKind {
Inline(File),
Outline,
}
/// Code, and the interface to that code
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Function {
pub name: Sym,
pub gens: Generics,
pub sign: TyFn,
pub bind: Pattern,
pub body: Option<Expr>,
pub bind: Vec<Param>,
pub body: Option<Block>,
}
/// A single parameter for a [Function]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Param {
pub mutability: Mutability,
pub name: Sym,
}
/// A user-defined product type
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Struct {
pub name: Sym,
pub gens: Generics,
pub kind: StructKind,
}
@ -168,22 +183,36 @@ pub struct StructMember {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Enum {
pub name: Sym,
pub gens: Generics,
pub variants: Vec<Variant>,
pub kind: EnumKind,
}
/// An [Enum]'s [Variant]s, if it has a variant block
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum EnumKind {
/// Represents an enum with no variants
NoVariants,
Variants(Vec<Variant>),
}
/// A single [Enum] variant
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Variant {
pub name: Sym,
pub kind: StructKind,
pub body: Option<Box<Expr>>,
pub kind: VariantKind,
}
/// Whether the [Variant] has a C-like constant value, a tuple, or [StructMember]s
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum VariantKind {
Plain,
CLike(u128),
Tuple(Ty),
Struct(Vec<StructMember>),
}
/// Sub-[items](Item) (associated functions, etc.) for a [Ty]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Impl {
pub gens: Generics,
pub target: ImplKind,
pub body: File,
}
@ -215,9 +244,8 @@ pub enum UseTree {
/// A type expression
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Ty {
pub span: Span,
pub extents: Span,
pub kind: TyKind,
pub gens: Generics,
}
/// Information about a [Ty]pe expression
@ -225,33 +253,31 @@ pub struct Ty {
pub enum TyKind {
Never,
Empty,
Infer,
Path(Path),
Array(TyArray),
Slice(TySlice),
Tuple(TyTuple),
Ref(TyRef),
Ptr(TyPtr),
Fn(TyFn),
}
/// An array of [`T`](Ty)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TyArray {
pub ty: Box<Ty>,
pub ty: Box<TyKind>,
pub count: usize,
}
/// A [Ty]pe slice expression: `[T]`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TySlice {
pub ty: Box<Ty>,
pub ty: Box<TyKind>,
}
/// A tuple of [Ty]pes
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TyTuple {
pub types: Vec<Ty>,
pub types: Vec<TyKind>,
}
/// A [Ty]pe-reference expression as (number of `&`, [Path])
@ -259,20 +285,14 @@ pub struct TyTuple {
pub struct TyRef {
pub mutable: Mutability,
pub count: u16,
pub to: Box<Ty>,
}
/// A [Ty]pe-reference expression as (number of `&`, [Path])
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TyPtr {
pub to: Box<Ty>,
pub to: Path,
}
/// The args and return value for a function pointer [Ty]pe
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TyFn {
pub args: Box<Ty>,
pub rety: Box<Ty>,
pub args: Box<TyKind>,
pub rety: Option<Box<Ty>>,
}
/// A path to an [Item] in the [Module] tree
@ -283,9 +303,10 @@ pub struct Path {
}
/// A single component of a [Path]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum PathPart {
SuperKw,
SelfKw,
SelfTy,
Ident(Sym),
}
@ -293,7 +314,7 @@ pub enum PathPart {
/// An abstract statement, and associated metadata
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Stmt {
pub span: Span,
pub extents: Span,
pub kind: StmtKind,
pub semi: Semi,
}
@ -316,7 +337,7 @@ pub enum Semi {
/// An expression, the beating heart of the language
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Expr {
pub span: Span,
pub extents: Span,
pub kind: ExprKind,
}
@ -326,28 +347,12 @@ pub enum ExprKind {
/// An empty expression: `(` `)`
#[default]
Empty,
/// A [Closure] expression: `|` [`Expr`] `|` ( -> [`Ty`])? [`Expr`]
Closure(Closure),
/// A [Tuple] expression: `(` [`Expr`] (`,` [`Expr`])+ `)`
Tuple(Tuple),
/// A [Struct creation](Structor) expression: [Path] `{` ([Fielder] `,`)* [Fielder]? `}`
Structor(Structor),
/// An [Array] literal: `[` [`Expr`] (`,` [`Expr`])\* `]`
Array(Array),
/// An Array literal constructed with [repeat syntax](ArrayRep)
/// `[` [Expr] `;` [Literal] `]`
ArrayRep(ArrayRep),
/// An address-of expression: `&` `mut`? [`Expr`]
AddrOf(AddrOf),
/// A backtick-quoted expression
Quote(Quote),
/// A [Literal]: 0x42, 1e123, 2.4, "Hello"
Literal(Literal),
/// A [Grouping](Group) expression `(` [`Expr`] `)`
Group(Group),
/// A [Block] expression: `{` [`Stmt`]\* [`Expr`]? `}`
Block(Block),
/// A local bind instruction, `let` [`Sym`] `=` [`Expr`]
Let(Let),
/// A [Match] expression: `match` [Expr] `{` ([MatchArm] `,`)* [MatchArm]? `}`
Match(Match),
/// An [Assign]ment expression: [`Expr`] (`=` [`Expr`])\+
Assign(Assign),
/// A [Modify]-assignment expression: [`Expr`] ([`ModifyKind`] [`Expr`])\+
@ -356,23 +361,36 @@ pub enum ExprKind {
Binary(Binary),
/// A [Unary] expression: [`UnaryKind`]\* [`Expr`]
Unary(Unary),
/// A [Cast] expression: [`Expr`] `as` [`Ty`]
Cast(Cast),
/// A [Member] access expression: [`Expr`] [`MemberKind`]\*
Member(Member),
/// An Array [Index] expression: a[10, 20, 30]
Index(Index),
/// A [Cast] expression: [`Expr`] `as` [`Ty`]
Cast(Cast),
/// A [Struct creation](Structor) expression: [Path] `{` ([Fielder] `,`)* [Fielder]? `}`
Structor(Structor),
/// A [path expression](Path): `::`? [PathPart] (`::` [PathPart])*
Path(Path),
/// A local bind instruction, `let` [`Sym`] `=` [`Expr`]
Let(Let),
/// A [Match] expression: `match` [Expr] `{` ([MatchArm] `,`)* [MatchArm]? `}`
Match(Match),
/// A [Literal]: 0x42, 1e123, 2.4, "Hello"
Literal(Literal),
/// An [Array] literal: `[` [`Expr`] (`,` [`Expr`])\* `]`
Array(Array),
/// An Array literal constructed with [repeat syntax](ArrayRep)
/// `[` [Expr] `;` [Literal] `]`
ArrayRep(ArrayRep),
/// An address-of expression: `&` `mut`? [`Expr`]
AddrOf(AddrOf),
/// A [Block] expression: `{` [`Stmt`]\* [`Expr`]? `}`
Block(Block),
/// A [Grouping](Group) expression `(` [`Expr`] `)`
Group(Group),
/// A [Tuple] expression: `(` [`Expr`] (`,` [`Expr`])+ `)`
Tuple(Tuple),
/// A [While] expression: `while` [`Expr`] [`Block`] [`Else`]?
While(While),
/// An [If] expression: `if` [`Expr`] [`Block`] [`Else`]?
If(If),
/// A [For] expression: `for` [`Pattern`] `in` [`Expr`] [`Block`] [`Else`]?
/// A [For] expression: `for` Pattern `in` [`Expr`] [`Block`] [`Else`]?
For(For),
/// A [Break] expression: `break` [`Expr`]?
Break(Break),
@ -382,100 +400,54 @@ pub enum ExprKind {
Continue,
}
/// A Closure [expression](Expr): `|` [`Expr`] `|` ( -> [`Ty`])? [`Expr`]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Closure {
pub arg: Box<Pattern>,
pub body: Box<Expr>,
}
/// A [Tuple] expression: `(` [`Expr`] (`,` [`Expr`])+ `)`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Tuple {
pub exprs: Vec<Expr>,
}
/// A [Struct creation](Structor) expression: [Path] `{` ([Fielder] `,`)* [Fielder]? `}`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Structor {
pub to: Path,
pub init: Vec<Fielder>,
}
/// A [Struct field initializer] expression: [Sym] (`=` [Expr])?
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Fielder {
pub name: Sym,
pub init: Option<Box<Expr>>,
}
/// An [Array] literal: `[` [`Expr`] (`,` [`Expr`])\* `]`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Array {
pub values: Vec<Expr>,
}
/// An Array literal constructed with [repeat syntax](ArrayRep)
/// `[` [Expr] `;` [Literal] `]`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ArrayRep {
pub value: Box<Expr>,
pub repeat: Box<Expr>,
}
/// An address-of expression: `&` `mut`? [`Expr`]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct AddrOf {
pub mutable: Mutability,
pub expr: Box<Expr>,
}
/// A cast expression: [`Expr`] `as` [`Ty`]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Cast {
pub head: Box<Expr>,
pub ty: Ty,
}
/// A backtick-quoted subexpression-literal
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Quote {
pub quote: Box<Expr>,
pub quote: Box<ExprKind>,
}
/// A [Literal]: 0x42, 1e123, 2.4, "Hello"
/// A local variable declaration [Stmt]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Literal {
Bool(bool),
Char(char),
Int(u128),
Float(u64),
String(String),
pub struct Let {
pub mutable: Mutability,
pub name: Pattern,
pub ty: Option<Box<Ty>>,
pub init: Option<Box<Expr>>,
}
/// A [Grouping](Group) expression `(` [`Expr`] `)`
/// A [Pattern] meta-expression (any [`ExprKind`] that fits pattern rules)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Group {
pub expr: Box<Expr>,
pub enum Pattern {
Path(Path),
Literal(Literal),
Ref(Mutability, Box<Pattern>),
Tuple(Vec<Pattern>),
Array(Vec<Pattern>),
Struct(Path, Vec<(Path, Option<Pattern>)>),
}
/// A [Block] expression: `{` [`Stmt`]\* [`Expr`]? `}`
/// A `match` expression: `match` `{` ([MatchArm] `,`)* [MatchArm]? `}`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Block {
pub stmts: Vec<Stmt>,
pub struct Match {
pub scrutinee: Box<Expr>,
pub arms: Vec<MatchArm>,
}
/// A single arm of a [Match] expression: [`Pattern`] `=>` [`Expr`]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct MatchArm(pub Pattern, pub Expr);
/// An [Assign]ment expression: [`Expr`] ([`ModifyKind`] [`Expr`])\+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Assign {
pub parts: Box<(Expr, Expr)>,
pub parts: Box<(ExprKind, ExprKind)>,
}
/// A [Modify]-assignment expression: [`Expr`] ([`ModifyKind`] [`Expr`])\+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Modify {
pub kind: ModifyKind,
pub parts: Box<(Expr, Expr)>,
pub parts: Box<(ExprKind, ExprKind)>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@ -496,7 +468,7 @@ pub enum ModifyKind {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Binary {
pub kind: BinaryKind,
pub parts: Box<(Expr, Expr)>,
pub parts: Box<(ExprKind, ExprKind)>,
}
/// A [Binary] operator
@ -530,7 +502,7 @@ pub enum BinaryKind {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Unary {
pub kind: UnaryKind,
pub tail: Box<Expr>,
pub tail: Box<ExprKind>,
}
/// A [Unary] operator
@ -539,8 +511,6 @@ pub enum UnaryKind {
Deref,
Neg,
Not,
RangeInc,
RangeExc,
/// A Loop expression: `loop` [`Block`]
Loop,
/// Unused
@ -549,10 +519,17 @@ pub enum UnaryKind {
Tilde,
}
/// A cast expression: [`Expr`] `as` [`Ty`]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Cast {
pub head: Box<ExprKind>,
pub ty: Ty,
}
/// A [Member] access expression: [`Expr`] [`MemberKind`]\*
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Member {
pub head: Box<Expr>,
pub head: Box<ExprKind>,
pub kind: MemberKind,
}
@ -567,44 +544,61 @@ pub enum MemberKind {
/// A repeated [Index] expression: a[10, 20, 30][40, 50, 60]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Index {
pub head: Box<Expr>,
pub head: Box<ExprKind>,
pub indices: Vec<Expr>,
}
/// A local variable declaration [Stmt]
/// A [Struct creation](Structor) expression: [Path] `{` ([Fielder] `,`)* [Fielder]? `}`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Let {
pub mutable: Mutability,
pub name: Pattern,
pub ty: Option<Box<Ty>>,
pub struct Structor {
pub to: Path,
pub init: Vec<Fielder>,
}
/// A [Struct field initializer] expression: [Sym] (`=` [Expr])?
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Fielder {
pub name: Sym,
pub init: Option<Box<Expr>>,
}
/// A `match` expression: `match` `{` ([MatchArm] `,`)* [MatchArm]? `}`
/// An [Array] literal: `[` [`Expr`] (`,` [`Expr`])\* `]`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Match {
pub scrutinee: Box<Expr>,
pub arms: Vec<MatchArm>,
pub struct Array {
pub values: Vec<Expr>,
}
/// A single arm of a [Match] expression: [`Pattern`] `=>` [`Expr`]
/// An Array literal constructed with [repeat syntax](ArrayRep)
/// `[` [Expr] `;` [Literal] `]`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct MatchArm(pub Pattern, pub Expr);
pub struct ArrayRep {
pub value: Box<ExprKind>,
pub repeat: Box<ExprKind>,
}
/// A [Pattern] meta-expression (any [`ExprKind`] that fits pattern rules)
/// An address-of expression: `&` `mut`? [`Expr`]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Pattern {
Name(Sym),
Path(Path),
Literal(Literal),
Rest(Option<Box<Pattern>>),
Ref(Mutability, Box<Pattern>),
RangeExc(Box<Pattern>, Box<Pattern>),
RangeInc(Box<Pattern>, Box<Pattern>),
Tuple(Vec<Pattern>),
Array(Vec<Pattern>),
Struct(Path, Vec<(Sym, Option<Pattern>)>),
TupleStruct(Path, Vec<Pattern>),
pub struct AddrOf {
pub mutable: Mutability,
pub expr: Box<ExprKind>,
}
/// A [Block] expression: `{` [`Stmt`]\* [`Expr`]? `}`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Block {
pub stmts: Vec<Stmt>,
}
/// A [Grouping](Group) expression `(` [`Expr`] `)`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Group {
pub expr: Box<ExprKind>,
}
/// A [Tuple] expression: `(` [`Expr`] (`,` [`Expr`])+ `)`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Tuple {
pub exprs: Vec<Expr>,
}
/// A [While] expression: `while` [`Expr`] [`Block`] [`Else`]?
@ -626,7 +620,7 @@ pub struct If {
/// A [For] expression: `for` Pattern `in` [`Expr`] [`Block`] [`Else`]?
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct For {
pub bind: Pattern,
pub bind: Sym, // TODO: Patterns?
pub cond: Box<Expr>,
pub pass: Box<Block>,
pub fail: Else,

File diff suppressed because it is too large Load Diff

View File

@ -1,162 +0,0 @@
//! Converts between major enums and enum variants
use super::*;
impl<T: AsRef<str>> From<T> for PathPart {
fn from(value: T) -> Self {
match value.as_ref() {
"super" => PathPart::SuperKw,
ident => PathPart::Ident(ident.into()),
}
}
}
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 {
Alias => ItemKind::Alias,
Const => ItemKind::Const,
Static => ItemKind::Static,
Module => ItemKind::Module,
Function => ItemKind::Function,
Struct => ItemKind::Struct,
Enum => ItemKind::Enum,
Impl => ItemKind::Impl,
Use => ItemKind::Use,
}
impl From for StructKind {
Vec<Ty> => StructKind::Tuple,
// TODO: Struct members in struct
}
impl From for TyKind {
Path => TyKind::Path,
TyTuple => TyKind::Tuple,
TyRef => TyKind::Ref,
TyPtr => TyKind::Ptr,
TyFn => TyKind::Fn,
}
impl From for StmtKind {
Item => StmtKind::Item,
Expr => StmtKind::Expr,
}
impl From for ExprKind {
Let => ExprKind::Let,
Closure => ExprKind::Closure,
Quote => ExprKind::Quote,
Match => ExprKind::Match,
Assign => ExprKind::Assign,
Modify => ExprKind::Modify,
Binary => ExprKind::Binary,
Unary => ExprKind::Unary,
Cast => ExprKind::Cast,
Member => ExprKind::Member,
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,
Break => ExprKind::Break,
Return => ExprKind::Return,
}
impl From for Literal {
bool => Literal::Bool,
char => Literal::Char,
u128 => Literal::Int,
String => Literal::String,
}
}
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()) }
}
}
impl TryFrom<Expr> for Pattern {
type Error = Expr;
/// Performs the conversion. On failure, returns the *first* non-pattern subexpression.
fn try_from(value: Expr) -> Result<Self, Self::Error> {
Ok(match value.kind {
ExprKind::Literal(literal) => Pattern::Literal(literal),
ExprKind::Path(Path { absolute: false, ref parts }) => match parts.as_slice() {
[PathPart::Ident(name)] => Pattern::Name(*name),
_ => Err(value)?,
},
ExprKind::Empty => Pattern::Tuple(vec![]),
ExprKind::Group(Group { expr }) => Pattern::Tuple(vec![Pattern::try_from(*expr)?]),
ExprKind::Tuple(Tuple { exprs }) => Pattern::Tuple(
exprs
.into_iter()
.map(Pattern::try_from)
.collect::<Result<_, _>>()?,
),
ExprKind::AddrOf(AddrOf { mutable, expr }) => {
Pattern::Ref(mutable, Box::new(Pattern::try_from(*expr)?))
}
ExprKind::Array(Array { values }) => Pattern::Array(
values
.into_iter()
.map(Pattern::try_from)
.collect::<Result<_, _>>()?,
),
ExprKind::Binary(Binary { kind: BinaryKind::Call, parts }) => {
let (Expr { kind: ExprKind::Path(path), .. }, args) = *parts else {
return Err(parts.0);
};
match args.kind {
ExprKind::Empty | ExprKind::Tuple(_) => {}
_ => return Err(args),
}
let Pattern::Tuple(args) = Pattern::try_from(args)? else {
unreachable!("Arguments should be convertible to pattern!")
};
Pattern::TupleStruct(path, args)
}
ExprKind::Binary(Binary { kind: BinaryKind::RangeExc, parts }) => {
let (head, tail) = (Pattern::try_from(parts.0)?, Pattern::try_from(parts.1)?);
Pattern::RangeExc(head.into(), tail.into())
}
ExprKind::Binary(Binary { kind: BinaryKind::RangeInc, parts }) => {
let (head, tail) = (Pattern::try_from(parts.0)?, Pattern::try_from(parts.1)?);
Pattern::RangeInc(head.into(), tail.into())
}
ExprKind::Unary(Unary { kind: UnaryKind::RangeExc, tail }) => {
Pattern::Rest(Some(Pattern::try_from(*tail)?.into()))
}
ExprKind::Structor(Structor { to, init }) => {
let fields = init
.into_iter()
.map(|Fielder { name, init }| {
Ok((name, init.map(|i| Pattern::try_from(*i)).transpose()?))
})
.collect::<Result<_, Self::Error>>()?;
Pattern::Struct(to, fields)
}
_ => Err(value)?,
})
}
}

View File

@ -1,771 +0,0 @@
//! Implements [Display] for [AST](super::super) Types
use super::*;
use format::{delimiters::*, *};
use std::{
borrow::Borrow,
fmt::{Display, Write},
};
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() {
if idx > 0 {
f.write_str(sep)?;
}
write!(f, "{item}")?;
}
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),
}
}
}
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),
}
}
}
impl Display for Literal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Literal::Bool(v) => v.fmt(f),
Literal::Char(v) => write!(f, "'{}'", v.escape_debug()),
Literal::Int(v) => v.fmt(f),
Literal::Float(v) => write!(f, "{:?}", f64::from_bits(*v)),
Literal::String(v) => write!(f, "\"{}\"", v.escape_debug()),
}
}
}
impl Display for File {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
separate(&self.items, "\n\n")(f)
}
}
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)?;
separate(meta, ", ")(&mut f.delimit(INLINE_SQUARE))?;
"\n".fmt(f)
}
}
impl Display for Meta {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self;
write!(f, "{name}{kind}")
}
}
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}"),
MetaKind::Func(args) => separate(args, ", ")(f.delimit(INLINE_PARENS)),
}
}
}
impl Display for Item {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { span: _, attrs, vis, kind } = self;
attrs.fmt(f)?;
vis.fmt(f)?;
kind.fmt(f)
}
}
impl Display for ItemKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ItemKind::Alias(v) => v.fmt(f),
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),
ItemKind::Use(v) => v.fmt(f),
}
}
}
impl Display for Generics {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Generics { vars } = self;
if !vars.is_empty() {
separate(vars, ", ")(f.delimit_with("<", ">"))?
}
Ok(())
}
}
impl Display for Alias {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, from } = self;
match from {
Some(from) => write!(f, "type {name} = {from};"),
None => write!(f, "type {name};"),
}
}
}
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}")
}
}
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}")
}
}
impl Display for Module {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, file } = self;
write!(f, "mod {name}")?;
match file {
Some(items) => {
' '.fmt(f)?;
write!(f.delimit(BRACES), "{items}")
}
None => Ok(()),
}
}
}
impl Display for Function {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, gens, sign: sign @ TyFn { args, rety }, bind, body } = self;
let types = match args.kind {
TyKind::Tuple(TyTuple { ref types }) => types.as_slice(),
TyKind::Empty => Default::default(),
_ => {
write!(f, "Invalid function signature: {sign}")?;
Default::default()
}
};
let bind = match bind {
Pattern::Tuple(patterns) => patterns.as_slice(),
_ => {
write!(f, "Invalid argument binder: {bind}")?;
Default::default()
}
};
debug_assert_eq!(bind.len(), types.len());
write!(f, "fn {name}{gens} ")?;
{
let mut f = f.delimit(INLINE_PARENS);
for (idx, (arg, ty)) in bind.iter().zip(types.iter()).enumerate() {
if idx != 0 {
f.write_str(", ")?;
}
write!(f, "{arg}: {ty}")?;
}
}
if TyKind::Empty != rety.kind {
write!(f, " -> {rety}")?
}
match body {
Some(body) => write!(f, " {body}"),
None => ';'.fmt(f),
}
}
}
impl Display for Struct {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, gens, kind } = self;
write!(f, "struct {name}{gens}{kind}")
}
}
impl Display for StructKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StructKind::Empty => ';'.fmt(f),
StructKind::Tuple(v) => separate(v, ", ")(f.delimit(INLINE_PARENS)),
StructKind::Struct(v) => separate(v, ",\n")(f.delimit(SPACED_BRACES)),
}
}
}
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}")
}
}
impl Display for Enum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, gens, variants } = self;
write!(f, "enum {name}{gens}")?;
separate(variants, ",\n")(f.delimit(SPACED_BRACES))
}
}
impl Display for Variant {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind, body } = self;
write!(f, "{name}{kind}")?;
match body {
Some(body) => write!(f, " {body}"),
None => Ok(()),
}
}
}
impl Display for Impl {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { gens, target, body } = self;
write!(f, "impl{gens} {target} ")?;
write!(f.delimit(BRACES), "{body}")
}
}
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}")
}
}
}
}
impl Display for Use {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { absolute, tree } = self;
f.write_str(if *absolute { "use ::" } else { "use " })?;
write!(f, "{tree};")
}
}
impl Display for UseTree {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UseTree::Tree(tree) => separate(tree, ", ")(f.delimit(INLINE_BRACES)),
UseTree::Path(path, rest) => write!(f, "{path}::{rest}"),
UseTree::Alias(path, name) => write!(f, "{path} as {name}"),
UseTree::Name(name) => write!(f, "{name}"),
UseTree::Glob => write!(f, "*"),
}
}
}
impl Display for Ty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { span: _, kind, gens } = self;
write!(f, "{kind}{gens}")
}
}
impl Display for TyKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TyKind::Never => "!".fmt(f),
TyKind::Empty => "()".fmt(f),
TyKind::Infer => "_".fmt(f),
TyKind::Path(v) => v.fmt(f),
TyKind::Array(v) => v.fmt(f),
TyKind::Slice(v) => v.fmt(f),
TyKind::Tuple(v) => v.fmt(f),
TyKind::Ref(v) => v.fmt(f),
TyKind::Ptr(v) => v.fmt(f),
TyKind::Fn(v) => v.fmt(f),
}
}
}
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}]")
}
}
impl Display for TyTuple {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
separate(&self.types, ", ")(f.delimit(INLINE_PARENS))
}
}
impl Display for TyRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let &Self { count, mutable, ref to } = self;
for _ in 0..count {
f.write_char('&')?;
}
write!(f, "{mutable}{to}")
}
}
impl Display for TyPtr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { to } = self;
write!(f, "*{to}")
}
}
impl Display for TyFn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { args, rety } = self;
write!(f, "fn {args}")?;
if TyKind::Empty != rety.kind {
write!(f, " -> {rety}")?;
}
Ok(())
}
}
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::SelfTy => "Self".fmt(f),
PathPart::Ident(id) => id.fmt(f),
}
}
}
impl Display for Stmt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Stmt { span: _, kind, semi } = self;
write!(f, "{kind}{semi}")
}
}
impl Display for StmtKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StmtKind::Empty => Ok(()),
StmtKind::Item(v) => v.fmt(f),
StmtKind::Expr(v) => v.fmt(f),
}
}
}
impl Display for Semi {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Semi::Terminated => ';'.fmt(f),
Semi::Unterminated => Ok(()),
}
}
}
impl Display for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.kind.fmt(f)
}
}
impl Display for ExprKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExprKind::Empty => "()".fmt(f),
ExprKind::Closure(v) => v.fmt(f),
ExprKind::Quote(v) => v.fmt(f),
ExprKind::Let(v) => v.fmt(f),
ExprKind::Match(v) => v.fmt(f),
ExprKind::Assign(v) => v.fmt(f),
ExprKind::Modify(v) => v.fmt(f),
ExprKind::Binary(v) => v.fmt(f),
ExprKind::Unary(v) => v.fmt(f),
ExprKind::Cast(v) => v.fmt(f),
ExprKind::Member(v) => v.fmt(f),
ExprKind::Index(v) => v.fmt(f),
ExprKind::Structor(v) => v.fmt(f),
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),
ExprKind::Break(v) => v.fmt(f),
ExprKind::Return(v) => v.fmt(f),
ExprKind::Continue => "continue".fmt(f),
}
}
}
impl Display for Closure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { arg, body } = self;
match arg.as_ref() {
Pattern::Tuple(args) => separate(args, ", ")(f.delimit_with("|", "|")),
_ => arg.fmt(f),
}?;
write!(f, " {body}")
}
}
impl Display for Quote {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { quote } = self;
write!(f, "`{quote}`")
}
}
impl Display for Let {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { mutable, name, ty, init } = self;
write!(f, "let {mutable}{name}")?;
if let Some(value) = ty {
write!(f, ": {value}")?;
}
if let Some(value) = init {
write!(f, " = {value}")?;
}
Ok(())
}
}
impl Display for Pattern {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Pattern::Name(sym) => sym.fmt(f),
Pattern::Path(path) => path.fmt(f),
Pattern::Literal(literal) => literal.fmt(f),
Pattern::Rest(Some(name)) => write!(f, "..{name}"),
Pattern::Rest(None) => "..".fmt(f),
Pattern::Ref(mutability, pattern) => write!(f, "&{mutability}{pattern}"),
Pattern::RangeExc(head, tail) => write!(f, "{head}..{tail}"),
Pattern::RangeInc(head, tail) => write!(f, "{head}..={tail}"),
Pattern::Tuple(patterns) => separate(patterns, ", ")(f.delimit(INLINE_PARENS)),
Pattern::Array(patterns) => separate(patterns, ", ")(f.delimit(INLINE_SQUARE)),
Pattern::Struct(path, items) => {
write!(f, "{path} ")?;
let f = &mut f.delimit(INLINE_BRACES);
for (idx, (name, item)) in items.iter().enumerate() {
if idx != 0 {
f.write_str(", ")?;
}
write!(f, "{name}")?;
if let Some(pattern) = item {
write!(f, ": {pattern}")?
}
}
Ok(())
}
Pattern::TupleStruct(path, items) => {
write!(f, "{path}")?;
separate(items, ", ")(f.delimit(INLINE_PARENS))
}
}
}
}
impl Display for Match {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { scrutinee, arms } = self;
write!(f, "match {scrutinee} ")?;
separate(arms, ",\n")(f.delimit(BRACES))
}
}
impl Display for MatchArm {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self(pat, expr) = self;
write!(f, "{pat} => {expr}")
}
}
impl Display for Assign {
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 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { kind, parts } = self;
write!(f, "{} {kind} {}", parts.0, parts.1)
}
}
impl Display for ModifyKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ModifyKind::Mul => "*=",
ModifyKind::Div => "/=",
ModifyKind::Rem => "%=",
ModifyKind::Add => "+=",
ModifyKind::Sub => "-=",
ModifyKind::And => "&=",
ModifyKind::Or => "|=",
ModifyKind::Xor => "^=",
ModifyKind::Shl => "<<=",
ModifyKind::Shr => ">>=",
}
.fmt(f)
}
}
impl Display for Binary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { kind, parts } = self;
let (head, tail) = parts.borrow();
match kind {
BinaryKind::Call => write!(f, "{head}{tail}"),
_ => write!(f, "{head} {kind} {tail}"),
}
}
}
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 => "%",
BinaryKind::Call => "()",
}
.fmt(f)
}
}
impl Display for Unary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { kind, tail } = self;
write!(f, "{kind}{tail}")
}
}
impl Display for UnaryKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UnaryKind::Loop => "loop ",
UnaryKind::Deref => "*",
UnaryKind::Neg => "-",
UnaryKind::Not => "!",
UnaryKind::RangeExc => "..",
UnaryKind::RangeInc => "..=",
UnaryKind::At => "@",
UnaryKind::Tilde => "~",
}
.fmt(f)
}
}
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}")
}
}
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}"),
}
}
}
impl Display for Index {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { head, indices } = self;
write!(f, "{head}")?;
separate(indices, ", ")(f.delimit(INLINE_SQUARE))
}
}
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(())
}
}
impl Display for Array {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
separate(&self.values, ", ")(f.delimit(INLINE_SQUARE))
}
}
impl Display for ArrayRep {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { value, repeat } = self;
write!(f, "[{value}; {repeat}]")
}
}
impl Display for AddrOf {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { mutable, expr } = self;
write!(f, "&{mutable}{expr}")
}
}
impl Display for Block {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { stmts } = self;
match stmts.as_slice() {
[] => "{}".fmt(f),
stmts => separate(stmts, "\n")(f.delimit(BRACES)),
}
}
}
impl Display for Group {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({})", self.expr)
}
}
impl Display for Tuple {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { exprs } = self;
match exprs.as_slice() {
[] => write!(f, "()"),
[expr] => write!(f, "({expr},)"),
exprs => separate(exprs, ", ")(f.delimit(INLINE_PARENS)),
}
}
}
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}")
}
}
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}")
}
}
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}")
}
}
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(()),
}
}
}
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(()),
}
}
}

View File

@ -1,60 +0,0 @@
//! Utils for [Path]
use crate::{PathPart, Sym, ast::Path};
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`
pub fn concat(mut self, other: &Self) -> Self {
if other.absolute {
other.clone()
} else {
self.parts.extend(other.parts.iter().cloned());
self
}
}
/// Gets the defining [Sym] of this path
pub fn as_sym(&self) -> Option<Sym> {
match self.parts.as_slice() {
[.., PathPart::Ident(name)] => Some(*name),
_ => None,
}
}
/// Checks whether this path ends in the given [Sym]
pub fn ends_with(&self, name: &str) -> bool {
match self.parts.as_slice() {
[.., PathPart::Ident(last)] => name == &**last,
_ => false,
}
}
/// 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() {
return true;
}
}
false
}
}
impl PathPart {
pub fn from_sym(ident: Sym) -> Self {
Self::Ident(ident)
}
}
impl From<Sym> for Path {
fn from(value: Sym) -> Self {
Self { parts: vec![PathPart::Ident(value)], absolute: false }
}
}

View File

@ -1,617 +0,0 @@
//! Approximates the size of an AST
use std::mem::size_of_val;
use crate::ast::*;
use cl_structures::{intern::interned::Interned, span::Span};
/// Approximates the size of an AST without including indirection (pointers) or padding
pub trait WeightOf {
/// Approximates the size of a syntax tree without including pointer/indirection or padding.
fn weight_of(&self) -> usize;
}
impl WeightOf for File {
fn weight_of(&self) -> usize {
let Self { name, items } = self;
name.weight_of() + items.weight_of()
}
}
impl WeightOf for Attrs {
fn weight_of(&self) -> usize {
let Self { meta } = self;
meta.weight_of()
}
}
impl WeightOf for Meta {
fn weight_of(&self) -> usize {
let Self { name, kind } = self;
name.weight_of() + kind.weight_of()
}
}
impl WeightOf for MetaKind {
fn weight_of(&self) -> usize {
match self {
MetaKind::Plain => size_of_val(self),
MetaKind::Equals(v) => v.weight_of(),
MetaKind::Func(v) => v.weight_of(),
}
}
}
impl WeightOf for Item {
fn weight_of(&self) -> usize {
let Self { span, attrs, vis, kind } = self;
span.weight_of() + attrs.weight_of() + vis.weight_of() + kind.weight_of()
}
}
impl WeightOf for ItemKind {
fn weight_of(&self) -> usize {
match self {
ItemKind::Module(v) => v.weight_of(),
ItemKind::Alias(v) => v.weight_of(),
ItemKind::Enum(v) => v.weight_of(),
ItemKind::Struct(v) => v.weight_of(),
ItemKind::Const(v) => v.weight_of(),
ItemKind::Static(v) => v.weight_of(),
ItemKind::Function(v) => v.weight_of(),
ItemKind::Impl(v) => v.weight_of(),
ItemKind::Use(v) => v.weight_of(),
}
}
}
impl WeightOf for Generics {
fn weight_of(&self) -> usize {
let Self { vars } = self;
vars.iter().map(|v| v.weight_of()).sum()
}
}
impl WeightOf for Module {
fn weight_of(&self) -> usize {
let Self { name, file } = self;
name.weight_of() + file.weight_of()
}
}
impl WeightOf for Alias {
fn weight_of(&self) -> usize {
let Self { name, from } = self;
name.weight_of() + from.weight_of()
}
}
impl WeightOf for Const {
fn weight_of(&self) -> usize {
let Self { name, ty, init } = self;
name.weight_of() + ty.weight_of() + init.weight_of()
}
}
impl WeightOf for Static {
fn weight_of(&self) -> usize {
let Self { mutable, name, ty, init } = self;
mutable.weight_of() + name.weight_of() + ty.weight_of() + init.weight_of()
}
}
impl WeightOf for Function {
fn weight_of(&self) -> usize {
let Self { name, gens, sign, bind, body } = self;
name.weight_of() + gens.weight_of() + sign.weight_of() + bind.weight_of() + body.weight_of()
}
}
impl WeightOf for Struct {
fn weight_of(&self) -> usize {
let Self { name, gens, kind } = self;
name.weight_of() + gens.weight_of() + kind.weight_of()
}
}
impl WeightOf for StructKind {
fn weight_of(&self) -> usize {
match self {
StructKind::Empty => size_of_val(self),
StructKind::Tuple(items) => items.weight_of(),
StructKind::Struct(sm) => sm.weight_of(),
}
}
}
impl WeightOf for StructMember {
fn weight_of(&self) -> usize {
let Self { vis, name, ty } = self;
vis.weight_of() + name.weight_of() + ty.weight_of()
}
}
impl WeightOf for Enum {
fn weight_of(&self) -> usize {
let Self { name, gens, variants } = self;
name.weight_of() + gens.weight_of() + variants.weight_of()
}
}
impl WeightOf for Variant {
fn weight_of(&self) -> usize {
let Self { name, kind, body } = self;
name.weight_of() + kind.weight_of() + body.weight_of()
}
}
impl WeightOf for Impl {
fn weight_of(&self) -> usize {
let Self { gens, target, body } = self;
gens.weight_of() + target.weight_of() + body.weight_of()
}
}
impl WeightOf for ImplKind {
fn weight_of(&self) -> usize {
match self {
ImplKind::Type(ty) => ty.weight_of(),
ImplKind::Trait { impl_trait, for_type } => {
impl_trait.weight_of() + for_type.weight_of()
}
}
}
}
impl WeightOf for Use {
fn weight_of(&self) -> usize {
let Self { absolute, tree } = self;
absolute.weight_of() + tree.weight_of()
}
}
impl WeightOf for UseTree {
fn weight_of(&self) -> usize {
match self {
UseTree::Tree(tr) => tr.weight_of(),
UseTree::Path(pa, tr) => pa.weight_of() + tr.weight_of(),
UseTree::Alias(src, dst) => src.weight_of() + dst.weight_of(),
UseTree::Name(src) => src.weight_of(),
UseTree::Glob => size_of_val(self),
}
}
}
impl WeightOf for Ty {
fn weight_of(&self) -> usize {
let Self { span, kind, gens } = self;
span.weight_of() + kind.weight_of() + gens.weight_of()
}
}
impl WeightOf for TyKind {
fn weight_of(&self) -> usize {
match self {
TyKind::Never | TyKind::Empty | TyKind::Infer => size_of_val(self),
TyKind::Path(v) => v.weight_of(),
TyKind::Array(v) => v.weight_of(),
TyKind::Slice(v) => v.weight_of(),
TyKind::Tuple(v) => v.weight_of(),
TyKind::Ref(v) => v.weight_of(),
TyKind::Ptr(v) => v.weight_of(),
TyKind::Fn(v) => v.weight_of(),
}
}
}
impl WeightOf for TyArray {
fn weight_of(&self) -> usize {
let Self { ty, count } = self;
ty.weight_of() + count.weight_of()
}
}
impl WeightOf for TySlice {
fn weight_of(&self) -> usize {
let Self { ty } = self;
ty.weight_of()
}
}
impl WeightOf for TyTuple {
fn weight_of(&self) -> usize {
let Self { types } = self;
types.weight_of()
}
}
impl WeightOf for TyRef {
fn weight_of(&self) -> usize {
let Self { mutable, count, to } = self;
mutable.weight_of() + count.weight_of() + to.weight_of()
}
}
impl WeightOf for TyPtr {
fn weight_of(&self) -> usize {
let Self { to } = self;
to.weight_of()
}
}
impl WeightOf for TyFn {
fn weight_of(&self) -> usize {
let Self { args, rety } = self;
args.weight_of() + rety.weight_of()
}
}
impl WeightOf for Path {
fn weight_of(&self) -> usize {
let Self { absolute, parts } = self;
absolute.weight_of() + parts.weight_of()
}
}
impl WeightOf for PathPart {
fn weight_of(&self) -> usize {
match self {
PathPart::SuperKw => size_of_val(self),
PathPart::SelfTy => size_of_val(self),
PathPart::Ident(interned) => interned.weight_of(),
}
}
}
impl WeightOf for Stmt {
fn weight_of(&self) -> usize {
let Self { span, kind, semi } = self;
span.weight_of() + kind.weight_of() + semi.weight_of()
}
}
impl WeightOf for StmtKind {
fn weight_of(&self) -> usize {
match self {
StmtKind::Empty => size_of_val(self),
StmtKind::Item(item) => item.weight_of(),
StmtKind::Expr(expr) => expr.weight_of(),
}
}
}
impl WeightOf for Expr {
fn weight_of(&self) -> usize {
let Self { span, kind } = self;
span.weight_of() + kind.weight_of()
}
}
impl WeightOf for ExprKind {
fn weight_of(&self) -> usize {
match self {
ExprKind::Empty => size_of_val(self),
ExprKind::Closure(v) => v.weight_of(),
ExprKind::Quote(v) => v.weight_of(),
ExprKind::Let(v) => v.weight_of(),
ExprKind::Match(v) => v.weight_of(),
ExprKind::Assign(v) => v.weight_of(),
ExprKind::Modify(v) => v.weight_of(),
ExprKind::Binary(v) => v.weight_of(),
ExprKind::Unary(v) => v.weight_of(),
ExprKind::Cast(v) => v.weight_of(),
ExprKind::Member(v) => v.weight_of(),
ExprKind::Index(v) => v.weight_of(),
ExprKind::Structor(v) => v.weight_of(),
ExprKind::Path(v) => v.weight_of(),
ExprKind::Literal(v) => v.weight_of(),
ExprKind::Array(v) => v.weight_of(),
ExprKind::ArrayRep(v) => v.weight_of(),
ExprKind::AddrOf(v) => v.weight_of(),
ExprKind::Block(v) => v.weight_of(),
ExprKind::Group(v) => v.weight_of(),
ExprKind::Tuple(v) => v.weight_of(),
ExprKind::While(v) => v.weight_of(),
ExprKind::If(v) => v.weight_of(),
ExprKind::For(v) => v.weight_of(),
ExprKind::Break(v) => v.weight_of(),
ExprKind::Return(v) => v.weight_of(),
ExprKind::Continue => size_of_val(self),
}
}
}
impl WeightOf for Closure {
fn weight_of(&self) -> usize {
let Self { arg, body } = self;
arg.weight_of() + body.weight_of()
}
}
impl WeightOf for Quote {
fn weight_of(&self) -> usize {
let Self { quote } = self;
quote.weight_of()
}
}
impl WeightOf for Let {
fn weight_of(&self) -> usize {
let Self { mutable, name, ty, init } = self;
mutable.weight_of() + name.weight_of() + ty.weight_of() + init.weight_of()
}
}
impl WeightOf for Pattern {
fn weight_of(&self) -> usize {
match self {
Pattern::Name(s) => size_of_val(s),
Pattern::Path(p) => p.weight_of(),
Pattern::Literal(literal) => literal.weight_of(),
Pattern::Rest(Some(pattern)) => pattern.weight_of(),
Pattern::Rest(None) => 0,
Pattern::Ref(mutability, pattern) => mutability.weight_of() + pattern.weight_of(),
Pattern::RangeExc(head, tail) => head.weight_of() + tail.weight_of(),
Pattern::RangeInc(head, tail) => head.weight_of() + tail.weight_of(),
Pattern::Tuple(patterns) | Pattern::Array(patterns) => patterns.weight_of(),
Pattern::Struct(path, items) => {
let sitems: usize = items
.iter()
.map(|(name, opt)| name.weight_of() + opt.weight_of())
.sum();
path.weight_of() + sitems
}
Pattern::TupleStruct(path, patterns) => path.weight_of() + patterns.weight_of(),
}
}
}
impl WeightOf for Match {
fn weight_of(&self) -> usize {
let Self { scrutinee, arms } = self;
scrutinee.weight_of() + arms.weight_of()
}
}
impl WeightOf for MatchArm {
fn weight_of(&self) -> usize {
let Self(pattern, expr) = self;
pattern.weight_of() + expr.weight_of()
}
}
impl WeightOf for Assign {
fn weight_of(&self) -> usize {
let Self { parts } = self;
parts.0.weight_of() + parts.1.weight_of()
}
}
impl WeightOf for Modify {
#[rustfmt::skip]
fn weight_of(&self) -> usize {
let Self { kind, parts } = self;
kind.weight_of()
+ parts.0.weight_of()
+ parts.1.weight_of()
}
}
impl WeightOf for Binary {
fn weight_of(&self) -> usize {
let Self { kind, parts } = self;
kind.weight_of() + parts.0.weight_of() + parts.1.weight_of()
}
}
impl WeightOf for Unary {
#[rustfmt::skip]
fn weight_of(&self) -> usize {
let Self { kind, tail } = self;
kind.weight_of() + tail.weight_of()
}
}
impl WeightOf for Cast {
fn weight_of(&self) -> usize {
let Self { head, ty } = self;
head.weight_of() + ty.weight_of()
}
}
impl WeightOf for Member {
fn weight_of(&self) -> usize {
let Self { head, kind } = self;
head.weight_of() + kind.weight_of() // accounting
}
}
impl WeightOf for MemberKind {
fn weight_of(&self) -> usize {
match self {
MemberKind::Call(_, tuple) => tuple.weight_of(),
MemberKind::Struct(_) => 0,
MemberKind::Tuple(literal) => literal.weight_of(),
}
}
}
impl WeightOf for Index {
fn weight_of(&self) -> usize {
let Self { head, indices } = self;
head.weight_of() + indices.weight_of()
}
}
impl WeightOf for Literal {
fn weight_of(&self) -> usize {
match self {
Literal::Bool(v) => v.weight_of(),
Literal::Char(v) => v.weight_of(),
Literal::Int(v) => v.weight_of(),
Literal::Float(v) => v.weight_of(),
Literal::String(v) => v.weight_of(),
}
}
}
impl WeightOf for Structor {
fn weight_of(&self) -> usize {
let Self { to, init } = self;
to.weight_of() + init.weight_of()
}
}
impl WeightOf for Fielder {
fn weight_of(&self) -> usize {
let Self { name, init } = self;
name.weight_of() + init.weight_of()
}
}
impl WeightOf for Array {
fn weight_of(&self) -> usize {
let Self { values } = self;
values.weight_of()
}
}
impl WeightOf for ArrayRep {
fn weight_of(&self) -> usize {
let Self { value, repeat } = self;
value.weight_of() + repeat.weight_of()
}
}
impl WeightOf for AddrOf {
fn weight_of(&self) -> usize {
let Self { mutable, expr } = self;
mutable.weight_of() + expr.weight_of()
}
}
impl WeightOf for Block {
fn weight_of(&self) -> usize {
let Self { stmts } = self;
stmts.weight_of()
}
}
impl WeightOf for Group {
fn weight_of(&self) -> usize {
let Self { expr } = self;
expr.weight_of()
}
}
impl WeightOf for Tuple {
fn weight_of(&self) -> usize {
let Self { exprs } = self;
exprs.weight_of()
}
}
impl WeightOf for While {
fn weight_of(&self) -> usize {
let Self { cond, pass, fail } = self;
cond.weight_of() + pass.weight_of() + fail.weight_of()
}
}
impl WeightOf for If {
fn weight_of(&self) -> usize {
let Self { cond, pass, fail } = self;
cond.weight_of() + pass.weight_of() + fail.weight_of()
}
}
impl WeightOf for For {
fn weight_of(&self) -> usize {
let Self { bind, cond, pass, fail } = self;
bind.weight_of() + cond.weight_of() + pass.weight_of() + fail.weight_of()
}
}
impl WeightOf for Else {
fn weight_of(&self) -> usize {
let Self { body } = self;
body.weight_of()
}
}
impl WeightOf for Break {
fn weight_of(&self) -> usize {
let Self { body } = self;
body.weight_of()
}
}
impl WeightOf for Return {
fn weight_of(&self) -> usize {
let Self { body } = self;
body.weight_of()
}
}
// ------------ SizeOf Blanket Implementations
impl<T: WeightOf> WeightOf for Option<T> {
fn weight_of(&self) -> usize {
match self {
Some(t) => t.weight_of().max(size_of_val(t)),
None => size_of_val(self),
}
}
}
impl<T: WeightOf> WeightOf for [T] {
fn weight_of(&self) -> usize {
self.iter().map(WeightOf::weight_of).sum()
}
}
impl<T: WeightOf> WeightOf for Vec<T> {
fn weight_of(&self) -> usize {
size_of::<Self>() + self.iter().map(WeightOf::weight_of).sum::<usize>()
}
}
impl<T: WeightOf> WeightOf for Box<T> {
fn weight_of(&self) -> usize {
(**self).weight_of() + size_of::<Self>()
}
}
impl WeightOf for str {
fn weight_of(&self) -> usize {
self.len()
}
}
impl_size_of! {
// primitives
u8, u16, u32, u64, u128, usize,
i8, i16, i32, i64, i128, isize,
f32, f64, bool, char,
// cl-structures
Span,
// cl-ast
Visibility, Mutability, Semi, ModifyKind, BinaryKind, UnaryKind
}
impl<T> WeightOf for Interned<'_, T> {
fn weight_of(&self) -> usize {
size_of_val(self) // interned values are opaque to SizeOF
}
}
macro impl_size_of($($T:ty),*$(,)?) {
$(impl WeightOf for $T {
fn weight_of(&self) -> usize {
::std::mem::size_of_val(self)
}
})*
}

View File

@ -2,11 +2,7 @@
//! with default implementations across the entire AST
pub mod fold;
pub mod visit;
pub mod walk;
pub use fold::Fold;
pub use visit::Visit;
pub use walk::Walk;

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, span: Span) -> Span {
span
fn fold_span(&mut self, extents: Span) -> Span {
extents
}
fn fold_mutability(&mut self, mutability: Mutability) -> Mutability {
mutability
@ -44,8 +44,8 @@ pub trait Fold {
s
}
fn fold_file(&mut self, f: File) -> File {
let File { name, items } = f;
File { name, items: items.into_iter().map(|i| self.fold_item(i)).collect() }
let File { items } = f;
File { items: items.into_iter().map(|i| self.fold_item(i)).collect() }
}
fn fold_attrs(&mut self, a: Attrs) -> Attrs {
let Attrs { meta } = a;
@ -59,9 +59,9 @@ pub trait Fold {
or_fold_meta_kind(self, kind)
}
fn fold_item(&mut self, i: Item) -> Item {
let Item { span, attrs, vis, kind } = i;
let Item { extents, attrs, vis, kind } = i;
Item {
span: self.fold_span(span),
extents: self.fold_span(extents),
attrs: self.fold_attrs(attrs),
vis: self.fold_visibility(vis),
kind: self.fold_item_kind(kind),
@ -70,13 +70,9 @@ pub trait Fold {
fn fold_item_kind(&mut self, kind: ItemKind) -> ItemKind {
or_fold_item_kind(self, kind)
}
fn fold_generics(&mut self, gens: Generics) -> Generics {
let Generics { vars } = gens;
Generics { vars: vars.into_iter().map(|sym| self.fold_sym(sym)).collect() }
}
fn fold_alias(&mut self, a: Alias) -> Alias {
let Alias { name, from } = a;
Alias { name: self.fold_sym(name), from: from.map(|from| Box::new(self.fold_ty(*from))) }
let Alias { to, from } = a;
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;
@ -96,26 +92,31 @@ pub trait Fold {
}
}
fn fold_module(&mut self, m: Module) -> Module {
let Module { name, file } = m;
Module { name: self.fold_sym(name), file: file.map(|v| self.fold_file(v)) }
let Module { name, kind } = m;
Module { name: self.fold_sym(name), kind: self.fold_module_kind(kind) }
}
fn fold_module_kind(&mut self, m: ModuleKind) -> ModuleKind {
match m {
ModuleKind::Inline(f) => ModuleKind::Inline(self.fold_file(f)),
ModuleKind::Outline => ModuleKind::Outline,
}
}
fn fold_function(&mut self, f: Function) -> Function {
let Function { name, gens, sign, bind, body } = f;
let Function { name, sign, bind, body } = f;
Function {
name: self.fold_sym(name),
gens: self.fold_generics(gens),
sign: self.fold_ty_fn(sign),
bind: self.fold_pattern(bind),
body: body.map(|b| self.fold_expr(b)),
bind: bind.into_iter().map(|p| self.fold_param(p)).collect(),
body: body.map(|b| self.fold_block(b)),
}
}
fn fold_param(&mut self, p: Param) -> Param {
let Param { mutability, name } = p;
Param { mutability: self.fold_mutability(mutability), name: self.fold_sym(name) }
}
fn fold_struct(&mut self, s: Struct) -> Struct {
let Struct { name, gens, kind } = s;
Struct {
name: self.fold_sym(name),
gens: self.fold_generics(gens),
kind: self.fold_struct_kind(kind),
}
let Struct { name, kind } = s;
Struct { name: self.fold_sym(name), kind: self.fold_struct_kind(kind) }
}
fn fold_struct_kind(&mut self, kind: StructKind) -> StructKind {
match kind {
@ -139,29 +140,23 @@ pub trait Fold {
}
}
fn fold_enum(&mut self, e: Enum) -> Enum {
let Enum { name, gens, variants: kind } = e;
Enum {
name: self.fold_sym(name),
gens: self.fold_generics(gens),
variants: kind.into_iter().map(|v| self.fold_variant(v)).collect(),
}
let Enum { name, kind } = e;
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)
}
fn fold_variant(&mut self, v: Variant) -> Variant {
let Variant { name, kind, body } = v;
let Variant { name, kind } = v;
Variant {
name: self.fold_sym(name),
kind: self.fold_struct_kind(kind),
body: body.map(|e| Box::new(self.fold_expr(*e))),
}
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)
}
fn fold_impl(&mut self, i: Impl) -> Impl {
let Impl { gens, target, body } = i;
Impl {
gens: self.fold_generics(gens),
target: self.fold_impl_kind(target),
body: self.fold_file(body),
}
let Impl { target, body } = i;
Impl { target: self.fold_impl_kind(target), body: self.fold_file(body) }
}
fn fold_impl_kind(&mut self, kind: ImplKind) -> ImplKind {
or_fold_impl_kind(self, kind)
@ -174,39 +169,39 @@ pub trait Fold {
or_fold_use_tree(self, tree)
}
fn fold_ty(&mut self, t: Ty) -> Ty {
let Ty { span, kind, gens } = t;
Ty {
span: self.fold_span(span),
kind: self.fold_ty_kind(kind),
gens: self.fold_generics(gens),
}
let Ty { extents, kind } = t;
Ty { extents: self.fold_span(extents), kind: self.fold_ty_kind(kind) }
}
fn fold_ty_kind(&mut self, kind: TyKind) -> TyKind {
or_fold_ty_kind(self, kind)
}
fn fold_ty_array(&mut self, a: TyArray) -> TyArray {
let TyArray { ty, count } = a;
TyArray { ty: Box::new(self.fold_ty(*ty)), count }
TyArray { ty: Box::new(self.fold_ty_kind(*ty)), count }
}
fn fold_ty_slice(&mut self, s: TySlice) -> TySlice {
let TySlice { ty } = s;
TySlice { ty: Box::new(self.fold_ty(*ty)) }
TySlice { ty: Box::new(self.fold_ty_kind(*ty)) }
}
fn fold_ty_tuple(&mut self, t: TyTuple) -> TyTuple {
let TyTuple { types } = t;
TyTuple { types: types.into_iter().map(|kind| self.fold_ty(kind)).collect() }
TyTuple {
types: types
.into_iter()
.map(|kind| self.fold_ty_kind(kind))
.collect(),
}
}
fn fold_ty_ref(&mut self, t: TyRef) -> TyRef {
let TyRef { mutable, count, to } = t;
TyRef { mutable: self.fold_mutability(mutable), count, to: Box::new(self.fold_ty(*to)) }
}
fn fold_ty_ptr(&mut self, t: TyPtr) -> TyPtr {
let TyPtr { to } = t;
TyPtr { to: Box::new(self.fold_ty(*to)) }
TyRef { mutable: self.fold_mutability(mutable), count, to: self.fold_path(to) }
}
fn fold_ty_fn(&mut self, t: TyFn) -> TyFn {
let TyFn { args, rety } = t;
TyFn { args: Box::new(self.fold_ty(*args)), rety: Box::new(self.fold_ty(*rety)) }
TyFn {
args: Box::new(self.fold_ty_kind(*args)),
rety: rety.map(|t| Box::new(self.fold_ty(*t))),
}
}
fn fold_path(&mut self, p: Path) -> Path {
let Path { absolute, parts } = p;
@ -215,14 +210,15 @@ pub trait Fold {
fn fold_path_part(&mut self, p: PathPart) -> PathPart {
match p {
PathPart::SuperKw => PathPart::SuperKw,
PathPart::SelfKw => PathPart::SelfKw,
PathPart::SelfTy => PathPart::SelfTy,
PathPart::Ident(i) => PathPart::Ident(self.fold_sym(i)),
}
}
fn fold_stmt(&mut self, s: Stmt) -> Stmt {
let Stmt { span, kind, semi } = s;
let Stmt { extents, kind, semi } = s;
Stmt {
span: self.fold_span(span),
extents: self.fold_span(extents),
kind: self.fold_stmt_kind(kind),
semi: self.fold_semi(semi),
}
@ -234,16 +230,12 @@ pub trait Fold {
s
}
fn fold_expr(&mut self, e: Expr) -> Expr {
let Expr { span, kind } = e;
Expr { span: self.fold_span(span), kind: self.fold_expr_kind(kind) }
let Expr { extents, kind } = e;
Expr { extents: self.fold_span(extents), kind: self.fold_expr_kind(kind) }
}
fn fold_expr_kind(&mut self, kind: ExprKind) -> ExprKind {
or_fold_expr_kind(self, kind)
}
fn fold_closure(&mut self, value: Closure) -> Closure {
let Closure { arg, body } = value;
Closure { arg: Box::new(self.fold_pattern(*arg)), body: Box::new(self.fold_expr(*body)) }
}
fn fold_let(&mut self, l: Let) -> Let {
let Let { mutable, name, ty, init } = l;
Let {
@ -256,23 +248,12 @@ pub trait Fold {
fn fold_pattern(&mut self, p: Pattern) -> Pattern {
match p {
Pattern::Name(sym) => Pattern::Name(self.fold_sym(sym)),
Pattern::Path(path) => Pattern::Path(self.fold_path(path)),
Pattern::Literal(literal) => Pattern::Literal(self.fold_literal(literal)),
Pattern::Rest(Some(name)) => Pattern::Rest(Some(self.fold_pattern(*name).into())),
Pattern::Rest(None) => Pattern::Rest(None),
Pattern::Ref(mutability, pattern) => Pattern::Ref(
self.fold_mutability(mutability),
Box::new(self.fold_pattern(*pattern)),
),
Pattern::RangeExc(head, tail) => Pattern::RangeInc(
Box::new(self.fold_pattern(*head)),
Box::new(self.fold_pattern(*tail)),
),
Pattern::RangeInc(head, tail) => Pattern::RangeInc(
Box::new(self.fold_pattern(*head)),
Box::new(self.fold_pattern(*tail)),
),
Pattern::Tuple(patterns) => {
Pattern::Tuple(patterns.into_iter().map(|p| self.fold_pattern(p)).collect())
}
@ -286,13 +267,6 @@ pub trait Fold {
.map(|(name, bind)| (name, bind.map(|p| self.fold_pattern(p))))
.collect(),
),
Pattern::TupleStruct(path, items) => Pattern::TupleStruct(
self.fold_path(path),
items
.into_iter()
.map(|bind| self.fold_pattern(bind))
.collect(),
),
}
}
@ -311,18 +285,18 @@ pub trait Fold {
let MatchArm(pat, expr) = a;
MatchArm(self.fold_pattern(pat), self.fold_expr(expr))
}
fn fold_assign(&mut self, a: Assign) -> Assign {
let Assign { parts } = a;
let (head, tail) = *parts;
Assign { parts: Box::new((self.fold_expr(head), self.fold_expr(tail))) }
Assign { parts: Box::new((self.fold_expr_kind(head), self.fold_expr_kind(tail))) }
}
fn fold_modify(&mut self, m: Modify) -> Modify {
let Modify { kind, parts } = m;
let (head, tail) = *parts;
Modify {
kind: self.fold_modify_kind(kind),
parts: Box::new((self.fold_expr(head), self.fold_expr(tail))),
parts: Box::new((self.fold_expr_kind(head), self.fold_expr_kind(tail))),
}
}
fn fold_modify_kind(&mut self, kind: ModifyKind) -> ModifyKind {
@ -333,7 +307,7 @@ pub trait Fold {
let (head, tail) = *parts;
Binary {
kind: self.fold_binary_kind(kind),
parts: Box::new((self.fold_expr(head), self.fold_expr(tail))),
parts: Box::new((self.fold_expr_kind(head), self.fold_expr_kind(tail))),
}
}
fn fold_binary_kind(&mut self, kind: BinaryKind) -> BinaryKind {
@ -341,18 +315,18 @@ pub trait Fold {
}
fn fold_unary(&mut self, u: Unary) -> Unary {
let Unary { kind, tail } = u;
Unary { kind: self.fold_unary_kind(kind), tail: Box::new(self.fold_expr(*tail)) }
Unary { kind: self.fold_unary_kind(kind), tail: Box::new(self.fold_expr_kind(*tail)) }
}
fn fold_unary_kind(&mut self, kind: UnaryKind) -> UnaryKind {
kind
}
fn fold_cast(&mut self, cast: Cast) -> Cast {
let Cast { head, ty } = cast;
Cast { head: Box::new(self.fold_expr(*head)), ty: self.fold_ty(ty) }
Cast { head: Box::new(self.fold_expr_kind(*head)), ty: self.fold_ty(ty) }
}
fn fold_member(&mut self, m: Member) -> Member {
let Member { head, kind } = m;
Member { head: Box::new(self.fold_expr(*head)), kind: self.fold_member_kind(kind) }
Member { head: Box::new(self.fold_expr_kind(*head)), kind: self.fold_member_kind(kind) }
}
fn fold_member_kind(&mut self, kind: MemberKind) -> MemberKind {
or_fold_member_kind(self, kind)
@ -360,7 +334,7 @@ pub trait Fold {
fn fold_index(&mut self, i: Index) -> Index {
let Index { head, indices } = i;
Index {
head: Box::new(self.fold_expr(*head)),
head: Box::new(self.fold_expr_kind(*head)),
indices: indices.into_iter().map(|e| self.fold_expr(e)).collect(),
}
}
@ -383,11 +357,17 @@ pub trait Fold {
}
fn fold_array_rep(&mut self, a: ArrayRep) -> ArrayRep {
let ArrayRep { value, repeat } = a;
ArrayRep { value: Box::new(self.fold_expr(*value)), repeat }
ArrayRep {
value: Box::new(self.fold_expr_kind(*value)),
repeat: Box::new(self.fold_expr_kind(*repeat)),
}
}
fn fold_addrof(&mut self, a: AddrOf) -> AddrOf {
let AddrOf { mutable, expr } = a;
AddrOf { mutable: self.fold_mutability(mutable), expr: Box::new(self.fold_expr(*expr)) }
AddrOf {
mutable: self.fold_mutability(mutable),
expr: Box::new(self.fold_expr_kind(*expr)),
}
}
fn fold_block(&mut self, b: Block) -> Block {
let Block { stmts } = b;
@ -395,7 +375,7 @@ pub trait Fold {
}
fn fold_group(&mut self, g: Group) -> Group {
let Group { expr } = g;
Group { expr: Box::new(self.fold_expr(*expr)) }
Group { expr: Box::new(self.fold_expr_kind(*expr)) }
}
fn fold_tuple(&mut self, t: Tuple) -> Tuple {
let Tuple { exprs } = t;
@ -420,7 +400,7 @@ pub trait Fold {
fn fold_for(&mut self, f: For) -> For {
let For { bind, cond, pass, fail } = f;
For {
bind: self.fold_pattern(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),
@ -480,6 +460,15 @@ pub fn or_fold_item_kind<F: Fold + ?Sized>(folder: &mut F, kind: ItemKind) -> It
}
}
#[inline]
/// Folds a [ModuleKind] in the default way
pub fn or_fold_module_kind<F: Fold + ?Sized>(folder: &mut F, kind: ModuleKind) -> ModuleKind {
match kind {
ModuleKind::Inline(f) => ModuleKind::Inline(folder.fold_file(f)),
ModuleKind::Outline => ModuleKind::Outline,
}
}
#[inline]
/// Folds a [StructKind] in the default way
pub fn or_fold_struct_kind<F: Fold + ?Sized>(folder: &mut F, kind: StructKind) -> StructKind {
@ -496,6 +485,32 @@ pub fn or_fold_struct_kind<F: Fold + ?Sized>(folder: &mut F, kind: StructKind) -
}
}
#[inline]
/// Folds an [EnumKind] in the default way
pub fn or_fold_enum_kind<F: Fold + ?Sized>(folder: &mut F, kind: EnumKind) -> EnumKind {
match kind {
EnumKind::NoVariants => EnumKind::NoVariants,
EnumKind::Variants(v) => {
EnumKind::Variants(v.into_iter().map(|v| folder.fold_variant(v)).collect())
}
}
}
#[inline]
/// Folds a [VariantKind] in the default way
pub fn or_fold_variant_kind<F: Fold + ?Sized>(folder: &mut F, kind: VariantKind) -> VariantKind {
match kind {
VariantKind::Plain => VariantKind::Plain,
VariantKind::CLike(n) => VariantKind::CLike(n),
VariantKind::Tuple(t) => VariantKind::Tuple(folder.fold_ty(t)),
VariantKind::Struct(mem) => VariantKind::Struct(
mem.into_iter()
.map(|m| folder.fold_struct_member(m))
.collect(),
),
}
}
#[inline]
/// Folds an [ImplKind] in the default way
pub fn or_fold_impl_kind<F: Fold + ?Sized>(folder: &mut F, kind: ImplKind) -> ImplKind {
@ -532,13 +547,11 @@ pub fn or_fold_ty_kind<F: Fold + ?Sized>(folder: &mut F, kind: TyKind) -> TyKind
match kind {
TyKind::Never => TyKind::Never,
TyKind::Empty => TyKind::Empty,
TyKind::Infer => TyKind::Infer,
TyKind::Path(p) => TyKind::Path(folder.fold_path(p)),
TyKind::Array(a) => TyKind::Array(folder.fold_ty_array(a)),
TyKind::Slice(s) => TyKind::Slice(folder.fold_ty_slice(s)),
TyKind::Tuple(t) => TyKind::Tuple(folder.fold_ty_tuple(t)),
TyKind::Ref(t) => TyKind::Ref(folder.fold_ty_ref(t)),
TyKind::Ptr(t) => TyKind::Ptr(folder.fold_ty_ptr(t)),
TyKind::Fn(t) => TyKind::Fn(folder.fold_ty_fn(t)),
}
}
@ -557,7 +570,6 @@ pub fn or_fold_stmt_kind<F: Fold + ?Sized>(folder: &mut F, kind: StmtKind) -> St
pub fn or_fold_expr_kind<F: Fold + ?Sized>(folder: &mut F, kind: ExprKind) -> ExprKind {
match kind {
ExprKind::Empty => ExprKind::Empty,
ExprKind::Closure(c) => ExprKind::Closure(folder.fold_closure(c)),
ExprKind::Quote(q) => ExprKind::Quote(q), // quoted expressions are left unmodified
ExprKind::Let(l) => ExprKind::Let(folder.fold_let(l)),
ExprKind::Match(m) => ExprKind::Match(folder.fold_match(m)),

View File

@ -3,258 +3,527 @@
use crate::ast::*;
use cl_structures::span::Span;
use super::walk::Walk;
/// Immutably walks the entire AST
///
/// Each method acts as a customization point.
///
/// There are a set of default implementations for enums
/// under the name [`or_visit_`*](or_visit_expr_kind),
/// provided for ease of use.
///
/// For all other nodes, traversal is *explicit*.
pub trait Visit<'a>: Sized {
/// Visits a [Walker](Walk)
#[inline]
fn visit<W: Walk>(&mut self, walker: &'a W) -> &mut Self {
walker.visit_in(self);
self
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_sym(&mut self, _name: &'a Sym) {}
fn visit_literal(&mut self, l: &'a Literal) {
or_visit_literal(self, l)
}
/// Visits the children of a [Walker](Walk)
fn visit_children<W: Walk>(&mut self, walker: &'a W) {
walker.children(self)
fn visit_bool(&mut self, _b: &'a bool) {}
fn visit_char(&mut self, _c: &'a char) {}
fn visit_int(&mut self, _i: &'a u128) {}
fn visit_smuggled_float(&mut self, _f: &'a u64) {}
fn visit_string(&mut self, _s: &'a str) {}
fn visit_file(&mut self, f: &'a File) {
let File { items } = f;
items.iter().for_each(|i| self.visit_item(i));
}
fn visit_attrs(&mut self, a: &'a Attrs) {
let Attrs { meta } = a;
meta.iter().for_each(|m| self.visit_meta(m));
}
fn visit_meta(&mut self, m: &'a Meta) {
let Meta { name, kind } = m;
self.visit_sym(name);
self.visit_meta_kind(kind);
}
fn visit_meta_kind(&mut self, kind: &'a MetaKind) {
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);
self.visit_attrs(attrs);
self.visit_visibility(vis);
self.visit_item_kind(kind);
}
fn visit_item_kind(&mut self, kind: &'a ItemKind) {
or_visit_item_kind(self, kind)
}
fn visit_alias(&mut self, a: &'a Alias) {
let Alias { to, from } = a;
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_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_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_sym(name);
self.visit_module_kind(kind);
}
fn visit_module_kind(&mut self, kind: &'a ModuleKind) {
or_visit_module_kind(self, kind)
}
fn visit_function(&mut self, f: &'a Function) {
let Function { name, sign, bind, body } = f;
self.visit_sym(name);
self.visit_ty_fn(sign);
bind.iter().for_each(|p| self.visit_param(p));
if let Some(b) = body {
self.visit_block(b)
}
}
fn visit_param(&mut self, p: &'a Param) {
let Param { mutability, name } = p;
self.visit_mutability(mutability);
self.visit_sym(name);
}
fn visit_struct(&mut self, s: &'a Struct) {
let Struct { name, kind } = s;
self.visit_sym(name);
self.visit_struct_kind(kind);
}
fn visit_struct_kind(&mut self, kind: &'a StructKind) {
or_visit_struct_kind(self, kind)
}
fn visit_struct_member(&mut self, m: &'a StructMember) {
let StructMember { vis, name, ty } = m;
self.visit_visibility(vis);
self.visit_sym(name);
self.visit_ty(ty);
}
fn visit_enum(&mut self, e: &'a Enum) {
let Enum { name, kind } = e;
self.visit_sym(name);
self.visit_enum_kind(kind);
}
fn visit_enum_kind(&mut self, kind: &'a EnumKind) {
or_visit_enum_kind(self, kind)
}
fn visit_variant(&mut self, v: &'a Variant) {
let Variant { name, kind } = v;
self.visit_sym(name);
self.visit_variant_kind(kind);
}
fn visit_variant_kind(&mut self, kind: &'a VariantKind) {
or_visit_variant_kind(self, kind)
}
fn visit_impl(&mut self, i: &'a Impl) {
let Impl { target, body } = i;
self.visit_impl_kind(target);
self.visit_file(body);
}
fn visit_impl_kind(&mut self, target: &'a ImplKind) {
or_visit_impl_kind(self, target)
}
fn visit_use(&mut self, u: &'a Use) {
let Use { absolute: _, tree } = u;
self.visit_use_tree(tree);
}
fn visit_use_tree(&mut self, tree: &'a UseTree) {
or_visit_use_tree(self, tree)
}
fn visit_ty(&mut self, t: &'a Ty) {
let Ty { extents, kind } = t;
self.visit_span(extents);
self.visit_ty_kind(kind);
}
fn visit_ty_kind(&mut self, kind: &'a TyKind) {
or_visit_ty_kind(self, kind)
}
fn visit_ty_array(&mut self, a: &'a TyArray) {
let TyArray { ty, count: _ } = a;
self.visit_ty_kind(ty);
}
fn visit_ty_slice(&mut self, s: &'a TySlice) {
let TySlice { ty } = s;
self.visit_ty_kind(ty)
}
fn visit_ty_tuple(&mut self, t: &'a TyTuple) {
let TyTuple { types } = t;
types.iter().for_each(|kind| self.visit_ty_kind(kind))
}
fn visit_ty_ref(&mut self, t: &'a TyRef) {
let TyRef { mutable, count: _, to } = t;
self.visit_mutability(mutable);
self.visit_path(to);
}
fn visit_ty_fn(&mut self, t: &'a TyFn) {
let TyFn { args, rety } = t;
self.visit_ty_kind(args);
if let Some(rety) = rety {
self.visit_ty(rety);
}
}
fn visit_path(&mut self, p: &'a Path) {
let Path { absolute: _, parts } = p;
parts.iter().for_each(|p| self.visit_path_part(p))
}
fn visit_path_part(&mut self, p: &'a PathPart) {
match p {
PathPart::SuperKw => {}
PathPart::SelfKw => {}
PathPart::SelfTy => {}
PathPart::Ident(i) => self.visit_sym(i),
}
}
fn visit_stmt(&mut self, s: &'a Stmt) {
let Stmt { extents, kind, semi } = s;
self.visit_span(extents);
self.visit_stmt_kind(kind);
self.visit_semi(semi);
}
fn visit_stmt_kind(&mut self, kind: &'a StmtKind) {
or_visit_stmt_kind(self, kind)
}
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);
self.visit_expr_kind(kind)
}
fn visit_expr_kind(&mut self, e: &'a ExprKind) {
or_visit_expr_kind(self, e)
}
fn visit_let(&mut self, l: &'a Let) {
let Let { mutable, name, ty, init } = l;
self.visit_mutability(mutable);
self.visit_pattern(name);
if let Some(ty) = ty {
self.visit_ty(ty);
}
if let Some(init) = init {
self.visit_expr(init)
}
}
fn visit_span(&mut self, value: &'a Span) {
value.children(self)
}
fn visit_mutability(&mut self, value: &'a Mutability) {
value.children(self)
}
fn visit_visibility(&mut self, value: &'a Visibility) {
value.children(self)
}
fn visit_sym(&mut self, value: &'a Sym) {
value.children(self)
}
fn visit_literal(&mut self, value: &'a Literal) {
value.children(self)
}
fn visit_bool(&mut self, value: &'a bool) {
value.children(self)
}
fn visit_char(&mut self, value: &'a char) {
value.children(self)
}
fn visit_int(&mut self, value: &'a u128) {
value.children(self)
}
fn visit_smuggled_float(&mut self, value: &'a u64) {
value.children(self)
}
fn visit_string(&mut self, value: &'a str) {
value.children(self)
}
fn visit_file(&mut self, value: &'a File) {
value.children(self)
}
fn visit_attrs(&mut self, value: &'a Attrs) {
value.children(self)
}
fn visit_meta(&mut self, value: &'a Meta) {
value.children(self)
}
fn visit_meta_kind(&mut self, value: &'a MetaKind) {
value.children(self)
}
fn visit_item(&mut self, value: &'a Item) {
value.children(self)
}
fn visit_item_kind(&mut self, value: &'a ItemKind) {
value.children(self)
}
fn visit_generics(&mut self, value: &'a Generics) {
value.children(self)
}
fn visit_alias(&mut self, value: &'a Alias) {
value.children(self)
}
fn visit_const(&mut self, value: &'a Const) {
value.children(self)
}
fn visit_static(&mut self, value: &'a Static) {
value.children(self)
}
fn visit_module(&mut self, value: &'a Module) {
value.children(self)
}
fn visit_function(&mut self, value: &'a Function) {
value.children(self)
}
fn visit_struct(&mut self, value: &'a Struct) {
value.children(self)
}
fn visit_struct_kind(&mut self, value: &'a StructKind) {
value.children(self)
}
fn visit_struct_member(&mut self, value: &'a StructMember) {
value.children(self)
}
fn visit_enum(&mut self, value: &'a Enum) {
value.children(self)
}
fn visit_variant(&mut self, value: &'a Variant) {
value.children(self)
}
fn visit_impl(&mut self, value: &'a Impl) {
value.children(self)
}
fn visit_impl_kind(&mut self, value: &'a ImplKind) {
value.children(self)
}
fn visit_use(&mut self, value: &'a Use) {
value.children(self)
}
fn visit_use_tree(&mut self, value: &'a UseTree) {
value.children(self)
}
fn visit_ty(&mut self, value: &'a Ty) {
value.children(self)
}
fn visit_ty_kind(&mut self, value: &'a TyKind) {
value.children(self)
}
fn visit_ty_array(&mut self, value: &'a TyArray) {
value.children(self)
}
fn visit_ty_slice(&mut self, value: &'a TySlice) {
value.children(self)
}
fn visit_ty_tuple(&mut self, value: &'a TyTuple) {
value.children(self)
}
fn visit_ty_ref(&mut self, value: &'a TyRef) {
value.children(self)
}
fn visit_ty_ptr(&mut self, value: &'a TyPtr) {
value.children(self)
}
fn visit_ty_fn(&mut self, value: &'a TyFn) {
value.children(self)
}
fn visit_path(&mut self, value: &'a Path) {
value.children(self)
}
fn visit_path_part(&mut self, value: &'a PathPart) {
value.children(self)
}
fn visit_stmt(&mut self, value: &'a Stmt) {
value.children(self)
}
fn visit_stmt_kind(&mut self, value: &'a StmtKind) {
value.children(self)
}
fn visit_semi(&mut self, value: &'a Semi) {
value.children(self)
}
fn visit_expr(&mut self, value: &'a Expr) {
value.children(self)
}
fn visit_expr_kind(&mut self, value: &'a ExprKind) {
value.children(self)
}
fn visit_closure(&mut self, value: &'a Closure) {
value.children(self)
}
fn visit_quote(&mut self, value: &'a Quote) {
value.children(self)
}
fn visit_let(&mut self, value: &'a Let) {
value.children(self)
fn visit_pattern(&mut self, p: &'a Pattern) {
match p {
Pattern::Path(path) => self.visit_path(path),
Pattern::Literal(literal) => self.visit_literal(literal),
Pattern::Ref(mutability, pattern) => {
self.visit_mutability(mutability);
self.visit_pattern(pattern);
}
Pattern::Tuple(patterns) => {
patterns.iter().for_each(|p| self.visit_pattern(p));
}
Pattern::Array(patterns) => {
patterns.iter().for_each(|p| self.visit_pattern(p));
}
Pattern::Struct(path, items) => {
self.visit_path(path);
items.iter().for_each(|(_name, bind)| {
bind.as_ref().inspect(|bind| {
self.visit_pattern(bind);
});
});
}
}
}
fn visit_pattern(&mut self, value: &'a Pattern) {
value.children(self)
fn visit_match(&mut self, m: &'a Match) {
let Match { scrutinee, arms } = m;
self.visit_expr(scrutinee);
arms.iter().for_each(|arm| self.visit_match_arm(arm));
}
fn visit_match(&mut self, value: &'a Match) {
value.children(self)
fn visit_match_arm(&mut self, a: &'a MatchArm) {
let MatchArm(pat, expr) = a;
self.visit_pattern(pat);
self.visit_expr(expr);
}
fn visit_match_arm(&mut self, value: &'a MatchArm) {
value.children(self)
fn visit_assign(&mut self, a: &'a Assign) {
let Assign { parts } = a;
let (head, tail) = parts.as_ref();
self.visit_expr_kind(head);
self.visit_expr_kind(tail);
}
fn visit_assign(&mut self, value: &'a Assign) {
value.children(self)
fn visit_modify(&mut self, m: &'a Modify) {
let Modify { kind, parts } = m;
let (head, tail) = parts.as_ref();
self.visit_modify_kind(kind);
self.visit_expr_kind(head);
self.visit_expr_kind(tail);
}
fn visit_modify(&mut self, value: &'a Modify) {
value.children(self)
fn visit_modify_kind(&mut self, _kind: &'a ModifyKind) {}
fn visit_binary(&mut self, b: &'a Binary) {
let Binary { kind, parts } = b;
let (head, tail) = parts.as_ref();
self.visit_binary_kind(kind);
self.visit_expr_kind(head);
self.visit_expr_kind(tail);
}
fn visit_modify_kind(&mut self, value: &'a ModifyKind) {
value.children(self)
fn visit_binary_kind(&mut self, _kind: &'a BinaryKind) {}
fn visit_unary(&mut self, u: &'a Unary) {
let Unary { kind, tail } = u;
self.visit_unary_kind(kind);
self.visit_expr_kind(tail);
}
fn visit_binary(&mut self, value: &'a Binary) {
value.children(self)
fn visit_unary_kind(&mut self, _kind: &'a UnaryKind) {}
fn visit_cast(&mut self, cast: &'a Cast) {
let Cast { head, ty } = cast;
self.visit_expr_kind(head);
self.visit_ty(ty);
}
fn visit_binary_kind(&mut self, value: &'a BinaryKind) {
value.children(self)
fn visit_member(&mut self, m: &'a Member) {
let Member { head, kind } = m;
self.visit_expr_kind(head);
self.visit_member_kind(kind);
}
fn visit_unary(&mut self, value: &'a Unary) {
value.children(self)
fn visit_member_kind(&mut self, kind: &'a MemberKind) {
or_visit_member_kind(self, kind)
}
fn visit_unary_kind(&mut self, value: &'a UnaryKind) {
value.children(self)
fn visit_index(&mut self, i: &'a Index) {
let Index { head, indices } = i;
self.visit_expr_kind(head);
indices.iter().for_each(|e| self.visit_expr(e));
}
fn visit_cast(&mut self, value: &'a Cast) {
value.children(self)
fn visit_structor(&mut self, s: &'a Structor) {
let Structor { to, init } = s;
self.visit_path(to);
init.iter().for_each(|e| self.visit_fielder(e))
}
fn visit_member(&mut self, value: &'a Member) {
value.children(self)
fn visit_fielder(&mut self, f: &'a Fielder) {
let Fielder { name, init } = f;
self.visit_sym(name);
if let Some(init) = init {
self.visit_expr(init);
}
}
fn visit_member_kind(&mut self, value: &'a MemberKind) {
value.children(self)
fn visit_array(&mut self, a: &'a Array) {
let Array { values } = a;
values.iter().for_each(|e| self.visit_expr(e))
}
fn visit_index(&mut self, value: &'a Index) {
value.children(self)
fn visit_array_rep(&mut self, a: &'a ArrayRep) {
let ArrayRep { value, repeat } = a;
self.visit_expr_kind(value);
self.visit_expr_kind(repeat);
}
fn visit_structor(&mut self, value: &'a Structor) {
value.children(self)
fn visit_addrof(&mut self, a: &'a AddrOf) {
let AddrOf { mutable, expr } = a;
self.visit_mutability(mutable);
self.visit_expr_kind(expr);
}
fn visit_fielder(&mut self, value: &'a Fielder) {
value.children(self)
fn visit_block(&mut self, b: &'a Block) {
let Block { stmts } = b;
stmts.iter().for_each(|s| self.visit_stmt(s));
}
fn visit_array(&mut self, value: &'a Array) {
value.children(self)
fn visit_group(&mut self, g: &'a Group) {
let Group { expr } = g;
self.visit_expr_kind(expr)
}
fn visit_array_rep(&mut self, value: &'a ArrayRep) {
value.children(self)
fn visit_tuple(&mut self, t: &'a Tuple) {
let Tuple { exprs } = t;
exprs.iter().for_each(|e| self.visit_expr(e))
}
fn visit_addrof(&mut self, value: &'a AddrOf) {
value.children(self)
fn visit_while(&mut self, w: &'a While) {
let While { cond, pass, fail } = w;
self.visit_expr(cond);
self.visit_block(pass);
self.visit_else(fail);
}
fn visit_block(&mut self, value: &'a Block) {
value.children(self)
fn visit_if(&mut self, i: &'a If) {
let If { cond, pass, fail } = i;
self.visit_expr(cond);
self.visit_block(pass);
self.visit_else(fail);
}
fn visit_group(&mut self, value: &'a Group) {
value.children(self)
fn visit_for(&mut self, f: &'a For) {
let For { bind, cond, pass, fail } = f;
self.visit_sym(bind);
self.visit_expr(cond);
self.visit_block(pass);
self.visit_else(fail);
}
fn visit_tuple(&mut self, value: &'a Tuple) {
value.children(self)
fn visit_else(&mut self, e: &'a Else) {
let Else { body } = e;
if let Some(body) = body {
self.visit_expr(body)
}
}
fn visit_while(&mut self, value: &'a While) {
value.children(self)
fn visit_break(&mut self, b: &'a Break) {
let Break { body } = b;
if let Some(body) = body {
self.visit_expr(body)
}
}
fn visit_if(&mut self, value: &'a If) {
value.children(self)
}
fn visit_for(&mut self, value: &'a For) {
value.children(self)
}
fn visit_else(&mut self, value: &'a Else) {
value.children(self)
}
fn visit_break(&mut self, value: &'a Break) {
value.children(self)
}
fn visit_return(&mut self, value: &'a Return) {
value.children(self)
fn visit_return(&mut self, r: &'a Return) {
let Return { body } = r;
if let Some(body) = body {
self.visit_expr(body)
}
}
fn visit_continue(&mut self) {}
}
pub fn or_visit_literal<'a, V: Visit<'a>>(visitor: &mut V, l: &'a Literal) {
match l {
Literal::Bool(b) => visitor.visit_bool(b),
Literal::Char(c) => visitor.visit_char(c),
Literal::Int(i) => visitor.visit_int(i),
Literal::Float(f) => visitor.visit_smuggled_float(f),
Literal::String(s) => visitor.visit_string(s),
}
}
pub fn or_visit_meta_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a MetaKind) {
match kind {
MetaKind::Plain => {}
MetaKind::Equals(l) => visitor.visit_literal(l),
MetaKind::Func(lits) => lits.iter().for_each(|l| visitor.visit_literal(l)),
}
}
pub fn or_visit_item_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a ItemKind) {
match kind {
ItemKind::Module(m) => visitor.visit_module(m),
ItemKind::Alias(a) => visitor.visit_alias(a),
ItemKind::Enum(e) => visitor.visit_enum(e),
ItemKind::Struct(s) => visitor.visit_struct(s),
ItemKind::Const(c) => visitor.visit_const(c),
ItemKind::Static(s) => visitor.visit_static(s),
ItemKind::Function(f) => visitor.visit_function(f),
ItemKind::Impl(i) => visitor.visit_impl(i),
ItemKind::Use(u) => visitor.visit_use(u),
}
}
pub fn or_visit_module_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a ModuleKind) {
match kind {
ModuleKind::Inline(f) => visitor.visit_file(f),
ModuleKind::Outline => {}
}
}
pub fn or_visit_struct_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a StructKind) {
match kind {
StructKind::Empty => {}
StructKind::Tuple(ty) => ty.iter().for_each(|t| visitor.visit_ty(t)),
StructKind::Struct(m) => m.iter().for_each(|m| visitor.visit_struct_member(m)),
}
}
pub fn or_visit_enum_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a EnumKind) {
match kind {
EnumKind::NoVariants => {}
EnumKind::Variants(variants) => variants.iter().for_each(|v| visitor.visit_variant(v)),
}
}
pub fn or_visit_variant_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a VariantKind) {
match kind {
VariantKind::Plain => {}
VariantKind::CLike(_) => {}
VariantKind::Tuple(t) => visitor.visit_ty(t),
VariantKind::Struct(m) => m.iter().for_each(|m| visitor.visit_struct_member(m)),
}
}
pub fn or_visit_impl_kind<'a, V: Visit<'a>>(visitor: &mut V, target: &'a ImplKind) {
match target {
ImplKind::Type(t) => visitor.visit_ty(t),
ImplKind::Trait { impl_trait, for_type } => {
visitor.visit_path(impl_trait);
visitor.visit_ty(for_type)
}
}
}
pub fn or_visit_use_tree<'a, V: Visit<'a>>(visitor: &mut V, tree: &'a UseTree) {
match tree {
UseTree::Tree(tree) => {
tree.iter().for_each(|tree| visitor.visit_use_tree(tree));
}
UseTree::Path(path, rest) => {
visitor.visit_path_part(path);
visitor.visit_use_tree(rest)
}
UseTree::Alias(path, name) => {
visitor.visit_sym(path);
visitor.visit_sym(name);
}
UseTree::Name(name) => {
visitor.visit_sym(name);
}
UseTree::Glob => {}
}
}
pub fn or_visit_ty_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a TyKind) {
match kind {
TyKind::Never => {}
TyKind::Empty => {}
TyKind::Path(p) => visitor.visit_path(p),
TyKind::Array(t) => visitor.visit_ty_array(t),
TyKind::Slice(t) => visitor.visit_ty_slice(t),
TyKind::Tuple(t) => visitor.visit_ty_tuple(t),
TyKind::Ref(t) => visitor.visit_ty_ref(t),
TyKind::Fn(t) => visitor.visit_ty_fn(t),
}
}
pub fn or_visit_stmt_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a StmtKind) {
match kind {
StmtKind::Empty => {}
StmtKind::Item(i) => visitor.visit_item(i),
StmtKind::Expr(e) => visitor.visit_expr(e),
}
}
pub fn or_visit_expr_kind<'a, V: Visit<'a>>(visitor: &mut V, e: &'a ExprKind) {
match e {
ExprKind::Empty => {}
ExprKind::Quote(_q) => {} // Quoted expressions are left unvisited
ExprKind::Let(l) => visitor.visit_let(l),
ExprKind::Match(m) => visitor.visit_match(m),
ExprKind::Assign(a) => visitor.visit_assign(a),
ExprKind::Modify(m) => visitor.visit_modify(m),
ExprKind::Binary(b) => visitor.visit_binary(b),
ExprKind::Unary(u) => visitor.visit_unary(u),
ExprKind::Cast(c) => visitor.visit_cast(c),
ExprKind::Member(m) => visitor.visit_member(m),
ExprKind::Index(i) => visitor.visit_index(i),
ExprKind::Structor(s) => visitor.visit_structor(s),
ExprKind::Path(p) => visitor.visit_path(p),
ExprKind::Literal(l) => visitor.visit_literal(l),
ExprKind::Array(a) => visitor.visit_array(a),
ExprKind::ArrayRep(a) => visitor.visit_array_rep(a),
ExprKind::AddrOf(a) => visitor.visit_addrof(a),
ExprKind::Block(b) => visitor.visit_block(b),
ExprKind::Group(g) => visitor.visit_group(g),
ExprKind::Tuple(t) => visitor.visit_tuple(t),
ExprKind::While(w) => visitor.visit_while(w),
ExprKind::If(i) => visitor.visit_if(i),
ExprKind::For(f) => visitor.visit_for(f),
ExprKind::Break(b) => visitor.visit_break(b),
ExprKind::Return(r) => visitor.visit_return(r),
ExprKind::Continue => visitor.visit_continue(),
}
}
pub fn or_visit_member_kind<'a, V: Visit<'a>>(visitor: &mut V, kind: &'a MemberKind) {
match kind {
MemberKind::Call(field, args) => {
visitor.visit_sym(field);
visitor.visit_tuple(args);
}
MemberKind::Struct(field) => visitor.visit_sym(field),
MemberKind::Tuple(field) => visitor.visit_literal(field),
}
}

View File

@ -1,964 +0,0 @@
//! Accepts an AST Visitor. Walks the AST, calling the visitor on each step.
use super::visit::Visit;
use crate::ast::*;
use cl_structures::span::Span;
/// Helps a [Visitor](Visit) walk through `Self`.
pub trait Walk {
/// Calls the respective `visit_*` function in V
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V);
#[allow(unused)]
/// Walks the children of self, visiting them in V
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {}
}
impl Walk for Span {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_span(self);
}
}
impl Walk for Sym {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_sym(self);
}
}
impl Walk for Mutability {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_mutability(self);
}
}
impl Walk for Visibility {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_visibility(self);
}
}
impl Walk for bool {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_bool(self);
}
}
impl Walk for char {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_char(self);
}
}
impl Walk for u128 {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_int(self);
}
}
impl Walk for u64 {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_smuggled_float(self);
}
}
impl Walk for str {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_string(self);
}
}
impl Walk for Literal {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_literal(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
match self {
Literal::Bool(value) => value.children(v),
Literal::Char(value) => value.children(v),
Literal::Int(value) => value.children(v),
Literal::Float(value) => value.children(v),
Literal::String(value) => value.children(v),
};
}
}
impl Walk for File {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_file(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let File { name: _, items } = self;
items.iter().for_each(|i| v.visit_item(i));
}
}
impl Walk for Attrs {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_attrs(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Attrs { meta } = self;
meta.children(v);
}
}
impl Walk for Meta {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_meta(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Meta { name, kind } = self;
name.visit_in(v);
kind.visit_in(v);
}
}
impl Walk for MetaKind {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_meta_kind(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
match self {
MetaKind::Plain => {}
MetaKind::Equals(lit) => lit.visit_in(v),
MetaKind::Func(lits) => lits.visit_in(v),
}
}
}
impl Walk for Item {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_item(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Item { span, attrs, vis, kind } = self;
span.visit_in(v);
attrs.visit_in(v);
vis.visit_in(v);
kind.visit_in(v);
}
}
impl Walk for ItemKind {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_item_kind(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
match self {
ItemKind::Module(value) => value.visit_in(v),
ItemKind::Alias(value) => value.visit_in(v),
ItemKind::Enum(value) => value.visit_in(v),
ItemKind::Struct(value) => value.visit_in(v),
ItemKind::Const(value) => value.visit_in(v),
ItemKind::Static(value) => value.visit_in(v),
ItemKind::Function(value) => value.visit_in(v),
ItemKind::Impl(value) => value.visit_in(v),
ItemKind::Use(value) => value.visit_in(v),
}
}
}
impl Walk for Generics {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_generics(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Self { vars } = self;
vars.visit_in(v);
}
}
impl Walk for Module {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_module(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Module { name, file } = self;
name.visit_in(v);
file.visit_in(v);
}
}
impl Walk for Alias {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_alias(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Alias { name, from } = self;
name.visit_in(v);
from.visit_in(v);
}
}
impl Walk for Const {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_const(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Const { name, ty, init } = self;
name.visit_in(v);
ty.visit_in(v);
init.visit_in(v);
}
}
impl Walk for Static {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_static(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Static { mutable, name, ty, init } = self;
mutable.visit_in(v);
name.visit_in(v);
ty.visit_in(v);
init.visit_in(v);
}
}
impl Walk for Function {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_function(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Function { name, gens, sign, bind, body } = self;
name.visit_in(v);
gens.visit_in(v);
sign.visit_in(v);
bind.visit_in(v);
body.visit_in(v);
}
}
impl Walk for Struct {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_struct(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Struct { name, gens, kind } = self;
name.visit_in(v);
gens.visit_in(v);
kind.visit_in(v);
}
}
impl Walk for StructKind {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_struct_kind(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
match self {
StructKind::Empty => {}
StructKind::Tuple(tys) => tys.visit_in(v),
StructKind::Struct(ms) => ms.visit_in(v),
}
}
}
impl Walk for StructMember {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_struct_member(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let StructMember { vis, name, ty } = self;
vis.visit_in(v);
name.visit_in(v);
ty.visit_in(v);
}
}
impl Walk for Enum {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_enum(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Enum { name, gens, variants } = self;
name.visit_in(v);
gens.visit_in(v);
variants.visit_in(v);
}
}
impl Walk for Variant {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_variant(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Variant { name, kind, body } = self;
name.visit_in(v);
kind.visit_in(v);
body.visit_in(v);
}
}
impl Walk for Impl {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_impl(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Impl { gens, target, body } = self;
gens.visit_in(v);
target.visit_in(v);
body.visit_in(v);
}
}
impl Walk for ImplKind {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_impl_kind(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
match self {
ImplKind::Type(t) => t.visit_in(v),
ImplKind::Trait { impl_trait, for_type } => {
impl_trait.visit_in(v);
for_type.visit_in(v);
}
}
}
}
impl Walk for Use {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_use(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Use { absolute: _, tree } = self;
tree.visit_in(v);
}
}
impl Walk for UseTree {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_use_tree(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
match self {
UseTree::Tree(tree) => tree.iter().for_each(|t| t.visit_in(v)),
UseTree::Path(part, tree) => {
part.visit_in(v);
tree.visit_in(v);
}
UseTree::Alias(from, to) => {
from.visit_in(v);
to.visit_in(v);
}
UseTree::Name(name) => name.visit_in(v),
UseTree::Glob => {}
}
}
}
impl Walk for Ty {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_ty(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Ty { span, kind, gens } = self;
span.visit_in(v);
kind.visit_in(v);
gens.visit_in(v);
}
}
impl Walk for TyKind {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_ty_kind(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
match self {
TyKind::Never => {}
TyKind::Empty => {}
TyKind::Infer => {}
TyKind::Path(value) => value.visit_in(v),
TyKind::Array(value) => value.visit_in(v),
TyKind::Slice(value) => value.visit_in(v),
TyKind::Tuple(value) => value.visit_in(v),
TyKind::Ref(value) => value.visit_in(v),
TyKind::Ptr(value) => value.visit_in(v),
TyKind::Fn(value) => value.visit_in(v),
}
}
}
impl Walk for TyArray {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_ty_array(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let TyArray { ty, count: _ } = self;
ty.visit_in(v);
// count.walk(v); // not available
}
}
impl Walk for TySlice {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_ty_slice(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let TySlice { ty } = self;
ty.visit_in(v);
}
}
impl Walk for TyTuple {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_ty_tuple(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let TyTuple { types } = self;
types.visit_in(v);
}
}
impl Walk for TyRef {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_ty_ref(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let TyRef { mutable, count: _, to } = self;
mutable.children(v);
to.children(v);
}
}
impl Walk for TyPtr {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_ty_ptr(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let TyPtr { to } = self;
to.children(v);
}
}
impl Walk for TyFn {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_ty_fn(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let TyFn { args, rety } = self;
args.visit_in(v);
rety.visit_in(v);
}
}
impl Walk for Path {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_path(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Path { absolute: _, parts } = self;
parts.visit_in(v);
}
}
impl Walk for PathPart {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_path_part(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
match self {
PathPart::SuperKw => {}
PathPart::SelfTy => {}
PathPart::Ident(sym) => sym.visit_in(v),
}
}
}
impl Walk for Stmt {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_stmt(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Stmt { span, kind, semi } = self;
span.visit_in(v);
kind.visit_in(v);
semi.visit_in(v);
}
}
impl Walk for StmtKind {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_stmt_kind(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
match self {
StmtKind::Empty => {}
StmtKind::Item(value) => value.visit_in(v),
StmtKind::Expr(value) => value.visit_in(v),
}
}
}
impl Walk for Semi {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_semi(self);
}
}
impl Walk for Expr {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_expr(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Expr { span, kind } = self;
span.visit_in(v);
kind.visit_in(v);
}
}
impl Walk for ExprKind {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_expr_kind(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
match self {
ExprKind::Empty => {}
ExprKind::Closure(value) => value.visit_in(v),
ExprKind::Tuple(value) => value.visit_in(v),
ExprKind::Structor(value) => value.visit_in(v),
ExprKind::Array(value) => value.visit_in(v),
ExprKind::ArrayRep(value) => value.visit_in(v),
ExprKind::AddrOf(value) => value.visit_in(v),
ExprKind::Quote(value) => value.visit_in(v),
ExprKind::Literal(value) => value.visit_in(v),
ExprKind::Group(value) => value.visit_in(v),
ExprKind::Block(value) => value.visit_in(v),
ExprKind::Assign(value) => value.visit_in(v),
ExprKind::Modify(value) => value.visit_in(v),
ExprKind::Binary(value) => value.visit_in(v),
ExprKind::Unary(value) => value.visit_in(v),
ExprKind::Member(value) => value.visit_in(v),
ExprKind::Index(value) => value.visit_in(v),
ExprKind::Cast(value) => value.visit_in(v),
ExprKind::Path(value) => value.visit_in(v),
ExprKind::Let(value) => value.visit_in(v),
ExprKind::Match(value) => value.visit_in(v),
ExprKind::While(value) => value.visit_in(v),
ExprKind::If(value) => value.visit_in(v),
ExprKind::For(value) => value.visit_in(v),
ExprKind::Break(value) => value.visit_in(v),
ExprKind::Return(value) => value.visit_in(v),
ExprKind::Continue => v.visit_continue(),
}
}
}
impl Walk for Closure {
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_closure(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Self { arg, body } = self;
v.visit_pattern(arg);
v.visit_expr(body);
}
}
impl Walk for Tuple {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_tuple(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Tuple { exprs } = self;
exprs.visit_in(v);
}
}
impl Walk for Structor {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_structor(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Structor { to, init } = self;
to.visit_in(v);
init.visit_in(v);
}
}
impl Walk for Fielder {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_fielder(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Fielder { name, init } = self;
name.visit_in(v);
init.visit_in(v);
}
}
impl Walk for Array {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_array(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Array { values } = self;
values.visit_in(v);
}
}
impl Walk for ArrayRep {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_array_rep(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let ArrayRep { value, repeat: _ } = self;
value.visit_in(v);
// repeat.visit_in(v) // TODO
}
}
impl Walk for AddrOf {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_addrof(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let AddrOf { mutable, expr } = self;
mutable.visit_in(v);
expr.visit_in(v);
}
}
impl Walk for Cast {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_cast(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Cast { head, ty } = self;
head.visit_in(v);
ty.visit_in(v);
}
}
impl Walk for Quote {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_quote(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Quote { quote } = self;
quote.visit_in(v);
}
}
impl Walk for Group {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_group(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Group { expr } = self;
expr.visit_in(v);
}
}
impl Walk for Block {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_block(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Block { stmts } = self;
stmts.visit_in(v);
}
}
impl Walk for Assign {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_assign(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Assign { parts } = self;
parts.visit_in(v);
}
}
impl Walk for Modify {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_modify(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Modify { kind, parts } = self;
kind.visit_in(v);
parts.visit_in(v);
}
}
impl Walk for ModifyKind {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_modify_kind(self);
}
}
impl Walk for Binary {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_binary(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Binary { kind, parts } = self;
kind.visit_in(v);
parts.visit_in(v);
}
}
impl Walk for BinaryKind {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_binary_kind(self);
}
}
impl Walk for Unary {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_unary(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Unary { kind, tail } = self;
kind.visit_in(v);
tail.visit_in(v);
}
}
impl Walk for UnaryKind {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_unary_kind(self);
}
}
impl Walk for Member {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_member(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Member { head, kind } = self;
head.visit_in(v);
kind.visit_in(v);
}
}
impl Walk for MemberKind {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_member_kind(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
match self {
MemberKind::Call(sym, tuple) => {
sym.visit_in(v);
tuple.visit_in(v);
}
MemberKind::Struct(sym) => sym.visit_in(v),
MemberKind::Tuple(literal) => literal.visit_in(v),
}
}
}
impl Walk for Index {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_index(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Index { head, indices } = self;
head.visit_in(v);
indices.visit_in(v);
}
}
impl Walk for Let {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_let(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Let { mutable, name, ty, init } = self;
mutable.visit_in(v);
name.visit_in(v);
ty.visit_in(v);
init.visit_in(v);
}
}
impl Walk for Match {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_match(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Match { scrutinee, arms } = self;
scrutinee.visit_in(v);
arms.visit_in(v);
}
}
impl Walk for MatchArm {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_match_arm(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let MatchArm(pat, expr) = self;
pat.visit_in(v);
expr.visit_in(v);
}
}
impl Walk for Pattern {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_pattern(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
match self {
Pattern::Name(sym) => sym.visit_in(v),
Pattern::Path(path) => path.visit_in(v),
Pattern::Literal(literal) => literal.visit_in(v),
Pattern::Rest(pattern) => pattern.visit_in(v),
Pattern::Ref(mutability, pattern) => {
mutability.visit_in(v);
pattern.visit_in(v);
}
Pattern::RangeExc(from, to) => {
from.visit_in(v);
to.visit_in(v);
}
Pattern::RangeInc(from, to) => {
from.visit_in(v);
to.visit_in(v);
}
Pattern::Tuple(patterns) => patterns.visit_in(v),
Pattern::Array(patterns) => patterns.visit_in(v),
Pattern::Struct(path, items) => {
path.visit_in(v);
items.visit_in(v);
}
Pattern::TupleStruct(path, patterns) => {
path.visit_in(v);
patterns.visit_in(v);
}
}
}
}
impl Walk for While {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_while(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let While { cond, pass, fail } = self;
cond.visit_in(v);
pass.visit_in(v);
fail.visit_in(v);
}
}
impl Walk for If {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_if(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let If { cond, pass, fail } = self;
cond.visit_in(v);
pass.visit_in(v);
fail.visit_in(v);
}
}
impl Walk for For {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_for(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let For { bind, cond, pass, fail } = self;
cond.visit_in(v);
fail.visit_in(v);
bind.visit_in(v);
pass.visit_in(v);
}
}
impl Walk for Else {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_else(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Else { body } = self;
body.visit_in(v);
}
}
impl Walk for Break {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_break(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Break { body } = self;
body.visit_in(v);
}
}
impl Walk for Return {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
v.visit_return(self);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let Return { body } = self;
body.visit_in(v);
}
}
// --- BLANKET IMPLEMENTATIONS
impl<T: Walk> Walk for [T] {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
self.iter().for_each(|value| value.visit_in(v));
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
self.iter().for_each(|value| value.children(v));
}
}
impl<T: Walk> Walk for Vec<T> {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
self.as_slice().visit_in(v);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
self.as_slice().children(v);
}
}
impl<A: Walk, B: Walk> Walk for (A, B) {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let (a, b) = self;
a.visit_in(v);
b.visit_in(v);
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
let (a, b) = self;
a.children(v);
b.children(v);
}
}
impl<T: Walk> Walk for Option<T> {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
if let Some(value) = self.as_ref() {
value.visit_in(v)
}
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
if let Some(value) = self {
value.children(v)
}
}
}
impl<T: Walk> Walk for Box<T> {
#[inline]
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
self.as_ref().visit_in(v)
}
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
self.as_ref().children(v)
}
}

View File

@ -1,11 +1,9 @@
//! Desugaring passes for Conlang
pub mod constant_folder;
pub mod path_absoluter;
pub mod squash_groups;
pub mod while_else;
pub use constant_folder::ConstantFolder;
pub use path_absoluter::NormalizePaths;
pub use squash_groups::SquashGroups;
pub use while_else::WhileElseDesugar;

View File

@ -1,90 +0,0 @@
use crate::{
ast::{ExprKind as Ek, *},
ast_visitor::{Fold, fold::or_fold_expr_kind},
};
pub struct ConstantFolder;
macro bin_rule(
match ($kind: ident, $head: expr, $tail: expr) {
$(($op:ident, $impl:expr, $($ty:ident -> $rety:ident),*)),*$(,)?
}
) {
#[allow(clippy::all)]
match ($kind, $head, $tail) {
$($(( BinaryKind::$op,
Expr { kind: ExprKind::Literal(Literal::$ty(a)), .. },
Expr { kind: ExprKind::Literal(Literal::$ty(b)), .. },
) => {
ExprKind::Literal(Literal::$rety($impl(a, b)))
},)*)*
(kind, head, tail) => ExprKind::Binary(Binary {
kind,
parts: Box::new((head, tail)),
}),
}
}
macro un_rule(
match ($kind: ident, $tail: expr) {
$(($op:ident, $impl:expr, $($ty:ident),*)),*$(,)?
}
) {
match ($kind, $tail) {
$($((UnaryKind::$op, Expr { kind: ExprKind::Literal(Literal::$ty(v)), .. }) => {
ExprKind::Literal(Literal::$ty($impl(v)))
},)*)*
(kind, tail) => ExprKind::Unary(Unary { kind, tail: Box::new(tail) }),
}
}
impl Fold for ConstantFolder {
fn fold_expr_kind(&mut self, kind: Ek) -> Ek {
match kind {
Ek::Group(Group { expr }) => self.fold_expr_kind(expr.kind),
Ek::Binary(Binary { kind, parts }) => {
let (head, tail) = *parts;
bin_rule! (match (kind, self.fold_expr(head), self.fold_expr(tail)) {
(Lt, |a, b| a < b, Bool -> Bool, Int -> Bool),
(LtEq, |a, b| a <= b, Bool -> Bool, Int -> Bool),
(Equal, |a, b| a == b, Bool -> Bool, Int -> Bool),
(NotEq, |a, b| a != b, Bool -> Bool, Int -> Bool),
(GtEq, |a, b| a >= b, Bool -> Bool, Int -> Bool),
(Gt, |a, b| a > b, Bool -> Bool, Int -> Bool),
(BitAnd, |a, b| a & b, Bool -> Bool, Int -> Int),
(BitOr, |a, b| a | b, Bool -> Bool, Int -> Int),
(BitXor, |a, b| a ^ b, Bool -> Bool, Int -> Int),
(Shl, |a, b| a << b, Int -> Int),
(Shr, |a, b| a >> b, Int -> Int),
(Add, |a, b| a + b, Int -> Int),
(Sub, |a, b| a - b, Int -> Int),
(Mul, |a, b| a * b, Int -> Int),
(Div, |a, b| a / b, Int -> Int),
(Rem, |a, b| a % b, Int -> Int),
// Cursed bit-smuggled float shenanigans
(Lt, |a, b| (f64::from_bits(a) < f64::from_bits(b)), Float -> Bool),
(LtEq, |a, b| (f64::from_bits(a) >= f64::from_bits(b)), Float -> Bool),
(Equal, |a, b| (f64::from_bits(a) == f64::from_bits(b)), Float -> Bool),
(NotEq, |a, b| (f64::from_bits(a) != f64::from_bits(b)), Float -> Bool),
(GtEq, |a, b| (f64::from_bits(a) <= f64::from_bits(b)), Float -> Bool),
(Gt, |a, b| (f64::from_bits(a) > f64::from_bits(b)), Float -> Bool),
(Add, |a, b| (f64::from_bits(a) + f64::from_bits(b)).to_bits(), Float -> Float),
(Sub, |a, b| (f64::from_bits(a) - f64::from_bits(b)).to_bits(), Float -> Float),
(Mul, |a, b| (f64::from_bits(a) * f64::from_bits(b)).to_bits(), Float -> Float),
(Div, |a, b| (f64::from_bits(a) / f64::from_bits(b)).to_bits(), Float -> Float),
(Rem, |a, b| (f64::from_bits(a) % f64::from_bits(b)).to_bits(), Float -> Float),
})
}
Ek::Unary(Unary { kind, tail }) => {
un_rule! (match (kind, self.fold_expr(*tail)) {
(Not, std::ops::Not::not, Int, Bool),
(Neg, std::ops::Not::not, Bool),
(Neg, |i| -(i as i128) as u128, Int),
(Neg, |f| (-f64::from_bits(f)).to_bits(), Float),
(At, std::ops::Not::not, Float), /* Lmao */
})
}
_ => or_fold_expr_kind(self, kind),
}
}
}

View File

@ -23,14 +23,13 @@ impl Default for NormalizePaths {
impl Fold for NormalizePaths {
fn fold_module(&mut self, m: Module) -> Module {
let Module { name, file } = m;
let Module { name, kind } = m;
self.path.push(PathPart::Ident(name));
let name = self.fold_sym(name);
let file = file.map(|f| self.fold_file(f));
let (name, kind) = (self.fold_sym(name), self.fold_module_kind(kind));
self.path.pop();
Module { name, file }
Module { name, kind }
}
fn fold_path(&mut self, p: Path) -> Path {
@ -46,7 +45,7 @@ impl Fold for NormalizePaths {
if !absolute {
for segment in self.path.parts.iter().rev() {
tree = UseTree::Path(*segment, Box::new(tree))
tree = UseTree::Path(segment.clone(), Box::new(tree))
}
}

View File

@ -7,7 +7,7 @@ pub struct SquashGroups;
impl Fold for SquashGroups {
fn fold_expr_kind(&mut self, kind: ExprKind) -> ExprKind {
match kind {
ExprKind::Group(Group { expr }) => self.fold_expr(*expr).kind,
ExprKind::Group(Group { expr }) => self.fold_expr_kind(*expr),
_ => or_fold_expr_kind(self, kind),
}
}

View File

@ -10,27 +10,24 @@ pub struct WhileElseDesugar;
impl Fold for WhileElseDesugar {
fn fold_expr(&mut self, e: Expr) -> Expr {
let Expr { span, kind } = e;
let kind = desugar_while(span, kind);
Expr { span: self.fold_span(span), kind: self.fold_expr_kind(kind) }
let Expr { extents, kind } = e;
let kind = desugar_while(extents, kind);
Expr { extents: self.fold_span(extents), kind: self.fold_expr_kind(kind) }
}
}
/// Desugars while(-else) expressions into loop-if-else-break expressions
fn desugar_while(span: Span, kind: ExprKind) -> ExprKind {
fn desugar_while(extents: 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 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 }) };
// 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 }) };
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 { span, kind: loop_body }),
})
ExprKind::Unary(Unary { kind: UnaryKind::Loop, tail: Box::new(loop_body) })
}
_ => kind,
}

View File

@ -14,7 +14,6 @@
#![feature(decl_macro)]
pub use ast::*;
pub use ast_impl::weight_of::WeightOf;
pub mod ast;
pub mod ast_impl;

View File

@ -1,18 +0,0 @@
[package]
name = "cl-embed"
version = "0.1.0"
repository.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true
publish.workspace = true
[dependencies]
cl-interpret = { path = "../cl-interpret" }
cl-ast = { path = "../cl-ast" }
cl-structures = { path = "../cl-structures" }
cl-lexer = { path = "../cl-lexer" }
cl-parser = { path = "../cl-parser" }
[dev-dependencies]
repline = { path = "../../repline" }

View File

@ -1,27 +0,0 @@
//! Demonstrates the cl_embed library
use cl_embed::*;
use repline::{Response, prebaked};
fn main() -> Result<(), repline::Error> {
let mut env = Environment::new();
if let Err(e) = conlang_include!("calculator/expression.cl")(&mut env) {
panic!("{e}")
}
prebaked::read_and("", "calc >", " ? >", |line| {
env.bind("line", line);
let res = conlang! {
let (expr, rest) = parse(line.chars(), Power::None);
execute(expr)
}(&mut env)?;
println!("{res}");
Ok(Response::Accept)
})
}

View File

@ -1 +0,0 @@
../../../../sample-code/calculator.cl

View File

@ -1,182 +0,0 @@
//! Embed Conlang code into your Rust project!
//!
//! # This crate is experimental, and has no guarantees of stability.
#![feature(decl_macro)]
#![cfg_attr(test, feature(assert_matches))]
#![allow(unused_imports)]
pub use cl_interpret::{convalue::ConValue as Value, env::Environment};
use cl_ast::{Block, File, Module, ast_visitor::Fold};
use cl_interpret::{convalue::ConValue, interpret::Interpret};
use cl_lexer::Lexer;
use cl_parser::{Parser, error::Error as ParseError, inliner::ModuleInliner};
use std::{path::Path, sync::OnceLock};
/// Constructs a function which evaluates a Conlang Block
///
/// # Examples
///
/// Bind and use a variable
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use cl_embed::{conlang, Environment, Value};
///
/// let mut env = Environment::new();
///
/// // Bind a variable named `message` to "Hello, world!"
/// env.bind("message", "Hello, World!");
///
/// let print_hello = conlang!{
/// println(message);
/// };
///
/// // Run the function
/// let ret = print_hello(&mut env)?;
///
/// // `println` returns Empty
/// assert!(matches!(ret, Value::Empty));
///
/// # Ok(())
/// # }
/// ```
pub macro conlang(
$($t:tt)*
) {{
// Parse once
static FN: OnceLock<Result<Block, ParseError>> = OnceLock::new();
|env: &mut Environment| -> Result<ConValue, EvalError> {
FN.get_or_init(|| {
// TODO: embed the full module tree at compile time
let path =
AsRef::<Path>::as_ref(&concat!(env!("CARGO_MANIFEST_DIR"), "/../../", file!()))
.with_extension("");
let mut mi = ModuleInliner::new(path);
let code = mi.fold_block(
Parser::new(
concat!(file!(), ":", line!(), ":", column!()),
Lexer::new(stringify!({ $($t)* })),
)
.parse::<Block>()?,
);
if let Some((ie, pe)) = mi.into_errs() {
for (file, err) in ie {
eprintln!("{}: {err}", file.display());
}
for (file, err) in pe {
eprintln!("{}: {err}", file.display());
}
}
Ok(code)
})
.as_ref()
.map_err(Clone::clone)?
.interpret(env)
.map_err(Into::into)
}
}}
pub macro conlang_include{
($path:literal, $name:ident) => {
|env: &mut Environment| -> Result<ConValue, EvalError> {
// TODO: embed the full module tree at compile time
let path = AsRef::<Path>::as_ref(&concat!(env!("CARGO_MANIFEST_DIR"), "/../../", file!()))
.with_file_name(concat!($path));
let mut mi = ModuleInliner::new(path);
let code = mi.fold_module(Module {
name: stringify!($name).into(),
file: Some(
Parser::new(
concat!(file!(), ":", line!(), ":", column!()),
Lexer::new(include_str!($path)),
)
.parse()?,
),
});
if let Some((ie, pe)) = mi.into_errs() {
for (file, err) in ie {
eprintln!("{}: {err}", file.display());
}
for (file, err) in pe {
eprintln!("{}: {err}", file.display());
}
}
code.interpret(env).map_err(Into::into)
}
},
($path:literal) => {
|env: &mut Environment| -> Result<ConValue, EvalError> {
// TODO: embed the full module tree at compile time
let path = AsRef::<Path>::as_ref(&concat!(env!("CARGO_MANIFEST_DIR"), "/../../", file!()))
.with_file_name(concat!($path));
let mut mi = ModuleInliner::new(path);
let code = mi.fold_file(
Parser::new(
concat!(file!(), ":", line!(), ":", column!()),
Lexer::new(include_str!($path)),
)
.parse()?,
);
if let Some((ie, pe)) = mi.into_errs() {
for (file, err) in ie {
eprintln!("{}: {err}", file.display());
}
for (file, err) in pe {
eprintln!("{}: {err}", file.display());
}
}
code.interpret(env).map_err(Into::into)
}
}
}
#[derive(Clone, Debug)]
pub enum EvalError {
Parse(cl_parser::error::Error),
Interpret(cl_interpret::error::Error),
}
impl From<cl_parser::error::Error> for EvalError {
fn from(value: cl_parser::error::Error) -> Self {
Self::Parse(value)
}
}
impl From<cl_interpret::error::Error> for EvalError {
fn from(value: cl_interpret::error::Error) -> Self {
Self::Interpret(value)
}
}
impl std::error::Error for EvalError {}
impl std::fmt::Display for EvalError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EvalError::Parse(error) => error.fmt(f),
EvalError::Interpret(error) => error.fmt(f),
}
}
}
#[cfg(test)]
mod tests {
use std::assert_matches::assert_matches;
use super::*;
#[test]
fn it_works() -> Result<(), EvalError> {
let mut env = Environment::new();
let result = conlang! {
fn add(left, right) -> isize {
left + right
}
add(2, 2)
}(&mut env);
assert_matches!(result, Ok(Value::Int(4)));
Ok(())
}
}

View File

@ -19,7 +19,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let parent = path.parent().unwrap_or("".as_ref());
let code = std::fs::read_to_string(&path)?;
let code = Parser::new(path.display().to_string(), Lexer::new(&code)).parse()?;
let code = Parser::new(Lexer::new(&code)).parse()?;
let code = match ModuleInliner::new(parent).inline(code) {
Ok(code) => code,
Err((code, ioerrs, perrs)) => {
@ -40,7 +40,7 @@ fn main() -> Result<(), Box<dyn Error>> {
if env.get(main).is_ok() {
let args = args
.flat_map(|arg| {
Parser::new(&arg, Lexer::new(&arg))
Parser::new(Lexer::new(&arg))
.parse::<Expr>()
.map(|arg| env.eval(&arg))
})

View File

@ -5,7 +5,10 @@ use crate::{
env::Environment,
error::{Error, IResult},
};
use std::io::{Write, stdout};
use std::{
io::{stdout, Write},
slice,
};
/// A function built into the interpreter.
#[derive(Clone, Copy)]
@ -54,7 +57,7 @@ impl super::Callable for Builtin {
/// Turns a function definition into a [Builtin].
///
/// ```rust
/// # use cl_interpret::{builtin::builtin, convalue::ConValue};
/// # use cl_interpret::{builtin2::builtin, convalue::ConValue};
/// let my_builtin = builtin! {
/// /// Use the `@env` suffix to bind the environment!
/// /// (needed for recursive calls)
@ -75,11 +78,11 @@ pub macro builtin(
$(#[$($meta)*])*
fn $name(_env: &mut Environment, _args: &[ConValue]) -> IResult<ConValue> {
// Set up the builtin! environment
$(#[allow(unused)]let $env = _env;)?
$(let $env = _env;)?
// Allow for single argument `fn foo(args @ ..)` pattern
#[allow(clippy::redundant_at_rest_pattern, irrefutable_let_patterns)]
let [$($arg),*] = _args else {
Err($crate::error::Error::TypeError())?
Err($crate::error::Error::TypeError)?
};
$body.map(Into::into)
}
@ -98,10 +101,10 @@ pub macro builtins($(
[$(builtin!($(#[$($meta)*])* fn $name ($($args)*) $(@$env)? $body)),*]
}
/// Creates an [Error::BuiltinError] using interpolation of runtime expressions.
/// Creates an [Error::BuiltinDebug] using interpolation of runtime expressions.
/// See [std::format].
pub macro error_format ($($t:tt)*) {
$crate::error::Error::BuiltinError(format!($($t)*))
$crate::error::Error::BuiltinDebug(format!($($t)*))
}
pub const Builtins: &[Builtin] = &builtins![
@ -143,71 +146,30 @@ pub const Builtins: &[Builtin] = &builtins![
Ok(())
}
fn panic(message) {
Err(error_format!("Panic: {message}"))?;
Ok(())
}
/// Dumps the environment
fn dump() @env {
println!("{env}");
Ok(())
}
/// Gets all global variables in the environment
fn globals() @env {
let globals = env.globals();
Ok(ConValue::Slice(globals.base, globals.binds.len()))
}
fn builtins() @env {
let len = env.globals().binds.len();
for builtin in 0..len {
if let Some(value @ ConValue::Builtin(_)) = env.get_id(builtin) {
println!("{builtin}: {value}")
}
for builtin in env.builtins().values().flatten() {
println!("{builtin}");
}
Ok(())
}
fn alloca(ConValue::Int(len)) @env {
Ok(env.alloca(ConValue::Empty, *len as usize))
}
/// Returns the length of the input list as a [ConValue::Int]
fn len(list) @env {
Ok(match list {
ConValue::Empty => 0,
ConValue::String(s) => s.chars().count() as _,
ConValue::Ref(r) => {
return len(env, &[env.get_id(*r).ok_or(Error::StackOverflow(*r))?.clone()])
}
ConValue::Slice(_, len) => *len as _,
ConValue::Array(arr) => arr.len() as _,
ConValue::Ref(r) => return len(env, slice::from_ref(r.as_ref())),
ConValue::Array(t) => t.len() as _,
ConValue::Tuple(t) => t.len() as _,
_ => Err(Error::TypeError())?,
})
}
fn push(ConValue::Ref(index), item) @env{
let Some(ConValue::Array(v)) = env.get_id_mut(*index) else {
Err(Error::TypeError())?
};
let mut items = std::mem::take(v).into_vec();
items.push(item.clone());
*v = items.into_boxed_slice();
Ok(ConValue::Empty)
}
fn chars(string) @env {
Ok(match string {
ConValue::String(s) => ConValue::Array(s.chars().map(Into::into).collect()),
ConValue::Ref(r) => {
return chars(env, &[env.get_id(*r).ok_or(Error::StackOverflow(*r))?.clone()])
}
_ => Err(Error::TypeError())?,
ConValue::RangeExc(start, end) => (end - start) as _,
ConValue::RangeInc(start, end) => (end - start + 1) as _,
_ => Err(Error::TypeError)?,
})
}
@ -216,10 +178,6 @@ pub const Builtins: &[Builtin] = &builtins![
Ok(ConValue::Empty)
}
fn slice_of(ConValue::Ref(arr), ConValue::Int(start)) {
Ok(ConValue::Slice(*arr, *start as usize))
}
/// Returns a shark
fn shark() {
Ok('\u{1f988}')
@ -232,7 +190,7 @@ pub const Math: &[Builtin] = &builtins![
Ok(match (lhs, rhs) {
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a * b),
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
})
}
@ -241,7 +199,7 @@ pub const Math: &[Builtin] = &builtins![
Ok(match (lhs, rhs){
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a / b),
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
})
}
@ -250,7 +208,7 @@ pub const Math: &[Builtin] = &builtins![
Ok(match (lhs, rhs) {
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a % b),
_ => Err(Error::TypeError())?,
_ => Err(Error::TypeError)?,
})
}
@ -260,7 +218,7 @@ pub const Math: &[Builtin] = &builtins![
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a + b),
(ConValue::String(a), ConValue::String(b)) => (a.to_string() + &b.to_string()).into(),
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
})
}
@ -269,7 +227,7 @@ pub const Math: &[Builtin] = &builtins![
Ok(match (lhs, rhs) {
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a - b),
_ => Err(Error::TypeError())?,
_ => Err(Error::TypeError)?,
})
}
@ -278,7 +236,7 @@ pub const Math: &[Builtin] = &builtins![
Ok(match (lhs, rhs) {
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a << b),
_ => Err(Error::TypeError())?,
_ => Err(Error::TypeError)?,
})
}
@ -287,7 +245,7 @@ pub const Math: &[Builtin] = &builtins![
Ok(match (lhs, rhs) {
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a >> b),
_ => Err(Error::TypeError())?,
_ => Err(Error::TypeError)?,
})
}
@ -297,7 +255,7 @@ pub const Math: &[Builtin] = &builtins![
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a & b),
(ConValue::Bool(a), ConValue::Bool(b)) => ConValue::Bool(a & b),
_ => Err(Error::TypeError())?,
_ => Err(Error::TypeError)?,
})
}
@ -307,7 +265,7 @@ pub const Math: &[Builtin] = &builtins![
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a | b),
(ConValue::Bool(a), ConValue::Bool(b)) => ConValue::Bool(a | b),
_ => Err(Error::TypeError())?,
_ => Err(Error::TypeError)?,
})
}
@ -317,36 +275,24 @@ pub const Math: &[Builtin] = &builtins![
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a ^ b),
(ConValue::Bool(a), ConValue::Bool(b)) => ConValue::Bool(a ^ b),
_ => Err(Error::TypeError())?,
_ => Err(Error::TypeError)?,
})
}
#[allow(non_snake_case)]
fn RangeExc(start, end) @env {
Ok(ConValue::TupleStruct(Box::new((
"RangeExc", Box::new([start.clone(), end.clone()])
))))
/// Exclusive Range `a..b`
fn range_exc(from, to) {
let (&ConValue::Int(from), &ConValue::Int(to)) = (from, to) else {
Err(Error::TypeError)?
};
Ok(ConValue::RangeExc(from, to))
}
#[allow(non_snake_case)]
fn RangeInc(start, end) @env {
Ok(ConValue::TupleStruct(Box::new((
"RangeInc", Box::new([start.clone(), end.clone()])
))))
}
#[allow(non_snake_case)]
fn RangeTo(end) @env {
Ok(ConValue::TupleStruct(Box::new((
"RangeTo", Box::new([end.clone()])
))))
}
#[allow(non_snake_case)]
fn RangeToInc(end) @env {
Ok(ConValue::TupleStruct(Box::new((
"RangeToInc", Box::new([end.clone()])
))))
/// Inclusive Range `a..=b`
fn range_inc(from, to) {
let (&ConValue::Int(from), &ConValue::Int(to)) = (from, to) else {
Err(Error::TypeError)?
};
Ok(ConValue::RangeInc(from, to))
}
/// Negates the ConValue
@ -355,7 +301,7 @@ pub const Math: &[Builtin] = &builtins![
ConValue::Empty => ConValue::Empty,
ConValue::Int(v) => ConValue::Int(v.wrapping_neg()),
ConValue::Float(v) => ConValue::Float(-v),
_ => Err(Error::TypeError())?,
_ => Err(Error::TypeError)?,
})
}
@ -365,7 +311,7 @@ pub const Math: &[Builtin] = &builtins![
ConValue::Empty => ConValue::Empty,
ConValue::Int(v) => ConValue::Int(!v),
ConValue::Bool(v) => ConValue::Bool(!v),
_ => Err(Error::TypeError())?,
_ => Err(Error::TypeError)?,
})
}
@ -381,9 +327,10 @@ pub const Math: &[Builtin] = &builtins![
}
/// Does the opposite of `&`
fn deref(tail) @env {
fn deref(tail) {
use std::rc::Rc;
Ok(match tail {
ConValue::Ref(v) => env.get_id(*v).cloned().ok_or(Error::StackOverflow(*v))?,
ConValue::Ref(v) => Rc::as_ref(v).clone(),
_ => tail.clone(),
})
}

View File

@ -1,68 +0,0 @@
use crate::{
Callable,
convalue::ConValue,
env::Environment,
error::{Error, ErrorKind, IResult},
function::collect_upvars::CollectUpvars,
interpret::Interpret,
pattern,
};
use cl_ast::{Sym, ast_visitor::Visit};
use std::{collections::HashMap, fmt::Display};
/// Represents an ad-hoc anonymous function
/// which captures surrounding state by COPY
#[derive(Clone, Debug)]
pub struct Closure {
decl: cl_ast::Closure,
lift: HashMap<Sym, ConValue>,
}
impl Closure {
const NAME: &'static str = "{closure}";
}
impl Closure {
pub fn new(env: &mut Environment, decl: &cl_ast::Closure) -> Self {
let lift = CollectUpvars::new(env).visit(decl).finish_copied();
Self { decl: decl.clone(), lift }
}
}
impl Display for Closure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { decl, lift: _ } = self;
write!(f, "{decl}")
}
}
impl Callable for Closure {
fn call(&self, env: &mut Environment, args: &[ConValue]) -> IResult<ConValue> {
let Self { decl, lift } = self;
let mut env = env.frame(Self::NAME);
// place lifts in scope
for (name, value) in lift.clone() {
env.insert(name, value);
}
let mut env = env.frame("args");
for (name, value) in pattern::substitution(&env, &decl.arg, ConValue::Tuple(args.into()))? {
env.insert(name, value);
}
let res = decl.body.interpret(&mut env);
drop(env);
match res {
Err(Error { kind: ErrorKind::Return(value), .. }) => Ok(value),
Err(Error { kind: ErrorKind::Break(value), .. }) => Err(Error::BadBreak(value)),
other => other,
}
}
fn name(&self) -> cl_ast::Sym {
Self::NAME.into()
}
}

View File

@ -1,40 +1,15 @@
//! Values in the dynamically typed AST interpreter.
//!
//! The most permanent fix is a temporary one.
use cl_ast::{Expr, Sym, format::FmtAdapter};
use crate::{closure::Closure, constructor::Constructor};
use cl_ast::{format::FmtAdapter, ExprKind, Sym};
use super::{
Callable, Environment,
builtin::Builtin,
error::{Error, IResult},
function::Function,
function::Function, Callable, Environment,
};
use std::{collections::HashMap, ops::*, rc::Rc};
/*
A Value can be:
- A Primitive (Empty, isize, etc.)
- A Record (Array, Tuple, Struct)
- A Variant (discriminant, Value) pair
array [
10, // 0
20, // 1
]
tuple (
10, // 0
20, // 1
)
struct {
x: 10, // x => 0
y: 20, // y => 1
}
*/
type Integer = isize;
/// A Conlang value stores data in the interpreter
@ -54,29 +29,23 @@ pub enum ConValue {
/// A string
String(Sym),
/// A reference
Ref(usize),
/// A reference to an array
Slice(usize, usize),
Ref(Rc<ConValue>),
/// An Array
Array(Box<[ConValue]>),
/// A tuple
Tuple(Box<[ConValue]>),
/// An exclusive range
RangeExc(Integer, Integer),
/// An inclusive range
RangeInc(Integer, Integer),
/// A value of a product type
Struct(Box<(&'static str, HashMap<Sym, ConValue>)>),
/// A value of a product type with anonymous members
TupleStruct(Box<(&'static str, Box<[ConValue]>)>),
Struct(Box<(Sym, HashMap<Sym, ConValue>)>),
/// An entire namespace
Module(Box<HashMap<Sym, ConValue>>),
/// A namespace, sans storage
Module2(HashMap<Sym, usize>),
Module(Box<HashMap<Sym, Option<ConValue>>>),
/// A quoted expression
Quote(Box<Expr>),
Quote(Box<ExprKind>),
/// A callable thing
Function(Rc<Function>),
/// A tuple constructor
TupleConstructor(Constructor),
/// A closure, capturing by reference
Closure(Rc<Closure>),
/// A built-in function
Builtin(&'static Builtin),
}
@ -86,70 +55,36 @@ impl ConValue {
pub fn truthy(&self) -> IResult<bool> {
match self {
ConValue::Bool(v) => Ok(*v),
_ => Err(Error::TypeError())?,
_ => Err(Error::TypeError)?,
}
}
pub fn typename(&self) -> IResult<&'static str> {
Ok(match self {
ConValue::Empty => "Empty",
ConValue::Int(_) => "i64",
ConValue::Float(_) => "f64",
ConValue::Bool(_) => "bool",
ConValue::Char(_) => "char",
ConValue::String(_) => "String",
ConValue::Ref(_) => "Ref",
ConValue::Slice(_, _) => "Slice",
ConValue::Array(_) => "Array",
ConValue::Tuple(_) => "Tuple",
ConValue::Struct(_) => "Struct",
ConValue::TupleStruct(_) => "TupleStruct",
ConValue::Module(_) => "",
ConValue::Module2(_) => "",
ConValue::Quote(_) => "Quote",
ConValue::Function(_) => "Fn",
ConValue::TupleConstructor(_) => "Fn",
ConValue::Closure(_) => "Fn",
ConValue::Builtin(_) => "Fn",
})
pub fn range_exc(self, other: Self) -> IResult<Self> {
let (Self::Int(a), Self::Int(b)) = (self, other) else {
Err(Error::TypeError)?
};
Ok(Self::RangeExc(a, b))
}
#[allow(non_snake_case)]
pub fn TupleStruct(id: Sym, values: Box<[ConValue]>) -> Self {
Self::TupleStruct(Box::new((id.to_ref(), values)))
pub fn range_inc(self, other: Self) -> IResult<Self> {
let (Self::Int(a), Self::Int(b)) = (self, other) else {
Err(Error::TypeError)?
};
Ok(Self::RangeInc(a, b))
}
#[allow(non_snake_case)]
pub fn Struct(id: Sym, values: HashMap<Sym, ConValue>) -> Self {
Self::Struct(Box::new((id.to_ref(), values)))
}
pub fn index(&self, index: &Self, _env: &Environment) -> IResult<ConValue> {
let &Self::Int(index) = index else {
Err(Error::TypeError())?
pub fn index(&self, index: &Self) -> IResult<ConValue> {
let Self::Int(index) = index else {
Err(Error::TypeError)?
};
match self {
ConValue::String(string) => string
.chars()
.nth(index as _)
.nth(*index as _)
.map(ConValue::Char)
.ok_or(Error::OobIndex(index as usize, string.chars().count())),
.ok_or(Error::OobIndex(*index as usize, string.chars().count())),
ConValue::Array(arr) => arr
.get(index as usize)
.get(*index as usize)
.cloned()
.ok_or(Error::OobIndex(index as usize, arr.len())),
&ConValue::Slice(id, len) => {
let index = if index < 0 {
len.wrapping_add_signed(index)
} else {
index as usize
};
if index < len {
Ok(ConValue::Ref(id + index))
} else {
Err(Error::OobIndex(index, len))
}
}
_ => Err(Error::TypeError()),
.ok_or(Error::OobIndex(*index as usize, arr.len())),
_ => Err(Error::TypeError),
}
}
cmp! {
@ -178,29 +113,14 @@ impl Callable for ConValue {
fn name(&self) -> Sym {
match self {
ConValue::Function(func) => func.name(),
ConValue::Closure(func) => func.name(),
ConValue::Builtin(func) => func.name(),
_ => "".into(),
}
}
fn call(&self, env: &mut Environment, args: &[ConValue]) -> IResult<ConValue> {
fn call(&self, interpreter: &mut Environment, args: &[ConValue]) -> IResult<ConValue> {
match self {
Self::Function(func) => func.call(env, args),
Self::TupleConstructor(func) => func.call(env, args),
Self::Closure(func) => func.call(env, args),
Self::Builtin(func) => func.call(env, args),
Self::Module(m) => {
if let Some(func) = m.get(&"call".into()) {
func.call(env, args)
} else {
Err(Error::NotCallable(self.clone()))
}
}
&Self::Ref(ptr) => {
// Move onto stack, and call
let func = env.get_id(ptr).ok_or(Error::StackOverflow(ptr))?.clone();
func.call(env, args)
}
Self::Function(func) => func.call(interpreter, args),
Self::Builtin(func) => func.call(interpreter, args),
_ => Err(Error::NotCallable(self.clone())),
}
}
@ -217,7 +137,7 @@ macro cmp ($($fn:ident: $empty:literal, $op:tt);*$(;)?) {$(
(Self::Bool(a), Self::Bool(b)) => Ok(Self::Bool(a $op b)),
(Self::Char(a), Self::Char(b)) => Ok(Self::Bool(a $op b)),
(Self::String(a), Self::String(b)) => Ok(Self::Bool(&**a $op &**b)),
_ => Err(Error::TypeError())
_ => Err(Error::TypeError)
}
}
)*}
@ -245,9 +165,9 @@ from! {
char => ConValue::Char,
Sym => ConValue::String,
&str => ConValue::String,
Expr => ConValue::Quote,
String => ConValue::String,
Rc<str> => ConValue::String,
ExprKind => ConValue::Quote,
Function => ConValue::Function,
Vec<ConValue> => ConValue::Tuple,
&'static Builtin => ConValue::Builtin,
@ -259,9 +179,9 @@ impl From<()> for ConValue {
}
impl From<&[ConValue]> for ConValue {
fn from(value: &[ConValue]) -> Self {
match value {
[] => Self::Empty,
[value] => value.clone(),
match value.len() {
0 => Self::Empty,
1 => value[0].clone(),
_ => Self::Tuple(value.into()),
}
}
@ -287,25 +207,25 @@ ops! {
(ConValue::Char(a), ConValue::Char(b)) => {
ConValue::String([a, b].into_iter().collect::<String>().into())
}
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
]
BitAnd: bitand = [
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a & b),
(ConValue::Bool(a), ConValue::Bool(b)) => ConValue::Bool(a & b),
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
]
BitOr: bitor = [
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a | b),
(ConValue::Bool(a), ConValue::Bool(b)) => ConValue::Bool(a | b),
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
]
BitXor: bitxor = [
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a ^ b),
(ConValue::Bool(a), ConValue::Bool(b)) => ConValue::Bool(a ^ b),
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
]
Div: div = [
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
@ -313,13 +233,13 @@ ops! {
eprintln!("Warning: Divide by zero in {a} / {b}"); a
})),
(ConValue::Float(a), ConValue::Float(b)) => ConValue::Float(a / b),
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
]
Mul: mul = [
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a.wrapping_mul(b)),
(ConValue::Float(a), ConValue::Float(b)) => ConValue::Float(a * b),
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
]
Rem: rem = [
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
@ -327,23 +247,23 @@ ops! {
println!("Warning: Divide by zero in {a} % {b}"); a
})),
(ConValue::Float(a), ConValue::Float(b)) => ConValue::Float(a % b),
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
]
Shl: shl = [
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a.wrapping_shl(b as _)),
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
]
Shr: shr = [
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a.wrapping_shr(b as _)),
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
]
Sub: sub = [
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a.wrapping_sub(b)),
(ConValue::Float(a), ConValue::Float(b)) => ConValue::Float(a - b),
_ => Err(Error::TypeError())?
_ => Err(Error::TypeError)?
]
}
impl std::fmt::Display for ConValue {
@ -355,8 +275,7 @@ impl std::fmt::Display for ConValue {
ConValue::Bool(v) => v.fmt(f),
ConValue::Char(v) => v.fmt(f),
ConValue::String(v) => v.fmt(f),
ConValue::Ref(v) => write!(f, "&<{}>", v),
ConValue::Slice(id, len) => write!(f, "&<{id}>[{len}..]"),
ConValue::Ref(v) => write!(f, "&{v}"),
ConValue::Array(array) => {
'['.fmt(f)?;
for (idx, element) in array.iter().enumerate() {
@ -367,6 +286,8 @@ impl std::fmt::Display for ConValue {
}
']'.fmt(f)
}
ConValue::RangeExc(a, b) => write!(f, "{a}..{}", b + 1),
ConValue::RangeInc(a, b) => write!(f, "{a}..={b}"),
ConValue::Tuple(tuple) => {
'('.fmt(f)?;
for (idx, element) in tuple.iter().enumerate() {
@ -377,22 +298,12 @@ impl std::fmt::Display for ConValue {
}
')'.fmt(f)
}
ConValue::TupleStruct(parts) => {
let (id, tuple) = parts.as_ref();
write!(f, "{id}")?;
'('.fmt(f)?;
for (idx, element) in tuple.iter().enumerate() {
if idx > 0 {
", ".fmt(f)?
}
element.fmt(f)?
}
')'.fmt(f)
}
ConValue::Struct(parts) => {
let (id, map) = parts.as_ref();
let (name, map) = parts.as_ref();
use std::fmt::Write;
write!(f, "{id} ")?;
if !name.is_empty() {
write!(f, "{name}: ")?;
}
let mut f = f.delimit_with("{", "\n}");
for (k, v) in map.iter() {
write!(f, "\n{k}: {v},")?;
@ -403,15 +314,11 @@ impl std::fmt::Display for ConValue {
use std::fmt::Write;
let mut f = f.delimit_with("{", "\n}");
for (k, v) in module.iter() {
write!(f, "\n{k}: {v},")?;
}
Ok(())
}
ConValue::Module2(module) => {
use std::fmt::Write;
let mut f = f.delimit_with("{", "\n}");
for (k, v) in module.iter() {
write!(f, "\n{k}: <{v}>,")?;
write!(f, "\n{k}: ")?;
match v {
Some(v) => write!(f, "{v},"),
None => write!(f, "_,"),
}?
}
Ok(())
}
@ -421,25 +328,9 @@ impl std::fmt::Display for ConValue {
ConValue::Function(func) => {
write!(f, "{}", func.decl())
}
ConValue::TupleConstructor(Constructor { name: index, arity }) => {
write!(f, "{index}(..{arity})")
}
ConValue::Closure(func) => {
write!(f, "{}", func.as_ref())
}
ConValue::Builtin(func) => {
write!(f, "{}", func.description())
}
}
}
}
pub macro cvstruct (
$Name:ident {
$($member:ident : $expr:expr),*
}
) {{
let mut members = HashMap::new();
$(members.insert(stringify!($member).into(), ($expr).into());)*
ConValue::Struct(Box::new((stringify!($Name).into(), members)))
}}

View File

@ -1,13 +1,13 @@
//! Lexical and non-lexical scoping for variables
use crate::{builtin::Builtin, constructor::Constructor, modules::ModuleTree};
use crate::builtin::Builtin;
use super::{
Callable, Interpret,
builtin::{Builtins, Math},
convalue::ConValue,
error::{Error, IResult},
function::Function,
Callable, Interpret,
};
use cl_ast::{Function as FnDecl, Sym};
use std::{
@ -17,58 +17,54 @@ use std::{
rc::Rc,
};
pub type StackFrame = HashMap<Sym, ConValue>;
pub type StackBinds = HashMap<Sym, usize>;
#[derive(Clone, Debug, Default)]
pub(crate) struct EnvFrame {
pub name: Option<&'static str>,
/// The length of the array when this stack frame was constructed
pub base: usize,
/// The bindings of name to stack position
pub binds: StackBinds,
}
type StackFrame = HashMap<Sym, Option<ConValue>>;
/// Implements a nested lexical scope
#[derive(Clone, Debug)]
pub struct Environment {
values: Vec<ConValue>,
frames: Vec<EnvFrame>,
modules: ModuleTree,
builtin: StackFrame,
global: Vec<(StackFrame, &'static str)>,
frames: Vec<(StackFrame, &'static str)>,
}
impl Display for Environment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for EnvFrame { name, base: _, binds } in self.frames.iter().rev() {
writeln!(
f,
"--- {}[{}] ---",
if let Some(name) = name { name } else { "" },
binds.len(),
)?;
let mut binds: Vec<_> = binds.iter().collect();
binds.sort_by(|(_, a), (_, b)| a.cmp(b));
for (name, idx) in binds {
write!(f, "{idx:4} {name}: ")?;
match self.values.get(*idx) {
for (frame, name) in self
.global
.iter()
.rev()
.take(2)
.rev()
.chain(self.frames.iter())
{
writeln!(f, "--- {name} ---")?;
for (var, val) in frame {
write!(f, "{var}: ")?;
match val {
Some(value) => writeln!(f, "\t{value}"),
None => writeln!(f, "ERROR: {name}'s address blows the stack!"),
None => writeln!(f, "<undefined>"),
}?
}
}
Ok(())
}
}
impl Default for Environment {
fn default() -> Self {
let mut this = Self::no_builtins();
this.add_builtins(Builtins).add_builtins(Math);
this
Self {
builtin: to_hashmap(Builtins.iter().chain(Math.iter())),
global: vec![(HashMap::new(), "globals")],
frames: vec![],
}
}
}
fn to_hashmap(from: impl IntoIterator<Item = &'static Builtin>) -> HashMap<Sym, Option<ConValue>> {
from.into_iter()
.map(|v| (v.name(), Some(v.into())))
.collect()
}
impl Environment {
pub fn new() -> Self {
Self::default()
@ -76,186 +72,139 @@ impl Environment {
/// Creates an [Environment] with no [builtins](super::builtin)
pub fn no_builtins() -> Self {
Self {
values: Vec::new(),
frames: vec![EnvFrame::default()],
modules: ModuleTree::default(),
builtin: HashMap::new(),
global: vec![(Default::default(), "globals")],
frames: vec![],
}
}
/// Reflexively evaluates a node
pub fn builtins(&self) -> &StackFrame {
&self.builtin
}
pub fn add_builtin(&mut self, builtin: &'static Builtin) -> &mut Self {
self.builtin.insert(builtin.name(), Some(builtin.into()));
self
}
pub fn add_builtins(&mut self, builtins: &'static [Builtin]) {
for builtin in builtins {
self.add_builtin(builtin);
}
}
pub fn push_frame(&mut self, name: &'static str, frame: StackFrame) {
self.frames.push((frame, name));
}
pub fn pop_frame(&mut self) -> Option<(StackFrame, &'static str)> {
self.frames.pop()
}
pub fn eval(&mut self, node: &impl Interpret) -> IResult<ConValue> {
node.interpret(self)
}
/// Calls a function inside the Environment's scope,
/// Calls a function inside the interpreter's scope,
/// and returns the result
pub fn call(&mut self, name: Sym, args: &[ConValue]) -> IResult<ConValue> {
let function = self.get(name)?;
// FIXME: Clone to satisfy the borrow checker
let function = self.get(name)?.clone();
function.call(self, args)
}
pub fn modules_mut(&mut self) -> &mut ModuleTree {
&mut self.modules
}
pub fn modules(&self) -> &ModuleTree {
&self.modules
}
/// Binds a value to the given name in the current scope.
pub fn bind(&mut self, name: impl Into<Sym>, value: impl Into<ConValue>) {
self.insert(name.into(), value.into());
}
pub fn bind_raw(&mut self, name: Sym, id: usize) -> Option<()> {
let EnvFrame { name: _, base: _, binds } = self.frames.last_mut()?;
binds.insert(name, id);
Some(())
}
/// Gets all registered globals, bound or unbound.
pub(crate) fn globals(&self) -> &EnvFrame {
self.frames.first().unwrap()
}
/// Adds builtins
///
/// # Panics
///
/// Will panic if stack contains more than the globals frame!
pub fn add_builtins(&mut self, builtins: &'static [Builtin]) -> &mut Self {
if self.frames.len() != 1 {
panic!("Cannot add builtins to full stack: {self}")
}
for builtin in builtins {
self.insert(builtin.name(), builtin.into());
}
self
}
pub fn push_frame(&mut self, name: &'static str, frame: StackFrame) {
self.frames.push(EnvFrame {
name: Some(name),
base: self.values.len(),
binds: HashMap::new(),
});
for (k, v) in frame {
self.insert(k, v);
}
}
pub fn pop_frame(&mut self) -> Option<(StackFrame, &'static str)> {
let mut out = HashMap::new();
let EnvFrame { name, base, binds } = self.frames.pop()?;
for (k, v) in binds {
out.insert(k, self.values.get_mut(v).map(std::mem::take)?);
}
self.values.truncate(base);
Some((out, name.unwrap_or("")))
}
/// Enters a nested scope, returning a [`Frame`] stack-guard.
///
/// [`Frame`] implements Deref/DerefMut for [`Environment`].
pub fn frame(&mut self, name: &'static str) -> Frame {
Frame::new(self, name)
}
/// Enters a nested scope, assigning the contents of `frame`,
/// and returning a [`Frame`] stack-guard.
///
/// [`Frame`] implements Deref/DerefMut for [`Environment`].
pub fn with_frame<'e>(&'e mut self, name: &'static str, frame: StackFrame) -> Frame<'e> {
let mut scope = self.frame(name);
for (k, v) in frame {
scope.insert(k, v);
}
scope
}
/// Resolves a variable mutably.
///
/// Returns a mutable reference to the variable's record, if it exists.
pub fn get_mut(&mut self, name: Sym) -> IResult<&mut ConValue> {
let at = self.id_of(name)?;
self.get_id_mut(at).ok_or(Error::NotDefined(name))
pub fn get_mut(&mut self, id: Sym) -> IResult<&mut Option<ConValue>> {
for (frame, _) in self.frames.iter_mut().rev() {
if let Some(var) = frame.get_mut(&id) {
return Ok(var);
}
}
for (frame, _) in self.global.iter_mut().rev() {
if let Some(var) = frame.get_mut(&id) {
return Ok(var);
}
}
self.builtin.get_mut(&id).ok_or(Error::NotDefined(id))
}
/// Resolves a variable immutably.
///
/// Returns a reference to the variable's contents, if it is defined and initialized.
pub fn get(&self, name: Sym) -> IResult<ConValue> {
let id = self.id_of(name)?;
let res = self.values.get(id);
Ok(res.ok_or(Error::NotDefined(name))?.clone())
}
/// Resolves the index associated with a [Sym]
pub fn id_of(&self, name: Sym) -> IResult<usize> {
for EnvFrame { binds, .. } in self.frames.iter().rev() {
if let Some(id) = binds.get(&name).copied() {
return Ok(id);
pub fn get(&self, id: Sym) -> IResult<ConValue> {
for (frame, _) in self.frames.iter().rev() {
match frame.get(&id) {
Some(Some(var)) => return Ok(var.clone()),
Some(None) => return Err(Error::NotInitialized(id)),
_ => (),
}
}
Err(Error::NotDefined(name))
for (frame, _) in self.global.iter().rev() {
match frame.get(&id) {
Some(Some(var)) => return Ok(var.clone()),
Some(None) => return Err(Error::NotInitialized(id)),
_ => (),
}
}
self.builtin
.get(&id)
.cloned()
.flatten()
.ok_or(Error::NotDefined(id))
}
pub fn get_id(&self, id: usize) -> Option<&ConValue> {
self.values.get(id)
}
pub fn get_id_mut(&mut self, id: usize) -> Option<&mut ConValue> {
self.values.get_mut(id)
}
pub fn get_slice(&self, start: usize, len: usize) -> Option<&[ConValue]> {
self.values.get(start..start + len)
}
pub fn get_slice_mut(&mut self, start: usize, len: usize) -> Option<&mut [ConValue]> {
self.values.get_mut(start..start + len)
pub(crate) fn get_local(&self, id: Sym) -> IResult<ConValue> {
for (frame, _) in self.frames.iter().rev() {
match frame.get(&id) {
Some(Some(var)) => return Ok(var.clone()),
Some(None) => return Err(Error::NotInitialized(id)),
_ => (),
}
}
Err(Error::NotInitialized(id))
}
/// Inserts a new [ConValue] into this [Environment]
pub fn insert(&mut self, k: Sym, v: ConValue) {
if self.bind_raw(k, self.values.len()).is_some() {
self.values.push(v);
pub fn insert(&mut self, id: Sym, value: Option<ConValue>) {
if let Some((frame, _)) = self.frames.last_mut() {
frame.insert(id, value);
} else if let Some((frame, _)) = self.global.last_mut() {
frame.insert(id, value);
}
}
/// A convenience function for registering a [FnDecl] as a [Function]
pub fn insert_fn(&mut self, decl: &FnDecl) {
let FnDecl { name, .. } = decl;
let (name, function) = (*name, Rc::new(Function::new(decl)));
self.insert(name, ConValue::Function(function.clone()));
let (name, function) = (name, Rc::new(Function::new(decl)));
if let Some((frame, _)) = self.frames.last_mut() {
frame.insert(*name, Some(ConValue::Function(function.clone())));
} else if let Some((frame, _)) = self.global.last_mut() {
frame.insert(*name, Some(ConValue::Function(function.clone())));
}
// Tell the function to lift its upvars now, after it's been declared
function.lift_upvars(self);
}
}
pub fn insert_tup_constructor(&mut self, name: Sym, arity: usize) {
let cs = Constructor { arity: arity as _, name };
self.insert(name, ConValue::TupleConstructor(cs));
/// Functions which aid in the implementation of [`Frame`]
impl Environment {
/// Enters a scope, creating a new namespace for variables
fn enter(&mut self, name: &'static str) -> &mut Self {
self.frames.push((Default::default(), name));
self
}
/// Gets the current stack top position
pub fn pos(&self) -> usize {
self.values.len()
}
/// Allocates a local variable
pub fn stack_alloc(&mut self, value: ConValue) -> IResult<usize> {
let adr = self.values.len();
self.values.push(value);
Ok(adr)
}
/// Allocates some space on the stack
pub fn alloca(&mut self, value: ConValue, len: usize) -> ConValue {
let idx = self.values.len();
self.values.extend(std::iter::repeat_n(value, len));
ConValue::Slice(idx, len)
/// Exits the scope, destroying all local variables and
/// returning the outer scope, if there is one
fn exit(&mut self) -> &mut Self {
self.frames.pop();
self
}
}
@ -266,28 +215,7 @@ pub struct Frame<'scope> {
}
impl<'scope> Frame<'scope> {
fn new(scope: &'scope mut Environment, name: &'static str) -> Self {
scope.frames.push(EnvFrame {
name: Some(name),
base: scope.values.len(),
binds: HashMap::new(),
});
Self { scope }
}
pub fn pop_values(mut self) -> Option<StackFrame> {
let mut out = HashMap::new();
let binds = std::mem::take(&mut self.frames.last_mut()?.binds);
for (k, v) in binds {
out.insert(k, self.values.get_mut(v).map(std::mem::take)?);
}
Some(out)
}
pub fn into_binds(mut self) -> Option<StackBinds> {
let EnvFrame { name: _, base: _, binds } = self.frames.pop()?;
std::mem::forget(self);
Some(binds)
Self { scope: scope.enter(name) }
}
}
impl Deref for Frame<'_> {
@ -303,8 +231,6 @@ impl DerefMut for Frame<'_> {
}
impl Drop for Frame<'_> {
fn drop(&mut self) {
if let Some(frame) = self.frames.pop() {
self.values.truncate(frame.base);
}
self.scope.exit();
}
}

View File

@ -1,123 +1,14 @@
//! The [Error] type represents any error thrown by the [Environment](super::Environment)
use cl_ast::{Pattern, Sym};
use cl_structures::span::Span;
use super::convalue::ConValue;
pub type IResult<T> = Result<T, Error>;
#[derive(Clone, Debug)]
pub struct Error {
pub kind: ErrorKind,
span: Option<Span>,
}
impl Error {
#![allow(non_snake_case)]
/// Adds a [struct Span] to this [Error], if there isn't already a more specific one.
pub fn with_span(self, span: Span) -> Self {
Self { span: self.span.or(Some(span)), ..self }
}
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
/// Propagate a Return value
pub fn Return(value: ConValue) -> Self {
Self { kind: ErrorKind::Return(value), span: None }
}
/// Propagate a Break value
pub fn Break(value: ConValue) -> Self {
Self { kind: ErrorKind::Break(value), span: None }
}
/// Break propagated across function bounds
pub fn BadBreak(value: ConValue) -> Self {
Self { kind: ErrorKind::BadBreak(value), span: None }
}
/// Continue to the next iteration of a loop
pub fn Continue() -> Self {
Self { kind: ErrorKind::Continue, span: None }
}
/// Underflowed the stack
pub fn StackUnderflow() -> Self {
Self { kind: ErrorKind::StackUnderflow, span: None }
}
/// Overflowed the stack
pub fn StackOverflow(place: usize) -> Self {
Self { kind: ErrorKind::StackOverflow(place), span: None }
}
/// Exited the last scope
pub fn ScopeExit() -> Self {
Self { kind: ErrorKind::ScopeExit, span: None }
}
/// Type incompatibility
// TODO: store the type information in this error
pub fn TypeError() -> Self {
Self { kind: ErrorKind::TypeError, span: None }
}
/// In clause of For loop didn't yield a Range
pub fn NotIterable() -> Self {
Self { kind: ErrorKind::NotIterable, span: None }
}
/// A value could not be indexed
pub fn NotIndexable() -> Self {
Self { kind: ErrorKind::NotIndexable, span: None }
}
/// An array index went out of bounds
pub fn OobIndex(index: usize, length: usize) -> Self {
Self { kind: ErrorKind::OobIndex(index, length), span: None }
}
/// An expression is not assignable
pub fn NotAssignable() -> Self {
Self { kind: ErrorKind::NotAssignable, span: None }
}
/// A name was not defined in scope before being used
pub fn NotDefined(name: Sym) -> Self {
Self { kind: ErrorKind::NotDefined(name), span: None }
}
/// A name was defined but not initialized
pub fn NotInitialized(name: Sym) -> Self {
Self { kind: ErrorKind::NotInitialized(name), span: None }
}
/// A value was called, but is not callable
pub fn NotCallable(value: ConValue) -> Self {
Self { kind: ErrorKind::NotCallable(value), span: None }
}
/// A function was called with the wrong number of arguments
pub fn ArgNumber(want: usize, got: usize) -> Self {
Self { kind: ErrorKind::ArgNumber { want, got }, span: None }
}
/// A pattern failed to match
pub fn PatFailed(pat: Box<Pattern>) -> Self {
Self { kind: ErrorKind::PatFailed(pat), span: None }
}
/// Fell through a non-exhaustive match
pub fn MatchNonexhaustive() -> Self {
Self { kind: ErrorKind::MatchNonexhaustive, span: None }
}
/// Error produced by a Builtin
pub fn BuiltinError(msg: String) -> Self {
Self { kind: ErrorKind::BuiltinError(msg), span: None }
}
}
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { kind, span } = self;
if let Some(Span { head, tail }) = span {
write!(f, "{head}..{tail}: ")?;
}
write!(f, "{kind}")
}
}
/// Represents any error thrown by the [Environment](super::Environment)
#[derive(Clone, Debug)]
pub enum ErrorKind {
pub enum Error {
/// Propagate a Return value
Return(ConValue),
/// Propagate a Break value
@ -128,8 +19,6 @@ pub enum ErrorKind {
Continue,
/// Underflowed the stack
StackUnderflow,
/// Overflowed the stack
StackOverflow(usize),
/// Exited the last scope
ScopeExit,
/// Type incompatibility
@ -156,56 +45,53 @@ pub enum ErrorKind {
/// Fell through a non-exhaustive match
MatchNonexhaustive,
/// Error produced by a Builtin
BuiltinError(String),
BuiltinDebug(String),
}
impl std::error::Error for ErrorKind {}
impl std::fmt::Display for ErrorKind {
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ErrorKind::Return(value) => write!(f, "return {value}"),
ErrorKind::Break(value) => write!(f, "break {value}"),
ErrorKind::BadBreak(value) => write!(f, "rogue break: {value}"),
ErrorKind::Continue => "continue".fmt(f),
ErrorKind::StackUnderflow => "Stack underflow".fmt(f),
ErrorKind::StackOverflow(id) => {
write!(f, "Attempt to access <{id}> resulted in stack overflow.")
}
ErrorKind::ScopeExit => "Exited the last scope. This is a logic bug.".fmt(f),
ErrorKind::TypeError => "Incompatible types".fmt(f),
ErrorKind::NotIterable => "`in` clause of `for` loop did not yield an iterable".fmt(f),
ErrorKind::NotIndexable => {
Error::Return(value) => write!(f, "return {value}"),
Error::Break(value) => write!(f, "break {value}"),
Error::BadBreak(value) => write!(f, "rogue break: {value}"),
Error::Continue => "continue".fmt(f),
Error::StackUnderflow => "Stack underflow".fmt(f),
Error::ScopeExit => "Exited the last scope. This is a logic bug.".fmt(f),
Error::TypeError => "Incompatible types".fmt(f),
Error::NotIterable => "`in` clause of `for` loop did not yield an iterable".fmt(f),
Error::NotIndexable => {
write!(f, "expression cannot be indexed")
}
ErrorKind::OobIndex(idx, len) => {
Error::OobIndex(idx, len) => {
write!(f, "Index out of bounds: index was {idx}. but len is {len}")
}
ErrorKind::NotAssignable => {
Error::NotAssignable => {
write!(f, "expression is not assignable")
}
ErrorKind::NotDefined(value) => {
Error::NotDefined(value) => {
write!(f, "{value} not bound. Did you mean `let {value};`?")
}
ErrorKind::NotInitialized(value) => {
Error::NotInitialized(value) => {
write!(f, "{value} bound, but not initialized")
}
ErrorKind::NotCallable(value) => {
Error::NotCallable(value) => {
write!(f, "{value} is not callable.")
}
ErrorKind::ArgNumber { want, got } => {
Error::ArgNumber { want, got } => {
write!(
f,
"Expected {want} argument{}, got {got}",
if *want == 1 { "" } else { "s" }
)
}
ErrorKind::PatFailed(pattern) => {
Error::PatFailed(pattern) => {
write!(f, "Failed to match pattern {pattern}")
}
ErrorKind::MatchNonexhaustive => {
Error::MatchNonexhaustive => {
write!(f, "Fell through a non-exhaustive match expression!")
}
ErrorKind::BuiltinError(s) => write!(f, "{s}"),
Error::BuiltinDebug(s) => write!(f, "DEBUG: {s}"),
}
}
}

View File

@ -2,10 +2,8 @@
use collect_upvars::collect_upvars;
use crate::error::ErrorKind;
use super::{Callable, ConValue, Environment, Error, IResult, Interpret, pattern};
use cl_ast::{Function as FnDecl, Sym};
use super::{Callable, ConValue, Environment, Error, IResult, Interpret};
use cl_ast::{Function as FnDecl, Param, Sym};
use std::{
cell::{Ref, RefCell},
collections::HashMap,
@ -14,7 +12,7 @@ use std::{
pub mod collect_upvars;
type Upvars = HashMap<Sym, ConValue>;
type Upvars = HashMap<Sym, Option<ConValue>>;
/// Represents a block of code which persists inside the Interpreter
#[derive(Clone, Debug)]
@ -50,30 +48,33 @@ impl Callable for Function {
name
}
fn call(&self, env: &mut Environment, args: &[ConValue]) -> IResult<ConValue> {
let FnDecl { name, gens: _, 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() });
}
let Some(body) = body else {
return Err(Error::NotDefined(*name));
};
let upvars = self.upvars.take();
let mut env = env.with_frame("upvars", upvars);
env.push_frame("upvars", upvars);
// TODO: completely refactor data storage
let mut frame = env.frame("fn args");
for (name, value) in pattern::substitution(&frame, bind, ConValue::Tuple(args.into()))? {
frame.insert(name, value);
for (Param { mutability: _, name }, value) in bind.iter().zip(args) {
frame.insert(*name, Some(value.clone()));
}
let res = body.interpret(&mut frame);
drop(frame);
if let Some(upvars) = env.pop_values() {
if let Some((upvars, _)) = env.pop_frame() {
self.upvars.replace(upvars);
}
match res {
Err(Error { kind: ErrorKind::Return(value), .. }) => Ok(value),
Err(Error { kind: ErrorKind::Break(value), .. }) => Err(Error::BadBreak(value)),
other => other,
Err(Error::Return(value)) => Ok(value),
Err(Error::Break(value)) => Err(Error::BadBreak(value)),
result => result,
}
}
}

View File

@ -1,19 +1,16 @@
//! Collects the "Upvars" of a function at the point of its creation, allowing variable capture
use crate::env::Environment;
use cl_ast::{
Function, Let, Path, PathPart, Pattern, Sym,
ast_visitor::{visit::*, walk::Walk},
};
use crate::{convalue::ConValue, env::Environment};
use cl_ast::{ast_visitor::visit::*, Function, Let, Param, Path, PathPart, Pattern, Sym};
use std::collections::{HashMap, HashSet};
pub fn collect_upvars(f: &Function, env: &Environment) -> super::Upvars {
CollectUpvars::new(env).visit(f).finish_copied()
CollectUpvars::new(env).get_upvars(f)
}
#[derive(Clone, Debug)]
pub struct CollectUpvars<'env> {
env: &'env Environment,
upvars: HashMap<Sym, usize>,
upvars: HashMap<Sym, Option<ConValue>>,
blacklist: HashSet<Sym>,
}
@ -21,17 +18,9 @@ impl<'env> CollectUpvars<'env> {
pub fn new(env: &'env Environment) -> Self {
Self { upvars: HashMap::new(), blacklist: HashSet::new(), env }
}
pub fn finish(&mut self) -> HashMap<Sym, usize> {
std::mem::take(&mut self.upvars)
}
pub fn finish_copied(&mut self) -> super::Upvars {
let Self { env, upvars, blacklist: _ } = self;
std::mem::take(upvars)
.into_iter()
.filter_map(|(k, v)| env.get_id(v).cloned().map(|v| (k, v)))
.collect()
pub fn get_upvars(mut self, f: &cl_ast::Function) -> HashMap<Sym, Option<ConValue>> {
self.visit_function(f);
self.upvars
}
pub fn add_upvar(&mut self, name: &Sym) {
@ -39,42 +28,61 @@ impl<'env> CollectUpvars<'env> {
if blacklist.contains(name) || upvars.contains_key(name) {
return;
}
if let Ok(place) = env.id_of(*name) {
upvars.insert(*name, place);
if let Ok(upvar) = env.get_local(*name) {
upvars.insert(*name, Some(upvar));
}
}
pub fn bind_name(&mut self, name: &Sym) {
self.blacklist.insert(*name);
}
pub fn scope(&mut self, f: impl Fn(&mut CollectUpvars<'env>)) {
let blacklist = self.blacklist.clone();
// visit the scope
f(self);
// restore the blacklist
self.blacklist = blacklist;
}
}
impl<'a> Visit<'a> for CollectUpvars<'_> {
fn visit_block(&mut self, b: &'a cl_ast::Block) {
self.scope(|cu| b.children(cu));
let blacklist = self.blacklist.clone();
// visit the block
let cl_ast::Block { stmts } = b;
stmts.iter().for_each(|s| self.visit_stmt(s));
// restore the blacklist
self.blacklist = blacklist;
}
fn visit_let(&mut self, l: &'a cl_ast::Let) {
let Let { mutable, name, ty, init } = l;
self.visit_mutability(mutable);
ty.visit_in(self);
if let Some(ty) = ty {
self.visit_ty(ty);
}
// visit the initializer, which may use the bound name
init.visit_in(self);
if let Some(init) = init {
self.visit_expr(init)
}
// a bound name can never be an upvar
self.visit_pattern(name);
}
fn visit_function(&mut self, f: &'a cl_ast::Function) {
let Function { name: _, sign: _, bind, body } = f;
// parameters can never be upvars
for Param { mutability: _, name } in bind {
self.bind_name(name);
}
if let Some(body) = body {
self.visit_block(body);
}
}
fn visit_for(&mut self, f: &'a cl_ast::For) {
let cl_ast::For { bind, cond, pass, fail } = f;
self.visit_expr(cond);
self.visit_else(fail);
self.bind_name(bind); // TODO: is bind only bound in the pass block?
self.visit_block(pass);
}
fn visit_path(&mut self, p: &'a cl_ast::Path) {
// TODO: path resolution in environments
let Path { absolute: false, parts } = p else {
@ -95,18 +103,32 @@ impl<'a> Visit<'a> for CollectUpvars<'_> {
}
}
fn visit_pattern(&mut self, value: &'a cl_ast::Pattern) {
match value {
Pattern::Name(name) => {
self.bind_name(name);
fn visit_pattern(&mut self, p: &'a cl_ast::Pattern) {
match p {
Pattern::Path(path) => {
if let [PathPart::Ident(name)] = path.parts.as_slice() {
self.bind_name(name)
}
}
Pattern::Literal(literal) => self.visit_literal(literal),
Pattern::Ref(mutability, pattern) => {
self.visit_mutability(mutability);
self.visit_pattern(pattern);
}
Pattern::Tuple(patterns) => {
patterns.iter().for_each(|p| self.visit_pattern(p));
}
Pattern::Array(patterns) => {
patterns.iter().for_each(|p| self.visit_pattern(p));
}
Pattern::Struct(path, items) => {
self.visit_path(path);
items.iter().for_each(|(_name, bind)| {
bind.as_ref().inspect(|bind| {
self.visit_pattern(bind);
});
});
}
Pattern::RangeExc(_, _) | Pattern::RangeInc(_, _) => {}
_ => value.children(self),
}
}
fn visit_match_arm(&mut self, value: &'a cl_ast::MatchArm) {
// MatchArms bind variables with a very small local scope
self.scope(|cu| value.children(cu));
}
}

File diff suppressed because it is too large Load Diff

View File

@ -5,11 +5,11 @@
use cl_ast::Sym;
use convalue::ConValue;
use env::Environment;
use error::{Error, ErrorKind, IResult};
use error::{Error, IResult};
use interpret::Interpret;
/// Callable types can be called from within a Conlang program
pub trait Callable {
pub trait Callable: std::fmt::Debug {
/// Calls this [Callable] in the provided [Environment], with [ConValue] args \
/// The Callable is responsible for checking the argument count and validating types
fn call(&self, interpreter: &mut Environment, args: &[ConValue]) -> IResult<ConValue>;
@ -23,651 +23,10 @@ pub mod interpret;
pub mod function;
pub mod constructor {
use cl_ast::Sym;
use crate::{
Callable,
convalue::ConValue,
env::Environment,
error::{Error, IResult},
};
#[derive(Clone, Copy, Debug)]
pub struct Constructor {
pub name: Sym,
pub arity: u32,
}
impl Callable for Constructor {
fn call(&self, _env: &mut Environment, args: &[ConValue]) -> IResult<ConValue> {
let &Self { name, arity } = self;
if arity as usize == args.len() {
Ok(ConValue::TupleStruct(Box::new((
name.to_ref(),
args.into(),
))))
} else {
Err(Error::ArgNumber(arity as usize, args.len()))
}
}
fn name(&self) -> cl_ast::Sym {
"tuple-constructor".into()
}
}
}
pub mod closure;
pub mod builtin;
pub mod pattern;
pub mod env;
pub mod modules {
use crate::env::StackBinds;
use cl_ast::{PathPart, Sym};
use std::collections::HashMap;
/// Immutable object-oriented interface to a [ModuleTree]
#[derive(Clone, Copy, Debug)]
pub struct ModuleNode<'tree> {
tree: &'tree ModuleTree,
index: usize,
}
/// Mutable object-oriented interface to a [ModuleTree]
#[derive(Debug)]
pub struct ModuleNodeMut<'tree> {
tree: &'tree mut ModuleTree,
index: usize,
}
macro_rules! module_node_impl {
() => {
/// Gets the index from this node
pub fn index(self) -> usize {
self.index
}
/// Gets this node's parent
pub fn parent(self) -> Option<Self> {
let parent = self.tree.parent(self.index)?;
Some(Self { index: parent, ..self })
}
/// Gets the node's "encompassing Type"
pub fn selfty(self) -> Option<Self> {
let selfty = self.tree.selfty(self.index)?;
Some(Self { index: selfty, ..self })
}
/// Gets the child of this node with the given name
pub fn child(self, name: &Sym) -> Option<Self> {
let child = self.tree.child(self.index, name)?;
Some(Self { index: child, ..self })
}
/// Gets a stack value in this node with the given name
pub fn item(self, name: &Sym) -> Option<usize> {
self.tree.items(self.index)?.get(name).copied()
}
/// Returns true when this node represents type information
pub fn is_ty(self) -> Option<bool> {
self.tree.is_ty.get(self.index).copied()
}
/// Returns a reference to this node's children, if present
pub fn children(&self) -> Option<&HashMap<Sym, usize>> {
self.tree.children(self.index)
}
/// Returns a reference to this node's items, if present
pub fn items(&self) -> Option<&StackBinds> {
self.tree.items(self.index)
}
/// Traverses a path starting at this node
///
/// Returns a new node, and the unconsumed path portion.
pub fn find(self, path: &[PathPart]) -> (Self, &[PathPart]) {
let (index, path) = self.tree.find(self.index, path);
(Self { index, ..self }, path)
}
/// Traverses a path starting at this node
///
/// Returns an item address if the path terminated in an item.
pub fn find_item(&self, path: &[PathPart]) -> Option<usize> {
self.tree.find_item(self.index, path)
}
};
}
impl ModuleNode<'_> {
module_node_impl! {}
}
impl ModuleNodeMut<'_> {
module_node_impl! {}
/// Creates a new child in this node
pub fn add_child(self, name: Sym, is_ty: bool) -> Self {
let node = self.tree.add_child(self.index, name, is_ty);
self.tree.get_mut(node)
}
/// Creates an arbitrary edge in the module graph
pub fn add_import(&mut self, name: Sym, child: usize) {
self.tree.add_import(self.index, name, child)
}
pub fn add_imports(&mut self, binds: HashMap<Sym, usize>) {
self.tree.add_imports(self.index, binds)
}
/// Binds a new item in this node
pub fn add_item(&mut self, name: Sym, stack_index: usize) {
self.tree.add_item(self.index, name, stack_index)
}
/// Binds an entire stack frame in this node
pub fn add_items(&mut self, binds: StackBinds) {
self.tree.add_items(self.index, binds)
}
/// Constructs a borrowing [ModuleNode]
pub fn as_ref(&self) -> ModuleNode<'_> {
let Self { tree, index } = self;
ModuleNode { tree, index: *index }
}
}
#[derive(Clone, Debug)]
pub struct ModuleTree {
parents: Vec<usize>,
children: Vec<HashMap<Sym, usize>>,
items: Vec<StackBinds>,
is_ty: Vec<bool>,
}
impl ModuleTree {
/// Constructs a new ModuleTree with a single root module
pub fn new() -> Self {
Self {
parents: vec![0],
children: vec![HashMap::new()],
items: vec![HashMap::new()],
is_ty: vec![false],
}
}
/// Gets a borrowed handle to the node at `index`
pub fn get(&self, index: usize) -> ModuleNode<'_> {
ModuleNode { tree: self, index }
}
/// Gets a mutable handle to the node at `index`
pub fn get_mut(&mut self, index: usize) -> ModuleNodeMut<'_> {
ModuleNodeMut { tree: self, index }
}
/// Creates a new child in this node
pub fn add_child(&mut self, parent: usize, name: Sym, is_ty: bool) -> usize {
let index = self.parents.len();
self.children[parent].insert(name, index);
self.parents.push(parent);
self.children.push(HashMap::new());
self.is_ty.push(is_ty);
index
}
/// Binds a new item in this node
pub fn add_item(&mut self, node: usize, name: Sym, stack_index: usize) {
self.items[node].insert(name, stack_index);
}
/// Creates an arbitrary child edge
pub fn add_import(&mut self, parent: usize, name: Sym, child: usize) {
self.children[parent].insert(name, child);
}
/// Binds an entire stack frame in this node
pub fn add_items(&mut self, node: usize, binds: StackBinds) {
self.items[node].extend(binds);
}
/// Binds an arbitrary set of child edges
pub fn add_imports(&mut self, node: usize, binds: HashMap<Sym, usize>) {
self.children[node].extend(binds);
}
/// Gets this node's parent
pub fn parent(&self, node: usize) -> Option<usize> {
if node == 0 {
return None;
}
self.parents.get(node).copied()
}
/// Gets the node's "encompassing Type"
pub fn selfty(&self, node: usize) -> Option<usize> {
if self.is_ty[node] {
return Some(node);
}
self.selfty(self.parent(node)?)
}
/// Gets the child of this node with the given name
pub fn child(&self, node: usize, id: &Sym) -> Option<usize> {
self.children[node].get(id).copied()
}
/// Gets a stack value in this node with the given name
pub fn item(&self, node: usize, name: &Sym) -> Option<usize> {
self.items.get(node).and_then(|map| map.get(name).copied())
}
/// Returns a reference to this node's children, if present
pub fn children(&self, node: usize) -> Option<&HashMap<Sym, usize>> {
self.children.get(node)
}
/// Returns a reference to this node's items, if present
pub fn items(&self, node: usize) -> Option<&StackBinds> {
self.items.get(node)
}
/// Traverses a path starting at this node
///
/// Returns a new node, and the unconsumed path portion.
pub fn find<'p>(&self, node: usize, path: &'p [PathPart]) -> (usize, &'p [PathPart]) {
match path {
[PathPart::SuperKw, tail @ ..] => match self.parent(node) {
Some(node) => self.find(node, tail),
None => (node, path),
},
[PathPart::Ident(name), tail @ ..] => match self.child(node, name) {
Some(node) => self.find(node, tail),
None => (node, path),
},
[PathPart::SelfTy, tail @ ..] => match self.selfty(node) {
Some(node) => self.find(node, tail),
None => (node, path),
},
[] => (node, path),
}
}
/// Traverses a path starting at this node
///
/// Returns an item address if the path terminated in an item.
pub fn find_item(&self, node: usize, path: &[PathPart]) -> Option<usize> {
let (node, [PathPart::Ident(name)]) = self.find(node, path) else {
return None;
};
self.item(node, name)
}
}
impl Default for ModuleTree {
fn default() -> Self {
Self::new()
}
}
}
pub mod collector {
use std::ops::{Deref, DerefMut};
use crate::{
convalue::ConValue,
env::Environment,
modules::{ModuleNode, ModuleNodeMut},
};
use cl_ast::{
ast_visitor::{Visit, Walk},
*,
};
pub struct Collector<'env> {
module: usize,
env: &'env mut Environment,
}
impl Collector<'_> {
pub fn as_node(&self) -> ModuleNode<'_> {
self.env.modules().get(self.module)
}
pub fn as_node_mut(&mut self) -> ModuleNodeMut {
self.env.modules_mut().get_mut(self.module)
}
pub fn scope(&mut self, name: Sym, is_ty: bool, f: impl Fn(&mut Collector<'_>)) {
let module = match self.as_node_mut().child(&name) {
Some(m) => m,
None => self.as_node_mut().add_child(name, is_ty),
}
.index();
let mut frame = self.env.frame(name.to_ref());
f(&mut Collector { env: &mut frame, module });
let binds = frame.into_binds().unwrap_or_default();
self.modules_mut().add_items(module, binds);
}
pub fn in_foreign_scope<F, T>(&mut self, path: &[PathPart], f: F) -> Option<T>
where F: Fn(&mut Collector<'_>) -> T {
let (module, []) = self.env.modules_mut().find(self.module, path) else {
return None;
};
let mut frame = self.env.frame("impl");
let out = f(&mut Collector { env: &mut frame, module });
let binds = frame.into_binds().unwrap_or_default();
self.env.modules_mut().add_items(module, binds);
Some(out)
}
}
impl<'env> Deref for Collector<'env> {
type Target = Environment;
fn deref(&self) -> &Self::Target {
self.env
}
}
impl DerefMut for Collector<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.env
}
}
impl<'a, 'env> Visit<'a> for Collector<'env> {
fn visit_file(&mut self, value: &'a File) {
let mut sorter = ItemSorter::default();
sorter.visit(value);
sorter.visit_all(self);
}
fn visit_block(&mut self, value: &'a Block) {
let mut sorter = ItemSorter::default();
sorter.visit(value);
sorter.visit_all(self);
}
fn visit_module(&mut self, value: &'a cl_ast::Module) {
self.scope(value.name, false, |scope| value.children(scope));
}
fn visit_alias(&mut self, value: &'a cl_ast::Alias) {
let Alias { name, from } = value;
match from.as_ref().map(Box::as_ref) {
Some(Ty { kind: TyKind::Path(path), .. }) => {
let mut node = if path.absolute {
self.modules_mut().get_mut(0)
} else {
self.as_node_mut()
};
if let Some(item) = node.find_item(&path.parts) {
node.add_item(*name, item);
}
}
Some(other) => todo!("Type expressions in the collector: {other}"),
None => self.scope(*name, true, |_| {}),
}
}
fn visit_enum(&mut self, value: &'a cl_ast::Enum) {
let Enum { name, gens: _, variants } = value;
self.scope(*name, true, |frame| {
for (idx, Variant { name, kind, body }) in variants.iter().enumerate() {
frame.visit(body);
frame.scope(*name, false, |frame| {
frame.bind("__discriminant", idx as isize);
match kind {
StructKind::Empty => {
frame.insert_tup_constructor("call".into(), 0);
frame.bind("__nmemb", ConValue::Int(0));
}
StructKind::Tuple(args) => {
// Constructs the AST from scratch. TODO: This, better.
frame.insert_tup_constructor("call".into(), args.len());
frame.bind("__nmemb", ConValue::Int(args.len() as _));
}
StructKind::Struct(members) => {
// TODO: more precise type checking of structs
for (idx, memb) in members.iter().enumerate() {
let StructMember { vis: _, name, ty: _ } = memb;
frame.bind(*name, idx as isize);
}
frame.bind("__nmemb", ConValue::Int(members.len() as _));
}
}
});
}
});
}
fn visit_struct(&mut self, value: &'a cl_ast::Struct) {
let Struct { name, gens: _, kind } = value;
self.scope(*name, true, |frame| {
match kind {
StructKind::Empty => {
frame.insert_tup_constructor("call".into(), 0);
frame.bind("__nmemb", ConValue::Int(0));
}
StructKind::Tuple(args) => {
// Constructs the AST from scratch. TODO: This, better.
frame.insert_tup_constructor("call".into(), args.len());
frame.bind("__nmemb", ConValue::Int(args.len() as _));
}
StructKind::Struct(members) => {
// TODO: more precise type checking of structs
for (idx, memb) in members.iter().enumerate() {
let StructMember { vis: _, name, ty: _ } = memb;
frame.bind(*name, idx as isize);
}
frame.bind("__nmemb", ConValue::Int(members.len() as _));
}
}
});
}
fn visit_const(&mut self, value: &'a cl_ast::Const) {
let Const { name, ty: _, init } = value;
self.visit(init);
self.bind(*name, ());
}
fn visit_static(&mut self, value: &'a cl_ast::Static) {
let Static { mutable: _, name, ty: _, init } = value;
self.visit(init);
self.bind(*name, ());
}
fn visit_function(&mut self, value: &'a cl_ast::Function) {
let Function { name, gens: _, sign: _, bind: _, body } = value;
self.scope(*name, false, |scope| {
scope.visit(body);
let f = crate::function::Function::new(value);
scope.bind("call", f);
});
}
fn visit_impl(&mut self, value: &'a cl_ast::Impl) {
let Impl { gens: _, target: ImplKind::Type(Ty { kind: TyKind::Path(name), .. }), body } =
value
else {
eprintln!("TODO: impl X for Ty");
return;
};
self.in_foreign_scope(&name.parts, |scope| {
body.visit_in(scope);
});
}
fn visit_use(&mut self, value: &'a cl_ast::Use) {
fn traverse(dest: &mut Collector<'_>, node: usize, tree: &UseTree) {
match tree {
UseTree::Tree(ts) => ts.iter().for_each(|tree| traverse(dest, node, tree)),
UseTree::Path(PathPart::Ident(name), tree) => {
if let (node, []) = dest.modules().find(node, &[PathPart::Ident(*name)]) {
traverse(dest, node, tree)
}
}
UseTree::Path(PathPart::SuperKw, tree) => {
if let Some(node) = dest.modules().parent(node) {
traverse(dest, node, tree)
}
}
UseTree::Path(PathPart::SelfTy, tree) => {
if let Some(node) = dest.modules().selfty(node) {
traverse(dest, node, tree)
}
}
UseTree::Alias(name, as_name) => {
if let Some(child) = dest.modules().child(node, name) {
dest.as_node_mut().add_import(*as_name, child);
}
if let Some(item) = dest.modules().item(node, name) {
dest.as_node_mut().add_item(*as_name, item);
}
}
UseTree::Name(name) => {
if let Some(child) = dest.modules().child(node, name) {
dest.as_node_mut().add_import(*name, child);
}
if let Some(item) = dest.modules().item(node, name) {
dest.as_node_mut().add_item(*name, item);
}
}
UseTree::Glob => {
let &mut Collector { module, ref mut env } = dest;
if let Some(children) = env.modules().children(node) {
for (name, index) in children.clone() {
env.modules_mut().add_import(module, name, index);
}
}
if let Some(items) = env.modules().items(node).cloned() {
env.modules_mut().add_items(node, items);
}
}
}
}
let Use { absolute, tree } = value;
let node = if *absolute { 0 } else { self.module };
traverse(self, node, tree);
}
}
// fn make_tuple_constructor(name: Sym, args: &[Ty]) -> ConValue {
// let span = match (
// args.first().map(|a| a.span.head),
// args.last().map(|a| a.span.tail),
// ) {
// (Some(head), Some(tail)) => Span(head, tail),
// _ => Span::dummy(),
// };
// let constructor = Function {
// name,
// gens: Default::default(),
// sign: TyFn {
// args: Ty { kind: TyKind::Tuple(TyTuple { types: args.to_vec() }), span }.into(),
// rety: Some(Ty { span: Span::dummy(), kind: TyKind::Path(Path::from(name))
// }.into()), },
// bind: Pattern::Tuple(
// args.iter()
// .enumerate()
// .map(|(idx, _)| Pattern::Name(idx.to_string().into()))
// .collect(),
// ),
// body: None,
// };
// // ConValue::TupleConstructor(crate::constructor::Constructor {ind})
// todo!("Tuple constructor {constructor}")
// }
/// Sorts items
#[derive(Debug, Default)]
struct ItemSorter<'ast> {
modules: Vec<&'ast Module>,
structs: Vec<&'ast Struct>,
enums: Vec<&'ast Enum>,
aliases: Vec<&'ast Alias>,
consts: Vec<&'ast Const>,
statics: Vec<&'ast Static>,
functions: Vec<&'ast Function>,
impls: Vec<&'ast Impl>,
imports: Vec<&'ast Use>,
}
impl<'a> ItemSorter<'a> {
fn visit_all<V: Visit<'a>>(&self, v: &mut V) {
let Self {
modules,
aliases,
enums,
structs,
consts,
statics,
functions,
impls,
imports,
} = self;
// 0
for item in modules {
item.visit_in(v);
}
// 1
for item in structs {
item.visit_in(v);
}
for item in enums {
item.visit_in(v);
}
for item in aliases {
item.visit_in(v);
}
// 2
// 5
for item in consts {
item.visit_in(v);
}
for item in statics {
item.visit_in(v);
}
for item in functions {
item.visit_in(v);
}
// 4
for item in impls {
item.visit_in(v);
}
// 3
for item in imports {
item.visit_in(v);
}
}
}
impl<'a> Visit<'a> for ItemSorter<'a> {
fn visit_module(&mut self, value: &'a cl_ast::Module) {
self.modules.push(value);
}
fn visit_alias(&mut self, value: &'a cl_ast::Alias) {
self.aliases.push(value);
}
fn visit_enum(&mut self, value: &'a cl_ast::Enum) {
self.enums.push(value);
}
fn visit_struct(&mut self, value: &'a cl_ast::Struct) {
self.structs.push(value);
}
fn visit_const(&mut self, value: &'a cl_ast::Const) {
self.consts.push(value);
}
fn visit_static(&mut self, value: &'a cl_ast::Static) {
self.statics.push(value);
}
fn visit_function(&mut self, value: &'a cl_ast::Function) {
self.functions.push(value);
}
fn visit_impl(&mut self, value: &'a cl_ast::Impl) {
self.impls.push(value);
}
fn visit_use(&mut self, value: &'a cl_ast::Use) {
self.imports.push(value);
}
}
}
pub mod error;
#[cfg(test)]

View File

@ -1,330 +0,0 @@
//! Unification algorithm for cl-ast [Pattern]s and [ConValue]s
//!
//! [`variables()`] returns a flat list of symbols that are bound by a given pattern
//! [`substitution()`] unifies a ConValue with a pattern, and produces a list of bound names
use crate::{
convalue::ConValue,
env::Environment,
error::{Error, IResult},
};
use cl_ast::{Literal, Pattern, Sym};
use std::collections::{HashMap, VecDeque};
/// Gets the path variables in the given Pattern
pub fn variables(pat: &Pattern) -> Vec<&Sym> {
fn patvars<'p>(set: &mut Vec<&'p Sym>, pat: &'p Pattern) {
match pat {
Pattern::Name(name) if name.to_ref() == "_" => {}
Pattern::Name(name) => set.push(name),
Pattern::Path(_) => {}
Pattern::Literal(_) => {}
Pattern::Rest(Some(pattern)) => patvars(set, pattern),
Pattern::Rest(None) => {}
Pattern::Ref(_, pattern) => patvars(set, pattern),
Pattern::RangeExc(_, _) => {}
Pattern::RangeInc(_, _) => {}
Pattern::Tuple(patterns) | Pattern::Array(patterns) => {
patterns.iter().for_each(|pat| patvars(set, pat))
}
Pattern::Struct(_path, items) => {
items.iter().for_each(|(name, pat)| match pat {
Some(pat) => patvars(set, pat),
None => set.push(name),
});
}
Pattern::TupleStruct(_path, items) => {
items.iter().for_each(|pat| patvars(set, pat));
}
}
}
let mut set = Vec::new();
patvars(&mut set, pat);
set
}
fn rest_binding<'pat>(
env: &Environment,
sub: &mut HashMap<Sym, ConValue>,
mut patterns: &'pat [Pattern],
mut values: VecDeque<ConValue>,
) -> IResult<Option<(&'pat Pattern, VecDeque<ConValue>)>> {
// Bind the head of the list
while let [pattern, tail @ ..] = patterns {
if matches!(pattern, Pattern::Rest(_)) {
break;
}
let value = values
.pop_front()
.ok_or_else(|| Error::PatFailed(Box::new(pattern.clone())))?;
append_sub(env, sub, pattern, value)?;
patterns = tail;
}
// Bind the tail of the list
while let [head @ .., pattern] = patterns {
if matches!(pattern, Pattern::Rest(_)) {
break;
}
let value = values
.pop_back()
.ok_or_else(|| Error::PatFailed(Box::new(pattern.clone())))?;
append_sub(env, sub, pattern, value)?;
patterns = head;
}
// Bind the ..rest of the list
match patterns {
[] | [Pattern::Rest(None)] => Ok(None),
[Pattern::Rest(Some(pattern))] => Ok(Some((pattern.as_ref(), values))),
_ => Err(Error::PatFailed(Box::new(Pattern::Array(patterns.into())))),
}
}
fn rest_binding_ref<'pat>(
env: &Environment,
sub: &mut HashMap<Sym, ConValue>,
mut patterns: &'pat [Pattern],
mut head: usize,
mut tail: usize,
) -> IResult<Option<(&'pat Pattern, usize, usize)>> {
// Bind the head of the list
while let [pattern, pat_tail @ ..] = patterns {
if matches!(pattern, Pattern::Rest(_)) {
break;
}
if head >= tail {
return Err(Error::PatFailed(Box::new(pattern.clone())));
}
append_sub(env, sub, pattern, ConValue::Ref(head))?;
head += 1;
patterns = pat_tail;
}
// Bind the tail of the list
while let [pat_head @ .., pattern] = patterns {
if matches!(pattern, Pattern::Rest(_)) {
break;
}
if head >= tail {
return Err(Error::PatFailed(Box::new(pattern.clone())));
};
append_sub(env, sub, pattern, ConValue::Ref(tail))?;
tail -= 1;
patterns = pat_head;
}
// Bind the ..rest of the list
match (patterns, tail - head) {
([], 0) | ([Pattern::Rest(None)], _) => Ok(None),
([Pattern::Rest(Some(pattern))], _) => Ok(Some((pattern.as_ref(), head, tail))),
_ => Err(Error::PatFailed(Box::new(Pattern::Array(patterns.into())))),
}
}
/// Appends a substitution to the provided table
pub fn append_sub(
env: &Environment,
sub: &mut HashMap<Sym, ConValue>,
pat: &Pattern,
value: ConValue,
) -> IResult<()> {
match (pat, value) {
(Pattern::Literal(Literal::Bool(a)), ConValue::Bool(b)) => {
(*a == b).then_some(()).ok_or(Error::NotAssignable())
}
(Pattern::Literal(Literal::Char(a)), ConValue::Char(b)) => {
(*a == b).then_some(()).ok_or(Error::NotAssignable())
}
(Pattern::Literal(Literal::Float(a)), ConValue::Float(b)) => (f64::from_bits(*a) == b)
.then_some(())
.ok_or(Error::NotAssignable()),
(Pattern::Literal(Literal::Int(a)), ConValue::Int(b)) => {
(b == *a as _).then_some(()).ok_or(Error::NotAssignable())
}
(Pattern::Literal(Literal::String(a)), ConValue::String(b)) => {
(*a == *b).then_some(()).ok_or(Error::NotAssignable())
}
(Pattern::Literal(_), _) => Err(Error::NotAssignable()),
(Pattern::Rest(Some(pat)), value) => match (pat.as_ref(), value) {
(Pattern::Literal(Literal::Int(a)), ConValue::Int(b)) => {
(b < *a as _).then_some(()).ok_or(Error::NotAssignable())
}
(Pattern::Literal(Literal::Char(a)), ConValue::Char(b)) => {
(b < *a as _).then_some(()).ok_or(Error::NotAssignable())
}
(Pattern::Literal(Literal::Bool(a)), ConValue::Bool(b)) => {
(!b & *a).then_some(()).ok_or(Error::NotAssignable())
}
(Pattern::Literal(Literal::Float(a)), ConValue::Float(b)) => {
(b < *a as _).then_some(()).ok_or(Error::NotAssignable())
}
(Pattern::Literal(Literal::String(a)), ConValue::String(b)) => {
(&*b < a).then_some(()).ok_or(Error::NotAssignable())
}
_ => Err(Error::NotAssignable()),
},
(Pattern::Name(name), _) if "_".eq(&**name) => Ok(()),
(Pattern::Name(name), value) => {
sub.insert(*name, value);
Ok(())
}
(Pattern::Ref(_, pat), ConValue::Ref(r)) => match env.get_id(r) {
Some(value) => append_sub(env, sub, pat, value.clone()),
None => Err(Error::PatFailed(pat.clone())),
},
(Pattern::Ref(_, pat), ConValue::Slice(head, len)) => {
let mut values = Vec::with_capacity(len);
for idx in head..(head + len) {
values.push(env.get_id(idx).cloned().ok_or(Error::StackOverflow(idx))?);
}
append_sub(env, sub, pat, ConValue::Array(values.into_boxed_slice()))
}
(Pattern::RangeExc(head, tail), value) => match (head.as_ref(), tail.as_ref(), value) {
(
Pattern::Literal(Literal::Int(a)),
Pattern::Literal(Literal::Int(c)),
ConValue::Int(b),
) => (*a as isize <= b as _ && b < *c as isize)
.then_some(())
.ok_or(Error::NotAssignable()),
(
Pattern::Literal(Literal::Char(a)),
Pattern::Literal(Literal::Char(c)),
ConValue::Char(b),
) => (*a <= b && b < *c)
.then_some(())
.ok_or(Error::NotAssignable()),
(
Pattern::Literal(Literal::Float(a)),
Pattern::Literal(Literal::Float(c)),
ConValue::Float(b),
) => (f64::from_bits(*a) <= b && b < f64::from_bits(*c))
.then_some(())
.ok_or(Error::NotAssignable()),
(
Pattern::Literal(Literal::String(a)),
Pattern::Literal(Literal::String(c)),
ConValue::String(b),
) => (a.as_str() <= b.to_ref() && b.to_ref() < c.as_str())
.then_some(())
.ok_or(Error::NotAssignable()),
_ => Err(Error::NotAssignable()),
},
(Pattern::RangeInc(head, tail), value) => match (head.as_ref(), tail.as_ref(), value) {
(
Pattern::Literal(Literal::Int(a)),
Pattern::Literal(Literal::Int(c)),
ConValue::Int(b),
) => (*a as isize <= b && b <= *c as isize)
.then_some(())
.ok_or(Error::NotAssignable()),
(
Pattern::Literal(Literal::Char(a)),
Pattern::Literal(Literal::Char(c)),
ConValue::Char(b),
) => (*a <= b && b <= *c)
.then_some(())
.ok_or(Error::NotAssignable()),
(
Pattern::Literal(Literal::Float(a)),
Pattern::Literal(Literal::Float(c)),
ConValue::Float(b),
) => (f64::from_bits(*a) <= b && b <= f64::from_bits(*c))
.then_some(())
.ok_or(Error::NotAssignable()),
(
Pattern::Literal(Literal::String(a)),
Pattern::Literal(Literal::String(c)),
ConValue::String(b),
) => (a.as_str() <= b.to_ref() && b.to_ref() <= c.as_str())
.then_some(())
.ok_or(Error::NotAssignable()),
_ => Err(Error::NotAssignable()),
},
(Pattern::Array(patterns), ConValue::Array(values)) => {
match rest_binding(env, sub, patterns, values.into_vec().into())? {
Some((pattern, values)) => {
append_sub(env, sub, pattern, ConValue::Array(Vec::from(values).into()))
}
_ => Ok(()),
}
}
(Pattern::Array(patterns), ConValue::Slice(head, len)) => {
match rest_binding_ref(env, sub, patterns, head, head + len)? {
Some((pat, head, tail)) => {
append_sub(env, sub, pat, ConValue::Slice(head, tail - head))
}
None => Ok(()),
}
}
(Pattern::Tuple(patterns), ConValue::Empty) if patterns.is_empty() => Ok(()),
(Pattern::Tuple(patterns), ConValue::Tuple(values)) => {
match rest_binding(env, sub, patterns, values.into_vec().into())? {
Some((pattern, values)) => {
append_sub(env, sub, pattern, ConValue::Tuple(Vec::from(values).into()))
}
_ => Ok(()),
}
}
(Pattern::TupleStruct(path, patterns), ConValue::TupleStruct(parts)) => {
let (id, values) = *parts;
let tid = path
.as_sym()
.ok_or_else(|| Error::PatFailed(pat.clone().into()))?;
if id != tid.to_ref() {
return Err(Error::PatFailed(pat.clone().into()));
}
match rest_binding(env, sub, patterns, values.into_vec().into())? {
Some((pattern, values)) => {
append_sub(env, sub, pattern, ConValue::Tuple(Vec::from(values).into()))
}
_ => Ok(()),
}
}
(Pattern::Struct(path, patterns), ConValue::Struct(parts)) => {
let (id, mut values) = *parts;
let tid = path
.as_sym()
.ok_or_else(|| Error::PatFailed(pat.clone().into()))?;
if id != tid.to_ref() {
return Err(Error::PatFailed(pat.clone().into()));
}
for (name, pat) in patterns {
let value = values.remove(name).ok_or(Error::TypeError())?;
match pat {
Some(pat) => append_sub(env, sub, pat, value)?,
None => {
sub.insert(*name, value);
}
}
}
Ok(())
}
_ => {
// eprintln!("Could not match pattern `{pat}` with value `{value}`!");
Err(Error::NotAssignable())
}
}
}
/// Constructs a substitution from a pattern and a value
pub fn substitution(
env: &Environment,
pat: &Pattern,
value: ConValue,
) -> IResult<HashMap<Sym, ConValue>> {
let mut sub = HashMap::new();
append_sub(env, &mut sub, pat, value)?;
Ok(sub)
}

View File

@ -1,5 +1,5 @@
#![allow(unused_imports)]
use crate::{Interpret, convalue::ConValue, env::Environment};
use crate::{convalue::ConValue, env::Environment, Interpret};
use cl_ast::*;
use cl_lexer::Lexer;
use cl_parser::Parser;
@ -71,7 +71,7 @@ mod macros {
///
/// Returns a `Result<`[`Block`]`, ParseError>`
pub macro block($($t:tt)*) {
Block::parse(&mut Parser::new("test", Lexer::new(stringify!({ $($t)* }))))
Block::parse(&mut Parser::new(Lexer::new(stringify!({ $($t)* }))))
}
/// Evaluates a block of code in the given environment
@ -403,7 +403,7 @@ mod operators {
env_eq!(env.is_10_ne_20, true); // !=
env_eq!(env.is_10_ge_20, false); // >=
env_eq!(env.is_10_gt_20, false); // >
// Equal to
// Equal to
env_eq!(env.is_10_lt_10, false);
env_eq!(env.is_10_le_10, true);
env_eq!(env.is_10_eq_10, true);
@ -530,25 +530,6 @@ mod control_flow {
env_eq!(env.evaluated, "fail");
}
#[test]
fn while_evaluates_fail_block_on_false() {
let mut env = Default::default();
assert_eval!(env,
let cond = true;
let evaluated = while cond { cond = false } else { true }
);
env_eq!(env.evaluated, true);
}
#[test]
fn while_does_not_evaluate_fail_block_on_break() {
let mut env = Default::default();
assert_eval!(env,
let evaluated = while true { break true } else { false }
);
env_eq!(env.evaluated, true);
}
#[test]
fn match_evaluates_in_order() {
let mut env = Default::default();

View File

@ -15,8 +15,8 @@ mod tests;
pub mod lexer_iter {
//! Iterator over a [`Lexer`], returning [`LResult<Token>`]s
use super::{
Lexer, Token,
error::{LResult, Reason},
Lexer, Token,
};
/// Iterator over a [`Lexer`], returning [`LResult<Token>`]s
@ -374,7 +374,6 @@ impl Lexer<'_> {
impl Lexer<'_> {
fn int_with_base(&mut self) -> LResult<Token> {
match self.peek() {
Ok('~') => self.consume()?.digits::<36>(),
Ok('x') => self.consume()?.digits::<16>(),
Ok('d') => self.consume()?.digits::<10>(),
Ok('o') => self.consume()?.digits::<8>(),

View File

@ -1,6 +1,5 @@
use super::*;
use cl_ast::{Expr, Sym};
use cl_lexer::error::{Error as LexError, Reason};
use std::fmt::Display;
pub type PResult<T> = Result<T, Error>;
@ -8,7 +7,6 @@ pub type PResult<T> = Result<T, Error>;
/// Contains information about [Parser] errors
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
pub in_file: Sym,
pub reason: ErrorKind,
pub while_parsing: Parsing,
pub loc: Loc,
@ -31,7 +29,6 @@ pub enum ErrorKind {
ExpectedParsing {
want: Parsing,
},
InvalidPattern(Box<Expr>),
/// Indicates unfinished code
Todo(&'static str),
}
@ -60,7 +57,6 @@ pub enum Parsing {
Item,
ItemKind,
Generics,
Alias,
Const,
Static,
@ -97,7 +93,6 @@ pub enum Parsing {
Expr,
ExprKind,
Closure,
Assign,
AssignKind,
Binary,
@ -132,18 +127,13 @@ pub enum Parsing {
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { in_file, reason, while_parsing, loc } = self;
let Self { reason, while_parsing, loc } = self;
match reason {
// TODO entries are debug-printed
ErrorKind::Todo(_) => write!(f, "{in_file}:{loc} {reason} {while_parsing:?}"),
ErrorKind::Todo(_) => write!(f, "{loc} {reason} {while_parsing:?}"),
// lexical errors print their own higher-resolution loc info
ErrorKind::Lexical(e) => write!(f, "{e} (while parsing {while_parsing})"),
_ => {
if !in_file.is_empty() {
write!(f, "{in_file}:")?
}
write!(f, "{loc}: {reason} while parsing {while_parsing}")
}
_ => write!(f, "{loc} {reason} while parsing {while_parsing}"),
}
}
}
@ -158,7 +148,6 @@ impl Display for ErrorKind {
ErrorKind::Unexpected(t) => write!(f, "Encountered unexpected token `{t}`"),
ErrorKind::ExpectedToken { want: e, got: g } => write!(f, "Expected `{e}`, got `{g}`"),
ErrorKind::ExpectedParsing { want } => write!(f, "Expected {want}"),
ErrorKind::InvalidPattern(got) => write!(f, "Got invalid `{got}`"),
ErrorKind::Todo(unfinished) => write!(f, "TODO: {unfinished}"),
}
}
@ -178,7 +167,6 @@ impl Display for Parsing {
Parsing::MetaKind => "an attribute's arguments",
Parsing::Item => "an item",
Parsing::ItemKind => "an item",
Parsing::Generics => "a list of type arguments",
Parsing::Alias => "a type alias",
Parsing::Const => "a const item",
Parsing::Static => "a static variable",
@ -215,7 +203,6 @@ impl Display for Parsing {
Parsing::Expr => "an expression",
Parsing::ExprKind => "an expression",
Parsing::Closure => "an anonymous function",
Parsing::Assign => "an assignment",
Parsing::AssignKind => "an assignment operator",
Parsing::Binary => "a binary expression",

View File

@ -48,71 +48,51 @@ impl ModuleInliner {
}
/// Records an [I/O error](std::io::Error) for later
fn handle_io_error(&mut self, error: std::io::Error) -> Option<File> {
fn handle_io_error(&mut self, error: std::io::Error) -> ModuleKind {
self.io_errs.push((self.path.clone(), error));
None
ModuleKind::Outline
}
/// Records a [parse error](crate::error::Error) for later
fn handle_parse_error(&mut self, error: crate::error::Error) -> Option<File> {
fn handle_parse_error(&mut self, error: crate::error::Error) -> ModuleKind {
self.parse_errs.push((self.path.clone(), error));
None
ModuleKind::Outline
}
}
impl Fold for ModuleInliner {
/// Traverses down the module tree, entering ever nested directories
fn fold_module(&mut self, m: Module) -> Module {
let Module { name, file } = m;
let Module { name, kind } = m;
self.path.push(&*name); // cd ./name
let file = self.fold_module_kind(file);
let kind = self.fold_module_kind(kind);
self.path.pop(); // cd ..
Module { name, file }
Module { name, kind }
}
}
impl ModuleInliner {
/// Attempts to read and parse a file for every module in the tree
fn fold_module_kind(&mut self, m: Option<File>) -> Option<File> {
use std::borrow::Cow;
if let Some(f) = m {
return Some(self.fold_file(f));
fn fold_module_kind(&mut self, m: ModuleKind) -> ModuleKind {
if let ModuleKind::Inline(f) = m {
return ModuleKind::Inline(self.fold_file(f));
}
// cd path/mod.cl
self.path.set_extension("cl");
let mut used_path: Cow<Path> = Cow::Borrowed(&self.path);
let file = match std::fs::read_to_string(&self.path) {
Err(error) => {
let Some(basename) = self.path.file_name() else {
return self.handle_io_error(error);
};
used_path = Cow::Owned(
self.path
.parent()
.and_then(Path::parent)
.map(|path| path.join(basename))
.unwrap_or_default(),
);
match std::fs::read_to_string(&used_path) {
Err(error) => return self.handle_io_error(error),
Ok(file) => file,
}
}
Err(error) => return self.handle_io_error(error),
Ok(file) => file,
};
match Parser::new(used_path.display().to_string(), Lexer::new(&file)).parse() {
Err(e) => self.handle_parse_error(e),
Ok(file) => {
self.path.set_extension("");
// The newly loaded module may need further inlining
Some(self.fold_file(file))
}
}
let kind = match Parser::new(Lexer::new(&file)).parse() {
Err(e) => return self.handle_parse_error(e),
Ok(file) => ModuleKind::Inline(file),
};
// cd path/mod
self.path.set_extension("");
// The newly loaded module may need further inlining
self.fold_module_kind(kind)
}
}

View File

@ -13,8 +13,6 @@ mod prec;
/// Parses a sequence of [Tokens](Token) into an [AST](cl_ast)
#[derive(Debug)]
pub struct Parser<'t> {
/// Name of the file being parsed
file: Sym,
/// Lazy tokenizer
lexer: Lexer<'t>,
/// Look-ahead buffer
@ -25,8 +23,8 @@ pub struct Parser<'t> {
/// Basic parser functionality
impl<'t> Parser<'t> {
pub fn new(filename: impl AsRef<str>, lexer: Lexer<'t>) -> Self {
Self { file: filename.as_ref().into(), loc: Loc::from(&lexer), lexer, next: None }
pub fn new(lexer: Lexer<'t>) -> Self {
Self { loc: Loc::from(&lexer), lexer, next: None }
}
/// Gets the location of the last consumed [Token]
@ -42,7 +40,7 @@ impl<'t> Parser<'t> {
/// Constructs an [Error]
pub fn error(&self, reason: ErrorKind, while_parsing: Parsing) -> Error {
Error { in_file: self.file, reason, while_parsing, loc: self.loc }
Error { reason, while_parsing, loc: self.loc }
}
/// Internal impl of peek and consume
@ -188,18 +186,11 @@ macro literal_like() {
/// Expands to a pattern which matches path-like [TokenKinds](TokenKind)
macro path_like() {
TokenKind::Super | TokenKind::SelfTy | TokenKind::Identifier | TokenKind::ColonColon
}
type Spanned<T> = (T, Span);
impl<'t, T: Parse<'t>> Parse<'t> for Spanned<T> {
fn parse(p: &mut Parser<'t>) -> PResult<Self> {
let head = p.loc();
let body = p.parse()?;
let tail = p.loc();
Ok((body, Span(head, tail)))
}
TokenKind::Super
| TokenKind::SelfKw
| TokenKind::SelfTy
| TokenKind::Identifier
| TokenKind::ColonColon
}
pub trait Parse<'t>: Sized {
@ -269,10 +260,9 @@ impl Parse<'_> for File {
Ok(_) => true,
Err(e) => Err(e)?,
} {
items.push(Item::parse(p)?);
let _ = p.match_type(TokenKind::Semi, Parsing::File);
items.push(Item::parse(p)?)
}
Ok(File { name: p.file.to_ref(), items })
Ok(File { items })
}
}
@ -302,13 +292,17 @@ impl Parse<'_> for MetaKind {
/// Parses data associated with a [Meta] attribute
fn parse(p: &mut Parser) -> PResult<MetaKind> {
const P: Parsing = Parsing::Meta;
let tuple = delim(sep(Parse::parse, TokenKind::Comma, PARENS.1, P), PARENS, P);
let lit_tuple = delim(
sep(Literal::parse, TokenKind::Comma, PARENS.1, P),
PARENS,
P,
);
Ok(match p.peek_kind(P) {
Ok(TokenKind::Eq) => {
p.consume_peeked();
MetaKind::Equals(p.parse()?)
MetaKind::Equals(Literal::parse(p)?)
}
Ok(TokenKind::LParen) => MetaKind::Func(tuple(p)?),
Ok(TokenKind::LParen) => MetaKind::Func(lit_tuple(p)?),
_ => MetaKind::Plain,
})
}
@ -326,7 +320,7 @@ impl Parse<'_> for Item {
attrs: Attrs::parse(p)?,
vis: Visibility::parse(p)?,
kind: ItemKind::parse(p)?,
span: Span(start, p.loc()),
extents: Span(start, p.loc()),
})
}
}
@ -337,35 +331,20 @@ impl Parse<'_> for ItemKind {
/// See also: [Item::parse]
fn parse(p: &mut Parser) -> PResult<Self> {
Ok(match p.peek_kind(Parsing::Item)? {
TokenKind::Type => ItemKind::Alias(p.parse()?),
TokenKind::Const => ItemKind::Const(p.parse()?),
TokenKind::Static => ItemKind::Static(p.parse()?),
TokenKind::Mod => ItemKind::Module(p.parse()?),
TokenKind::Fn => ItemKind::Function(p.parse()?),
TokenKind::Struct => ItemKind::Struct(p.parse()?),
TokenKind::Enum => ItemKind::Enum(p.parse()?),
TokenKind::Impl => ItemKind::Impl(p.parse()?),
TokenKind::Use => ItemKind::Use(p.parse()?),
TokenKind::Type => Alias::parse(p)?.into(),
TokenKind::Const => Const::parse(p)?.into(),
TokenKind::Static => Static::parse(p)?.into(),
TokenKind::Mod => Module::parse(p)?.into(),
TokenKind::Fn => Function::parse(p)?.into(),
TokenKind::Struct => Struct::parse(p)?.into(),
TokenKind::Enum => Enum::parse(p)?.into(),
TokenKind::Impl => Impl::parse(p)?.into(),
TokenKind::Use => Use::parse(p)?.into(),
t => Err(p.error(Unexpected(t), Parsing::Item))?,
})
}
}
impl Parse<'_> for Generics {
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
const P: Parsing = Parsing::Generics;
let vars = match p.peek_kind(P) {
Ok(TokenKind::Lt) => delim(
sep(Sym::parse, TokenKind::Comma, TokenKind::Gt, P),
(TokenKind::Lt, TokenKind::Gt),
P,
)(p)?,
_ => Vec::new(),
};
Ok(Generics { vars })
}
}
impl Parse<'_> for Alias {
/// Parses a [`type` alias](Alias)
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
@ -373,9 +352,9 @@ impl Parse<'_> for Alias {
p.consume_peeked();
let out = Ok(Alias {
name: Sym::parse(p)?,
to: Sym::parse(p)?,
from: if p.match_type(TokenKind::Eq, P).is_ok() {
Some(p.parse()?)
Some(Ty::parse(p)?.into())
} else {
None
},
@ -399,7 +378,7 @@ impl Parse<'_> for Const {
},
init: {
p.match_type(TokenKind::Eq, P)?;
p.parse()?
Expr::parse(p)?.into()
},
});
p.match_type(TokenKind::Semi, P)?;
@ -418,11 +397,11 @@ impl Parse<'_> for Static {
name: Sym::parse(p)?,
ty: {
p.match_type(TokenKind::Colon, P)?;
p.parse()?
Ty::parse(p)?.into()
},
init: {
p.match_type(TokenKind::Eq, P)?;
p.parse()?
Expr::parse(p)?.into()
},
});
p.match_type(TokenKind::Semi, P)?;
@ -435,22 +414,24 @@ impl Parse<'_> for Module {
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
p.consume_peeked();
Ok(Module {
name: Sym::parse(p)?,
file: {
const P: Parsing = Parsing::ModuleKind;
let inline = delim(Parse::parse, CURLIES, P);
Ok(Module { name: Sym::parse(p)?, kind: ModuleKind::parse(p)? })
}
}
match p.peek_kind(P)? {
TokenKind::LCurly => Some(inline(p)?),
TokenKind::Semi => {
p.consume_peeked();
None
}
got => Err(p.error(ExpectedToken { want: TokenKind::Semi, got }, P))?,
}
},
})
impl Parse<'_> for ModuleKind {
/// Parses the item list associated with a [Module], if present
fn parse(p: &mut Parser) -> PResult<ModuleKind> {
const P: Parsing = Parsing::ModuleKind;
let inline = delim(Parse::parse, CURLIES, P);
match p.peek_kind(P)? {
TokenKind::LCurly => Ok(ModuleKind::Inline(inline(p)?)),
TokenKind::Semi => {
p.consume_peeked();
Ok(ModuleKind::Outline)
}
got => Err(p.error(ExpectedToken { want: TokenKind::Semi, got }, P)),
}
}
}
@ -461,38 +442,38 @@ impl Parse<'_> for Function {
p.consume_peeked();
let name = Sym::parse(p)?;
let gens = Generics::parse(p)?;
let ((bind, types), span) = delim(Spanned::<FnSig>::parse, PARENS, P)(p)?;
let (bind, types) = delim(FnSig::parse, PARENS, P)(p)?;
let sign = TyFn {
args: Box::new(match types.len() {
0 => Ty { span, kind: TyKind::Empty, gens: Default::default() },
_ => Ty { span, kind: TyKind::Tuple(TyTuple { types }), gens: Default::default() },
}),
rety: Box::new(match p.match_type(TokenKind::Arrow, Parsing::TyFn) {
Ok(_) => Ty::parse(p)?,
Err(_) => Ty { span, kind: TyKind::Empty, gens: Generics { vars: vec![] } },
0 => TyKind::Empty,
_ => TyKind::Tuple(TyTuple { types }),
}),
rety: Ok(match p.match_type(TokenKind::Arrow, Parsing::TyFn) {
Ok(_) => Some(Ty::parse(p)?),
Err(_) => None,
})?
.map(Box::new),
};
Ok(Function {
name,
gens,
sign,
bind,
body: match p.peek_kind(P)? {
TokenKind::LCurly => Some(Block::parse(p)?),
TokenKind::Semi => {
p.consume_peeked();
None
}
_ => Some(Expr::parse(p)?),
t => Err(p.error(Unexpected(t), P))?,
},
})
}
}
type FnSig = (Pattern, Vec<Ty>);
type FnSig = (Vec<Param>, Vec<TyKind>);
impl Parse<'_> for FnSig {
/// Parses the parameter list of a Function
/// Parses the [parameters](Param) associated with a Function
fn parse(p: &mut Parser) -> PResult<FnSig> {
const P: Parsing = Parsing::Function;
let (mut params, mut types) = (vec![], vec![]);
@ -504,21 +485,20 @@ impl Parse<'_> for FnSig {
break;
}
}
Ok((Pattern::Tuple(params), types))
Ok((params, types))
}
}
type TypedParam = (Pattern, Ty);
type TypedParam = (Param, TyKind);
impl Parse<'_> for TypedParam {
/// Parses a single function parameter
fn parse(p: &mut Parser) -> PResult<(Pattern, Ty)> {
/// Parses a single function [parameter](Param)
fn parse(p: &mut Parser) -> PResult<(Param, TyKind)> {
Ok((
Pattern::parse(p)?,
if p.match_type(TokenKind::Colon, Parsing::Param).is_ok() {
Ty::parse(p)?
} else {
Ty { span: Span::dummy(), kind: TyKind::Infer, gens: Default::default() }
Param { mutability: Mutability::parse(p)?, name: Sym::parse(p)? },
{
p.match_type(TokenKind::Colon, Parsing::Param)?;
TyKind::parse(p)?
},
))
}
@ -528,7 +508,7 @@ impl Parse<'_> for Struct {
/// Parses a [`struct` definition](Struct)
fn parse(p: &mut Parser) -> PResult<Struct> {
p.match_type(TokenKind::Struct, Parsing::Struct)?;
Ok(Struct { name: Sym::parse(p)?, gens: Generics::parse(p)?, kind: StructKind::parse(p)? })
Ok(Struct { name: Sym::parse(p)?, kind: StructKind::parse(p)? })
}
}
@ -536,19 +516,22 @@ impl Parse<'_> for StructKind {
/// Parses the various [kinds of Struct](StructKind)
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
const P: Parsing = Parsing::StructKind;
Ok(match p.peek_kind(P) {
Ok(TokenKind::LParen) => StructKind::Tuple(delim(
Ok(match p.peek_kind(P)? {
TokenKind::LParen => StructKind::Tuple(delim(
sep(Ty::parse, TokenKind::Comma, PARENS.1, P),
PARENS,
P,
)(p)?),
Ok(TokenKind::LCurly) => StructKind::Struct(delim(
TokenKind::LCurly => StructKind::Struct(delim(
sep(StructMember::parse, TokenKind::Comma, CURLIES.1, P),
CURLIES,
P,
)(p)?),
Ok(_) | Err(Error { reason: ErrorKind::EndOfInput, .. }) => StructKind::Empty,
Err(e) => Err(e)?,
TokenKind::Semi => {
p.consume_peeked();
StructKind::Empty
}
got => Err(p.error(ExpectedToken { want: TokenKind::Semi, got }, P))?,
})
}
}
@ -572,20 +555,26 @@ impl Parse<'_> for Enum {
/// Parses an [`enum`](Enum) definition
fn parse(p: &mut Parser) -> PResult<Enum> {
p.match_type(TokenKind::Enum, Parsing::Enum)?;
Ok(Enum {
name: Sym::parse(p)?,
gens: Generics::parse(p)?,
variants: {
const P: Parsing = Parsing::EnumKind;
match p.peek_kind(P)? {
TokenKind::LCurly => delim(
sep(Variant::parse, TokenKind::Comma, TokenKind::RCurly, P),
CURLIES,
P,
)(p)?,
t => Err(p.error(Unexpected(t), P))?,
}
},
Ok(Enum { name: Sym::parse(p)?, kind: EnumKind::parse(p)? })
}
}
impl Parse<'_> for EnumKind {
/// Parses the various [kinds of Enum](EnumKind)
fn parse(p: &mut Parser<'_>) -> PResult<EnumKind> {
const P: Parsing = Parsing::EnumKind;
Ok(match p.peek_kind(P)? {
TokenKind::LCurly => EnumKind::Variants(delim(
sep(Variant::parse, TokenKind::Comma, TokenKind::RCurly, P),
CURLIES,
P,
)(p)?),
TokenKind::Semi => {
p.consume_peeked();
EnumKind::NoVariants
}
t => Err(p.error(Unexpected(t), P))?,
})
}
}
@ -593,19 +582,39 @@ impl Parse<'_> for Enum {
impl Parse<'_> for Variant {
/// Parses an [`enum`](Enum) [Variant]
fn parse(p: &mut Parser) -> PResult<Variant> {
let name = Sym::parse(p)?;
let kind;
let body;
Ok(Variant { name: Sym::parse(p)?, kind: VariantKind::parse(p)? })
}
}
if p.match_type(TokenKind::Eq, Parsing::Variant).is_ok() {
kind = StructKind::Empty;
body = Some(Box::new(Expr::parse(p)?));
} else {
kind = StructKind::parse(p)?;
body = None;
}
impl Parse<'_> for VariantKind {
/// Parses the various [kinds of Enum Variant](VariantKind)
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
const P: Parsing = Parsing::VariantKind;
Ok(match p.peek_kind(P)? {
TokenKind::Eq => {
p.match_type(TokenKind::Eq, P)?;
let tok = p.match_type(TokenKind::Literal, P)?;
Ok(Variant { name, kind, body })
VariantKind::CLike(match tok.data() {
TokenData::Integer(i) => *i,
_ => panic!("Expected token data for {tok:?} while parsing {P}"),
})
}
TokenKind::LCurly => VariantKind::Struct(delim(
sep(StructMember::parse, TokenKind::Comma, TokenKind::RCurly, P),
CURLIES,
P,
)(p)?),
TokenKind::LParen => {
let tup = Ty::parse(p)?;
if !matches!(tup.kind, TyKind::Tuple(_) | TyKind::Empty) {
Err(p.error(ErrorKind::ExpectedParsing { want: Parsing::TyTuple }, P))?
}
VariantKind::Tuple(tup)
}
_ => VariantKind::Plain,
})
}
}
@ -614,11 +623,7 @@ impl Parse<'_> for Impl {
const P: Parsing = Parsing::Impl;
p.match_type(TokenKind::Impl, P)?;
Ok(Impl {
gens: Generics::parse(p)?,
target: ImplKind::parse(p)?,
body: delim(File::parse, CURLIES, P)(p)?,
})
Ok(Impl { target: ImplKind::parse(p)?, body: delim(File::parse, CURLIES, P)(p)? })
}
}
@ -634,10 +639,9 @@ impl Parse<'_> for ImplKind {
Ok(ImplKind::Trait { impl_trait, for_type: Ty::parse(p)?.into() })
} else {
Err(Error {
in_file: p.file,
reason: ExpectedParsing { want: Parsing::Path },
while_parsing: P,
loc: target.span.head,
loc: target.extents.head,
})?
}
}
@ -669,7 +673,7 @@ impl Parse<'_> for UseTree {
CURLIES,
P,
)(p)?),
TokenKind::Super | TokenKind::Identifier => {
TokenKind::SelfKw | TokenKind::Super | TokenKind::Identifier => {
let name = PathPart::parse(p)?;
if p.match_type(TokenKind::ColonColon, P).is_ok() {
UseTree::Path(name, Box::new(UseTree::parse(p)?))
@ -696,9 +700,8 @@ impl Parse<'_> for Ty {
///
/// See also: [TyKind::parse]
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
let (kind, span) = p.parse()?;
let gens = p.parse()?;
Ok(Ty { span, kind, gens })
let start = p.loc();
Ok(Ty { kind: TyKind::parse(p)?, extents: Span(start, p.loc()) })
}
}
@ -714,10 +717,9 @@ impl Parse<'_> for TyKind {
TyKind::Never
}
TokenKind::Amp | TokenKind::AmpAmp => TyRef::parse(p)?.into(),
TokenKind::Star => TyPtr::parse(p)?.into(),
TokenKind::LBrack => {
p.match_type(BRACKETS.0, Parsing::TySlice)?;
let ty = p.parse()?;
let ty = TyKind::parse(p)?;
let (out, kind) = match p.match_type(TokenKind::Semi, Parsing::TyArray).is_ok() {
true => {
let literal = p.match_type(TokenKind::Literal, Parsing::TyArray)?;
@ -725,11 +727,14 @@ impl Parse<'_> for TyKind {
Err(p.error(Unexpected(TokenKind::Literal), Parsing::TyArray))?
};
(
TyKind::Array(TyArray { ty, count: count as _ }),
TyKind::Array(TyArray { ty: Box::new(ty), count: count as _ }),
Parsing::TyArray,
)
}
false => (TyKind::Slice(TySlice { ty }), Parsing::TySlice),
false => (
TyKind::Slice(TySlice { ty: Box::new(ty) }),
Parsing::TySlice,
),
};
p.match_type(BRACKETS.1, kind)?;
out
@ -742,14 +747,7 @@ impl Parse<'_> for TyKind {
}
}
TokenKind::Fn => TyFn::parse(p)?.into(),
path_like!() => {
let path = Path::parse(p)?;
if path.is_sinkhole() {
TyKind::Infer
} else {
TyKind::Path(path)
}
}
path_like!() => Path::parse(p)?.into(),
t => Err(p.error(Unexpected(t), P))?,
};
@ -761,7 +759,9 @@ impl Parse<'_> for TyTuple {
/// [TyTuple] = `(` ([Ty] `,`)* [Ty]? `)`
fn parse(p: &mut Parser) -> PResult<TyTuple> {
const P: Parsing = Parsing::TyTuple;
Ok(TyTuple { types: delim(sep(Ty::parse, TokenKind::Comma, PARENS.1, P), PARENS, P)(p)? })
Ok(TyTuple {
types: delim(sep(TyKind::parse, TokenKind::Comma, PARENS.1, P), PARENS, P)(p)?,
})
}
}
@ -778,16 +778,7 @@ impl Parse<'_> for TyRef {
}
p.consume_peeked();
}
Ok(TyRef { count, mutable: p.parse()?, to: p.parse()? })
}
}
impl Parse<'_> for TyPtr {
/// [TyPtr] = `*` [Ty]
fn parse(p: &mut Parser) -> PResult<TyPtr> {
const P: Parsing = Parsing::TyRef;
p.match_type(TokenKind::Star, P)?;
Ok(TyPtr { to: p.parse()? })
Ok(TyRef { count, mutable: Mutability::parse(p)?, to: Path::parse(p)? })
}
}
@ -797,21 +788,18 @@ impl Parse<'_> for TyFn {
const P: Parsing = Parsing::TyFn;
p.match_type(TokenKind::Fn, P)?;
let head = p.loc();
let args = delim(sep(Ty::parse, TokenKind::Comma, PARENS.1, P), PARENS, P)(p)?;
let span = Span(head, p.loc());
let args = delim(sep(TyKind::parse, TokenKind::Comma, PARENS.1, P), PARENS, P)(p)?;
Ok(TyFn {
args: Box::new(match args {
t if t.is_empty() => Ty { kind: TyKind::Empty, span, gens: Default::default() },
types => {
Ty { kind: TyKind::Tuple(TyTuple { types }), span, gens: Default::default() }
}
}),
rety: Box::new(match p.match_type(TokenKind::Arrow, Parsing::TyFn) {
Ok(_) => Ty::parse(p)?,
Err(_) => Ty { span, kind: TyKind::Empty, gens: Generics { vars: vec![] } },
t if t.is_empty() => TyKind::Empty,
types => TyKind::Tuple(TyTuple { types }),
}),
rety: match p.match_type(TokenKind::Arrow, Parsing::TyFn) {
Ok(_) => Some(Ty::parse(p)?),
Err(_) => None,
}
.map(Into::into),
})
}
}
@ -853,6 +841,7 @@ impl Parse<'_> for PathPart {
const P: Parsing = Parsing::PathPart;
let out = match p.peek_kind(P)? {
TokenKind::Super => PathPart::SuperKw,
TokenKind::SelfKw => PathPart::SelfKw,
TokenKind::SelfTy => PathPart::SelfTy,
TokenKind::Identifier => PathPart::Ident(Sym::parse(p)?),
t => return Err(p.error(Unexpected(t), P)),
@ -870,12 +859,15 @@ impl Parse<'_> for Stmt {
///
/// See also: [StmtKind::parse]
fn parse(p: &mut Parser) -> PResult<Stmt> {
let (kind, span) = Spanned::<StmtKind>::parse(p)?;
let semi = match p.match_type(TokenKind::Semi, Parsing::Stmt) {
Ok(_) => Semi::Terminated,
_ => Semi::Unterminated,
};
Ok(Stmt { span, kind, semi })
let start = p.loc();
Ok(Stmt {
kind: StmtKind::parse(p)?,
semi: match p.match_type(TokenKind::Semi, Parsing::Stmt) {
Ok(_) => Semi::Terminated,
_ => Semi::Unterminated,
},
extents: Span(start, p.loc()),
})
}
}
@ -886,8 +878,8 @@ impl Parse<'_> for StmtKind {
fn parse(p: &mut Parser) -> PResult<StmtKind> {
Ok(match p.peek_kind(Parsing::StmtKind)? {
TokenKind::Semi => StmtKind::Empty,
item_like!() => StmtKind::Item(p.parse()?),
_ => StmtKind::Expr(p.parse()?),
item_like!() => Item::parse(p)?.into(),
_ => Expr::parse(p)?.into(),
})
}
}
@ -896,40 +888,26 @@ impl Parse<'_> for StmtKind {
impl Parse<'_> for Expr {
/// Parses an [Expr]
///
/// See also: [ExprKind::parse]
fn parse(p: &mut Parser) -> PResult<Expr> {
prec::expr(p, 0)
let start = p.loc();
Ok(Expr { kind: ExprKind::parse(p)?, extents: Span(start, p.loc()) })
}
}
impl Parse<'_> for Closure {
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
let args = sep(
Pattern::parse,
TokenKind::Comma,
TokenKind::Bar,
Parsing::Closure,
);
let arg = match p.peek_kind(Parsing::Closure)? {
TokenKind::BarBar => {
p.consume_peeked();
Box::new(Pattern::Tuple(vec![]))
}
_ => Box::new(delim(
|p| args(p).map(Pattern::Tuple),
(TokenKind::Bar, TokenKind::Bar),
Parsing::Closure,
)(p)?),
};
let body = p.parse()?;
Ok(Closure { arg, body })
impl Parse<'_> for ExprKind {
/// Parses an [ExprKind] at the lowest precedence level
// Implementer's note: Do not call this from within [prec::exprkind]
fn parse(p: &mut Parser<'_>) -> PResult<ExprKind> {
prec::exprkind(p, 0)
}
}
impl Parse<'_> for Quote {
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
let quote = delim(
Expr::parse,
ExprKind::parse,
(TokenKind::Grave, TokenKind::Grave),
Parsing::ExprKind,
)(p)?
@ -942,15 +920,15 @@ impl Parse<'_> for Let {
fn parse(p: &mut Parser) -> PResult<Let> {
p.consume_peeked();
Ok(Let {
mutable: p.parse()?,
name: p.parse()?,
mutable: Mutability::parse(p)?,
name: Pattern::parse(p)?,
ty: if p.match_type(TokenKind::Colon, Parsing::Let).is_ok() {
Some(p.parse()?)
Some(Ty::parse(p)?.into())
} else {
None
},
init: if p.match_type(TokenKind::Eq, Parsing::Let).is_ok() {
Some(condition(p)?.into())
Some(Expr::parse(p)?.into())
} else {
None
},
@ -989,7 +967,7 @@ impl Parse<'_> for Fielder {
Ok(Fielder {
name: Sym::parse(p)?,
init: match p.match_type(TokenKind::Colon, P) {
Ok(_) => Some(p.parse()?),
Ok(_) => Some(Box::new(Expr::parse(p)?)),
Err(_) => None,
},
})
@ -1003,20 +981,16 @@ impl Parse<'_> for AddrOf {
match p.peek_kind(P)? {
TokenKind::Amp => {
p.consume_peeked();
Ok(AddrOf { mutable: p.parse()?, expr: p.parse()? })
Ok(AddrOf { mutable: Mutability::parse(p)?, expr: ExprKind::parse(p)?.into() })
}
TokenKind::AmpAmp => {
let start = p.loc();
p.consume_peeked();
Ok(AddrOf {
mutable: Mutability::Not,
expr: Expr {
kind: ExprKind::AddrOf(AddrOf {
mutable: Mutability::parse(p)?,
expr: p.parse()?,
}),
span: Span(start, p.loc()),
}
expr: ExprKind::AddrOf(AddrOf {
mutable: Mutability::parse(p)?,
expr: ExprKind::parse(p)?.into(),
})
.into(),
})
}
@ -1033,18 +1007,13 @@ impl Parse<'_> for Block {
}
}
/// Conditions (which precede curly-braced blocks) get special treatment
fn condition(p: &mut Parser) -> PResult<Expr> {
prec::expr(p, prec::Precedence::Condition.level())
}
impl Parse<'_> for While {
/// [While] = `while` [Expr] [Block] [Else]?
#[rustfmt::skip]
fn parse(p: &mut Parser) -> PResult<While> {
p.match_type(TokenKind::While, Parsing::While)?;
Ok(While {
cond: condition(p)?.into(),
cond: Expr::parse(p)?.into(),
pass: Block::parse(p)?.into(),
fail: Else::parse(p)?
})
@ -1057,7 +1026,7 @@ impl Parse<'_> for If {
fn parse(p: &mut Parser) -> PResult<If> {
p.match_type(TokenKind::If, Parsing::If)?;
Ok(If {
cond: condition(p)?.into(),
cond: Expr::parse(p)?.into(),
pass: Block::parse(p)?.into(),
fail: Else::parse(p)?,
})
@ -1065,13 +1034,16 @@ impl Parse<'_> for If {
}
impl Parse<'_> for For {
/// [For]: `for` [Pattern] `in` [Expr] [Block] [Else]?
/// [For]: `for` Pattern (TODO) `in` [Expr] [Block] [Else]?
#[rustfmt::skip]
fn parse(p: &mut Parser) -> PResult<For> {
p.match_type(TokenKind::For, Parsing::For)?;
let bind = Sym::parse(p)?;
p.match_type(TokenKind::In, Parsing::For)?;
Ok(For {
bind: delim(Parse::parse, (TokenKind::For, TokenKind::In), Parsing::For)(p)?,
cond: condition(p)?.into(),
pass: p.parse()?,
bind,
cond: Expr::parse(p)?.into(),
pass: Block::parse(p)?.into(),
fail: Else::parse(p)?,
})
}
@ -1083,7 +1055,7 @@ impl Parse<'_> for Else {
match p.peek_kind(Parsing::Else) {
Ok(TokenKind::Else) => {
p.consume_peeked();
Ok(Else { body: Some(p.parse()?) })
Ok(Expr::parse(p)?.into())
}
Ok(_) | Err(Error { reason: EndOfInput, .. }) => Ok(None.into()),
Err(e) => Err(e),
@ -1107,106 +1079,11 @@ impl Parse<'_> for Return {
}
}
fn pathpattern(p: &mut Parser<'_>) -> PResult<Pattern> {
const P: Parsing = Parsing::Pattern;
let name = Path::parse(p)?;
let struct_members = |p: &mut Parser| {
let name = p.parse()?;
let pat = if p.match_type(TokenKind::Colon, P).is_ok() {
Some(p.parse()?)
} else {
None
};
Ok((name, pat))
};
Ok(match p.peek_kind(Parsing::Pattern)? {
TokenKind::LCurly => Pattern::Struct(
name,
delim(
sep(struct_members, TokenKind::Comma, TokenKind::RCurly, P),
CURLIES,
P,
)(p)?,
),
TokenKind::LParen => Pattern::TupleStruct(
name,
delim(
sep(Parse::parse, TokenKind::Comma, TokenKind::RParen, P),
PARENS,
P,
)(p)?,
),
_ => name
.as_sym()
.map(Pattern::Name)
.unwrap_or(Pattern::Path(name)),
})
}
impl Parse<'_> for Pattern {
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
const P: Parsing = Parsing::Pattern;
let head = match p.peek_kind(P)? {
// Name, Path, Struct, TupleStruct
TokenKind::Identifier => pathpattern(p)?,
// Literal
TokenKind::True | TokenKind::False | TokenKind::Literal => Pattern::Literal(p.parse()?),
// Rest
TokenKind::DotDot => {
p.consume_peeked();
if matches!(
p.peek_kind(P),
Ok(TokenKind::Identifier | TokenKind::Literal)
) {
Pattern::Rest(Some(p.parse()?))
} else {
Pattern::Rest(None)
}
}
// Ref
TokenKind::Amp => {
p.consume_peeked();
Pattern::Ref(p.parse()?, p.parse()?)
}
// Ref(Ref)
TokenKind::AmpAmp => {
p.consume_peeked();
Pattern::Ref(
Mutability::Not,
Box::new(Pattern::Ref(p.parse()?, p.parse()?)),
)
}
// Tuple
TokenKind::LParen => Pattern::Tuple(delim(
sep(Parse::parse, TokenKind::Comma, TokenKind::RParen, P),
PARENS,
P,
)(p)?),
// Array
TokenKind::LBrack => Pattern::Array(delim(
sep(Parse::parse, TokenKind::Comma, TokenKind::RBrack, P),
BRACKETS,
P,
)(p)?),
_ => {
let bad_expr = p.parse()?;
Err(p.error(ErrorKind::InvalidPattern(bad_expr), P))?
}
};
match p.peek_kind(P) {
Ok(TokenKind::DotDot) => {
p.consume_peeked();
Ok(Pattern::RangeExc(head.into(), p.parse()?))
}
Ok(TokenKind::DotDotEq) => {
p.consume_peeked();
Ok(Pattern::RangeInc(head.into(), p.parse()?))
}
_ => Ok(head),
}
let value = prec::exprkind(p, prec::Precedence::Highest.level())?;
Pattern::try_from(value)
.map_err(|_| p.error(ExpectedParsing { want: Parsing::Pattern }, Parsing::Pattern))
}
}
@ -1214,14 +1091,13 @@ impl Parse<'_> for Match {
/// [Match] = `match` [Expr] `{` [MatchArm],* `}`
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
p.match_type(TokenKind::Match, Parsing::Match)?;
Ok(Match {
scrutinee: condition(p)?.into(),
arms: delim(
sep(MatchArm::parse, TokenKind::Comma, CURLIES.1, Parsing::Match),
CURLIES,
Parsing::Match,
)(p)?,
})
let scrutinee = Expr::parse(p)?.into();
let arms = delim(
sep(MatchArm::parse, TokenKind::Comma, CURLIES.1, Parsing::Match),
CURLIES,
Parsing::Match,
)(p)?;
Ok(Match { scrutinee, arms })
}
}
@ -1239,12 +1115,6 @@ impl Parse<'_> for MatchArm {
fn ret_body(p: &mut Parser, while_parsing: Parsing) -> PResult<Option<Box<Expr>>> {
Ok(match p.peek_kind(while_parsing)? {
TokenKind::Semi => None,
_ => Some(p.parse()?),
_ => Some(Expr::parse(p)?.into()),
})
}
impl<'t, P: Parse<'t>> Parse<'t> for Box<P> {
fn parse(p: &mut Parser<'t>) -> PResult<Self> {
p.parse().map(Box::new)
}
}

View File

@ -8,50 +8,43 @@
use super::{Parse, *};
/// Parses an [ExprKind]
pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
pub fn exprkind(p: &mut Parser, power: u8) -> PResult<ExprKind> {
let parsing = Parsing::ExprKind;
let start = p.loc();
// Prefix expressions
let mut head = Expr {
kind: match p.peek_kind(Parsing::Unary)? {
literal_like!() => Literal::parse(p)?.into(),
path_like!() => exprkind_pathlike(p)?,
TokenKind::Amp | TokenKind::AmpAmp => AddrOf::parse(p)?.into(),
TokenKind::Bar | TokenKind::BarBar => Closure::parse(p)?.into(),
TokenKind::Grave => Quote::parse(p)?.into(),
TokenKind::LCurly => Block::parse(p)?.into(),
TokenKind::LBrack => exprkind_arraylike(p)?,
TokenKind::LParen => exprkind_tuplelike(p)?,
TokenKind::Let => Let::parse(p)?.into(),
TokenKind::Match => Match::parse(p)?.into(),
TokenKind::While => ExprKind::While(While::parse(p)?),
TokenKind::If => ExprKind::If(If::parse(p)?),
TokenKind::For => ExprKind::For(For::parse(p)?),
TokenKind::Break => ExprKind::Break(Break::parse(p)?),
TokenKind::Return => ExprKind::Return(Return::parse(p)?),
TokenKind::Continue => {
p.consume_peeked();
ExprKind::Continue
}
op => {
let (kind, prec) =
from_prefix(op).ok_or_else(|| p.error(Unexpected(op), parsing))?;
let ((), after) = prec.prefix().expect("should have a precedence");
p.consume_peeked();
Unary { kind, tail: expr(p, after)?.into() }.into()
}
},
span: Span(start, p.loc()),
// Prefix expressions
let mut head = match p.peek_kind(Parsing::Unary)? {
literal_like!() => Literal::parse(p)?.into(),
path_like!() => exprkind_pathlike(p)?,
TokenKind::Amp | TokenKind::AmpAmp => AddrOf::parse(p)?.into(),
TokenKind::Grave => Quote::parse(p)?.into(),
TokenKind::LCurly => Block::parse(p)?.into(),
TokenKind::LBrack => exprkind_arraylike(p)?,
TokenKind::LParen => exprkind_tuplelike(p)?,
TokenKind::Let => Let::parse(p)?.into(),
TokenKind::Match => Match::parse(p)?.into(),
TokenKind::While => ExprKind::While(While::parse(p)?),
TokenKind::If => ExprKind::If(If::parse(p)?),
TokenKind::For => ExprKind::For(For::parse(p)?),
TokenKind::Break => ExprKind::Break(Break::parse(p)?),
TokenKind::Return => ExprKind::Return(Return::parse(p)?),
TokenKind::Continue => {
p.consume_peeked();
ExprKind::Continue
}
op => {
let (kind, prec) = from_prefix(op).ok_or_else(|| p.error(Unexpected(op), parsing))?;
let ((), after) = prec.prefix().expect("should have a precedence");
p.consume_peeked();
Unary { kind, tail: exprkind(p, after)?.into() }.into()
}
};
fn from_postfix(op: TokenKind) -> Option<Precedence> {
Some(match op {
TokenKind::LBrack => Precedence::Index,
TokenKind::LParen => Precedence::Call,
TokenKind::LCurly => Precedence::Structor,
TokenKind::Dot => Precedence::Member,
TokenKind::As => Precedence::Cast,
_ => None?,
})
}
@ -62,48 +55,26 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
if before < power {
break;
}
p.consume_peeked();
head = Expr {
kind: match op {
TokenKind::LBrack => {
p.consume_peeked();
let indices =
sep(Expr::parse, TokenKind::Comma, TokenKind::RBrack, parsing)(p)?;
p.match_type(TokenKind::RBrack, parsing)?;
ExprKind::Index(Index { head: head.into(), indices })
}
TokenKind::LParen => {
p.consume_peeked();
let exprs =
sep(Expr::parse, TokenKind::Comma, TokenKind::RParen, parsing)(p)?;
p.match_type(TokenKind::RParen, parsing)?;
Binary {
kind: BinaryKind::Call,
parts: (
head,
Expr { kind: Tuple { exprs }.into(), span: Span(start, p.loc()) },
)
.into(),
}
head = match op {
TokenKind::LBrack => {
let indices =
sep(Expr::parse, TokenKind::Comma, TokenKind::RBrack, parsing)(p)?;
p.match_type(TokenKind::RBrack, parsing)?;
ExprKind::Index(Index { head: head.into(), indices })
}
TokenKind::LParen => {
let exprs = sep(Expr::parse, TokenKind::Comma, TokenKind::RParen, parsing)(p)?;
p.match_type(TokenKind::RParen, parsing)?;
Binary { kind: BinaryKind::Call, parts: (head, Tuple { exprs }.into()).into() }
.into()
}
TokenKind::LCurly => match head.kind {
ExprKind::Path(path) => ExprKind::Structor(structor_body(p, path)?),
_ => break,
},
TokenKind::Dot => {
p.consume_peeked();
let kind = MemberKind::parse(p)?;
Member { head: Box::new(head), kind }.into()
}
TokenKind::As => {
p.consume_peeked();
let ty = Ty::parse(p)?;
Cast { head: head.into(), ty }.into()
}
_ => Err(p.error(Unexpected(op), parsing))?,
},
span: Span(start, p.loc()),
}
TokenKind::Dot => {
let kind = MemberKind::parse(p)?;
Member { head: Box::new(head), kind }.into()
}
_ => Err(p.error(Unexpected(op), parsing))?,
};
continue;
}
@ -115,11 +86,8 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
}
p.consume_peeked();
let tail = expr(p, after)?;
head = Expr {
kind: Binary { kind, parts: (head, tail).into() }.into(),
span: Span(start, p.loc()),
};
let tail = exprkind(p, after)?;
head = Binary { kind, parts: (head, tail).into() }.into();
continue;
}
@ -130,11 +98,8 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
}
p.consume_peeked();
let tail = expr(p, after)?;
head = Expr {
kind: Modify { kind, parts: (head, tail).into() }.into(),
span: Span(start, p.loc()),
};
let tail = exprkind(p, after)?;
head = Modify { kind, parts: (head, tail).into() }.into();
continue;
}
@ -147,12 +112,8 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
}
p.consume_peeked();
let tail = expr(p, after)?;
head = Expr {
kind: Assign { parts: (head, tail).into() }.into(),
span: Span(start, p.loc()),
};
let tail = exprkind(p, after)?;
head = Assign { parts: (head, tail).into() }.into();
continue;
}
@ -164,8 +125,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(), span: Span(start, p.loc()) };
head = Cast { head: head.into(), ty }.into();
continue;
}
@ -201,10 +161,10 @@ fn exprkind_array_rep(p: &mut Parser) -> PResult<ExprKind> {
let first = Expr::parse(p)?;
Ok(match p.peek_kind(P)? {
TokenKind::Semi => ArrayRep {
value: first.into(),
value: first.kind.into(),
repeat: {
p.consume_peeked();
p.parse()?
Box::new(exprkind(p, 0)?)
},
}
.into(),
@ -251,13 +211,17 @@ fn exprkind_group(p: &mut Parser) -> PResult<ExprKind> {
}
Ok(Tuple { exprs }.into())
}
_ => Ok(Group { expr: first.into() }.into()),
_ => Ok(Group { expr: first.kind.into() }.into()),
}
}
/// Parses an expression beginning with a [Path] (i.e. [Path] or [Structor])
fn exprkind_pathlike(p: &mut Parser) -> PResult<ExprKind> {
Path::parse(p).map(Into::into)
let head = Path::parse(p)?;
Ok(match p.match_type(TokenKind::Colon, Parsing::Path) {
Ok(_) => ExprKind::Structor(structor_body(p, head)?),
Err(_) => ExprKind::Path(head),
})
}
/// [Structor]Body = `{` ([Fielder] `,`)* [Fielder]? `}`
@ -280,8 +244,6 @@ fn structor_body(p: &mut Parser, to: Path) -> PResult<Structor> {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Precedence {
Assign,
Structor, // A structor is never a valid conditional
Condition, // Anything that syntactically needs a block following it
Logic,
Compare,
Range,
@ -294,7 +256,7 @@ pub enum Precedence {
Cast,
Member, // left-associative
Call,
Deref,
Highest,
}
impl Precedence {
@ -307,7 +269,6 @@ impl Precedence {
match self {
Self::Assign => Some(((), self.level())),
Self::Unary => Some(((), self.level())),
Self::Deref => Some(((), self.level())),
_ => None,
}
}
@ -323,9 +284,7 @@ impl Precedence {
pub fn postfix(self) -> Option<(u8, ())> {
match self {
Self::Structor | Self::Index | Self::Call | Self::Member | Self::Cast => {
Some((self.level(), ()))
}
Self::Index | Self::Call | Self::Member => Some((self.level(), ())),
_ => None,
}
}
@ -358,8 +317,7 @@ impl From<UnaryKind> for Precedence {
use UnaryKind as Op;
match value {
Op::Loop => Precedence::Assign,
Op::Deref => Precedence::Deref,
_ => Precedence::Unary,
Op::Deref | Op::Neg | Op::Not | Op::At | Op::Tilde => Precedence::Unary,
}
}
}
@ -380,8 +338,6 @@ operator! {
Star => Deref,
Minus => Neg,
Bang => Not,
DotDot => RangeExc,
DotDotEq => RangeInc,
At => At,
Tilde => Tilde,
};

View File

@ -14,9 +14,6 @@ cl-ast = { path = "../cl-ast" }
cl-lexer = { path = "../cl-lexer" }
cl-token = { path = "../cl-token" }
cl-parser = { path = "../cl-parser" }
cl-typeck = { path = "../cl-typeck" }
cl-interpret = { path = "../cl-interpret" }
cl-structures = { path = "../cl-structures" }
cl-arena = { version = "0", registry = "soft-fish" }
repline = { path = "../../repline" }
argwerk = "0.20.4"

View File

@ -1,955 +0,0 @@
//! Pretty prints a conlang AST in yaml
use cl_ast::{File, Stmt};
use cl_lexer::Lexer;
use cl_parser::Parser;
use repline::{Repline, error::Error as RlError};
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
if let Some(path) = std::env::args().nth(1) {
let f = std::fs::read_to_string(&path).expect("Path must be valid.");
let mut parser = Parser::new(path, Lexer::new(&f));
let code: File = match parser.parse() {
Ok(f) => f,
Err(e) => {
eprintln!("{e}");
return Ok(());
}
};
CLangifier::new().p(&code);
println!();
return Ok(());
}
let mut rl = Repline::new("\x1b[33m", "cl>", "? >");
loop {
let mut line = match rl.read() {
Err(RlError::CtrlC(_)) => break,
Err(RlError::CtrlD(line)) => {
rl.deny();
line
}
Ok(line) => line,
Err(e) => Err(e)?,
};
if !line.ends_with(';') {
line.push(';');
}
let mut parser = Parser::new("stdin", Lexer::new(&line));
let code = match parser.parse::<Stmt>() {
Ok(code) => {
rl.accept();
code
}
Err(e) => {
print!("\x1b[40G\x1bJ\x1b[91m{e}\x1b[0m");
continue;
}
};
print!("\x1b[G\x1b[J");
CLangifier::new().p(&code);
println!();
}
Ok(())
}
pub use clangifier::CLangifier;
pub mod clangifier {
use crate::clangify::CLangify;
use std::{
fmt::Display,
io::Write,
ops::{Add, Deref, DerefMut},
};
#[derive(Debug, Default)]
pub struct CLangifier {
depth: usize,
}
impl CLangifier {
pub fn new() -> Self {
Self::default()
}
pub fn indent(&mut self) -> Section {
Section::new(self)
}
/// Prints a [Yamlify] value
#[inline]
pub fn p<T: CLangify + ?Sized>(&mut self, yaml: &T) -> &mut Self {
yaml.print(self);
self
}
fn increase(&mut self) {
self.depth += 1;
}
fn decrease(&mut self) {
self.depth -= 1;
}
fn print_indentation(&self, writer: &mut impl Write) {
for _ in 0..self.depth {
let _ = write!(writer, " ");
}
}
pub fn endl(&mut self) -> &mut Self {
self.p("\n")
.print_indentation(&mut std::io::stdout().lock());
self
}
/// Prints a section header and increases indentation
pub fn nest(&mut self, name: impl Display) -> Section {
print!("{name}");
self.indent()
}
}
impl<C: CLangify + ?Sized> Add<&C> for &mut CLangifier {
type Output = Self;
fn add(self, rhs: &C) -> Self::Output {
self.p(rhs)
}
}
/// Tracks the start and end of an indented block (a "section")
pub struct Section<'y> {
yamler: &'y mut CLangifier,
}
impl<'y> Section<'y> {
pub fn new(yamler: &'y mut CLangifier) -> Self {
yamler.increase();
Self { yamler }
}
}
impl Deref for Section<'_> {
type Target = CLangifier;
fn deref(&self) -> &Self::Target {
self.yamler
}
}
impl DerefMut for Section<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.yamler
}
}
impl Drop for Section<'_> {
fn drop(&mut self) {
let Self { yamler } = self;
yamler.decrease();
}
}
}
pub mod clangify {
use core::panic;
use std::iter;
use super::clangifier::CLangifier;
use cl_ast::*;
pub trait CLangify {
fn print(&self, y: &mut CLangifier);
}
impl CLangify for File {
fn print(&self, mut y: &mut CLangifier) {
let File { name, items } = self;
// TODO: turn name into include guard
y = (y + "// Generated from " + name).endl();
for (idx, item) in items.iter().enumerate() {
if idx > 0 {
y.endl().endl();
}
y.p(item);
}
y.endl();
}
}
impl CLangify for Visibility {
fn print(&self, _y: &mut CLangifier) {}
}
impl CLangify for Mutability {
fn print(&self, y: &mut CLangifier) {
if let Mutability::Not = self {
y.p("const ");
}
}
}
impl CLangify for Attrs {
fn print(&self, y: &mut CLangifier) {
let Self { meta } = self;
y.nest("Attrs").p(meta);
todo!("Attributes");
}
}
impl CLangify for Meta {
fn print(&self, y: &mut CLangifier) {
let Self { name, kind } = self;
y.nest("Meta").p(name).p(kind);
todo!("Attributes");
}
}
impl CLangify for MetaKind {
fn print(&self, y: &mut CLangifier) {
match self {
MetaKind::Plain => y,
MetaKind::Equals(value) => y.p(value),
MetaKind::Func(args) => y.p(args),
};
todo!("Attributes");
}
}
impl CLangify for Item {
fn print(&self, y: &mut CLangifier) {
let Self { span: _, attrs: _, vis, kind } = self;
y.p(vis).p(kind);
}
}
impl CLangify for ItemKind {
fn print(&self, y: &mut CLangifier) {
match self {
ItemKind::Alias(f) => y.p(f),
ItemKind::Const(f) => y.p(f),
ItemKind::Static(f) => y.p(f),
ItemKind::Module(f) => y.p(f),
ItemKind::Function(f) => y.p(f),
ItemKind::Struct(f) => y.p(f),
ItemKind::Enum(f) => y.p(f),
ItemKind::Impl(f) => y.p(f),
ItemKind::Use(f) => y.p(f),
};
}
}
impl CLangify for Generics {
fn print(&self, _y: &mut CLangifier) {
let Self { vars } = self;
if !vars.is_empty() {
panic!("C doesn't have generics, dumbass.")
}
}
}
impl CLangify for Alias {
fn print(&self, y: &mut CLangifier) {
let Self { name, from } = self;
y.p("typedef ").p(from).p(" ");
y.p(name).p("; ");
}
}
impl CLangify for Const {
fn print(&self, y: &mut CLangifier) {
let Self { name, ty, init } = self;
y.p("const ").p(ty).p(" ");
y.p(name).p(" = ").p(init);
}
}
impl CLangify for Static {
fn print(&self, y: &mut CLangifier) {
let Self { mutable, name, ty, init } = self;
y.p(mutable).p(ty).p(" ");
y.p(name).p(" = ").p(init);
}
}
impl CLangify for Module {
fn print(&self, y: &mut CLangifier) {
let Self { name, file } = self;
y.nest("// mod ").p(name).p(" {").endl();
y.p(file);
y.endl().p("// } mod ").p(name);
}
}
impl CLangify for Function {
fn print(&self, y: &mut CLangifier) {
let Self { name, gens: _, sign, bind, body } = self;
let TyFn { args, rety } = sign;
let types = match &args.kind {
TyKind::Tuple(TyTuple { types }) => types.as_slice(),
TyKind::Empty => &[],
_ => panic!("Unsupported function args: {args}"),
};
let bind = match bind {
Pattern::Tuple(tup) => tup.as_slice(),
_ => panic!("Unsupported function binders: {args}"),
};
y.p(rety).p(" ").p(name).p(" (");
for (idx, (bind, ty)) in bind.iter().zip(types).enumerate() {
if idx > 0 {
y.p(", ");
}
// y.print("/* TODO: desugar pat match args */");
y.p(ty).p(" ").p(bind);
}
y.p(") ").p(body);
}
}
impl CLangify for Struct {
fn print(&self, y: &mut CLangifier) {
let Self { name, gens: _, kind } = self;
y.p("struct ").p(name).nest(" {").p(kind);
y.endl().p("}");
}
}
impl CLangify for StructKind {
fn print(&self, y: &mut CLangifier) {
match self {
StructKind::Empty => y.endl().p("char _zero_sized_t;"),
StructKind::Tuple(k) => {
for (idx, ty) in k.iter().enumerate() {
y.endl().p(ty).p(" _").p(&idx).p(";");
}
y
}
StructKind::Struct(k) => y.p(k),
};
}
}
impl CLangify for StructMember {
fn print(&self, y: &mut CLangifier) {
let Self { vis, name, ty } = self;
y.p(vis).p(ty).p(" ").p(name).p(";");
}
}
impl CLangify for Enum {
fn print(&self, y: &mut CLangifier) {
let Self { name, gens: _, variants } = self;
y.nest("enum ").p(name).p(" {").endl();
for (idx, variant) in variants.iter().enumerate() {
if idx > 0 {
y.p(",").endl();
}
y.p(variant);
}
y.endl().p("\n}");
}
}
impl CLangify for Variant {
fn print(&self, y: &mut CLangifier) {
let Self { name, kind, body } = self;
y.p(name).p(kind).p(body);
}
}
impl CLangify for Impl {
fn print(&self, y: &mut CLangifier) {
let Self { gens, target, body } = self;
y.nest("/* TODO: impl ").p(gens).p(target).p(" { */ ");
y.p(body);
y.p("/* } // impl ").p(target).p(" */ ");
}
}
impl CLangify for ImplKind {
fn print(&self, y: &mut CLangifier) {
match self {
ImplKind::Type(t) => y.p(t),
ImplKind::Trait { impl_trait, for_type } => {
todo!("impl {impl_trait} for {for_type}")
}
};
}
}
impl CLangify for Use {
fn print(&self, y: &mut CLangifier) {
let Self { absolute: _, tree } = self;
y.p(tree);
}
}
impl CLangify for UseTree {
fn print(&self, y: &mut CLangifier) {
match self {
UseTree::Tree(trees) => y.p(trees),
UseTree::Path(path, tree) => y.p("/* ").p(path).p(" */").p(tree),
UseTree::Alias(from, to) => y.p("#import <").p(from).p(">.h// ").p(to).p(" "),
UseTree::Name(name) => y.p("#import <").p(name).p(".h> "),
UseTree::Glob => y.p("/* TODO: use globbing */"),
};
}
}
impl CLangify for Block {
fn print(&self, y: &mut CLangifier) {
let Self { stmts } = self;
{
let mut y = y.nest("{");
y.endl();
if let [
stmts @ ..,
Stmt { span: _, kind: StmtKind::Expr(expr), semi: Semi::Unterminated },
] = stmts.as_slice()
{
y.p(stmts).p("return ").p(expr).p(";");
} else {
y.p(stmts);
}
}
y.endl().p("}");
}
}
impl CLangify for Stmt {
fn print(&self, y: &mut CLangifier) {
let Self { span: _, kind, semi: _ } = self;
y.p(kind).p(";").endl();
}
}
impl CLangify for Semi {
fn print(&self, y: &mut CLangifier) {
y.p(";");
}
}
impl CLangify for StmtKind {
fn print(&self, y: &mut CLangifier) {
match self {
StmtKind::Empty => y,
StmtKind::Item(s) => y.p(s),
StmtKind::Expr(s) => y.p(s),
};
}
}
impl CLangify for Expr {
fn print(&self, y: &mut CLangifier) {
let Self { span: _, kind } = self;
y.p(kind);
}
}
impl CLangify for ExprKind {
fn print(&self, y: &mut CLangifier) {
match self {
ExprKind::Closure(k) => todo!("Downgrade {k}"),
ExprKind::Quote(k) => k.print(y),
ExprKind::Let(k) => k.print(y),
ExprKind::Match(k) => k.print(y),
ExprKind::Assign(k) => k.print(y),
ExprKind::Modify(k) => k.print(y),
ExprKind::Binary(k) => k.print(y),
ExprKind::Unary(k) => k.print(y),
ExprKind::Cast(k) => k.print(y),
ExprKind::Member(k) => k.print(y),
ExprKind::Index(k) => k.print(y),
ExprKind::Structor(k) => k.print(y),
ExprKind::Path(k) => k.print(y),
ExprKind::Literal(k) => k.print(y),
ExprKind::Array(k) => k.print(y),
ExprKind::ArrayRep(k) => k.print(y),
ExprKind::AddrOf(k) => k.print(y),
ExprKind::Block(k) => k.print(y),
ExprKind::Empty => {}
ExprKind::Group(k) => k.print(y),
ExprKind::Tuple(k) => k.print(y),
ExprKind::While(k) => k.print(y),
ExprKind::If(k) => k.print(y),
ExprKind::For(k) => k.print(y),
ExprKind::Break(k) => k.print(y),
ExprKind::Return(k) => k.print(y),
ExprKind::Continue => {
y.nest("continue");
}
}
}
}
impl CLangify for Quote {
fn print(&self, y: &mut CLangifier) {
y.nest("\"");
print!("{self}");
y.p("\"");
}
}
impl CLangify for Let {
fn print(&self, y: &mut CLangifier) {
let Self { mutable, name, ty, init } = self;
let ty = ty.as_deref().map(|ty| &ty.kind).unwrap_or(&TyKind::Infer);
match ty {
TyKind::Array(TyArray { ty, count }) => {
y.p(ty).p(" ").p(mutable).p(name).p("[").p(count).p("]");
}
TyKind::Fn(TyFn { args, rety }) => {
y.nest("(").p(rety).p(" *").p(mutable).p(name).p(")(");
match &args.kind {
TyKind::Empty => {}
TyKind::Tuple(TyTuple { types }) => {
for (idx, ty) in types.iter().enumerate() {
if idx > 0 {
y.p(", ");
}
y.p(ty);
}
}
_ => {
y.p(args);
}
}
y.p(")");
}
_ => {
y.indent().p(ty).p(" ").p(mutable).p(name);
}
}
if let Some(init) = init {
y.p(" = ").p(init);
}
}
}
impl CLangify for Pattern {
fn print(&self, y: &mut CLangifier) {
// TODO: Pattern match desugaring!!!
match self {
Pattern::Name(name) => y.p(name),
Pattern::Path(path) => y.p(path),
Pattern::Literal(literal) => y.p(literal),
Pattern::Rest(name) => y.p("..").p(name),
Pattern::Ref(mutability, pattern) => y.p("&").p(mutability).p(pattern),
Pattern::RangeExc(head, tail) => y.p("RangeExc").p(head).p(tail),
Pattern::RangeInc(head, tail) => y.p("RangeExc").p(head).p(tail),
Pattern::Tuple(patterns) => y.nest("Tuple").p(patterns),
Pattern::Array(patterns) => y.nest("Array").p(patterns),
Pattern::Struct(path, items) => {
{
let mut y = y.nest("Struct");
y.p(path);
for (name, item) in items {
y.p(name).p(item);
}
}
y
}
Pattern::TupleStruct(path, items) => {
{
let mut y = y.nest("TupleStruct");
y.p(path).p(items);
}
y
}
};
}
}
impl CLangify for Match {
fn print(&self, y: &mut CLangifier) {
let Self { scrutinee, arms } = self;
y.p("/* match ").p(scrutinee);
y.nest(" { ").p(arms);
y.p(" } */");
}
}
impl CLangify for MatchArm {
fn print(&self, y: &mut CLangifier) {
let Self(pat, expr) = self;
y.p(pat).p(" => ").p(expr).p(", ");
}
}
impl CLangify for Assign {
fn print(&self, y: &mut CLangifier) {
let Self { parts } = self;
y.p(&parts.0).p(" = ").p(&parts.1);
}
}
impl CLangify for Modify {
fn print(&self, y: &mut CLangifier) {
let Self { kind, parts } = self;
y.p(&parts.0).p(kind).p(&parts.1);
}
}
impl CLangify for ModifyKind {
fn print(&self, _y: &mut CLangifier) {
print!(" {self} ");
}
}
impl CLangify for Binary {
fn print(&self, y: &mut CLangifier) {
let Self { kind, parts } = self;
match kind {
BinaryKind::Call => y.p(&parts.0).p(&parts.1),
_ => y.p("(").p(&parts.0).p(kind).p(&parts.1).p(")"),
};
}
}
impl CLangify for BinaryKind {
fn print(&self, _y: &mut CLangifier) {
print!(" {self} ");
}
}
impl CLangify for Unary {
fn print(&self, y: &mut CLangifier) {
let Self { kind, tail } = self;
match kind {
UnaryKind::Deref => y.p("*").p(tail),
UnaryKind::Neg => y.p("-").p(tail),
UnaryKind::Not => y.p("!").p(tail),
UnaryKind::RangeInc => todo!("Unary RangeInc in C"),
UnaryKind::RangeExc => todo!("Unary RangeExc in C"),
UnaryKind::Loop => y.nest("while (1) { ").p(tail).p(" }"),
UnaryKind::At => todo!(),
UnaryKind::Tilde => todo!(),
};
}
}
impl CLangify for Cast {
fn print(&self, y: &mut CLangifier) {
let Self { head, ty } = self;
y.nest("(").p(ty).p(")");
y.p(head);
}
}
impl CLangify for Member {
fn print(&self, y: &mut CLangifier) {
let Self { head, kind } = self;
match kind {
MemberKind::Call(name, Tuple { exprs }) => {
y.p(name);
y.p("(");
for (idx, expr) in iter::once(head.as_ref()).chain(exprs).enumerate() {
if idx > 0 {
y.p(", ");
}
y.p(expr);
}
y.p(")")
}
MemberKind::Struct(name) => y.p(head).p(".").p(name),
MemberKind::Tuple(idx) => y.p(head).p("._").p(idx),
};
}
}
impl CLangify for Tuple {
fn print(&self, y: &mut CLangifier) {
let Self { exprs } = self;
let mut y = y.nest("( ");
for (idx, expr) in exprs.iter().enumerate() {
if idx > 0 {
y.p(", ");
}
y.p(expr);
}
y.p(" )");
}
}
impl CLangify for Index {
fn print(&self, y: &mut CLangifier) {
let Self { head, indices } = self;
y.p(head);
for index in indices {
y.p("[").p(index).p("]");
}
}
}
impl CLangify for Structor {
fn print(&self, y: &mut CLangifier) {
let Self { to, init } = self;
y.nest("(").p(to).p(")");
{
let mut y = y.nest("{ ");
for (idx, field) in init.iter().enumerate() {
if idx > 0 {
y.p(", ");
}
y.p(field);
}
y.p(init);
}
y.p("}");
}
}
impl CLangify for Fielder {
fn print(&self, y: &mut CLangifier) {
let Self { name, init } = self;
y.p(".").p(name).p(" = ").p(init);
}
}
impl CLangify for Array {
fn print(&self, y: &mut CLangifier) {
let Self { values } = self;
{
let mut y = y.nest("{");
y.endl();
for (idx, value) in values.iter().enumerate() {
if idx > 0 {
y.p(", ");
}
y.p(value);
}
}
y.endl().p("}");
}
}
impl CLangify for ArrayRep {
fn print(&self, y: &mut CLangifier) {
let Self { value, repeat } = self;
let ExprKind::Literal(Literal::Int(repeat)) = &repeat.kind else {
eprintln!("Constant needs folding: {repeat}");
return;
};
{
let mut y = y.nest("{");
for _ in 0..*repeat {
y.endl().p(value).p(",");
}
}
y.endl().p("}");
}
}
impl CLangify for AddrOf {
fn print(&self, y: &mut CLangifier) {
let Self { mutable: _, expr } = self;
y.p("&").p(expr);
}
}
impl CLangify for Group {
fn print(&self, y: &mut CLangifier) {
let Self { expr } = self;
y.p("(").p(expr).p(")");
}
}
impl CLangify for While {
fn print(&self, y: &mut CLangifier) {
// TODO: to properly propagate intermediate values, a new temp variable needs to be
// declared on every line lmao. This will require type info.
let Self { cond, pass, fail } = self;
let Else { body: fail } = fail;
y.nest("while(1) {")
.endl()
.p("if (")
.p(cond)
.p(") ")
.p(pass);
{
let mut y = y.nest(" else {");
y.endl();
if let Some(fail) = fail {
y.p(fail).p(";").endl();
}
y.p("break;");
}
y.endl().p("}");
}
}
impl CLangify for Else {
fn print(&self, y: &mut CLangifier) {
let Self { body } = self;
if let Some(body) = body {
y.p(" else ").p(body);
}
}
}
impl CLangify for If {
fn print(&self, y: &mut CLangifier) {
let Self { cond, pass, fail } = self;
y.p("if (").p(cond).p(")");
y.p(pass).p(fail);
}
}
impl CLangify for For {
#[rustfmt::skip]
fn print(&self, y: &mut CLangifier) {
let Self { bind, cond, pass, fail: _ } = self;
let (mode, (head, tail)) = match &cond.kind {
ExprKind::Binary(Binary { kind: BinaryKind::RangeExc, parts }) => (false, &**parts),
ExprKind::Binary(Binary { kind: BinaryKind::RangeInc, parts }) => (true, &**parts),
_ => todo!("Clangify for loops"),
};
// for (int bind = head; bind mode? < : <= tail; bind++);
y.p("for ( int ").p(bind).p(" = ").p(head).p("; ");
y.p(bind).p(if mode {"<="} else {"<"}).p(tail).p("; ");
y.p("++").p(bind).p(" ) ").p(pass);
}
}
impl CLangify for Break {
fn print(&self, y: &mut CLangifier) {
let Self { body } = self;
y.nest("break ").p(body);
}
}
impl CLangify for Return {
fn print(&self, y: &mut CLangifier) {
let Self { body } = self;
y.nest("return ").p(body);
}
}
impl CLangify for Literal {
fn print(&self, y: &mut CLangifier) {
match self {
Literal::Float(l) => y.p(l),
Literal::Bool(l) => y.p(l),
Literal::Int(l) => y.p(l),
Literal::Char(l) => y.p("'").p(l).p("'"),
Literal::String(l) => y.p(&'"').p(l).p(&'"'),
};
}
}
impl CLangify for Sym {
fn print(&self, y: &mut CLangifier) {
y.p(self.to_ref());
}
}
impl CLangify for Ty {
fn print(&self, y: &mut CLangifier) {
let Self { span: _, kind, gens: _ } = self;
y.p(kind);
}
}
impl CLangify for TyKind {
fn print(&self, y: &mut CLangifier) {
match self {
TyKind::Never => y.p("Never"),
TyKind::Empty => y.p("Empty"),
TyKind::Infer => y.p("auto"),
TyKind::Path(t) => y.p(t),
TyKind::Tuple(t) => y.p(t),
TyKind::Ref(t) => y.p(t),
TyKind::Ptr(t) => y.p(t),
TyKind::Fn(t) => y.p(t),
TyKind::Slice(t) => y.p(t),
TyKind::Array(t) => y.p(t),
};
}
}
impl CLangify for Path {
fn print(&self, y: &mut CLangifier) {
let Self { absolute: _, parts } = self;
for (idx, part) in parts.iter().enumerate() {
if idx > 0 {
y.p("_");
}
y.p(part);
}
}
}
impl CLangify for PathPart {
fn print(&self, y: &mut CLangifier) {
match self {
PathPart::SuperKw => y.p("super"),
PathPart::SelfTy => y.p("Self"),
PathPart::Ident(i) => y.p(i),
};
}
}
impl CLangify for TyArray {
fn print(&self, y: &mut CLangifier) {
let Self { ty, count } = self;
y.p(ty).p("[").p(count).p("]");
}
}
impl CLangify for TySlice {
fn print(&self, y: &mut CLangifier) {
let Self { ty } = self;
y.p(ty).p("* ");
}
}
impl CLangify for TyTuple {
fn print(&self, y: &mut CLangifier) {
let Self { types } = self;
{
let mut y = y.nest("struct {");
y.endl();
for (idx, ty) in types.iter().enumerate() {
if idx > 0 {
y.p(",").endl();
}
y.p(ty);
}
}
y.endl().p("}");
}
}
impl CLangify for TyRef {
fn print(&self, y: &mut CLangifier) {
let Self { count, mutable, to } = self;
y.p(mutable).p(to);
for _ in 0..*count {
y.p("*");
}
}
}
impl CLangify for TyPtr {
fn print(&self, y: &mut CLangifier) {
let Self { to } = self;
y.p(to).p("*");
}
}
impl CLangify for TyFn {
fn print(&self, y: &mut CLangifier) {
let Self { args, rety } = self;
// TODO: function pointer syntax
y.nest("(").p(rety).p(" *)(");
match &args.kind {
TyKind::Empty => y,
TyKind::Tuple(TyTuple { types }) => {
for (idx, ty) in types.iter().enumerate() {
if idx > 0 {
y.p(", ");
}
y.p(ty);
}
y
}
_ => y.p(args),
}
.p(")");
}
}
impl<T: CLangify> CLangify for Option<T> {
fn print(&self, y: &mut CLangifier) {
if let Some(v) = self {
y.p(v);
}
}
}
impl<T: CLangify> CLangify for Box<T> {
fn print(&self, y: &mut CLangifier) {
y.p(&**self);
}
}
impl<T: CLangify> CLangify for Vec<T> {
fn print(&self, y: &mut CLangifier) {
for thing in self {
y.p(thing);
}
}
}
impl<T: CLangify> CLangify for [T] {
fn print(&self, y: &mut CLangifier) {
for thing in self {
y.p(thing);
}
}
}
impl CLangify for () {
fn print(&self, _y: &mut CLangifier) {
// TODO: C has no language support for zst
}
}
impl<T: CLangify> CLangify for &T {
fn print(&self, y: &mut CLangifier) {
(*self).print(y)
}
}
impl CLangify for std::fmt::Arguments<'_> {
fn print(&self, _y: &mut CLangifier) {
print!("{self}")
}
}
macro_rules! scalar {
($($t:ty),*$(,)?) => {
$(impl CLangify for $t {
fn print(&self, _y: &mut CLangifier) {
print!("{self}");
}
})*
};
}
scalar! {
bool, char, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, str, &str, String
}
}

View File

@ -3,7 +3,7 @@
use cl_ast::Stmt;
use cl_lexer::Lexer;
use cl_parser::Parser;
use repline::{Repline, error::Error as RlError};
use repline::{error::Error as RlError, Repline};
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
@ -19,7 +19,7 @@ fn main() -> Result<(), Box<dyn Error>> {
Err(e) => Err(e)?,
};
let mut parser = Parser::new("", Lexer::new(&line));
let mut parser = Parser::new(Lexer::new(&line));
let code = match parser.parse::<Stmt>() {
Ok(code) => {
rl.accept();
@ -41,6 +41,7 @@ pub use yamler::Yamler;
pub mod yamler {
use crate::yamlify::Yamlify;
use std::{
fmt::Display,
io::Write,
ops::{Deref, DerefMut},
};
@ -80,33 +81,28 @@ pub mod yamler {
}
/// Prints a section header and increases indentation
pub fn key(&mut self, name: impl Yamlify) -> Section {
pub fn key(&mut self, name: impl Display) -> Section {
println!();
self.print_indentation(&mut std::io::stdout().lock());
print!(" ");
name.yaml(self);
print!(":");
print!("- {name}:");
self.indent()
}
/// Prints a yaml key value pair: `- name: "value"`
pub fn pair<D: Yamlify, T: Yamlify>(&mut self, name: D, value: T) -> &mut Self {
self.key(name).value(value);
pub fn pair<D: Display, T: Yamlify>(&mut self, name: D, value: T) -> &mut Self {
self.key(name).yaml(&value);
self
}
/// Prints a yaml scalar value: `"name"``
pub fn value<D: Yamlify>(&mut self, value: D) -> &mut Self {
print!(" ");
value.yaml(self);
pub fn value<D: Display>(&mut self, value: D) -> &mut Self {
print!(" {value}");
self
}
pub fn list<D: Yamlify>(&mut self, list: &[D]) -> &mut Self {
for value in list {
println!();
self.print_indentation(&mut std::io::stdout().lock());
self.yaml(&"- ").yaml(value);
for (idx, value) in list.iter().enumerate() {
self.pair(idx, value);
}
self
}
@ -154,8 +150,8 @@ pub mod yamlify {
impl Yamlify for File {
fn yaml(&self, y: &mut Yamler) {
let File { name, items } = self;
y.key("File").pair("name", name).yaml(items);
let File { items } = self;
y.key("File").yaml(items);
}
}
impl Yamlify for Visibility {
@ -197,7 +193,7 @@ pub mod yamlify {
impl Yamlify for Item {
fn yaml(&self, y: &mut Yamler) {
let Self { span: _, attrs, vis, kind } = self;
let Self { extents: _, attrs, vis, kind } = self;
y.key("Item").yaml(attrs).yaml(vis).yaml(kind);
}
}
@ -216,16 +212,10 @@ pub mod yamlify {
};
}
}
impl Yamlify for Generics {
fn yaml(&self, y: &mut Yamler) {
let Self { vars } = self;
y.key("Generics").value(vars);
}
}
impl Yamlify for Alias {
fn yaml(&self, y: &mut Yamler) {
let Self { name, from } = self;
y.key("Alias").pair("to", name).pair("from", from);
let Self { to, from } = self;
y.key("Alias").pair("to", to).pair("from", from);
}
}
impl Yamlify for Const {
@ -245,16 +235,23 @@ pub mod yamlify {
}
impl Yamlify for Module {
fn yaml(&self, y: &mut Yamler) {
let Self { name, file } = self;
y.key("Module").pair("name", name).yaml(file);
let Self { name, kind } = self;
y.key("Module").pair("name", name).yaml(kind);
}
}
impl Yamlify for ModuleKind {
fn yaml(&self, y: &mut Yamler) {
match self {
ModuleKind::Inline(f) => y.yaml(f),
ModuleKind::Outline => y,
};
}
}
impl Yamlify for Function {
fn yaml(&self, y: &mut Yamler) {
let Self { name, gens, sign, bind, body } = self;
let Self { name, sign, bind, body } = self;
y.key("Function")
.pair("name", name)
.pair("gens", gens)
.pair("sign", sign)
.pair("bind", bind)
.pair("body", body);
@ -262,11 +259,8 @@ pub mod yamlify {
}
impl Yamlify for Struct {
fn yaml(&self, y: &mut Yamler) {
let Self { name, gens, kind } = self;
y.key("Struct")
.pair("gens", gens)
.pair("name", name)
.yaml(kind);
let Self { name, kind } = self;
y.key("Struct").pair("name", name).yaml(kind);
}
}
impl Yamlify for StructKind {
@ -286,29 +280,38 @@ pub mod yamlify {
}
impl Yamlify for Enum {
fn yaml(&self, y: &mut Yamler) {
let Self { name, gens, variants: kind } = self;
y.key("Enum")
.pair("gens", gens)
.pair("name", name)
.yaml(kind);
let Self { name, kind } = self;
y.key("Enum").pair("name", name).yaml(kind);
}
}
impl Yamlify for EnumKind {
fn yaml(&self, y: &mut Yamler) {
match self {
EnumKind::NoVariants => y,
EnumKind::Variants(v) => y.yaml(v),
};
}
}
impl Yamlify for Variant {
fn yaml(&self, y: &mut Yamler) {
let Self { name, kind, body } = self;
y.key("Variant")
.pair("name", name)
.pair("kind", kind)
.pair("body", body);
let Self { name, kind } = self;
y.key("Variant").pair("name", name).yaml(kind);
}
}
impl Yamlify for VariantKind {
fn yaml(&self, y: &mut Yamler) {
match self {
VariantKind::Plain => y,
VariantKind::CLike(v) => y.yaml(v),
VariantKind::Tuple(v) => y.yaml(v),
VariantKind::Struct(v) => y.yaml(v),
};
}
}
impl Yamlify for Impl {
fn yaml(&self, y: &mut Yamler) {
let Self { gens, target, body } = self;
y.key("Impl")
.pair("gens", gens)
.pair("target", target)
.pair("body", body);
let Self { target, body } = self;
y.key("Impl").pair("target", target).pair("body", body);
}
}
impl Yamlify for ImplKind {
@ -346,8 +349,8 @@ pub mod yamlify {
}
impl Yamlify for Stmt {
fn yaml(&self, y: &mut Yamler) {
let Self { span: _, kind, semi } = self;
y.key("Stmt").value(kind).yaml(semi);
let Self { extents: _, kind, semi } = self;
y.key("Stmt").yaml(kind).yaml(semi);
}
}
impl Yamlify for Semi {
@ -368,14 +371,13 @@ pub mod yamlify {
}
impl Yamlify for Expr {
fn yaml(&self, y: &mut Yamler) {
let Self { span: _, kind } = self;
let Self { extents: _, kind } = self;
y.yaml(kind);
}
}
impl Yamlify for ExprKind {
fn yaml(&self, y: &mut Yamler) {
match self {
ExprKind::Closure(k) => k.yaml(y),
ExprKind::Quote(k) => k.yaml(y),
ExprKind::Let(k) => k.yaml(y),
ExprKind::Match(k) => k.yaml(y),
@ -407,12 +409,6 @@ pub mod yamlify {
}
}
}
impl Yamlify for Closure {
fn yaml(&self, y: &mut Yamler) {
let Self { arg, body } = self;
y.key("Closure").pair("arg", arg).pair("body", body);
}
}
impl Yamlify for Quote {
fn yaml(&self, y: &mut Yamler) {
y.key("Quote").value(self);
@ -432,35 +428,23 @@ pub mod yamlify {
impl Yamlify for Pattern {
fn yaml(&self, y: &mut Yamler) {
match self {
Pattern::Name(name) => y.value(name),
Pattern::Path(path) => y.value(path),
Pattern::Literal(literal) => y.value(literal),
Pattern::Rest(name) => y.pair("Rest", name),
Pattern::Ref(mutability, pattern) => y.yaml(mutability).pair("Pat", pattern),
Pattern::RangeInc(head, tail) => {
y.key("RangeInc").pair("head", head).pair("tail", tail);
y
Pattern::Ref(mutability, pattern) => {
y.pair("mutability", mutability).pair("subpattern", pattern)
}
Pattern::RangeExc(head, tail) => {
y.key("RangeExc").pair("head", head).pair("tail", tail);
y
}
Pattern::Tuple(patterns) => y.key("Tuple").list(patterns),
Pattern::Array(patterns) => y.key("Array").list(patterns),
Pattern::Tuple(patterns) => y.key("Tuple").yaml(patterns),
Pattern::Array(patterns) => y.key("Array").yaml(patterns),
Pattern::Struct(path, items) => {
{
let mut y = y.key("Struct");
y.yaml(path);
y.pair("name", path);
for (name, item) in items {
y.pair(name, item);
}
}
y
}
Pattern::TupleStruct(path, items) => {
y.key("TupleStruct").yaml(path).list(items);
y
}
};
}
}
@ -496,6 +480,11 @@ pub mod yamlify {
.pair("tail", &parts.1);
}
}
impl Yamlify for ModifyKind {
fn yaml(&self, y: &mut Yamler) {
y.value(self);
}
}
impl Yamlify for Binary {
fn yaml(&self, y: &mut Yamler) {
let Self { kind, parts } = self;
@ -505,12 +494,22 @@ pub mod yamlify {
.pair("tail", &parts.1);
}
}
impl Yamlify for BinaryKind {
fn yaml(&self, y: &mut Yamler) {
y.value(self);
}
}
impl Yamlify for Unary {
fn yaml(&self, y: &mut Yamler) {
let Self { kind, tail } = self;
y.key("Unary").pair("kind", kind).pair("tail", tail);
}
}
impl Yamlify for UnaryKind {
fn yaml(&self, y: &mut Yamler) {
y.value(self);
}
}
impl Yamlify for Cast {
fn yaml(&self, y: &mut Yamler) {
let Self { head, ty } = self;
@ -541,10 +540,7 @@ pub mod yamlify {
impl Yamlify for Index {
fn yaml(&self, y: &mut Yamler) {
let Self { head, indices } = self;
y.key("Index")
.pair("head", head)
.key("indices")
.list(indices);
y.key("Index").pair("head", head).list(indices);
}
}
impl Yamlify for Structor {
@ -597,7 +593,7 @@ pub mod yamlify {
impl Yamlify for Else {
fn yaml(&self, y: &mut Yamler) {
let Self { body } = self;
y.key("fail").yaml(body);
y.key("Else").yaml(body);
}
}
impl Yamlify for If {
@ -629,20 +625,25 @@ pub mod yamlify {
}
}
impl Yamlify for Literal {
fn yaml(&self, _y: &mut Yamler) {
match self {
Literal::Bool(v) => print!("{v}"),
Literal::Char(v) => print!("'{}'", v.escape_debug()),
Literal::Int(v) => print!("{v}"),
Literal::Float(v) => print!("{v}"),
Literal::String(v) => print!("{}", v.escape_debug()),
}
fn yaml(&self, y: &mut Yamler) {
y.value(format_args!("\"{self}\""));
}
}
impl Yamlify for Sym {
fn yaml(&self, y: &mut Yamler) {
y.value(self);
}
}
impl Yamlify for Param {
fn yaml(&self, y: &mut Yamler) {
let Self { mutability, name } = self;
y.key("Param").yaml(mutability).pair("name", name);
}
}
impl Yamlify for Ty {
fn yaml(&self, y: &mut Yamler) {
let Self { span: _, kind, gens } = self;
y.key("Ty").yaml(kind).yaml(gens);
let Self { extents: _, kind } = self;
y.key("Ty").yaml(kind);
}
}
impl Yamlify for TyKind {
@ -650,14 +651,12 @@ pub mod yamlify {
match self {
TyKind::Never => y.value("Never"),
TyKind::Empty => y.value("Empty"),
TyKind::Infer => y.value("_"),
TyKind::Path(t) => y.yaml(t),
TyKind::Tuple(t) => y.yaml(t),
TyKind::Ref(t) => y.yaml(t),
TyKind::Ptr(t) => y.yaml(t),
TyKind::Fn(t) => y.yaml(t),
TyKind::Slice(t) => y.yaml(t),
TyKind::Array(t) => y.yaml(t),
TyKind::Slice(_) => todo!(),
TyKind::Array(_) => todo!(),
};
}
}
@ -668,13 +667,16 @@ pub mod yamlify {
if *absolute {
y.pair("absolute", absolute);
}
y.yaml(parts);
for part in parts {
y.pair("part", part);
}
}
}
impl Yamlify for PathPart {
fn yaml(&self, y: &mut Yamler) {
match self {
PathPart::SuperKw => y.value("super"),
PathPart::SelfKw => y.value("self"),
PathPart::SelfTy => y.value("Self"),
PathPart::Ident(i) => y.yaml(i),
};
@ -710,13 +712,6 @@ pub mod yamlify {
.pair("to", to);
}
}
impl Yamlify for TyPtr {
fn yaml(&self, y: &mut Yamler) {
let Self { to } = self;
y.key("TyPtr")
.pair("to", to);
}
}
impl Yamlify for TyFn {
fn yaml(&self, y: &mut Yamler) {
let Self { args, rety } = self;
@ -740,7 +735,9 @@ pub mod yamlify {
}
impl<T: Yamlify> Yamlify for Vec<T> {
fn yaml(&self, y: &mut Yamler) {
y.list(self);
for thing in self {
y.yaml(thing);
}
}
}
impl Yamlify for () {
@ -756,15 +753,14 @@ pub mod yamlify {
macro_rules! scalar {
($($t:ty),*$(,)?) => {
$(impl Yamlify for $t {
fn yaml(&self, _y: &mut Yamler) {
print!("{self}");
fn yaml(&self, y: &mut Yamler) {
y.value(self);
}
})*
};
}
scalar! {
bool, char, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, &str, String,
BinaryKind, UnaryKind, ModifyKind, Sym,
bool, char, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, &str, String
}
}

View File

@ -9,7 +9,7 @@ use cl_ast::File;
use cl_interpret::{builtin::builtins, convalue::ConValue, env::Environment, interpret::Interpret};
use cl_lexer::Lexer;
use cl_parser::Parser;
use std::{borrow::Cow, error::Error, path::Path};
use std::{error::Error, path::Path};
/// Run the command line interface
pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
@ -23,39 +23,19 @@ pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
menu::clear();
Ok(ConValue::Empty)
}
fn eval(string) @env {
use cl_interpret::error::Error;
let string = match *string {
ConValue::String(string) => string,
ConValue::Ref(v) => {
let string = env.get_id(v).cloned().unwrap_or_default();
return eval(env, &[string])
}
_ => Err(Error::TypeError())?
};
match Parser::new("eval", Lexer::new(string.to_ref())).parse::<cl_ast::Stmt>() {
Err(e) => Ok(ConValue::String(format!("{e}").into())),
Ok(v) => v.interpret(env),
}
/// Evaluates a quoted expression
fn eval(ConValue::Quote(quote)) @env {
env.eval(quote.as_ref())
}
/// Executes a file
fn import(ConValue::String(path)) @env {
load_file(env, &**path).or(Ok(ConValue::Empty))
}
fn putchar(ConValue::Char(c)) {
print!("{c}");
Ok(ConValue::Empty)
}
/// Gets a line of input from stdin
fn get_line(ConValue::String(prompt)) {
match repline::Repline::new("", prompt.to_ref(), "").read() {
fn get_line() {
match repline::Repline::new("", "", "").read() {
Ok(line) => Ok(ConValue::String(line.into())),
Err(repline::Error::CtrlD(line)) => Ok(ConValue::String(line.into())),
Err(repline::Error::CtrlC(_)) => Err(cl_interpret::error::Error::Break(ConValue::Empty)),
Err(e) => Ok(ConValue::String(e.to_string().into())),
}
}
@ -67,9 +47,7 @@ pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
if repl {
if let Some(file) = file {
if let Err(e) = load_file(&mut env, file) {
eprintln!("{e}")
}
load_file(&mut env, file)?;
}
let mut ctx = Context::with_env(env);
match mode {
@ -79,36 +57,25 @@ pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
Mode::Run => menu::run(&mut ctx)?,
}
} else {
let path = format_path_for_display(file.as_deref());
let code = match &file {
Some(file) => std::fs::read_to_string(file)?,
None => std::io::read_to_string(std::io::stdin())?,
};
match mode {
Mode::Lex => lex_code(&path, &code),
Mode::Fmt => fmt_code(&path, &code),
Mode::Run | Mode::Menu => run_code(&path, &code, &mut env),
Mode::Lex => lex_code(&code, file),
Mode::Fmt => fmt_code(&code),
Mode::Run | Mode::Menu => run_code(&code, &mut env),
}?;
}
Ok(())
}
fn format_path_for_display(path: Option<&Path>) -> Cow<str> {
match path {
Some(file) => file
.to_str()
.map(Cow::Borrowed)
.unwrap_or_else(|| Cow::Owned(file.display().to_string())),
None => Cow::Borrowed(""),
}
}
fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue, Box<dyn Error>> {
let path = path.as_ref();
let inliner = cl_parser::inliner::ModuleInliner::new(path.with_extension(""));
let inliner =
cl_parser::inliner::ModuleInliner::new(path.as_ref().parent().unwrap_or(Path::new("")));
let file = std::fs::read_to_string(path)?;
let code = Parser::new(path.display().to_string(), Lexer::new(&file)).parse()?;
let code = Parser::new(Lexer::new(&file)).parse()?;
let code = match inliner.inline(code) {
Ok(a) => a,
Err((code, io_errs, parse_errs)) => {
@ -121,22 +88,13 @@ fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue,
code
}
};
use cl_ast::WeightOf;
eprintln!("File {} weighs {} units", code.name, code.weight_of());
match env.eval(&code) {
Ok(v) => Ok(v),
Err(e) => {
eprintln!("{e}");
Ok(ConValue::Empty)
}
}
Ok(env.eval(&code)?)
}
fn lex_code(path: &str, code: &str) -> Result<(), Box<dyn Error>> {
fn lex_code(code: &str, path: Option<impl AsRef<Path>>) -> Result<(), Box<dyn Error>> {
for token in Lexer::new(code) {
if !path.is_empty() {
print!("{}:", path);
if let Some(path) = &path {
print!("{}:", path.as_ref().display());
}
match token {
Ok(token) => print_token(&token),
@ -146,14 +104,14 @@ fn lex_code(path: &str, code: &str) -> Result<(), Box<dyn Error>> {
Ok(())
}
fn fmt_code(path: &str, code: &str) -> Result<(), Box<dyn Error>> {
let code = Parser::new(path, Lexer::new(code)).parse::<File>()?;
fn fmt_code(code: &str) -> Result<(), Box<dyn Error>> {
let code = Parser::new(Lexer::new(code)).parse::<File>()?;
println!("{code}");
Ok(())
}
fn run_code(path: &str, code: &str, env: &mut Environment) -> Result<(), Box<dyn Error>> {
let code = Parser::new(path, Lexer::new(code)).parse::<File>()?;
fn run_code(code: &str, env: &mut Environment) -> Result<(), Box<dyn Error>> {
let code = Parser::new(Lexer::new(code)).parse::<File>()?;
match code.interpret(env)? {
ConValue::Empty => {}
ret => println!("{ret}"),

View File

@ -47,7 +47,7 @@ pub fn run(ctx: &mut ctx::Context) -> ReplResult<()> {
if line.trim().is_empty() {
return Ok(Response::Deny);
}
let code = Parser::new("", Lexer::new(line)).parse::<Stmt>()?;
let code = Parser::new(Lexer::new(line)).parse::<Stmt>()?;
let code = ModuleInliner::new(".").fold_stmt(code);
print!("{}", ansi::OUTPUT);
@ -75,7 +75,7 @@ pub fn lex(_ctx: &mut ctx::Context) -> ReplResult<()> {
pub fn fmt(_ctx: &mut ctx::Context) -> ReplResult<()> {
read_and(ansi::BRIGHT_MAGENTA, "cl>", " ?>", |line| {
let mut p = Parser::new("", Lexer::new(line));
let mut p = Parser::new(Lexer::new(line));
match p.parse::<Stmt>() {
Ok(code) => println!("{}{code}{}", ansi::OUTPUT, ansi::RESET),

View File

@ -61,10 +61,8 @@ macro_rules! make_index {($($(#[$meta:meta])* $name:ident),*$(,)?) => {$(
)*}}
use self::iter::MapIndexIter;
use std::{
ops::{Index, IndexMut},
slice::GetDisjointMutError,
};
use core::slice::GetManyMutError;
use std::ops::{Index, IndexMut};
pub use make_index;
@ -105,11 +103,11 @@ impl<V, K: MapIndex> IndexMap<K, V> {
/// Returns mutable references to many indices at once.
///
/// Returns an error if any index is out of bounds, or if the same index was passed twice.
pub fn get_disjoint_mut<const N: usize>(
pub fn get_many_mut<const N: usize>(
&mut self,
indices: [K; N],
) -> Result<[&mut V; N], GetDisjointMutError> {
self.map.get_disjoint_mut(indices.map(|id| id.get()))
) -> Result<[&mut V; N], GetManyMutError> {
self.map.get_many_mut(indices.map(|id| id.get()))
}
/// Returns an iterator over the IndexMap.

View File

@ -37,8 +37,8 @@ pub mod interned {
}
/// Gets the internal value as a reference with the interner's lifetime
pub fn to_ref(&self) -> &'a T {
self.value
pub fn to_ref(interned: &Self) -> &'a T {
interned.value
}
}
@ -264,17 +264,12 @@ pub mod typed_interner {
/// A [TypedInterner] hands out [Interned] references for arbitrary types.
///
/// See the [module-level documentation](self) for more information.
#[derive(Default)]
pub struct TypedInterner<'a, T: Eq + Hash> {
arena: TypedArena<'a, T>,
keys: RwLock<HashSet<&'a T>>,
}
impl<'a, T: Eq + Hash> Default for TypedInterner<'a, T> {
fn default() -> Self {
Self { arena: Default::default(), keys: Default::default() }
}
}
impl<'a, T: Eq + Hash> TypedInterner<'a, T> {
/// Creates a new [TypedInterner] backed by the provided [TypedArena]
pub fn new(arena: TypedArena<'a, T>) -> Self {

View File

@ -10,7 +10,7 @@
//! [im]: index_map::IndexMap
//! [mi]: index_map::MapIndex
#![warn(clippy::all)]
#![feature(dropck_eyepatch, decl_macro)]
#![feature(dropck_eyepatch, decl_macro, get_many_mut)]
#![deny(unsafe_op_in_unsafe_fn)]
pub mod intern;

View File

@ -42,6 +42,6 @@ impl Loc {
impl std::fmt::Display for Loc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Loc { line, col } = self;
write!(f, "{line}:{col}")
write!(f, "{line}:{col}:")
}
}

View File

@ -93,8 +93,8 @@ use std::{
/// assert_eq!(Some(&10), v.last());
/// ```
pub macro stack {
($capacity:literal) => {
Stack::<_, $capacity>::new()
($count:literal) => {
Stack::<_, $count>::new()
},
($value:expr ; $count:literal) => {{
let mut stack: Stack<_, $count> = Stack::new();
@ -103,13 +103,6 @@ pub macro stack {
}
stack
}},
($value:expr ; $count:literal ; $capacity:literal) => {{
let mut stack: Stack<_, $capacity> = Stack::new();
for _ in 0..$count {
stack.push($value)
}
stack
}},
($($values:expr),* $(,)?) => {
Stack::from([$($values),*])
}
@ -149,14 +142,20 @@ impl<T, const N: usize> Deref for Stack<T, N> {
#[inline]
fn deref(&self) -> &Self::Target {
self.as_slice()
// Safety:
// - We have ensured all elements from 0 to len have been initialized
// - self.elem[0] came from a reference, and so is aligned to T
// unsafe { &*(&self.buf[0..self.len] as *const [_] as *const [T]) }
unsafe { slice::from_raw_parts(self.buf.as_ptr().cast(), self.len) }
}
}
impl<T, const N: usize> DerefMut for Stack<T, N> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut_slice()
// Safety:
// - See Deref
unsafe { slice::from_raw_parts_mut(self.buf.as_mut_ptr().cast(), self.len) }
}
}
@ -164,7 +163,7 @@ impl<T, const N: usize> DerefMut for Stack<T, N> {
unsafe impl<#[may_dangle] T, const N: usize> Drop for Stack<T, N> {
#[inline]
fn drop(&mut self) {
// Safety: Elements in [0..self.len] are initialized
// Safety: We have ensured that all elements in the list are
if std::mem::needs_drop::<T>() {
unsafe { core::ptr::drop_in_place(self.as_mut_slice()) };
}
@ -272,24 +271,18 @@ impl<T, const N: usize> Stack<T, N> {
}
/// Returns an unsafe mutable pointer to the stack's buffer
pub const fn as_mut_ptr(&mut self) -> *mut T {
pub fn as_mut_ptr(&mut self) -> *mut T {
self.buf.as_mut_ptr().cast()
}
/// Extracts a slice containing the entire vector
pub const fn as_slice(&self) -> &[T] {
// Safety:
// - We have ensured all elements from 0 to len have been initialized
// - self.elem[0] came from a reference, and so is aligned to T
// unsafe { &*(&self.buf[0..self.len] as *const [_] as *const [T]) }
unsafe { slice::from_raw_parts(self.buf.as_ptr().cast(), self.len) }
pub fn as_slice(&self) -> &[T] {
self
}
/// Extracts a mutable slice containing the entire vector
pub const fn as_mut_slice(&mut self) -> &mut [T] {
// Safety:
// - See Stack::as_slice
unsafe { slice::from_raw_parts_mut(self.buf.as_mut_ptr().cast(), self.len) }
pub fn as_mut_slice(&mut self) -> &mut [T] {
self
}
/// Returns the total number of elements the stack can hold
@ -362,7 +355,7 @@ impl<T, const N: usize> Stack<T, N> {
/// v.push(3);
/// assert_eq!(&[0, 1, 2, 3], v.as_slice());
/// ```
pub const fn push(&mut self, value: T) {
pub fn push(&mut self, value: T) {
if self.len >= N {
panic!("Attempted to push into full stack")
}
@ -373,7 +366,7 @@ impl<T, const N: usize> Stack<T, N> {
/// Push a new element onto the end of the stack
///
/// Returns [`Err(value)`](Result::Err) if the new length would exceed capacity
pub const fn try_push(&mut self, value: T) -> Result<(), T> {
pub fn try_push(&mut self, value: T) -> Result<(), T> {
if self.len >= N {
return Err(value);
}
@ -388,11 +381,8 @@ impl<T, const N: usize> Stack<T, N> {
///
/// len after push must not exceed capacity N
#[inline]
const unsafe fn push_unchecked(&mut self, value: T) {
unsafe {
// self.buf.get_unchecked_mut(self.len).write(value); // TODO: This is non-const
ptr::write(self.as_mut_ptr().add(self.len), value)
}
unsafe fn push_unchecked(&mut self, value: T) {
unsafe { ptr::write(self.as_mut_ptr().add(self.len), value) }
self.len += 1; // post inc
}
@ -412,14 +402,13 @@ impl<T, const N: usize> Stack<T, N> {
/// assert_eq!(Some(0), v.pop());
/// assert_eq!(None, v.pop());
/// ```
pub const fn pop(&mut self) -> Option<T> {
pub fn pop(&mut self) -> Option<T> {
if self.len == 0 {
None
} else {
self.len -= 1;
// Safety: MaybeUninit<T> implies ManuallyDrop<T>,
// therefore should not get dropped twice
// Some(unsafe { self.buf.get_unchecked_mut(self.len).assume_init_read() })
Some(unsafe { ptr::read(self.as_ptr().add(self.len).cast()) })
}
}
@ -518,7 +507,7 @@ impl<T, const N: usize> Stack<T, N> {
///
/// assert_eq!(Ok(()), v.try_insert(0, 0));
/// ```
pub const fn try_insert(&mut self, index: usize, data: T) -> Result<(), (T, InsertFailed<N>)> {
pub fn try_insert(&mut self, index: usize, data: T) -> Result<(), (T, InsertFailed<N>)> {
if index > self.len {
return Err((data, InsertFailed::Bounds(index)));
}
@ -534,7 +523,7 @@ impl<T, const N: usize> Stack<T, N> {
/// - index must be less than self.len
/// - length after insertion must be <= N
#[inline]
const unsafe fn insert_unchecked(&mut self, index: usize, data: T) {
unsafe fn insert_unchecked(&mut self, index: usize, data: T) {
let base = self.as_mut_ptr();
unsafe { ptr::copy(base.add(index), base.add(index + 1), self.len - index) }
@ -558,9 +547,7 @@ impl<T, const N: usize> Stack<T, N> {
/// ```
pub fn clear(&mut self) {
// Hopefully copy elision takes care of this lmao
while !self.is_empty() {
drop(self.pop());
}
drop(std::mem::take(self))
}
/// Returns the number of elements in the stack
@ -570,7 +557,7 @@ impl<T, const N: usize> Stack<T, N> {
///
/// assert_eq!(5, v.len());
/// ```
pub const fn len(&self) -> usize {
pub fn len(&self) -> usize {
self.len
}
@ -585,7 +572,7 @@ impl<T, const N: usize> Stack<T, N> {
/// assert!(v.is_full());
/// ```
#[inline]
pub const fn is_full(&self) -> bool {
pub fn is_full(&self) -> bool {
self.len >= N
}
@ -600,7 +587,7 @@ impl<T, const N: usize> Stack<T, N> {
/// assert!(v.is_empty());
/// ```
#[inline]
pub const fn is_empty(&self) -> bool {
pub fn is_empty(&self) -> bool {
self.len == 0
}
}
@ -638,7 +625,6 @@ mod tests {
v.pop();
assert_eq!(v.len(), usize::MAX - 1);
}
#[test]
fn new() {
let v: Stack<(), 255> = Stack::new();
@ -759,19 +745,4 @@ mod tests {
]);
std::mem::drop(std::hint::black_box(v));
}
#[test]
fn drop_zst() {
struct Droppable;
impl Drop for Droppable {
fn drop(&mut self) {
use std::sync::atomic::{AtomicU32, Ordering};
static V: AtomicU32 = AtomicU32::new(1);
eprintln!("{}", V.fetch_add(1, Ordering::Relaxed));
}
}
let v = Stack::from([const { Droppable }; 10]);
std::mem::drop(v);
}
}

View File

@ -33,6 +33,7 @@ pub enum TokenKind {
Mut, // "mut"
Pub, // "pub"
Return, // "return"
SelfKw, // "self"
SelfTy, // "Self"
Static, // "static"
Struct, // "struct"
@ -106,34 +107,35 @@ impl Display for TokenKind {
TokenKind::Literal => "literal".fmt(f),
TokenKind::Identifier => "identifier".fmt(f),
TokenKind::As => "as".fmt(f),
TokenKind::Break => "break".fmt(f),
TokenKind::Cl => "cl".fmt(f),
TokenKind::Const => "const".fmt(f),
TokenKind::Continue => "continue".fmt(f),
TokenKind::Else => "else".fmt(f),
TokenKind::Enum => "enum".fmt(f),
TokenKind::False => "false".fmt(f),
TokenKind::Fn => "fn".fmt(f),
TokenKind::For => "for".fmt(f),
TokenKind::If => "if".fmt(f),
TokenKind::Impl => "impl".fmt(f),
TokenKind::In => "in".fmt(f),
TokenKind::Let => "let".fmt(f),
TokenKind::Loop => "loop".fmt(f),
TokenKind::Match => "match".fmt(f),
TokenKind::Mod => "mod".fmt(f),
TokenKind::Mut => "mut".fmt(f),
TokenKind::Pub => "pub".fmt(f),
TokenKind::Return => "return".fmt(f),
TokenKind::SelfTy => "Self".fmt(f),
TokenKind::Static => "static".fmt(f),
TokenKind::Struct => "struct".fmt(f),
TokenKind::Super => "super".fmt(f),
TokenKind::True => "true".fmt(f),
TokenKind::Type => "type".fmt(f),
TokenKind::Use => "use".fmt(f),
TokenKind::While => "while".fmt(f),
TokenKind::As => "sama".fmt(f),
TokenKind::Break => "pana".fmt(f),
TokenKind::Cl => "la".fmt(f),
TokenKind::Const => "kiwen".fmt(f),
TokenKind::Continue => "tawa".fmt(f),
TokenKind::Else => "taso".fmt(f),
TokenKind::Enum => "kulupu".fmt(f),
TokenKind::False => "ike".fmt(f),
TokenKind::Fn => "nasin".fmt(f),
TokenKind::For => "ale".fmt(f),
TokenKind::If => "tan".fmt(f),
TokenKind::Impl => "insa".fmt(f),
TokenKind::In => "lon".fmt(f),
TokenKind::Let => "poki".fmt(f),
TokenKind::Loop => "awen".fmt(f),
TokenKind::Match => "seme".fmt(f),
TokenKind::Mod => "selo".fmt(f),
TokenKind::Mut => "ante".fmt(f),
TokenKind::Pub => "lukin".fmt(f),
TokenKind::Return => "pini".fmt(f),
TokenKind::SelfKw => "mi".fmt(f),
TokenKind::SelfTy => "Mi".fmt(f),
TokenKind::Static => "mute".fmt(f),
TokenKind::Struct => "lipu".fmt(f),
TokenKind::Super => "mama".fmt(f),
TokenKind::True => "pona".fmt(f),
TokenKind::Type => "ijo".fmt(f),
TokenKind::Use => "jo".fmt(f),
TokenKind::While => "lawa".fmt(f),
TokenKind::LCurly => "{".fmt(f),
TokenKind::RCurly => "}".fmt(f),
@ -198,34 +200,35 @@ impl FromStr for TokenKind {
/// Parses a string s to return a Keyword
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"as" => Self::As,
"break" => Self::Break,
"cl" => Self::Cl,
"const" => Self::Const,
"continue" => Self::Continue,
"else" => Self::Else,
"enum" => Self::Enum,
"false" => Self::False,
"fn" => Self::Fn,
"for" => Self::For,
"if" => Self::If,
"impl" => Self::Impl,
"in" => Self::In,
"let" => Self::Let,
"loop" => Self::Loop,
"match" => Self::Match,
"mod" => Self::Mod,
"mut" => Self::Mut,
"pub" => Self::Pub,
"return" => Self::Return,
"Self" => Self::SelfTy,
"static" => Self::Static,
"struct" => Self::Struct,
"super" => Self::Super,
"true" => Self::True,
"type" => Self::Type,
"use" => Self::Use,
"while" => Self::While,
"as" | "sama" => Self::As,
"break" | "pana" => Self::Break,
"cl" | "la" => Self::Cl,
"const" | "kiwen" => Self::Const,
"continue" | "tawa" => Self::Continue,
"else" | "taso" => Self::Else,
"enum" | "kulupu" => Self::Enum,
"false" | "ike" => Self::False,
"fn" | "nasin" => Self::Fn,
"for" | "ale" => Self::For,
"if" | "tan" => Self::If,
"impl" | "insa" => Self::Impl,
"in" | "lon" => Self::In,
"let" | "poki" => Self::Let,
"loop" | "awen" => Self::Loop,
"match" | "seme" => Self::Match,
"mod" | "selo" => Self::Mod,
"mut" | "ante" => Self::Mut,
"pub" | "lukin" => Self::Pub,
"return" | "pini" => Self::Return,
"self" | "mi" => Self::SelfKw,
"Self" | "Mi" => Self::SelfTy,
"static" | "mute" => Self::Static,
"struct" | "lipu" => Self::Struct,
"super" | "mama" => Self::Super,
"true" | "pona" => Self::True,
"type" | "ijo" => Self::Type,
"use" | "jo" => Self::Use,
"while" | "lawa" => Self::While,
_ => Err(())?,
})
}

View File

@ -1,20 +1,12 @@
use cl_typeck::{
entry::Entry,
stage::{
infer::{engine::InferenceEngine, error::InferenceError, inference::Inference},
*,
},
table::Table,
type_expression::TypeExpression,
};
use cl_typeck::{entry::Entry, stage::*, table::Table, type_expression::TypeExpression};
use cl_ast::{
Expr, Path, Stmt, Ty,
ast_visitor::{Fold, Visit},
desugar::*,
Stmt, Ty,
};
use cl_lexer::Lexer;
use cl_parser::{Parser, inliner::ModuleInliner};
use cl_parser::{inliner::ModuleInliner, Parser};
use cl_structures::intern::string_interner::StringInterner;
use repline::{error::Error as RlError, prebaked::*};
use std::{
@ -42,7 +34,7 @@ const C_LISTING: &str = "\x1b[38;5;117m";
fn main() -> Result<(), Box<dyn Error>> {
let mut prj = Table::default();
let mut parser = Parser::new("PREAMBLE", Lexer::new(PREAMBLE));
let mut parser = Parser::new(Lexer::new(PREAMBLE));
let code = match parser.parse() {
Ok(code) => code,
Err(e) => {
@ -52,15 +44,8 @@ fn main() -> Result<(), Box<dyn Error>> {
};
// This code is special - it gets loaded from a hard-coded project directory (for now)
let code = inline_modules(code, concat!(env!("CARGO_MANIFEST_DIR"), "/../../stdlib"));
let code = cl_ast::desugar::WhileElseDesugar.fold_file(code);
Populator::new(&mut prj).visit_file(interned(code));
for arg in std::env::args().skip(1) {
import_file(&mut prj, arg)?;
}
resolve_all(&mut prj)?;
main_menu(&mut prj)?;
Ok(())
}
@ -68,24 +53,20 @@ fn main() -> Result<(), Box<dyn Error>> {
fn main_menu(prj: &mut Table) -> Result<(), RlError> {
banner();
read_and(C_MAIN, "mu>", "? >", |line| {
for line in line.trim().split_ascii_whitespace() {
match line {
"c" | "code" => enter_code(prj)?,
"clear" => clear()?,
"dump" => dump(prj)?,
"d" | "desugar" => live_desugar()?,
"e" | "exit" => return Ok(Response::Break),
"f" | "file" => import_files(prj)?,
"i" | "id" => get_by_id(prj)?,
"l" | "list" => list_types(prj),
"q" | "query" => query_type_expression(prj)?,
"r" | "resolve" => resolve_all(prj)?,
"s" | "strings" => print_strings(),
"a" | "all" => infer_all(prj)?,
"t" | "test" => infer_expression(prj)?,
"h" | "help" | "" => {
println!(
"Valid commands are:
match line.trim() {
"c" | "code" => enter_code(prj)?,
"clear" => clear()?,
"d" | "desugar" => live_desugar()?,
"e" | "exit" => return Ok(Response::Break),
"f" | "file" => import_files(prj)?,
"i" | "id" => get_by_id(prj)?,
"l" | "list" => list_types(prj),
"q" | "query" => query_type_expression(prj)?,
"r" | "resolve" => resolve_all(prj)?,
"s" | "strings" => print_strings(),
"h" | "help" | "" => {
println!(
"Valid commands are:
clear : Clear the screen
code (c): Enter code to type-check
desugar (d): WIP: Test the experimental desugaring passes
@ -96,11 +77,10 @@ fn main_menu(prj: &mut Table) -> Result<(), RlError> {
resolve (r): Perform type resolution
help (h): Print this list
exit (e): Exit the program"
);
return Ok(Response::Deny);
}
_ => Err(r#"Invalid command. Type "help" to see the list of valid commands."#)?,
);
return Ok(Response::Deny);
}
_ => Err(r#"Invalid command. Type "help" to see the list of valid commands."#)?,
}
Ok(Response::Accept)
})
@ -111,7 +91,7 @@ fn enter_code(prj: &mut Table) -> Result<(), RlError> {
if line.trim().is_empty() {
return Ok(Response::Break);
}
let code = Parser::new("", Lexer::new(line)).parse()?;
let code = Parser::new(Lexer::new(line)).parse()?;
let code = inline_modules(code, "");
let code = WhileElseDesugar.fold_file(code);
@ -122,12 +102,9 @@ fn enter_code(prj: &mut Table) -> Result<(), RlError> {
fn live_desugar() -> Result<(), RlError> {
read_and(C_RESV, "se>", "? >", |line| {
let code = Parser::new("", Lexer::new(line)).parse::<Stmt>()?;
let code = Parser::new(Lexer::new(line)).parse::<Stmt>()?;
println!("Raw, as parsed:\n{C_LISTING}{code}\x1b[0m");
let code = ConstantFolder.fold_stmt(code);
println!("ConstantFolder\n{C_LISTING}{code}\x1b[0m");
let code = SquashGroups.fold_stmt(code);
println!("SquashGroups\n{C_LISTING}{code}\x1b[0m");
@ -150,48 +127,14 @@ fn query_type_expression(prj: &mut Table) -> Result<(), RlError> {
if line.trim().is_empty() {
return Ok(Response::Break);
}
// A query is comprised of a Ty and a relative Path
let mut p = Parser::new("", Lexer::new(line));
let ty: Ty = p.parse()?;
let path: Path = p
.parse()
.map(|p| Path { absolute: false, ..p })
.unwrap_or_default();
// parse it as a path, and convert the path into a borrowed path
let ty: Ty = Parser::new(Lexer::new(line)).parse()?;
let id = ty.evaluate(prj, prj.root())?;
let id = path.evaluate(prj, id)?;
pretty_handle(id.to_entry(prj))?;
Ok(Response::Accept)
})
}
#[allow(dead_code)]
fn infer_expression(prj: &mut Table) -> Result<(), RlError> {
read_and(C_RESV, "ex>", "!?>", |line| {
if line.trim().is_empty() {
return Ok(Response::Break);
}
let mut p = Parser::new("", Lexer::new(line));
let e: Expr = p.parse()?;
let mut inf = InferenceEngine::new(prj, prj.root());
let ty = match exp_terned(e).infer(&mut inf) {
Ok(ty) => ty,
Err(e) => match e {
InferenceError::Mismatch(a, b) => {
eprintln!("Mismatched types: {}, {}", prj.entry(a), prj.entry(b));
return Ok(Response::Deny);
}
InferenceError::Recursive(a, b) => {
eprintln!("Recursive types: {}, {}", prj.entry(a), prj.entry(b));
return Ok(Response::Deny);
}
e => Err(e)?,
},
};
eprintln!("--> {}", prj.entry(ty));
Ok(Response::Accept)
})
}
fn get_by_id(prj: &mut Table) -> Result<(), RlError> {
use cl_parser::parser::Parse;
use cl_structures::index_map::MapIndex;
@ -200,7 +143,7 @@ fn get_by_id(prj: &mut Table) -> Result<(), RlError> {
if line.trim().is_empty() {
return Ok(Response::Break);
}
let mut parser = Parser::new("", Lexer::new(line));
let mut parser = Parser::new(Lexer::new(line));
let def_id = match Parse::parse(&mut parser)? {
cl_ast::Literal::Int(int) => int as _,
other => Err(format!("Expected integer, got {other}"))?,
@ -244,24 +187,6 @@ fn resolve_all(table: &mut Table) -> Result<(), Box<dyn Error>> {
Ok(())
}
fn infer_all(table: &mut Table) -> Result<(), Box<dyn Error>> {
for (id, error) in InferenceEngine::new(table, table.root()).infer_all() {
match error {
InferenceError::Mismatch(a, b) => {
eprint!("Mismatched types: {}, {}", table.entry(a), table.entry(b));
}
InferenceError::Recursive(a, b) => {
eprint!("Recursive types: {}, {}", table.entry(a), table.entry(b));
}
e => eprint!("{e}"),
}
eprintln!(" in {id}\n({})\n", id.to_entry(table).source().unwrap())
}
println!("...Inferred!");
Ok(())
}
fn list_types(table: &mut Table) {
for handle in table.debug_entry_iter() {
let id = handle.id();
@ -271,32 +196,6 @@ fn list_types(table: &mut Table) {
}
}
fn import_file(table: &mut Table, path: impl AsRef<std::path::Path>) -> Result<(), Box<dyn Error>> {
let Ok(file) = std::fs::read_to_string(path.as_ref()) else {
for file in std::fs::read_dir(path)? {
println!("{}", file?.path().display())
}
return Ok(());
};
let mut parser = Parser::new("", Lexer::new(&file));
let code = match parser.parse() {
Ok(code) => inline_modules(
code,
PathBuf::from(path.as_ref()).parent().unwrap_or("".as_ref()),
),
Err(e) => {
eprintln!("{C_ERROR}{}:{e}\x1b[0m", path.as_ref().display());
return Ok(());
}
};
let code = cl_ast::desugar::WhileElseDesugar.fold_file(code);
Populator::new(table).visit_file(interned(code));
Ok(())
}
fn import_files(table: &mut Table) -> Result<(), RlError> {
read_and(C_RESV, "fi>", "? >", |line| {
let line = line.trim();
@ -310,7 +209,7 @@ fn import_files(table: &mut Table) -> Result<(), RlError> {
return Ok(Response::Accept);
};
let mut parser = Parser::new("", Lexer::new(&file));
let mut parser = Parser::new(Lexer::new(&file));
let code = match parser.parse() {
Ok(code) => inline_modules(code, PathBuf::from(line).parent().unwrap_or("".as_ref())),
Err(e) => {
@ -405,30 +304,6 @@ fn inline_modules(code: cl_ast::File, path: impl AsRef<path::Path>) -> cl_ast::F
}
}
fn dump(table: &Table) -> Result<(), Box<dyn Error>> {
fn dump_recursive(
name: cl_ast::Sym,
entry: Entry,
depth: usize,
to_file: &mut std::fs::File,
) -> std::io::Result<()> {
use std::io::Write;
write!(to_file, "{:w$}{name}: {entry}", "", w = depth)?;
if let Some(children) = entry.children() {
writeln!(to_file, " {{")?;
for (name, child) in children {
dump_recursive(*name, entry.with_id(*child), depth + 2, to_file)?;
}
write!(to_file, "{:w$}}}", "", w = depth)?;
}
writeln!(to_file)
}
let mut file = std::fs::File::create("typeck-table.ron")?;
dump_recursive("root".into(), table.root_entry(), 0, &mut file)?;
Ok(())
}
fn clear() -> Result<(), Box<dyn Error>> {
println!("\x1b[H\x1b[2J");
banner();
@ -445,18 +320,9 @@ fn banner() {
/// Interns a [File](cl_ast::File), returning a static reference to it.
fn interned(file: cl_ast::File) -> &'static cl_ast::File {
use cl_structures::intern::typed_interner::TypedInterner;
use cl_structures::intern::{interned::Interned, typed_interner::TypedInterner};
static INTERNER: LazyLock<TypedInterner<'static, cl_ast::File>> =
LazyLock::new(Default::default);
INTERNER.get_or_insert(file).to_ref()
}
/// Interns an [Expr](cl_ast::Expr), returning a static reference to it.
fn exp_terned(expr: cl_ast::Expr) -> &'static cl_ast::Expr {
use cl_structures::intern::typed_interner::TypedInterner;
static INTERNER: LazyLock<TypedInterner<'static, cl_ast::Expr>> =
LazyLock::new(Default::default);
INTERNER.get_or_insert(expr).to_ref()
Interned::to_ref(&INTERNER.get_or_insert(file))
}

View File

@ -8,7 +8,7 @@
use std::collections::HashMap;
use cl_ast::{Expr, Meta, PathPart, Sym};
use cl_ast::{Meta, PathPart, Sym};
use cl_structures::span::Span;
use crate::{
@ -20,7 +20,6 @@ use crate::{
type_kind::TypeKind,
};
mod debug;
mod display;
impl Handle {
@ -32,82 +31,69 @@ impl Handle {
}
}
#[derive(Debug)]
pub struct Entry<'t, 'a> {
table: &'t Table<'a>,
id: Handle,
}
macro_rules! impl_entry_ {
() => {
pub const fn id(&self) -> Handle {
self.id
}
pub const fn inner(&'t self) -> &'t Table<'a> {
self.table
}
pub fn kind(&self) -> Option<&NodeKind> {
self.table.kind(self.id)
}
pub const fn root(&self) -> Handle {
self.table.root()
}
pub fn children(&self) -> Option<&HashMap<Sym, Handle>> {
self.table.children(self.id)
}
pub fn imports(&self) -> Option<&HashMap<Sym, Handle>> {
self.table.imports(self.id)
}
pub fn bodies(&self) -> Option<&'a Expr> {
self.table.body(self.id)
}
pub fn span(&self) -> Option<&Span> {
self.table.span(self.id)
}
pub fn meta(&self) -> Option<&[Meta]> {
self.table.meta(self.id)
}
pub fn source(&self) -> Option<&Source<'a>> {
self.table.source(self.id)
}
pub fn name(&self) -> Option<Sym> {
self.table.name(self.id)
}
};
}
impl<'t, 'a> Entry<'t, 'a> {
pub const fn new(table: &'t Table<'a>, id: Handle) -> Self {
Self { table, id }
}
impl_entry_!();
pub const fn id(&self) -> Handle {
self.id
}
pub const fn with_id(&self, id: Handle) -> Entry<'t, 'a> {
pub fn inner(&self) -> &Table<'a> {
self.table
}
pub const fn with_id(&self, id: Handle) -> Entry<'_, 'a> {
Self { table: self.table, id }
}
pub fn nav(&self, path: &[PathPart]) -> Option<Entry<'t, 'a>> {
pub fn nav(&self, path: &[PathPart]) -> Option<Entry<'_, 'a>> {
Some(Entry { id: self.table.nav(self.id, path)?, table: self.table })
}
pub fn parent(&self) -> Option<Entry<'t, 'a>> {
pub const fn root(&self) -> Handle {
self.table.root()
}
pub fn kind(&self) -> Option<&NodeKind> {
self.table.kind(self.id)
}
pub fn parent(&self) -> Option<Entry<'_, 'a>> {
Some(Entry { id: *self.table.parent(self.id)?, ..*self })
}
pub fn ty(&self) -> Option<&'t TypeKind> {
pub fn children(&self) -> Option<&HashMap<Sym, Handle>> {
self.table.children(self.id)
}
pub fn imports(&self) -> Option<&HashMap<Sym, Handle>> {
self.table.imports(self.id)
}
pub fn ty(&self) -> Option<&TypeKind> {
self.table.ty(self.id)
}
pub fn span(&self) -> Option<&Span> {
self.table.span(self.id)
}
pub fn meta(&self) -> Option<&'a [Meta]> {
self.table.meta(self.id)
}
pub fn source(&self) -> Option<&Source<'a>> {
self.table.source(self.id)
}
pub fn impl_target(&self) -> Option<Entry<'_, 'a>> {
Some(Entry { id: self.table.impl_target(self.id)?, ..*self })
}
@ -115,6 +101,10 @@ impl<'t, 'a> Entry<'t, 'a> {
pub fn selfty(&self) -> Option<Entry<'_, 'a>> {
Some(Entry { id: self.table.selfty(self.id)?, ..*self })
}
pub fn name(&self) -> Option<Sym> {
self.table.name(self.id)
}
}
#[derive(Debug)]
@ -128,20 +118,14 @@ impl<'t, 'a> EntryMut<'t, 'a> {
Self { table, id }
}
impl_entry_!();
pub fn ty(&self) -> Option<&TypeKind> {
self.table.ty(self.id)
}
pub fn inner_mut(&mut self) -> &mut Table<'a> {
self.table
}
pub fn as_ref(&self) -> Entry<'_, 'a> {
Entry { table: self.table, id: self.id }
}
pub const fn id(&self) -> Handle {
self.id
}
/// Evaluates a [TypeExpression] in this entry's context
pub fn evaluate<Out>(&mut self, ty: &impl TypeExpression<Out>) -> Result<Out, tex::Error> {
let Self { table, id } = self;
@ -170,10 +154,6 @@ impl<'t, 'a> EntryMut<'t, 'a> {
self.table.add_child(self.id, name, child)
}
pub fn set_body(&mut self, body: &'a Expr) -> Option<&'a Expr> {
self.table.set_body(self.id, body)
}
pub fn set_ty(&mut self, kind: TypeKind) -> Option<TypeKind> {
self.table.set_ty(self.id, kind)
}
@ -194,10 +174,6 @@ impl<'t, 'a> EntryMut<'t, 'a> {
self.table.set_impl_target(self.id, target)
}
pub fn mark_unchecked(&mut self) {
self.table.mark_unchecked(self.id)
}
pub fn mark_use_item(&mut self) {
self.table.mark_use_item(self.id)
}

View File

@ -1,33 +0,0 @@
//! [std::fmt::Debug] implementation for [Entry]
use super::Entry;
impl std::fmt::Debug for Entry<'_, '_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// virtual fields
let mut ds = f.debug_struct("Entry");
if let Some(name) = self.name() {
ds.field("name", &name.to_ref());
}
ds.field("kind", &self.kind());
if let Some(ty) = self.ty() {
ds.field("type", ty);
}
if let Some(meta) = self.meta() {
ds.field("meta", &meta);
}
if let Some(body) = self.bodies() {
ds.field("body", body);
}
if let Some(children) = self.children() {
ds.field("children", children);
}
if let Some(imports) = self.imports() {
ds.field("imports", imports);
}
// if let Some(source) = self.source() {
// ds.field("source", source);
// }
ds.field("implements", &self.impl_target()).finish()
}
}

View File

@ -18,21 +18,14 @@ impl fmt::Display for Entry<'_, '_> {
if let Some(ty) = self.ty() {
match ty {
TypeKind::Inferred => write!(f, "<_{}>", self.id),
TypeKind::Variable => write!(f, "<?{}>", self.id),
TypeKind::Instance(id) => write!(f, "{}", self.with_id(*id)),
TypeKind::Primitive(kind) => write!(f, "{kind}"),
TypeKind::Intrinsic(kind) => write!(f, "{kind}"),
TypeKind::Adt(adt) => write_adt(adt, self, f),
&TypeKind::Ref(id) => {
f.write_str("&")?;
let h_id = self.with_id(id);
write_name_or(h_id, f)
}
&TypeKind::Ptr(id) => {
f.write_str("*")?;
let h_id = self.with_id(id);
write_name_or(h_id, f)
}
TypeKind::Slice(id) => {
write_name_or(self.with_id(*id), &mut f.delimit_with("[", "]"))
}
@ -60,14 +53,7 @@ impl fmt::Display for Entry<'_, '_> {
TypeKind::Module => write!(f, "module?"),
}
} else {
match kind {
NodeKind::Type
| NodeKind::Const
| NodeKind::Static
| NodeKind::Temporary
| NodeKind::Let => write!(f, "WARNING: NO TYPE ASSIGNED FOR {}", self.id),
_ => write!(f, "{kind}"),
}
write!(f, "{kind}")
}
}
}
@ -78,7 +64,13 @@ fn write_adt(adt: &Adt, h: &Entry, f: &mut impl Write) -> fmt::Result {
let mut variants = variants.iter();
separate(", ", || {
variants.next().map(|(name, def)| {
move |f: &mut Delimit<_>| write!(f, "{name}: {}", h.with_id(*def))
move |f: &mut Delimit<_>| match def {
Some(def) => {
write!(f, "{name}: ")?;
write_name_or(h.with_id(*def), f)
}
None => write!(f, "{name}"),
}
})
})(f.delimit_with("enum {", "}"))
}
@ -86,14 +78,20 @@ fn write_adt(adt: &Adt, h: &Entry, f: &mut impl Write) -> fmt::Result {
let mut members = members.iter();
separate(", ", || {
let (name, vis, id) = members.next()?;
Some(move |f: &mut Delimit<_>| write!(f, "{vis}{name}: {}", h.with_id(*id)))
Some(move |f: &mut Delimit<_>| {
write!(f, "{vis}{name}: ")?;
write_name_or(h.with_id(*id), f)
})
})(f.delimit_with("struct {", "}"))
}
Adt::TupleStruct(members) => {
let mut members = members.iter();
separate(", ", || {
let (vis, def) = members.next()?;
Some(move |f: &mut Delimit<_>| write!(f, "{vis}{}", h.with_id(*def)))
Some(move |f: &mut Delimit<_>| {
write!(f, "{vis}")?;
write_name_or(h.with_id(*def), f)
})
})(f.delimit_with("struct (", ")"))
}
Adt::UnitStruct => write!(f, "struct"),

View File

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

View File

@ -1,7 +1,6 @@
//! Categorizes an entry in a table according to its embedded type information
#![allow(unused)]
use crate::{
entry::EntryMut,
handle::Handle,
source::Source,
table::{NodeKind, Table},
@ -12,37 +11,39 @@ use cl_ast::*;
/// Ensures a type entry exists for the provided handle in the table
pub fn categorize(table: &mut Table, node: Handle) -> CatResult<()> {
if let Some(meta) = table.meta(node) {
for meta @ Meta { name, kind } in meta {
if let ("intrinsic", MetaKind::Equals(Literal::String(s))) = (&**name, kind) {
let kind =
TypeKind::Intrinsic(s.parse().map_err(|_| Error::BadMeta(meta.clone()))?);
table.set_ty(node, kind);
return Ok(());
}
}
}
let Some(source) = table.source(node) else {
return Ok(());
};
match source {
Source::Alias(a) => cat_alias(table, node, a)?,
Source::Enum(e) => cat_enum(table, node, e)?,
Source::Variant(v) => cat_variant(table, node, v)?,
Source::Struct(s) => cat_struct(table, node, s)?,
Source::Const(c) => cat_const(table, node, c)?,
Source::Static(s) => cat_static(table, node, s)?,
Source::Function(f) => cat_function(table, node, f)?,
Source::Local(l) => cat_local(table, node, l)?,
Source::Impl(i) => cat_impl(table, node, i)?,
_ => {}
Source::Root => Ok(()),
Source::Module(_) => Ok(()),
Source::Alias(a) => cat_alias(table, node, a),
Source::Enum(e) => cat_enum(table, node, e),
Source::Variant(_) => Ok(()),
Source::Struct(s) => cat_struct(table, node, s),
Source::Const(c) => cat_const(table, node, c),
Source::Static(s) => cat_static(table, node, s),
Source::Function(f) => cat_function(table, node, f),
Source::Local(l) => cat_local(table, node, l),
Source::Impl(i) => cat_impl(table, node, i),
Source::Use(_) => Ok(()),
Source::Ty(ty) => ty
.evaluate(table, node)
.map_err(|e| Error::TypeEval(e, " while categorizing a type"))
.map(drop),
}
if let Some(meta) = table.meta(node) {
for meta @ Meta { name, kind } in meta {
if let ("lang", MetaKind::Equals(Literal::String(s))) = (&**name, kind) {
if let Ok(prim) = s.parse() {
table.set_ty(node, TypeKind::Primitive(prim));
} else {
table.mark_lang_item(s.into(), node);
continue;
}
return Ok(());
}
}
}
Ok(())
}
fn parent(table: &Table, node: Handle) -> Handle {
@ -64,14 +65,14 @@ fn cat_alias(table: &mut Table, node: Handle, a: &Alias) -> CatResult<()> {
}
fn cat_struct(table: &mut Table, node: Handle, s: &Struct) -> CatResult<()> {
let Struct { name: _, gens: _, kind } = s;
// TODO: Generics
let parent = parent(table, node);
let Struct { name: _, kind } = s;
let kind = match kind {
StructKind::Empty => TypeKind::Adt(Adt::UnitStruct),
StructKind::Tuple(types) => {
let mut out = vec![];
for ty in types {
out.push((Visibility::Public, ty.evaluate(table, node)?))
out.push((Visibility::Public, ty.evaluate(table, parent)?))
}
TypeKind::Adt(Adt::TupleStruct(out))
}
@ -97,55 +98,51 @@ fn cat_member(
Ok((*name, *vis, ty.evaluate(table, node)?))
}
fn cat_enum<'a>(_table: &mut Table<'a>, _node: Handle, e: &'a Enum) -> CatResult<()> {
let Enum { name: _, gens: _, variants: _ } = e;
fn cat_enum<'a>(table: &mut Table<'a>, node: Handle, e: &'a Enum) -> CatResult<()> {
let Enum { name: _, kind } = e;
let kind = match kind {
EnumKind::NoVariants => TypeKind::Adt(Adt::Enum(vec![])),
EnumKind::Variants(variants) => {
let mut out_vars = vec![];
for v in variants {
out_vars.push(cat_variant(table, node, v)?)
}
TypeKind::Adt(Adt::Enum(out_vars))
}
};
// table.set_ty(node, kind);
table.set_ty(node, kind);
Ok(())
}
fn cat_variant<'a>(table: &mut Table<'a>, node: Handle, v: &'a Variant) -> CatResult<()> {
let Variant { name, kind, body } = v;
let parent = table.parent(node).copied().unwrap_or(table.root());
match (kind, body) {
(StructKind::Empty, None) => {
table.set_ty(node, TypeKind::Adt(Adt::UnitStruct));
Ok(())
fn cat_variant<'a>(
table: &mut Table<'a>,
node: Handle,
v: &'a Variant,
) -> CatResult<(Sym, Option<Handle>)> {
let parent = parent(table, node);
let Variant { name, kind } = v;
match kind {
VariantKind::Plain => Ok((*name, None)),
VariantKind::CLike(c) => todo!("enum-variant constant {c}"),
VariantKind::Tuple(ty) => {
let ty = ty
.evaluate(table, parent)
.map_err(|e| Error::TypeEval(e, " while categorizing a variant"))?;
Ok((*name, Some(ty)))
}
(StructKind::Empty, Some(c)) => {
table.set_body(node, c);
table.set_ty(node, TypeKind::Adt(Adt::UnitStruct));
Ok(())
}
(StructKind::Tuple(ty), None) => {
let ty = TypeKind::Adt(Adt::TupleStruct(
ty.iter()
.map(|ty| ty.evaluate(table, node).map(|ty| (Visibility::Public, ty)))
.collect::<Result<_, _>>()?,
));
table.set_ty(node, ty);
Ok(())
}
(StructKind::Struct(members), None) => {
VariantKind::Struct(members) => {
let mut out = vec![];
for StructMember { vis, name, ty } in members {
let ty = ty.evaluate(table, node)?;
out.push((*name, *vis, ty));
let mut this = node.to_entry_mut(table);
let mut child = this.new_entry(NodeKind::Type);
child.set_source(Source::Variant(v));
child.set_ty(TypeKind::Instance(ty));
let child = child.id();
this.add_child(*name, child);
for m in members {
out.push(cat_member(table, node, m)?)
}
let kind = TypeKind::Adt(Adt::Struct(out));
table.set_ty(node, TypeKind::Adt(Adt::Struct(out)));
Ok(())
}
(_, Some(body)) => {
panic!("Unexpected body `{body}` in enum variant `{v}`")
let mut h = node.to_entry_mut(table);
let mut variant = h.new_entry(NodeKind::Type);
variant.set_source(Source::Variant(v));
variant.set_ty(kind);
Ok((*name, Some(variant.id())))
}
}
}
@ -171,9 +168,10 @@ fn cat_static(table: &mut Table, node: Handle, s: &Static) -> CatResult<()> {
}
fn cat_function(table: &mut Table, node: Handle, f: &Function) -> CatResult<()> {
let parent = parent(table, node);
let kind = TypeKind::Instance(
f.sign
.evaluate(table, node)
.evaluate(table, parent)
.map_err(|e| Error::TypeEval(e, " while categorizing a function"))?,
);
table.set_ty(node, kind);
@ -193,7 +191,7 @@ fn cat_local(table: &mut Table, node: Handle, l: &Let) -> CatResult<()> {
fn cat_impl(table: &mut Table, node: Handle, i: &Impl) -> CatResult<()> {
let parent = parent(table, node);
let Impl { gens, target, body: _ } = i;
let Impl { target, body: _ } = i;
let target = match target {
ImplKind::Type(t) => t.evaluate(table, parent),
ImplKind::Trait { impl_trait: _, for_type: t } => t.evaluate(table, parent),
@ -208,6 +206,7 @@ type CatResult<T> = Result<T, Error>;
#[derive(Clone, Debug)]
pub enum Error {
BadMeta(Meta),
Recursive(Handle),
TypeEval(TypeEval, &'static str),
}
@ -220,7 +219,10 @@ impl From<TypeEval> for Error {
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::BadMeta(meta) => write!(f, "Unknown attribute: #[{meta}]"),
Error::BadMeta(meta) => write!(f, "Unknown meta attribute: #[{meta}]"),
Error::Recursive(id) => {
write!(f, "Encountered recursive type without indirection: {id}")
}
Error::TypeEval(e, during) => write!(f, "{e}{during}"),
}
}

View File

@ -15,9 +15,9 @@ pub fn impl_one(table: &mut Table, node: Handle) -> Result<(), Handle> {
let Some(target) = table.impl_target(node) else {
Err(node)?
};
if let Some(children) = table.children.get_mut(&node) {
let children = children.clone();
table.children.entry(target).or_default().extend(children);
let Table { children, imports, .. } = table;
if let Some(children) = children.get(&node) {
imports.entry(target).or_default().extend(children);
}
Ok(())
}

View File

@ -70,7 +70,7 @@ fn import_tree<'a>(
UseTree::Path(part, rest) => {
let source = table
.nav(src, slice::from_ref(part))
.ok_or(Error::NotFound(src, *part))?;
.ok_or_else(|| Error::NotFound(src, part.clone()))?;
import_tree(table, source, dst, rest, seen)
}
UseTree::Alias(src_name, dst_name) => {

View File

@ -5,8 +5,244 @@
//! [1]: https://github.com/tcr/rust-hindley-milner/
//! [2]: https://github.com/rob-smallshire/hindley-milner-python
pub mod engine;
use cl_ast::Sym;
use core::fmt;
use std::{cell::RefCell, rc::Rc};
pub mod inference;
/*
Types in Conlang:
- Never type: !
- type !
- for<A> ! -> A
- Primitive types: bool, i32, (), ...
- type bool; ...
- Reference types: &T, *T
- for<T> type ref<T>; for<T> type ptr<T>
- Slice type: [T]
- for<T> type slice<T>
- Array type: [T;usize]
- for<T> type array<T, instanceof<usize>>
- Tuple type: (T, ...Z)
- for<T, ..> type tuple<T, ..> // on a per-case basis!
- Funct type: fn Tuple -> R
- for<T, R> type T -> R // on a per-case basis!
*/
pub mod error;
/// A refcounted [Type]
pub type RcType = Rc<Type>;
#[derive(Debug, PartialEq, Eq)]
pub struct Variable {
pub instance: RefCell<Option<RcType>>,
}
#[derive(Debug, PartialEq, Eq)]
pub struct Operator {
name: Sym,
types: RefCell<Vec<RcType>>,
}
/// A [Type::Variable] or [Type::Operator]:
/// - A [Type::Variable] can be either bound or unbound (instance: Some(_) | None)
/// - A [Type::Operator] has a name (used to identify the operator) and a list of types.
///
/// A type which contains unbound variables is considered "generic" (see
/// [`Type::is_generic()`]).
#[derive(Debug, PartialEq, Eq)]
pub enum Type {
Variable(Variable),
Operator(Operator),
}
impl Type {
/// Creates a new unbound [type variable](Type::Variable)
pub fn new_var() -> RcType {
Rc::new(Self::Variable(Variable { instance: RefCell::new(None) }))
}
/// Creates a variable that is a new instance of another [Type]
pub fn new_inst(of: &RcType) -> RcType {
Rc::new(Self::Variable(Variable {
instance: RefCell::new(Some(of.clone())),
}))
}
/// Creates a new [type operator](Type::Operator)
pub fn new_op(name: Sym, types: &[RcType]) -> RcType {
Rc::new(Self::Operator(Operator {
name,
types: RefCell::new(types.to_vec()),
}))
}
/// Creates a new [type operator](Type::Operator) representing a lambda
pub fn new_fn(takes: &RcType, returns: &RcType) -> RcType {
Self::new_op("fn".into(), &[takes.clone(), returns.clone()])
}
/// Creates a new [type operator](Type::Operator) representing a primitive type
pub fn new_prim(name: Sym) -> RcType {
Self::new_op(name, &[])
}
/// Creates a new [type operator](Type::Operator) representing a tuple
pub fn new_tuple(members: &[RcType]) -> RcType {
Self::new_op("tuple".into(), members)
}
/// Sets this type variable to be an instance `of` the other
/// # Panics
/// Panics if `self` is not a type variable
pub fn set_instance(self: &RcType, of: &RcType) {
match self.as_ref() {
Type::Operator(_) => unimplemented!("Cannot set instance of a type operator"),
Type::Variable(Variable { instance }) => *instance.borrow_mut() = Some(of.clone()),
}
}
/// Checks whether there are any unbound type variables in this type.
/// ```rust
/// # use cl_typeck::stage::infer::*;
/// let bool = Type::new_op("bool".into(), &[]);
/// let true_v = Type::new_inst(&bool);
/// let unbound = Type::new_var();
/// let id_fun = Type::new_fn(&unbound, &unbound);
/// let truthy = Type::new_fn(&unbound, &bool);
/// assert!(!bool.is_generic()); // bool contains no unbound type variables
/// assert!(!true_v.is_generic()); // true_v is bound to `bool`
/// assert!(unbound.is_generic()); // unbound is an unbound type variable
/// assert!(id_fun.is_generic()); // id_fun is a function with unbound type variables
/// assert!(truthy.is_generic()); // truthy is a function with one unbound type variable
/// ```
pub fn is_generic(self: &RcType) -> bool {
match self.as_ref() {
Type::Variable(Variable { instance }) => match instance.borrow().as_ref() {
// base case: self is an unbound type variable (instance is none)
None => true,
// Variable is bound to a type which may be generic
Some(instance) => instance.is_generic(),
},
Type::Operator(Operator { types, .. }) => {
// Operator may have generic args
types.borrow().iter().any(Self::is_generic)
}
}
}
/// Makes a deep copy of a type expression.
///
/// Bound variables are shared, unbound variables are duplicated.
pub fn deep_clone(self: &RcType) -> RcType {
// If there aren't any unbound variables, it's fine to clone the entire expression
if !self.is_generic() {
return self.clone();
}
// There are unbound type variables, so we make a new one
match self.as_ref() {
Type::Variable { .. } => Self::new_var(),
Type::Operator(Operator { name, types }) => Self::new_op(
*name,
&types
.borrow()
.iter()
.map(Self::deep_clone)
.collect::<Vec<_>>(),
),
}
}
/// Returns the defining instance of `self`,
/// collapsing type instances along the way.
/// # May panic
/// Panics if this type variable's instance field is already borrowed.
/// # Examples
/// ```rust
/// # use cl_typeck::stage::infer::*;
/// let t_bool = Type::new_op("bool".into(), &[]);
/// let t_nest = Type::new_inst(&Type::new_inst(&Type::new_inst(&t_bool)));
/// let pruned = t_nest.prune();
/// assert_eq!(pruned, t_bool);
/// assert_eq!(t_nest, Type::new_inst(&t_bool));
/// ```
pub fn prune(self: &RcType) -> RcType {
if let Type::Variable(Variable { instance }) = self.as_ref() {
if let Some(old_inst) = instance.borrow_mut().as_mut() {
let new_inst = old_inst.prune(); // get defining instance
*old_inst = new_inst.clone(); // collapse
return new_inst;
}
}
self.clone()
}
/// Checks whether a type expression occurs in another type expression
///
/// # Note:
/// - Since the test uses strict equality, `self` should be pruned prior to testing.
/// - The test is *not guaranteed to terminate* for recursive types.
pub fn occurs_in(self: &RcType, other: &RcType) -> bool {
if self == other {
return true;
}
match other.as_ref() {
Type::Variable(Variable { instance }) => match instance.borrow().as_ref() {
Some(t) => self.occurs_in(t),
None => false,
},
Type::Operator(Operator { types, .. }) => {
// Note: this might panic.
// Think about whether it panics for only recursive types?
types.borrow().iter().any(|other| self.occurs_in(other))
}
}
}
/// Unifies two type expressions, propagating changes via interior mutability
pub fn unify(self: &RcType, other: &RcType) -> Result<(), InferenceError> {
let (a, b) = (self.prune(), other.prune()); // trim the hedges
match (a.as_ref(), b.as_ref()) {
(Type::Variable { .. }, _) if !a.occurs_in(&b) => a.set_instance(&b),
(Type::Variable { .. }, _) => Err(InferenceError::Recursive(a, b))?,
(Type::Operator { .. }, Type::Variable { .. }) => b.unify(&a)?,
(
Type::Operator(Operator { name: a_name, types: a_types }),
Type::Operator(Operator { name: b_name, types: b_types }),
) => {
let (a_types, b_types) = (a_types.borrow(), b_types.borrow());
if a_name != b_name || a_types.len() != b_types.len() {
Err(InferenceError::Mismatch(a.clone(), b.clone()))?
}
for (a, b) in a_types.iter().zip(b_types.iter()) {
a.unify(b)?
}
}
}
Ok(())
}
}
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Type::Variable(Variable { instance }) => match instance.borrow().as_ref() {
Some(instance) => write!(f, "{instance}"),
None => write!(f, "_"),
},
Type::Operator(Operator { name, types }) => {
write!(f, "({name}")?;
for ty in types.borrow().iter() {
write!(f, " {ty}")?;
}
f.write_str(")")
}
}
}
}
/// An error produced during type inference
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum InferenceError {
Mismatch(RcType, RcType),
Recursive(RcType, RcType),
}
impl fmt::Display for InferenceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
InferenceError::Mismatch(a, b) => write!(f, "Type mismatch: {a:?} != {b:?}"),
InferenceError::Recursive(_, _) => write!(f, "Recursive type!"),
}
}
}

View File

@ -1,615 +0,0 @@
use std::{cell::Cell, collections::HashSet, rc::Rc};
use super::error::InferenceError;
use crate::{
entry::Entry,
handle::Handle,
source::Source,
stage::infer::inference::Inference,
table::{NodeKind, Table},
type_expression::TypeExpression,
type_kind::{Adt, Primitive, TypeKind},
};
use cl_ast::Sym;
/*
Types in Conlang:
- Never type: !
- type !
- for<A> ! -> A
- Primitive types: bool, i32, (), ...
- type bool; ...
- Reference types: &T, *T
- for<T> type ref<T>; for<T> type ptr<T>
- Slice type: [T]
- for<T> type slice<T>
- Array type: [T;usize]
- for<T> type array<T, instanceof<usize>>
- Tuple type: (T, ...Z)
- for<T, ..> type tuple<T, ..> // on a per-case basis!
- Funct type: fn Tuple -> R
- for<T, R> type T -> R // on a per-case basis!
*/
type HandleSet = Rc<Cell<Option<Handle>>>;
pub struct InferenceEngine<'table, 'a> {
pub(super) table: &'table mut Table<'a>,
/// The current working node
pub(crate) at: Handle,
/// The current breakset
pub(crate) bset: HandleSet,
/// The current returnset
pub(crate) rset: HandleSet,
}
impl<'table, 'a> InferenceEngine<'table, 'a> {
/// Infers the type of an object by deferring to [`Inference::infer()`]
pub fn infer(&mut self, inferrable: &'a impl Inference<'a>) -> Result<Handle, InferenceError> {
inferrable.infer(self)
}
/// Constructs a new [`InferenceEngine`], scoped around a [`Handle`] in a [`Table`].
pub fn new(table: &'table mut Table<'a>, at: Handle) -> Self {
Self { at, table, bset: Default::default(), rset: Default::default() }
}
/// Constructs an [`InferenceEngine`] that borrows the same table as `self`,
/// but with a shortened lifetime.
pub fn scoped(&mut self) -> InferenceEngine<'_, 'a> {
InferenceEngine {
at: self.at,
table: self.table,
bset: self.bset.clone(),
rset: self.rset.clone(),
}
}
pub fn infer_all(&mut self) -> Vec<(Handle, InferenceError)> {
let queue = std::mem::take(&mut self.table.unchecked);
let mut res = Vec::new();
for handle in queue {
let mut eng = self.at(handle);
let Some(source) = eng.table.source(handle) else {
eprintln!("No source found for {handle}");
continue;
};
println!("Inferring {source}");
let ret = match source {
Source::Module(v) => v.infer(&mut eng),
Source::Alias(v) => v.infer(&mut eng),
Source::Enum(v) => v.infer(&mut eng),
Source::Variant(v) => v.infer(&mut eng),
Source::Struct(v) => v.infer(&mut eng),
Source::Const(v) => v.infer(&mut eng),
Source::Static(v) => v.infer(&mut eng),
Source::Function(v) => v.infer(&mut eng),
Source::Local(v) => v.infer(&mut eng),
Source::Impl(v) => v.infer(&mut eng),
_ => Ok(eng.empty()),
};
match &ret {
&Ok(handle) => println!("=> {}", eng.entry(handle)),
Err(err @ InferenceError::AnnotationEval(_)) => eprintln!("=> ERROR: {err}"),
Err(InferenceError::FieldCount(h, want, got)) => {
eprintln!("=> ERROR: Field count {want} != {got} in {}", eng.entry(*h))
}
Err(err @ InferenceError::NotFound(_)) => eprintln!("=> ERROR: {err}"),
Err(InferenceError::Mismatch(h1, h2)) => eprintln!(
"=> ERROR: Type mismatch {} != {}",
eng.entry(*h1),
eng.entry(*h2)
),
Err(InferenceError::Recursive(h1, h2)) => eprintln!(
"=> ERROR: Cycle found in types {}, {}",
eng.entry(*h1),
eng.entry(*h2)
),
}
println!();
if let Err(err) = ret {
res.push((handle, err));
eng.table.mark_unchecked(handle);
}
}
res
}
/// Constructs a new InferenceEngine with the
pub fn at(&mut self, at: Handle) -> InferenceEngine<'_, 'a> {
InferenceEngine { at, ..self.scoped() }
}
pub fn open_bset(&mut self) -> InferenceEngine<'_, 'a> {
InferenceEngine { bset: Default::default(), ..self.scoped() }
}
pub fn open_rset(&mut self) -> InferenceEngine<'_, 'a> {
InferenceEngine { rset: Default::default(), ..self.scoped() }
}
pub fn bset(&mut self, ty: Handle) -> Result<(), InferenceError> {
match self.bset.get() {
Some(bset) => self.unify(ty, bset),
None => {
self.bset.set(Some(ty));
Ok(())
}
}
}
pub fn rset(&mut self, ty: Handle) -> Result<(), InferenceError> {
match self.rset.get() {
Some(rset) => self.unify(ty, rset),
None => {
self.rset.set(Some(ty));
Ok(())
}
}
}
/// Constructs an [Entry] out of a [Handle], for ease of use
pub fn entry(&self, of: Handle) -> Entry<'_, 'a> {
self.table.entry(of)
}
pub fn by_name<Out, N: TypeExpression<Out>>(
&mut self,
name: &N,
) -> Result<Out, crate::type_expression::Error> {
name.evaluate(self.table, self.at)
}
/// Creates a new unbound [type variable](Handle)
pub fn new_var(&mut self) -> Handle {
self.table.type_variable()
}
pub fn new_inferred(&mut self) -> Handle {
self.table.inferred_type()
}
/// Creates a variable that is a new instance of another [Type](Handle)
pub fn new_inst(&mut self, of: Handle) -> Handle {
self.table.anon_type(TypeKind::Instance(of))
}
/// Gets the defining usage of a type without collapsing intermediates
pub fn def_usage(&self, to: Handle) -> Handle {
match self.table.entry(to).ty() {
Some(TypeKind::Instance(id)) => self.def_usage(*id),
_ => to,
}
}
pub fn get_fn(&self, at: Handle, name: Sym) -> Option<(Handle, Handle)> {
use cl_ast::PathPart;
if let Some(&TypeKind::FnSig { args, rety }) = self
.entry(at)
.nav(&[PathPart::Ident(name)])
.as_ref()
.and_then(Entry::ty)
{
Some((args, rety))
} else {
None
}
}
/// Creates a new type variable representing a tuple
pub fn new_tuple(&mut self, tys: Vec<Handle>) -> Handle {
self.table.anon_type(TypeKind::Tuple(tys))
}
/// Creates a new type variable representing an array
pub fn new_array(&mut self, ty: Handle, size: usize) -> Handle {
self.table.anon_type(TypeKind::Array(ty, size))
}
/// Creates a new type variable representing a slice of contiguous memory
pub fn new_slice(&mut self, ty: Handle) -> Handle {
self.table.anon_type(TypeKind::Slice(ty))
}
/// Creates a new reference to a type
pub fn new_ref(&mut self, to: Handle) -> Handle {
self.table.anon_type(TypeKind::Ref(to))
}
/// All primitives must be predefined in the standard library.
pub fn primitive(&self, name: Sym) -> Option<Handle> {
// TODO: keep a map of primitives in the table root
self.table.get_by_sym(self.table.root(), &name)
}
pub fn never(&mut self) -> Handle {
self.table.anon_type(TypeKind::Never)
}
pub fn empty(&mut self) -> Handle {
self.table.anon_type(TypeKind::Empty)
}
pub fn bool(&self) -> Handle {
self.primitive("bool".into())
.expect("There should be a type named bool.")
}
pub fn char(&self) -> Handle {
self.primitive("char".into())
.expect("There should be a type named char.")
}
pub fn str(&self) -> Handle {
self.primitive("str".into())
.expect("There should be a type named str.")
}
pub fn u32(&self) -> Handle {
self.primitive("u32".into())
.expect("There should be a type named u32.")
}
pub fn usize(&self) -> Handle {
self.primitive("usize".into())
.expect("There should be a type named usize.")
}
/// Creates a new inferred-integer literal
pub fn integer_literal(&mut self) -> Handle {
let h = self.table.new_entry(self.at, NodeKind::Temporary);
self.table
.set_ty(h, TypeKind::Primitive(Primitive::Integer));
h
}
/// Creates a new inferred-float literal
pub fn float_literal(&mut self) -> Handle {
let h = self.table.new_entry(self.at, NodeKind::Temporary);
self.table.set_ty(h, TypeKind::Primitive(Primitive::Float));
h
}
/// Enters a new scope
pub fn local_scope(&mut self, name: Sym) {
let scope = self.table.new_entry(self.at, NodeKind::Scope);
self.table.add_child(self.at, name, scope);
self.at = scope;
}
/// Creates a new locally-scoped InferenceEngine.
pub fn block_scope(&mut self) -> InferenceEngine<'_, 'a> {
let scope = self.table.new_entry(self.at, NodeKind::Scope);
self.table.add_child(self.at, "".into(), scope);
self.at(scope)
}
/// Sets this type variable `to` be an instance `of` the other
/// # Panics
/// Panics if `to` is not a type variable
pub fn set_instance(&mut self, to: Handle, of: Handle) {
let mut e = self.table.entry_mut(to);
match e.as_ref().ty() {
Some(TypeKind::Inferred) => {
if let Some(ty) = self.table.ty(of) {
self.table.set_ty(to, ty.clone());
}
None
}
Some(TypeKind::Variable)
| Some(TypeKind::Primitive(Primitive::Float | Primitive::Integer)) => {
e.set_ty(TypeKind::Instance(of))
}
other => todo!("Cannot set {} to instance of: {other:?}", e.as_ref()),
};
}
/// Checks whether there are any unbound type variables in this type
pub fn is_generic(&self, ty: Handle) -> bool {
fn is_generic_rec(this: &InferenceEngine, ty: Handle, seen: &mut HashSet<Handle>) -> bool {
if !seen.insert(ty) {
return false;
}
let entry = this.table.entry(ty);
let Some(ty) = entry.ty() else {
return false;
};
match ty {
TypeKind::Inferred => false,
TypeKind::Variable => true,
&TypeKind::Array(ty, _) => is_generic_rec(this, ty, seen),
&TypeKind::Instance(ty) => is_generic_rec(this, ty, seen),
TypeKind::Primitive(_) => false,
TypeKind::Adt(Adt::Enum(tys)) => {
tys.iter().any(|&(_, ty)| is_generic_rec(this, ty, seen))
}
TypeKind::Adt(Adt::Struct(tys)) => {
tys.iter().any(|&(_, _, ty)| is_generic_rec(this, ty, seen))
}
TypeKind::Adt(Adt::TupleStruct(tys)) => {
tys.iter().any(|&(_, ty)| is_generic_rec(this, ty, seen))
}
TypeKind::Adt(Adt::UnitStruct) => false,
TypeKind::Adt(Adt::Union(tys)) => {
tys.iter().any(|&(_, ty)| is_generic_rec(this, ty, seen))
}
&TypeKind::Ref(ty) => is_generic_rec(this, ty, seen),
&TypeKind::Ptr(ty) => is_generic_rec(this, ty, seen),
&TypeKind::Slice(ty) => is_generic_rec(this, ty, seen),
TypeKind::Tuple(tys) => tys.iter().any(|&ty| is_generic_rec(this, ty, seen)),
&TypeKind::FnSig { args, rety } => {
is_generic_rec(this, args, seen) || is_generic_rec(this, rety, seen)
}
TypeKind::Empty | TypeKind::Never | TypeKind::Module => false,
}
}
is_generic_rec(self, ty, &mut HashSet::new())
}
/// Makes a deep copy of a type expression.
///
/// Bound variables are shared, unbound variables are duplicated.
pub fn deep_clone(&mut self, ty: Handle) -> Handle {
if !self.is_generic(ty) {
return ty;
};
let entry = self.table.entry(ty);
let Some(ty) = entry.ty().cloned() else {
return ty;
};
// TODO: Parent the deep clone into a new "monomorphs" branch of tree
match ty {
TypeKind::Variable => self.new_inferred(),
TypeKind::Array(h, s) => {
let ty = self.deep_clone(h);
self.table.anon_type(TypeKind::Array(ty, s))
}
TypeKind::Instance(h) => {
let ty = self.deep_clone(h);
self.table.anon_type(TypeKind::Instance(ty))
}
TypeKind::Adt(Adt::Enum(tys)) => {
let tys = tys
.into_iter()
.map(|(name, ty)| (name, self.deep_clone(ty)))
.collect();
self.table.anon_type(TypeKind::Adt(Adt::Enum(tys)))
}
TypeKind::Adt(Adt::Struct(tys)) => {
let tys = tys
.into_iter()
.map(|(n, v, ty)| (n, v, self.deep_clone(ty)))
.collect();
self.table.anon_type(TypeKind::Adt(Adt::Struct(tys)))
}
TypeKind::Adt(Adt::TupleStruct(tys)) => {
let tys = tys
.into_iter()
.map(|(v, ty)| (v, self.deep_clone(ty)))
.collect();
self.table.anon_type(TypeKind::Adt(Adt::TupleStruct(tys)))
}
TypeKind::Adt(Adt::Union(tys)) => {
let tys = tys
.into_iter()
.map(|(n, ty)| (n, self.deep_clone(ty)))
.collect();
self.table.anon_type(TypeKind::Adt(Adt::Union(tys)))
}
TypeKind::Ref(h) => {
let ty = self.deep_clone(h);
self.table.anon_type(TypeKind::Ref(ty))
}
TypeKind::Slice(h) => {
let ty = self.deep_clone(h);
self.table.anon_type(TypeKind::Slice(ty))
}
TypeKind::Tuple(tys) => {
let tys = tys.into_iter().map(|ty| self.deep_clone(ty)).collect();
self.table.anon_type(TypeKind::Tuple(tys))
}
TypeKind::FnSig { args, rety } => {
let args = self.deep_clone(args);
let rety = self.deep_clone(rety);
self.table.anon_type(TypeKind::FnSig { args, rety })
}
_ => self.table.anon_type(ty),
}
}
/// Returns the defining instance of `self`,
/// collapsing type instances along the way.
pub fn prune(&mut self, ty: Handle) -> Handle {
if let Some(TypeKind::Instance(new_ty)) = self.table.ty(ty) {
let new_ty = self.prune(*new_ty);
self.table.set_ty(ty, TypeKind::Instance(new_ty));
new_ty
} else {
ty
}
}
/// Checks whether a type occurs in another type
///
/// # Note:
/// - Since the test uses strict equality, `self` should be pruned prior to testing.
/// - The test is *not guaranteed to terminate* for recursive types.
pub fn occurs_in(&self, this: Handle, other: Handle) -> bool {
if this == other {
return true;
}
let Some(ty) = self.table.ty(other) else {
return false;
};
match ty {
TypeKind::Instance(other) => self.occurs_in(this, *other),
TypeKind::Adt(Adt::Enum(items)) => {
items.iter().any(|(_, other)| self.occurs_in(this, *other))
}
TypeKind::Adt(Adt::Struct(items)) => items
.iter()
.any(|(_, _, other)| self.occurs_in(this, *other)),
TypeKind::Adt(Adt::TupleStruct(items)) => {
items.iter().any(|(_, other)| self.occurs_in(this, *other))
}
TypeKind::Adt(Adt::Union(items)) => {
items.iter().any(|(_, other)| self.occurs_in(this, *other))
}
TypeKind::Ref(other) => self.occurs_in(this, *other),
TypeKind::Ptr(other) => self.occurs_in(this, *other),
TypeKind::Slice(other) => self.occurs_in(this, *other),
TypeKind::Array(other, _) => self.occurs_in(this, *other),
TypeKind::Tuple(handles) => handles.iter().any(|&other| self.occurs_in(this, other)),
TypeKind::FnSig { args, rety } => {
self.occurs_in(this, *args) || self.occurs_in(this, *rety)
}
TypeKind::Inferred
| TypeKind::Variable
| TypeKind::Adt(Adt::UnitStruct)
| TypeKind::Primitive(_)
| TypeKind::Empty
| TypeKind::Never
| TypeKind::Module => false,
}
}
/// Unifies two types
pub fn unify(&mut self, this: Handle, other: Handle) -> Result<(), InferenceError> {
let (ah, bh) = (self.prune(this), self.prune(other));
if ah == bh {
return Ok(());
}
let (a, b) = (self.table.entry(ah), self.table.entry(bh));
let (Some(a), Some(b)) = (a.ty(), b.ty()) else {
return Err(InferenceError::Mismatch(ah, bh));
};
match (a, b) {
(TypeKind::Inferred, _) => {
self.set_instance(ah, bh);
Ok(())
}
(_, TypeKind::Inferred) => self.unify(bh, ah),
(TypeKind::Variable, _) => Err(InferenceError::Mismatch(ah, bh)),
(TypeKind::Instance(a), TypeKind::Instance(b)) if !self.occurs_in(*a, *b) => {
self.set_instance(*a, *b);
Ok(())
}
(TypeKind::Instance(_), _) => Err(InferenceError::Recursive(ah, bh)),
(TypeKind::Primitive(Primitive::Float), TypeKind::Primitive(Primitive::Integer))
| (TypeKind::Primitive(Primitive::Integer), TypeKind::Primitive(Primitive::Float)) => {
Err(InferenceError::Mismatch(ah, bh))
}
// Primitives have their own set of vars which only unify with primitives.
(TypeKind::Primitive(Primitive::Integer), TypeKind::Primitive(i)) if i.is_integer() => {
self.set_instance(ah, bh);
Ok(())
}
(TypeKind::Primitive(Primitive::Float), TypeKind::Primitive(f)) if f.is_float() => {
self.set_instance(ah, bh);
Ok(())
}
(_, TypeKind::Variable)
| (_, TypeKind::Instance(_))
| (TypeKind::Primitive(_), TypeKind::Primitive(Primitive::Integer))
| (TypeKind::Primitive(_), TypeKind::Primitive(Primitive::Float)) => self.unify(bh, ah),
(TypeKind::Adt(Adt::Enum(ia)), TypeKind::Adt(Adt::Enum(ib)))
if ia.len() == ib.len() =>
{
for ((na, a), (nb, b)) in ia.clone().into_iter().zip(ib.clone().into_iter()) {
if na != nb {
return Err(InferenceError::Mismatch(ah, bh));
}
self.unify(a, b)?;
}
Ok(())
}
(TypeKind::Adt(Adt::Enum(en)), TypeKind::Adt(_)) => {
#[allow(unused)]
let Some(other_parent) = self.table.parent(bh) else {
Err(InferenceError::Mismatch(ah, bh))?
};
if ah != *other_parent {
Err(InferenceError::Mismatch(ah, *other_parent))?
}
#[allow(unused)]
for (sym, handle) in en {
let handle = self.def_usage(*handle);
if handle == bh {
return Ok(());
}
}
Err(InferenceError::Mismatch(ah, bh))
}
(TypeKind::Adt(Adt::Struct(ia)), TypeKind::Adt(Adt::Struct(ib)))
if ia.len() == ib.len() =>
{
for ((na, va, a), (nb, vb, b)) in ia.clone().into_iter().zip(ib.clone().into_iter())
{
if na != nb || va != vb {
return Err(InferenceError::Mismatch(ah, bh));
}
self.unify(a, b)?;
}
Ok(())
}
(TypeKind::Adt(Adt::TupleStruct(ia)), TypeKind::Adt(Adt::TupleStruct(ib)))
if ia.len() == ib.len() =>
{
for ((va, a), (vb, b)) in ia.clone().into_iter().zip(ib.clone().into_iter()) {
if va != vb {
return Err(InferenceError::Mismatch(ah, bh));
}
self.unify(a, b)?;
}
Ok(())
}
(TypeKind::Adt(Adt::Union(ia)), TypeKind::Adt(Adt::Union(ib)))
if ia.len() == ib.len() =>
{
todo!()
}
(TypeKind::Ref(a), TypeKind::Ref(b)) => self.unify(*a, *b),
(TypeKind::Ptr(a), TypeKind::Ptr(b)) => self.unify(*a, *b),
(TypeKind::Slice(a), TypeKind::Slice(b)) => self.unify(*a, *b),
// Slice unifies with array
(TypeKind::Array(a, _), TypeKind::Slice(b)) => self.unify(*a, *b),
(TypeKind::Slice(_), TypeKind::Array(_, _)) => self.unify(bh, ah),
(TypeKind::Array(a, sa), TypeKind::Array(b, sb)) if sa == sb => self.unify(*a, *b),
(TypeKind::Tuple(a), TypeKind::Tuple(b)) => {
if a.len() != b.len() {
return Err(InferenceError::Mismatch(ah, bh));
}
let (a, b) = (a.clone(), b.clone());
for (a, b) in a.iter().zip(b.iter()) {
self.unify(*a, *b)?;
}
Ok(())
}
(&TypeKind::FnSig { args: a1, rety: r1 }, &TypeKind::FnSig { args: a2, rety: r2 }) => {
self.unify(a1, a2)?;
self.unify(r1, r2)
}
(TypeKind::Empty, TypeKind::Tuple(t)) | (TypeKind::Tuple(t), TypeKind::Empty)
if t.is_empty() =>
{
Ok(())
}
(TypeKind::Never, _) | (_, TypeKind::Never) => Ok(()),
(a, b) if a == b => Ok(()),
_ => Err(InferenceError::Mismatch(ah, bh)),
}
}
}

View File

@ -1,39 +0,0 @@
use cl_ast::Path;
use crate::handle::Handle;
use core::fmt;
/// An error produced during type inference
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum InferenceError {
AnnotationEval(crate::type_expression::Error),
FieldCount(Handle, usize, usize),
NotFound(Path),
Mismatch(Handle, Handle),
Recursive(Handle, Handle),
}
impl std::error::Error for InferenceError {}
#[rustfmt::skip]
impl fmt::Display for InferenceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
InferenceError::AnnotationEval(error) => write!(f, "{error}"),
InferenceError::FieldCount(name, want, got) => {
write!(f,
"Struct {name} {} fields! Expected {want}, got {got}",
if want < got { "has too many" } else { "is missing" }
)
}
InferenceError::NotFound(p) => write!(f, "Path not visible in scope: {p}"),
InferenceError::Mismatch(a, b) => write!(f, "Type mismatch: {a:?} != {b:?}"),
InferenceError::Recursive(_, _) => write!(f, "Recursive type!"),
}
}
}
impl From<crate::type_expression::Error> for InferenceError {
fn from(value: crate::type_expression::Error) -> Self {
Self::AnnotationEval(value)
}
}

View File

@ -1,970 +0,0 @@
//! The [Inference] trait is the heart of cl-typeck's type inference.
//!
//! Each syntax structure must describe how to unify its types.
use std::iter;
use super::{engine::InferenceEngine, error::InferenceError};
use crate::{
handle::Handle,
type_expression::TypeExpression,
type_kind::{Adt, TypeKind},
};
use cl_ast::*;
// TODO: "Infer" the types of Items
type IfResult = Result<Handle, InferenceError>;
pub trait Inference<'a> {
/// Performs type inference
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult;
}
impl<'a> Inference<'a> for File {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Self { name: _, items } = self;
for item in items {
item.infer(e)?;
}
Ok(e.empty())
}
}
impl<'a> Inference<'a> for Item {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Self { span: _, attrs: _, vis: _, kind } = self;
kind.infer(e)
}
}
impl<'a> Inference<'a> for ItemKind {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
match self {
ItemKind::Module(v) => v.infer(e),
ItemKind::Alias(v) => v.infer(e),
ItemKind::Enum(v) => v.infer(e),
ItemKind::Struct(v) => v.infer(e),
ItemKind::Const(v) => v.infer(e),
ItemKind::Static(v) => v.infer(e),
ItemKind::Function(v) => v.infer(e),
ItemKind::Impl(v) => v.infer(e),
ItemKind::Use(_v) => Ok(e.empty()),
}
}
}
impl<'a> Inference<'a> for Generics {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
// bind names
for name in &self.vars {
let ty = e.new_var();
e.table.add_child(e.at, *name, ty);
}
Ok(e.empty())
}
}
impl<'a> Inference<'a> for Module {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Self { name, file } = self;
let Some(file) = file else {
return Err(InferenceError::NotFound((*name).into()));
};
let module = e.by_name(name)?;
e.at(module).infer(file)
}
}
impl<'a> Inference<'a> for Alias {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
Ok(e.empty())
}
}
impl<'a> Inference<'a> for Const {
#[allow(unused)]
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Self { name, ty, init } = self;
// Same as static
let node = e.by_name(name)?;
let ty = e.infer(ty)?;
let mut scope = e.at(node);
// infer body
let body = scope.infer(init)?;
// unify with ty
e.unify(body, ty)?;
Ok(node)
}
}
impl<'a> Inference<'a> for Static {
#[allow(unused)]
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Static { mutable, name, ty, init } = self;
let node = e.by_name(name)?;
let ty = e.infer(ty)?;
let mut scope = e.at(node);
// infer body
let body = scope.infer(init)?;
// unify with ty
e.unify(body, ty)?;
Ok(node)
}
}
impl<'a> Inference<'a> for Function {
#[allow(unused)]
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Self { name, gens, sign, bind, body } = self;
// bind name to signature
let node = e.by_name(name)?;
let node = e.deep_clone(node);
let fnty = e.by_name(sign)?;
e.unify(node, fnty)?;
// bind gens to new variables at function scope
let mut scope = e.at(node);
scope.infer(gens)?;
// bind binds to args
let pat = scope.infer(bind)?;
let arg = scope.by_name(sign.args.as_ref())?;
scope.unify(pat, arg);
let mut retscope = scope.open_rset();
// infer body
let bodty = retscope.infer(body)?;
let rety = sign.rety.infer(&mut retscope)?;
// unify body with rety
retscope.unify(bodty, rety)?;
// unify rset with rety
if let Some(rset) = retscope.rset.get() {
scope.unify(rset, rety)?;
}
Ok(node)
}
}
// TODO: do we need type inference/checking in struct definitions?
// there are no bodies
impl<'a> Inference<'a> for Enum {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Self { name, gens, variants } = self;
let node = e.by_name(name)?;
let mut scope = e.at(node);
scope.infer(gens)?;
for variant in variants {
let var_ty = scope.infer(variant)?;
scope.unify(var_ty, node)?;
}
Ok(node)
}
}
impl<'a> Inference<'a> for Variant {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Self { name: _, kind: _, body } = self;
let ty = e.new_inferred();
// TODO: evaluate kind
if let Some(body) = body {
let value = body.infer(e)?;
e.unify(ty, value)?;
}
Ok(ty)
}
}
impl<'a> Inference<'a> for Struct {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
Ok(e.new_inferred())
}
}
impl<'a> Inference<'a> for Impl {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Self { gens: _, target, body } = self;
// TODO: match gens to target gens
// gens.infer(e)?;
let instance = target.infer(e)?;
let instance = e.def_usage(instance);
let mut scope = e.at(instance);
scope.infer(body)
}
}
impl<'a> Inference<'a> for ImplKind {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
match self {
ImplKind::Type(ty) => ty.infer(e),
ImplKind::Trait { impl_trait: _, for_type } => for_type.infer(e),
}
}
}
impl<'a> Inference<'a> for Ty {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
Ok(e.by_name(self)?)
}
}
impl<'a> Inference<'a> for cl_ast::Stmt {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Self { span: _, kind, semi } = self;
let out = kind.infer(e)?;
Ok(match semi {
Semi::Terminated => e.empty(),
Semi::Unterminated => out,
})
}
}
impl<'a> Inference<'a> for cl_ast::StmtKind {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
match self {
StmtKind::Empty => Ok(e.empty()),
StmtKind::Item(item) => item.infer(e),
StmtKind::Expr(expr) => expr.infer(e),
}
}
}
impl<'a> Inference<'a> for cl_ast::Expr {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let out = self.kind.infer(e)?;
println!("expr ({self}) -> {}", e.entry(out));
Ok(out)
}
}
impl<'a> Inference<'a> for cl_ast::ExprKind {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
match self {
ExprKind::Empty => Ok(e.empty()),
ExprKind::Closure(closure) => closure.infer(e),
ExprKind::Tuple(tuple) => tuple.infer(e),
ExprKind::Structor(structor) => structor.infer(e),
ExprKind::Array(array) => array.infer(e),
ExprKind::ArrayRep(array_rep) => array_rep.infer(e),
ExprKind::AddrOf(addr_of) => addr_of.infer(e),
ExprKind::Quote(quote) => quote.infer(e),
ExprKind::Literal(literal) => literal.infer(e),
ExprKind::Group(group) => group.infer(e),
ExprKind::Block(block) => block.infer(e),
ExprKind::Assign(assign) => assign.infer(e),
ExprKind::Modify(modify) => modify.infer(e),
ExprKind::Binary(binary) => binary.infer(e),
ExprKind::Unary(unary) => unary.infer(e),
ExprKind::Member(member) => member.infer(e),
ExprKind::Index(index) => index.infer(e),
ExprKind::Path(path) => path.infer(e),
ExprKind::Cast(cast) => cast.infer(e),
ExprKind::Let(l) => l.infer(e),
ExprKind::Match(m) => m.infer(e),
ExprKind::While(w) => w.infer(e),
ExprKind::If(i) => i.infer(e),
ExprKind::For(f) => f.infer(e),
ExprKind::Break(b) => b.infer(e),
ExprKind::Return(r) => r.infer(e),
ExprKind::Continue => Ok(e.never()),
}
}
}
impl<'a> Inference<'a> for Closure {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Self { arg, body } = self;
let args = arg.infer(e)?;
let mut scope = e.block_scope();
let mut scope = scope.open_rset();
let rety = scope.infer(body)?;
if let Some(rset) = scope.rset.get() {
e.unify(rety, rset)?;
}
Ok(e.table.anon_type(TypeKind::FnSig { args, rety }))
}
}
impl<'a> Inference<'a> for Tuple {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Tuple { exprs } = self;
exprs
.iter()
// Infer each member
.map(|expr| expr.infer(e))
// Construct tuple
.collect::<Result<Vec<_>, InferenceError>>()
// Return tuple
.map(|tys| e.new_tuple(tys))
}
}
impl<'a> Inference<'a> for Structor {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Structor { to, init } = self;
// Evaluate the path in the current context
let to = to.infer(e)?;
match e.entry(to).ty() {
// Typecheck the fielders against the fields
Some(TypeKind::Adt(Adt::Struct(fields))) => {
if init.len() != fields.len() {
return Err(InferenceError::FieldCount(to, fields.len(), init.len()));
}
let fields = fields.clone(); // todo: fix this somehow.
let mut field_inits: std::collections::HashMap<_, _> = init
.iter()
.map(|Fielder { name, init }| (name, init))
.collect();
// Unify fields with fielders
for (name, _vis, ty) in fields {
match field_inits.remove(&name) {
Some(Some(field)) => {
let init_ty = field.infer(e)?;
e.unify(init_ty, ty)?;
}
Some(None) => {
// Get name in scope
let init_ty = e
.table
.get_by_sym(e.at, &name)
.ok_or_else(|| InferenceError::NotFound(Path::from(name)))?;
e.unify(init_ty, ty)?;
}
None => Err(InferenceError::NotFound(Path::from(name)))?,
}
}
Ok(to)
}
_ => Err(InferenceError::NotFound(self.to.clone())),
}
}
}
impl<'a> Inference<'a> for Array {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Array { values } = self;
let out = e.new_inferred();
for value in values {
let ty = value.infer(e)?;
e.unify(out, ty)?;
}
Ok(e.new_array(out, values.len()))
}
}
impl<'a> Inference<'a> for ArrayRep {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let ArrayRep { value, repeat } = self;
let ty = value.infer(e)?;
let rep = repeat.infer(e)?;
let usize_ty = e.usize();
e.unify(rep, usize_ty)?;
match &repeat.kind {
ExprKind::Literal(Literal::Int(repeat)) => Ok(e.new_array(ty, *repeat as usize)),
_ => {
todo!("TODO: constant folding before type checking?");
}
}
}
}
impl<'a> Inference<'a> for AddrOf {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let AddrOf { mutable: _, expr } = self;
// TODO: mut ref
let ty = expr.infer(e)?;
Ok(e.new_ref(ty))
}
}
impl<'a> Inference<'a> for Quote {
fn infer(&'a self, _e: &mut InferenceEngine<'_, 'a>) -> IfResult {
todo!("Quote: {self}")
}
}
impl<'a> Inference<'a> for Literal {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let ty = match self {
Literal::Bool(_) => e.bool(),
Literal::Char(_) => e.char(),
Literal::Int(_) => e.integer_literal(),
Literal::Float(_) => e.float_literal(),
Literal::String(_) => {
let str_ty = e.str();
e.new_ref(str_ty)
}
};
Ok(e.new_inst(ty))
}
}
impl<'a> Inference<'a> for Group {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Group { expr } = self;
expr.infer(e)
}
}
impl<'a> Inference<'a> for Block {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Block { stmts } = self;
let mut e = e.block_scope();
let empty = e.empty();
if let [stmts @ .., ret] = stmts.as_slice() {
for stmt in stmts {
match (&stmt.kind, &stmt.semi) {
(StmtKind::Expr(expr), Semi::Terminated) => {
expr.infer(&mut e)?;
}
(StmtKind::Expr(expr), Semi::Unterminated) => {
let ty = expr.infer(&mut e)?;
e.unify(ty, empty)?;
}
_ => {}
}
}
let out = if let StmtKind::Expr(expr) = &ret.kind {
expr.infer(&mut e)?
} else {
empty
};
if Semi::Unterminated == ret.semi {
return Ok(out);
}
}
Ok(empty)
}
}
impl<'a> Inference<'a> for Assign {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Assign { parts } = self;
let (head, tail) = parts.as_ref();
// Infer the tail expression
let tail = tail.infer(e)?;
// Infer the head expression
let head = head.infer(e)?;
// Unify head and tail
e.unify(head, tail)?;
// Return Empty
Ok(e.empty())
}
}
impl<'a> Inference<'a> for Modify {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Modify { kind: _, parts } = self;
let (head, tail) = parts.as_ref();
// Infer the tail expression
let tail = tail.infer(e)?;
// Infer the head expression
let head = head.infer(e)?;
// TODO: Search within the head type for `(op)_assign`
e.unify(head, tail)?;
// TODO: Typecheck `op_assign(&mut head, tail)`
Ok(e.empty())
}
}
impl<'a> Inference<'a> for Binary {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
use BinaryKind as Bk;
let Binary { kind, parts } = self;
let (head, tail) = parts.as_ref();
// Infer the tail expression
let tail = tail.infer(e)?;
// Infer the head expression
let head = head.infer(e)?;
let head = e.prune(head);
// TODO: Search within the head type for `(op)`
match kind {
BinaryKind::Call => match e.entry(head).ty() {
Some(TypeKind::Adt(Adt::TupleStruct(types))) => {
let Some(TypeKind::Tuple(values)) = e.entry(tail).ty() else {
Err(InferenceError::Mismatch(head, tail))?
};
if types.len() != values.len() {
Err(InferenceError::FieldCount(head, types.len(), values.len()))?
}
let pairs = types
.iter()
.zip(values.iter())
.map(|(&(_vis, ty), &value)| (ty, value))
.collect::<Vec<_>>();
for (ty, value) in pairs {
e.unify(ty, value)?;
}
Ok(head)
}
Some(&TypeKind::FnSig { args, rety }) => {
e.unify(tail, args)?;
Ok(rety)
}
_ => Err(InferenceError::Mismatch(head, tail))?,
},
Bk::Lt | Bk::LtEq | Bk::Equal | Bk::NotEq | Bk::GtEq | Bk::Gt => {
e.unify(head, tail)?;
Ok(e.bool())
}
Bk::LogAnd | Bk::LogOr | Bk::LogXor => {
let bool = e.bool();
e.unify(head, bool)?;
e.unify(tail, bool)?;
Ok(bool)
}
Bk::RangeExc => todo!("Ranges in the type checker"),
Bk::RangeInc => todo!("Ranges in the type checker"),
Bk::Shl | Bk::Shr => {
let shift_amount = e.u32();
e.unify(tail, shift_amount)?;
Ok(head)
}
Bk::BitAnd
| Bk::BitOr
| Bk::BitXor
| Bk::Add
| Bk::Sub
| Bk::Mul
| Bk::Div
| Bk::Rem => {
// Typecheck op(head, tail)
e.unify(head, tail)?;
Ok(head)
}
}
}
}
impl<'a> Inference<'a> for Unary {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Unary { kind, tail } = self;
match kind {
UnaryKind::Deref => {
let tail = tail.infer(e)?;
let tail = e.def_usage(tail);
// TODO: get the base type
match e.entry(tail).ty() {
Some(&TypeKind::Ref(h)) => Ok(h),
_ => todo!("Deref {}", e.entry(tail)),
}
}
UnaryKind::Loop => {
let mut e = e.block_scope();
// Enter a new breakset
let mut e = e.open_bset();
// Infer the fail branch
let tail = tail.infer(&mut e)?;
// Unify the pass branch with Empty
let empt = e.empty();
e.unify(tail, empt)?;
// Return breakset
match e.bset.get() {
Some(bset) => Ok(bset),
None => Ok(e.never()),
}
}
_op => {
// Infer the tail expression
let tail = tail.infer(e)?;
// TODO: Search within the tail type for `(op)`
Ok(tail)
}
}
}
}
impl<'a> Inference<'a> for Member {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Member { head, kind } = self;
// Infer the head expression
let head = head.infer(e)?;
// Get the type of head
let head = e.prune(head);
let ty = e.entry(head);
// Search within the head type for the memberkind
match kind {
MemberKind::Call(name, tuple) => {
if let Some((args, rety)) = e.get_fn(ty.id(), *name) {
let values = iter::once(Ok(e.new_ref(head)))
// infer for each member-
.chain(tuple.exprs.iter().map(|expr| expr.infer(e)))
// Construct tuple
.collect::<Result<Vec<_>, InferenceError>>()
// Return tuple
.map(|tys| e.new_tuple(tys))?;
e.unify(args, values)?;
Ok(rety)
} else {
Err(InferenceError::NotFound(Path::from(*name)))
}
}
MemberKind::Struct(name) => match ty.nav(&[PathPart::Ident(*name)]) {
Some(ty) => Ok(ty.id()),
None => Err(InferenceError::NotFound(Path::from(*name))),
},
MemberKind::Tuple(Literal::Int(idx)) => match ty.ty() {
Some(TypeKind::Tuple(tys)) => tys
.get(*idx as usize)
.copied()
.ok_or(InferenceError::FieldCount(head, tys.len(), *idx as usize)),
Some(TypeKind::Adt(Adt::TupleStruct(tys))) => tys
.get(*idx as usize)
.map(|(_vis, ty)| *ty)
.ok_or(InferenceError::FieldCount(head, tys.len(), *idx as usize)),
_ => Err(InferenceError::Mismatch(ty.id(), e.table.root())),
},
_ => Err(InferenceError::Mismatch(ty.id(), ty.root())),
}
// Type is required to be inferred at this point.
}
}
impl<'a> Inference<'a> for Index {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Index { head, indices } = self;
let usize = e.usize();
// Infer the head expression
let head = head.infer(e)?;
let mut head = e.prune(head);
// For each index expression:
for index in indices {
// Infer the index type
let index = index.infer(e)?;
if let Some((args, rety)) = e.get_fn(head, "index".into()) {
// Unify args and tuple (&head, index)
let selfty = e.new_ref(head);
let tupty = e.new_tuple(vec![selfty, index]);
e.unify(args, tupty)?;
head = e.prune(rety);
continue;
}
// Decide whether the head can be indexed by that type
// TODO: check for a `.index` method on the type
match e.entry(head).ty().unwrap() {
&TypeKind::Slice(handle) | &TypeKind::Array(handle, _) => {
e.unify(usize, index)?;
head = e.prune(handle);
}
other => todo!("Indexing on type {other}"),
}
// head = result of indexing head
}
Ok(head)
}
}
impl<'a> Inference<'a> for Cast {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Cast { head, ty } = self;
// Infer the head expression
let _head = head.infer(e)?;
// Evaluate the type
let ty = ty
.evaluate(e.table, e.at)
.map_err(InferenceError::AnnotationEval)?;
// Decide whether the type is castable
// TODO: not deciding is absolutely unsound!!!
// Return the type
Ok(ty)
}
}
impl<'a> Inference<'a> for Path {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
e.by_name(self)
.map_err(|_| InferenceError::NotFound(self.clone()))
}
}
impl<'a> Inference<'a> for Let {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Let { mutable: _, name, ty, init } = self;
let ty = match ty {
Some(ty) => ty
.evaluate(e.table, e.at)
.map_err(InferenceError::AnnotationEval)?,
None => e.new_inferred(),
};
// Infer the initializer
if let Some(init) = init {
// Unify the initializer and the ty
let initty = init.infer(e)?;
e.unify(ty, initty)?;
}
// Deep copy the ty, if it exists
let ty = e.deep_clone(ty);
// Infer the pattern
let patty = name.infer(e)?;
// Unify the pattern and the ty
e.unify(ty, patty)?;
// `if let` returns whether the pattern succeeded or not
Ok(e.bool())
}
}
impl<'a> Inference<'a> for Match {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Match { scrutinee, arms } = self;
// Infer the scrutinee
let scrutinee = scrutinee.infer(e)?;
let mut out = None;
// For each pattern:
for MatchArm(pat, expr) in arms {
let mut scope = e.block_scope();
// Infer the pattern
let pat = pat.infer(&mut scope)?;
// Unify it with the scrutinee
scope.unify(scrutinee, pat)?;
// Infer the Expr
let expr = expr.infer(&mut scope)?;
// Unify the expr with the out variable
match out {
Some(ty) => e.unify(ty, expr)?,
None => out = Some(expr),
}
}
// Return out. If there are no arms, assume Never.
match out {
Some(ty) => Ok(ty),
None => Ok(e.never()),
}
}
}
impl<'a> Inference<'a> for Pattern {
// TODO: This is the wrong way to typeck pattern matching.
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
match self {
Pattern::Name(name) => {
// Evaluating a pattern creates and enters a new scope.
// Surely this will cause zero problems.
e.local_scope(*name);
e.table.set_ty(e.at, TypeKind::Inferred);
Ok(e.at)
}
Pattern::Path(path) => {
// Evaluating a path pattern puts type constraints on the scrutinee
path.evaluate(e.table, e.at)
.map_err(|_| InferenceError::NotFound(path.clone()))
}
Pattern::Literal(literal) => literal.infer(e),
Pattern::Rest(Some(pat)) => {
eprintln!("TODO: Rest patterns in tuples?");
let ty = pat.infer(e)?;
Ok(e.new_slice(ty))
}
Pattern::Rest(_) => Ok(e.new_inferred()),
Pattern::Ref(_, pattern) => {
let ty = pattern.infer(e)?;
Ok(e.new_ref(ty))
}
Pattern::RangeExc(pat1, pat2) => {
let ty1 = pat1.infer(e)?;
let ty2 = pat2.infer(e)?;
e.unify(ty1, ty2)?;
Ok(ty1)
}
Pattern::RangeInc(pat1, pat2) => {
let ty1 = pat1.infer(e)?;
let ty2 = pat2.infer(e)?;
e.unify(ty1, ty2)?;
Ok(ty1)
}
Pattern::Tuple(patterns) => {
let tys = patterns
.iter()
.map(|pat| pat.infer(e))
.collect::<Result<Vec<Handle>, InferenceError>>()?;
Ok(e.new_tuple(tys))
}
Pattern::Array(patterns) => match patterns.as_slice() {
// TODO: rest patterns here
[one, rest @ ..] => {
let ty = one.infer(e)?;
for rest in rest {
let ty2 = rest.infer(e)?;
e.unify(ty, ty2)?;
}
Ok(e.new_slice(ty))
}
[] => {
let ty = e.new_inferred();
Ok(e.new_slice(ty))
}
},
Pattern::Struct(_path, _items) => {
eprintln!("TODO: struct patterns: {self}");
Ok(e.empty())
}
Pattern::TupleStruct(path, patterns) => {
eprintln!("TODO: tuple struct patterns: {self}");
let struc = e.by_name(path)?;
let Some(TypeKind::Adt(Adt::TupleStruct(ts))) = e.entry(struc).ty() else {
Err(InferenceError::Mismatch(struc, e.never()))?
};
let ts: Vec<_> = ts.iter().map(|(_v, h)| *h).collect();
let tys = patterns
.iter()
.map(|pat| pat.infer(e))
.collect::<Result<Vec<Handle>, InferenceError>>()?;
let ts = e.new_tuple(ts);
let tup = e.new_tuple(tys);
e.unify(ts, tup)?;
Ok(struc)
}
}
}
}
impl<'a> Inference<'a> for While {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let While { cond, pass, fail } = self;
// Infer the condition
let cond = cond.infer(e)?;
// Unify the condition with bool
let bool = e.bool();
e.unify(bool, cond)?;
// Infer the fail branch
let fail = fail.infer(e)?;
// Unify the fail branch with breakset
let mut e = e.open_bset();
// Infer the pass branch
let pass = pass.infer(&mut e)?;
// Unify the pass branch with Empty
let empt = e.empty();
e.unify(pass, empt)?;
match e.bset.get() {
None => Ok(e.empty()),
Some(bset) => {
e.unify(fail, bset)?;
Ok(fail)
}
}
}
}
impl<'a> Inference<'a> for If {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let If { cond, pass, fail } = self;
// Do inference on the condition'
let cond = cond.infer(e)?;
// Unify the condition with bool
let bool = e.bool();
e.unify(bool, cond)?;
// Do inference on the pass branch
let pass = pass.infer(e)?;
// Do inference on the fail branch
let fail = fail.infer(e)?;
// Unify pass and fail
e.unify(pass, fail)?;
// Return the result
Ok(pass)
}
}
impl<'a> Inference<'a> for For {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let For { bind, cond, pass, fail } = self;
let mut e = e.block_scope();
let bind = bind.infer(&mut e)?;
// What does it mean to be iterable? Why, `next()`, of course!
let cond = cond.infer(&mut e)?;
let cond = e.prune(cond);
if let Some((args, rety)) = e.get_fn(cond, "next".into()) {
// Check that the args are correct
let params = vec![e.new_ref(cond)];
let params = e.new_tuple(params);
e.unify(args, params)?;
e.unify(rety, bind)?;
}
// Enter a new breakset
let mut e = e.open_bset();
// Infer the fail branch
let fail = fail.infer(&mut e)?;
// Open a breakset
let mut e = e.open_bset();
// Infer the pass branch
let pass = pass.infer(&mut e)?;
// Unify the pass branch with Empty
let empt = e.empty();
e.unify(pass, empt)?;
// Return breakset
if let Some(bset) = e.bset.get() {
e.unify(fail, bset)?;
Ok(fail)
} else {
Ok(e.empty())
}
}
}
impl<'a> Inference<'a> for Else {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
self.body.infer(e)
}
}
impl<'a> Inference<'a> for Break {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Break { body } = self;
// Infer the body of the break
let ty = body.infer(e)?;
// Unify it with the breakset of the loop
e.bset(ty)?;
// Return never
Ok(e.never())
}
}
impl<'a> Inference<'a> for Return {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
let Return { body } = self;
// Infer the body of the return
let ty = body.infer(e)?;
// Unify it with the return-set of the function
e.rset(ty)?;
// Return never
Ok(e.never())
}
}
impl<'a, I: Inference<'a>> Inference<'a> for Option<I> {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
match self {
Some(expr) => expr.infer(e),
None => Ok(e.empty()),
}
}
}
impl<'a, I: Inference<'a>> Inference<'a> for Box<I> {
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> IfResult {
self.as_ref().infer(e)
}
}

View File

@ -4,12 +4,8 @@ use crate::{
handle::Handle,
source::Source,
table::{NodeKind, Table},
type_kind::TypeKind,
};
use cl_ast::{
ItemKind, Sym,
ast_visitor::{Visit, Walk},
};
use cl_ast::{ast_visitor::Visit, ItemKind, Sym};
#[derive(Debug)]
pub struct Populator<'t, 'a> {
@ -37,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 { span, attrs, vis: _, kind } = i;
let cl_ast::Item { extents, attrs, vis, kind } = i;
// TODO: this, better, better.
let entry_kind = match kind {
ItemKind::Alias(_) => NodeKind::Type,
@ -54,129 +50,91 @@ impl<'a> Visit<'a> for Populator<'_, 'a> {
};
let mut entry = self.new_entry(entry_kind);
entry.inner.set_span(*span);
entry.inner.set_span(*extents);
entry.inner.set_meta(&attrs.meta);
entry.visit_children(i);
entry.visit_span(extents);
entry.visit_attrs(attrs);
entry.visit_visibility(vis);
entry.visit_item_kind(kind);
if let (Some(name), child) = (entry.name, entry.inner.id()) {
self.inner.add_child(name, child);
}
}
fn visit_generics(&mut self, value: &'a cl_ast::Generics) {
let cl_ast::Generics { vars } = value;
for var in vars {
let mut entry = self.inner.new_entry(NodeKind::Type);
entry.set_ty(TypeKind::Variable);
let id = entry.id();
self.inner.add_child(*var, id);
}
}
fn visit_alias(&mut self, a: &'a cl_ast::Alias) {
let cl_ast::Alias { name, from } = a;
let cl_ast::Alias { to, from } = a;
self.inner.set_source(Source::Alias(a));
self.set_name(*name);
self.set_name(*to);
self.visit(from);
if let Some(t) = from {
self.visit_ty(t)
}
}
fn visit_const(&mut self, c: &'a cl_ast::Const) {
let cl_ast::Const { name, ty, init } = c;
self.inner.set_source(Source::Const(c));
self.inner.set_body(init);
self.set_name(*name);
self.visit(ty);
self.visit(init);
self.visit_ty(ty);
self.visit_expr(init);
}
fn visit_static(&mut self, s: &'a cl_ast::Static) {
let cl_ast::Static { name, init, .. } = s;
let cl_ast::Static { mutable, name, ty, init } = s;
self.inner.set_source(Source::Static(s));
self.inner.set_body(init);
self.set_name(*name);
s.children(self);
}
fn visit_function(&mut self, f: &'a cl_ast::Function) {
let cl_ast::Function { name, gens, sign, bind, body } = f;
self.inner.set_source(Source::Function(f));
self.set_name(*name);
self.visit(gens);
self.visit(sign);
self.visit(bind);
if let Some(b) = body {
self.inner.set_body(b);
self.visit(b);
}
self.visit_mutability(mutable);
self.visit_ty(ty);
self.visit_expr(init);
}
fn visit_module(&mut self, m: &'a cl_ast::Module) {
let cl_ast::Module { name, file } = m;
let cl_ast::Module { name, kind } = m;
self.inner.set_source(Source::Module(m));
self.set_name(*name);
self.visit(file);
self.visit_module_kind(kind);
}
fn visit_function(&mut self, f: &'a cl_ast::Function) {
let cl_ast::Function { name, sign, bind, body } = f;
self.inner.set_source(Source::Function(f));
self.set_name(*name);
self.visit_ty_fn(sign);
bind.iter().for_each(|p| self.visit_param(p));
if let Some(b) = body {
self.visit_block(b)
}
}
fn visit_struct(&mut self, s: &'a cl_ast::Struct) {
let cl_ast::Struct { name, gens, kind } = s;
let cl_ast::Struct { name, kind } = s;
self.inner.set_source(Source::Struct(s));
self.set_name(*name);
self.visit(gens);
self.visit(kind);
self.visit_struct_kind(kind);
}
fn visit_enum(&mut self, e: &'a cl_ast::Enum) {
let cl_ast::Enum { name, gens, variants } = e;
let cl_ast::Enum { name, kind } = e;
self.inner.set_source(Source::Enum(e));
self.set_name(*name);
self.visit(gens);
let mut children = Vec::new();
for variant in variants.iter() {
let mut entry = self.new_entry(NodeKind::Type);
variant.visit_in(&mut entry);
let child = entry.inner.id();
children.push((variant.name, child));
self.inner.add_child(variant.name, child);
}
self.inner
.set_ty(TypeKind::Adt(crate::type_kind::Adt::Enum(children)));
}
fn visit_variant(&mut self, value: &'a cl_ast::Variant) {
let cl_ast::Variant { name, kind, body } = value;
self.inner.set_source(Source::Variant(value));
self.set_name(*name);
self.visit(kind);
if let Some(body) = body {
self.inner.set_body(body);
}
self.visit_enum_kind(kind);
}
fn visit_impl(&mut self, i: &'a cl_ast::Impl) {
let cl_ast::Impl { gens, target: _, body } = i;
let cl_ast::Impl { target, body } = i;
self.inner.set_source(Source::Impl(i));
self.inner.mark_impl_item();
// We don't know if target is generic yet -- that's checked later.
for generic in &gens.vars {
let mut entry = self.new_entry(NodeKind::Type);
entry.inner.set_ty(TypeKind::Inferred);
let child = entry.inner.id();
self.inner.add_child(*generic, child);
}
self.visit(body);
self.visit_impl_kind(target);
self.visit_file(body);
}
fn visit_use(&mut self, u: &'a cl_ast::Use) {
@ -184,6 +142,26 @@ impl<'a> Visit<'a> for Populator<'_, 'a> {
self.inner.set_source(Source::Use(u));
self.inner.mark_use_item();
self.visit(tree);
self.visit_use_tree(tree);
}
fn visit_let(&mut self, l: &'a cl_ast::Let) {
let cl_ast::Let { mutable, name: _, ty, init } = l;
let mut entry = self.new_entry(NodeKind::Local);
entry.inner.set_source(Source::Local(l));
// entry.set_name(*name);
entry.visit_mutability(mutable);
if let Some(ty) = ty {
entry.visit_ty(ty);
}
if let Some(init) = init {
entry.visit_expr(init)
}
// let child = entry.inner.id();
// self.inner.add_child(*name, child);
todo!("Pattern destructuring in cl-typeck")
}
}

View File

@ -31,7 +31,7 @@ use crate::{
source::Source,
type_kind::TypeKind,
};
use cl_ast::{Expr, Meta, PathPart, Sym};
use cl_ast::{Meta, PathPart, Sym};
use cl_structures::{index_map::IndexMap, span::Span};
use std::collections::HashMap;
@ -50,17 +50,15 @@ pub struct Table<'a> {
pub(crate) children: HashMap<Handle, HashMap<Sym, Handle>>,
pub(crate) imports: HashMap<Handle, HashMap<Sym, Handle>>,
pub(crate) use_items: HashMap<Handle, Vec<Handle>>,
bodies: HashMap<Handle, &'a Expr>,
types: HashMap<Handle, TypeKind>,
spans: HashMap<Handle, Span>,
metas: HashMap<Handle, &'a [Meta]>,
sources: HashMap<Handle, Source<'a>>,
// code: HashMap<Handle, BasicBlock>, // TODO: lower sources
impl_targets: HashMap<Handle, Handle>,
anon_types: HashMap<TypeKind, Handle>,
lang_items: HashMap<Sym, Handle>,
// --- Queues for algorithms ---
pub(crate) unchecked: Vec<Handle>,
pub(crate) impls: Vec<Handle>,
pub(crate) uses: Vec<Handle>,
}
@ -79,15 +77,12 @@ impl<'a> Table<'a> {
children: HashMap::new(),
imports: HashMap::new(),
use_items: HashMap::new(),
bodies: HashMap::new(),
types: HashMap::new(),
spans: HashMap::new(),
metas: HashMap::new(),
sources: HashMap::new(),
impl_targets: HashMap::new(),
anon_types: HashMap::new(),
lang_items: HashMap::new(),
unchecked: Vec::new(),
impls: Vec::new(),
uses: Vec::new(),
}
@ -115,10 +110,6 @@ impl<'a> Table<'a> {
self.imports.entry(parent).or_default().insert(name, import)
}
pub fn mark_unchecked(&mut self, item: Handle) {
self.unchecked.push(item);
}
pub fn mark_use_item(&mut self, item: Handle) {
let parent = self.parents[item];
self.use_items.entry(parent).or_default().push(item);
@ -129,11 +120,7 @@ impl<'a> Table<'a> {
self.impls.push(item);
}
pub fn mark_lang_item(&mut self, name: Sym, item: Handle) {
self.lang_items.insert(name, item);
}
pub fn handle_iter(&self) -> impl Iterator<Item = Handle> + use<> {
pub fn handle_iter(&mut self) -> impl Iterator<Item = Handle> {
self.kinds.keys()
}
@ -155,18 +142,6 @@ impl<'a> Table<'a> {
entry
}
pub(crate) fn inferred_type(&mut self) -> Handle {
let handle = self.new_entry(self.root, NodeKind::Type);
self.types.insert(handle, TypeKind::Inferred);
handle
}
pub(crate) fn type_variable(&mut self) -> Handle {
let handle = self.new_entry(self.root, NodeKind::Type);
self.types.insert(handle, TypeKind::Variable);
handle
}
pub const fn root_entry(&self) -> Entry<'_, 'a> {
self.root.to_entry(self)
}
@ -197,10 +172,6 @@ impl<'a> Table<'a> {
self.imports.get(&node)
}
pub fn body(&self, node: Handle) -> Option<&'a Expr> {
self.bodies.get(&node).copied()
}
pub fn ty(&self, node: Handle) -> Option<&TypeKind> {
self.types.get(&node)
}
@ -221,15 +192,6 @@ impl<'a> Table<'a> {
self.impl_targets.get(&node).copied()
}
pub fn reparent(&mut self, node: Handle, parent: Handle) -> Handle {
self.parents.replace(node, parent)
}
pub fn set_body(&mut self, node: Handle, body: &'a Expr) -> Option<&'a Expr> {
self.mark_unchecked(node);
self.bodies.insert(node, body)
}
pub fn set_ty(&mut self, node: Handle, kind: TypeKind) -> Option<TypeKind> {
self.types.insert(node, kind)
}
@ -262,14 +224,6 @@ impl<'a> Table<'a> {
}
}
pub fn super_of(&self, node: Handle) -> Option<Handle> {
match self.kinds.get(node)? {
NodeKind::Root => None,
NodeKind::Module => self.parent(node).copied(),
_ => self.super_of(*self.parent(node)?),
}
}
pub fn name(&self, node: Handle) -> Option<Sym> {
self.source(node).and_then(|s| s.name())
}
@ -305,7 +259,8 @@ impl<'a> Table<'a> {
/// Does path traversal relative to the provided `node`.
pub fn nav(&self, node: Handle, path: &[PathPart]) -> Option<Handle> {
match path {
[PathPart::SuperKw, rest @ ..] => self.nav(self.super_of(node)?, rest),
[PathPart::SuperKw, rest @ ..] => self.nav(*self.parent(node)?, rest),
[PathPart::SelfKw, rest @ ..] => self.nav(node, rest),
[PathPart::SelfTy, rest @ ..] => self.nav(self.selfty(node)?, rest),
[PathPart::Ident(name), rest @ ..] => self.nav(self.get_by_sym(node, name)?, rest),
[] => Some(node),
@ -327,9 +282,7 @@ pub enum NodeKind {
Const,
Static,
Function,
Temporary,
Let,
Scope,
Local,
Impl,
Use,
}
@ -346,9 +299,7 @@ mod display {
NodeKind::Const => write!(f, "const"),
NodeKind::Static => write!(f, "static"),
NodeKind::Function => write!(f, "fn"),
NodeKind::Temporary => write!(f, "temp"),
NodeKind::Let => write!(f, "let"),
NodeKind::Scope => write!(f, "scope"),
NodeKind::Local => write!(f, "local"),
NodeKind::Use => write!(f, "use"),
NodeKind::Impl => write!(f, "impl"),
}

View File

@ -2,7 +2,7 @@
//! construct type bindings in a [Table]'s typing context.
use crate::{handle::Handle, table::Table, type_kind::TypeKind};
use cl_ast::{PathPart, Sym, Ty, TyArray, TyFn, TyKind, TyPtr, TyRef, TySlice, TyTuple};
use cl_ast::{PathPart, Ty, TyArray, TyFn, TyKind, TyRef, TySlice, TyTuple};
#[derive(Clone, Debug, PartialEq, Eq)] // TODO: impl Display and Error
pub enum Error {
@ -42,13 +42,11 @@ impl TypeExpression for TyKind {
match self {
TyKind::Never => Ok(table.anon_type(TypeKind::Never)),
TyKind::Empty => Ok(table.anon_type(TypeKind::Empty)),
TyKind::Infer => Ok(table.inferred_type()),
TyKind::Path(p) => p.evaluate(table, node),
TyKind::Array(a) => a.evaluate(table, node),
TyKind::Slice(s) => s.evaluate(table, node),
TyKind::Tuple(t) => t.evaluate(table, node),
TyKind::Ref(r) => r.evaluate(table, node),
TyKind::Ptr(r) => r.evaluate(table, node),
TyKind::Fn(f) => f.evaluate(table, node),
}
}
@ -69,15 +67,6 @@ impl TypeExpression for [PathPart] {
}
}
impl TypeExpression for Sym {
fn evaluate(&self, table: &mut Table, node: Handle) -> Result<Handle, Error> {
let path = [PathPart::Ident(*self)];
table
.nav(node, &path)
.ok_or_else(|| Error::BadPath { parent: node, path: path.to_vec() })
}
}
impl TypeExpression for TyArray {
fn evaluate(&self, table: &mut Table, node: Handle) -> Result<Handle, Error> {
let Self { ty, count } = self;
@ -117,21 +106,15 @@ impl TypeExpression for TyRef {
}
}
impl TypeExpression for TyPtr {
fn evaluate(&self, table: &mut Table, node: Handle) -> Result<Handle, Error> {
let Self { to } = self;
let mut t = to.evaluate(table, node)?;
t = table.anon_type(TypeKind::Ptr(t));
Ok(t)
}
}
impl TypeExpression for TyFn {
fn evaluate(&self, table: &mut Table, node: Handle) -> Result<Handle, Error> {
let Self { args, rety } = self;
let kind = TypeKind::FnSig {
args: args.evaluate(table, node)?,
rety: rety.evaluate(table, node)?,
rety: match rety {
Some(ty) => ty.evaluate(table, node)?,
None => TyKind::Empty.evaluate(table, node)?,
},
};
Ok(table.anon_type(kind))
}

View File

@ -10,20 +10,14 @@ mod display;
/// (a component of a [Table](crate::table::Table))
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum TypeKind {
/// A type that is yet to be inferred!
Inferred,
/// A type variable, to be monomorphized
Variable,
/// An alias for an already-defined type
Instance(Handle),
/// A primitive type, built-in to the compiler
Primitive(Primitive),
Intrinsic(Intrinsic),
/// A user-defined aromatic data type
Adt(Adt),
/// A reference to an already-defined type: &T
Ref(Handle),
/// A raw pointer to an already-defined type: &T
Ptr(Handle),
/// A contiguous view of dynamically sized memory
Slice(Handle),
/// A contiguous view of statically sized memory
@ -44,7 +38,7 @@ pub enum TypeKind {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Adt {
/// A union-like enum type
Enum(Vec<(Sym, Handle)>),
Enum(Vec<(Sym, Option<Handle>)>),
/// A structural product type with named members
Struct(Vec<(Sym, Visibility, Handle)>),
@ -61,66 +55,42 @@ pub enum Adt {
/// The set of compiler-intrinsic types.
/// These primitive types have native implementations of the basic operations.
#[rustfmt::skip]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Primitive {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Intrinsic {
I8, I16, I32, I64, I128, Isize, // Signed integers
U8, U16, U32, U64, U128, Usize, // Unsigned integers
F8, F16, F32, F64, F128, Fsize, // Floating point numbers
Integer, Float, // Inferred int and float
Bool, // boolean value
Char, // Unicode codepoint
Str, // UTF-8 string
}
#[rustfmt::skip]
impl Primitive {
/// Checks whether self is an integer
pub fn is_integer(self) -> bool {
matches!(
self,
| Self::I8 | Self::I16 | Self::I32 | Self::I64 | Self::I128 | Self::Isize
| Self::U8 | Self::U16 | Self::U32 | Self::U64 | Self::U128 | Self::Usize
| Self::Integer
)
}
/// Checks whether self is a floating point number
pub fn is_float(self) -> bool {
matches!(
self,
| Self::F8 | Self::F16 | Self::F32 | Self::F64 | Self::F128 | Self::Fsize
| Self::Float
)
}
}
// Author's note: the fsize type is a meme
impl FromStr for Primitive {
impl FromStr for Intrinsic {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"i8" => Primitive::I8,
"i16" => Primitive::I16,
"i32" => Primitive::I32,
"i64" => Primitive::I64,
"i128" => Primitive::I128,
"isize" => Primitive::Isize,
"u8" => Primitive::U8,
"u16" => Primitive::U16,
"u32" => Primitive::U32,
"u64" => Primitive::U64,
"u128" => Primitive::U128,
"usize" => Primitive::Usize,
"f8" => Primitive::F8,
"f16" => Primitive::F16,
"f32" => Primitive::F32,
"f64" => Primitive::F64,
"f128" => Primitive::F128,
"fsize" => Primitive::Fsize,
"bool" => Primitive::Bool,
"char" => Primitive::Char,
"str" => Primitive::Str,
"i8" => Intrinsic::I8,
"i16" => Intrinsic::I16,
"i32" => Intrinsic::I32,
"i64" => Intrinsic::I64,
"i128" => Intrinsic::I128,
"isize" => Intrinsic::Isize,
"u8" => Intrinsic::U8,
"u16" => Intrinsic::U16,
"u32" => Intrinsic::U32,
"u64" => Intrinsic::U64,
"u128" => Intrinsic::U128,
"usize" => Intrinsic::Usize,
"f8" => Intrinsic::F8,
"f16" => Intrinsic::F16,
"f32" => Intrinsic::F32,
"f64" => Intrinsic::F64,
"f128" => Intrinsic::F128,
"fsize" => Intrinsic::Fsize,
"bool" => Intrinsic::Bool,
"char" => Intrinsic::Char,
_ => Err(())?,
})
}

View File

@ -1,6 +1,6 @@
//! [Display] implementations for [TypeKind], [Adt], and [Intrinsic]
use super::{Adt, Primitive, TypeKind};
use super::{Adt, Intrinsic, TypeKind};
use crate::format_utils::*;
use cl_ast::format::FmtAdapter;
use std::fmt::{self, Display, Write};
@ -8,13 +8,10 @@ use std::fmt::{self, Display, Write};
impl Display for TypeKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TypeKind::Inferred => write!(f, "_"),
TypeKind::Variable => write!(f, "?"),
TypeKind::Instance(def) => write!(f, "alias to #{def}"),
TypeKind::Primitive(i) => i.fmt(f),
TypeKind::Intrinsic(i) => i.fmt(f),
TypeKind::Adt(a) => a.fmt(f),
TypeKind::Ref(def) => write!(f, "&{def}"),
TypeKind::Ptr(def) => write!(f, "*{def}"),
TypeKind::Slice(def) => write!(f, "slice [#{def}]"),
TypeKind::Array(def, size) => write!(f, "array [#{def}; {size}]"),
TypeKind::Tuple(defs) => {
@ -39,7 +36,10 @@ impl Display for Adt {
let mut variants = variants.iter();
separate(", ", || {
let (name, def) = variants.next()?;
Some(move |f: &mut Delimit<_>| write!(f, "{name}: #{def}"))
Some(move |f: &mut Delimit<_>| match def {
Some(def) => write!(f, "{name}: #{def}"),
None => write!(f, "{name}"),
})
})(f.delimit_with("enum {", "}"))
}
Adt::Struct(members) => {
@ -68,32 +68,29 @@ impl Display for Adt {
}
}
impl Display for Primitive {
impl Display for Intrinsic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Primitive::I8 => f.write_str("i8"),
Primitive::I16 => f.write_str("i16"),
Primitive::I32 => f.write_str("i32"),
Primitive::I64 => f.write_str("i64"),
Primitive::I128 => f.write_str("i128"),
Primitive::Isize => f.write_str("isize"),
Primitive::U8 => f.write_str("u8"),
Primitive::U16 => f.write_str("u16"),
Primitive::U32 => f.write_str("u32"),
Primitive::U64 => f.write_str("u64"),
Primitive::U128 => f.write_str("u128"),
Primitive::Usize => f.write_str("usize"),
Primitive::F8 => f.write_str("f8"),
Primitive::F16 => f.write_str("f16"),
Primitive::F32 => f.write_str("f32"),
Primitive::F64 => f.write_str("f64"),
Primitive::F128 => f.write_str("f128"),
Primitive::Fsize => f.write_str("fsize"),
Primitive::Integer => f.write_str("{integer}"),
Primitive::Float => f.write_str("{float}"),
Primitive::Bool => f.write_str("bool"),
Primitive::Char => f.write_str("char"),
Primitive::Str => f.write_str("str"),
Intrinsic::I8 => f.write_str("i8"),
Intrinsic::I16 => f.write_str("i16"),
Intrinsic::I32 => f.write_str("i32"),
Intrinsic::I64 => f.write_str("i64"),
Intrinsic::I128 => f.write_str("i128"),
Intrinsic::Isize => f.write_str("isize"),
Intrinsic::U8 => f.write_str("u8"),
Intrinsic::U16 => f.write_str("u16"),
Intrinsic::U32 => f.write_str("u32"),
Intrinsic::U64 => f.write_str("u64"),
Intrinsic::U128 => f.write_str("u128"),
Intrinsic::Usize => f.write_str("usize"),
Intrinsic::F8 => f.write_str("f8"),
Intrinsic::F16 => f.write_str("f16"),
Intrinsic::F32 => f.write_str("f32"),
Intrinsic::F64 => f.write_str("f64"),
Intrinsic::F128 => f.write_str("f128"),
Intrinsic::Fsize => f.write_str("fsize"),
Intrinsic::Bool => f.write_str("bool"),
Intrinsic::Char => f.write_str("char"),
}
}
}

View File

@ -26,7 +26,7 @@ Static = "static" Mutability Identifier ':' Ty '=' Expr ';' ;
Module = "mod" Identifier ModuleKind ;
ModuleKind = '{' Item* '}' | ';' ;
Function = "fn" Identifier '(' (Param ',')* Param? ')' ('->' Ty)? (Expr | ';') ;
Function = "fn" Identifier '(' (Param ',')* Param? ')' ('->' Ty)? Block? ;
Param = Mutability Identifier ':' Ty ;
Struct = "struct" Identifier (StructTuple | StructBody)?;
@ -127,17 +127,6 @@ Block = '{' Stmt* '}';
Group = Empty | '(' (Expr | Tuple) ')' ;
Tuple = (Expr ',')* Expr? ;
Match = "match" { (MatchArm ',')* MatchArm? } ;
MatchArm = Pattern '=>' Expr ;
Pattern = Path
| Literal
| '&' "mut"? Pattern
| '(' (Pattern ',')* (Pattern | '..' )? ')'
| '[' (Pattern ',')* (Pattern | '..' Identifier?)? ']'
| StructPattern
;
Loop = "loop" Block ;
While = "while" Expr Block Else ;
If = "if" Expr Block Else ;

View File

@ -8,4 +8,4 @@ license.workspace = true
publish.workspace = true
[dependencies]
crossterm = { version = "0.29.0", default-features = false }
crossterm = { version = "0.27.0", default-features = false }

View File

@ -1,42 +0,0 @@
//! Demonstrates the use of [read_and()]:
//!
//! The provided closure:
//! 1. Takes a line of input (a [String])
//! 2. Performs some calculation (using [FromStr])
//! 3. Returns a [Result] containing a [Response] or an [Err]
use repline::{error::Error as RlError, Repline, Response};
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let mut rl = Repline::with_input(
"fn main {\r\n\tprintln(\"Foo!\")\r\n}\r\n".as_bytes(),
"\x1b[33m",
" >",
" ?>",
);
while rl.read().is_ok() {}
let mut rl = rl.swap_input(std::io::stdin());
loop {
let f = |_line| -> Result<_, RlError> { Ok(Response::Continue) };
let line = match rl.read() {
Err(RlError::CtrlC(_)) => break,
Err(RlError::CtrlD(line)) => {
rl.deny();
line
}
Ok(line) => line,
Err(e) => Err(e)?,
};
print!("\x1b[G\x1b[J");
match f(&line) {
Ok(Response::Accept) => rl.accept(),
Ok(Response::Deny) => rl.deny(),
Ok(Response::Break) => break,
Ok(Response::Continue) => continue,
Err(e) => print!("\x1b[40G\x1b[A\x1bJ\x1b[91m{e}\x1b[0m\x1b[B"),
}
}
Ok(())
}

View File

@ -1,9 +1,9 @@
//! The [Editor] is a multi-line buffer of [`char`]s which operates on an ANSI-compatible terminal.
use crossterm::{cursor::*, queue, style::*, terminal::*};
use crossterm::{cursor::*, execute, queue, style::*, terminal::*};
use std::{collections::VecDeque, fmt::Display, io::Write};
use super::error::ReplResult;
use super::error::{Error, ReplResult};
fn is_newline(c: &char) -> bool {
*c == '\n'
@ -14,7 +14,7 @@ fn write_chars<'a, W: Write>(
w: &mut W,
) -> std::io::Result<()> {
for c in c {
queue!(w, Print(c))?;
write!(w, "{c}")?;
}
Ok(())
}
@ -42,63 +42,73 @@ impl<'a> Editor<'a> {
head.iter().chain(tail.iter())
}
fn putchar<W: Write>(&self, c: char, w: &mut W) -> ReplResult<()> {
let Self { color, again, .. } = self;
match c {
'\n' => queue!(
w,
Print('\n'),
MoveToColumn(0),
Print(color),
Print(again),
Print(ResetColor),
Print(' ')
),
c => queue!(w, Print(c)),
}?;
Ok(())
}
pub fn redraw_head<W: Write>(&self, w: &mut W) -> ReplResult<()> {
let Self { head, color, begin, .. } = self;
/// Moves up to the first line of the editor, and clears the screen.
///
/// This assumes the screen hasn't moved since the last draw.
pub fn undraw<W: Write>(&self, w: &mut W) -> ReplResult<()> {
let Self { head, .. } = self;
match head.iter().copied().filter(is_newline).count() {
0 => queue!(w, MoveToColumn(0)),
n => queue!(w, MoveUp(n as u16)),
0 => write!(w, "\x1b[0G"),
lines => write!(w, "\x1b[{}F", lines),
}?;
queue!(w, Print(color), Print(begin), Print(ResetColor), Print(' '))?;
for c in head {
self.putchar(*c, w)?;
}
queue!(w, Clear(ClearType::FromCursorDown))?;
// write!(w, "\x1b[0J")?;
Ok(())
}
pub fn redraw_tail<W: Write>(&self, w: &mut W) -> ReplResult<()> {
let Self { tail, .. } = self;
queue!(w, SavePosition, Clear(ClearType::FromCursorDown))?;
for c in tail {
self.putchar(*c, w)?;
/// Redraws the entire editor
pub fn redraw<W: Write>(&self, w: &mut W) -> ReplResult<()> {
let Self { head, tail, color, begin, again } = self;
write!(w, "{color}{begin}\x1b[0m ")?;
// draw head
for c in head {
match c {
'\n' => write!(w, "\r\n{color}{again}\x1b[0m "),
_ => w.write_all({ *c as u32 }.to_le_bytes().as_slice()),
}?
}
queue!(w, RestorePosition)?;
// save cursor
execute!(w, SavePosition)?;
// draw tail
for c in tail {
match c {
'\n' => write!(w, "\r\n{color}{again}\x1b[0m "),
_ => write!(w, "{c}"),
}?
}
// restore cursor
execute!(w, RestorePosition)?;
Ok(())
}
/// Prints a context-sensitive prompt (either `begin` if this is the first line,
/// or `again` for subsequent lines)
pub fn prompt<W: Write>(&self, w: &mut W) -> ReplResult<()> {
let Self { head, color, begin, again, .. } = self;
queue!(
w,
MoveToColumn(0),
Print(color),
Print(if head.is_empty() { begin } else { again }),
ResetColor,
Print(' '),
)?;
Ok(())
}
/// Prints the characters before the cursor on the current line.
pub fn print_head<W: Write>(&self, w: &mut W) -> ReplResult<()> {
let Self { head, color, begin, again, .. } = self;
let nl = self.head.iter().rposition(is_newline).map(|n| n + 1);
let prompt = if nl.is_some() { again } else { begin };
queue!(
self.prompt(w)?;
write_chars(
self.head.iter().skip(
self.head
.iter()
.rposition(is_newline)
.unwrap_or(self.head.len())
+ 1,
),
w,
MoveToColumn(0),
Print(color),
Print(prompt),
ResetColor,
Print(' '),
)?;
write_chars(head.iter().skip(nl.unwrap_or(0)), w)?;
Ok(())
}
@ -111,54 +121,53 @@ impl<'a> Editor<'a> {
Ok(())
}
pub fn print_err<W: Write>(&self, err: impl Display, w: &mut W) -> ReplResult<()> {
queue!(
w,
SavePosition,
Clear(ClearType::UntilNewLine),
Print(err),
RestorePosition
)?;
Ok(())
}
/// Writes a character at the cursor, shifting the text around as necessary.
pub fn push<W: Write>(&mut self, c: char, w: &mut W) -> ReplResult<()> {
self.head.push_back(c);
queue!(w, Clear(ClearType::UntilNewLine))?;
self.putchar(c, w)?;
match c {
'\n' => self.redraw_tail(w),
_ => self.print_tail(w),
// Tail optimization: if the tail is empty,
//we don't have to undraw and redraw on newline
if self.tail.is_empty() {
self.head.push_back(c);
match c {
'\n' => {
write!(w, "\r\n")?;
self.print_head(w)?;
}
c => {
queue!(w, Print(c))?;
}
};
return Ok(());
}
if '\n' == c {
self.undraw(w)?;
}
self.head.push_back(c);
match c {
'\n' => self.redraw(w)?,
_ => {
write!(w, "{c}")?;
self.print_tail(w)?;
}
}
Ok(())
}
/// Erases a character at the cursor, shifting the text around as necessary.
pub fn pop<W: Write>(&mut self, w: &mut W) -> ReplResult<Option<char>> {
if let Some('\n') = self.head.back() {
self.undraw(w)?;
}
let c = self.head.pop_back();
// if the character was a newline, we need to go back a line
match c {
None => return Ok(None),
Some('\n') => {
queue!(w, MoveToPreviousLine(1))?;
self.print_head(w)?;
self.redraw_tail(w)?;
}
Some('\n') => self.redraw(w)?,
Some(_) => {
queue!(w, MoveLeft(1), Clear(ClearType::UntilNewLine))?;
// go back a char
queue!(w, MoveLeft(1), Print(' '), MoveLeft(1))?;
self.print_tail(w)?;
}
}
Ok(c)
}
/// Pops the character after the cursor, redrawing if necessary
pub fn delete<W: Write>(&mut self, w: &mut W) -> ReplResult<Option<char>> {
let c = self.tail.pop_front();
match c {
Some('\n') => self.redraw_tail(w)?,
_ => self.print_tail(w)?,
None => {}
}
Ok(c)
}
@ -176,14 +185,9 @@ impl<'a> Editor<'a> {
}
/// Sets the editor to the contents of a string, placing the cursor at the end.
pub fn restore<W: Write>(&mut self, s: &str, w: &mut W) -> ReplResult<()> {
match self.head.iter().copied().filter(is_newline).count() {
0 => queue!(w, MoveToColumn(0), Clear(ClearType::FromCursorDown))?,
n => queue!(w, MoveUp(n as u16), Clear(ClearType::FromCursorDown))?,
};
pub fn restore(&mut self, s: &str) {
self.clear();
self.print_head(w)?;
self.extend(s.chars(), w)
self.head.extend(s.chars())
}
/// Clears the editor, removing all characters.
@ -192,6 +196,24 @@ impl<'a> Editor<'a> {
self.tail.clear();
}
/// Pops the character after the cursor, redrawing if necessary
pub fn delete<W: Write>(&mut self, w: &mut W) -> ReplResult<char> {
match self.tail.front() {
Some('\n') => {
self.undraw(w)?;
let out = self.tail.pop_front();
self.redraw(w)?;
out
}
_ => {
let out = self.tail.pop_front();
self.print_tail(w)?;
out
}
}
.ok_or(Error::EndOfInput)
}
/// Erases a word from the buffer, where a word is any non-whitespace characters
/// preceded by a single whitespace character
pub fn erase_word<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
@ -204,28 +226,9 @@ impl<'a> Editor<'a> {
self.head.len() + self.tail.len()
}
/// Returns true if the cursor is at the start of the buffer
pub fn at_start(&self) -> bool {
self.head.is_empty()
}
/// Returns true if the cursor is at the end of the buffer
pub fn at_end(&self) -> bool {
self.tail.is_empty()
}
/// Returns true if the cursor is at the start of a line
pub fn at_line_start(&self) -> bool {
matches!(self.head.back(), None | Some('\n'))
}
/// Returns true if the cursor is at the end of a line
pub fn at_line_end(&self) -> bool {
matches!(self.tail.front(), None | Some('\n'))
}
/// Returns true if the buffer is empty.
pub fn is_empty(&self) -> bool {
self.at_start() && self.at_end()
self.head.is_empty() && self.tail.is_empty()
}
/// Returns true if the buffer ends with a given pattern
@ -243,102 +246,59 @@ impl<'a> Editor<'a> {
}
/// Moves the cursor back `steps` steps
pub fn cursor_back<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
let Some(c) = self.head.pop_back() else {
return Ok(());
};
self.tail.push_front(c);
match c {
'\n' => {
queue!(w, MoveToPreviousLine(1))?;
self.print_head(w)
pub fn cursor_back<W: Write>(&mut self, steps: usize, w: &mut W) -> ReplResult<()> {
for _ in 0..steps {
if let Some('\n') = self.head.back() {
self.undraw(w)?;
}
_ => queue!(w, MoveLeft(1)).map_err(Into::into),
}
}
/// Moves the cursor forward `steps` steps
pub fn cursor_forward<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
let Some(c) = self.tail.pop_front() else {
return Ok(());
};
self.head.push_back(c);
match c {
'\n' => {
queue!(w, MoveToNextLine(1))?;
self.print_head(w)
let Some(c) = self.head.pop_back() else {
return Ok(());
};
self.tail.push_front(c);
match c {
'\n' => self.redraw(w)?,
_ => queue!(w, MoveLeft(1))?,
}
_ => queue!(w, MoveRight(1)).map_err(Into::into),
}
}
/// Moves the cursor up to the previous line, attempting to preserve relative offset
pub fn cursor_up<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
// Calculates length of the current line
let mut len = self.head.len();
self.cursor_line_start(w)?;
len -= self.head.len();
if self.at_start() {
return Ok(());
}
self.cursor_back(w)?;
self.cursor_line_start(w)?;
while 0 < len && !self.at_line_end() {
self.cursor_forward(w)?;
len -= 1;
}
Ok(())
}
/// Moves the cursor down to the next line, attempting to preserve relative offset
pub fn cursor_down<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
let mut len = self.head.iter().rev().take_while(|&&c| c != '\n').count();
self.cursor_line_end(w)?;
self.cursor_forward(w)?;
while 0 < len && !self.at_line_end() {
self.cursor_forward(w)?;
len -= 1;
/// Moves the cursor forward `steps` steps
pub fn cursor_forward<W: Write>(&mut self, steps: usize, w: &mut W) -> ReplResult<()> {
for _ in 0..steps {
if let Some('\n') = self.tail.front() {
self.undraw(w)?
}
let Some(c) = self.tail.pop_front() else {
return Ok(());
};
self.head.push_back(c);
match c {
'\n' => self.redraw(w)?,
_ => queue!(w, MoveRight(1))?,
}
}
Ok(())
}
/// Moves the cursor to the beginning of the current line
pub fn cursor_line_start<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
while !self.at_line_start() {
self.cursor_back(w)?
pub fn home<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
loop {
match self.head.back() {
Some('\n') | None => break Ok(()),
Some(_) => self.cursor_back(1, w)?,
}
}
Ok(())
}
/// Moves the cursor to the end of the current line
pub fn cursor_line_end<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
while !self.at_line_end() {
self.cursor_forward(w)?
pub fn end<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
loop {
match self.tail.front() {
Some('\n') | None => break Ok(()),
Some(_) => self.cursor_forward(1, w)?,
}
}
Ok(())
}
pub fn cursor_start<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
while !self.at_start() {
self.cursor_back(w)?
}
Ok(())
}
pub fn cursor_end<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
while !self.at_end() {
self.cursor_forward(w)?
}
Ok(())
}
}

View File

@ -49,7 +49,7 @@ where F: FnMut(&str) -> Result<Response, Box<dyn Error>> {
Ok(Response::Deny) => rl.deny(),
Ok(Response::Break) => break,
Ok(Response::Continue) => continue,
Err(e) => rl.print_inline(format_args!("\x1b[40G\x1b[91m{e}\x1b[0m"))?,
Err(e) => print!("\x1b[40G\x1b[A\x1bJ\x1b[91m{e}\x1b[0m\x1b[B"),
}
}
Ok(())

View File

@ -1,12 +1,11 @@
//! Prompts the user, reads the lines. Not much more to it than that.
//!
//! This module is in charge of parsing keyboard input and interpreting it for the line editor.
#![allow(clippy::unbuffered_bytes)]
use crate::{editor::Editor, error::*, iter::*, raw::raw};
use std::{
collections::VecDeque,
io::{Bytes, Read, Result, Write, stdout},
io::{stdout, Bytes, Read, Result, Write},
};
/// Prompts the user, reads the lines. Not much more to it than that.
@ -36,14 +35,6 @@ impl<'a, R: Read> Repline<'a, R> {
ed: Editor::new(color, begin, again),
}
}
pub fn swap_input<S: Read>(self, new_input: S) -> Repline<'a, S> {
Repline {
input: Chars(Flatten(new_input.bytes())),
history: self.history,
hindex: self.hindex,
ed: self.ed,
}
}
/// Set the terminal prompt color
pub fn set_color(&mut self, color: &'a str) {
self.ed.color = color
@ -64,7 +55,8 @@ impl<'a, R: Read> Repline<'a, R> {
let mut stdout = stdout().lock();
let stdout = &mut stdout;
let _make_raw = raw();
// self.ed.begin_frame(stdout)?;
// self.ed.redraw_frame(stdout)?;
self.ed.print_head(stdout)?;
loop {
stdout.flush()?;
@ -82,17 +74,19 @@ impl<'a, R: Read> Repline<'a, R> {
return Err(Error::CtrlD(self.ed.to_string()));
}
// Tab: extend line by 4 spaces
'\t' => self.ed.extend(INDENT.chars(), stdout)?,
'\t' => {
self.ed.extend(INDENT.chars(), stdout)?;
}
// ignore newlines, process line feeds. Not sure how cross-platform this is.
'\n' => {}
'\r' => {
self.ed.push('\n', stdout)?;
if self.ed.at_end() {
return Ok(self.ed.to_string());
}
return Ok(self.ed.to_string());
}
// Ctrl+Backspace in my terminal
'\x17' => self.ed.erase_word(stdout)?,
'\x17' => {
self.ed.erase_word(stdout)?;
}
// Escape sequence
'\x1b' => self.escape(stdout)?,
// backspace
@ -117,19 +111,6 @@ impl<'a, R: Read> Repline<'a, R> {
}
}
}
/// Prints a message without moving the cursor
pub fn print_inline(&mut self, value: impl std::fmt::Display) -> ReplResult<()> {
let mut stdout = stdout().lock();
self.print_err(&mut stdout, value)
}
/// Prints a message (ideally an error) without moving the cursor
fn print_err<W: Write>(&self, w: &mut W, value: impl std::fmt::Display) -> ReplResult<()> {
self.ed.print_err(value, w)
}
// Prints some debug info into the editor's buffer and the provided writer
pub fn put<D: std::fmt::Display, W: Write>(&mut self, disp: D, w: &mut W) -> ReplResult<()> {
self.ed.extend(format!("{disp}").chars(), w)
}
/// Handle ANSI Escape
fn escape<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
match self.input.next().ok_or(Error::EndOfInput)?? {
@ -142,38 +123,26 @@ impl<'a, R: Read> Repline<'a, R> {
/// Handle ANSI Control Sequence Introducer
fn csi<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
match self.input.next().ok_or(Error::EndOfInput)?? {
'A' if self.ed.at_start() && self.hindex > 0 => {
self.hindex -= 1;
self.restore_history(w)?;
'A' => {
self.hindex = self.hindex.saturating_sub(1);
self.restore_history(w)?
}
'A' => self.ed.cursor_up(w)?,
'B' if self.ed.at_end() && self.hindex < self.history.len().saturating_sub(1) => {
self.hindex += 1;
self.restore_history(w)?;
'B' => {
self.hindex = self.hindex.saturating_add(1).min(self.history.len());
self.restore_history(w)?
}
'B' => self.ed.cursor_down(w)?,
'C' => self.ed.cursor_forward(w)?,
'D' => self.ed.cursor_back(w)?,
'H' => self.ed.cursor_line_start(w)?,
'F' => self.ed.cursor_line_end(w)?,
'C' => self.ed.cursor_forward(1, w)?,
'D' => self.ed.cursor_back(1, w)?,
'H' => self.ed.home(w)?,
'F' => self.ed.end(w)?,
'3' => {
if let '~' = self.input.next().ok_or(Error::EndOfInput)?? {
self.ed.delete(w)?;
}
}
'5' => {
if let '~' = self.input.next().ok_or(Error::EndOfInput)?? {
self.ed.cursor_start(w)?
}
}
'6' => {
if let '~' = self.input.next().ok_or(Error::EndOfInput)?? {
self.ed.cursor_end(w)?
let _ = self.ed.delete(w);
}
}
other => {
if cfg!(debug_assertions) {
self.print_err(w, other.escape_debug())?;
self.ed.extend(other.escape_debug(), w)?;
}
}
}
@ -182,12 +151,11 @@ impl<'a, R: Read> Repline<'a, R> {
/// Restores the currently selected history
fn restore_history<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
let Self { history, hindex, ed, .. } = self;
ed.undraw(w)?;
ed.clear();
ed.print_head(w)?;
if let Some(history) = history.get(*hindex) {
ed.restore(history, w)?;
ed.print_err(
format_args!("\t\x1b[30mHistory {hindex} restored!\x1b[0m"),
w,
)?;
ed.extend(history.chars(), w)?
}
Ok(())
}
@ -197,12 +165,9 @@ impl<'a, R: Read> Repline<'a, R> {
while buf.ends_with(char::is_whitespace) {
buf.pop();
}
if let Some(idx) = self.history.iter().position(|v| *v == buf) {
self.history
.remove(idx)
.expect("should have just found this");
};
self.history.push_back(buf);
if !self.history.contains(&buf) {
self.history.push_back(buf)
}
while self.history.len() > 20 {
self.history.pop_front();
}

View File

@ -1,117 +0,0 @@
#!/usr/bin/env -S conlang-run
//! A simple five-function pn calculator
enum Expr {
Atom(f64),
Op(char, [Expr]),
}
/// executes an expression
fn execute(expr: Expr) -> f64 {
match expr {
Expr::Atom(value) => value,
Expr::Op('*', [lhs, rhs]) => execute(lhs) * execute(rhs),
Expr::Op('/', [lhs, rhs]) => execute(lhs) / execute(rhs),
Expr::Op('%', [lhs, rhs]) => execute(lhs) % execute(rhs),
Expr::Op('+', [lhs, rhs]) => execute(lhs) + execute(rhs),
Expr::Op('-', [lhs, rhs]) => execute(lhs) - execute(rhs),
Expr::Op('-', [lhs]) => - execute(lhs),
other => {
panic("Unknown operation: " + fmt(other))
}
}
}
/// Pretty-prints an expression
fn fmt_expr(expr: Expr) -> str {
match expr {
Expr::Atom(value) => fmt(value),
Expr::Op(operator, [lhs, rhs]) => fmt('(', fmt_expr(lhs), ' ', operator, ' ', fmt_expr(rhs), ')'),
Expr::Op(operator, [rhs]) => fmt(operator, fmt_expr(rhs)),
_ => println("Unexpected expr: ", expr),
}
}
fn print_expr(expr: Expr) {
println(fmt_expr(expr))
}
/// Parses expressions
fn parse(line: [char], power: i32) -> (Expr, [char]) {
fn map((expr, line): (Expr, [char]), f: fn(Expr) -> Expr) -> (Expr, [char]) {
(f(expr), line)
}
line = space(line);
let (lhs, line) = match line {
['0'..='9', ..] => number(line),
['(', ..rest] => match parse(rest, Power::None) {
(expr, [')', ..rest]) => (expr, rest),
(expr, rest) => panic(fmt("Expected ')', got ", expr, ", ", rest)),
},
[op, ..rest] => parse(rest, pre_bp(op)).map(|lhs| Expr::Op(op, [lhs])),
_ => panic("Unexpected end of input"),
};
while let [op, ..rest] = space(line) {
let (before, after) = inf_bp(op);
if before < power {
break;
};
(lhs, line) = parse(rest, after).map(|rhs| Expr::Op(op, [lhs, rhs]));
};
(lhs, line)
}
fn number(line: [char]) -> (Expr, [char]) {
let value = 0.0;
while (let [first, ..rest] = line) && (let '0'..='9' = first) {
(value, line) = (value * 10.0 + (first as f64 - '0' as f64), rest)
} else (Expr::Atom(value), line)
}
fn space(line: [char]) -> [char] {
match line {
[' ', ..rest] => space(rest),
['\n', ..rest] => space(rest),
line => line
}
}
enum Power {
None,
Factor,
Term,
Exponent,
Unary,
}
fn inf_bp(op: char) -> (i32, i32) {
(|x| (2 * x, 2 * x + 1))(
match op {
'*' => Power::Term,
'/' => Power::Term,
'%' => Power::Term,
'+' => Power::Factor,
'-' => Power::Factor,
_ => panic("Unknown operation: " + op),
})
}
fn pre_bp(op: char) -> i32 {
(|x| 2 * x + 1)(
match op {
'-' => Power::Unary,
_ => panic("Unknown unary operator: " + op),
})
}
fn my_eval(input: str) {
let (expr, rest) = input.chars().parse(0);
println(expr, " ", rest);
execute(expr)
}

View File

@ -1,32 +0,0 @@
fn f(__fmt: str) -> str {
let __out = "";
let __expr = "";
let __depth = 0;
for __c in chars(__fmt) {
match __c {
'{' => {
__depth += 1
if __depth <= 1 {
continue
}
},
'}' => {
__depth -= 1
if __depth <= 0 {
__out += fmt(eval(__expr))
__expr = ""
continue
}
},
':' => if __depth == 1 {
__out += __expr + ": "
},
_ => {}
}
if __depth > 0 {
__expr += __c
} else __out += __c;
}
__out
}

5
sample-code/hello.tp Normal file
View File

@ -0,0 +1,5 @@
// The main function
nasin wan () {
toki_linja("mi toki ale a!")
}

View File

@ -5,10 +5,14 @@ struct Student {
age: i32,
}
fn Student (name: str, age: i32) -> Student {
Student: { name, age }
}
fn match_test(student: Student) {
match student {
Student { name: "shark", age } => println("Found a shark of ", age, " year(s)"),
Student { name, age: 22 } => println("Found a 22-year-old named ", name),
Student { name, age } => println("Found someone named ", name, " of ", age, " year(s)"),
Student: { name: "shark", age } => println("Found a shark of ", age, " year(s)"),
Student: { name, age: 22 } => println("Found a 22-year-old named ", name),
Student: { name, age } => println("Found someone named ", name, " of ", age, " year(s)"),
}
}

10
sample-code/pona.tp Normal file
View File

@ -0,0 +1,10 @@
//! toki!
nasin toki_pona(nimi: linja) -> linja {
seme nimi {
"a" => "PARTICLE: Emphasis or emotion",
_ => "",
}
}
kiwen main: nasin(Linja) -> Linja = toki_pona;

View File

@ -1,8 +0,0 @@
//! Implements a Truth Machine
fn main (n)
match n {
1 => loop print(1),
n => println(n),
}

View File

@ -1,69 +0,0 @@
//! Disjoint Set Forest implementation of the Union Find algorithm
// enum TreeNode {
// Empty,
// Filled { parent: usize, rank: usize }
// }
// TODO: Namespace based on type of first argument
fn makeset(f, x) {
match (*f)[x] {
() => (*f)[x] = Filled { parent: x, rank: 0 },
_ => {}
}
}
fn union(f, a, b) {
let (a, b) = (find(f, a), find(f, b));
if a == b { return; }
match ((*f)[a], (*f)[b]) {
(Filled {parent: _, rank: r_a}, Filled {parent: _, rank: r_b}) => {
if r_a < r_b {
union(f, b, a)
} else {
(*f)[b].parent = a;
(*f)[a].rank += 1;
}
}
}
}
fn find(f, x) {
match (*f)[x] {
Filled { parent, rank } => if parent == x {
x
} else {
let parent = find(f, parent);
(*f)[x].parent = parent;
parent
},
() => x,
}
}
fn show(f) {
for node in 0..(len((*f))) {
match (*f)[node] {
Filled { parent, rank } => println(node, ": { parent: ", parent, ", rank: ", rank, " }"),
_ => {}
}
}
}
fn test(f) {
"Union Find on Disjoint Set Forest".println()
for i in 0..10 { makeset(f, i) }
for i in 10..20 { makeset(f, i) }
show(f)
println()
for i in 1..10 { union(f, i*2-1, i*2) }
for i in 5..10 { union(f, i*2+1, i*2) }
show(f)
}
fn main() {
let f = [();20]
f.test()
}

View File

@ -1,34 +0,0 @@
//! Conlang FFI
use super::preamble::*;
type void = ();
#[extern("C")]
/// void free(void *_Nullable ptr);
///
/// Frees a block of memory previously allocated with `malloc`
fn free(ptr: *void);
#[extern("C")]
/// void *malloc(size_t size);
///
/// Allocates a block of uninitialized memory
fn malloc(size: usize) -> *void;
#[extern("C")]
/// void *calloc(size_t n, size_t size);
///
/// Allocates a block of zero-initialized memory
fn calloc(n: usize, size: usize);
#[extern("C")]
/// void *realloc(void *_Nullable ptr, size_t size);
///
/// Reallocates a block of memory to fit `size` bytes
fn realloc(ptr: *void, size: usize) -> *void;
#[extern("C")]
/// void *reallocarray(void *_Nullable ptr, size_t n, size_t size);
///
/// Reallocates a block of memory to fit `n` elements of `size` bytes.
fn reallocarray(ptr: *void, n: usize, size: usize) -> *void;

View File

@ -1,29 +1,12 @@
//! # The Conlang Standard Library
pub mod preamble {
pub use super::{
io::*,
num::*,
option::Option,
range::{RangeExc, RangeInc},
result::Result,
str::*,
};
pub use super::{num::*, str::str};
}
pub mod ffi;
pub mod io;
pub mod num;
pub mod str;
pub mod option;
pub mod result;
pub mod range;
// #[cfg("test")]
// mod test;
#[cfg("test")]
mod test;

View File

@ -1,19 +0,0 @@
//! The IO module contains functions for interacting with
//! input and output streams
use super::str::str;
/// Immediately causes program execution to stop
#[builtin = "panic"]
fn panic(..args) -> !;
/// Prints whatever you give it
#[builtin = "print"]
fn print(..args);
/// Prints whatever you give it, followed by a newline
#[builtin = "println"]
fn println(..args);
/// Debug-prints a thing, returning it
#[builtin = "dbg"]
fn dbg(arg) -> _ arg

View File

@ -1,51 +1,51 @@
//! The primitive numeric types
#[lang = "bool"]
#[intrinsic = "bool"]
pub type bool;
#[lang = "char"]
#[intrinsic = "char"]
pub type char;
#[lang = "i8"]
#[intrinsic = "i8"]
pub type i8;
#[lang = "i16"]
#[intrinsic = "i16"]
pub type i16;
#[lang = "i32"]
#[intrinsic = "i32"]
pub type i32;
#[lang = "i64"]
#[intrinsic = "i64"]
pub type i64;
#[lang = "i128"]
#[intrinsic = "i128"]
pub type i128;
#[lang = "isize"]
#[intrinsic = "isize"]
pub type isize;
#[lang = "u8"]
#[intrinsic = "u8"]
pub type u8;
#[lang = "u16"]
#[intrinsic = "u16"]
pub type u16;
#[lang = "u32"]
#[intrinsic = "u32"]
pub type u32;
#[lang = "u64"]
#[intrinsic = "u64"]
pub type u64;
#[lang = "u128"]
#[intrinsic = "u128"]
pub type u128;
#[lang = "usize"]
#[intrinsic = "usize"]
pub type usize;
#[lang = "f32"]
#[intrinsic = "f32"]
pub type f32;
#[lang = "f64"]
#[intrinsic = "f64"]
pub type f64;
// Contains implementations for (TODO) overloaded operators on num types
@ -278,9 +278,9 @@ pub mod ops {
}
impl usize {
pub const MIN: Self = u64::MIN as usize; // __march_ptr_width_unsigned_min(); // TODO: intrinsics
pub const MAX: Self = u64::MAX as usize; // __march_ptr_width_unsigned_max(); // TODO: intrinsics
pub const BIT_WIDTH: u32 = u64::BIT_WIDTH; // __march_ptr_width_bits(); // TODO: intrinsics
pub const MIN: Self = __march_ptr_width_unsigned_min();
pub const MAX: Self = __march_ptr_width_unsigned_max();
pub const BIT_WIDTH: u32 = __march_ptr_width_bits();
pub fn default() -> Self {
0
}
@ -512,9 +512,9 @@ pub mod ops {
}
impl isize {
pub const MIN: Self = i64::MIN as isize; // __march_ptr_width_signed_min(); // TODO: intrinsics
pub const MAX: Self = i64::MAX as isize; // __march_ptr_width_signed_max(); // TODO: intrinsics
pub const BIT_WIDTH: u32 = i64::BIT_WIDTH; // __march_ptr_width_bits(); // TODO: intrinsics
pub const MIN: Self = __march_ptr_width_signed_min();
pub const MAX: Self = __march_ptr_width_signed_max();
pub const BIT_WIDTH: u32 = __march_ptr_width_bits();
pub fn default() -> Self {
0
}

View File

@ -1,36 +0,0 @@
//! The optional type, representing the presence or absence of a thing.
use super::preamble::*;
pub enum Option<T> {
Some(T),
None,
}
impl<T> Option<T> {
// pub fn is_some(self) -> bool {
// match self {
// Option::Some(_) => true,
// Option::None() => false,
// }
// }
// pub fn is_none(self: &Self) -> bool {
// match self {
// Option::Some(_) => false,
// Option::None() => true,
// }
// }
// /// Maps from one option space to another
// pub fn map<U>(self: Self, f: fn(T) -> U) -> Option<U> {
// match self {
// Option::Some(value) => Option::Some(f(value)),
// Option::None() => Option::None(),
// }
// }
// pub fn and_then<U>(self: Self, f: fn(T) -> Option<U>) -> Option<U> {
// match self {
// Option::Some(value) => f(value),
// Option::None() => Option::None(),
// }
// }
}

View File

@ -1,17 +0,0 @@
//! Iterable ranges
/// An Exclusive Range `a .. b` iterates from a to b, excluding b
#[lang = "range_exc"]
pub struct RangeExc<T>(T, T)
/// An Inclusive Range `a ..= b` iterates from a to b, including b
#[lang = "range_inc"]
pub struct RangeInc<T>(T, T)
impl<T> RangeExc<T> {
// fn next(self: &Self) -> T {
// let out = (*self.0);
// (*self).0 += 1;
// out
// }
}

View File

@ -1,36 +0,0 @@
//! The Result type, indicating a fallible operation.
use super::preamble::*;
pub enum Result<T, E> {
Ok(T),
Err(E),
}
impl<T, E> Result<T, E> {
// pub fn is_ok(self: &Self) -> bool {
// match *self {
// Ok(_) => true,
// Err(_) => false,
// }
// }
// pub fn is_err(self: &Self) -> bool {
// match *self {
// Ok(_) => false,
// Err(_) => true,
// }
// }
// /// Maps the value inside the Result::Ok, leaving errors alone.
// pub fn map<U>(self: &Self, f: fn(T) -> U) -> Result<U, E> {
// match *self {
// Ok(t) => Ok(f(t)),
// Err(e) => Err(e),
// }
// }
// /// Maps the value inside the Result::Err, leaving values alone.
// pub fn map_err<F>(self: &Self, f: fn(E) -> F) -> Result<T, F> {
// match *self {
// Ok(t) => Ok(t),
// Err(e) => Err(f(e)),
// }
// }
}

View File

@ -1,11 +1,4 @@
//! TODO: give conland a string type
use super::num::{char, u8};
use super::num::u8;
#[lang = "str"]
type str = [u8];
#[builtin = "chars"]
fn chars(s: &str) -> [char];
#[builtin = "fmt"]
fn fmt(..args) -> &str;

View File

@ -4,14 +4,14 @@ use super::preamble::*;
struct UnitLike;
struct TupleLike(super::num::i32, super::test::char)
struct TupleLike(super::num::i32, super::self::test::char)
struct StructLike {
pub member1: UnitLike,
member2: TupleLike,
}
// enum NeverLike;
enum NeverLike;
enum EmptyLike {
Empty,
@ -32,13 +32,12 @@ fn noop () -> bool {
} else break loop if false {
} else break true
} else break true;
}
fn while_else() -> i32 {
let (conditional, pass, fail) = (true, 10, 100 as i32);
while conditional {
pass;
pass
} else {
fail
}
@ -67,7 +66,7 @@ fn if_else() -> i32 {
mod horrible_imports {
mod foo {
use super::{bar::*, baz::*};
struct Foo(&Bar, &Baz)
struct Foo(&Foo, &Bar)
}
mod bar {
use super::{foo::*, baz::*};