Compare commits

..

1 Commits

Author SHA1 Message Date
3cb85c7f42 toki: pona 2025-02-19 04:04:40 -06:00
23 changed files with 442 additions and 558 deletions

View File

@ -146,14 +146,14 @@ pub struct Function {
pub name: Sym,
pub sign: TyFn,
pub bind: Vec<Param>,
pub body: Option<Expr>,
pub body: Option<Block>,
}
/// A single parameter for a [Function]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Param {
pub mutability: Mutability,
pub bind: Pattern,
pub name: Sym,
}
/// A user-defined product type
@ -418,13 +418,12 @@ pub struct Let {
/// A [Pattern] meta-expression (any [`ExprKind`] that fits pattern rules)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Pattern {
Name(Sym),
Path(Path),
Literal(Literal),
Ref(Mutability, Box<Pattern>),
Tuple(Vec<Pattern>),
Array(Vec<Pattern>),
Struct(Path, Vec<(Sym, Option<Pattern>)>),
TupleStruct(Path, Vec<Pattern>),
Struct(Path, Vec<(Path, Option<Pattern>)>),
}
/// A `match` expression: `match` `{` ([MatchArm] `,`)* [MatchArm]? `}`
@ -621,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,

View File

@ -9,8 +9,24 @@ mod display {
use std::{
borrow::Borrow,
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>(
iterable: impl IntoIterator<Item = I>,
sep: &'static str,
@ -30,7 +46,7 @@ mod display {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Mutability::Not => Ok(()),
Mutability::Mut => "mut ".fmt(f),
Mutability::Mut => keyword("ante ", f),
}
}
}
@ -39,19 +55,28 @@ mod display {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Visibility::Private => Ok(()),
Visibility::Public => "pub ".fmt(f),
Visibility::Public => keyword("lukin ", 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()),
fn fmt(this: &Literal, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match this {
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()),
}
}
if std::io::stdout().is_terminal() {
write!(f, "\x1b[94m")?;
fmt(self, f)?;
write!(f, "\x1b[0m")
} else {
fmt(self, f)
}
}
}
@ -77,7 +102,8 @@ mod display {
impl Display for Meta {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self;
write!(f, "{name}{kind}")
ident(name, f)?;
write!(f, "{kind}")
}
}
@ -119,9 +145,10 @@ mod display {
impl Display for Alias {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { to, from } = self;
keyword("ijo", f)?;
match from {
Some(from) => write!(f, "type {to} = {from};"),
None => write!(f, "type {to};"),
Some(from) => write!(f, " {to} = {from};"),
None => write!(f, " {to};"),
}
}
}
@ -129,21 +156,28 @@ mod display {
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}")
keyword("kiwen ", f)?;
ident(name, f)?;
write!(f, ": {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}")
keyword("mute", f)?;
write!(f, " {mutable}")?;
ident(name, f)?;
write!(f, ": {ty} = {init}")
}
}
impl Display for Module {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self;
write!(f, "mod {name}{kind}")
keyword("selo ", f)?;
ident(name, f)?;
write!(f, "{kind}")
}
}
@ -172,7 +206,10 @@ mod display {
};
debug_assert_eq!(bind.len(), types.len());
write!(f, "fn {name} ")?;
keyword("nasin", f)?;
" ".fmt(f)?;
ident(name, f)?;
" ".fmt(f)?;
{
let mut f = f.delimit(INLINE_PARENS);
for (idx, (arg, ty)) in bind.iter().zip(types.iter()).enumerate() {
@ -194,15 +231,18 @@ mod display {
impl Display for Param {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { mutability, bind } = self;
write!(f, "{mutability}{bind}")
let Self { mutability, name } = self;
write!(f, "{mutability}")?;
ident(name, f)
}
}
impl Display for Struct {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self;
write!(f, "struct {name}{kind}")
keyword("lipu ", f)?;
ident(name, f)?;
write!(f, "{kind}")
}
}
@ -219,14 +259,18 @@ mod display {
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}")
write!(f, "{vis}")?;
ident(name, f)?;
write!(f, ": {ty}")
}
}
impl Display for Enum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self;
write!(f, "enum {name}{kind}")
keyword("kulupu ", f)?;
ident(name, f)?;
write!(f, "{kind}")
}
}
@ -242,7 +286,8 @@ mod display {
impl Display for Variant {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self;
write!(f, "{name}{kind}")
ident(name, f)?;
write!(f, "{kind}")
}
}
@ -260,7 +305,8 @@ mod display {
impl Display for Impl {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { target, body } = self;
write!(f, "impl {target} ")?;
keyword("insa", f)?;
write!(f, " {target} ")?;
write!(f.delimit(BRACES), "{body}")
}
}
@ -270,7 +316,9 @@ mod display {
match self {
ImplKind::Type(t) => t.fmt(f),
ImplKind::Trait { impl_trait, for_type } => {
write!(f, "{impl_trait} for {for_type}")
write!(f, "{impl_trait} ")?;
keyword("ale", f)?;
write!(f, " {for_type}")
}
}
}
@ -279,7 +327,8 @@ mod display {
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 " })?;
keyword("jo", f)?;
f.write_str(if *absolute { " ::" } else { " " })?;
write!(f, "{tree};")
}
}
@ -289,8 +338,12 @@ mod display {
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::Alias(path, name) => {
write!(f, "{path} ")?;
keyword("sama ", f)?;
ident(name, f)
}
UseTree::Name(name) => ident(name, f),
UseTree::Glob => write!(f, "*"),
}
}
@ -350,7 +403,8 @@ mod display {
impl Display for TyFn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { args, rety } = self;
write!(f, "fn {args}")?;
keyword("nasin", f)?;
write!(f, " {args}")?;
match rety {
Some(v) => write!(f, " -> {v}"),
None => Ok(()),
@ -371,10 +425,10 @@ mod display {
impl Display for PathPart {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PathPart::SuperKw => "super".fmt(f),
PathPart::SelfKw => "self".fmt(f),
PathPart::SelfTy => "Self".fmt(f),
PathPart::Ident(id) => id.fmt(f),
PathPart::SuperKw => keyword("mama", f),
PathPart::SelfKw => keyword("mi", f),
PathPart::SelfTy => keyword("Mi", f),
PathPart::Ident(id) => ident(id, f),
}
}
}
@ -439,7 +493,7 @@ mod display {
ExprKind::For(v) => v.fmt(f),
ExprKind::Break(v) => v.fmt(f),
ExprKind::Return(v) => v.fmt(f),
ExprKind::Continue => "continue".fmt(f),
ExprKind::Continue => keyword("tama", f),
}
}
}
@ -454,7 +508,10 @@ mod display {
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}")?;
keyword("poki", f)?;
write!(f, " {mutable}")?;
ident(name, f)?;
if let Some(value) = ty {
write!(f, ": {value}")?;
}
@ -468,17 +525,17 @@ mod display {
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::Ref(mutability, pattern) => write!(f, "&{mutability}{pattern}"),
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);
write!(f, "{path}: ")?;
let f = &mut f.delimit(BRACES);
for (idx, (name, item)) in items.iter().enumerate() {
if idx != 0 {
f.write_str(", ")?;
f.write_str(",\n")?;
}
write!(f, "{name}")?;
if let Some(pattern) = item {
@ -487,10 +544,6 @@ mod display {
}
Ok(())
}
Pattern::TupleStruct(path, items) => {
write!(f, "{path}")?;
separate(items, ", ")(f.delimit(INLINE_PARENS))
}
}
}
}
@ -498,7 +551,8 @@ mod display {
impl Display for Match {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { scrutinee, arms } = self;
write!(f, "match {scrutinee} ")?;
keyword("seme", f)?;
write!(f, " {scrutinee} ")?;
separate(arms, ",\n")(f.delimit(BRACES))
}
}
@ -593,7 +647,7 @@ mod display {
impl Display for UnaryKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UnaryKind::Loop => "loop ",
UnaryKind::Loop => return keyword("awen ", f),
UnaryKind::Deref => "*",
UnaryKind::Neg => "-",
UnaryKind::Not => "!",
@ -607,7 +661,9 @@ mod display {
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}")
write!(f, "{head} ")?;
keyword("sama", f)?;
write!(f, " {ty}")
}
}
@ -621,9 +677,12 @@ mod display {
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}"),
MemberKind::Call(name, args) => {
ident(name, f)?;
separate(&args.exprs, ", ")(f.delimit(INLINE_PARENS))
}
MemberKind::Struct(name) => ident(name, f),
MemberKind::Tuple(name) => ident(name, f),
}
}
}
@ -639,7 +698,7 @@ mod display {
impl Display for Structor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { to, init } = self;
write!(f, "{to} ")?;
write!(f, "{to}: ")?;
separate(init, ", ")(f.delimit(INLINE_BRACES))
}
}
@ -647,7 +706,7 @@ mod display {
impl Display for Fielder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, init } = self;
write!(f, "{name}")?;
ident(name, f)?;
if let Some(init) = init {
write!(f, ": {init}")?;
}
@ -707,28 +766,35 @@ mod display {
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}")
write!(f, "lawa {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}")
keyword("tan", f)?;
write!(f, " {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}")
keyword("ale", f)?;
write!(f, " {bind} ")?;
keyword("lon", f)?;
write!(f, " {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}"),
Some(body) => {
keyword(" taso", f)?;
write!(f, " {body}")
}
_ => Ok(()),
}
}
@ -736,7 +802,7 @@ mod display {
impl Display for Break {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "break")?;
keyword("pana", f)?;
match &self.body {
Some(body) => write!(f, " {body}"),
_ => Ok(()),
@ -746,7 +812,7 @@ mod display {
impl Display for Return {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "return")?;
keyword("pini", f)?;
match &self.body {
Some(body) => write!(f, " {body}"),
_ => Ok(()),
@ -867,10 +933,7 @@ mod convert {
fn try_from(value: ExprKind) -> Result<Self, Self::Error> {
Ok(match value {
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::Path(path) => Pattern::Path(path),
ExprKind::Empty => Pattern::Tuple(vec![]),
ExprKind::Group(Group { expr }) => Pattern::Tuple(vec![Pattern::try_from(*expr)?]),
ExprKind::Tuple(Tuple { exprs }) => Pattern::Tuple(
@ -888,24 +951,16 @@ mod convert {
.map(|e| Pattern::try_from(e.kind))
.collect::<Result<_, _>>()?,
),
ExprKind::Binary(Binary { kind: BinaryKind::Call, parts }) => {
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::Index(index) => todo!(),
// ExprKind::Member(member) => todo!(),
ExprKind::Structor(Structor { to, init }) => {
let fields = init
.into_iter()
.map(|Fielder { name, init }| {
Ok((name, init.map(|i| Pattern::try_from(i.kind)).transpose()?))
Ok((
name.into(),
init.map(|i| Pattern::try_from(i.kind)).transpose()?,
))
})
.collect::<Result<_, Self::Error>>()?;
Pattern::Struct(to, fields)
@ -940,14 +995,6 @@ mod path {
}
}
/// Checks whether this path ends in the given [Sym]
pub fn ends_with(&self, name: &Sym) -> 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() {

View File

@ -107,12 +107,12 @@ pub trait Fold {
name: self.fold_sym(name),
sign: self.fold_ty_fn(sign),
bind: bind.into_iter().map(|p| self.fold_param(p)).collect(),
body: body.map(|b| self.fold_expr(b)),
body: body.map(|b| self.fold_block(b)),
}
}
fn fold_param(&mut self, p: Param) -> Param {
let Param { mutability, bind } = p;
Param { mutability: self.fold_mutability(mutability), bind: self.fold_pattern(bind) }
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, kind } = s;
@ -248,7 +248,7 @@ 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::Ref(mutability, pattern) => Pattern::Ref(
self.fold_mutability(mutability),
@ -267,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(),
),
}
}
@ -407,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),

View File

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

View File

@ -57,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)

View File

@ -6,8 +6,7 @@ use cl_ast::{format::FmtAdapter, ExprKind, Sym};
use super::{
builtin::Builtin,
error::{Error, IResult},
function::Function,
Callable, Environment,
function::Function, Callable, Environment,
};
use std::{collections::HashMap, ops::*, rc::Rc};
@ -41,8 +40,6 @@ pub enum ConValue {
RangeInc(Integer, Integer),
/// A value of a product type
Struct(Box<(Sym, HashMap<Sym, ConValue>)>),
/// A value of a product type with anonymous members
TupleStruct(Box<(Sym, Box<[ConValue]>)>),
/// An entire namespace
Module(Box<HashMap<Sym, Option<ConValue>>>),
/// A quoted expression
@ -301,25 +298,11 @@ impl std::fmt::Display for ConValue {
}
')'.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) => {
let (name, map) = parts.as_ref();
use std::fmt::Write;
if !name.is_empty() {
write!(f, "{name} ")?;
write!(f, "{name}: ")?;
}
let mut f = f.delimit_with("{", "\n}");
for (k, v) in map.iter() {

View File

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

View File

@ -67,11 +67,11 @@ impl<'a> Visit<'a> for CollectUpvars<'_> {
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: _, bind } in bind {
self.visit_pattern(bind);
for Param { mutability: _, name } in bind {
self.bind_name(name);
}
if let Some(body) = body {
self.visit_expr(body);
self.visit_block(body);
}
}
@ -79,7 +79,7 @@ impl<'a> Visit<'a> for CollectUpvars<'_> {
let cl_ast::For { bind, cond, pass, fail } = f;
self.visit_expr(cond);
self.visit_else(fail);
self.visit_pattern(bind);
self.bind_name(bind); // TODO: is bind only bound in the pass block?
self.visit_block(pass);
}
@ -105,8 +105,10 @@ impl<'a> Visit<'a> for CollectUpvars<'_> {
fn visit_pattern(&mut self, p: &'a cl_ast::Pattern) {
match p {
Pattern::Name(name) => {
self.bind_name(name);
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) => {
@ -127,10 +129,6 @@ 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 super::*;
use cl_ast::{ast_visitor::Visit, *};
use cl_ast::*;
use cl_structures::intern::interned::Interned;
/// A work-in-progress tree walk interpreter for Conlang
pub trait Interpret {
@ -20,29 +20,9 @@ pub trait Interpret {
impl Interpret for File {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
/// Sorts items
#[derive(Debug, Default)]
struct ItemSorter<'ast>(pub [Vec<&'ast Item>; 6]);
impl<'ast> Visit<'ast> for ItemSorter<'ast> {
fn visit_item(&mut self, i: &'ast Item) {
self.0[match &i.kind {
ItemKind::Module(_) => 0,
ItemKind::Use(_) => 1,
ItemKind::Enum(_) | ItemKind::Struct(_) | ItemKind::Alias(_) => 2,
ItemKind::Function(_) => 3,
ItemKind::Impl(_) => 4,
ItemKind::Const(_) | ItemKind::Static(_) => 5,
}]
.push(i)
}
}
let mut items = ItemSorter::default();
items.visit_file(self);
for item in items.0.into_iter().flatten() {
for item in &self.items {
item.interpret(env)?;
}
Ok(ConValue::Empty)
}
}
@ -113,42 +93,8 @@ impl Interpret for Function {
}
}
impl Interpret for Struct {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
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, _)| Param {
mutability: Mutability::Not,
bind: 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}"),
}
fn interpret(&self, _env: &mut Environment) -> IResult<ConValue> {
println!("TODO: {self}");
Ok(ConValue::Empty)
}
}
@ -160,7 +106,7 @@ impl Interpret for Enum {
}
impl Interpret for Impl {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
println!("TODO: impl {}", self.target);
println!("TODO: {self}");
let Self { target: _, body } = self;
body.interpret(env)
}
@ -300,13 +246,19 @@ impl Interpret for Let {
let Let { mutable: _, name, ty: _, init } = self;
match init.as_ref().map(|i| i.interpret(env)).transpose()? {
Some(value) => {
for (name, value) in pattern::substitution(name, value)? {
env.insert(*name, Some(value));
for (path, value) in assignment::pattern_substitution(name, value)? {
match path.parts.as_slice() {
[PathPart::Ident(name)] => env.insert(*name, Some(value)),
_ => eprintln!("Bad assignment: {path} = {value}"),
}
}
}
None => {
for name in pattern::variables(name) {
env.insert(*name, None);
for path in assignment::pattern_variables(name) {
match path.parts.as_slice() {
[PathPart::Ident(name)] => env.insert(*name, None),
_ => eprintln!("Bad assignment: {path}"),
}
}
}
}
@ -318,10 +270,13 @@ impl Interpret for Match {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Self { scrutinee, arms } = self;
let scrutinee = scrutinee.interpret(env)?;
for MatchArm(pat, expr) in arms {
if let Ok(substitution) = pattern::substitution(pat, scrutinee.clone()) {
'arm: for MatchArm(pat, expr) in arms {
if let Ok(substitution) = assignment::pattern_substitution(pat, scrutinee.clone()) {
let mut env = env.frame("match");
for (name, value) in substitution {
for (path, value) in substitution {
let [PathPart::Ident(name)] = path.parts.as_slice() else {
continue 'arm;
};
env.insert(*name, Some(value));
}
return expr.interpret(&mut env);
@ -337,11 +292,127 @@ mod assignment {
use std::collections::HashMap;
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<()> {
for (name, value) in
pattern::substitution(pat, value).map_err(|_| Error::PatFailed(pat.clone().into()))?
{
*env.get_mut(*name)? = Some(value);
let mut substitution = HashMap::new();
append_sub(&mut substitution, pat, value)
.map_err(|_| Error::PatFailed(pat.clone().into()))?;
for (path, value) in substitution {
assign_path(env, path, value)?;
}
Ok(())
}
@ -353,12 +424,20 @@ mod assignment {
match pat {
ExprKind::Member(member) => *addrof_member(env, member)? = value,
ExprKind::Index(index) => *addrof_index(env, index)? = value,
ExprKind::Path(path) => *addrof_path(env, &path.parts)? = Some(value),
_ => Err(Error::NotAssignable)?,
}
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>(
env: &'e mut Environment,
pat: &ExprKind,
@ -382,17 +461,14 @@ mod assignment {
match path {
[PathPart::Ident(name)] => env.get_mut(*name),
[PathPart::Ident(name), rest @ ..] => match env.get_mut(*name)? {
Some(ConValue::Module(env)) => project_path_in_namespace(env, rest),
Some(ConValue::Module(env)) => addrof_path_within_namespace(env, rest),
_ => Err(Error::NotIndexable),
},
_ => Err(Error::NotAssignable),
}
}
pub fn addrof_member<'e>(
env: &'e mut Environment,
member: &Member,
) -> IResult<&'e mut ConValue> {
fn addrof_member<'e>(env: &'e mut Environment, member: &Member) -> IResult<&'e mut ConValue> {
let Member { head, kind } = member;
let ExprKind::Path(path) = head.as_ref() else {
return Err(Error::TypeError);
@ -400,7 +476,15 @@ mod assignment {
let slot = addrof_path(env, &path.parts)?
.as_mut()
.ok_or(Error::NotAssignable)?;
project_memberkind(slot, kind)
Ok(match (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> {
@ -411,50 +495,19 @@ mod assignment {
.collect::<IResult<Vec<_>>>()?;
let mut head = addrof(env, head)?;
for index in indices {
head = project_index(head, &index)?;
head = match (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)
}
/// 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>(
pub fn addrof_path_within_namespace<'e>(
env: &'e mut Namespace,
path: &[PathPart],
) -> IResult<&'e mut Option<ConValue>> {
@ -463,11 +516,11 @@ mod assignment {
[PathPart::Ident(name)] => env.get_mut(name).ok_or(Error::NotDefined(*name)),
[PathPart::Ident(name), rest @ ..] => {
match env.get_mut(name).ok_or(Error::NotDefined(*name))? {
Some(ConValue::Module(env)) => project_path_in_namespace(env, rest),
Some(ConValue::Module(env)) => addrof_path_within_namespace(env, rest),
_ => Err(Error::NotIndexable),
}
}
[PathPart::SelfKw, rest @ ..] => project_path_in_namespace(env, rest),
[PathPart::SelfKw, rest @ ..] => addrof_path_within_namespace(env, rest),
[PathPart::SelfTy, ..] => todo!("calc_address for `Self`"),
[PathPart::SuperKw, ..] => todo!("calc_address for `super`"),
}
@ -679,14 +732,16 @@ impl Interpret for Cast {
impl Interpret for Member {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Member { head, kind } = self;
if let ExprKind::Path(_) = head.as_ref() {
return assignment::addrof_member(env, self).cloned();
}
let head = head.interpret(env)?;
match (head, kind) {
(ConValue::Struct(parts), MemberKind::Call(name, args))
if parts.1.contains_key(name) =>
{
(ConValue::Tuple(v), MemberKind::Tuple(Literal::Int(id))) => v
.get(*id as usize)
.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![];
for arg in &args.exprs {
values.push(arg.interpret(env)?);
@ -704,7 +759,7 @@ impl Interpret for Member {
}
env.call(*name, &values)
}
(mut head, kind) => assignment::project_memberkind(&mut head, kind).cloned(),
_ => Err(Error::TypeError)?,
}
}
}
@ -854,7 +909,7 @@ impl Interpret for If {
}
impl Interpret for For {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Self { bind, cond, pass, fail } = self;
let Self { bind: name, cond, pass, fail } = self;
let cond = cond.interpret(env)?;
// TODO: A better iterator model
let mut bounds: Box<dyn Iterator<Item = ConValue>> = match &cond {
@ -866,10 +921,8 @@ impl Interpret for For {
};
loop {
let mut env = env.frame("loop variable");
if let Some(value) = bounds.next() {
for (name, value) in pattern::substitution(bind, value)? {
env.insert(*name, Some(value));
}
if let Some(loop_var) = bounds.next() {
env.insert(*name, Some(loop_var));
match pass.interpret(&mut env) {
Err(Error::Break(value)) => return Ok(value),
Err(Error::Continue) => continue,

View File

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

View File

@ -1,130 +0,0 @@
//! 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, 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::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
}
/// 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::Array(patterns), ConValue::Array(values))
| (Pattern::Tuple(patterns), ConValue::Tuple(values)) => {
if patterns.len() != values.len() {
Err(Error::ArgNumber { want: patterns.len(), got: values.len() })?
}
for (pat, value) in patterns.iter().zip(Vec::from(values).into_iter()) {
append_sub(sub, pat, value)?;
}
Ok(())
}
(Pattern::Tuple(patterns), ConValue::Empty) if patterns.is_empty() => Ok(()),
(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::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::Struct(path, patterns), ConValue::Struct(parts)) => {
let (name, mut values) = *parts;
if !path.ends_with(&name) {
Err(Error::TypeError)?
}
if patterns.len() != values.len() {
return Err(Error::ArgNumber { want: patterns.len(), got: values.len() });
}
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(())
}
(Pattern::TupleStruct(path, patterns), ConValue::TupleStruct(parts)) => {
let (name, values) = *parts;
if !path.ends_with(&name) {
Err(Error::TypeError)?
}
if patterns.len() != values.len() {
Err(Error::ArgNumber { want: patterns.len(), got: values.len() })?
}
for (pat, value) in patterns.iter().zip(Vec::from(values).into_iter()) {
append_sub(sub, pat, value)?;
}
Ok(())
}
(pat, value) => {
eprintln!("Could not match pattern `{pat}` with value `{value}`!");
Err(Error::NotAssignable)
}
}
}
/// 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,6 +1,5 @@
use super::*;
use cl_ast::ExprKind;
use cl_lexer::error::{Error as LexError, Reason};
use std::fmt::Display;
pub type PResult<T> = Result<T, Error>;
@ -30,7 +29,6 @@ pub enum ErrorKind {
ExpectedParsing {
want: Parsing,
},
InvalidPattern(Box<ExprKind>),
/// Indicates unfinished code
Todo(&'static str),
}
@ -150,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}"),
}
}

View File

@ -459,11 +459,12 @@ impl Parse<'_> for Function {
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))?,
},
})
}
@ -494,7 +495,7 @@ impl Parse<'_> for TypedParam {
/// Parses a single function [parameter](Param)
fn parse(p: &mut Parser) -> PResult<(Param, TyKind)> {
Ok((
Param { mutability: Mutability::parse(p)?, bind: Pattern::parse(p)? },
Param { mutability: Mutability::parse(p)?, name: Sym::parse(p)? },
{
p.match_type(TokenKind::Colon, Parsing::Param)?;
TyKind::parse(p)?
@ -1006,20 +1007,13 @@ 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 {
/// [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)?
})
@ -1032,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)?,
})
@ -1040,15 +1034,15 @@ 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 = Pattern::parse(p)?;
let bind = Sym::parse(p)?;
p.match_type(TokenKind::In, Parsing::For)?;
Ok(For {
bind,
cond: condition(p)?.into(),
cond: Expr::parse(p)?.into(),
pass: Block::parse(p)?.into(),
fail: Else::parse(p)?,
})
@ -1087,8 +1081,9 @@ impl Parse<'_> for Return {
impl Parse<'_> for Pattern {
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
let value = prec::exprkind(p, prec::Precedence::Pattern.level())?;
Pattern::try_from(value).map_err(|e| p.error(InvalidPattern(e.into()), Parsing::Pattern))
let value = prec::exprkind(p, prec::Precedence::Highest.level())?;
Pattern::try_from(value)
.map_err(|_| p.error(ExpectedParsing { want: Parsing::Pattern }, Parsing::Pattern))
}
}

View File

@ -44,9 +44,7 @@ pub fn exprkind(p: &mut Parser, power: u8) -> PResult<ExprKind> {
Some(match op {
TokenKind::LBrack => Precedence::Index,
TokenKind::LParen => Precedence::Call,
TokenKind::LCurly => Precedence::Structor,
TokenKind::Dot => Precedence::Member,
TokenKind::As => Precedence::Cast,
_ => None?,
})
}
@ -57,36 +55,25 @@ pub fn exprkind(p: &mut Parser, power: u8) -> PResult<ExprKind> {
if before < power {
break;
}
p.consume_peeked();
head = 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, Tuple { exprs }.into()).into() }
.into()
}
TokenKind::LCurly => match head {
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))?,
};
continue;
@ -230,7 +217,11 @@ fn exprkind_group(p: &mut Parser) -> PResult<ExprKind> {
/// 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]? `}`
@ -253,9 +244,6 @@ fn structor_body(p: &mut Parser, to: Path) -> PResult<Structor> {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Precedence {
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,
Compare,
Range,
@ -268,6 +256,7 @@ pub enum Precedence {
Cast,
Member, // left-associative
Call,
Highest,
}
impl Precedence {
@ -295,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,
}
}

View File

@ -428,7 +428,7 @@ 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::Ref(mutability, pattern) => {
y.pair("mutability", mutability).pair("subpattern", pattern)
@ -438,20 +438,13 @@ pub mod yamlify {
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) => {
{
let mut y = y.key("TupleStruct");
y.yaml(path).list(items);
}
y
}
};
}
}
@ -643,8 +636,8 @@ pub mod yamlify {
}
impl Yamlify for Param {
fn yaml(&self, y: &mut Yamler) {
let Self { mutability, bind } = self;
y.key("Param").yaml(mutability).pair("pat", bind);
let Self { mutability, name } = self;
y.key("Param").yaml(mutability).pair("name", name);
}
}
impl Yamlify for Ty {

View File

@ -107,35 +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::SelfKw => "self".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),
@ -200,35 +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::SelfKw,
"Self" => Self::SelfTy,
"static" => Self::Static,
"struct" => Self::Struct,
"super" => Self::Super,
"true" => Self::True,
"type" => Self::Type,
"use" => Self::Use,
"while" => Self::While,
"as" | "sama" => Self::As,
"break" | "pana" => Self::Break,
"cl" | "la" => Self::Cl,
"const" | "kiwen" => Self::Const,
"continue" | "tawa" => Self::Continue,
"else" | "taso" => Self::Else,
"enum" | "kulupu" => Self::Enum,
"false" | "ike" => Self::False,
"fn" | "nasin" => Self::Fn,
"for" | "ale" => Self::For,
"if" | "tan" => Self::If,
"impl" | "insa" => Self::Impl,
"in" | "lon" => Self::In,
"let" | "poki" => Self::Let,
"loop" | "awen" => Self::Loop,
"match" | "seme" => Self::Match,
"mod" | "selo" => Self::Mod,
"mut" | "ante" => Self::Mut,
"pub" | "lukin" => Self::Pub,
"return" | "pini" => Self::Return,
"self" | "mi" => Self::SelfKw,
"Self" | "Mi" => Self::SelfTy,
"static" | "mute" => Self::Static,
"struct" | "lipu" => Self::Struct,
"super" | "mama" => Self::Super,
"true" | "pona" => Self::True,
"type" | "ijo" => Self::Type,
"use" | "jo" => Self::Use,
"while" | "lawa" => Self::While,
_ => Err(())?,
})
}

View File

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

View File

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

View File

@ -112,17 +112,6 @@ impl<'a> Editor<'a> {
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.
pub fn print_tail<W: Write>(&self, w: &mut W) -> ReplResult<()> {
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::Break) => break,
Ok(Response::Continue) => continue,
Err(e) => rl.print_inline(format_args!("\x1b[40G\x1b[91m{e}\x1b[0m"))?,
Err(e) => print!("\x1b[40G\x1b[A\x1bJ\x1b[91m{e}\x1b[0m\x1b[B"),
}
}
Ok(())

View File

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

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

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

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

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