823 lines
26 KiB
Rust
823 lines
26 KiB
Rust
#![cfg(test)]
|
|
use ::std::rc::Rc;
|
|
use std::str::FromStr;
|
|
|
|
use super::tokenize;
|
|
use super::ParseResult;
|
|
use crate::ast::*;
|
|
use super::Declaration::*;
|
|
use super::Signature;
|
|
use super::TypeIdentifier::*;
|
|
use super::TypeSingletonName;
|
|
use super::ExpressionKind::*;
|
|
use super::Variant::*;
|
|
use super::ForBody::*;
|
|
|
|
fn parse(input: &str) -> ParseResult<AST> {
|
|
let tokens: Vec<crate::tokenizing::Token> = tokenize(input);
|
|
let mut parser = super::Parser::new(tokens);
|
|
parser.parse()
|
|
}
|
|
|
|
macro_rules! parse_test {
|
|
($string:expr, $correct:expr) => {
|
|
assert_eq!(parse($string).unwrap(), $correct)
|
|
};
|
|
}
|
|
macro_rules! parse_test_wrap_ast {
|
|
($string:expr, $correct:expr) => { parse_test!($string, AST { id: ItemIdStore::new_id(), statements: vec![$correct] }) }
|
|
}
|
|
macro_rules! parse_error {
|
|
($string:expr) => { assert!(parse($string).is_err()) }
|
|
}
|
|
macro_rules! qname {
|
|
( $( $component:expr),* ) => {
|
|
{
|
|
let mut components = vec![];
|
|
$(
|
|
components.push(rc!($component));
|
|
)*
|
|
QualifiedName { components, id: ItemIdStore::new_id() }
|
|
}
|
|
};
|
|
}
|
|
macro_rules! val {
|
|
($var:expr) => { Value(QualifiedName { components: vec![Rc::new($var.to_string())], id: ItemIdStore::new_id() }) };
|
|
}
|
|
macro_rules! ty {
|
|
($name:expr) => { Singleton(tys!($name)) }
|
|
}
|
|
macro_rules! tys {
|
|
($name:expr) => { TypeSingletonName { name: Rc::new($name.to_string()), params: vec![] } };
|
|
}
|
|
|
|
macro_rules! decl {
|
|
($expr_type:expr) => {
|
|
Statement { id: ItemIdStore::new_id(), kind: StatementKind::Declaration($expr_type) }
|
|
};
|
|
}
|
|
|
|
macro_rules! import {
|
|
($import_spec:expr) => {
|
|
Statement { id: ItemIdStore::new_id(), kind: StatementKind::Import($import_spec) }
|
|
}
|
|
}
|
|
|
|
macro_rules! module {
|
|
($module_spec:expr) => {
|
|
Statement { id: ItemIdStore::new_id(), kind: StatementKind::Module($module_spec) }
|
|
}
|
|
}
|
|
|
|
macro_rules! ex {
|
|
($expr_type:expr) => { Expression::new(ItemIdStore::new_id(), $expr_type) };
|
|
($expr_type:expr, $type_anno:expr) => { Expression::with_anno(ItemIdStore::new_id(), $expr_type, $type_anno) };
|
|
(s $expr_text:expr) => {
|
|
{
|
|
let tokens: Vec<crate::tokenizing::Token> = tokenize($expr_text);
|
|
let mut parser = super::Parser::new(tokens);
|
|
parser.expression().unwrap()
|
|
}
|
|
};
|
|
}
|
|
|
|
macro_rules! inv {
|
|
($expr_type:expr) => { InvocationArgument::Positional($expr_type) }
|
|
}
|
|
|
|
macro_rules! binexp {
|
|
($op:expr, $lhs:expr, $rhs:expr) => { BinExp(BinOp::from_sigil($op), bx!(Expression::new(ItemIdStore::new_id(), $lhs).into()), bx!(Expression::new(ItemIdStore::new_id(), $rhs).into())) }
|
|
}
|
|
macro_rules! prefexp {
|
|
($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_str($op).unwrap(), bx!(Expression::new(ItemIdStore::new_id(), $lhs).into())) }
|
|
}
|
|
macro_rules! exst {
|
|
($expr_type:expr) => { Statement { id: ItemIdStore::new_id(), kind: StatementKind::Expression(Expression::new(ItemIdStore::new_id(), $expr_type).into())} };
|
|
($expr_type:expr, $type_anno:expr) => { Statement { id: ItemIdStore::new_id(), kind: StatementKind::Expression(Expression::with_anno(ItemIdStore::new_id(), $expr_type, $type_anno).into())} };
|
|
($op:expr, $lhs:expr, $rhs:expr) => { Statement { id: ItemIdStore::new_id(), ,kind: StatementKind::Expression(ex!(binexp!($op, $lhs, $rhs)))}
|
|
};
|
|
(s $statement_text:expr) => {
|
|
{
|
|
let tokens: Vec<crate::tokenizing::Token> = tokenize($statement_text);
|
|
let mut parser = super::Parser::new(tokens);
|
|
parser.statement().unwrap()
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_number_literals_and_binexps() {
|
|
parse_test_wrap_ast! { ".2", exst!(FloatLiteral(0.2)) };
|
|
parse_test_wrap_ast! { "8.1", exst!(FloatLiteral(8.1)) };
|
|
|
|
parse_test_wrap_ast! { "0b010", exst!(NatLiteral(2)) };
|
|
parse_test_wrap_ast! { "0b0_1_0_", exst!(NatLiteral(2)) }
|
|
|
|
parse_test_wrap_ast! {"0xff", exst!(NatLiteral(255)) };
|
|
parse_test_wrap_ast! {"0xf_f_", exst!(NatLiteral(255)) };
|
|
|
|
parse_test_wrap_ast! {"0xf_f_+1", exst!(binexp!("+", NatLiteral(255), NatLiteral(1))) };
|
|
|
|
parse_test! {"3; 4; 4.3",
|
|
AST {
|
|
id: ItemIdStore::new_id(),
|
|
statements: vec![exst!(NatLiteral(3)), exst!(NatLiteral(4)),
|
|
exst!(FloatLiteral(4.3))]
|
|
}
|
|
};
|
|
|
|
parse_test_wrap_ast!("1 + 2 * 3",
|
|
exst!(binexp!("+", NatLiteral(1), binexp!("*", NatLiteral(2), NatLiteral(3))))
|
|
);
|
|
|
|
parse_test_wrap_ast!("1 * 2 + 3",
|
|
exst!(binexp!("+", binexp!("*", NatLiteral(1), NatLiteral(2)), NatLiteral(3)))
|
|
) ;
|
|
|
|
parse_test_wrap_ast!("1 && 2", exst!(binexp!("&&", NatLiteral(1), NatLiteral(2))));
|
|
|
|
parse_test_wrap_ast!("1 + 2 * 3 + 4", exst!(
|
|
binexp!("+",
|
|
binexp!("+", NatLiteral(1), binexp!("*", NatLiteral(2), NatLiteral(3))),
|
|
NatLiteral(4))));
|
|
|
|
parse_test_wrap_ast!("(1 + 2) * 3",
|
|
exst!(binexp!("*", binexp!("+", NatLiteral(1), NatLiteral(2)), NatLiteral(3))));
|
|
|
|
parse_test_wrap_ast!(".1 + .2", exst!(binexp!("+", FloatLiteral(0.1), FloatLiteral(0.2))));
|
|
parse_test_wrap_ast!("1 / 2", exst!(binexp!("/", NatLiteral(1), NatLiteral(2))));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_tuples() {
|
|
parse_test_wrap_ast!("()", exst!(TupleLiteral(vec![])));
|
|
parse_test_wrap_ast!("(\"hella\", 34)", exst!(
|
|
TupleLiteral(
|
|
vec![ex!(s r#""hella""#).into(), ex!(s "34").into()]
|
|
)
|
|
));
|
|
parse_test_wrap_ast!("((1+2), \"slough\")", exst!(TupleLiteral(vec![
|
|
ex!(binexp!("+", NatLiteral(1), NatLiteral(2))).into(),
|
|
ex!(StringLiteral(rc!(slough))).into(),
|
|
])))
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_identifiers() {
|
|
parse_test_wrap_ast!("a", exst!(val!("a")));
|
|
parse_test_wrap_ast!("some_value", exst!(val!("some_value")));
|
|
parse_test_wrap_ast!("a + b", exst!(binexp!("+", val!("a"), val!("b"))));
|
|
//parse_test!("a[b]", AST(vec![Expression(
|
|
//parse_test!("a[]", <- TODO THIS NEEDS TO FAIL
|
|
//parse_test("a()[b]()[d]")
|
|
//TODO fix this parsing stuff
|
|
/*
|
|
parse_test! { "perspicacity()[a]", AST(vec![
|
|
exst!(Index {
|
|
indexee: bx!(ex!(Call { f: bx!(ex!(val!("perspicacity"))), arguments: vec![] })),
|
|
indexers: vec![ex!(val!("a"))]
|
|
})
|
|
])
|
|
}
|
|
*/
|
|
parse_test_wrap_ast!("a[b,c]", exst!(Index { indexee: bx!(ex!(val!("a"))), indexers: vec![ex!(val!("b")), ex!(val!("c"))]} ));
|
|
|
|
parse_test_wrap_ast!("None", exst!(val!("None")));
|
|
parse_test_wrap_ast!("Pandas { a: x + y }",
|
|
exst!(NamedStruct { name: qname!(Pandas), fields: vec![(rc!(a), ex!(binexp!("+", val!("x"), val!("y"))))]})
|
|
);
|
|
parse_test_wrap_ast! { "Pandas { a: n, b: q, }",
|
|
exst!(NamedStruct { name: qname!(Pandas), fields:
|
|
vec![(rc!(a), ex!(val!("n"))), (rc!(b), ex!(val!("q")))]
|
|
}
|
|
)
|
|
};
|
|
}
|
|
|
|
|
|
#[test]
|
|
fn qualified_identifiers() {
|
|
parse_test_wrap_ast! {
|
|
"let q_q = Yolo::Swaggins",
|
|
decl!(Binding { name: rc!(q_q), constant: true, type_anno: None,
|
|
expr: Expression::new(ItemIdStore::new_id(), Value(qname!(Yolo, Swaggins))),
|
|
})
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
"thing::item::call()",
|
|
exst!(Call { f: bx![ex!(Value(qname!(thing, item, call)))], arguments: vec![] })
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn reserved_words() {
|
|
parse_error!("module::item::call()");
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_complicated_operators() {
|
|
parse_test_wrap_ast!("a <- b", exst!(binexp!("<-", val!("a"), val!("b"))));
|
|
parse_test_wrap_ast!("a || b", exst!(binexp!("||", val!("a"), val!("b"))));
|
|
parse_test_wrap_ast!("a<>b", exst!(binexp!("<>", val!("a"), val!("b"))));
|
|
parse_test_wrap_ast!("a.b.c.d", exst!(binexp!(".",
|
|
binexp!(".",
|
|
binexp!(".", val!("a"), val!("b")),
|
|
val!("c")),
|
|
val!("d"))));
|
|
parse_test_wrap_ast!("-3", exst!(prefexp!("-", NatLiteral(3))));
|
|
parse_test_wrap_ast!("-0.2", exst!(prefexp!("-", FloatLiteral(0.2))));
|
|
parse_test_wrap_ast!("!3", exst!(prefexp!("!", NatLiteral(3))));
|
|
parse_test_wrap_ast!("a <- -b", exst!(binexp!("<-", val!("a"), prefexp!("-", val!("b")))));
|
|
parse_test_wrap_ast!("a <--b", exst!(binexp!("<--", val!("a"), val!("b"))));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_functions() {
|
|
parse_test_wrap_ast!("fn oi()", decl!(FuncSig(Signature { name: rc!(oi), operator: false, params: vec![], type_anno: None })));
|
|
parse_test_wrap_ast!("oi()", exst!(Call { f: bx!(ex!(val!("oi"))), arguments: vec![] }));
|
|
parse_test_wrap_ast!("oi(a, 2 + 2)", exst!(Call
|
|
{ f: bx!(ex!(val!("oi"))),
|
|
arguments: vec![inv!(ex!(val!("a"))), inv!(ex!(binexp!("+", NatLiteral(2), NatLiteral(2)))).into()]
|
|
}));
|
|
parse_error!("a(b,,c)");
|
|
|
|
parse_test_wrap_ast!("fn a(b, c: Int): Int", decl!(
|
|
FuncSig(Signature { name: rc!(a), operator: false, params: vec![
|
|
FormalParam { name: rc!(b), anno: None, default: None },
|
|
FormalParam { name: rc!(c), anno: Some(ty!("Int")), default: None }
|
|
], type_anno: Some(ty!("Int")) })));
|
|
|
|
|
|
parse_test_wrap_ast!("fn a(x) { x() }", decl!(
|
|
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![FormalParam { name: rc!(x), anno: None, default: None }], type_anno: None },
|
|
vec![exst!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })])));
|
|
parse_test_wrap_ast!("fn a(x) {\n x() }", decl!(
|
|
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![FormalParam { name: rc!(x), anno: None, default: None }], type_anno: None },
|
|
vec![exst!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })])));
|
|
|
|
let multiline = r#"
|
|
fn a(x) {
|
|
x()
|
|
}
|
|
"#;
|
|
parse_test_wrap_ast!(multiline, decl!(
|
|
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![FormalParam { name: rc!(x), default: None, anno: None }], type_anno: None },
|
|
vec![exst!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })])));
|
|
let multiline2 = r#"
|
|
fn a(x) {
|
|
|
|
x()
|
|
|
|
}
|
|
"#;
|
|
parse_test_wrap_ast!(multiline2, decl!(
|
|
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![FormalParam { name: rc!(x), default: None, anno: None }], type_anno: None },
|
|
vec![exst!(s "x()")])));
|
|
}
|
|
|
|
#[test]
|
|
fn functions_with_default_args() {
|
|
parse_test_wrap_ast! {
|
|
"fn func(x: Int, y: Int = 4) { }",
|
|
decl!(
|
|
FuncDecl(Signature { name: rc!(func), operator: false, type_anno: None, params: vec![
|
|
FormalParam { name: rc!(x), default: None, anno: Some(ty!("Int")) },
|
|
FormalParam { name: rc!(y), default: Some(ex!(s "4")), anno: Some(ty!("Int")) }
|
|
]}, vec![])
|
|
)
|
|
};
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_bools() {
|
|
parse_test_wrap_ast!("false", exst!(BoolLiteral(false)));
|
|
parse_test_wrap_ast!("true", exst!(BoolLiteral(true)));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_strings() {
|
|
parse_test_wrap_ast!(r#""hello""#, exst!(StringLiteral(rc!(hello))));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_types() {
|
|
parse_test_wrap_ast!("type Yolo = Yolo", decl!(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(rc!(Yolo))]), mutable: false} ));
|
|
parse_test_wrap_ast!("type mut Yolo = Yolo", decl!(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(rc!(Yolo))]), mutable: true} ));
|
|
parse_test_wrap_ast!("type alias Sex = Drugs", decl!(TypeAlias { alias: rc!(Sex), original: rc!(Drugs) }));
|
|
parse_test_wrap_ast!("type Sanchez = Miguel | Alejandro(Int, Option<a>) | Esperanza { a: Int, b: String }",
|
|
decl!(TypeDecl {
|
|
name: tys!("Sanchez"),
|
|
body: TypeBody(vec![
|
|
UnitStruct(rc!(Miguel)),
|
|
TupleStruct(rc!(Alejandro), vec![
|
|
Singleton(TypeSingletonName { name: rc!(Int), params: vec![] }),
|
|
Singleton(TypeSingletonName { name: rc!(Option), params: vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })] }),
|
|
]),
|
|
Record{
|
|
name: rc!(Esperanza),
|
|
members: vec![
|
|
(rc!(a), Singleton(TypeSingletonName { name: rc!(Int), params: vec![] })),
|
|
(rc!(b), Singleton(TypeSingletonName { name: rc!(String), params: vec![] })),
|
|
]
|
|
}
|
|
]),
|
|
mutable: false
|
|
}));
|
|
|
|
parse_test_wrap_ast! {
|
|
"type Jorge<a> = Diego | Kike(a)",
|
|
decl!(TypeDecl{
|
|
name: TypeSingletonName { name: rc!(Jorge), params: vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })] },
|
|
body: TypeBody(vec![UnitStruct(rc!(Diego)), TupleStruct(rc!(Kike), vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })])]),
|
|
mutable: false
|
|
}
|
|
)
|
|
};
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_bindings() {
|
|
parse_test_wrap_ast!("let mut a = 10", decl!(Binding { name: rc!(a), constant: false, type_anno: None, expr: ex!(NatLiteral(10)) } ));
|
|
parse_test_wrap_ast!("let a = 2 + 2", decl!(Binding { name: rc!(a), constant: true, type_anno: None, expr: ex!(binexp!("+", NatLiteral(2), NatLiteral(2))) }));
|
|
parse_test_wrap_ast!("let a: Nat = 2 + 2", decl!(
|
|
Binding { name: rc!(a), constant: true, type_anno: Some(Singleton(TypeSingletonName { name: rc!(Nat), params: vec![] })),
|
|
expr: ex!(binexp!("+", NatLiteral(2), NatLiteral(2))) }
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_block_expressions() {
|
|
parse_test_wrap_ast! {
|
|
"if a() then { b(); c() }", exst!(
|
|
IfExpression {
|
|
discriminator: Some(bx! {
|
|
ex!(Call { f: bx!(ex!(val!("a"))), arguments: vec![]})
|
|
}),
|
|
body: bx! {
|
|
IfExpressionBody::SimpleConditional {
|
|
then_case: vec![exst!(Call { f: bx!(ex!(val!("b"))), arguments: vec![]}), exst!(Call { f: bx!(ex!(val!("c"))), arguments: vec![] })],
|
|
else_case: None,
|
|
}
|
|
}
|
|
}
|
|
)
|
|
};
|
|
|
|
parse_test_wrap_ast! {
|
|
"if a() then { b(); c() } else { q }", exst!(
|
|
IfExpression {
|
|
discriminator: Some(bx! {
|
|
ex!(Call { f: bx!(ex!(val!("a"))), arguments: vec![]})
|
|
}),
|
|
body: bx! {
|
|
IfExpressionBody::SimpleConditional {
|
|
then_case: vec![exst!(Call { f: bx!(ex!(val!("b"))), arguments: vec![]}), exst!(Call { f: bx!(ex!(val!("c"))), arguments: vec![] })],
|
|
else_case: Some(vec![exst!(val!("q"))]),
|
|
}
|
|
}
|
|
}
|
|
)
|
|
};
|
|
|
|
/*
|
|
parse_test!("if a() then { b(); c() }", AST(vec![exst!(
|
|
IfExpression(bx!(ex!(Call { f: bx!(ex!(val!("a"))), arguments: vec![]})),
|
|
vec![exst!(Call { f: bx!(ex!(val!("b"))), arguments: vec![]}), exst!(Call { f: bx!(ex!(val!("c"))), arguments: vec![] })],
|
|
None)
|
|
)]));
|
|
parse_test!(r#"
|
|
if true then {
|
|
const a = 10
|
|
b
|
|
} else {
|
|
c
|
|
}"#,
|
|
AST(vec![exst!(IfExpression(bx!(ex!(BoolLiteral(true))),
|
|
vec![decl!(Binding { name: rc!(a), constant: true, expr: ex!(NatLiteral(10)) }),
|
|
exst!(val!(rc!(b)))],
|
|
Some(vec![exst!(val!(rc!(c)))])))])
|
|
);
|
|
|
|
parse_test!("if a { b } else { c }", AST(vec![exst!(
|
|
IfExpression(bx!(ex!(val!("a"))),
|
|
vec![exst!(val!("b"))],
|
|
Some(vec![exst!(val!("c"))])))]));
|
|
|
|
parse_test!("if (A {a: 1}) { b } else { c }", AST(vec![exst!(
|
|
IfExpression(bx!(ex!(NamedStruct { name: rc!(A), fields: vec![(rc!(a), ex!(NatLiteral(1)))]})),
|
|
vec![exst!(val!("b"))],
|
|
Some(vec![exst!(val!("c"))])))]));
|
|
|
|
parse_error!("if A {a: 1} { b } else { c }");
|
|
*/
|
|
}
|
|
#[test]
|
|
fn parsing_interfaces() {
|
|
parse_test_wrap_ast!("interface Unglueable { fn unglue(a: Glue); fn mar(): Glue }",
|
|
decl!(Interface {
|
|
name: rc!(Unglueable),
|
|
signatures: vec![
|
|
Signature {
|
|
name: rc!(unglue),
|
|
operator: false,
|
|
params: vec![
|
|
FormalParam { name: rc!(a), anno: Some(Singleton(TypeSingletonName { name: rc!(Glue), params: vec![] })), default: None }
|
|
],
|
|
type_anno: None
|
|
},
|
|
Signature { name: rc!(mar), operator: false, params: vec![], type_anno: Some(Singleton(TypeSingletonName { name: rc!(Glue), params: vec![] })) },
|
|
]
|
|
})
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_impls() {
|
|
parse_test_wrap_ast!("impl Heh { fn yolo(); fn swagg(); }",
|
|
decl!(Impl {
|
|
type_name: ty!("Heh"),
|
|
interface_name: None,
|
|
block: vec![
|
|
FuncSig(Signature { name: rc!(yolo), operator: false, params: vec![], type_anno: None }),
|
|
FuncSig(Signature { name: rc!(swagg), operator: false, params: vec![], type_anno: None })
|
|
] }));
|
|
|
|
parse_test_wrap_ast!("impl Mondai for Lollerino { fn yolo(); fn swagg(); }",
|
|
decl!(Impl {
|
|
type_name: ty!("Lollerino"),
|
|
interface_name: Some(TypeSingletonName { name: rc!(Mondai), params: vec![] }),
|
|
block: vec![
|
|
FuncSig(Signature { name: rc!(yolo), operator: false, params: vec![], type_anno: None}),
|
|
FuncSig(Signature { name: rc!(swagg), operator: false, params: vec![], type_anno: None })
|
|
] }));
|
|
|
|
parse_test_wrap_ast!("impl Hella<T> for (Alpha, Omega) { }",
|
|
decl!(Impl {
|
|
type_name: Tuple(vec![ty!("Alpha"), ty!("Omega")]),
|
|
interface_name: Some(TypeSingletonName { name: rc!(Hella), params: vec![ty!("T")] }),
|
|
block: vec![]
|
|
})
|
|
);
|
|
|
|
parse_test_wrap_ast!("impl Option<WTFMate> { fn oi() }",
|
|
decl!(Impl {
|
|
type_name: Singleton(TypeSingletonName { name: rc!(Option), params: vec![ty!("WTFMate")]}),
|
|
interface_name: None,
|
|
block: vec![
|
|
FuncSig(Signature { name: rc!(oi), operator: false, params: vec![], type_anno: None }),
|
|
]
|
|
}));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_type_annotations() {
|
|
parse_test_wrap_ast!("let a = b : Int",
|
|
decl!(Binding { name: rc!(a), constant: true, type_anno: None, expr:
|
|
ex!(val!("b"), ty!("Int")) }));
|
|
|
|
parse_test_wrap_ast!("a : Int",
|
|
exst!(val!("a"), ty!("Int"))
|
|
);
|
|
|
|
parse_test_wrap_ast!("a : Option<Int>",
|
|
exst!(val!("a"), Singleton(TypeSingletonName { name: rc!(Option), params: vec![ty!("Int")] }))
|
|
);
|
|
|
|
parse_test_wrap_ast!("a : KoreanBBQSpecifier<Kimchi, Option<Bulgogi> >",
|
|
exst!(val!("a"), Singleton(TypeSingletonName { name: rc!(KoreanBBQSpecifier), params: vec![
|
|
ty!("Kimchi"), Singleton(TypeSingletonName { name: rc!(Option), params: vec![ty!("Bulgogi")] })
|
|
] }))
|
|
);
|
|
|
|
parse_test_wrap_ast!("a : (Int, Yolo<a>)",
|
|
exst!(val!("a"), Tuple(
|
|
vec![ty!("Int"), Singleton(TypeSingletonName {
|
|
name: rc!(Yolo), params: vec![ty!("a")]
|
|
})])));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_lambdas() {
|
|
parse_test_wrap_ast! { r#"\(x) { x + 1}"#, exst!(
|
|
Lambda { params: vec![FormalParam { name: rc!(x), anno: None, default: None } ], type_anno: None, body: vec![exst!(s "x + 1")] }
|
|
)
|
|
}
|
|
|
|
parse_test_wrap_ast!(r#"\ (x: Int, y) { a;b;c;}"#,
|
|
exst!(Lambda {
|
|
params: vec![
|
|
FormalParam { name: rc!(x), anno: Some(ty!("Int")), default: None },
|
|
FormalParam { name: rc!(y), anno: None, default: None }
|
|
],
|
|
type_anno: None,
|
|
body: vec![exst!(s "a"), exst!(s "b"), exst!(s "c")]
|
|
})
|
|
);
|
|
|
|
parse_test_wrap_ast! { r#"\(x){y}(1)"#,
|
|
exst!(Call { f: bx!(ex!(
|
|
Lambda {
|
|
params: vec![
|
|
FormalParam { name: rc!(x), anno: None, default: None }
|
|
],
|
|
type_anno: None,
|
|
body: vec![exst!(s "y")] }
|
|
)),
|
|
arguments: vec![inv!(ex!(NatLiteral(1))).into()] })
|
|
};
|
|
|
|
parse_test_wrap_ast! {
|
|
r#"\(x: Int): String { "q" }"#,
|
|
exst!(Lambda {
|
|
params: vec![
|
|
FormalParam { name: rc!(x), anno: Some(ty!("Int")), default: None },
|
|
],
|
|
type_anno: Some(ty!("String")),
|
|
body: vec![exst!(s r#""q""#)]
|
|
})
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn single_param_lambda() {
|
|
parse_test_wrap_ast! {
|
|
r"\x { x + 10 }",
|
|
exst!(Lambda {
|
|
params: vec![FormalParam { name: rc!(x), anno: None, default: None }],
|
|
type_anno: None,
|
|
body: vec![exst!(s r"x + 10")]
|
|
})
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
r"\x: Nat { x + 10 }",
|
|
exst!(Lambda {
|
|
params: vec![FormalParam { name: rc!(x), anno: Some(ty!("Nat")), default: None }],
|
|
type_anno: None,
|
|
body: vec![exst!(s r"x + 10")]
|
|
})
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn more_advanced_lambdas() {
|
|
parse_test! {
|
|
r#"fn wahoo() { let a = 10; \(x) { x + a } };
|
|
wahoo()(3) "#,
|
|
AST {
|
|
id: ItemIdStore::new_id(),
|
|
statements: vec![
|
|
exst!(s r"fn wahoo() { let a = 10; \(x) { x + a } }"),
|
|
exst! {
|
|
Call {
|
|
f: bx!(ex!(Call { f: bx!(ex!(val!("wahoo"))), arguments: vec![] })),
|
|
arguments: vec![inv!(ex!(NatLiteral(3))).into()],
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn list_literals() {
|
|
parse_test_wrap_ast! {
|
|
"[1,2]",
|
|
exst!(ListLiteral(vec![ex!(NatLiteral(1)), ex!(NatLiteral(2))]))
|
|
};
|
|
}
|
|
|
|
#[test]
|
|
fn while_expr() {
|
|
parse_test_wrap_ast! {
|
|
"while { }",
|
|
exst!(WhileExpression { condition: None, body: vec![] })
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
"while a == b { }",
|
|
exst!(WhileExpression { condition: Some(bx![ex![binexp!("==", val!("a"), val!("b"))]]), body: vec![] })
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn for_expr() {
|
|
parse_test_wrap_ast! {
|
|
"for { a <- maybeValue } return 1",
|
|
exst!(ForExpression {
|
|
enumerators: vec![Enumerator { id: rc!(a), generator: ex!(val!("maybeValue")) }],
|
|
body: bx!(MonadicReturn(ex!(s "1")))
|
|
})
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
"for n <- someRange { f(n); }",
|
|
exst!(ForExpression { enumerators: vec![Enumerator { id: rc!(n), generator: ex!(val!("someRange"))}],
|
|
body: bx!(ForBody::StatementBlock(vec![exst!(s "f(n)")]))
|
|
})
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn patterns() {
|
|
parse_test_wrap_ast! {
|
|
"if x is Some(a) then { 4 } else { 9 }", exst!(
|
|
IfExpression {
|
|
discriminator: Some(bx!(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::SimplePatternMatch {
|
|
pattern: Pattern::TupleStruct(qname!(Some), vec![Pattern::VarOrName(qname!(a))]),
|
|
then_case: vec![exst!(s "4")],
|
|
else_case: Some(vec![exst!(s "9")]) })
|
|
}
|
|
)
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
"if x is Some(a) then 4 else 9", exst!(
|
|
IfExpression {
|
|
discriminator: Some(bx!(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::SimplePatternMatch {
|
|
pattern: Pattern::TupleStruct(qname!(Some), vec![Pattern::VarOrName(qname!(a))]),
|
|
then_case: vec![exst!(s "4")],
|
|
else_case: Some(vec![exst!(s "9")]) }
|
|
)
|
|
}
|
|
)
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
"if x is Something { a, b: x } then { 4 } else { 9 }", exst!(
|
|
IfExpression {
|
|
discriminator: Some(bx!(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::SimplePatternMatch {
|
|
pattern: Pattern::Record(qname!(Something), vec![
|
|
(rc!(a),Pattern::Literal(PatternLiteral::StringPattern(rc!(a)))),
|
|
(rc!(b),Pattern::VarOrName(qname!(x)))
|
|
]),
|
|
then_case: vec![exst!(s "4")],
|
|
else_case: Some(vec![exst!(s "9")])
|
|
}
|
|
)
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pattern_literals() {
|
|
parse_test_wrap_ast! {
|
|
"if x is -1 then 1 else 2",
|
|
exst!(
|
|
IfExpression {
|
|
discriminator: Some(bx!(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::SimplePatternMatch {
|
|
pattern: Pattern::Literal(PatternLiteral::NumPattern { neg: true, num: NatLiteral(1) }),
|
|
then_case: vec![exst!(NatLiteral(1))],
|
|
else_case: Some(vec![exst!(NatLiteral(2))]),
|
|
})
|
|
}
|
|
)
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
"if x is 1 then 1 else 2",
|
|
exst!(
|
|
IfExpression {
|
|
discriminator: Some(bx!(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::SimplePatternMatch {
|
|
pattern: Pattern::Literal(PatternLiteral::NumPattern { neg: false, num: NatLiteral(1) }),
|
|
then_case: vec![exst!(s "1")],
|
|
else_case: Some(vec![exst!(s "2")]),
|
|
})
|
|
}
|
|
)
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
"if x is true then 1 else 2",
|
|
exst!(
|
|
IfExpression {
|
|
discriminator: Some(bx!(ex!(s "x"))),
|
|
body: bx!(
|
|
IfExpressionBody::SimplePatternMatch {
|
|
pattern: Pattern::Literal(PatternLiteral::BoolPattern(true)),
|
|
then_case: vec![exst!(NatLiteral(1))],
|
|
else_case: Some(vec![exst!(NatLiteral(2))]),
|
|
})
|
|
}
|
|
)
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
"if x is \"gnosticism\" then 1 else 2",
|
|
exst!(
|
|
IfExpression {
|
|
discriminator: Some(bx!(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::SimplePatternMatch {
|
|
pattern: Pattern::Literal(PatternLiteral::StringPattern(rc!(gnosticism))),
|
|
then_case: vec![exst!(s "1")],
|
|
else_case: Some(vec![exst!(s "2")]),
|
|
})
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn imports() {
|
|
parse_test_wrap_ast! {
|
|
"import harbinger::draughts::Norgleheim",
|
|
import!(ImportSpecifier {
|
|
id: ItemIdStore::new_id(),
|
|
path_components: vec![rc!(harbinger), rc!(draughts), rc!(Norgleheim)],
|
|
imported_names: ImportedNames::LastOfPath
|
|
})
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn imports_2() {
|
|
parse_test_wrap_ast! {
|
|
"import harbinger::draughts::{Norgleheim, Xraksenlaigar}",
|
|
import!(ImportSpecifier {
|
|
id: ItemIdStore::new_id(),
|
|
path_components: vec![rc!(harbinger), rc!(draughts)],
|
|
imported_names: ImportedNames::List(vec![
|
|
rc!(Norgleheim),
|
|
rc!(Xraksenlaigar)
|
|
])
|
|
})
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn imports_3() {
|
|
parse_test_wrap_ast! {
|
|
"import bespouri::{}",
|
|
import!(ImportSpecifier {
|
|
id: ItemIdStore::new_id(),
|
|
path_components: vec![rc!(bespouri)],
|
|
imported_names: ImportedNames::List(vec![])
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
#[test]
|
|
fn imports_4() {
|
|
parse_test_wrap_ast! {
|
|
"import bespouri::*",
|
|
import!(ImportSpecifier {
|
|
id: ItemIdStore::new_id(),
|
|
path_components: vec![rc!(bespouri)],
|
|
imported_names: ImportedNames::All
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
#[test]
|
|
fn if_expr() {
|
|
parse_test_wrap_ast! {
|
|
"if x { is 1 then 5, else 20 }",
|
|
exst! {
|
|
IfExpression {
|
|
discriminator: Some(bx!(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::CondList(
|
|
vec![
|
|
ConditionArm {
|
|
condition: Condition::Pattern(Pattern::Literal(PatternLiteral::NumPattern { neg: false, num: NatLiteral(1)})),
|
|
guard: None,
|
|
body: vec![exst!(s "5")],
|
|
},
|
|
ConditionArm {
|
|
condition: Condition::Else,
|
|
guard: None,
|
|
body: vec![exst!(s "20")],
|
|
},
|
|
]
|
|
))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn modules() {
|
|
parse_test_wrap_ast! {
|
|
r#"
|
|
module ephraim {
|
|
let a = 10
|
|
fn nah() { 33 }
|
|
}
|
|
"#,
|
|
module!(
|
|
ModuleSpecifier { name: rc!(ephraim), contents: vec![
|
|
decl!(Binding { name: rc!(a), constant: true, type_anno: None, expr: ex!(s "10") }),
|
|
decl!(FuncDecl(Signature { name: rc!(nah), operator: false, params: vec![], type_anno: None }, vec![exst!(NatLiteral(33))])),
|
|
] }
|
|
)
|
|
}
|
|
}
|