Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3cb85c7f42 |
@@ -1,2 +0,0 @@
|
||||
[registries.soft-fish]
|
||||
index = "https://git.soft.fish/j/_cargo-index.git"
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -6,8 +6,5 @@
|
||||
**/Cargo.lock
|
||||
target
|
||||
|
||||
# Symbol table dump?
|
||||
typeck-table.ron
|
||||
|
||||
# Pest files generated by Grammatical
|
||||
*.p*st
|
||||
|
||||
@@ -2,21 +2,21 @@
|
||||
members = [
|
||||
"compiler/cl-repl",
|
||||
"compiler/cl-typeck",
|
||||
"compiler/cl-embed",
|
||||
"compiler/cl-interpret",
|
||||
"compiler/cl-structures",
|
||||
"compiler/cl-token",
|
||||
"compiler/cl-ast",
|
||||
"compiler/cl-parser",
|
||||
"compiler/cl-lexer",
|
||||
"repline",
|
||||
]
|
||||
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"]
|
||||
|
||||
|
||||
@@ -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,42 +244,40 @@ 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
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum TyKind {
|
||||
Never,
|
||||
Infer,
|
||||
Empty,
|
||||
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])
|
||||
@@ -258,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
|
||||
@@ -282,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),
|
||||
}
|
||||
@@ -292,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,
|
||||
}
|
||||
@@ -315,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,
|
||||
}
|
||||
|
||||
@@ -325,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`])\+
|
||||
@@ -355,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),
|
||||
@@ -381,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)]
|
||||
@@ -495,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
|
||||
@@ -529,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
|
||||
@@ -538,8 +511,6 @@ pub enum UnaryKind {
|
||||
Deref,
|
||||
Neg,
|
||||
Not,
|
||||
RangeInc,
|
||||
RangeExc,
|
||||
/// A Loop expression: `loop` [`Block`]
|
||||
Loop,
|
||||
/// Unused
|
||||
@@ -548,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,
|
||||
}
|
||||
|
||||
@@ -566,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`]?
|
||||
@@ -625,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
@@ -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)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,773 +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(),
|
||||
_ => {
|
||||
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 let TyKind::Tuple(TyTuple { types }) = &rety.kind
|
||||
&& !types.as_slice().is_empty()
|
||||
{
|
||||
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::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 let TyKind::Tuple(TyTuple { types }) = &rety.kind
|
||||
&& !types.as_slice().is_empty()
|
||||
{
|
||||
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(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
&& 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 }
|
||||
}
|
||||
}
|
||||
@@ -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::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)
|
||||
}
|
||||
})*
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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 {
|
||||
@@ -531,13 +546,12 @@ pub fn or_fold_use_tree<F: Fold + ?Sized>(folder: &mut F, tree: UseTree) -> UseT
|
||||
pub fn or_fold_ty_kind<F: Fold + ?Sized>(folder: &mut F, kind: TyKind) -> TyKind {
|
||||
match kind {
|
||||
TyKind::Never => TyKind::Never,
|
||||
TyKind::Infer => TyKind::Infer,
|
||||
TyKind::Empty => TyKind::Empty,
|
||||
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)),
|
||||
}
|
||||
}
|
||||
@@ -556,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)),
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,963 +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::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)
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
@@ -3,15 +3,15 @@ use std::fmt::Write;
|
||||
|
||||
impl<W: Write + ?Sized> FmtAdapter for W {}
|
||||
pub trait FmtAdapter: Write {
|
||||
fn indent(&mut self) -> Indent<'_, Self> {
|
||||
fn indent(&mut self) -> Indent<Self> {
|
||||
Indent { f: self }
|
||||
}
|
||||
|
||||
fn delimit(&mut self, delim: Delimiters) -> Delimit<'_, Self> {
|
||||
fn delimit(&mut self, delim: Delimiters) -> Delimit<Self> {
|
||||
Delimit::new(self, delim)
|
||||
}
|
||||
|
||||
fn delimit_with(&mut self, open: &'static str, close: &'static str) -> Delimit<'_, Self> {
|
||||
fn delimit_with(&mut self, open: &'static str, close: &'static str) -> Delimit<Self> {
|
||||
Delimit::new(self, Delimiters { open, close })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
#![feature(decl_macro)]
|
||||
|
||||
pub use ast::*;
|
||||
pub use ast_impl::weight_of::WeightOf;
|
||||
|
||||
pub mod ast;
|
||||
pub mod ast_impl;
|
||||
|
||||
@@ -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 = { version = "*", registry = "soft-fish" }
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
../../../../sample-code/calculator.cl
|
||||
@@ -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(())
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ use std::{error::Error, path::PathBuf};
|
||||
use cl_ast::Expr;
|
||||
use cl_interpret::{convalue::ConValue, env::Environment};
|
||||
use cl_lexer::Lexer;
|
||||
use cl_parser::{Parser, inliner::ModuleInliner};
|
||||
use cl_parser::{inliner::ModuleInliner, Parser};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let mut args = std::env::args();
|
||||
@@ -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,18 +40,15 @@ 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))
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
match env.call(main, &args) {
|
||||
Ok(ConValue::Empty) => {}
|
||||
Ok(retval) => println!("{retval}"),
|
||||
Err(e) => {
|
||||
panic!("{e}");
|
||||
}
|
||||
match env.call(main, &args)? {
|
||||
ConValue::Empty => {}
|
||||
retval => println!("{retval}"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,5 +8,9 @@ fn main() {
|
||||
|
||||
/// Implements the classic recursive definition of fib()
|
||||
fn fib(a: i64) -> i64 {
|
||||
if a > 1 { fib(a - 1) + fib(a - 2) } else { a }
|
||||
if a > 1 {
|
||||
fib(a - 1) + fib(a - 2)
|
||||
} else {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)]
|
||||
@@ -41,12 +44,6 @@ impl std::fmt::Debug for Builtin {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Builtin {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(self.desc)
|
||||
}
|
||||
}
|
||||
|
||||
impl super::Callable for Builtin {
|
||||
fn call(&self, interpreter: &mut Environment, args: &[ConValue]) -> IResult<ConValue> {
|
||||
(self.func)(interpreter, args)
|
||||
@@ -60,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)
|
||||
@@ -81,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)
|
||||
}
|
||||
@@ -104,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![
|
||||
@@ -149,82 +146,30 @@ pub const Builtins: &[Builtin] = &builtins![
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn panic(args @ ..) @env {
|
||||
use std::fmt::Write;
|
||||
let mut out = String::new();
|
||||
if let Err(e) = args.iter().try_for_each(|arg| write!(out, "{arg}")) {
|
||||
println!("{e}");
|
||||
}
|
||||
let mut stdout = stdout().lock();
|
||||
write!(stdout, "Explicit panic: `").ok();
|
||||
args.iter().try_for_each(|arg| write!(stdout, "{arg}") ).ok();
|
||||
writeln!(stdout, "`").ok();
|
||||
Err(Error::Panic(out))?;
|
||||
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::Str(s) => s.chars().count() as _,
|
||||
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::Str(s) => ConValue::Array(s.chars().map(Into::into).collect()),
|
||||
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)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -233,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}')
|
||||
@@ -249,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)?
|
||||
})
|
||||
}
|
||||
|
||||
@@ -258,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)?
|
||||
})
|
||||
}
|
||||
|
||||
@@ -267,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)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -276,16 +217,8 @@ 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),
|
||||
(ConValue::Str(a), ConValue::Str(b)) => (a.to_string() + b).into(),
|
||||
(ConValue::Str(a), ConValue::String(b)) => (a.to_string() + b).into(),
|
||||
(ConValue::String(a), ConValue::Str(b)) => (a.to_string() + b).into(),
|
||||
(ConValue::String(a), ConValue::String(b)) => (a.to_string() + b).into(),
|
||||
(ConValue::Str(s), ConValue::Char(c)) => { let mut s = s.to_string(); s.push(*c); s.into() }
|
||||
(ConValue::String(s), ConValue::Char(c)) => { let mut s = s.to_string(); s.push(*c); s.into() }
|
||||
(ConValue::Char(a), ConValue::Char(b)) => {
|
||||
ConValue::String([a, b].into_iter().collect::<String>())
|
||||
}
|
||||
_ => Err(Error::TypeError())?
|
||||
(ConValue::String(a), ConValue::String(b)) => (a.to_string() + &b.to_string()).into(),
|
||||
_ => Err(Error::TypeError)?
|
||||
})
|
||||
}
|
||||
|
||||
@@ -294,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)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -303,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)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -312,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)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -322,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)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -332,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)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -342,28 +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("RangeExc".into(), Box::new(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("RangeInc".into(), Box::new(Box::new([start.clone(), end.clone()]))))
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
fn RangeTo(end) @env {
|
||||
Ok(ConValue::TupleStruct("RangeTo".into(), Box::new(Box::new([end.clone()]))))
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
fn RangeToInc(end) @env {
|
||||
Ok(ConValue::TupleStruct("RangeToInc".into(), Box::new(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
|
||||
@@ -372,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)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -382,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)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -392,18 +321,16 @@ pub const Math: &[Builtin] = &builtins![
|
||||
(ConValue::Int(a), ConValue::Int(b)) => a.cmp(b) as _,
|
||||
(ConValue::Bool(a), ConValue::Bool(b)) => a.cmp(b) as _,
|
||||
(ConValue::Char(a), ConValue::Char(b)) => a.cmp(b) as _,
|
||||
(ConValue::Str(a), ConValue::Str(b)) => a.cmp(b) as _,
|
||||
(ConValue::Str(a), ConValue::String(b)) => a.to_ref().cmp(b.as_str()) as _,
|
||||
(ConValue::String(a), ConValue::Str(b)) => a.as_str().cmp(b.to_ref()) as _,
|
||||
(ConValue::String(a), ConValue::String(b)) => a.cmp(b) as _,
|
||||
_ => Err(error_format!("Incomparable values: {head}, {tail}"))?
|
||||
}))
|
||||
}
|
||||
|
||||
/// 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(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -51,33 +26,26 @@ pub enum ConValue {
|
||||
Bool(bool),
|
||||
/// A unicode character
|
||||
Char(char),
|
||||
/// A string literal
|
||||
Str(Sym),
|
||||
/// A dynamic string
|
||||
String(String),
|
||||
/// 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]>),
|
||||
// TODO: Instead of storing the identifier, store the index of the struct module
|
||||
/// An exclusive range
|
||||
RangeExc(Integer, Integer),
|
||||
/// An inclusive range
|
||||
RangeInc(Integer, Integer),
|
||||
/// A value of a product type
|
||||
Struct(Sym, Box<HashMap<Sym, ConValue>>),
|
||||
/// A value of a product type with anonymous members
|
||||
TupleStruct(Sym, Box<Box<[ConValue]>>),
|
||||
Struct(Box<(Sym, HashMap<Sym, ConValue>)>),
|
||||
/// An entire namespace
|
||||
Module(Box<HashMap<Sym, ConValue>>),
|
||||
Module(Box<HashMap<Sym, Option<ConValue>>>),
|
||||
/// A quoted expression
|
||||
Quote(Rc<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),
|
||||
}
|
||||
@@ -87,75 +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) -> &'static str {
|
||||
match self {
|
||||
ConValue::Empty => "Empty",
|
||||
ConValue::Int(_) => "i64",
|
||||
ConValue::Float(_) => "f64",
|
||||
ConValue::Bool(_) => "bool",
|
||||
ConValue::Char(_) => "char",
|
||||
ConValue::Str(_) => "str",
|
||||
ConValue::String(_) => "String",
|
||||
ConValue::Ref(_) => "Ref",
|
||||
ConValue::Slice(_, _) => "Slice",
|
||||
ConValue::Array(_) => "Array",
|
||||
ConValue::Tuple(_) => "Tuple",
|
||||
ConValue::Struct(_, _) => "Struct",
|
||||
ConValue::TupleStruct(_, _) => "TupleStruct",
|
||||
ConValue::Module(_) => "",
|
||||
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(id, Box::new(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(id, Box::new(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::Str(string) => string
|
||||
.chars()
|
||||
.nth(index as _)
|
||||
.map(ConValue::Char)
|
||||
.ok_or(Error::OobIndex(index as usize, string.chars().count())),
|
||||
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! {
|
||||
@@ -184,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())),
|
||||
}
|
||||
}
|
||||
@@ -222,11 +136,8 @@ macro cmp ($($fn:ident: $empty:literal, $op:tt);*$(;)?) {$(
|
||||
(Self::Float(a), Self::Float(b)) => Ok(Self::Bool(a $op b)),
|
||||
(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::Str(a), Self::Str(b)) => Ok(Self::Bool(&**a $op &**b)),
|
||||
(Self::Str(a), Self::String(b)) => Ok(Self::Bool(&**a $op &**b)),
|
||||
(Self::String(a), Self::Str(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)
|
||||
}
|
||||
}
|
||||
)*}
|
||||
@@ -244,7 +155,7 @@ macro from ($($T:ty => $v:expr),*$(,)?) {
|
||||
}
|
||||
impl From<&Sym> for ConValue {
|
||||
fn from(value: &Sym) -> Self {
|
||||
ConValue::Str(*value)
|
||||
ConValue::String(*value)
|
||||
}
|
||||
}
|
||||
from! {
|
||||
@@ -252,11 +163,11 @@ from! {
|
||||
f64 => ConValue::Float,
|
||||
bool => ConValue::Bool,
|
||||
char => ConValue::Char,
|
||||
Sym => ConValue::Str,
|
||||
&str => ConValue::Str,
|
||||
Expr => ConValue::Quote,
|
||||
Sym => ConValue::String,
|
||||
&str => ConValue::String,
|
||||
String => ConValue::String,
|
||||
Rc<str> => ConValue::Str,
|
||||
Rc<str> => ConValue::String,
|
||||
ExprKind => ConValue::Quote,
|
||||
Function => ConValue::Function,
|
||||
Vec<ConValue> => ConValue::Tuple,
|
||||
&'static Builtin => ConValue::Builtin,
|
||||
@@ -268,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()),
|
||||
}
|
||||
}
|
||||
@@ -291,34 +202,30 @@ ops! {
|
||||
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
|
||||
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a.wrapping_add(b)),
|
||||
(ConValue::Float(a), ConValue::Float(b)) => ConValue::Float(a + b),
|
||||
(ConValue::Str(a), ConValue::Str(b)) => (a.to_string() + &*b).into(),
|
||||
(ConValue::Str(a), ConValue::String(b)) => (a.to_string() + &*b).into(),
|
||||
(ConValue::String(a), ConValue::Str(b)) => (a.to_string() + &*b).into(),
|
||||
(ConValue::String(a), ConValue::String(b)) => (a.to_string() + &*b).into(),
|
||||
(ConValue::Str(s), ConValue::Char(c)) => { let mut s = s.to_string(); s.push(c); s.into() }
|
||||
(ConValue::String(s), ConValue::Char(c)) => { let mut s = s.to_string(); s.push(c); s.into() }
|
||||
(ConValue::Char(a), ConValue::Char(b)) => {
|
||||
ConValue::String([a, b].into_iter().collect::<String>())
|
||||
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,
|
||||
@@ -326,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,
|
||||
@@ -340,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 {
|
||||
@@ -367,10 +274,8 @@ impl std::fmt::Display for ConValue {
|
||||
ConValue::Float(v) => v.fmt(f),
|
||||
ConValue::Bool(v) => v.fmt(f),
|
||||
ConValue::Char(v) => v.fmt(f),
|
||||
ConValue::Str(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() {
|
||||
@@ -381,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() {
|
||||
@@ -391,20 +298,12 @@ impl std::fmt::Display for ConValue {
|
||||
}
|
||||
')'.fmt(f)
|
||||
}
|
||||
ConValue::TupleStruct(id, tuple) => {
|
||||
write!(f, "{id}")?;
|
||||
'('.fmt(f)?;
|
||||
for (idx, element) in tuple.iter().enumerate() {
|
||||
if idx > 0 {
|
||||
", ".fmt(f)?
|
||||
}
|
||||
element.fmt(f)?
|
||||
}
|
||||
')'.fmt(f)
|
||||
}
|
||||
ConValue::Struct(id, map) => {
|
||||
ConValue::Struct(parts) => {
|
||||
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},")?;
|
||||
@@ -415,7 +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},")?;
|
||||
write!(f, "\n{k}: ")?;
|
||||
match v {
|
||||
Some(v) => write!(f, "{v},"),
|
||||
None => write!(f, "_,"),
|
||||
}?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -425,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)
|
||||
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)))
|
||||
}}
|
||||
|
||||
@@ -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<'_> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,127 +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,
|
||||
pub(super) 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 }
|
||||
}
|
||||
/// Explicit panic
|
||||
pub fn Panic(msg: String) -> Self {
|
||||
Self { kind: ErrorKind::Panic(msg, 0), 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
|
||||
@@ -132,8 +19,6 @@ pub enum ErrorKind {
|
||||
Continue,
|
||||
/// Underflowed the stack
|
||||
StackUnderflow,
|
||||
/// Overflowed the stack
|
||||
StackOverflow(usize),
|
||||
/// Exited the last scope
|
||||
ScopeExit,
|
||||
/// Type incompatibility
|
||||
@@ -159,60 +44,54 @@ pub enum ErrorKind {
|
||||
PatFailed(Box<Pattern>),
|
||||
/// Fell through a non-exhaustive match
|
||||
MatchNonexhaustive,
|
||||
/// Explicit panic
|
||||
Panic(String, usize),
|
||||
/// 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::Panic(s, _depth) => write!(f, "Explicit panic: {s}"),
|
||||
ErrorKind::BuiltinError(s) => write!(f, "{s}"),
|
||||
Error::BuiltinDebug(s) => write!(f, "DEBUG: {s}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)]
|
||||
@@ -33,7 +31,7 @@ impl Function {
|
||||
pub fn decl(&self) -> &FnDecl {
|
||||
&self.decl
|
||||
}
|
||||
pub fn upvars(&self) -> Ref<'_, Upvars> {
|
||||
pub fn upvars(&self) -> Ref<Upvars> {
|
||||
self.upvars.borrow()
|
||||
}
|
||||
pub fn lift_upvars(&self, env: &Environment) {
|
||||
@@ -50,34 +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)),
|
||||
Err(Error { kind: ErrorKind::Panic(msg, depth), span: Some(span) }) => {
|
||||
println!("{depth:>4}: {name}{bind} at {}", span.head);
|
||||
Err(Error { kind: ErrorKind::Panic(msg, depth + 1), span: None })
|
||||
}
|
||||
other => other,
|
||||
Err(Error::Return(value)) => Ok(value),
|
||||
Err(Error::Break(value)) => Err(Error::BadBreak(value)),
|
||||
result => result,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
@@ -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,648 +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(name, Box::new(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)]
|
||||
|
||||
@@ -1,347 +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::Str(b)) => {
|
||||
(*a == *b).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::Str(b)) => {
|
||||
(&*b < a).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::Str(b),
|
||||
) => (a.as_str() <= b.to_ref() && b.to_ref() < c.as_str())
|
||||
.then_some(())
|
||||
.ok_or(Error::NotAssignable()),
|
||||
(
|
||||
Pattern::Literal(Literal::String(a)),
|
||||
Pattern::Literal(Literal::String(c)),
|
||||
ConValue::String(b),
|
||||
) => (a.as_str() <= b.as_str() && b.as_str() < 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::Str(b),
|
||||
) => (a.as_str() <= b.to_ref() && b.to_ref() <= c.as_str())
|
||||
.then_some(())
|
||||
.ok_or(Error::NotAssignable()),
|
||||
(
|
||||
Pattern::Literal(Literal::String(a)),
|
||||
Pattern::Literal(Literal::String(c)),
|
||||
ConValue::String(b),
|
||||
) => (a.as_str() <= b.as_str() && b.as_str() <= 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(id, values)) => {
|
||||
let tid = path
|
||||
.as_sym()
|
||||
.ok_or_else(|| Error::PatFailed(pat.clone().into()))?;
|
||||
if id != tid {
|
||||
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(id, mut values)) => {
|
||||
let tid = path
|
||||
.as_sym()
|
||||
.ok_or_else(|| Error::PatFailed(pat.clone().into()))?;
|
||||
if id != tid {
|
||||
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)
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -5,7 +5,7 @@ use cl_structures::span::Loc;
|
||||
use cl_token::{TokenKind as Kind, *};
|
||||
use std::{
|
||||
iter::Peekable,
|
||||
str::{CharIndices, FromStr},
|
||||
str::{Chars, FromStr},
|
||||
};
|
||||
use unicode_ident::*;
|
||||
|
||||
@@ -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
|
||||
@@ -76,379 +76,408 @@ pub mod lexer_iter {
|
||||
/// ```
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Lexer<'t> {
|
||||
/// The source text
|
||||
text: &'t str,
|
||||
/// A peekable iterator over the source text
|
||||
iter: Peekable<CharIndices<'t>>,
|
||||
/// The end of the current token
|
||||
head: usize,
|
||||
/// The (line, col) end of the current token
|
||||
head_loc: (u32, u32),
|
||||
/// The start of the current token
|
||||
tail: usize,
|
||||
/// The (line, col) start of the current token
|
||||
tail_loc: (u32, u32),
|
||||
iter: Peekable<Chars<'t>>,
|
||||
start: usize,
|
||||
start_loc: (u32, u32),
|
||||
current: usize,
|
||||
current_loc: (u32, u32),
|
||||
}
|
||||
|
||||
impl<'t> Lexer<'t> {
|
||||
/// Creates a new [Lexer] over a [str]
|
||||
pub fn new(text: &'t str) -> Self {
|
||||
Self {
|
||||
text,
|
||||
iter: text.char_indices().peekable(),
|
||||
head: 0,
|
||||
head_loc: (1, 1),
|
||||
tail: 0,
|
||||
tail_loc: (1, 1),
|
||||
iter: text.chars().peekable(),
|
||||
start: 0,
|
||||
start_loc: (1, 1),
|
||||
current: 0,
|
||||
current_loc: (1, 1),
|
||||
}
|
||||
}
|
||||
/// Scans through the text, searching for the next [Token]
|
||||
pub fn scan(&mut self) -> LResult<Token> {
|
||||
match self.skip_whitespace().peek()? {
|
||||
'{' => self.consume()?.produce_op(Kind::LCurly),
|
||||
'}' => self.consume()?.produce_op(Kind::RCurly),
|
||||
'[' => self.consume()?.produce_op(Kind::LBrack),
|
||||
']' => self.consume()?.produce_op(Kind::RBrack),
|
||||
'(' => self.consume()?.produce_op(Kind::LParen),
|
||||
')' => self.consume()?.produce_op(Kind::RParen),
|
||||
'&' => self.consume()?.amp(),
|
||||
'@' => self.consume()?.produce_op(Kind::At),
|
||||
'\\' => self.consume()?.produce_op(Kind::Backslash),
|
||||
'!' => self.consume()?.bang(),
|
||||
'|' => self.consume()?.bar(),
|
||||
':' => self.consume()?.colon(),
|
||||
',' => self.consume()?.produce_op(Kind::Comma),
|
||||
'.' => self.consume()?.dot(),
|
||||
'=' => self.consume()?.equal(),
|
||||
'`' => self.consume()?.produce_op(Kind::Grave),
|
||||
'>' => self.consume()?.greater(),
|
||||
'#' => self.consume()?.hash(),
|
||||
'<' => self.consume()?.less(),
|
||||
'-' => self.consume()?.minus(),
|
||||
'+' => self.consume()?.plus(),
|
||||
'?' => self.consume()?.produce_op(Kind::Question),
|
||||
'%' => self.consume()?.rem(),
|
||||
';' => self.consume()?.produce_op(Kind::Semi),
|
||||
'/' => self.consume()?.slash(),
|
||||
'*' => self.consume()?.star(),
|
||||
'~' => self.consume()?.produce_op(Kind::Tilde),
|
||||
'^' => self.consume()?.xor(),
|
||||
'0' => self.consume()?.int_with_base(),
|
||||
'1'..='9' => self.digits::<10>(),
|
||||
'"' => self.consume()?.string(),
|
||||
'\'' => self.consume()?.character(),
|
||||
'_' => self.identifier(),
|
||||
i if is_xid_start(i) => self.identifier(),
|
||||
e => {
|
||||
let err = Err(Error::unexpected_char(e, self.line(), self.col()));
|
||||
let _ = self.consume();
|
||||
err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the current line
|
||||
pub fn line(&self) -> u32 {
|
||||
self.tail_loc.0
|
||||
self.start_loc.0
|
||||
}
|
||||
|
||||
/// Returns the current column
|
||||
pub fn col(&self) -> u32 {
|
||||
self.tail_loc.1
|
||||
self.start_loc.1
|
||||
}
|
||||
|
||||
/// Returns the current token's lexeme
|
||||
fn lexeme(&mut self) -> &'t str {
|
||||
&self.text[self.tail..self.head]
|
||||
fn next(&mut self) -> LResult<char> {
|
||||
let out = self.peek();
|
||||
self.consume()?;
|
||||
out
|
||||
}
|
||||
|
||||
/// Peeks the next character without advancing the lexer
|
||||
fn peek(&mut self) -> Option<char> {
|
||||
self.iter.peek().map(|(_, c)| *c)
|
||||
fn peek(&mut self) -> LResult<char> {
|
||||
self.iter
|
||||
.peek()
|
||||
.copied()
|
||||
.ok_or(Error::end_of_file(self.line(), self.col()))
|
||||
}
|
||||
|
||||
/// Advances the 'tail' (current position)
|
||||
fn advance_tail(&mut self) {
|
||||
let (idx, c) = self.iter.peek().copied().unwrap_or((self.text.len(), '\0'));
|
||||
let (line, col) = &mut self.head_loc;
|
||||
let diff = idx - self.head;
|
||||
|
||||
self.head = idx;
|
||||
match c {
|
||||
'\n' => {
|
||||
fn produce(&mut self, kind: Kind, data: impl Into<TokenData>) -> LResult<Token> {
|
||||
let loc = self.start_loc;
|
||||
self.start_loc = self.current_loc;
|
||||
self.start = self.current;
|
||||
Ok(Token::new(kind, data, loc.0, loc.1))
|
||||
}
|
||||
fn produce_op(&mut self, kind: Kind) -> LResult<Token> {
|
||||
self.produce(kind, ())
|
||||
}
|
||||
fn skip_whitespace(&mut self) -> &mut Self {
|
||||
while let Ok(c) = self.peek() {
|
||||
if !c.is_whitespace() {
|
||||
break;
|
||||
}
|
||||
let _ = self.consume();
|
||||
}
|
||||
self.start = self.current;
|
||||
self.start_loc = self.current_loc;
|
||||
self
|
||||
}
|
||||
fn consume(&mut self) -> LResult<&mut Self> {
|
||||
self.current += 1;
|
||||
match self.iter.next() {
|
||||
Some('\n') => {
|
||||
let (line, col) = &mut self.current_loc;
|
||||
*line += 1;
|
||||
*col = 1;
|
||||
}
|
||||
_ => *col += diff as u32,
|
||||
Some(_) => self.current_loc.1 += 1,
|
||||
None => Err(Error::end_of_file(self.line(), self.col()))?,
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
/// Digraphs and trigraphs
|
||||
impl Lexer<'_> {
|
||||
fn amp(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('&') => self.consume()?.produce_op(Kind::AmpAmp),
|
||||
Ok('=') => self.consume()?.produce_op(Kind::AmpEq),
|
||||
_ => self.produce_op(Kind::Amp),
|
||||
}
|
||||
}
|
||||
fn bang(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('!') => self.consume()?.produce_op(Kind::BangBang),
|
||||
Ok('=') => self.consume()?.produce_op(Kind::BangEq),
|
||||
_ => self.produce_op(Kind::Bang),
|
||||
}
|
||||
}
|
||||
fn bar(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('|') => self.consume()?.produce_op(Kind::BarBar),
|
||||
Ok('=') => self.consume()?.produce_op(Kind::BarEq),
|
||||
_ => self.produce_op(Kind::Bar),
|
||||
}
|
||||
}
|
||||
fn colon(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok(':') => self.consume()?.produce_op(Kind::ColonColon),
|
||||
_ => self.produce_op(Kind::Colon),
|
||||
}
|
||||
}
|
||||
fn dot(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('.') => {
|
||||
if let Ok('=') = self.consume()?.peek() {
|
||||
self.consume()?.produce_op(Kind::DotDotEq)
|
||||
} else {
|
||||
self.produce_op(Kind::DotDot)
|
||||
}
|
||||
}
|
||||
_ => self.produce_op(Kind::Dot),
|
||||
}
|
||||
}
|
||||
fn equal(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('=') => self.consume()?.produce_op(Kind::EqEq),
|
||||
Ok('>') => self.consume()?.produce_op(Kind::FatArrow),
|
||||
_ => self.produce_op(Kind::Eq),
|
||||
}
|
||||
}
|
||||
fn greater(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('=') => self.consume()?.produce_op(Kind::GtEq),
|
||||
Ok('>') => {
|
||||
if let Ok('=') = self.consume()?.peek() {
|
||||
self.consume()?.produce_op(Kind::GtGtEq)
|
||||
} else {
|
||||
self.produce_op(Kind::GtGt)
|
||||
}
|
||||
}
|
||||
_ => self.produce_op(Kind::Gt),
|
||||
}
|
||||
}
|
||||
fn hash(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('!') => self.consume()?.hashbang(),
|
||||
_ => self.produce_op(Kind::Hash),
|
||||
}
|
||||
}
|
||||
fn hashbang(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('/' | '\'') => self.line_comment(),
|
||||
_ => self.produce_op(Kind::HashBang),
|
||||
}
|
||||
}
|
||||
fn less(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('=') => self.consume()?.produce_op(Kind::LtEq),
|
||||
Ok('<') => {
|
||||
if let Ok('=') = self.consume()?.peek() {
|
||||
self.consume()?.produce_op(Kind::LtLtEq)
|
||||
} else {
|
||||
self.produce_op(Kind::LtLt)
|
||||
}
|
||||
}
|
||||
_ => self.produce_op(Kind::Lt),
|
||||
}
|
||||
}
|
||||
fn minus(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('=') => self.consume()?.produce_op(Kind::MinusEq),
|
||||
Ok('>') => self.consume()?.produce_op(Kind::Arrow),
|
||||
_ => self.produce_op(Kind::Minus),
|
||||
}
|
||||
}
|
||||
fn plus(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('=') => self.consume()?.produce_op(Kind::PlusEq),
|
||||
_ => self.produce_op(Kind::Plus),
|
||||
}
|
||||
}
|
||||
fn rem(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('=') => self.consume()?.produce_op(Kind::RemEq),
|
||||
_ => self.produce_op(Kind::Rem),
|
||||
}
|
||||
}
|
||||
fn slash(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('=') => self.consume()?.produce_op(Kind::SlashEq),
|
||||
Ok('/') => self.consume()?.line_comment(),
|
||||
Ok('*') => self.consume()?.block_comment(),
|
||||
_ => self.produce_op(Kind::Slash),
|
||||
}
|
||||
}
|
||||
fn star(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('=') => self.consume()?.produce_op(Kind::StarEq),
|
||||
_ => self.produce_op(Kind::Star),
|
||||
}
|
||||
}
|
||||
fn xor(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('=') => self.consume()?.produce_op(Kind::XorEq),
|
||||
Ok('^') => self.consume()?.produce_op(Kind::XorXor),
|
||||
_ => self.produce_op(Kind::Xor),
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes the last-peeked character, or the next character if none peeked.
|
||||
pub fn take(&mut self) -> Option<char> {
|
||||
let (_, c) = self.iter.next()?;
|
||||
self.advance_tail();
|
||||
Some(c)
|
||||
}
|
||||
|
||||
/// Takes the next char if it matches the `expected` char
|
||||
pub fn next_if(&mut self, expected: char) -> Option<char> {
|
||||
let (_, c) = self.iter.next_if(|&(_, c)| c == expected)?;
|
||||
self.advance_tail();
|
||||
Some(c)
|
||||
}
|
||||
|
||||
/// Consumes the last-peeked character, advancing the tail
|
||||
pub fn consume(&mut self) -> &mut Self {
|
||||
self.iter.next();
|
||||
self.advance_tail();
|
||||
self
|
||||
}
|
||||
|
||||
/// Produces an [Error] at the start of the current token
|
||||
fn error(&self, reason: Reason) -> Error {
|
||||
Error { reason, line: self.line(), col: self.col() }
|
||||
}
|
||||
|
||||
/// Produces a token with the current [lexeme](Lexer::lexeme) as its data
|
||||
fn produce(&mut self, kind: Kind) -> LResult<Token> {
|
||||
let lexeme = self.lexeme().to_owned();
|
||||
self.produce_with(kind, lexeme)
|
||||
}
|
||||
|
||||
/// Produces a token with the provided `data`
|
||||
fn produce_with(&mut self, kind: Kind, data: impl Into<TokenData>) -> LResult<Token> {
|
||||
let loc = self.tail_loc;
|
||||
self.tail_loc = self.head_loc;
|
||||
self.tail = self.head;
|
||||
Ok(Token::new(kind, data, loc.0, loc.1))
|
||||
}
|
||||
|
||||
/// Produces a token with no `data`
|
||||
fn produce_op(&mut self, kind: Kind) -> LResult<Token> {
|
||||
self.produce_with(kind, ())
|
||||
}
|
||||
|
||||
/// Consumes 0 or more whitespace
|
||||
fn skip_whitespace(&mut self) -> &mut Self {
|
||||
while self.peek().is_some_and(char::is_whitespace) {
|
||||
let _ = self.consume();
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Starts a new token
|
||||
fn start_token(&mut self) -> &mut Self {
|
||||
self.tail_loc = self.head_loc;
|
||||
self.tail = self.head;
|
||||
self
|
||||
}
|
||||
|
||||
/// Scans through the text, searching for the next [Token]
|
||||
pub fn scan(&mut self) -> LResult<Token> {
|
||||
use TokenKind::*;
|
||||
// !"#%&'()*+,-./:;<=>?@[\\]^`{|}~
|
||||
let tok = match self
|
||||
.skip_whitespace()
|
||||
.start_token()
|
||||
.peek()
|
||||
.ok_or_else(|| self.error(Reason::EndOfFile))?
|
||||
{
|
||||
'!' => Bang,
|
||||
'"' => return self.string(),
|
||||
'#' => Hash,
|
||||
'%' => Rem,
|
||||
'&' => Amp,
|
||||
'\'' => return self.character(),
|
||||
'(' => LParen,
|
||||
')' => RParen,
|
||||
'*' => Star,
|
||||
'+' => Plus,
|
||||
',' => Comma,
|
||||
'-' => Minus,
|
||||
'.' => Dot,
|
||||
'/' => Slash,
|
||||
'0' => TokenKind::Literal,
|
||||
'1'..='9' => return self.digits::<10>(),
|
||||
':' => Colon,
|
||||
';' => Semi,
|
||||
'<' => Lt,
|
||||
'=' => Eq,
|
||||
'>' => Gt,
|
||||
'?' => Question,
|
||||
'@' => At,
|
||||
'[' => LBrack,
|
||||
'\\' => Backslash,
|
||||
']' => RBrack,
|
||||
'^' => Xor,
|
||||
'`' => Grave,
|
||||
'{' => LCurly,
|
||||
'|' => Bar,
|
||||
'}' => RCurly,
|
||||
'~' => Tilde,
|
||||
'_' => return self.identifier(),
|
||||
c if is_xid_start(c) => return self.identifier(),
|
||||
e => {
|
||||
let err = Err(self.error(Reason::UnexpectedChar(e)));
|
||||
let _ = self.consume();
|
||||
err?
|
||||
}
|
||||
};
|
||||
|
||||
// Handle digraphs
|
||||
let tok = match (tok, self.consume().peek()) {
|
||||
(Literal, Some('b')) => return self.consume().digits::<2>(),
|
||||
(Literal, Some('d')) => return self.consume().digits::<10>(),
|
||||
(Literal, Some('o')) => return self.consume().digits::<8>(),
|
||||
(Literal, Some('x')) => return self.consume().digits::<16>(),
|
||||
(Literal, Some('~')) => return self.consume().digits::<36>(),
|
||||
(Literal, _) => return self.digits::<10>(),
|
||||
(Amp, Some('&')) => AmpAmp,
|
||||
(Amp, Some('=')) => AmpEq,
|
||||
(Bang, Some('!')) => BangBang,
|
||||
(Bang, Some('=')) => BangEq,
|
||||
(Bar, Some('|')) => BarBar,
|
||||
(Bar, Some('=')) => BarEq,
|
||||
(Colon, Some(':')) => ColonColon,
|
||||
(Dot, Some('.')) => DotDot,
|
||||
(Eq, Some('=')) => EqEq,
|
||||
(Eq, Some('>')) => FatArrow,
|
||||
(Gt, Some('=')) => GtEq,
|
||||
(Gt, Some('>')) => GtGt,
|
||||
(Hash, Some('!')) => HashBang,
|
||||
(Lt, Some('=')) => LtEq,
|
||||
(Lt, Some('<')) => LtLt,
|
||||
(Minus, Some('=')) => MinusEq,
|
||||
(Minus, Some('>')) => Arrow,
|
||||
(Plus, Some('=')) => PlusEq,
|
||||
(Rem, Some('=')) => RemEq,
|
||||
(Slash, Some('*')) => return self.block_comment()?.produce(Kind::Comment),
|
||||
(Slash, Some('/')) => return self.line_comment(),
|
||||
(Slash, Some('=')) => SlashEq,
|
||||
(Star, Some('=')) => StarEq,
|
||||
(Xor, Some('=')) => XorEq,
|
||||
(Xor, Some('^')) => XorXor,
|
||||
_ => return self.produce_op(tok),
|
||||
};
|
||||
|
||||
// Handle trigraphs
|
||||
let tok = match (tok, self.consume().peek()) {
|
||||
(HashBang, Some('/')) => return self.line_comment(),
|
||||
(DotDot, Some('=')) => DotDotEq,
|
||||
(GtGt, Some('=')) => GtGtEq,
|
||||
(LtLt, Some('=')) => LtLtEq,
|
||||
_ => return self.produce_op(tok),
|
||||
};
|
||||
|
||||
self.consume().produce_op(tok)
|
||||
}
|
||||
}
|
||||
|
||||
/// Comments
|
||||
impl Lexer<'_> {
|
||||
/// Consumes until the next newline '\n', producing a [Comment](Kind::Comment)
|
||||
fn line_comment(&mut self) -> LResult<Token> {
|
||||
while self.consume().peek().is_some_and(|c| c != '\n') {}
|
||||
self.produce(Kind::Comment)
|
||||
}
|
||||
|
||||
/// Consumes nested block-comments. Does not produce by itself.
|
||||
fn block_comment(&mut self) -> LResult<&mut Self> {
|
||||
self.consume();
|
||||
while let Some(c) = self.take() {
|
||||
match (c, self.peek()) {
|
||||
('/', Some('*')) => self.block_comment()?,
|
||||
('*', Some('/')) => return Ok(self.consume()),
|
||||
_ => continue,
|
||||
};
|
||||
let mut comment = String::new();
|
||||
while Ok('\n') != self.peek() {
|
||||
comment.push(self.next()?);
|
||||
}
|
||||
Err(self.error(Reason::UnmatchedDelimiters('/')))
|
||||
self.produce(Kind::Comment, comment)
|
||||
}
|
||||
fn block_comment(&mut self) -> LResult<Token> {
|
||||
let mut comment = String::new();
|
||||
while let Ok(c) = self.next() {
|
||||
if '*' == c && Ok('/') == self.peek() {
|
||||
break;
|
||||
}
|
||||
comment.push(c);
|
||||
}
|
||||
self.consume()?.produce(Kind::Comment, comment)
|
||||
}
|
||||
}
|
||||
|
||||
/// Identifiers
|
||||
impl Lexer<'_> {
|
||||
/// Produces an [Identifier](Kind::Identifier) or keyword
|
||||
fn identifier(&mut self) -> LResult<Token> {
|
||||
while self.consume().peek().is_some_and(is_xid_continue) {}
|
||||
if let Ok(keyword) = Kind::from_str(self.lexeme()) {
|
||||
self.produce_with(keyword, ())
|
||||
let mut out = String::from(self.xid_start()?);
|
||||
while let Ok(c) = self.xid_continue() {
|
||||
out.push(c)
|
||||
}
|
||||
if let Ok(keyword) = Kind::from_str(&out) {
|
||||
self.produce(keyword, ())
|
||||
} else {
|
||||
self.produce(Kind::Identifier)
|
||||
self.produce(Kind::Identifier, TokenData::String(out))
|
||||
}
|
||||
}
|
||||
fn xid_start(&mut self) -> LResult<char> {
|
||||
match self.peek()? {
|
||||
xid if xid == '_' || is_xid_start(xid) => {
|
||||
self.consume()?;
|
||||
Ok(xid)
|
||||
}
|
||||
bad => Err(Error::not_identifier(bad, self.line(), self.col())),
|
||||
}
|
||||
}
|
||||
fn xid_continue(&mut self) -> LResult<char> {
|
||||
match self.peek()? {
|
||||
xid if is_xid_continue(xid) => {
|
||||
self.consume()?;
|
||||
Ok(xid)
|
||||
}
|
||||
bad => Err(Error::not_identifier(bad, self.line(), self.col())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Integers
|
||||
impl Lexer<'_> {
|
||||
/// Produces a [Literal](Kind::Literal) with an integer or float value.
|
||||
fn int_with_base(&mut self) -> LResult<Token> {
|
||||
match self.peek() {
|
||||
Ok('x') => self.consume()?.digits::<16>(),
|
||||
Ok('d') => self.consume()?.digits::<10>(),
|
||||
Ok('o') => self.consume()?.digits::<8>(),
|
||||
Ok('b') => self.consume()?.digits::<2>(),
|
||||
Ok('0'..='9' | '.') => self.digits::<10>(),
|
||||
_ => self.produce(Kind::Literal, 0),
|
||||
}
|
||||
}
|
||||
fn digits<const B: u32>(&mut self) -> LResult<Token> {
|
||||
let mut value = 0;
|
||||
while let Some(true) = self.peek().as_ref().map(char::is_ascii_alphanumeric) {
|
||||
while let Ok(true) = self.peek().as_ref().map(char::is_ascii_alphanumeric) {
|
||||
value = value * B as u128 + self.digit::<B>()? as u128;
|
||||
}
|
||||
// TODO: find a better way to handle floats in the tokenizer
|
||||
match self.peek() {
|
||||
Some('.') => {
|
||||
Ok('.') => {
|
||||
// FIXME: hack: 0.. is not [0.0, '.']
|
||||
if let Some('.') = self.clone().consume().take() {
|
||||
return self.produce_with(Kind::Literal, value);
|
||||
if let Ok('.') = self.clone().consume()?.next() {
|
||||
return self.produce(Kind::Literal, value);
|
||||
}
|
||||
let mut float = format!("{value}.");
|
||||
self.consume();
|
||||
while let Some(true) = self.peek().as_ref().map(char::is_ascii_digit) {
|
||||
float.push(self.iter.next().map(|(_, c)| c).unwrap_or_default());
|
||||
self.consume()?;
|
||||
while let Ok(true) = self.peek().as_ref().map(char::is_ascii_digit) {
|
||||
float.push(self.iter.next().unwrap_or_default());
|
||||
}
|
||||
let float = f64::from_str(&float).expect("must be parsable as float");
|
||||
self.produce_with(Kind::Literal, float)
|
||||
self.produce(Kind::Literal, float)
|
||||
}
|
||||
_ => self.produce_with(Kind::Literal, value),
|
||||
_ => self.produce(Kind::Literal, value),
|
||||
}
|
||||
}
|
||||
|
||||
/// Consumes a single digit of base [B](Lexer::digit)
|
||||
fn digit<const B: u32>(&mut self) -> LResult<u32> {
|
||||
let digit = self.take().ok_or_else(|| self.error(Reason::EndOfFile))?;
|
||||
let digit = self.peek()?;
|
||||
self.consume()?;
|
||||
digit
|
||||
.to_digit(B)
|
||||
.ok_or_else(|| self.error(Reason::InvalidDigit(digit)))
|
||||
.ok_or(Error::invalid_digit(digit, self.line(), self.col()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Strings and characters
|
||||
impl Lexer<'_> {
|
||||
/// Produces a [Literal](Kind::Literal) with a pre-escaped [String]
|
||||
pub fn string(&mut self) -> Result<Token, Error> {
|
||||
let mut lexeme = String::new();
|
||||
let mut depth = 0;
|
||||
self.consume();
|
||||
loop {
|
||||
lexeme.push(match self.take() {
|
||||
None => Err(self.error(Reason::UnmatchedDelimiters('"')))?,
|
||||
Some('\\') => self.unescape()?,
|
||||
Some('"') if depth == 0 => break,
|
||||
Some(c @ '{') => {
|
||||
depth += 1;
|
||||
c
|
||||
}
|
||||
Some(c @ '}') => {
|
||||
depth -= 1;
|
||||
c
|
||||
}
|
||||
Some(c) => c,
|
||||
})
|
||||
fn string(&mut self) -> LResult<Token> {
|
||||
let mut value = String::new();
|
||||
while '"'
|
||||
!= self
|
||||
.peek()
|
||||
.map_err(|e| e.mask_reason(Reason::UnmatchedDelimiters('"')))?
|
||||
{
|
||||
value.push(self.unescape()?)
|
||||
}
|
||||
lexeme.shrink_to_fit();
|
||||
self.produce_with(Kind::Literal, lexeme)
|
||||
self.consume()?.produce(Kind::Literal, value)
|
||||
}
|
||||
|
||||
/// Produces a [Literal](Kind::Literal) with a pre-escaped [char]
|
||||
fn character(&mut self) -> Result<Token, Error> {
|
||||
let c = match self.consume().take() {
|
||||
Some('\\') => self.unescape()?,
|
||||
Some(c) => c,
|
||||
None => '\0',
|
||||
};
|
||||
if self.take().is_some_and(|c| c == '\'') {
|
||||
self.produce_with(Kind::Literal, c)
|
||||
} else {
|
||||
Err(self.error(Reason::UnmatchedDelimiters('\'')))
|
||||
fn character(&mut self) -> LResult<Token> {
|
||||
let out = self.unescape()?;
|
||||
match self.peek()? {
|
||||
'\'' => self.consume()?.produce(Kind::Literal, out),
|
||||
_ => Err(Error::unmatched_delimiters('\'', self.line(), self.col())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Unescapes a single character
|
||||
#[rustfmt::skip]
|
||||
/// Unescape a single character
|
||||
fn unescape(&mut self) -> LResult<char> {
|
||||
Ok(match self.take().ok_or_else(|| self.error(Reason::EndOfFile))? {
|
||||
' ' => '\u{a0}',
|
||||
'0' => '\0',
|
||||
match self.next() {
|
||||
Ok('\\') => (),
|
||||
other => return other,
|
||||
}
|
||||
Ok(match self.next()? {
|
||||
'a' => '\x07',
|
||||
'b' => '\x08',
|
||||
'e' => '\x1b',
|
||||
'f' => '\x0c',
|
||||
'n' => '\n',
|
||||
'r' => '\r',
|
||||
't' => '\t',
|
||||
'u' => self.unicode_escape()?,
|
||||
'x' => self.hex_escape()?,
|
||||
'u' => self.unicode_escape()?,
|
||||
'0' => '\0',
|
||||
chr => chr,
|
||||
})
|
||||
}
|
||||
/// Unescapes a single 2-digit hex escape
|
||||
/// unescape a single 2-digit hex escape
|
||||
fn hex_escape(&mut self) -> LResult<char> {
|
||||
let out = (self.digit::<16>()? << 4) + self.digit::<16>()?;
|
||||
char::from_u32(out).ok_or_else(|| self.error(Reason::BadUnicode(out)))
|
||||
char::from_u32(out).ok_or(Error::bad_unicode(out, self.line(), self.col()))
|
||||
}
|
||||
|
||||
/// Unescapes a single \u{} unicode escape
|
||||
pub fn unicode_escape(&mut self) -> Result<char, Error> {
|
||||
self.next_if('{')
|
||||
.ok_or_else(|| self.error(Reason::InvalidEscape('u')))?;
|
||||
/// unescape a single \u{} unicode escape
|
||||
fn unicode_escape(&mut self) -> LResult<char> {
|
||||
let mut out = 0;
|
||||
while let Some(c) = self.take() {
|
||||
if c == '}' {
|
||||
return char::from_u32(out).ok_or_else(|| self.error(Reason::BadUnicode(out)));
|
||||
let Ok('{') = self.peek() else {
|
||||
return Err(Error::invalid_escape('u', self.line(), self.col()));
|
||||
};
|
||||
self.consume()?;
|
||||
while let Ok(c) = self.peek() {
|
||||
match c {
|
||||
'}' => {
|
||||
self.consume()?;
|
||||
return char::from_u32(out).ok_or(Error::bad_unicode(
|
||||
out,
|
||||
self.line(),
|
||||
self.col(),
|
||||
));
|
||||
}
|
||||
_ => out = (out << 4) + self.digit::<16>()?,
|
||||
}
|
||||
out = out * 16
|
||||
+ c.to_digit(16)
|
||||
.ok_or_else(|| self.error(Reason::InvalidDigit(c)))?;
|
||||
}
|
||||
Err(self.error(Reason::UnmatchedDelimiters('}')))
|
||||
Err(Error::invalid_escape('u', self.line(), self.col()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -478,6 +507,8 @@ pub mod error {
|
||||
UnmatchedDelimiters(char),
|
||||
/// Found a character that doesn't belong to any [TokenKind](cl_token::TokenKind)
|
||||
UnexpectedChar(char),
|
||||
/// Found a character that's not valid in identifiers while looking for an identifier
|
||||
NotIdentifier(char),
|
||||
/// Found a character that's not valid in an escape sequence while looking for an escape
|
||||
/// sequence
|
||||
UnknownEscape(char),
|
||||
@@ -485,12 +516,30 @@ pub mod error {
|
||||
InvalidEscape(char),
|
||||
/// Character is not a valid digit in the requested base
|
||||
InvalidDigit(char),
|
||||
/// Base conversion requested, but the base character was not in the set of known
|
||||
/// characters
|
||||
UnknownBase(char),
|
||||
/// Unicode escape does not map to a valid unicode code-point
|
||||
BadUnicode(u32),
|
||||
/// Reached end of input
|
||||
EndOfFile,
|
||||
}
|
||||
error_impl! {
|
||||
unmatched_delimiters(c: char) => Reason::UnmatchedDelimiters(c),
|
||||
unexpected_char(c: char) => Reason::UnexpectedChar(c),
|
||||
not_identifier(c: char) => Reason::NotIdentifier(c),
|
||||
unknown_escape(e: char) => Reason::UnknownEscape(e),
|
||||
invalid_escape(e: char) => Reason::InvalidEscape(e),
|
||||
invalid_digit(digit: char) => Reason::InvalidDigit(digit),
|
||||
unknown_base(base: char) => Reason::UnknownBase(base),
|
||||
bad_unicode(value: u32) => Reason::BadUnicode(value),
|
||||
end_of_file => Reason::EndOfFile,
|
||||
}
|
||||
impl Error {
|
||||
/// Changes the [Reason] of this error
|
||||
pub(super) fn mask_reason(self, reason: Reason) -> Self {
|
||||
Self { reason, ..self }
|
||||
}
|
||||
/// Returns the [Reason] for this error
|
||||
pub fn reason(&self) -> &Reason {
|
||||
&self.reason
|
||||
@@ -500,6 +549,14 @@ pub mod error {
|
||||
(self.line, self.col)
|
||||
}
|
||||
}
|
||||
macro error_impl ($($fn:ident$(( $($p:ident: $t:ty),* ))? => $reason:expr),*$(,)?) {
|
||||
#[allow(dead_code)]
|
||||
impl Error {
|
||||
$(pub(super) fn $fn ($($($p: $t),*,)? line: u32, col: u32) -> Self {
|
||||
Self { reason: $reason, line, col }
|
||||
})*
|
||||
}
|
||||
}
|
||||
impl std::error::Error for Error {}
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
@@ -509,12 +566,14 @@ pub mod error {
|
||||
impl Display for Reason {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Reason::UnmatchedDelimiters(c) => write! {f, "Unmatched `{c:?}` in input"},
|
||||
Reason::UnexpectedChar(c) => write!(f, "Character `{c:?}` not expected"),
|
||||
Reason::UnmatchedDelimiters(c) => write! {f, "Unmatched `{c}` in input"},
|
||||
Reason::UnexpectedChar(c) => write!(f, "Character `{c}` not expected"),
|
||||
Reason::NotIdentifier(c) => write!(f, "Character `{c}` not valid in identifiers"),
|
||||
Reason::UnknownEscape(c) => write!(f, "`\\{c}` is not a known escape sequence"),
|
||||
Reason::InvalidEscape(c) => write!(f, "Escape sequence `\\{c}`... is malformed"),
|
||||
Reason::InvalidDigit(c) => write!(f, "`{c:?}` is not a valid digit"),
|
||||
Reason::BadUnicode(c) => write!(f, "`\\u{{{c:x}}}` is not valid unicode"),
|
||||
Reason::InvalidDigit(c) => write!(f, "`{c}` is not a valid digit"),
|
||||
Reason::UnknownBase(c) => write!(f, "`0{c}`... is not a valid base"),
|
||||
Reason::BadUnicode(c) => write!(f, "`{c}` is not a valid unicode code-point"),
|
||||
Reason::EndOfFile => write!(f, "Reached end of input"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,43 +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(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::Tuple(TyTuple { types: vec![] }),
|
||||
gens: Generics { vars: vec![] },
|
||||
},
|
||||
args: Box::new(match types.len() {
|
||||
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![]);
|
||||
@@ -509,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)?
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -533,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)? })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -541,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))?,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -577,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))?,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -598,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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -619,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)? })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -639,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,
|
||||
})?
|
||||
}
|
||||
}
|
||||
@@ -674,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)?))
|
||||
@@ -701,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()) })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -719,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)?;
|
||||
@@ -730,28 +727,27 @@ 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
|
||||
}
|
||||
TokenKind::LParen => {
|
||||
let out = TyTuple::parse(p)?;
|
||||
TyKind::Tuple(out)
|
||||
}
|
||||
TokenKind::Fn => TyFn::parse(p)?.into(),
|
||||
path_like!() => {
|
||||
let path = Path::parse(p)?;
|
||||
if path.is_sinkhole() {
|
||||
TyKind::Infer
|
||||
} else {
|
||||
TyKind::Path(path)
|
||||
match out.types.is_empty() {
|
||||
true => TyKind::Empty,
|
||||
false => TyKind::Tuple(out),
|
||||
}
|
||||
}
|
||||
TokenKind::Fn => TyFn::parse(p)?.into(),
|
||||
path_like!() => Path::parse(p)?.into(),
|
||||
t => Err(p.error(Unexpected(t), P))?,
|
||||
};
|
||||
|
||||
@@ -763,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)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -780,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)? })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -799,24 +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(Ty {
|
||||
kind: TyKind::Tuple(TyTuple { types: args }),
|
||||
span,
|
||||
gens: Default::default(),
|
||||
}),
|
||||
rety: Box::new(match p.match_type(TokenKind::Arrow, Parsing::TyFn) {
|
||||
Ok(_) => Ty::parse(p)?,
|
||||
Err(_) => Ty {
|
||||
span,
|
||||
kind: TyKind::Tuple(TyTuple { types: vec![] }),
|
||||
gens: Generics { vars: vec![] },
|
||||
},
|
||||
args: Box::new(match args {
|
||||
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),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -858,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)),
|
||||
@@ -875,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()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -891,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(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -901,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)?
|
||||
@@ -947,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
|
||||
},
|
||||
@@ -994,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,
|
||||
},
|
||||
})
|
||||
@@ -1008,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(),
|
||||
})
|
||||
}
|
||||
@@ -1038,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)?
|
||||
})
|
||||
@@ -1062,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)?,
|
||||
})
|
||||
@@ -1070,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)?,
|
||||
})
|
||||
}
|
||||
@@ -1088,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),
|
||||
@@ -1112,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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1219,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 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1244,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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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 = { version = "*", registry = "soft-fish" }
|
||||
repline = { path = "../../repline" }
|
||||
argwerk = "0.20.4"
|
||||
|
||||
@@ -4,7 +4,7 @@ use cl_lexer::Lexer;
|
||||
use cl_token::Token;
|
||||
use std::{
|
||||
error::Error,
|
||||
io::{IsTerminal, Read, stdin},
|
||||
io::{stdin, IsTerminal, Read},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
|
||||
@@ -1,951 +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(),
|
||||
_ => 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::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::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::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
|
||||
}
|
||||
}
|
||||
@@ -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},
|
||||
};
|
||||
@@ -54,7 +55,7 @@ pub mod yamler {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn indent(&mut self) -> Section<'_> {
|
||||
pub fn indent(&mut self) -> Section {
|
||||
Section::new(self)
|
||||
}
|
||||
|
||||
@@ -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,34 +625,38 @@ 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 {
|
||||
fn yaml(&self, y: &mut Yamler) {
|
||||
match self {
|
||||
TyKind::Never => y.value("Never"),
|
||||
TyKind::Infer => y.value("_"),
|
||||
TyKind::Empty => y.value("Empty"),
|
||||
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!(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -667,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),
|
||||
};
|
||||
@@ -709,12 +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;
|
||||
@@ -738,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 () {
|
||||
@@ -754,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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,4 +11,4 @@ pub const RESET: &str = "\x1b[0m";
|
||||
pub const OUTPUT: &str = "\x1b[38;5;117m";
|
||||
|
||||
pub const CLEAR_LINES: &str = "\x1b[G\x1b[J";
|
||||
pub const CLEAR_ALL: &str = "\x1b[H\x1b[2J\x1b[3J";
|
||||
pub const CLEAR_ALL: &str = "\x1b[H\x1b[2J";
|
||||
|
||||
@@ -49,9 +49,10 @@ pub fn is_terminal() -> bool {
|
||||
/// The CLI's operating mode
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum Mode {
|
||||
#[default]
|
||||
Menu,
|
||||
Lex,
|
||||
Fmt,
|
||||
#[default]
|
||||
Run,
|
||||
}
|
||||
|
||||
|
||||
@@ -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>> {
|
||||
@@ -18,52 +18,25 @@ pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
|
||||
let mut env = Environment::new();
|
||||
|
||||
env.add_builtins(&builtins! {
|
||||
/// Lexes, parses, and evaluates an expression in the current env
|
||||
fn eval(string) @env {
|
||||
use cl_interpret::error::Error;
|
||||
let string = match string {
|
||||
ConValue::Str(string) => string.to_ref(),
|
||||
ConValue::String(string) => string.as_str(),
|
||||
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)).parse::<cl_ast::Stmt>() {
|
||||
Err(e) => Ok(ConValue::Str(format!("{e}").into())),
|
||||
Ok(v) => v.interpret(env),
|
||||
}
|
||||
}
|
||||
|
||||
/// Executes a file
|
||||
fn import(path) @env {
|
||||
use cl_interpret::error::Error;
|
||||
match path {
|
||||
ConValue::Str(path) => load_file(env, &**path).or(Ok(ConValue::Empty)),
|
||||
ConValue::String(path) => load_file(env, &**path).or(Ok(ConValue::Empty)),
|
||||
_ => Err(Error::TypeError())
|
||||
}
|
||||
}
|
||||
|
||||
fn putchar(ConValue::Char(c)) {
|
||||
print!("{c}");
|
||||
/// Clears the screen
|
||||
fn clear() {
|
||||
menu::clear();
|
||||
Ok(ConValue::Empty)
|
||||
}
|
||||
/// 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))
|
||||
}
|
||||
|
||||
/// Gets a line of input from stdin
|
||||
fn get_line(prompt) {
|
||||
use cl_interpret::error::Error;
|
||||
let prompt = match prompt {
|
||||
ConValue::Str(prompt) => prompt.to_ref(),
|
||||
ConValue::String(prompt) => prompt.as_str(),
|
||||
_ => Err(Error::TypeError())?,
|
||||
};
|
||||
match repline::Repline::new("", prompt, "").read() {
|
||||
Ok(line) => Ok(ConValue::String(line)),
|
||||
Err(repline::Error::CtrlD(line)) => Ok(ConValue::String(line)),
|
||||
Err(repline::Error::CtrlC(_)) => Err(cl_interpret::error::Error::Break(ConValue::Empty)),
|
||||
Err(e) => Ok(ConValue::Str(e.to_string().into())),
|
||||
fn get_line() {
|
||||
match repline::Repline::new("", "", "").read() {
|
||||
Ok(line) => Ok(ConValue::String(line.into())),
|
||||
Err(e) => Ok(ConValue::String(e.to_string().into())),
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -73,44 +46,36 @@ pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
|
||||
if repl {
|
||||
if let Some(file) = file
|
||||
&& let Err(e) = load_file(&mut env, file)
|
||||
{
|
||||
eprintln!("{e}")
|
||||
if let Some(file) = file {
|
||||
load_file(&mut env, file)?;
|
||||
}
|
||||
let mut ctx = Context::with_env(env);
|
||||
menu::main_menu(mode, &mut ctx)?;
|
||||
match mode {
|
||||
Mode::Menu => menu::main_menu(&mut ctx)?,
|
||||
Mode::Lex => menu::lex(&mut ctx)?,
|
||||
Mode::Fmt => menu::fmt(&mut ctx)?,
|
||||
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 => 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)) => {
|
||||
@@ -123,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),
|
||||
@@ -148,23 +104,22 @@ 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}"),
|
||||
}
|
||||
if env.get("main".into()).is_ok() {
|
||||
match env.call("main".into(), &[]) {
|
||||
Ok(ConValue::Empty) => {}
|
||||
Ok(ret) => println!("{ret}"),
|
||||
Err(e) => println!("Error: {e}"),
|
||||
match env.call("main".into(), &[])? {
|
||||
ConValue::Empty => {}
|
||||
ret => println!("{ret}"),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//! The Conlang REPL, based on [repline]
|
||||
//!
|
||||
//!
|
||||
//! Uses [argwerk] for argument parsing.
|
||||
#![warn(clippy::all)]
|
||||
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
use std::error::Error;
|
||||
|
||||
use crate::{ansi, args::Mode, ctx};
|
||||
use crate::{ansi, ctx};
|
||||
use cl_ast::Stmt;
|
||||
use cl_interpret::convalue::ConValue;
|
||||
use cl_lexer::Lexer;
|
||||
use cl_parser::Parser;
|
||||
use repline::{Error as RlError, error::ReplResult, prebaked::*};
|
||||
use repline::{error::ReplResult, prebaked::*};
|
||||
|
||||
pub fn clear() {
|
||||
print!("{}", ansi::CLEAR_ALL);
|
||||
@@ -16,111 +14,74 @@ pub fn banner() {
|
||||
println!("--- conlang v{} 💪🦈 ---", env!("CARGO_PKG_VERSION"))
|
||||
}
|
||||
|
||||
type ReplCallback = fn(&mut ctx::Context, &str) -> Result<Response, Box<dyn Error>>;
|
||||
type ReplMode = (&'static str, &'static str, &'static str, ReplCallback);
|
||||
|
||||
#[rustfmt::skip]
|
||||
const MODES: &[ReplMode] = &[
|
||||
(ansi::CYAN, ".>", " >", mode_run),
|
||||
(ansi::BRIGHT_BLUE, "l>", " >", mode_lex),
|
||||
(ansi::BRIGHT_MAGENTA, "f>", " >", mode_fmt),
|
||||
];
|
||||
|
||||
const fn get_mode(mode: Mode) -> ReplMode {
|
||||
match mode {
|
||||
Mode::Lex => MODES[1],
|
||||
Mode::Fmt => MODES[2],
|
||||
Mode::Run => MODES[0],
|
||||
}
|
||||
}
|
||||
|
||||
/// Presents a selection interface to the user
|
||||
pub fn main_menu(mode: Mode, ctx: &mut ctx::Context) -> ReplResult<()> {
|
||||
let mut mode = get_mode(mode);
|
||||
pub fn main_menu(ctx: &mut ctx::Context) -> ReplResult<()> {
|
||||
banner();
|
||||
|
||||
let mut rl = repline::Repline::new(mode.0, mode.1, mode.2);
|
||||
loop {
|
||||
rl.set_prompt(mode.0, mode.1, mode.2);
|
||||
|
||||
let line = match rl.read() {
|
||||
Err(RlError::CtrlC(_)) => return Ok(()),
|
||||
Err(RlError::CtrlD(line)) => {
|
||||
rl.deny();
|
||||
line
|
||||
}
|
||||
Ok(line) => line,
|
||||
Err(e) => Err(e)?,
|
||||
};
|
||||
print!("\x1b[G\x1b[J");
|
||||
run(ctx)?;
|
||||
read_and(ansi::GREEN, "mu>", " ?>", |line| {
|
||||
match line.trim() {
|
||||
"" => continue,
|
||||
"clear" => clear(),
|
||||
"mode run" => mode = get_mode(Mode::Run),
|
||||
"mode lex" => mode = get_mode(Mode::Lex),
|
||||
"mode fmt" => mode = get_mode(Mode::Fmt),
|
||||
"quit" => return Ok(()),
|
||||
"help" => println!(
|
||||
"l" | "lex" => lex(ctx)?,
|
||||
"f" | "fmt" => fmt(ctx)?,
|
||||
"r" | "run" => run(ctx)?,
|
||||
"q" | "quit" => return Ok(Response::Break),
|
||||
"h" | "help" => println!(
|
||||
"Valid commands
|
||||
help : Print this list
|
||||
clear : Clear the screen
|
||||
quit : Exit the program
|
||||
mode lex : Lex the input
|
||||
mode fmt : Format the input
|
||||
mode run : Evaluate some expressions"
|
||||
lex (l): Spin up a lexer, and lex some lines
|
||||
fmt (f): Format the input
|
||||
run (r): Enter the REPL, and evaluate some statements
|
||||
help (h): Print this list
|
||||
quit (q): Exit the program"
|
||||
),
|
||||
_ => match mode.3(ctx, &line) {
|
||||
Ok(Response::Accept) => {
|
||||
rl.accept();
|
||||
continue;
|
||||
}
|
||||
Ok(Response::Deny) => {}
|
||||
Ok(Response::Break) => return Ok(()),
|
||||
Ok(Response::Continue) => continue,
|
||||
Err(e) => rl.print_inline(format_args!("\t\x1b[91m{e}\x1b[0m"))?,
|
||||
},
|
||||
_ => Err("Unknown command. Type \"help\" for help")?,
|
||||
}
|
||||
rl.deny();
|
||||
}
|
||||
Ok(Response::Accept)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn mode_run(ctx: &mut ctx::Context, line: &str) -> Result<Response, Box<dyn Error>> {
|
||||
pub fn run(ctx: &mut ctx::Context) -> ReplResult<()> {
|
||||
use cl_ast::ast_visitor::Fold;
|
||||
use cl_parser::inliner::ModuleInliner;
|
||||
|
||||
if line.trim().is_empty() {
|
||||
return Ok(Response::Deny);
|
||||
}
|
||||
let code = Parser::new("", Lexer::new(line)).parse::<Stmt>()?;
|
||||
let code = ModuleInliner::new(".").fold_stmt(code);
|
||||
|
||||
print!("{}", ansi::OUTPUT);
|
||||
match ctx.run(&code) {
|
||||
Ok(ConValue::Empty) => print!("{}", ansi::RESET),
|
||||
Ok(v) => println!("{}{v}", ansi::RESET),
|
||||
Err(e) => println!("{}! > {e}{}", ansi::RED, ansi::RESET),
|
||||
}
|
||||
Ok(Response::Accept)
|
||||
}
|
||||
|
||||
pub fn mode_lex(_ctx: &mut ctx::Context, line: &str) -> Result<Response, Box<dyn Error>> {
|
||||
for token in Lexer::new(line) {
|
||||
match token {
|
||||
Ok(token) => crate::tools::print_token(&token),
|
||||
Err(e) => eprintln!("! > {}{e}{}", ansi::RED, ansi::RESET),
|
||||
read_and(ansi::CYAN, "cl>", " ?>", |line| {
|
||||
if line.trim().is_empty() {
|
||||
return Ok(Response::Deny);
|
||||
}
|
||||
}
|
||||
let code = Parser::new(Lexer::new(line)).parse::<Stmt>()?;
|
||||
let code = ModuleInliner::new(".").fold_stmt(code);
|
||||
|
||||
Ok(Response::Accept)
|
||||
print!("{}", ansi::OUTPUT);
|
||||
match ctx.run(&code) {
|
||||
Ok(ConValue::Empty) => print!("{}", ansi::RESET),
|
||||
Ok(v) => println!("{}{v}", ansi::RESET),
|
||||
Err(e) => println!("{}! > {e}{}", ansi::RED, ansi::RESET),
|
||||
}
|
||||
Ok(Response::Accept)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn mode_fmt(_ctx: &mut ctx::Context, line: &str) -> Result<Response, Box<dyn Error>> {
|
||||
let mut p = Parser::new("", Lexer::new(line));
|
||||
pub fn lex(_ctx: &mut ctx::Context) -> ReplResult<()> {
|
||||
read_and(ansi::BRIGHT_BLUE, "lx>", " ?>", |line| {
|
||||
for token in Lexer::new(line) {
|
||||
match token {
|
||||
Ok(token) => crate::tools::print_token(&token),
|
||||
Err(e) => eprintln!("! > {}{e}{}", ansi::RED, ansi::RESET),
|
||||
}
|
||||
}
|
||||
|
||||
match p.parse::<Stmt>() {
|
||||
Ok(code) => println!("{}{code}{}", ansi::OUTPUT, ansi::RESET),
|
||||
Err(e) => Err(e)?,
|
||||
}
|
||||
|
||||
Ok(Response::Accept)
|
||||
Ok(Response::Accept)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn fmt(_ctx: &mut ctx::Context) -> ReplResult<()> {
|
||||
read_and(ansi::BRIGHT_MAGENTA, "cl>", " ?>", |line| {
|
||||
let mut p = Parser::new(Lexer::new(line));
|
||||
|
||||
match p.parse::<Stmt>() {
|
||||
Ok(code) => println!("{}{code}{}", ansi::OUTPUT, ansi::RESET),
|
||||
Err(e) => Err(e)?,
|
||||
}
|
||||
|
||||
Ok(Response::Accept)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -37,15 +37,16 @@ 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
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + Debug> Debug for Interned<'_, T> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "~")?;
|
||||
self.value.fmt(f)
|
||||
f.debug_struct("Interned")
|
||||
.field("value", &self.value)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
impl<'a, T: ?Sized> Interned<'a, T> {
|
||||
@@ -263,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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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}:")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(())?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -12,6 +12,6 @@ cl-ast = { path = "../cl-ast" }
|
||||
cl-structures = { path = "../cl-structures" }
|
||||
|
||||
[dev-dependencies]
|
||||
repline = { version = "*", registry = "soft-fish" }
|
||||
repline = { path = "../../repline" }
|
||||
cl-lexer = { path = "../cl-lexer" }
|
||||
cl-parser = { path = "../cl-parser" }
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -205,8 +181,4 @@ impl<'t, 'a> EntryMut<'t, 'a> {
|
||||
pub fn mark_impl_item(&mut self) {
|
||||
self.table.mark_impl_item(self.id)
|
||||
}
|
||||
|
||||
pub fn mark_lang_item(&mut self, lang_item: &'static str) {
|
||||
self.table.mark_lang_item(lang_item, self.id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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("[", "]"))
|
||||
}
|
||||
@@ -55,17 +48,12 @@ impl fmt::Display for Entry<'_, '_> {
|
||||
write!(f, "fn {} -> ", self.with_id(*args))?;
|
||||
write_name_or(self.with_id(*rety), f)
|
||||
}
|
||||
TypeKind::Empty => write!(f, "()"),
|
||||
TypeKind::Never => write!(f, "!"),
|
||||
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}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,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 {", "}"))
|
||||
}
|
||||
@@ -84,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"),
|
||||
|
||||
@@ -2,7 +2,10 @@ pub use cl_ast::format::*;
|
||||
use std::{fmt, iter};
|
||||
|
||||
/// Separates the items yielded by iterating the provided function
|
||||
pub const fn separate<'f, 's, Item, F, W>(sep: &'s str, t: F) -> impl FnOnce(W) -> fmt::Result + 's
|
||||
pub const fn separate<'f, 's, Item, F, W>(
|
||||
sep: &'s str,
|
||||
t: F,
|
||||
) -> impl FnOnce(W) -> fmt::Result + 's
|
||||
where
|
||||
Item: FnMut(&mut W) -> fmt::Result,
|
||||
F: FnMut() -> Option<Item> + 's,
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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,68 @@ 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::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::Alias(_) => {table.mark_unchecked(node)},
|
||||
_ => return Ok(()),
|
||||
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),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parent(table: &Table, node: Handle) -> Handle {
|
||||
table.parent(node).copied().unwrap_or(node)
|
||||
}
|
||||
|
||||
fn cat_alias(table: &mut Table, node: Handle, a: &Alias) -> CatResult<()> {
|
||||
let parent = parent(table, node);
|
||||
let kind = match &a.from {
|
||||
Some(ty) => TypeKind::Instance(
|
||||
ty.evaluate(table, parent)
|
||||
.map_err(|e| Error::TypeEval(e, " while categorizing an alias"))?,
|
||||
),
|
||||
None => TypeKind::Empty,
|
||||
};
|
||||
table.set_ty(node, kind);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
@@ -68,44 +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;
|
||||
// table.set_ty(node, kind);
|
||||
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);
|
||||
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) {
|
||||
(StructKind::Empty) => Ok(()),
|
||||
(StructKind::Empty) => Ok(()),
|
||||
(StructKind::Tuple(ty)) => {
|
||||
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(())
|
||||
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::Struct(members)) => {
|
||||
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(())
|
||||
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())))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,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);
|
||||
@@ -153,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),
|
||||
@@ -168,6 +206,7 @@ type CatResult<T> = Result<T, Error>;
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Error {
|
||||
BadMeta(Meta),
|
||||
Recursive(Handle),
|
||||
TypeEval(TypeEval, &'static str),
|
||||
}
|
||||
|
||||
@@ -180,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}"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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!"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,621 +0,0 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
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<'h> = Option<&'h mut Option<Handle>>;
|
||||
|
||||
pub struct InferenceEngine<'table, 'a, 'b, 'r> {
|
||||
pub(super) table: &'table mut Table<'a>,
|
||||
/// The current working node
|
||||
pub(crate) at: Handle,
|
||||
/// The current breakset
|
||||
pub(crate) bset: HandleSet<'b>,
|
||||
/// The current returnset
|
||||
pub(crate) rset: HandleSet<'r>,
|
||||
}
|
||||
|
||||
impl<'table, 'a, 'b, 'r> InferenceEngine<'table, 'a, 'b, 'r> {
|
||||
/// 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.as_deref_mut(),
|
||||
rset: self.rset.as_deref_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
),
|
||||
Err(InferenceError::NoBreak | InferenceError::NoReturn) => {}
|
||||
}
|
||||
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<'ob>(
|
||||
&mut self,
|
||||
bset: &'ob mut Option<Handle>,
|
||||
) -> InferenceEngine<'_, 'a, 'ob, '_> {
|
||||
InferenceEngine { bset: Some(bset), ..self.scoped() }
|
||||
}
|
||||
|
||||
pub fn open_rset<'or>(
|
||||
&mut self,
|
||||
rset: &'or mut Option<Handle>,
|
||||
) -> InferenceEngine<'_, 'a, '_, 'or> {
|
||||
InferenceEngine { rset: Some(rset), ..self.scoped() }
|
||||
}
|
||||
|
||||
pub fn bset(&mut self, ty: Handle) -> Result<(), InferenceError> {
|
||||
match self.bset.as_mut() {
|
||||
Some(&mut &mut Some(bset)) => self.unify(ty, bset),
|
||||
Some(none) => {
|
||||
let _ = none.insert(ty);
|
||||
Ok(())
|
||||
}
|
||||
None => Err(InferenceError::NoBreak),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rset(&mut self, ty: Handle) -> Result<(), InferenceError> {
|
||||
match self.rset.as_mut() {
|
||||
Some(&mut &mut Some(rset)) => self.unify(ty, rset),
|
||||
Some(none) => {
|
||||
let _ = none.insert(ty);
|
||||
Ok(())
|
||||
}
|
||||
None => Err(InferenceError::NoReturn),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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: &'static str) -> Handle {
|
||||
// TODO: keep a map of primitives in the table root
|
||||
self.table.get_lang_item(name)
|
||||
}
|
||||
|
||||
pub fn never(&mut self) -> Handle {
|
||||
self.table.get_lang_item("never")
|
||||
}
|
||||
|
||||
pub fn empty(&mut self) -> Handle {
|
||||
self.table.anon_type(TypeKind::Tuple(vec![]))
|
||||
}
|
||||
|
||||
pub fn bool(&self) -> Handle {
|
||||
self.primitive("bool")
|
||||
}
|
||||
|
||||
pub fn char(&self) -> Handle {
|
||||
self.primitive("char")
|
||||
}
|
||||
|
||||
pub fn str(&self) -> Handle {
|
||||
self.primitive("str")
|
||||
}
|
||||
|
||||
pub fn u32(&self) -> Handle {
|
||||
self.primitive("u32")
|
||||
}
|
||||
|
||||
pub fn usize(&self) -> Handle {
|
||||
self.primitive("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::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(tykind) = entry.ty().cloned() else {
|
||||
return ty;
|
||||
};
|
||||
|
||||
// TODO: Parent the deep clone into a new "monomorphs" branch of tree
|
||||
match tykind {
|
||||
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::Ptr(handle) => {
|
||||
let ty = self.deep_clone(handle);
|
||||
self.table.anon_type(TypeKind::Ptr(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 })
|
||||
}
|
||||
_ => 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(_) => false,
|
||||
TypeKind::Ptr(_) => false,
|
||||
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::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::Variable, TypeKind::Variable) => {
|
||||
self.set_instance(ah, bh);
|
||||
Ok(())
|
||||
}
|
||||
(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::Primitive(Primitive::Never), _)
|
||||
| (_, TypeKind::Primitive(Primitive::Never)) => Ok(()),
|
||||
(a, b) if a == b => Ok(()),
|
||||
_ => Err(InferenceError::Mismatch(ah, bh)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,43 +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),
|
||||
NoBreak,
|
||||
NoReturn,
|
||||
}
|
||||
|
||||
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!"),
|
||||
InferenceError::NoBreak => write!(f, "Encountered break outside loop!"),
|
||||
InferenceError::NoReturn => write!(f, "Encountered return outside function!"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::type_expression::Error> for InferenceError {
|
||||
fn from(value: crate::type_expression::Error) -> Self {
|
||||
Self::AnnotationEval(value)
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -4,12 +4,8 @@ use crate::{
|
||||
handle::Handle,
|
||||
source::Source,
|
||||
table::{NodeKind, Table},
|
||||
type_kind::TypeKind,
|
||||
};
|
||||
use cl_ast::{
|
||||
ItemKind, Literal, Meta, MetaKind, 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,133 +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);
|
||||
|
||||
for Meta { name, kind } in &attrs.meta {
|
||||
if let ("lang", MetaKind::Equals(Literal::String(s))) = (name.to_ref(), kind) {
|
||||
if let Ok(prim) = s.parse() {
|
||||
entry.inner.set_ty(TypeKind::Primitive(prim));
|
||||
}
|
||||
entry.inner.mark_lang_item(Sym::from(s).to_ref());
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
self.inner.set_source(Source::Function(f));
|
||||
self.set_name(f.name);
|
||||
|
||||
if let Some(body) = &f.body {
|
||||
self.inner.set_body(body);
|
||||
}
|
||||
|
||||
f.children(self);
|
||||
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, kind } = m;
|
||||
self.inner.set_source(Source::Module(m));
|
||||
self.set_name(m.name);
|
||||
self.set_name(*name);
|
||||
|
||||
m.children(self);
|
||||
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, kind } = s;
|
||||
self.inner.set_source(Source::Struct(s));
|
||||
self.set_name(s.name);
|
||||
self.set_name(*name);
|
||||
|
||||
s.children(self);
|
||||
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);
|
||||
match (kind, body) {
|
||||
(cl_ast::StructKind::Empty, None) => {
|
||||
self.inner.set_ty(TypeKind::Inferred);
|
||||
}
|
||||
(cl_ast::StructKind::Empty, Some(body)) => {
|
||||
self.inner.set_body(body);
|
||||
}
|
||||
(cl_ast::StructKind::Tuple(_items), None) => {}
|
||||
(cl_ast::StructKind::Struct(_struct_members), None) => {}
|
||||
(_, Some(body)) => panic!("Unexpected body {body} in enum variant `{value}`"),
|
||||
}
|
||||
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.
|
||||
|
||||
self.visit(body);
|
||||
self.visit_impl_kind(target);
|
||||
self.visit_file(body);
|
||||
}
|
||||
|
||||
fn visit_use(&mut self, u: &'a cl_ast::Use) {
|
||||
@@ -188,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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<&'static str, 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,18 +120,7 @@ impl<'a> Table<'a> {
|
||||
self.impls.push(item);
|
||||
}
|
||||
|
||||
pub fn mark_lang_item(&mut self, name: &'static str, item: Handle) {
|
||||
self.lang_items.insert(name, item);
|
||||
}
|
||||
|
||||
pub fn get_lang_item(&self, name: &str) -> Handle {
|
||||
match self.lang_items.get(name).copied() {
|
||||
Some(handle) => handle,
|
||||
None => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_iter(&self) -> impl Iterator<Item = Handle> + use<> {
|
||||
pub fn handle_iter(&mut self) -> impl Iterator<Item = Handle> {
|
||||
self.kinds.keys()
|
||||
}
|
||||
|
||||
@@ -162,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)
|
||||
}
|
||||
@@ -204,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)
|
||||
}
|
||||
@@ -228,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)
|
||||
}
|
||||
@@ -269,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())
|
||||
}
|
||||
@@ -312,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),
|
||||
@@ -334,9 +282,7 @@ pub enum NodeKind {
|
||||
Const,
|
||||
Static,
|
||||
Function,
|
||||
Temporary,
|
||||
Let,
|
||||
Scope,
|
||||
Local,
|
||||
Impl,
|
||||
Use,
|
||||
}
|
||||
@@ -353,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"),
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
@@ -40,14 +40,13 @@ impl TypeExpression for Ty {
|
||||
impl TypeExpression for TyKind {
|
||||
fn evaluate(&self, table: &mut Table, node: Handle) -> Result<Handle, Error> {
|
||||
match self {
|
||||
TyKind::Never => Ok(table.get_lang_item("never")),
|
||||
TyKind::Infer => Ok(table.inferred_type()),
|
||||
TyKind::Never => Ok(table.anon_type(TypeKind::Never)),
|
||||
TyKind::Empty => Ok(table.anon_type(TypeKind::Empty)),
|
||||
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),
|
||||
}
|
||||
}
|
||||
@@ -68,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;
|
||||
@@ -96,7 +86,10 @@ impl TypeExpression for TySlice {
|
||||
impl TypeExpression for TyTuple {
|
||||
fn evaluate(&self, table: &mut Table, node: Handle) -> Result<Handle, Error> {
|
||||
let Self { types } = self;
|
||||
let kind = TypeKind::Tuple(types.evaluate(table, node)?);
|
||||
let kind = match types.len() {
|
||||
0 => TypeKind::Empty,
|
||||
_ => TypeKind::Tuple(types.evaluate(table, node)?),
|
||||
};
|
||||
Ok(table.anon_type(kind))
|
||||
}
|
||||
}
|
||||
@@ -113,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))
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -32,6 +26,10 @@ pub enum TypeKind {
|
||||
Tuple(Vec<Handle>),
|
||||
/// A function which accepts multiple inputs and produces an output
|
||||
FnSig { args: Handle, rety: Handle },
|
||||
/// The unit type
|
||||
Empty,
|
||||
/// The never type
|
||||
Never,
|
||||
/// An untyped module
|
||||
Module,
|
||||
}
|
||||
@@ -40,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)>),
|
||||
@@ -57,68 +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
|
||||
Never, // The never type
|
||||
}
|
||||
|
||||
#[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,
|
||||
"never" => Primitive::Never,
|
||||
"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(())?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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) => {
|
||||
@@ -25,6 +22,8 @@ impl Display for TypeKind {
|
||||
})(f.delimit_with("tuple (", ")"))
|
||||
}
|
||||
TypeKind::FnSig { args, rety } => write!(f, "fn (#{args}) -> #{rety}"),
|
||||
TypeKind::Empty => f.write_str("()"),
|
||||
TypeKind::Never => f.write_str("!"),
|
||||
TypeKind::Module => f.write_str("mod"),
|
||||
}
|
||||
}
|
||||
@@ -37,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) => {
|
||||
@@ -66,33 +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"),
|
||||
Primitive::Never => f.write_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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
13
grammar.ebnf
13
grammar.ebnf
@@ -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 ;
|
||||
|
||||
11
repline/Cargo.toml
Normal file
11
repline/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "repline"
|
||||
repository.workspace = true
|
||||
version.workspace = true
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
publish.workspace = true
|
||||
|
||||
[dependencies]
|
||||
crossterm = { version = "0.27.0", default-features = false }
|
||||
17
repline/examples/repl_float.rs
Normal file
17
repline/examples/repl_float.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
//! 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::{prebaked::read_and, Response};
|
||||
use std::{error::Error, str::FromStr};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
read_and("\x1b[33m", " >", " ?>", |line| {
|
||||
println!("-> {:?}", f64::from_str(line.trim())?);
|
||||
Ok(Response::Accept)
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
324
repline/src/editor.rs
Normal file
324
repline/src/editor.rs
Normal file
@@ -0,0 +1,324 @@
|
||||
//! The [Editor] is a multi-line buffer of [`char`]s which operates on an ANSI-compatible terminal.
|
||||
|
||||
use crossterm::{cursor::*, execute, queue, style::*, terminal::*};
|
||||
use std::{collections::VecDeque, fmt::Display, io::Write};
|
||||
|
||||
use super::error::{Error, ReplResult};
|
||||
|
||||
fn is_newline(c: &char) -> bool {
|
||||
*c == '\n'
|
||||
}
|
||||
|
||||
fn write_chars<'a, W: Write>(
|
||||
c: impl IntoIterator<Item = &'a char>,
|
||||
w: &mut W,
|
||||
) -> std::io::Result<()> {
|
||||
for c in c {
|
||||
write!(w, "{c}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A multi-line editor which operates on an un-cleared ANSI terminal.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Editor<'a> {
|
||||
head: VecDeque<char>,
|
||||
tail: VecDeque<char>,
|
||||
|
||||
pub color: &'a str,
|
||||
begin: &'a str,
|
||||
again: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> Editor<'a> {
|
||||
/// Constructs a new Editor with the provided prompt color, begin prompt, and again prompt.
|
||||
pub fn new(color: &'a str, begin: &'a str, again: &'a str) -> Self {
|
||||
Self { head: Default::default(), tail: Default::default(), color, begin, again }
|
||||
}
|
||||
|
||||
/// Returns an iterator over characters in the editor.
|
||||
pub fn iter(&self) -> impl Iterator<Item = &char> {
|
||||
let Self { head, tail, .. } = self;
|
||||
head.iter().chain(tail.iter())
|
||||
}
|
||||
|
||||
/// 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 => write!(w, "\x1b[0G"),
|
||||
lines => write!(w, "\x1b[{}F", lines),
|
||||
}?;
|
||||
queue!(w, Clear(ClearType::FromCursorDown))?;
|
||||
// write!(w, "\x1b[0J")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 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()),
|
||||
}?
|
||||
}
|
||||
// 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<()> {
|
||||
self.prompt(w)?;
|
||||
write_chars(
|
||||
self.head.iter().skip(
|
||||
self.head
|
||||
.iter()
|
||||
.rposition(is_newline)
|
||||
.unwrap_or(self.head.len())
|
||||
+ 1,
|
||||
),
|
||||
w,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Prints the characters after the cursor on the current line.
|
||||
pub fn print_tail<W: Write>(&self, w: &mut W) -> ReplResult<()> {
|
||||
let Self { tail, .. } = self;
|
||||
queue!(w, SavePosition, Clear(ClearType::UntilNewLine))?;
|
||||
write_chars(tail.iter().take_while(|&c| !is_newline(c)), w)?;
|
||||
queue!(w, 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<()> {
|
||||
// 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 {
|
||||
Some('\n') => self.redraw(w)?,
|
||||
Some(_) => {
|
||||
// go back a char
|
||||
queue!(w, MoveLeft(1), Print(' '), MoveLeft(1))?;
|
||||
self.print_tail(w)?;
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
/// Writes characters into the editor at the location of the cursor.
|
||||
pub fn extend<T: IntoIterator<Item = char>, W: Write>(
|
||||
&mut self,
|
||||
iter: T,
|
||||
w: &mut W,
|
||||
) -> ReplResult<()> {
|
||||
for c in iter {
|
||||
self.push(c, w)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sets the editor to the contents of a string, placing the cursor at the end.
|
||||
pub fn restore(&mut self, s: &str) {
|
||||
self.clear();
|
||||
self.head.extend(s.chars())
|
||||
}
|
||||
|
||||
/// Clears the editor, removing all characters.
|
||||
pub fn clear(&mut self) {
|
||||
self.head.clear();
|
||||
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<()> {
|
||||
while self.pop(w)?.filter(|c| !c.is_whitespace()).is_some() {}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the number of characters in the buffer
|
||||
pub fn len(&self) -> usize {
|
||||
self.head.len() + self.tail.len()
|
||||
}
|
||||
|
||||
/// Returns true if the buffer is empty.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.head.is_empty() && self.tail.is_empty()
|
||||
}
|
||||
|
||||
/// Returns true if the buffer ends with a given pattern
|
||||
pub fn ends_with(&self, iter: impl DoubleEndedIterator<Item = char>) -> bool {
|
||||
let mut iter = iter.rev();
|
||||
let mut head = self.head.iter().rev();
|
||||
loop {
|
||||
match (iter.next(), head.next()) {
|
||||
(None, _) => break true,
|
||||
(Some(_), None) => break false,
|
||||
(Some(a), Some(b)) if a != *b => break false,
|
||||
(Some(_), Some(_)) => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves the cursor back `steps` steps
|
||||
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)?;
|
||||
}
|
||||
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))?,
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 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 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)?,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves the cursor to the end of the current line
|
||||
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)?,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'e> IntoIterator for &'e Editor<'_> {
|
||||
type Item = &'e char;
|
||||
type IntoIter = std::iter::Chain<
|
||||
std::collections::vec_deque::Iter<'e, char>,
|
||||
std::collections::vec_deque::Iter<'e, char>,
|
||||
>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.head.iter().chain(self.tail.iter())
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Editor<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
use std::fmt::Write;
|
||||
for c in self.iter() {
|
||||
f.write_char(*c)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
42
repline/src/error.rs
Normal file
42
repline/src/error.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use crate::iter::chars::BadUnicode;
|
||||
|
||||
/// Result type for Repline
|
||||
pub type ReplResult<T> = std::result::Result<T, Error>;
|
||||
/// Borrowed error (does not implement [Error](std::error::Error)!)
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
/// User broke with Ctrl+C
|
||||
CtrlC(String),
|
||||
/// User broke with Ctrl+D
|
||||
CtrlD(String),
|
||||
/// Invalid unicode codepoint
|
||||
BadUnicode(u32),
|
||||
/// Error came from [std::io]
|
||||
IoFailure(std::io::Error),
|
||||
/// End of input
|
||||
EndOfInput,
|
||||
}
|
||||
|
||||
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 {
|
||||
Error::CtrlC(_) => write!(f, "Ctrl+C"),
|
||||
Error::CtrlD(_) => write!(f, "Ctrl+D"),
|
||||
Error::BadUnicode(u) => write!(f, "\\u{{{u:x}}} is not a valid unicode codepoint"),
|
||||
Error::IoFailure(s) => write!(f, "{s}"),
|
||||
Error::EndOfInput => write!(f, "End of input"),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(value: std::io::Error) -> Self {
|
||||
Self::IoFailure(value)
|
||||
}
|
||||
}
|
||||
impl From<BadUnicode> for Error {
|
||||
fn from(value: BadUnicode) -> Self {
|
||||
let BadUnicode(code) = value;
|
||||
Self::BadUnicode(code)
|
||||
}
|
||||
}
|
||||
68
repline/src/iter.rs
Normal file
68
repline/src/iter.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
//! Shmancy iterator adapters
|
||||
|
||||
pub use chars::Chars;
|
||||
pub use flatten::Flatten;
|
||||
|
||||
pub mod chars {
|
||||
//! Converts an <code>[Iterator]<Item = [u8]></code> into an
|
||||
//! <code>[Iterator]<Item = [Result]<[char], [BadUnicode]>></code>
|
||||
|
||||
/// Invalid unicode codepoint found when iterating over [Chars]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct BadUnicode(pub u32);
|
||||
impl std::error::Error for BadUnicode {}
|
||||
impl std::fmt::Display for BadUnicode {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let Self(code) = self;
|
||||
write!(f, "Bad unicode: {code}")
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts an <code>[Iterator]<Item = [u8]></code> into an
|
||||
/// <code>[Iterator]<Item = [char]></code>
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Chars<I: Iterator<Item = u8>>(pub I);
|
||||
impl<I: Iterator<Item = u8>> Iterator for Chars<I> {
|
||||
type Item = Result<char, BadUnicode>;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let Self(bytes) = self;
|
||||
let start = bytes.next()? as u32;
|
||||
let (mut out, count) = match start {
|
||||
start if start & 0x80 == 0x00 => (start, 0), // ASCII valid range
|
||||
start if start & 0xe0 == 0xc0 => (start & 0x1f, 1), // 1 continuation byte
|
||||
start if start & 0xf0 == 0xe0 => (start & 0x0f, 2), // 2 continuation bytes
|
||||
start if start & 0xf8 == 0xf0 => (start & 0x07, 3), // 3 continuation bytes
|
||||
_ => return None,
|
||||
};
|
||||
for _ in 0..count {
|
||||
let cont = bytes.next()? as u32;
|
||||
if cont & 0xc0 != 0x80 {
|
||||
return None;
|
||||
}
|
||||
out = (out << 6) | (cont & 0x3f);
|
||||
}
|
||||
Some(char::from_u32(out).ok_or(BadUnicode(out)))
|
||||
}
|
||||
}
|
||||
}
|
||||
pub mod flatten {
|
||||
//! Flattens an [Iterator] returning [`Result<T, E>`](Result) or [`Option<T>`](Option)
|
||||
//! into a *non-[FusedIterator](std::iter::FusedIterator)* over `T`
|
||||
|
||||
/// Flattens an [Iterator] returning [`Result<T, E>`](Result) or [`Option<T>`](Option)
|
||||
/// into a *non-[FusedIterator](std::iter::FusedIterator)* over `T`
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Flatten<T, I: Iterator<Item = T>>(pub I);
|
||||
impl<T, E, I: Iterator<Item = Result<T, E>>> Iterator for Flatten<Result<T, E>, I> {
|
||||
type Item = T;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.0.next()?.ok()
|
||||
}
|
||||
}
|
||||
impl<T, I: Iterator<Item = Option<T>>> Iterator for Flatten<Option<T>, I> {
|
||||
type Item = T;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.0.next()?
|
||||
}
|
||||
}
|
||||
}
|
||||
13
repline/src/lib.rs
Normal file
13
repline/src/lib.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
//! A small pseudo-multiline editing library
|
||||
|
||||
mod editor;
|
||||
mod iter;
|
||||
mod raw;
|
||||
|
||||
pub mod error;
|
||||
pub mod prebaked;
|
||||
pub mod repline;
|
||||
|
||||
pub use error::Error;
|
||||
pub use prebaked::{read_and, Response};
|
||||
pub use repline::Repline;
|
||||
56
repline/src/prebaked.rs
Normal file
56
repline/src/prebaked.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
//! Here's a menu I prepared earlier!
|
||||
//!
|
||||
//! Constructs a [Repline] and repeatedly runs the provided closure on the input strings,
|
||||
//! obeying the closure's [Response].
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
use crate::{error::Error as RlError, repline::Repline};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
/// Control codes for the [prebaked menu](read_and)
|
||||
pub enum Response {
|
||||
/// Accept the line, and save it to history
|
||||
Accept,
|
||||
/// Reject the line, and clear the buffer
|
||||
Deny,
|
||||
/// End the loop
|
||||
Break,
|
||||
/// Gather more input and try again
|
||||
Continue,
|
||||
}
|
||||
|
||||
/// Implements a basic menu loop using an embedded [Repline].
|
||||
///
|
||||
/// Repeatedly runs the provided closure on the input strings,
|
||||
/// obeying the closure's [Response].
|
||||
///
|
||||
/// Captures and displays all user [Error]s.
|
||||
///
|
||||
/// # Keybinds
|
||||
/// - `Ctrl+C` exits the loop
|
||||
/// - `Ctrl+D` clears the input, but *runs the closure* with the old input
|
||||
pub fn read_and<F>(color: &str, begin: &str, again: &str, mut f: F) -> Result<(), RlError>
|
||||
where F: FnMut(&str) -> Result<Response, Box<dyn Error>> {
|
||||
let mut rl = Repline::new(color, begin, again);
|
||||
loop {
|
||||
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(())
|
||||
}
|
||||
21
repline/src/raw.rs
Normal file
21
repline/src/raw.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
//! Sets the terminal to [`raw`] mode for the duration of the returned object's lifetime.
|
||||
|
||||
/// Sets the terminal to raw mode for the duration of the returned object's lifetime.
|
||||
pub fn raw() -> impl Drop {
|
||||
Raw::default()
|
||||
}
|
||||
struct Raw();
|
||||
impl Default for Raw {
|
||||
fn default() -> Self {
|
||||
std::thread::yield_now();
|
||||
crossterm::terminal::enable_raw_mode()
|
||||
.expect("should be able to transition into raw mode");
|
||||
Raw()
|
||||
}
|
||||
}
|
||||
impl Drop for Raw {
|
||||
fn drop(&mut self) {
|
||||
crossterm::terminal::disable_raw_mode()
|
||||
.expect("should be able to transition out of raw mode");
|
||||
}
|
||||
}
|
||||
175
repline/src/repline.rs
Normal file
175
repline/src/repline.rs
Normal file
@@ -0,0 +1,175 @@
|
||||
//! 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.
|
||||
|
||||
use crate::{editor::Editor, error::*, iter::*, raw::raw};
|
||||
use std::{
|
||||
collections::VecDeque,
|
||||
io::{stdout, Bytes, Read, Result, Write},
|
||||
};
|
||||
|
||||
/// Prompts the user, reads the lines. Not much more to it than that.
|
||||
#[derive(Debug)]
|
||||
pub struct Repline<'a, R: Read> {
|
||||
input: Chars<Flatten<Result<u8>, Bytes<R>>>,
|
||||
|
||||
history: VecDeque<String>, // previous lines
|
||||
hindex: usize, // current index into the history buffer
|
||||
|
||||
ed: Editor<'a>, // the current line buffer
|
||||
}
|
||||
|
||||
impl<'a> Repline<'a, std::io::Stdin> {
|
||||
pub fn new(color: &'a str, begin: &'a str, again: &'a str) -> Self {
|
||||
Self::with_input(std::io::stdin(), color, begin, again)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, R: Read> Repline<'a, R> {
|
||||
/// Constructs a [Repline] with the given [Reader](Read), color, begin, and again prompts.
|
||||
pub fn with_input(input: R, color: &'a str, begin: &'a str, again: &'a str) -> Self {
|
||||
Self {
|
||||
input: Chars(Flatten(input.bytes())),
|
||||
history: Default::default(),
|
||||
hindex: 0,
|
||||
ed: Editor::new(color, begin, again),
|
||||
}
|
||||
}
|
||||
/// Set the terminal prompt color
|
||||
pub fn set_color(&mut self, color: &'a str) {
|
||||
self.ed.color = color
|
||||
}
|
||||
/// Append line to history and clear it
|
||||
pub fn accept(&mut self) {
|
||||
self.history_append(self.ed.to_string());
|
||||
self.ed.clear();
|
||||
self.hindex = self.history.len();
|
||||
}
|
||||
/// Clear the line
|
||||
pub fn deny(&mut self) {
|
||||
self.ed.clear()
|
||||
}
|
||||
/// Reads in a line, and returns it for validation
|
||||
pub fn read(&mut self) -> ReplResult<String> {
|
||||
const INDENT: &str = " ";
|
||||
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()?;
|
||||
match self.input.next().ok_or(Error::EndOfInput)?? {
|
||||
// Ctrl+C: End of Text. Immediately exits.
|
||||
'\x03' => {
|
||||
drop(_make_raw);
|
||||
writeln!(stdout)?;
|
||||
return Err(Error::CtrlC(self.ed.to_string()));
|
||||
}
|
||||
// Ctrl+D: End of Transmission. Ends the current line.
|
||||
'\x04' => {
|
||||
drop(_make_raw);
|
||||
writeln!(stdout)?;
|
||||
return Err(Error::CtrlD(self.ed.to_string()));
|
||||
}
|
||||
// Tab: extend line by 4 spaces
|
||||
'\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)?;
|
||||
return Ok(self.ed.to_string());
|
||||
}
|
||||
// Ctrl+Backspace in my terminal
|
||||
'\x17' => {
|
||||
self.ed.erase_word(stdout)?;
|
||||
}
|
||||
// Escape sequence
|
||||
'\x1b' => self.escape(stdout)?,
|
||||
// backspace
|
||||
'\x08' | '\x7f' => {
|
||||
let ed = &mut self.ed;
|
||||
if ed.ends_with(INDENT.chars()) {
|
||||
for _ in 0..INDENT.len() {
|
||||
ed.pop(stdout)?;
|
||||
}
|
||||
} else {
|
||||
ed.pop(stdout)?;
|
||||
}
|
||||
}
|
||||
c if c.is_ascii_control() => {
|
||||
if cfg!(debug_assertions) {
|
||||
self.ed.extend(c.escape_debug(), stdout)?;
|
||||
}
|
||||
}
|
||||
c => {
|
||||
self.ed.push(c, stdout)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Handle ANSI Escape
|
||||
fn escape<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
|
||||
match self.input.next().ok_or(Error::EndOfInput)?? {
|
||||
'[' => self.csi(w)?,
|
||||
'O' => todo!("Process alternate character mode"),
|
||||
other => self.ed.extend(other.escape_debug(), w)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Handle ANSI Control Sequence Introducer
|
||||
fn csi<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
|
||||
match self.input.next().ok_or(Error::EndOfInput)?? {
|
||||
'A' => {
|
||||
self.hindex = self.hindex.saturating_sub(1);
|
||||
self.restore_history(w)?
|
||||
}
|
||||
'B' => {
|
||||
self.hindex = self.hindex.saturating_add(1).min(self.history.len());
|
||||
self.restore_history(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)?? {
|
||||
let _ = self.ed.delete(w);
|
||||
}
|
||||
}
|
||||
other => {
|
||||
if cfg!(debug_assertions) {
|
||||
self.ed.extend(other.escape_debug(), w)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// 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.extend(history.chars(), w)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Append line to history
|
||||
fn history_append(&mut self, mut buf: String) {
|
||||
while buf.ends_with(char::is_whitespace) {
|
||||
buf.pop();
|
||||
}
|
||||
if !self.history.contains(&buf) {
|
||||
self.history.push_back(buf)
|
||||
}
|
||||
while self.history.len() > 20 {
|
||||
self.history.pop_front();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ fn n_digit(n: u32) -> char {
|
||||
}) as char
|
||||
}
|
||||
|
||||
fn in_range(&num: &u32, start: u32, end: u32) -> bool {
|
||||
fn in_range(num: u32, start: u32, end: u32) -> bool {
|
||||
(start <= num) && (num <= end )
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ fn ascii() {
|
||||
}
|
||||
print(n_digit(row), n_digit(col), ' ')
|
||||
}
|
||||
print(" │");
|
||||
print(" │")
|
||||
for col in 0..16 {
|
||||
print(ascii_picture(row << 4 | col))
|
||||
}
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
#!/usr/bin/env -S conlang -r false
|
||||
//! 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, rhs]) => (execute(lhs) as u64 >> execute(rhs) as u64) as f64,
|
||||
Expr::Op('<', [lhs, rhs]) => (execute(lhs) as u64 << execute(rhs) as u64) as f64,
|
||||
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,
|
||||
Shift,
|
||||
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,
|
||||
'>' => Power::Shift,
|
||||
'<' => Power::Shift,
|
||||
_ => Power::None,
|
||||
})
|
||||
}
|
||||
|
||||
fn pre_bp(op: char) -> i32 {
|
||||
(|x| 2 * x + 1)(
|
||||
match op {
|
||||
'-' => Power::Unary,
|
||||
_ => panic("Unknown unary operator: " + op),
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
loop {
|
||||
let line = get_line("calc >");
|
||||
let (expr, rest) = line.chars().parse(0);
|
||||
|
||||
println(fmt_expr(expr), " -> ", execute(expr));
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
//! Implements format string evaluation in weak Conlang
|
||||
|
||||
/// Formats a string
|
||||
#[rustfmt::skip]
|
||||
fn f(__fmt: &str) -> &str {
|
||||
let __out = "";
|
||||
let __expr = "";
|
||||
let __label = "";
|
||||
let __depth = 0;
|
||||
for __c in chars(__fmt) {
|
||||
match __c {
|
||||
'{' => {
|
||||
__depth += 1;
|
||||
if __depth <= 1 {
|
||||
continue
|
||||
}
|
||||
},
|
||||
'}' => {
|
||||
__depth -= 1;
|
||||
if __depth <= 0 {
|
||||
__out = fmt(__out, __label, eval(__expr));
|
||||
(__expr, __label) = ("", "");
|
||||
continue
|
||||
}
|
||||
},
|
||||
':' => if __depth == 1 && __label.len() == 0 {
|
||||
__label = __expr + __c;
|
||||
continue
|
||||
},
|
||||
'=' => if __depth == 1 && __label.len() == 0 {
|
||||
__label = __expr + __c;
|
||||
continue
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
match (__depth, __label.len()) {
|
||||
(0, _) => {__out += __c},
|
||||
(_, 0) => {__expr += __c},
|
||||
(_, _) => {__label += __c},
|
||||
}
|
||||
}
|
||||
__out
|
||||
}
|
||||
5
sample-code/hello.tp
Normal file
5
sample-code/hello.tp
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
// The main function
|
||||
nasin wan () {
|
||||
toki_linja("mi toki ale a!")
|
||||
}
|
||||
@@ -6,7 +6,7 @@ fn in_range(this: Ord, min: Ord, max: Ord) -> bool {
|
||||
}
|
||||
|
||||
fn frequency(s: str) -> [i32; 128] {
|
||||
let letters = [0; 128];
|
||||
let letters = [0;128];
|
||||
for letter in s {
|
||||
if (letter).in_range(' ', letters.len() as char) {
|
||||
letters[(letter as i32)] += 1;
|
||||
@@ -25,22 +25,20 @@ fn plot_freq(freq: [i32; 128]) -> str {
|
||||
buf
|
||||
}
|
||||
|
||||
const msg: str = "letter_frequency.cl
|
||||
const msg: str ="letter_frequency.cl
|
||||
Computes the frequency of ascii characters in a block of text, and prints it bucket-sorted.
|
||||
Press Ctrl+D to quit.";
|
||||
|
||||
fn main() {
|
||||
println(msg);
|
||||
fn main () {
|
||||
println(msg)
|
||||
let lines = "";
|
||||
loop {
|
||||
let line = get_line();
|
||||
if line == "" {
|
||||
break ();
|
||||
}
|
||||
if line == "" { break() }
|
||||
lines += line;
|
||||
}
|
||||
|
||||
let freq = frequency(lines);
|
||||
let plot = plot_freq(freq);
|
||||
let freq = frequency(lines)
|
||||
let plot = plot_freq(freq)
|
||||
println(plot)
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
#!/usr/bin/env -S conlang -r false
|
||||
|
||||
fn link (link, text) -> &str
|
||||
"\x1b]8;;" + link + "\x1b\\" + text + "\x1b]8;;\x1b\\";
|
||||
|
||||
fn main () {
|
||||
println(link("https://conlang.foo/docs", "Here's a link to the docs!"));
|
||||
println(link("https://conlang.foo/source", "Here's a link to the source!"))
|
||||
}
|
||||
@@ -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)"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,13 @@
|
||||
// These two functions shouldn't actually be polymorphic, but
|
||||
// the AST interpreter doesn't know about type annotations
|
||||
// or operator overloading.
|
||||
pub fn max<T>(a: T, b: T) -> T {
|
||||
#[generic("T")]
|
||||
pub fn max(a: T, b: T) -> T {
|
||||
(if a < b { b } else { a })
|
||||
}
|
||||
|
||||
pub fn min<T>(a: T, b: T) -> T {
|
||||
#[generic("T")]
|
||||
pub fn min(a: T, b: T) -> T {
|
||||
(if a > b { b } else { a })
|
||||
}
|
||||
|
||||
|
||||
10
sample-code/pona.tp
Normal file
10
sample-code/pona.tp
Normal 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;
|
||||
@@ -13,11 +13,11 @@ pub fn lfsr_next() {
|
||||
}
|
||||
|
||||
/// Returns a pseudorandom byte
|
||||
pub fn rand() -> u64 {
|
||||
pub fn rand() -> u8 {
|
||||
for _ in 0..8 {
|
||||
lfsr_next()
|
||||
};
|
||||
(state & 0xff) as u64
|
||||
}
|
||||
state & 0xff
|
||||
}
|
||||
|
||||
// Prints a maze out of diagonal box drawing characters, ['╲', '╱']
|
||||
|
||||
@@ -6,28 +6,24 @@ const EPSILON: f64 = 8.8541878188 / 1000000000000.0;
|
||||
|
||||
/// Calcuates the absolute value of a number
|
||||
fn f64_abs(n: f64) -> f64 {
|
||||
let n = n as f64;
|
||||
if n < (0.0) {
|
||||
-n
|
||||
} else {
|
||||
n
|
||||
}
|
||||
let n = n as f64
|
||||
if n < (0.0) { -n } else { n }
|
||||
}
|
||||
|
||||
/// Square root approximation using Newton's method
|
||||
fn sqrt(n: f64) -> f64 {
|
||||
let n = n as f64;
|
||||
let n = n as f64
|
||||
if n < 0.0 {
|
||||
return 0.0 / 0.0; // TODO: NaN constant
|
||||
return 0.0 / 0.0 // TODO: NaN constant
|
||||
}
|
||||
if n == 0.0 {
|
||||
return 0.0;
|
||||
return 0.0
|
||||
}
|
||||
|
||||
let z = n;
|
||||
let z = n
|
||||
loop {
|
||||
let adj = (z * z - n) / (2.0 * z);
|
||||
z -= adj;
|
||||
let adj = (z * z - n) / (2.0 * z)
|
||||
z -= adj
|
||||
if adj.f64_abs() < EPSILON {
|
||||
break z;
|
||||
}
|
||||
@@ -41,9 +37,7 @@ fn pythag(a: f64, b: f64) -> f64 {
|
||||
|
||||
/// Quadratic formula: (-b ± √(b² - 4ac)) / 2a
|
||||
fn quadratic(a: f64, b: f64, c: f64) -> (f64, f64) {
|
||||
let a = a as f64;
|
||||
let b = b as f64;
|
||||
let c = c as f64;
|
||||
let a = a as f64; let b = b as f64; let c = c as f64;
|
||||
(
|
||||
(-b + sqrt(b * b - 4.0 * a * c)) / 2.0 * a,
|
||||
(-b - sqrt(b * b - 4.0 * a * c)) / 2.0 * a,
|
||||
@@ -52,11 +46,11 @@ fn quadratic(a: f64, b: f64, c: f64) -> (f64, f64) {
|
||||
|
||||
fn main() {
|
||||
for i in 0..10 {
|
||||
println("sqrt(", i, ") ≅ ", sqrt(i as f64))
|
||||
println("sqrt(",i,") ≅ ",sqrt(i as f64))
|
||||
}
|
||||
println("\nPythagorean Theorem");
|
||||
println("Hypotenuse of ⊿(5, 12): ", pythag(5.0, 12.0));
|
||||
println("\nPythagorean Theorem")
|
||||
println("Hypotenuse of ⊿(5, 12): ", pythag(5.0, 12.0))
|
||||
|
||||
println("\nQuadratic formula");
|
||||
println("Roots of 10x² + 4x - 1: ", quadratic(10.0, 40, -1.0));
|
||||
println("\nQuadratic formula")
|
||||
println("Roots of 10x² + 4x - 1: ", quadratic(10.0, 40, -1.0))
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
//! Implements a Truth Machine
|
||||
|
||||
fn main (n)
|
||||
match n {
|
||||
1 => loop print(1),
|
||||
n => println(n),
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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;
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user