add as patterns
This commit is contained in:
parent
f1d19b73db
commit
938033ab5d
21
src/main.rs
21
src/main.rs
|
@ -53,7 +53,28 @@ use crate::base::*;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let src = "
|
let src = "
|
||||||
|
fn fancy_add {
|
||||||
|
() -> 0
|
||||||
|
(n as :number) -> n
|
||||||
|
(x as :number, y as :number) -> add (x, y)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fancy_sub {
|
||||||
|
() -> 0
|
||||||
|
(n as :number) -> n
|
||||||
|
(x as :number, y as :number) -> sub (x, y)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fib {
|
||||||
|
(1) -> 1
|
||||||
|
(2) -> 1
|
||||||
|
(n) -> fancy_add(
|
||||||
|
fib (fancy_sub (n, 1))
|
||||||
|
fib (fancy_sub (n, 2))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fib (30)
|
||||||
";
|
";
|
||||||
let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
|
let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
|
||||||
if lex_errs.len() > 0 {
|
if lex_errs.len() > 0 {
|
||||||
|
|
|
@ -67,10 +67,16 @@ pub fn match_pattern<'src, 'a>(
|
||||||
ctx.push((w, val.clone()));
|
ctx.push((w, val.clone()));
|
||||||
Some(ctx)
|
Some(ctx)
|
||||||
}
|
}
|
||||||
(Pattern::As(word, type_kw), value) => {
|
(Pattern::As(word, type_str), value) => {
|
||||||
let ludus_type = value.ludus_type();
|
let ludus_type = value.ludus_type();
|
||||||
|
let type_kw = Value::Keyword(type_str);
|
||||||
|
if type_kw == ludus_type {
|
||||||
|
ctx.push((word, value.clone()));
|
||||||
|
Some(ctx)
|
||||||
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// todo: add splats to these match clauses
|
// todo: add splats to these match clauses
|
||||||
(Pattern::Tuple(x), Value::Tuple(y)) => {
|
(Pattern::Tuple(x), Value::Tuple(y)) => {
|
||||||
if x.len() != y.len() {
|
if x.len() != y.len() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user