Pass RecursiveDescent tests
This commit is contained in:
parent
fb50827f2f
commit
d1fb0600f8
117
src/parse/rd.rs
117
src/parse/rd.rs
|
@ -136,7 +136,7 @@ impl<'a> RecursiveDescent<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_fn_impl(&mut self, mutable: bool) -> FnImpl<'a> {
|
pub fn build_fn_impl(&mut self, mutable: bool) -> FnImpl<'a> {
|
||||||
let def = self.fn_def_then(mutable, Token::Semicolon);
|
let def = self.fn_def_then(mutable, Token::BraceOpen);
|
||||||
let body = self.build_branch_body();
|
let body = self.build_branch_body();
|
||||||
FnImpl { def, body }
|
FnImpl { def, body }
|
||||||
}
|
}
|
||||||
|
@ -446,3 +446,118 @@ impl<'a> BuildAst<'a> for RecursiveDescent<'a> {
|
||||||
Ast { defs }
|
Ast { defs }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn parse(source: &str) {
|
||||||
|
let lex = Lexer::new(source);
|
||||||
|
let rd = RecursiveDescent::new(lex);
|
||||||
|
let ast = rd.build_ast();
|
||||||
|
println!("{:#?}", ast);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn function() {
|
||||||
|
parse(include_str!("../test/function.fae"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tail_expression() {
|
||||||
|
parse("fn add(i32 x, i32 y) i32 { x + y }");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn structure() {
|
||||||
|
parse(include_str!("../test/structure.fae"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn interface() {
|
||||||
|
parse(include_str!("../test/interface.fae"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example() {
|
||||||
|
parse(include_str!("../test/example.fae"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn clock() {
|
||||||
|
parse(include_str!("../test/clock.fae"));
|
||||||
|
}
|
||||||
|
|
||||||
|
mod expr {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn parse_expr(source: &str) {
|
||||||
|
println!("Source: {}", source);
|
||||||
|
let lex = Lexer::new(source);
|
||||||
|
let mut rd = RecursiveDescent::new(lex);
|
||||||
|
let expr = rd.build_expr();
|
||||||
|
println!("{:#?}", expr);
|
||||||
|
assert_eq!(expr.1, Token::Semicolon);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn int_literals() {
|
||||||
|
parse_expr("0b1 + 0x2 - 0o3 * 0x4;");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn float_literals() {
|
||||||
|
parse_expr("0.1 + .1 * 1.;");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn string_literals() {
|
||||||
|
parse_expr("\"String contents\";");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn locals() {
|
||||||
|
parse_expr("local1 + local2 - local3;");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nesting() {
|
||||||
|
parse_expr("(1 + 2) * (3 / (4 + 5));")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unary_ops() {
|
||||||
|
parse_expr("not false + -(2 + 3) * -1 / ~0xff;");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn members() {
|
||||||
|
parse_expr(".member1 * .member2;");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn if_expr() {
|
||||||
|
parse_expr("if true { 1 } else { 0 };");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn else_if_expr() {
|
||||||
|
parse_expr("if a { 1 } else if b { 2 } else { c };");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn func_call() {
|
||||||
|
parse_expr("func_call(1 + 2, 3, 4 * 5);");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn method() {
|
||||||
|
parse_expr(".method();");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn submembers() {
|
||||||
|
parse_expr(".member.submember1 / .member.submember2.submember3;");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue