Compare commits

..

18 Commits

Author SHA1 Message Date
adbabc66c5 cl-interpret: impls v1 2025-02-23 04:06:14 -06:00
5c99bf09ab stdlib: Make stdlib not error out w/ undefined symbols, add ranges. 2025-02-23 03:29:15 -06:00
4d9b13f7a1 cl-interpret: Enums, pt 1: C was right the whole time!!1 2025-02-23 03:22:48 -06:00
d9ac9e628d cl-interpret: Stage items within a file in resolution order.
TODO: Does this even help???
2025-02-23 03:21:34 -06:00
632ddf0eab cl-interpret: cleanup 2025-02-23 03:00:00 -06:00
e39b390441 cl-parser, cl-repl: Add ./[mod].cl to module search path 2025-02-23 02:44:26 -06:00
2fd08193fd cl-parser: Promote match scrutinee to position 2025-02-23 02:43:22 -06:00
7d3f189100 conlang: Introduce ..rest Patterns, refactor Ranges 2025-02-23 02:41:41 -06:00
cc6168b55e cl-ast: Remove Param, replace with flat Pattern 2025-02-23 02:01:38 -06:00
e3d94d8949 conlang: Unify binding operations!
This breaks out the pattern matching/unification algorithm from cl-interpret/interpret.rs to cl-interpret/pattern.rs

TODO: pattern destructuring in const, static :^)
2025-02-22 05:16:37 -06:00
7a8da33de9 cl-interpret: Tuple structs + fix tuple member access 2025-02-22 03:31:27 -06:00
697d139cfd conlang: Add Tuple-Struct Patterns
- Patterns are no longer parsed with the highest precedence
- Function calls with just a path and a tuple of args can now be transformed into a Pattern
2025-02-22 01:37:08 -06:00
5d2c714bc1 conlang: Patterns...2!
- Deny arbitrary paths in patterns (only one non-keyword identifier allowed!)
- Allow patterns in for-loop binders (literally useless atm, but it's a step toward making patterns the only way to bind names.)

Next: Functions, Tuple Struct Patterns... And solving the stupid syntactic ambiguity of structors.
2025-02-22 01:00:29 -06:00
b115fea71b cl-interpret: fix import in builtin! doctest 2025-02-21 22:49:14 -06:00
eebabf02fb repline: change error formatting 2025-02-20 22:05:43 -06:00
088cd4d1e4 cl-interpret: Change format of todo for Impl 2025-02-20 22:05:23 -06:00
0fd9c002fc cl-interpret: Interpret items in a file in a particular order
1) mod
2) use
3) enum, struct, type
4) fn
5) impl
6) const | static

This is a stopgap until names are statically resolved
2025-02-20 22:04:27 -06:00
772286eefa conlang: Single-expression functions 2025-02-20 21:59:42 -06:00
28 changed files with 763 additions and 567 deletions

View File

@ -145,15 +145,8 @@ pub enum ModuleKind {
pub struct Function { pub struct Function {
pub name: Sym, pub name: Sym,
pub sign: TyFn, pub sign: TyFn,
pub bind: Vec<Param>, pub bind: Vec<Pattern>,
pub body: Option<Block>, pub body: Option<Expr>,
}
/// 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 /// A user-defined product type
@ -418,12 +411,14 @@ pub struct Let {
/// A [Pattern] meta-expression (any [`ExprKind`] that fits pattern rules) /// A [Pattern] meta-expression (any [`ExprKind`] that fits pattern rules)
#[derive(Clone, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Pattern { pub enum Pattern {
Path(Path), Name(Sym),
Literal(Literal), Literal(Literal),
Rest(Option<Box<Pattern>>),
Ref(Mutability, Box<Pattern>), Ref(Mutability, Box<Pattern>),
Tuple(Vec<Pattern>), Tuple(Vec<Pattern>),
Array(Vec<Pattern>), Array(Vec<Pattern>),
Struct(Path, Vec<(Path, Option<Pattern>)>), Struct(Path, Vec<(Sym, Option<Pattern>)>),
TupleStruct(Path, Vec<Pattern>),
} }
/// A `match` expression: `match` `{` ([MatchArm] `,`)* [MatchArm]? `}` /// A `match` expression: `match` `{` ([MatchArm] `,`)* [MatchArm]? `}`
@ -511,6 +506,8 @@ pub enum UnaryKind {
Deref, Deref,
Neg, Neg,
Not, Not,
RangeInc,
RangeExc,
/// A Loop expression: `loop` [`Block`] /// A Loop expression: `loop` [`Block`]
Loop, Loop,
/// Unused /// Unused
@ -620,7 +617,7 @@ pub struct If {
/// A [For] expression: `for` Pattern `in` [`Expr`] [`Block`] [`Else`]? /// A [For] expression: `for` Pattern `in` [`Expr`] [`Block`] [`Else`]?
#[derive(Clone, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct For { pub struct For {
pub bind: Sym, // TODO: Patterns? pub bind: Pattern,
pub cond: Box<Expr>, pub cond: Box<Expr>,
pub pass: Box<Block>, pub pass: Box<Block>,
pub fail: Else, pub fail: Else,

View File

@ -9,24 +9,8 @@ mod display {
use std::{ use std::{
borrow::Borrow, borrow::Borrow,
fmt::{Display, Write}, fmt::{Display, Write},
io::IsTerminal,
}; };
fn keyword(d: impl Display, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if std::io::stdout().is_terminal() {
write!(f, "\x1b[31m{d}\x1b[0m")
} else {
d.fmt(f)
}
}
fn ident(d: impl Display, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if std::io::stdout().is_terminal() {
write!(f, "\x1b[95m{d}\x1b[0m")
} else {
d.fmt(f)
}
}
fn separate<I: Display, W: Write>( fn separate<I: Display, W: Write>(
iterable: impl IntoIterator<Item = I>, iterable: impl IntoIterator<Item = I>,
sep: &'static str, sep: &'static str,
@ -46,7 +30,7 @@ mod display {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Mutability::Not => Ok(()), Mutability::Not => Ok(()),
Mutability::Mut => keyword("ante ", f), Mutability::Mut => "mut ".fmt(f),
} }
} }
} }
@ -55,15 +39,14 @@ mod display {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Visibility::Private => Ok(()), Visibility::Private => Ok(()),
Visibility::Public => keyword("lukin ", f), Visibility::Public => "pub ".fmt(f),
} }
} }
} }
impl Display for Literal { impl Display for Literal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(this: &Literal, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self {
match this {
Literal::Bool(v) => v.fmt(f), Literal::Bool(v) => v.fmt(f),
Literal::Char(v) => write!(f, "'{}'", v.escape_debug()), Literal::Char(v) => write!(f, "'{}'", v.escape_debug()),
Literal::Int(v) => v.fmt(f), Literal::Int(v) => v.fmt(f),
@ -71,14 +54,6 @@ mod display {
Literal::String(v) => write!(f, "\"{}\"", v.escape_debug()), Literal::String(v) => write!(f, "\"{}\"", v.escape_debug()),
} }
} }
if std::io::stdout().is_terminal() {
write!(f, "\x1b[94m")?;
fmt(self, f)?;
write!(f, "\x1b[0m")
} else {
fmt(self, f)
}
}
} }
impl Display for File { impl Display for File {
@ -102,8 +77,7 @@ mod display {
impl Display for Meta { impl Display for Meta {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self; let Self { name, kind } = self;
ident(name, f)?; write!(f, "{name}{kind}")
write!(f, "{kind}")
} }
} }
@ -145,10 +119,9 @@ mod display {
impl Display for Alias { impl Display for Alias {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { to, from } = self; let Self { to, from } = self;
keyword("ijo", f)?;
match from { match from {
Some(from) => write!(f, " {to} = {from};"), Some(from) => write!(f, "type {to} = {from};"),
None => write!(f, " {to};"), None => write!(f, "type {to};"),
} }
} }
} }
@ -156,28 +129,21 @@ mod display {
impl Display for Const { impl Display for Const {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, ty, init } = self; let Self { name, ty, init } = self;
keyword("kiwen ", f)?; write!(f, "const {name}: {ty} = {init}")
ident(name, f)?;
write!(f, ": {ty} = {init}")
} }
} }
impl Display for Static { impl Display for Static {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { mutable, name, ty, init } = self; let Self { mutable, name, ty, init } = self;
keyword("mute", f)?; write!(f, "static {mutable}{name}: {ty} = {init}")
write!(f, " {mutable}")?;
ident(name, f)?;
write!(f, ": {ty} = {init}")
} }
} }
impl Display for Module { impl Display for Module {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self; let Self { name, kind } = self;
keyword("selo ", f)?; write!(f, "mod {name}{kind}")
ident(name, f)?;
write!(f, "{kind}")
} }
} }
@ -206,10 +172,7 @@ mod display {
}; };
debug_assert_eq!(bind.len(), types.len()); debug_assert_eq!(bind.len(), types.len());
keyword("nasin", f)?; write!(f, "fn {name} ")?;
" ".fmt(f)?;
ident(name, f)?;
" ".fmt(f)?;
{ {
let mut f = f.delimit(INLINE_PARENS); let mut f = f.delimit(INLINE_PARENS);
for (idx, (arg, ty)) in bind.iter().zip(types.iter()).enumerate() { for (idx, (arg, ty)) in bind.iter().zip(types.iter()).enumerate() {
@ -229,20 +192,10 @@ mod display {
} }
} }
impl Display for Param {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { mutability, name } = self;
write!(f, "{mutability}")?;
ident(name, f)
}
}
impl Display for Struct { impl Display for Struct {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self; let Self { name, kind } = self;
keyword("lipu ", f)?; write!(f, "struct {name}{kind}")
ident(name, f)?;
write!(f, "{kind}")
} }
} }
@ -259,18 +212,14 @@ mod display {
impl Display for StructMember { impl Display for StructMember {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { vis, name, ty } = self; let Self { vis, name, ty } = self;
write!(f, "{vis}")?; write!(f, "{vis}{name}: {ty}")
ident(name, f)?;
write!(f, ": {ty}")
} }
} }
impl Display for Enum { impl Display for Enum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self; let Self { name, kind } = self;
keyword("kulupu ", f)?; write!(f, "enum {name}{kind}")
ident(name, f)?;
write!(f, "{kind}")
} }
} }
@ -286,8 +235,7 @@ mod display {
impl Display for Variant { impl Display for Variant {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self; let Self { name, kind } = self;
ident(name, f)?; write!(f, "{name}{kind}")
write!(f, "{kind}")
} }
} }
@ -305,8 +253,7 @@ mod display {
impl Display for Impl { impl Display for Impl {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { target, body } = self; let Self { target, body } = self;
keyword("insa", f)?; write!(f, "impl {target} ")?;
write!(f, " {target} ")?;
write!(f.delimit(BRACES), "{body}") write!(f.delimit(BRACES), "{body}")
} }
} }
@ -316,9 +263,7 @@ mod display {
match self { match self {
ImplKind::Type(t) => t.fmt(f), ImplKind::Type(t) => t.fmt(f),
ImplKind::Trait { impl_trait, for_type } => { ImplKind::Trait { impl_trait, for_type } => {
write!(f, "{impl_trait} ")?; write!(f, "{impl_trait} for {for_type}")
keyword("ale", f)?;
write!(f, " {for_type}")
} }
} }
} }
@ -327,8 +272,7 @@ mod display {
impl Display for Use { impl Display for Use {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { absolute, tree } = self; let Self { absolute, tree } = self;
keyword("jo", f)?; f.write_str(if *absolute { "use ::" } else { "use " })?;
f.write_str(if *absolute { " ::" } else { " " })?;
write!(f, "{tree};") write!(f, "{tree};")
} }
} }
@ -338,12 +282,8 @@ mod display {
match self { match self {
UseTree::Tree(tree) => separate(tree, ", ")(f.delimit(INLINE_BRACES)), UseTree::Tree(tree) => separate(tree, ", ")(f.delimit(INLINE_BRACES)),
UseTree::Path(path, rest) => write!(f, "{path}::{rest}"), UseTree::Path(path, rest) => write!(f, "{path}::{rest}"),
UseTree::Alias(path, name) => { UseTree::Alias(path, name) => write!(f, "{path} as {name}"),
write!(f, "{path} ")?; UseTree::Name(name) => write!(f, "{name}"),
keyword("sama ", f)?;
ident(name, f)
}
UseTree::Name(name) => ident(name, f),
UseTree::Glob => write!(f, "*"), UseTree::Glob => write!(f, "*"),
} }
} }
@ -403,8 +343,7 @@ mod display {
impl Display for TyFn { impl Display for TyFn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { args, rety } = self; let Self { args, rety } = self;
keyword("nasin", f)?; write!(f, "fn {args}")?;
write!(f, " {args}")?;
match rety { match rety {
Some(v) => write!(f, " -> {v}"), Some(v) => write!(f, " -> {v}"),
None => Ok(()), None => Ok(()),
@ -425,10 +364,10 @@ mod display {
impl Display for PathPart { impl Display for PathPart {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
PathPart::SuperKw => keyword("mama", f), PathPart::SuperKw => "super".fmt(f),
PathPart::SelfKw => keyword("mi", f), PathPart::SelfKw => "self".fmt(f),
PathPart::SelfTy => keyword("Mi", f), PathPart::SelfTy => "Self".fmt(f),
PathPart::Ident(id) => ident(id, f), PathPart::Ident(id) => id.fmt(f),
} }
} }
} }
@ -493,7 +432,7 @@ mod display {
ExprKind::For(v) => v.fmt(f), ExprKind::For(v) => v.fmt(f),
ExprKind::Break(v) => v.fmt(f), ExprKind::Break(v) => v.fmt(f),
ExprKind::Return(v) => v.fmt(f), ExprKind::Return(v) => v.fmt(f),
ExprKind::Continue => keyword("tama", f), ExprKind::Continue => "continue".fmt(f),
} }
} }
} }
@ -508,10 +447,7 @@ mod display {
impl Display for Let { impl Display for Let {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { mutable, name, ty, init } = self; let Self { mutable, name, ty, init } = self;
keyword("poki", f)?; write!(f, "let {mutable}{name}")?;
write!(f, " {mutable}")?;
ident(name, f)?;
if let Some(value) = ty { if let Some(value) = ty {
write!(f, ": {value}")?; write!(f, ": {value}")?;
} }
@ -525,17 +461,19 @@ mod display {
impl Display for Pattern { impl Display for Pattern {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Pattern::Path(path) => path.fmt(f), Pattern::Name(sym) => sym.fmt(f),
Pattern::Literal(literal) => literal.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::Ref(mutability, pattern) => write!(f, "&{mutability}{pattern}"),
Pattern::Tuple(patterns) => separate(patterns, ", ")(f.delimit(INLINE_PARENS)), Pattern::Tuple(patterns) => separate(patterns, ", ")(f.delimit(INLINE_PARENS)),
Pattern::Array(patterns) => separate(patterns, ", ")(f.delimit(INLINE_SQUARE)), Pattern::Array(patterns) => separate(patterns, ", ")(f.delimit(INLINE_SQUARE)),
Pattern::Struct(path, items) => { Pattern::Struct(path, items) => {
write!(f, "{path}: ")?; write!(f, "{path} ")?;
let f = &mut f.delimit(BRACES); let f = &mut f.delimit(INLINE_BRACES);
for (idx, (name, item)) in items.iter().enumerate() { for (idx, (name, item)) in items.iter().enumerate() {
if idx != 0 { if idx != 0 {
f.write_str(",\n")?; f.write_str(", ")?;
} }
write!(f, "{name}")?; write!(f, "{name}")?;
if let Some(pattern) = item { if let Some(pattern) = item {
@ -544,6 +482,10 @@ mod display {
} }
Ok(()) Ok(())
} }
Pattern::TupleStruct(path, items) => {
write!(f, "{path}")?;
separate(items, ", ")(f.delimit(INLINE_PARENS))
}
} }
} }
} }
@ -551,8 +493,7 @@ mod display {
impl Display for Match { impl Display for Match {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { scrutinee, arms } = self; let Self { scrutinee, arms } = self;
keyword("seme", f)?; write!(f, "match {scrutinee} ")?;
write!(f, " {scrutinee} ")?;
separate(arms, ",\n")(f.delimit(BRACES)) separate(arms, ",\n")(f.delimit(BRACES))
} }
} }
@ -647,10 +588,12 @@ mod display {
impl Display for UnaryKind { impl Display for UnaryKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
UnaryKind::Loop => return keyword("awen ", f), UnaryKind::Loop => "loop ",
UnaryKind::Deref => "*", UnaryKind::Deref => "*",
UnaryKind::Neg => "-", UnaryKind::Neg => "-",
UnaryKind::Not => "!", UnaryKind::Not => "!",
UnaryKind::RangeExc => "..",
UnaryKind::RangeInc => "..=",
UnaryKind::At => "@", UnaryKind::At => "@",
UnaryKind::Tilde => "~", UnaryKind::Tilde => "~",
} }
@ -661,9 +604,7 @@ mod display {
impl Display for Cast { impl Display for Cast {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { head, ty } = self; let Self { head, ty } = self;
write!(f, "{head} ")?; write!(f, "{head} as {ty}")
keyword("sama", f)?;
write!(f, " {ty}")
} }
} }
@ -677,12 +618,9 @@ mod display {
impl Display for MemberKind { impl Display for MemberKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
MemberKind::Call(name, args) => { MemberKind::Call(name, args) => write!(f, "{name}{args}"),
ident(name, f)?; MemberKind::Struct(name) => write!(f, "{name}"),
separate(&args.exprs, ", ")(f.delimit(INLINE_PARENS)) MemberKind::Tuple(name) => write!(f, "{name}"),
}
MemberKind::Struct(name) => ident(name, f),
MemberKind::Tuple(name) => ident(name, f),
} }
} }
} }
@ -698,7 +636,7 @@ mod display {
impl Display for Structor { impl Display for Structor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { to, init } = self; let Self { to, init } = self;
write!(f, "{to}: ")?; write!(f, "{to} ")?;
separate(init, ", ")(f.delimit(INLINE_BRACES)) separate(init, ", ")(f.delimit(INLINE_BRACES))
} }
} }
@ -706,7 +644,7 @@ mod display {
impl Display for Fielder { impl Display for Fielder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, init } = self; let Self { name, init } = self;
ident(name, f)?; write!(f, "{name}")?;
if let Some(init) = init { if let Some(init) = init {
write!(f, ": {init}")?; write!(f, ": {init}")?;
} }
@ -766,35 +704,28 @@ mod display {
impl Display for While { impl Display for While {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { cond, pass, fail } = self; let Self { cond, pass, fail } = self;
write!(f, "lawa {cond} {pass}{fail}") write!(f, "while {cond} {pass}{fail}")
} }
} }
impl Display for If { impl Display for If {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { cond, pass, fail } = self; let Self { cond, pass, fail } = self;
keyword("tan", f)?; write!(f, "if {cond} {pass}{fail}")
write!(f, " {cond} {pass}{fail}")
} }
} }
impl Display for For { impl Display for For {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { bind, cond, pass, fail } = self; let Self { bind, cond, pass, fail } = self;
keyword("ale", f)?; write!(f, "for {bind} in {cond} {pass}{fail}")
write!(f, " {bind} ")?;
keyword("lon", f)?;
write!(f, " {cond} {pass}{fail}")
} }
} }
impl Display for Else { impl Display for Else {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.body { match &self.body {
Some(body) => { Some(body) => write!(f, " else {body}"),
keyword(" taso", f)?;
write!(f, " {body}")
}
_ => Ok(()), _ => Ok(()),
} }
} }
@ -802,7 +733,7 @@ mod display {
impl Display for Break { impl Display for Break {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
keyword("pana", f)?; write!(f, "break")?;
match &self.body { match &self.body {
Some(body) => write!(f, " {body}"), Some(body) => write!(f, " {body}"),
_ => Ok(()), _ => Ok(()),
@ -812,7 +743,7 @@ mod display {
impl Display for Return { impl Display for Return {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
keyword("pini", f)?; write!(f, "return")?;
match &self.body { match &self.body {
Some(body) => write!(f, " {body}"), Some(body) => write!(f, " {body}"),
_ => Ok(()), _ => Ok(()),
@ -933,7 +864,10 @@ mod convert {
fn try_from(value: ExprKind) -> Result<Self, Self::Error> { fn try_from(value: ExprKind) -> Result<Self, Self::Error> {
Ok(match value { Ok(match value {
ExprKind::Literal(literal) => Pattern::Literal(literal), ExprKind::Literal(literal) => Pattern::Literal(literal),
ExprKind::Path(path) => Pattern::Path(path), 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::Empty => Pattern::Tuple(vec![]),
ExprKind::Group(Group { expr }) => Pattern::Tuple(vec![Pattern::try_from(*expr)?]), ExprKind::Group(Group { expr }) => Pattern::Tuple(vec![Pattern::try_from(*expr)?]),
ExprKind::Tuple(Tuple { exprs }) => Pattern::Tuple( ExprKind::Tuple(Tuple { exprs }) => Pattern::Tuple(
@ -951,16 +885,27 @@ mod convert {
.map(|e| Pattern::try_from(e.kind)) .map(|e| Pattern::try_from(e.kind))
.collect::<Result<_, _>>()?, .collect::<Result<_, _>>()?,
), ),
// ExprKind::Index(index) => todo!(), ExprKind::Binary(Binary { kind: BinaryKind::Call, parts }) => {
// ExprKind::Member(member) => todo!(), let (ExprKind::Path(path), args) = *parts else {
return Err(parts.0);
};
match args {
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::Unary(Unary { kind: UnaryKind::RangeExc, tail }) => {
Pattern::Rest(Some(Pattern::try_from(*tail)?.into()))
}
ExprKind::Structor(Structor { to, init }) => { ExprKind::Structor(Structor { to, init }) => {
let fields = init let fields = init
.into_iter() .into_iter()
.map(|Fielder { name, init }| { .map(|Fielder { name, init }| {
Ok(( Ok((name, init.map(|i| Pattern::try_from(i.kind)).transpose()?))
name.into(),
init.map(|i| Pattern::try_from(i.kind)).transpose()?,
))
}) })
.collect::<Result<_, Self::Error>>()?; .collect::<Result<_, Self::Error>>()?;
Pattern::Struct(to, fields) Pattern::Struct(to, fields)
@ -995,6 +940,14 @@ mod path {
} }
} }
/// 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, `_` /// Checks whether this path refers to the sinkhole identifier, `_`
pub fn is_sinkhole(&self) -> bool { pub fn is_sinkhole(&self) -> bool {
if let [PathPart::Ident(id)] = self.parts.as_slice() { if let [PathPart::Ident(id)] = self.parts.as_slice() {

View File

@ -106,14 +106,10 @@ pub trait Fold {
Function { Function {
name: self.fold_sym(name), name: self.fold_sym(name),
sign: self.fold_ty_fn(sign), sign: self.fold_ty_fn(sign),
bind: bind.into_iter().map(|p| self.fold_param(p)).collect(), bind: bind.into_iter().map(|p| self.fold_pattern(p)).collect(),
body: body.map(|b| self.fold_block(b)), body: body.map(|b| self.fold_expr(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 { fn fold_struct(&mut self, s: Struct) -> Struct {
let Struct { name, kind } = s; let Struct { name, kind } = s;
Struct { name: self.fold_sym(name), kind: self.fold_struct_kind(kind) } Struct { name: self.fold_sym(name), kind: self.fold_struct_kind(kind) }
@ -248,8 +244,10 @@ pub trait Fold {
fn fold_pattern(&mut self, p: Pattern) -> Pattern { fn fold_pattern(&mut self, p: Pattern) -> Pattern {
match p { match p {
Pattern::Path(path) => Pattern::Path(self.fold_path(path)), Pattern::Name(sym) => Pattern::Name(self.fold_sym(sym)),
Pattern::Literal(literal) => Pattern::Literal(self.fold_literal(literal)), 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( Pattern::Ref(mutability, pattern) => Pattern::Ref(
self.fold_mutability(mutability), self.fold_mutability(mutability),
Box::new(self.fold_pattern(*pattern)), Box::new(self.fold_pattern(*pattern)),
@ -267,6 +265,13 @@ pub trait Fold {
.map(|(name, bind)| (name, bind.map(|p| self.fold_pattern(p)))) .map(|(name, bind)| (name, bind.map(|p| self.fold_pattern(p))))
.collect(), .collect(),
), ),
Pattern::TupleStruct(path, items) => Pattern::TupleStruct(
self.fold_path(path),
items
.into_iter()
.map(|bind| self.fold_pattern(bind))
.collect(),
),
} }
} }
@ -400,7 +405,7 @@ pub trait Fold {
fn fold_for(&mut self, f: For) -> For { fn fold_for(&mut self, f: For) -> For {
let For { bind, cond, pass, fail } = f; let For { bind, cond, pass, fail } = f;
For { For {
bind: self.fold_sym(bind), bind: self.fold_pattern(bind),
cond: Box::new(self.fold_expr(*cond)), cond: Box::new(self.fold_expr(*cond)),
pass: Box::new(self.fold_block(*pass)), pass: Box::new(self.fold_block(*pass)),
fail: self.fold_else(fail), fail: self.fold_else(fail),

View File

@ -83,16 +83,11 @@ pub trait Visit<'a>: Sized {
let Function { name, sign, bind, body } = f; let Function { name, sign, bind, body } = f;
self.visit_sym(name); self.visit_sym(name);
self.visit_ty_fn(sign); self.visit_ty_fn(sign);
bind.iter().for_each(|p| self.visit_param(p)); bind.iter().for_each(|p| self.visit_pattern(p));
if let Some(b) = body { if let Some(b) = body {
self.visit_block(b) self.visit_expr(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) { fn visit_struct(&mut self, s: &'a Struct) {
let Struct { name, kind } = s; let Struct { name, kind } = s;
self.visit_sym(name); self.visit_sym(name);
@ -214,8 +209,10 @@ pub trait Visit<'a>: Sized {
fn visit_pattern(&mut self, p: &'a Pattern) { fn visit_pattern(&mut self, p: &'a Pattern) {
match p { match p {
Pattern::Path(path) => self.visit_path(path), Pattern::Name(name) => self.visit_sym(name),
Pattern::Literal(literal) => self.visit_literal(literal), Pattern::Literal(literal) => self.visit_literal(literal),
Pattern::Rest(Some(name)) => self.visit_pattern(name),
Pattern::Rest(None) => {}
Pattern::Ref(mutability, pattern) => { Pattern::Ref(mutability, pattern) => {
self.visit_mutability(mutability); self.visit_mutability(mutability);
self.visit_pattern(pattern); self.visit_pattern(pattern);
@ -234,6 +231,10 @@ pub trait Visit<'a>: Sized {
}); });
}); });
} }
Pattern::TupleStruct(path, items) => {
self.visit_path(path);
items.iter().for_each(|bind| self.visit_pattern(bind));
}
} }
} }
@ -347,7 +348,7 @@ pub trait Visit<'a>: Sized {
} }
fn visit_for(&mut self, f: &'a For) { fn visit_for(&mut self, f: &'a For) {
let For { bind, cond, pass, fail } = f; let For { bind, cond, pass, fail } = f;
self.visit_sym(bind); self.visit_pattern(bind);
self.visit_expr(cond); self.visit_expr(cond);
self.visit_block(pass); self.visit_block(pass);
self.visit_else(fail); self.visit_else(fail);

View File

@ -57,7 +57,7 @@ impl super::Callable for Builtin {
/// Turns a function definition into a [Builtin]. /// Turns a function definition into a [Builtin].
/// ///
/// ```rust /// ```rust
/// # use cl_interpret::{builtin2::builtin, convalue::ConValue}; /// # use cl_interpret::{builtin::builtin, convalue::ConValue};
/// let my_builtin = builtin! { /// let my_builtin = builtin! {
/// /// Use the `@env` suffix to bind the environment! /// /// Use the `@env` suffix to bind the environment!
/// /// (needed for recursive calls) /// /// (needed for recursive calls)
@ -167,8 +167,6 @@ pub const Builtins: &[Builtin] = &builtins![
ConValue::Ref(r) => return len(env, slice::from_ref(r.as_ref())), ConValue::Ref(r) => return len(env, slice::from_ref(r.as_ref())),
ConValue::Array(t) => t.len() as _, ConValue::Array(t) => t.len() as _,
ConValue::Tuple(t) => t.len() as _, ConValue::Tuple(t) => t.len() as _,
ConValue::RangeExc(start, end) => (end - start) as _,
ConValue::RangeInc(start, end) => (end - start + 1) as _,
_ => Err(Error::TypeError)?, _ => Err(Error::TypeError)?,
}) })
} }
@ -279,20 +277,25 @@ pub const Math: &[Builtin] = &builtins![
}) })
} }
/// Exclusive Range `a..b` #[allow(non_snake_case)]
fn range_exc(from, to) { fn RangeExc(start, end) {
let (&ConValue::Int(from), &ConValue::Int(to)) = (from, to) else { Ok(ConValue::TupleStruct(Box::new((
Err(Error::TypeError)? "RangeExc", Box::new([start.clone(), end.clone()])
}; ))))
Ok(ConValue::RangeExc(from, to))
} }
/// Inclusive Range `a..=b` #[allow(non_snake_case)]
fn range_inc(from, to) { fn RangeInc(start, end) {
let (&ConValue::Int(from), &ConValue::Int(to)) = (from, to) else { Ok(ConValue::TupleStruct(Box::new((
Err(Error::TypeError)? "RangeInc", Box::new([start.clone(), end.clone()])
}; ))))
Ok(ConValue::RangeInc(from, to)) }
#[allow(non_snake_case)]
fn RangeTo(end) {
Ok(ConValue::TupleStruct(Box::new((
"RangeInc", Box::new([end.clone()])
))))
} }
/// Negates the ConValue /// Negates the ConValue

View File

@ -6,7 +6,8 @@ use cl_ast::{format::FmtAdapter, ExprKind, Sym};
use super::{ use super::{
builtin::Builtin, builtin::Builtin,
error::{Error, IResult}, error::{Error, IResult},
function::Function, Callable, Environment, function::Function,
Callable, Environment,
}; };
use std::{collections::HashMap, ops::*, rc::Rc}; use std::{collections::HashMap, ops::*, rc::Rc};
@ -34,12 +35,10 @@ pub enum ConValue {
Array(Box<[ConValue]>), Array(Box<[ConValue]>),
/// A tuple /// A tuple
Tuple(Box<[ConValue]>), Tuple(Box<[ConValue]>),
/// An exclusive range
RangeExc(Integer, Integer),
/// An inclusive range
RangeInc(Integer, Integer),
/// A value of a product type /// A value of a product type
Struct(Box<(Sym, HashMap<Sym, ConValue>)>), Struct(Box<(Sym, HashMap<Sym, ConValue>)>),
/// A value of a product type with anonymous members
TupleStruct(Box<(&'static str, Box<[ConValue]>)>),
/// An entire namespace /// An entire namespace
Module(Box<HashMap<Sym, Option<ConValue>>>), Module(Box<HashMap<Sym, Option<ConValue>>>),
/// A quoted expression /// A quoted expression
@ -58,18 +57,6 @@ impl ConValue {
_ => Err(Error::TypeError)?, _ => Err(Error::TypeError)?,
} }
} }
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))
}
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))
}
pub fn index(&self, index: &Self) -> IResult<ConValue> { pub fn index(&self, index: &Self) -> IResult<ConValue> {
let Self::Int(index) = index else { let Self::Int(index) = index else {
Err(Error::TypeError)? Err(Error::TypeError)?
@ -286,8 +273,6 @@ impl std::fmt::Display for ConValue {
} }
']'.fmt(f) ']'.fmt(f)
} }
ConValue::RangeExc(a, b) => write!(f, "{a}..{}", b + 1),
ConValue::RangeInc(a, b) => write!(f, "{a}..={b}"),
ConValue::Tuple(tuple) => { ConValue::Tuple(tuple) => {
'('.fmt(f)?; '('.fmt(f)?;
for (idx, element) in tuple.iter().enumerate() { for (idx, element) in tuple.iter().enumerate() {
@ -298,11 +283,25 @@ impl std::fmt::Display for ConValue {
} }
')'.fmt(f) ')'.fmt(f)
} }
ConValue::TupleStruct(parts) => {
let (name, tuple) = parts.as_ref();
if !name.is_empty() {
write!(f, "{name}")?;
}
'('.fmt(f)?;
for (idx, element) in tuple.iter().enumerate() {
if idx > 0 {
", ".fmt(f)?
}
element.fmt(f)?
}
')'.fmt(f)
}
ConValue::Struct(parts) => { ConValue::Struct(parts) => {
let (name, map) = parts.as_ref(); let (name, map) = parts.as_ref();
use std::fmt::Write; use std::fmt::Write;
if !name.is_empty() { if !name.is_empty() {
write!(f, "{name}: ")?; write!(f, "{name} ")?;
} }
let mut f = f.delimit_with("{", "\n}"); let mut f = f.delimit_with("{", "\n}");
for (k, v) in map.iter() { for (k, v) in map.iter() {

View File

@ -2,8 +2,8 @@
use collect_upvars::collect_upvars; use collect_upvars::collect_upvars;
use super::{Callable, ConValue, Environment, Error, IResult, Interpret}; use super::{pattern, Callable, ConValue, Environment, Error, IResult, Interpret};
use cl_ast::{Function as FnDecl, Param, Sym}; use cl_ast::{Function as FnDecl, Sym};
use std::{ use std::{
cell::{Ref, RefCell}, cell::{Ref, RefCell},
collections::HashMap, collections::HashMap,
@ -21,12 +21,16 @@ pub struct Function {
decl: Rc<FnDecl>, decl: Rc<FnDecl>,
/// Stores data from the enclosing scopes /// Stores data from the enclosing scopes
upvars: RefCell<Upvars>, upvars: RefCell<Upvars>,
is_constructor: bool,
} }
impl Function { impl Function {
pub fn new(decl: &FnDecl) -> Self { pub fn new(decl: &FnDecl) -> Self {
// let upvars = collect_upvars(decl, env); // let upvars = collect_upvars(decl, env);
Self { decl: decl.clone().into(), upvars: Default::default() } Self { decl: decl.clone().into(), upvars: Default::default(), is_constructor: false }
}
pub fn new_constructor(decl: FnDecl) -> Self {
Self { decl: decl.into(), upvars: Default::default(), is_constructor: true }
} }
pub fn decl(&self) -> &FnDecl { pub fn decl(&self) -> &FnDecl {
&self.decl &self.decl
@ -54,6 +58,9 @@ impl Callable for Function {
if args.len() != bind.len() { if args.len() != bind.len() {
return Err(Error::ArgNumber { want: bind.len(), got: args.len() }); return Err(Error::ArgNumber { want: bind.len(), got: args.len() });
} }
if self.is_constructor {
return Ok(ConValue::TupleStruct(Box::new((Sym::to_ref(name), args.into()))));
}
let Some(body) = body else { let Some(body) = body else {
return Err(Error::NotDefined(*name)); return Err(Error::NotDefined(*name));
}; };
@ -63,8 +70,10 @@ impl Callable for Function {
// TODO: completely refactor data storage // TODO: completely refactor data storage
let mut frame = env.frame("fn args"); let mut frame = env.frame("fn args");
for (Param { mutability: _, name }, value) in bind.iter().zip(args) { for (bind, value) in bind.iter().zip(args) {
frame.insert(*name, Some(value.clone())); for (name, value) in pattern::substitution(bind, value.clone())? {
frame.insert(*name, Some(value));
}
} }
let res = body.interpret(&mut frame); let res = body.interpret(&mut frame);
drop(frame); drop(frame);

View File

@ -1,6 +1,6 @@
//! Collects the "Upvars" of a function at the point of its creation, allowing variable capture //! Collects the "Upvars" of a function at the point of its creation, allowing variable capture
use crate::{convalue::ConValue, env::Environment}; use crate::{convalue::ConValue, env::Environment};
use cl_ast::{ast_visitor::visit::*, Function, Let, Param, Path, PathPart, Pattern, Sym}; use cl_ast::{ast_visitor::visit::*, Function, Let, Path, PathPart, Pattern, Sym};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
pub fn collect_upvars(f: &Function, env: &Environment) -> super::Upvars { pub fn collect_upvars(f: &Function, env: &Environment) -> super::Upvars {
@ -67,11 +67,9 @@ impl<'a> Visit<'a> for CollectUpvars<'_> {
fn visit_function(&mut self, f: &'a cl_ast::Function) { fn visit_function(&mut self, f: &'a cl_ast::Function) {
let Function { name: _, sign: _, bind, body } = f; let Function { name: _, sign: _, bind, body } = f;
// parameters can never be upvars // parameters can never be upvars
for Param { mutability: _, name } in bind { bind.iter().for_each(|pat| self.visit_pattern(pat));
self.bind_name(name);
}
if let Some(body) = body { if let Some(body) = body {
self.visit_block(body); self.visit_expr(body);
} }
} }
@ -79,7 +77,7 @@ impl<'a> Visit<'a> for CollectUpvars<'_> {
let cl_ast::For { bind, cond, pass, fail } = f; let cl_ast::For { bind, cond, pass, fail } = f;
self.visit_expr(cond); self.visit_expr(cond);
self.visit_else(fail); self.visit_else(fail);
self.bind_name(bind); // TODO: is bind only bound in the pass block? self.visit_pattern(bind);
self.visit_block(pass); self.visit_block(pass);
} }
@ -105,12 +103,14 @@ impl<'a> Visit<'a> for CollectUpvars<'_> {
fn visit_pattern(&mut self, p: &'a cl_ast::Pattern) { fn visit_pattern(&mut self, p: &'a cl_ast::Pattern) {
match p { match p {
Pattern::Path(path) => { Pattern::Name(name) => {
if let [PathPart::Ident(name)] = path.parts.as_slice() { self.bind_name(name);
self.bind_name(name)
}
} }
Pattern::Literal(literal) => self.visit_literal(literal), Pattern::Literal(literal) => self.visit_literal(literal),
Pattern::Rest(Some(name)) => {
self.visit_pattern(name);
}
Pattern::Rest(None) => {}
Pattern::Ref(mutability, pattern) => { Pattern::Ref(mutability, pattern) => {
self.visit_mutability(mutability); self.visit_mutability(mutability);
self.visit_pattern(pattern); self.visit_pattern(pattern);
@ -129,6 +129,10 @@ impl<'a> Visit<'a> for CollectUpvars<'_> {
}); });
}); });
} }
Pattern::TupleStruct(path, items) => {
self.visit_path(path);
items.iter().for_each(|bind| self.visit_pattern(bind));
}
} }
} }
} }

View File

@ -8,7 +8,7 @@
use std::{borrow::Borrow, rc::Rc}; use std::{borrow::Borrow, rc::Rc};
use super::*; use super::*;
use cl_ast::*; use cl_ast::{ast_visitor::Visit, *};
use cl_structures::intern::interned::Interned; use cl_structures::intern::interned::Interned;
/// A work-in-progress tree walk interpreter for Conlang /// A work-in-progress tree walk interpreter for Conlang
pub trait Interpret { pub trait Interpret {
@ -20,9 +20,30 @@ pub trait Interpret {
impl Interpret for File { impl Interpret for File {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
for item in &self.items { /// Sorts items
#[derive(Debug, Default)]
struct ItemSorter<'ast>(pub [Vec<&'ast Item>; 8]);
impl<'ast> Visit<'ast> for ItemSorter<'ast> {
fn visit_item(&mut self, i: &'ast Item) {
for stage in match &i.kind {
ItemKind::Module(_) => [0].as_slice(),
ItemKind::Use(_) => &[1, 6],
ItemKind::Enum(_) | ItemKind::Struct(_) | ItemKind::Alias(_) => &[2],
ItemKind::Function(_) => &[3, 7],
ItemKind::Impl(_) => &[4],
ItemKind::Const(_) | ItemKind::Static(_) => &[5],
} {
self.0[*stage].push(i)
}
}
}
let mut items = ItemSorter::default();
items.visit_file(self);
for item in items.0.into_iter().flatten() {
item.interpret(env)?; item.interpret(env)?;
} }
Ok(ConValue::Empty) Ok(ConValue::Empty)
} }
} }
@ -93,22 +114,83 @@ impl Interpret for Function {
} }
} }
impl Interpret for Struct { impl Interpret for Struct {
fn interpret(&self, _env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
println!("TODO: {self}"); let Self { name, kind } = self;
match kind {
StructKind::Empty => {}
StructKind::Tuple(args) => {
// Constructs the AST from scratch. TODO: This, better.
let constructor = Function {
name: *name,
sign: TyFn {
args: TyKind::Tuple(TyTuple {
types: args.iter().map(|ty| ty.kind.clone()).collect(),
})
.into(),
rety: Some(
Ty {
extents: cl_structures::span::Span::dummy(),
kind: TyKind::Path(Path::from(*name)),
}
.into(),
),
},
bind: args
.iter()
.enumerate()
.map(|(idx, _)| Pattern::Name(idx.to_string().into()))
.collect(),
body: None,
};
let constructor = crate::function::Function::new_constructor(constructor);
env.insert(*name, Some(constructor.into()));
}
StructKind::Struct(_) => eprintln!("TODO: {self}"),
}
Ok(ConValue::Empty) Ok(ConValue::Empty)
} }
} }
impl Interpret for Enum { impl Interpret for Enum {
fn interpret(&self, _env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
println!("TODO: {self}"); let Self { name, kind } = self;
if let EnumKind::Variants(variants) = kind {
env.push_frame(Sym::to_ref(name), Default::default());
for (idx, Variant { name, kind }) in variants.iter().enumerate() {
match kind {
VariantKind::Plain => env.insert(*name, Some(ConValue::Int(idx as _))),
VariantKind::CLike(idx) => env.insert(*name, Some(ConValue::Int(*idx as _))),
VariantKind::Tuple(ty) => eprintln!("TODO: Enum-tuple variants: {ty}"),
VariantKind::Struct(_) => eprintln!("TODO: Enum-struct members: {kind}"),
}
}
let (frame, _) = env
.pop_frame()
.expect("Frame stack should remain balanced.");
env.insert(*name, Some(ConValue::Module(Box::new(frame))));
}
Ok(ConValue::Empty) Ok(ConValue::Empty)
} }
} }
impl Interpret for Impl { impl Interpret for Impl {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
println!("TODO: {self}"); let Self { target: ImplKind::Type(Ty { extents: _, kind: TyKind::Path(name) }), body } =
let Self { target: _, body } = self; self
body.interpret(env) else {
eprintln!("TODO: impl X for Ty");
return Ok(ConValue::Empty);
};
env.push_frame("impl", Default::default());
body.interpret(env)?;
let (frame, _) = env
.pop_frame()
.expect("Environment frames must be balanced");
match assignment::addrof_path(env, name.parts.as_slice())? {
Some(ConValue::Module(m)) => m.extend(frame),
Some(other) => eprintln!("TODO: impl for {other}"),
None => {}
}
Ok(ConValue::Empty)
} }
} }
@ -246,19 +328,13 @@ impl Interpret for Let {
let Let { mutable: _, name, ty: _, init } = self; let Let { mutable: _, name, ty: _, init } = self;
match init.as_ref().map(|i| i.interpret(env)).transpose()? { match init.as_ref().map(|i| i.interpret(env)).transpose()? {
Some(value) => { Some(value) => {
for (path, value) in assignment::pattern_substitution(name, value)? { for (name, value) in pattern::substitution(name, value)? {
match path.parts.as_slice() { env.insert(*name, Some(value));
[PathPart::Ident(name)] => env.insert(*name, Some(value)),
_ => eprintln!("Bad assignment: {path} = {value}"),
}
} }
} }
None => { None => {
for path in assignment::pattern_variables(name) { for name in pattern::variables(name) {
match path.parts.as_slice() { env.insert(*name, None);
[PathPart::Ident(name)] => env.insert(*name, None),
_ => eprintln!("Bad assignment: {path}"),
}
} }
} }
} }
@ -270,13 +346,10 @@ impl Interpret for Match {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Self { scrutinee, arms } = self; let Self { scrutinee, arms } = self;
let scrutinee = scrutinee.interpret(env)?; let scrutinee = scrutinee.interpret(env)?;
'arm: for MatchArm(pat, expr) in arms { for MatchArm(pat, expr) in arms {
if let Ok(substitution) = assignment::pattern_substitution(pat, scrutinee.clone()) { if let Ok(substitution) = pattern::substitution(pat, scrutinee.clone()) {
let mut env = env.frame("match"); let mut env = env.frame("match");
for (path, value) in substitution { for (name, value) in substitution {
let [PathPart::Ident(name)] = path.parts.as_slice() else {
continue 'arm;
};
env.insert(*name, Some(value)); env.insert(*name, Some(value));
} }
return expr.interpret(&mut env); return expr.interpret(&mut env);
@ -292,127 +365,11 @@ mod assignment {
use std::collections::HashMap; use std::collections::HashMap;
type Namespace = HashMap<Sym, Option<ConValue>>; type Namespace = HashMap<Sym, Option<ConValue>>;
/// Gets the path variables in the given Pattern
pub fn pattern_variables(pat: &Pattern) -> Vec<&Path> {
fn patvars<'p>(set: &mut Vec<&'p Path>, pat: &'p Pattern) {
match pat {
Pattern::Path(path) if path.is_sinkhole() => {}
Pattern::Path(path) => set.push(path),
Pattern::Literal(_) => {}
Pattern::Ref(_, pattern) => patvars(set, pattern),
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),
});
}
}
}
let mut set = Vec::new();
patvars(&mut set, pat);
set
}
/// Appends a substitution to the provided table
pub fn append_sub<'pat>(
env: &mut HashMap<&'pat Path, ConValue>,
pat: &'pat Pattern,
value: ConValue,
) -> IResult<()> {
match pat {
Pattern::Path(path) if path.is_sinkhole() => Ok(()),
Pattern::Path(path) => {
env.insert(path, value);
Ok(())
}
Pattern::Literal(literal) => match (literal, value) {
(Literal::Bool(a), ConValue::Bool(b)) => *a == b,
(Literal::Char(a), ConValue::Char(b)) => *a == b,
(Literal::Int(a), ConValue::Int(b)) => *a as isize == b,
(Literal::Float(a), ConValue::Float(b)) => f64::from_bits(*a) == b,
(Literal::String(a), ConValue::String(b)) => *a == *b,
_ => false,
}
.then_some(())
.ok_or(Error::NotAssignable),
Pattern::Ref(_, pattern) => match value {
ConValue::Ref(value) => append_sub(env, pattern, Rc::unwrap_or_clone(value)),
_ => Err(Error::NotAssignable),
},
Pattern::Tuple(patterns) => match value {
ConValue::Tuple(values) => {
if patterns.len() != values.len() {
return Err(Error::OobIndex(patterns.len(), values.len()));
};
for (pat, value) in patterns.iter().zip(Vec::from(values).into_iter()) {
append_sub(env, pat, value)?;
}
Ok(())
}
_ => Err(Error::NotAssignable),
},
Pattern::Array(patterns) => match value {
ConValue::Array(values) => {
if patterns.len() != values.len() {
return Err(Error::OobIndex(patterns.len(), values.len()));
};
for (pat, value) in patterns.iter().zip(Vec::from(values).into_iter()) {
append_sub(env, pat, value)?;
}
Ok(())
}
_ => Err(Error::NotAssignable),
},
Pattern::Struct(_path, patterns) => {
let ConValue::Struct(parts) = value else {
return Err(Error::TypeError);
};
let (_, mut values) = *parts;
if values.len() != patterns.len() {
return Err(Error::TypeError);
}
for (name, pat) in patterns {
let [.., PathPart::Ident(index)] = name.parts.as_slice() else {
Err(Error::TypeError)?
};
let value = values.remove(index).ok_or(Error::TypeError)?;
match pat {
Some(pat) => append_sub(env, pat, value)?,
None => {
env.insert(name, value);
}
}
}
Ok(())
}
}
}
/// Constructs a substitution from a pattern and a value
pub fn pattern_substitution(
pat: &Pattern,
value: ConValue,
) -> IResult<HashMap<&Path, ConValue>> {
let mut substitution = HashMap::new();
append_sub(&mut substitution, pat, value)?;
Ok(substitution)
}
pub(super) fn pat_assign(env: &mut Environment, pat: &Pattern, value: ConValue) -> IResult<()> { pub(super) fn pat_assign(env: &mut Environment, pat: &Pattern, value: ConValue) -> IResult<()> {
let mut substitution = HashMap::new(); for (name, value) in
append_sub(&mut substitution, pat, value) pattern::substitution(pat, value).map_err(|_| Error::PatFailed(pat.clone().into()))?
.map_err(|_| Error::PatFailed(pat.clone().into()))?; {
for (path, value) in substitution { *env.get_mut(*name)? = Some(value);
assign_path(env, path, value)?;
} }
Ok(()) Ok(())
} }
@ -424,20 +381,12 @@ mod assignment {
match pat { match pat {
ExprKind::Member(member) => *addrof_member(env, member)? = value, ExprKind::Member(member) => *addrof_member(env, member)? = value,
ExprKind::Index(index) => *addrof_index(env, index)? = value, ExprKind::Index(index) => *addrof_index(env, index)? = value,
ExprKind::Path(path) => *addrof_path(env, &path.parts)? = Some(value),
_ => Err(Error::NotAssignable)?, _ => Err(Error::NotAssignable)?,
} }
Ok(()) Ok(())
} }
fn assign_path(env: &mut Environment, path: &Path, value: ConValue) -> IResult<()> {
let Ok(addr) = addrof_path(env, &path.parts) else {
eprintln!("Cannot assign {value} to path {path}");
return Err(Error::NotAssignable);
};
*addr = Some(value);
Ok(())
}
pub(super) fn addrof<'e>( pub(super) fn addrof<'e>(
env: &'e mut Environment, env: &'e mut Environment,
pat: &ExprKind, pat: &ExprKind,
@ -461,14 +410,17 @@ mod assignment {
match path { match path {
[PathPart::Ident(name)] => env.get_mut(*name), [PathPart::Ident(name)] => env.get_mut(*name),
[PathPart::Ident(name), rest @ ..] => match env.get_mut(*name)? { [PathPart::Ident(name), rest @ ..] => match env.get_mut(*name)? {
Some(ConValue::Module(env)) => addrof_path_within_namespace(env, rest), Some(ConValue::Module(env)) => project_path_in_namespace(env, rest),
_ => Err(Error::NotIndexable), _ => Err(Error::NotIndexable),
}, },
_ => Err(Error::NotAssignable), _ => Err(Error::NotAssignable),
} }
} }
fn addrof_member<'e>(env: &'e mut Environment, member: &Member) -> IResult<&'e mut ConValue> { pub fn addrof_member<'e>(
env: &'e mut Environment,
member: &Member,
) -> IResult<&'e mut ConValue> {
let Member { head, kind } = member; let Member { head, kind } = member;
let ExprKind::Path(path) = head.as_ref() else { let ExprKind::Path(path) = head.as_ref() else {
return Err(Error::TypeError); return Err(Error::TypeError);
@ -476,15 +428,7 @@ mod assignment {
let slot = addrof_path(env, &path.parts)? let slot = addrof_path(env, &path.parts)?
.as_mut() .as_mut()
.ok_or(Error::NotAssignable)?; .ok_or(Error::NotAssignable)?;
Ok(match (slot, kind) { project_memberkind(slot, kind)
(ConValue::Struct(s), MemberKind::Struct(id)) => {
s.1.get_mut(id).ok_or(Error::NotDefined(*id))?
}
(ConValue::Tuple(t), MemberKind::Tuple(Literal::Int(id))) => t
.get_mut(*id as usize)
.ok_or_else(|| Error::NotDefined(id.to_string().into()))?,
_ => Err(Error::TypeError)?,
})
} }
fn addrof_index<'e>(env: &'e mut Environment, index: &Index) -> IResult<&'e mut ConValue> { fn addrof_index<'e>(env: &'e mut Environment, index: &Index) -> IResult<&'e mut ConValue> {
@ -495,19 +439,50 @@ mod assignment {
.collect::<IResult<Vec<_>>>()?; .collect::<IResult<Vec<_>>>()?;
let mut head = addrof(env, head)?; let mut head = addrof(env, head)?;
for index in indices { for index in indices {
head = match (head, index) { head = project_index(head, &index)?;
(ConValue::Array(a), ConValue::Int(i)) => {
let a_len = a.len();
a.get_mut(i as usize)
.ok_or(Error::OobIndex(i as usize, a_len))?
}
_ => Err(Error::NotIndexable)?,
}
} }
Ok(head) Ok(head)
} }
pub fn addrof_path_within_namespace<'e>( /// Performs member-access "projection" from a ConValue to a particular element
pub fn project_memberkind<'v>(
value: &'v mut ConValue,
kind: &MemberKind,
) -> IResult<&'v mut ConValue> {
match (value, kind) {
(ConValue::Struct(s), MemberKind::Struct(id)) => {
s.1.get_mut(id).ok_or(Error::NotDefined(*id))
}
(ConValue::TupleStruct(s), MemberKind::Tuple(Literal::Int(id))) => {
let len = s.1.len();
s.1.get_mut(*id as usize)
.ok_or(Error::OobIndex(*id as _, len))
}
(ConValue::Tuple(t), MemberKind::Tuple(Literal::Int(id))) => {
let len = t.len();
t.get_mut(*id as usize)
.ok_or(Error::OobIndex(*id as _, len))
}
_ => Err(Error::TypeError),
}
}
/// Performs index "projection" from a ConValue to a particular element
pub fn project_index<'v>(
value: &'v mut ConValue,
index: &ConValue,
) -> IResult<&'v mut ConValue> {
match (value, index) {
(ConValue::Array(a), ConValue::Int(i)) => {
let a_len = a.len();
a.get_mut(*i as usize)
.ok_or(Error::OobIndex(*i as usize, a_len))
}
_ => Err(Error::NotIndexable),
}
}
pub fn project_path_in_namespace<'e>(
env: &'e mut Namespace, env: &'e mut Namespace,
path: &[PathPart], path: &[PathPart],
) -> IResult<&'e mut Option<ConValue>> { ) -> IResult<&'e mut Option<ConValue>> {
@ -516,11 +491,11 @@ mod assignment {
[PathPart::Ident(name)] => env.get_mut(name).ok_or(Error::NotDefined(*name)), [PathPart::Ident(name)] => env.get_mut(name).ok_or(Error::NotDefined(*name)),
[PathPart::Ident(name), rest @ ..] => { [PathPart::Ident(name), rest @ ..] => {
match env.get_mut(name).ok_or(Error::NotDefined(*name))? { match env.get_mut(name).ok_or(Error::NotDefined(*name))? {
Some(ConValue::Module(env)) => addrof_path_within_namespace(env, rest), Some(ConValue::Module(env)) => project_path_in_namespace(env, rest),
_ => Err(Error::NotIndexable), _ => Err(Error::NotIndexable),
} }
} }
[PathPart::SelfKw, rest @ ..] => addrof_path_within_namespace(env, rest), [PathPart::SelfKw, rest @ ..] => project_path_in_namespace(env, rest),
[PathPart::SelfTy, ..] => todo!("calc_address for `Self`"), [PathPart::SelfTy, ..] => todo!("calc_address for `Self`"),
[PathPart::SuperKw, ..] => todo!("calc_address for `super`"), [PathPart::SuperKw, ..] => todo!("calc_address for `super`"),
} }
@ -566,8 +541,6 @@ impl Interpret for Binary {
let (head, tail) = parts.borrow(); let (head, tail) = parts.borrow();
let head = head.interpret(env)?; let head = head.interpret(env)?;
// Short-circuiting ops
match kind { match kind {
BinaryKind::LogAnd => { BinaryKind::LogAnd => {
return if head.truthy()? { return if head.truthy()? {
@ -590,6 +563,7 @@ impl Interpret for Binary {
} }
_ => {} _ => {}
} }
let tail = tail.interpret(env)?; let tail = tail.interpret(env)?;
match kind { match kind {
BinaryKind::Lt => head.lt(&tail), BinaryKind::Lt => head.lt(&tail),
@ -598,8 +572,8 @@ impl Interpret for Binary {
BinaryKind::NotEq => head.neq(&tail), BinaryKind::NotEq => head.neq(&tail),
BinaryKind::GtEq => head.gt_eq(&tail), BinaryKind::GtEq => head.gt_eq(&tail),
BinaryKind::Gt => head.gt(&tail), BinaryKind::Gt => head.gt(&tail),
BinaryKind::RangeExc => head.range_exc(tail), BinaryKind::RangeExc => env.call("RangeExc".into(), &[head, tail]),
BinaryKind::RangeInc => head.range_inc(tail), BinaryKind::RangeInc => env.call("RangeInc".into(), &[head, tail]),
BinaryKind::BitAnd => head & tail, BinaryKind::BitAnd => head & tail,
BinaryKind::BitOr => head | tail, BinaryKind::BitOr => head | tail,
BinaryKind::BitXor => head ^ tail, BinaryKind::BitXor => head ^ tail,
@ -617,36 +591,6 @@ impl Interpret for Binary {
}, },
_ => Ok(head), _ => Ok(head),
} }
// // Temporarily disabled, to avoid function dispatch overhead while I screw around
// // Not like it helped much in the first place!
// match kind {
// BinaryKind::Mul => env.call("mul", &[head, tail]),
// BinaryKind::Div => env.call("div", &[head, tail]),
// BinaryKind::Rem => env.call("rem", &[head, tail]),
// BinaryKind::Add => env.call("add", &[head, tail]),
// BinaryKind::Sub => env.call("sub", &[head, tail]),
// BinaryKind::Shl => env.call("shl", &[head, tail]),
// BinaryKind::Shr => env.call("shr", &[head, tail]),
// BinaryKind::BitAnd => env.call("and", &[head, tail]),
// BinaryKind::BitOr => env.call("or", &[head, tail]),
// BinaryKind::BitXor => env.call("xor", &[head, tail]),
// BinaryKind::RangeExc => env.call("range_exc", &[head, tail]),
// BinaryKind::RangeInc => env.call("range_inc", &[head, tail]),
// BinaryKind::Lt => env.call("lt", &[head, tail]),
// BinaryKind::LtEq => env.call("lt_eq", &[head, tail]),
// BinaryKind::Equal => env.call("eq", &[head, tail]),
// BinaryKind::NotEq => env.call("neq", &[head, tail]),
// BinaryKind::GtEq => env.call("gt_eq", &[head, tail]),
// BinaryKind::Gt => env.call("gt", &[head, tail]),
// BinaryKind::Dot => todo!("search within a type's namespace!"),
// BinaryKind::Call => match tail {
// ConValue::Empty => head.call(env, &[]),
// ConValue::Tuple(args) => head.call(env, &args),
// _ => Err(Error::TypeError),
// },
// _ => Ok(head),
// }
} }
} }
@ -673,6 +617,14 @@ impl Interpret for Unary {
let operand = tail.interpret(env)?; let operand = tail.interpret(env)?;
env.call("not".into(), &[operand]) env.call("not".into(), &[operand])
} }
UnaryKind::RangeExc => {
let operand = tail.interpret(env)?;
env.call("RangeTo".into(), &[operand])
}
UnaryKind::RangeInc => {
let operand = tail.interpret(env)?;
env.call("RangeToInc".into(), &[operand])
}
UnaryKind::At => { UnaryKind::At => {
let operand = tail.interpret(env)?; let operand = tail.interpret(env)?;
println!("{operand}"); println!("{operand}");
@ -732,16 +684,14 @@ impl Interpret for Cast {
impl Interpret for Member { impl Interpret for Member {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Member { head, kind } = self; let Member { head, kind } = self;
if let ExprKind::Path(_) = head.as_ref() {
return assignment::addrof_member(env, self).cloned();
}
let head = head.interpret(env)?; let head = head.interpret(env)?;
match (head, kind) { match (head, kind) {
(ConValue::Tuple(v), MemberKind::Tuple(Literal::Int(id))) => v (ConValue::Struct(parts), MemberKind::Call(name, args))
.get(*id as usize) if parts.1.contains_key(name) =>
.cloned() {
.ok_or(Error::OobIndex(*id as usize, v.len())),
(ConValue::Struct(parts), MemberKind::Struct(name)) => {
parts.1.get(name).cloned().ok_or(Error::NotDefined(*name))
}
(ConValue::Struct(parts), MemberKind::Call(name, args)) => {
let mut values = vec![]; let mut values = vec![];
for arg in &args.exprs { for arg in &args.exprs {
values.push(arg.interpret(env)?); values.push(arg.interpret(env)?);
@ -759,7 +709,7 @@ impl Interpret for Member {
} }
env.call(*name, &values) env.call(*name, &values)
} }
_ => Err(Error::TypeError)?, (mut head, kind) => assignment::project_memberkind(&mut head, kind).cloned(),
} }
} }
} }
@ -909,20 +859,35 @@ impl Interpret for If {
} }
impl Interpret for For { impl Interpret for For {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Self { bind: name, cond, pass, fail } = self; let Self { bind, cond, pass, fail } = self;
let cond = cond.interpret(env)?; let cond = cond.interpret(env)?;
// TODO: A better iterator model // TODO: A better iterator model
let mut bounds: Box<dyn Iterator<Item = ConValue>> = match &cond { let mut bounds: Box<dyn Iterator<Item = ConValue>> = match &cond {
&ConValue::RangeExc(a, b) => Box::new((a..b).map(ConValue::Int)), ConValue::TupleStruct(inner) => match &**inner {
&ConValue::RangeInc(a, b) => Box::new((a..=b).map(ConValue::Int)), ("RangeExc", values) => match **values {
[ConValue::Int(from), ConValue::Int(to)] => {
Box::new((from..to).map(ConValue::Int))
}
_ => Err(Error::NotIterable)?,
},
("RangeInc", values) => match **values {
[ConValue::Int(from), ConValue::Int(to)] => {
Box::new((from..=to).map(ConValue::Int))
}
_ => Err(Error::NotIterable)?,
},
_ => Err(Error::NotIterable)?,
},
ConValue::Array(a) => Box::new(a.iter().cloned()), ConValue::Array(a) => Box::new(a.iter().cloned()),
ConValue::String(s) => Box::new(s.chars().map(ConValue::Char)), ConValue::String(s) => Box::new(s.chars().map(ConValue::Char)),
_ => Err(Error::TypeError)?, _ => Err(Error::TypeError)?,
}; };
loop { loop {
let mut env = env.frame("loop variable"); let mut env = env.frame("loop variable");
if let Some(loop_var) = bounds.next() { if let Some(value) = bounds.next() {
env.insert(*name, Some(loop_var)); for (name, value) in pattern::substitution(bind, value)? {
env.insert(*name, Some(value));
}
match pass.interpret(&mut env) { match pass.interpret(&mut env) {
Err(Error::Break(value)) => return Ok(value), Err(Error::Break(value)) => return Ok(value),
Err(Error::Continue) => continue, Err(Error::Continue) => continue,

View File

@ -25,6 +25,8 @@ pub mod function;
pub mod builtin; pub mod builtin;
pub mod pattern;
pub mod env; pub mod env;
pub mod error; pub mod error;

View File

@ -0,0 +1,192 @@
//! Unification algorithm for cl-ast patterns and ConValues
//!
//! [`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,
error::{Error, IResult},
};
use cl_ast::{Literal, Pattern, Sym};
use std::{
collections::{HashMap, VecDeque},
rc::Rc,
};
/// 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 == "_" => {}
Pattern::Name(name) => set.push(name),
Pattern::Literal(_) => {}
Pattern::Rest(Some(pattern)) => patvars(set, pattern),
Pattern::Rest(None) => {}
Pattern::Ref(_, pattern) => patvars(set, pattern),
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>(
sub: &mut HashMap<&'pat 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(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(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())))),
}
}
/// Appends a substitution to the provided table
pub fn append_sub<'pat>(
sub: &mut HashMap<&'pat Sym, ConValue>,
pat: &'pat Pattern,
value: ConValue,
) -> IResult<()> {
match (pat, value) {
(Pattern::Literal(Literal::Bool(a)), ConValue::Bool(b)) => {
(*a == b).then_some(()).ok_or(Error::NotAssignable)
}
(Pattern::Literal(Literal::Char(a)), ConValue::Char(b)) => {
(*a == b).then_some(()).ok_or(Error::NotAssignable)
}
(Pattern::Literal(Literal::Float(a)), ConValue::Float(b)) => (f64::from_bits(*a) == b)
.then_some(())
.ok_or(Error::NotAssignable),
(Pattern::Literal(Literal::Int(a)), ConValue::Int(b)) => {
(b == *a as _).then_some(()).ok_or(Error::NotAssignable)
}
(Pattern::Literal(Literal::String(a)), ConValue::String(b)) => {
(*a == *b).then_some(()).ok_or(Error::NotAssignable)
}
(Pattern::Literal(_), _) => Err(Error::NotAssignable),
(Pattern::Rest(Some(pat)), value) => match (pat.as_ref(), value) {
(Pattern::Literal(Literal::Int(a)), ConValue::Int(b)) => {
(b < *a as _).then_some(()).ok_or(Error::NotAssignable)
}
(Pattern::Literal(Literal::Char(a)), ConValue::Char(b)) => {
(b < *a as _).then_some(()).ok_or(Error::NotAssignable)
}
(Pattern::Literal(Literal::Bool(a)), ConValue::Bool(b)) => {
(!b & *a).then_some(()).ok_or(Error::NotAssignable)
}
(Pattern::Literal(Literal::Float(a)), ConValue::Float(b)) => {
(b < *a as _).then_some(()).ok_or(Error::NotAssignable)
}
(Pattern::Literal(Literal::String(a)), ConValue::String(b)) => {
(&*b < a).then_some(()).ok_or(Error::NotAssignable)
}
_ => Err(Error::NotAssignable)
},
(Pattern::Name(name), _) if "_".eq(&**name) => Ok(()),
(Pattern::Name(name), value) => {
sub.insert(name, value);
Ok(())
}
(Pattern::Ref(_, pat), ConValue::Ref(r)) => append_sub(sub, pat, Rc::unwrap_or_clone(r)),
(Pattern::Array(patterns), ConValue::Array(values)) => {
match rest_binding(sub, patterns, values.into_vec().into())? {
Some((pattern, values)) => {
append_sub(sub, pattern, ConValue::Array(Vec::from(values).into()))
}
_ => Ok(()),
}
}
(Pattern::Tuple(patterns), ConValue::Empty) if patterns.is_empty() => Ok(()),
(Pattern::Tuple(patterns), ConValue::Tuple(values)) => {
match rest_binding(sub, patterns, values.into_vec().into())? {
Some((pattern, values)) => {
append_sub(sub, pattern, ConValue::Tuple(Vec::from(values).into()))
}
_ => Ok(()),
}
}
(Pattern::TupleStruct(path, patterns), ConValue::TupleStruct(parts)) => {
let (name, values) = *parts;
if !path.ends_with(name) {
Err(Error::TypeError)?
}
match rest_binding(sub, patterns, values.into_vec().into())? {
Some((pattern, values)) => {
append_sub(sub, pattern, ConValue::Tuple(Vec::from(values).into()))
}
_ => Ok(()),
}
}
(Pattern::Struct(path, patterns), ConValue::Struct(parts)) => {
let (name, mut values) = *parts;
if !path.ends_with(&name) {
Err(Error::TypeError)?
}
for (name, pat) in patterns {
let value = values.remove(name).ok_or(Error::TypeError)?;
match pat {
Some(pat) => append_sub(sub, pat, value)?,
None => {
sub.insert(name, value);
}
}
}
Ok(())
}
(pat, value) => {
eprintln!("Could not match pattern `{pat}` with value `{value}`!");
Ok(())
}
}
}
/// Constructs a substitution from a pattern and a value
pub fn substitution(pat: &Pattern, value: ConValue) -> IResult<HashMap<&Sym, ConValue>> {
let mut sub = HashMap::new();
append_sub(&mut sub, pat, value)?;
Ok(sub)
}

View File

@ -1,5 +1,6 @@
use super::*; use super::*;
use cl_ast::ExprKind;
use cl_lexer::error::{Error as LexError, Reason}; use cl_lexer::error::{Error as LexError, Reason};
use std::fmt::Display; use std::fmt::Display;
pub type PResult<T> = Result<T, Error>; pub type PResult<T> = Result<T, Error>;
@ -29,6 +30,7 @@ pub enum ErrorKind {
ExpectedParsing { ExpectedParsing {
want: Parsing, want: Parsing,
}, },
InvalidPattern(Box<ExprKind>),
/// Indicates unfinished code /// Indicates unfinished code
Todo(&'static str), Todo(&'static str),
} }
@ -148,6 +150,7 @@ impl Display for ErrorKind {
ErrorKind::Unexpected(t) => write!(f, "Encountered unexpected token `{t}`"), ErrorKind::Unexpected(t) => write!(f, "Encountered unexpected token `{t}`"),
ErrorKind::ExpectedToken { want: e, got: g } => write!(f, "Expected `{e}`, got `{g}`"), ErrorKind::ExpectedToken { want: e, got: g } => write!(f, "Expected `{e}`, got `{g}`"),
ErrorKind::ExpectedParsing { want } => write!(f, "Expected {want}"), ErrorKind::ExpectedParsing { want } => write!(f, "Expected {want}"),
ErrorKind::InvalidPattern(got) => write!(f, "Got invalid `{got}`"),
ErrorKind::Todo(unfinished) => write!(f, "TODO: {unfinished}"), ErrorKind::Todo(unfinished) => write!(f, "TODO: {unfinished}"),
} }
} }

View File

@ -81,18 +81,32 @@ impl Fold for ModuleInliner {
self.path.set_extension("cl"); self.path.set_extension("cl");
let file = match std::fs::read_to_string(&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);
};
let path = self
.path
.parent()
.and_then(Path::parent)
.map(|path| path.join(basename))
.unwrap_or_default();
match std::fs::read_to_string(&path) {
Err(error) => return self.handle_io_error(error), Err(error) => return self.handle_io_error(error),
Ok(file) => file, Ok(file) => file,
}
}
Ok(file) => file,
}; };
let kind = match Parser::new(Lexer::new(&file)).parse() { match Parser::new(Lexer::new(&file)).parse() {
Err(e) => return self.handle_parse_error(e), Err(e) => self.handle_parse_error(e),
Ok(file) => ModuleKind::Inline(file), Ok(file) => {
};
// cd path/mod
self.path.set_extension(""); self.path.set_extension("");
// The newly loaded module may need further inlining // The newly loaded module may need further inlining
self.fold_module_kind(kind) ModuleKind::Inline(self.fold_file(file))
}
}
} }
} }

View File

@ -459,18 +459,17 @@ impl Parse<'_> for Function {
sign, sign,
bind, bind,
body: match p.peek_kind(P)? { body: match p.peek_kind(P)? {
TokenKind::LCurly => Some(Block::parse(p)?),
TokenKind::Semi => { TokenKind::Semi => {
p.consume_peeked(); p.consume_peeked();
None None
} }
t => Err(p.error(Unexpected(t), P))?, _ => Some(Expr::parse(p)?),
}, },
}) })
} }
} }
type FnSig = (Vec<Param>, Vec<TyKind>); type FnSig = (Vec<Pattern>, Vec<TyKind>);
impl Parse<'_> for FnSig { impl Parse<'_> for FnSig {
/// Parses the [parameters](Param) associated with a Function /// Parses the [parameters](Param) associated with a Function
@ -489,16 +488,17 @@ impl Parse<'_> for FnSig {
} }
} }
type TypedParam = (Param, TyKind); type TypedParam = (Pattern, TyKind);
impl Parse<'_> for TypedParam { impl Parse<'_> for TypedParam {
/// Parses a single function [parameter](Param) /// Parses a single function [parameter](Param)
fn parse(p: &mut Parser) -> PResult<(Param, TyKind)> { fn parse(p: &mut Parser) -> PResult<(Pattern, TyKind)> {
Ok(( Ok((
Param { mutability: Mutability::parse(p)?, name: Sym::parse(p)? }, Pattern::parse(p)?,
{ if p.match_type(TokenKind::Colon, Parsing::Param).is_ok() {
p.match_type(TokenKind::Colon, Parsing::Param)?;
TyKind::parse(p)? TyKind::parse(p)?
} else {
TyKind::Path(Path::from(Sym::from("_")))
}, },
)) ))
} }
@ -1007,13 +1007,20 @@ impl Parse<'_> for Block {
} }
} }
/// Conditions (which precede curly-braced blocks) get special treatment
fn condition(p: &mut Parser) -> PResult<Expr> {
let start = p.loc();
let kind = prec::exprkind(p, prec::Precedence::Condition.level())?;
Ok(Expr { kind, extents: Span(start, p.loc()) })
}
impl Parse<'_> for While { impl Parse<'_> for While {
/// [While] = `while` [Expr] [Block] [Else]? /// [While] = `while` [Expr] [Block] [Else]?
#[rustfmt::skip] #[rustfmt::skip]
fn parse(p: &mut Parser) -> PResult<While> { fn parse(p: &mut Parser) -> PResult<While> {
p.match_type(TokenKind::While, Parsing::While)?; p.match_type(TokenKind::While, Parsing::While)?;
Ok(While { Ok(While {
cond: Expr::parse(p)?.into(), cond: condition(p)?.into(),
pass: Block::parse(p)?.into(), pass: Block::parse(p)?.into(),
fail: Else::parse(p)? fail: Else::parse(p)?
}) })
@ -1026,7 +1033,7 @@ impl Parse<'_> for If {
fn parse(p: &mut Parser) -> PResult<If> { fn parse(p: &mut Parser) -> PResult<If> {
p.match_type(TokenKind::If, Parsing::If)?; p.match_type(TokenKind::If, Parsing::If)?;
Ok(If { Ok(If {
cond: Expr::parse(p)?.into(), cond: condition(p)?.into(),
pass: Block::parse(p)?.into(), pass: Block::parse(p)?.into(),
fail: Else::parse(p)?, fail: Else::parse(p)?,
}) })
@ -1034,15 +1041,15 @@ impl Parse<'_> for If {
} }
impl Parse<'_> for For { impl Parse<'_> for For {
/// [For]: `for` Pattern (TODO) `in` [Expr] [Block] [Else]? /// [For]: `for` [Pattern] `in` [Expr] [Block] [Else]?
#[rustfmt::skip] #[rustfmt::skip]
fn parse(p: &mut Parser) -> PResult<For> { fn parse(p: &mut Parser) -> PResult<For> {
p.match_type(TokenKind::For, Parsing::For)?; p.match_type(TokenKind::For, Parsing::For)?;
let bind = Sym::parse(p)?; let bind = Pattern::parse(p)?;
p.match_type(TokenKind::In, Parsing::For)?; p.match_type(TokenKind::In, Parsing::For)?;
Ok(For { Ok(For {
bind, bind,
cond: Expr::parse(p)?.into(), cond: condition(p)?.into(),
pass: Block::parse(p)?.into(), pass: Block::parse(p)?.into(),
fail: Else::parse(p)?, fail: Else::parse(p)?,
}) })
@ -1081,9 +1088,8 @@ impl Parse<'_> for Return {
impl Parse<'_> for Pattern { impl Parse<'_> for Pattern {
fn parse(p: &mut Parser<'_>) -> PResult<Self> { fn parse(p: &mut Parser<'_>) -> PResult<Self> {
let value = prec::exprkind(p, prec::Precedence::Highest.level())?; let value = prec::exprkind(p, prec::Precedence::Pattern.level())?;
Pattern::try_from(value) Pattern::try_from(value).map_err(|e| p.error(InvalidPattern(e.into()), Parsing::Pattern))
.map_err(|_| p.error(ExpectedParsing { want: Parsing::Pattern }, Parsing::Pattern))
} }
} }
@ -1091,7 +1097,7 @@ impl Parse<'_> for Match {
/// [Match] = `match` [Expr] `{` [MatchArm],* `}` /// [Match] = `match` [Expr] `{` [MatchArm],* `}`
fn parse(p: &mut Parser<'_>) -> PResult<Self> { fn parse(p: &mut Parser<'_>) -> PResult<Self> {
p.match_type(TokenKind::Match, Parsing::Match)?; p.match_type(TokenKind::Match, Parsing::Match)?;
let scrutinee = Expr::parse(p)?.into(); let scrutinee = condition(p)?.into();
let arms = delim( let arms = delim(
sep(MatchArm::parse, TokenKind::Comma, CURLIES.1, Parsing::Match), sep(MatchArm::parse, TokenKind::Comma, CURLIES.1, Parsing::Match),
CURLIES, CURLIES,

View File

@ -44,7 +44,9 @@ pub fn exprkind(p: &mut Parser, power: u8) -> PResult<ExprKind> {
Some(match op { Some(match op {
TokenKind::LBrack => Precedence::Index, TokenKind::LBrack => Precedence::Index,
TokenKind::LParen => Precedence::Call, TokenKind::LParen => Precedence::Call,
TokenKind::LCurly => Precedence::Structor,
TokenKind::Dot => Precedence::Member, TokenKind::Dot => Precedence::Member,
TokenKind::As => Precedence::Cast,
_ => None?, _ => None?,
}) })
} }
@ -55,25 +57,36 @@ pub fn exprkind(p: &mut Parser, power: u8) -> PResult<ExprKind> {
if before < power { if before < power {
break; break;
} }
p.consume_peeked();
head = match op { head = match op {
TokenKind::LBrack => { TokenKind::LBrack => {
p.consume_peeked();
let indices = let indices =
sep(Expr::parse, TokenKind::Comma, TokenKind::RBrack, parsing)(p)?; sep(Expr::parse, TokenKind::Comma, TokenKind::RBrack, parsing)(p)?;
p.match_type(TokenKind::RBrack, parsing)?; p.match_type(TokenKind::RBrack, parsing)?;
ExprKind::Index(Index { head: head.into(), indices }) ExprKind::Index(Index { head: head.into(), indices })
} }
TokenKind::LParen => { TokenKind::LParen => {
p.consume_peeked();
let exprs = sep(Expr::parse, TokenKind::Comma, TokenKind::RParen, parsing)(p)?; let exprs = sep(Expr::parse, TokenKind::Comma, TokenKind::RParen, parsing)(p)?;
p.match_type(TokenKind::RParen, parsing)?; p.match_type(TokenKind::RParen, parsing)?;
Binary { kind: BinaryKind::Call, parts: (head, Tuple { exprs }.into()).into() } Binary { kind: BinaryKind::Call, parts: (head, Tuple { exprs }.into()).into() }
.into() .into()
} }
TokenKind::LCurly => match head {
ExprKind::Path(path) => ExprKind::Structor(structor_body(p, path)?),
_ => break,
},
TokenKind::Dot => { TokenKind::Dot => {
p.consume_peeked();
let kind = MemberKind::parse(p)?; let kind = MemberKind::parse(p)?;
Member { head: Box::new(head), kind }.into() 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))?, _ => Err(p.error(Unexpected(op), parsing))?,
}; };
continue; continue;
@ -217,11 +230,7 @@ fn exprkind_group(p: &mut Parser) -> PResult<ExprKind> {
/// Parses an expression beginning with a [Path] (i.e. [Path] or [Structor]) /// Parses an expression beginning with a [Path] (i.e. [Path] or [Structor])
fn exprkind_pathlike(p: &mut Parser) -> PResult<ExprKind> { fn exprkind_pathlike(p: &mut Parser) -> PResult<ExprKind> {
let head = Path::parse(p)?; Path::parse(p).map(Into::into)
Ok(match p.match_type(TokenKind::Colon, Parsing::Path) {
Ok(_) => ExprKind::Structor(structor_body(p, head)?),
Err(_) => ExprKind::Path(head),
})
} }
/// [Structor]Body = `{` ([Fielder] `,`)* [Fielder]? `}` /// [Structor]Body = `{` ([Fielder] `,`)* [Fielder]? `}`
@ -244,6 +253,9 @@ fn structor_body(p: &mut Parser, to: Path) -> PResult<Structor> {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Precedence { pub enum Precedence {
Assign, Assign,
Pattern, // A pattern can contain a structor
Structor, // A structor is never a valid conditional
Condition, // Anything that syntactically needs a block following it
Logic, Logic,
Compare, Compare,
Range, Range,
@ -256,7 +268,6 @@ pub enum Precedence {
Cast, Cast,
Member, // left-associative Member, // left-associative
Call, Call,
Highest,
} }
impl Precedence { impl Precedence {
@ -284,7 +295,9 @@ impl Precedence {
pub fn postfix(self) -> Option<(u8, ())> { pub fn postfix(self) -> Option<(u8, ())> {
match self { match self {
Self::Index | Self::Call | Self::Member => Some((self.level(), ())), Self::Structor | Self::Index | Self::Call | Self::Member | Self::Cast => {
Some((self.level(), ()))
}
_ => None, _ => None,
} }
} }
@ -317,7 +330,7 @@ impl From<UnaryKind> for Precedence {
use UnaryKind as Op; use UnaryKind as Op;
match value { match value {
Op::Loop => Precedence::Assign, Op::Loop => Precedence::Assign,
Op::Deref | Op::Neg | Op::Not | Op::At | Op::Tilde => Precedence::Unary, _ => Precedence::Unary,
} }
} }
} }
@ -338,6 +351,8 @@ operator! {
Star => Deref, Star => Deref,
Minus => Neg, Minus => Neg,
Bang => Not, Bang => Not,
DotDot => RangeExc,
DotDotEq => RangeInc,
At => At, At => At,
Tilde => Tilde, Tilde => Tilde,
}; };

View File

@ -428,8 +428,9 @@ pub mod yamlify {
impl Yamlify for Pattern { impl Yamlify for Pattern {
fn yaml(&self, y: &mut Yamler) { fn yaml(&self, y: &mut Yamler) {
match self { match self {
Pattern::Path(path) => y.value(path), Pattern::Name(name) => y.value(name),
Pattern::Literal(literal) => y.value(literal), Pattern::Literal(literal) => y.value(literal),
Pattern::Rest(name) => y.pair("Rest", name),
Pattern::Ref(mutability, pattern) => { Pattern::Ref(mutability, pattern) => {
y.pair("mutability", mutability).pair("subpattern", pattern) y.pair("mutability", mutability).pair("subpattern", pattern)
} }
@ -438,13 +439,20 @@ pub mod yamlify {
Pattern::Struct(path, items) => { Pattern::Struct(path, items) => {
{ {
let mut y = y.key("Struct"); let mut y = y.key("Struct");
y.pair("name", path); y.yaml(path);
for (name, item) in items { for (name, item) in items {
y.pair(name, item); y.pair(name, item);
} }
} }
y y
} }
Pattern::TupleStruct(path, items) => {
{
let mut y = y.key("TupleStruct");
y.yaml(path).list(items);
}
y
}
}; };
} }
} }
@ -634,12 +642,6 @@ pub mod yamlify {
y.value(self); 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 { impl Yamlify for Ty {
fn yaml(&self, y: &mut Yamler) { fn yaml(&self, y: &mut Yamler) {
let Self { extents: _, kind } = self; let Self { extents: _, kind } = self;

View File

@ -72,8 +72,7 @@ pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
} }
fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue, Box<dyn Error>> { fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue, Box<dyn Error>> {
let inliner = let inliner = cl_parser::inliner::ModuleInliner::new(path.as_ref().with_extension(""));
cl_parser::inliner::ModuleInliner::new(path.as_ref().parent().unwrap_or(Path::new("")));
let file = std::fs::read_to_string(path)?; let file = std::fs::read_to_string(path)?;
let code = Parser::new(Lexer::new(&file)).parse()?; let code = Parser::new(Lexer::new(&file)).parse()?;
let code = match inliner.inline(code) { let code = match inliner.inline(code) {

View File

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

View File

@ -106,9 +106,9 @@ impl<'a> Visit<'a> for Populator<'_, 'a> {
self.set_name(*name); self.set_name(*name);
self.visit_ty_fn(sign); self.visit_ty_fn(sign);
bind.iter().for_each(|p| self.visit_param(p)); bind.iter().for_each(|p| self.visit_pattern(p));
if let Some(b) = body { if let Some(b) = body {
self.visit_block(b) self.visit_expr(b)
} }
} }

View File

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

View File

@ -112,6 +112,17 @@ impl<'a> Editor<'a> {
Ok(()) Ok(())
} }
pub fn print_err<W: Write>(&self, w: &mut W, err: impl Display) -> ReplResult<()> {
queue!(
w,
SavePosition,
Clear(ClearType::UntilNewLine),
Print(err),
RestorePosition
)?;
Ok(())
}
/// Prints the characters after the cursor on the current line. /// Prints the characters after the cursor on the current line.
pub fn print_tail<W: Write>(&self, w: &mut W) -> ReplResult<()> { pub fn print_tail<W: Write>(&self, w: &mut W) -> ReplResult<()> {
let Self { tail, .. } = self; let Self { tail, .. } = self;

View File

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

View File

@ -111,6 +111,15 @@ impl<'a, R: Read> Repline<'a, R> {
} }
} }
} }
/// Prints a message without moving the cursor
pub fn print_inline(&mut self, value: impl std::fmt::Display) -> ReplResult<()> {
let mut stdout = stdout().lock();
self.print_err(&mut stdout, value)
}
/// Prints a message (ideally an error) without moving the cursor
fn print_err<W: Write>(&mut self, w: &mut W, value: impl std::fmt::Display) -> ReplResult<()> {
self.ed.print_err(w, value)
}
/// Handle ANSI Escape /// Handle ANSI Escape
fn escape<W: Write>(&mut self, w: &mut W) -> ReplResult<()> { fn escape<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
match self.input.next().ok_or(Error::EndOfInput)?? { match self.input.next().ok_or(Error::EndOfInput)?? {

View File

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

View File

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

View File

@ -8,5 +8,7 @@ pub mod num;
pub mod str; pub mod str;
pub mod range;
#[cfg("test")] #[cfg("test")]
mod test; mod test;

View File

@ -278,9 +278,9 @@ pub mod ops {
} }
impl usize { impl usize {
pub const MIN: Self = __march_ptr_width_unsigned_min(); pub const MIN: Self = (); // __march_ptr_width_unsigned_min(); // TODO: intrinsics
pub const MAX: Self = __march_ptr_width_unsigned_max(); pub const MAX: Self = (); // __march_ptr_width_unsigned_max(); // TODO: intrinsics
pub const BIT_WIDTH: u32 = __march_ptr_width_bits(); pub const BIT_WIDTH: u32 = (); // __march_ptr_width_bits(); // TODO: intrinsics
pub fn default() -> Self { pub fn default() -> Self {
0 0
} }
@ -512,9 +512,9 @@ pub mod ops {
} }
impl isize { impl isize {
pub const MIN: Self = __march_ptr_width_signed_min(); pub const MIN: Self = (); // __march_ptr_width_signed_min(); // TODO: intrinsics
pub const MAX: Self = __march_ptr_width_signed_max(); pub const MAX: Self = (); // __march_ptr_width_signed_max(); // TODO: intrinsics
pub const BIT_WIDTH: u32 = __march_ptr_width_bits(); pub const BIT_WIDTH: u32 = (); // __march_ptr_width_bits(); // TODO: intrinsics
pub fn default() -> Self { pub fn default() -> Self {
0 0
} }

9
stdlib/std/range.cl Normal file
View File

@ -0,0 +1,9 @@
//! Iterable ranges
/// An Exclusive Range `a .. b` iterates from a to b, excluding b
#[intrinsic = "range_exc", T]
struct RangeExc(T, T)
/// An Inclusive Range `a ..= b` iterates from a to b, including b
#[intrinsic = "range_inc", T]
struct RangeInc(T, T)