conlang: Introduce ..rest Patterns, refactor Ranges
This commit is contained in:
parent
cc6168b55e
commit
7d3f189100
@ -413,6 +413,7 @@ pub struct Let {
|
|||||||
pub enum Pattern {
|
pub enum Pattern {
|
||||||
Name(Sym),
|
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>),
|
||||||
@ -505,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
|
||||||
|
@ -463,6 +463,8 @@ mod display {
|
|||||||
match self {
|
match self {
|
||||||
Pattern::Name(sym) => sym.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)),
|
||||||
@ -590,6 +592,8 @@ mod display {
|
|||||||
UnaryKind::Deref => "*",
|
UnaryKind::Deref => "*",
|
||||||
UnaryKind::Neg => "-",
|
UnaryKind::Neg => "-",
|
||||||
UnaryKind::Not => "!",
|
UnaryKind::Not => "!",
|
||||||
|
UnaryKind::RangeExc => "..",
|
||||||
|
UnaryKind::RangeInc => "..=",
|
||||||
UnaryKind::At => "@",
|
UnaryKind::At => "@",
|
||||||
UnaryKind::Tilde => "~",
|
UnaryKind::Tilde => "~",
|
||||||
}
|
}
|
||||||
@ -894,6 +898,9 @@ mod convert {
|
|||||||
};
|
};
|
||||||
Pattern::TupleStruct(path, args)
|
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()
|
||||||
@ -934,9 +941,9 @@ mod path {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Checks whether this path ends in the given [Sym]
|
/// Checks whether this path ends in the given [Sym]
|
||||||
pub fn ends_with(&self, name: &Sym) -> bool {
|
pub fn ends_with(&self, name: &str) -> bool {
|
||||||
match self.parts.as_slice() {
|
match self.parts.as_slice() {
|
||||||
[.., PathPart::Ident(last)] => name == last,
|
[.., PathPart::Ident(last)] => name == &**last,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -246,6 +246,8 @@ pub trait Fold {
|
|||||||
match p {
|
match p {
|
||||||
Pattern::Name(sym) => Pattern::Name(self.fold_sym(sym)),
|
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)),
|
||||||
|
@ -211,6 +211,8 @@ pub trait Visit<'a>: Sized {
|
|||||||
match p {
|
match p {
|
||||||
Pattern::Name(name) => self.visit_sym(name),
|
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);
|
||||||
@ -247,7 +249,7 @@ pub trait Visit<'a>: Sized {
|
|||||||
self.visit_pattern(pat);
|
self.visit_pattern(pat);
|
||||||
self.visit_expr(expr);
|
self.visit_expr(expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_assign(&mut self, a: &'a Assign) {
|
fn visit_assign(&mut self, a: &'a Assign) {
|
||||||
let Assign { parts } = a;
|
let Assign { parts } = a;
|
||||||
let (head, tail) = parts.as_ref();
|
let (head, tail) = parts.as_ref();
|
||||||
|
@ -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
|
||||||
|
@ -35,14 +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
|
/// A value of a product type with anonymous members
|
||||||
TupleStruct(Box<(Sym, Box<[ConValue]>)>),
|
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
|
||||||
@ -61,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)?
|
||||||
@ -289,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() {
|
||||||
|
@ -59,7 +59,7 @@ impl Callable for Function {
|
|||||||
return Err(Error::ArgNumber { want: bind.len(), got: args.len() });
|
return Err(Error::ArgNumber { want: bind.len(), got: args.len() });
|
||||||
}
|
}
|
||||||
if self.is_constructor {
|
if self.is_constructor {
|
||||||
return Ok(ConValue::TupleStruct(Box::new((*name, args.into()))));
|
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));
|
||||||
|
@ -107,6 +107,10 @@ impl<'a> Visit<'a> for CollectUpvars<'_> {
|
|||||||
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);
|
||||||
|
@ -542,8 +542,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,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}");
|
||||||
@ -855,8 +863,21 @@ impl Interpret for For {
|
|||||||
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)?,
|
||||||
|
@ -8,7 +8,10 @@ use crate::{
|
|||||||
error::{Error, IResult},
|
error::{Error, IResult},
|
||||||
};
|
};
|
||||||
use cl_ast::{Literal, Pattern, Sym};
|
use cl_ast::{Literal, Pattern, Sym};
|
||||||
use std::{collections::HashMap, rc::Rc};
|
use std::{
|
||||||
|
collections::{HashMap, VecDeque},
|
||||||
|
rc::Rc,
|
||||||
|
};
|
||||||
|
|
||||||
/// Gets the path variables in the given Pattern
|
/// Gets the path variables in the given Pattern
|
||||||
pub fn variables(pat: &Pattern) -> Vec<&Sym> {
|
pub fn variables(pat: &Pattern) -> Vec<&Sym> {
|
||||||
@ -17,6 +20,8 @@ pub fn variables(pat: &Pattern) -> Vec<&Sym> {
|
|||||||
Pattern::Name(name) if &**name == "_" => {}
|
Pattern::Name(name) if &**name == "_" => {}
|
||||||
Pattern::Name(name) => set.push(name),
|
Pattern::Name(name) => set.push(name),
|
||||||
Pattern::Literal(_) => {}
|
Pattern::Literal(_) => {}
|
||||||
|
Pattern::Rest(Some(pattern)) => patvars(set, pattern),
|
||||||
|
Pattern::Rest(None) => {}
|
||||||
Pattern::Ref(_, pattern) => patvars(set, pattern),
|
Pattern::Ref(_, pattern) => patvars(set, pattern),
|
||||||
Pattern::Tuple(patterns) | Pattern::Array(patterns) => {
|
Pattern::Tuple(patterns) | Pattern::Array(patterns) => {
|
||||||
patterns.iter().for_each(|pat| patvars(set, pat))
|
patterns.iter().for_each(|pat| patvars(set, pat))
|
||||||
@ -37,6 +42,41 @@ pub fn variables(pat: &Pattern) -> Vec<&Sym> {
|
|||||||
set
|
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
|
/// Appends a substitution to the provided table
|
||||||
pub fn append_sub<'pat>(
|
pub fn append_sub<'pat>(
|
||||||
sub: &mut HashMap<&'pat Sym, ConValue>,
|
sub: &mut HashMap<&'pat Sym, ConValue>,
|
||||||
@ -44,18 +84,6 @@ pub fn append_sub<'pat>(
|
|||||||
value: ConValue,
|
value: ConValue,
|
||||||
) -> IResult<()> {
|
) -> IResult<()> {
|
||||||
match (pat, value) {
|
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)) => {
|
(Pattern::Literal(Literal::Bool(a)), ConValue::Bool(b)) => {
|
||||||
(*a == b).then_some(()).ok_or(Error::NotAssignable)
|
(*a == b).then_some(()).ok_or(Error::NotAssignable)
|
||||||
}
|
}
|
||||||
@ -73,6 +101,25 @@ pub fn append_sub<'pat>(
|
|||||||
}
|
}
|
||||||
(Pattern::Literal(_), _) => Err(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), _) if "_".eq(&**name) => Ok(()),
|
||||||
(Pattern::Name(name), value) => {
|
(Pattern::Name(name), value) => {
|
||||||
sub.insert(name, value);
|
sub.insert(name, value);
|
||||||
@ -81,14 +128,43 @@ pub fn append_sub<'pat>(
|
|||||||
|
|
||||||
(Pattern::Ref(_, pat), ConValue::Ref(r)) => append_sub(sub, pat, Rc::unwrap_or_clone(r)),
|
(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)) => {
|
(Pattern::Struct(path, patterns), ConValue::Struct(parts)) => {
|
||||||
let (name, mut values) = *parts;
|
let (name, mut values) = *parts;
|
||||||
if !path.ends_with(&name) {
|
if !path.ends_with(&name) {
|
||||||
Err(Error::TypeError)?
|
Err(Error::TypeError)?
|
||||||
}
|
}
|
||||||
if patterns.len() != values.len() {
|
|
||||||
return Err(Error::ArgNumber { want: patterns.len(), got: values.len() });
|
|
||||||
}
|
|
||||||
for (name, pat) in patterns {
|
for (name, pat) in patterns {
|
||||||
let value = values.remove(name).ok_or(Error::TypeError)?;
|
let value = values.remove(name).ok_or(Error::TypeError)?;
|
||||||
match pat {
|
match pat {
|
||||||
@ -101,23 +177,9 @@ pub fn append_sub<'pat>(
|
|||||||
Ok(())
|
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) => {
|
(pat, value) => {
|
||||||
eprintln!("Could not match pattern `{pat}` with value `{value}`!");
|
eprintln!("Could not match pattern `{pat}` with value `{value}`!");
|
||||||
Err(Error::NotAssignable)
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -330,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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -351,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,
|
||||||
};
|
};
|
||||||
|
@ -430,6 +430,7 @@ pub mod yamlify {
|
|||||||
match self {
|
match self {
|
||||||
Pattern::Name(name) => y.value(name),
|
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)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user