deelang/rust/src/parser.rs

277 lines
8.3 KiB
Rust

#[derive(Debug,PartialEq)]
pub enum Stmt {
Assignment(String, Expr),
Funcall(Expr),
}
#[derive(Debug,PartialEq)]
pub enum Expr {
Id(String),
Num(f64),
Funcall(String, Vec<Expr>),
Funcdef(Option<String>, Box<Expr>),
Plus(Box<Expr>, Box<Expr>),
Minus(Box<Expr>, Box<Expr>),
Mult(Box<Expr>, Box<Expr>),
Div(Box<Expr>, Box<Expr>),
Block(Vec<Stmt>),
}
peg::parser! {
grammar deelang_parser() for str {
pub rule program() -> Vec<Stmt>
= __* s:stmt()* { s }
pub rule stmt() -> Stmt
= i:id() "<-" _ e:expr() stop() { Stmt::Assignment(i, e) } /
f:funcall() stop() { Stmt::Funcall(f) }
rule expr() -> Expr = precedence! {
e1:(@) "+" _ e2:@ { Expr::Plus(Box::new(e1), Box::new(e2)) }
e1:(@) "-" _ e2:@ { Expr::Minus(Box::new(e1), Box::new(e2)) }
--
e1:(@) "*" _ e2:@ { Expr::Mult(Box::new(e1), Box::new(e2)) }
e1:(@) "/" _ e2:@ { Expr::Div(Box::new(e1), Box::new(e2)) }
--
"(" _ e:expr() ")" _ { e }
f:funcall() { f }
f:funcdef() { f }
i:id() _ { Expr::Id(i) }
n:num() _ { Expr::Num(n) }
}
rule block() -> Expr
= stop() indent() __* s:stmt()+ dedent() { Expr::Block(s) }
rule id() -> String
= i:$(letter() (letter() / digit() / ['?'|'.'|'-'])*) _ { i.to_string() }
rule num() -> f64
= n:$(digit()+ "."? digit()* / "." digit()+) _ { n.parse().unwrap() }
rule funcall() -> Expr
= i:id() "(" _ e:(expr() ** ("," _)) ")" _ { Expr::Funcall(i, e) }
rule funcdef() -> Expr
= i:id()? "->" _ e:(expr() / block()) { Expr::Funcdef(i, Box::new(e)) }
rule letter()
= ['A'..='Z'] / ['a'..='z']
rule digit()
= ['0'..='9']
rule stop()
= __+ / eof()
rule indent()
= ">>>"
rule dedent()
= "<<<"
rule _ // Non-meaningful whitespace
= ['\t'|' ']*
rule __ // End Of Statement (comment, newline, eof, TODO semicolon)
= comment()? newline() / comment() &eof()
rule comment()
= "#" (!newline() [_])* &(newline() / eof())
rule newline()
= "\r\n" / "\r" / "\n"
rule eof()
= ![_]
}
}
fn preprocess(input: &str) -> String {
let mut stack = vec![0];
let mut output = String::new();
for line in input.lines() {
let mut count = 0;
for c in line.chars() {
if c == ' ' || c == '\t' {
count += 1;
} else if c == '#' {
break;
} else {
let curr = stack.last().unwrap();
if curr < &count {
stack.push(count);
output.push_str(">>>");
} else if curr > &count {
while stack.last().unwrap() > &count {
output.push_str("<<<");
output.push('\n');
stack.pop();
}
}
break;
}
}
output.push_str(line.trim());
output.push('\n');
}
println!("{}", output);
output
}
pub fn parse(prgm: &str) -> Vec<Stmt> {
let prgm = preprocess(&prgm);
deelang_parser::program(&prgm).unwrap()
}
pub fn parse_stmt(stmt: &str) -> Stmt {
let stmt = preprocess(&stmt);
deelang_parser::stmt(&stmt).unwrap()
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_comments() {
let prgm = r"## This is a comment
apple <- 1 ## This is too
## This comment ends the file";
let expected = vec![Stmt::Assignment("apple".to_string(), Expr::Num(1.0))];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
}
#[test]
fn test_funcall() {
let expected = vec![
Stmt::Funcall(Expr::Funcall("pear".to_string(), vec![])),
Stmt::Funcall(Expr::Funcall("pear".to_string(),
vec![Expr::Id("x".to_string()), Expr::Id("y".to_string())],
))
];
let prgm = r"pear()
pear(x, y)";
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
}
#[test]
fn test_assignment() {
let prgm = r"apple <- 1
apple <- pear(x, y)";
let expected = vec![
Stmt::Assignment("apple".to_string(),
Expr::Num(1.0)),
Stmt::Assignment("apple".to_string(),
Expr::Funcall("pear".to_string(),
vec![
Expr::Id("x".to_string()),
Expr::Id("y".to_string()),
])),
];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
}
#[test]
fn test_operators() {
let prgm = r"three <- 1 + 2
one <- 3 - 2
four <- (3 - 1) * 2";
let expected = vec![
Stmt::Assignment("three".to_string(),
Expr::Plus(
Box::new(Expr::Num(1.0)),
Box::new(Expr::Num(2.0))
)),
Stmt::Assignment("one".to_string(),
Expr::Minus(
Box::new(Expr::Num(3.0)),
Box::new(Expr::Num(2.0))
)),
Stmt::Assignment("four".to_string(),
Expr::Mult(
Box::new(Expr::Minus(
Box::new(Expr::Num(3.0)),
Box::new(Expr::Num(1.0)),
)),
Box::new(Expr::Num(2.0)),
))
];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
}
#[test]
fn test_compound_expression() {
let prgm = "apple <- pear(x, y) + z";
let expected = vec![
Stmt::Assignment("apple".to_string(),
Expr::Plus(
Box::new(Expr::Funcall(
"pear".to_string(),
vec![Expr::Id("x".to_string()), Expr::Id("y".to_string())],
)),
Box::new(Expr::Id("z".to_string()))
)
),
];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
}
#[test]
fn test_funcdef() {
let prgm = r"foo <- -> bar()
foo <- ->
>>>
bar()
baz()
<<<
foo <- x -> y -> x * y";
let expected = vec![
Stmt::Assignment(
"foo".to_string(),
Expr::Funcdef(
None,
Box::new(Expr::Funcall("bar".to_string(), vec![])),
)
),
Stmt::Assignment(
"foo".to_string(),
Expr::Funcdef(
None,
Box::new(Expr::Block(vec![
Stmt::Funcall(Expr::Funcall("bar".to_string(), vec![])),
Stmt::Funcall(Expr::Funcall("baz".to_string(), vec![])),
]))
)
),
Stmt::Assignment(
"foo".to_string(),
Expr::Funcdef(
Some("x".to_string()),
Box::new(
Expr::Funcdef(
Some("y".to_string()),
Box::new(
Expr::Mult(
Box::new(Expr::Id("x".to_string())),
Box::new(Expr::Id("y".to_string())),
)
)
)
)
)
)
];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
}
#[test]
fn test_preprocess() {
let prgm = r"
.
.
.
.
.
## Hello World";
let expected = r"
>>>.
>>>.
<<<
.
>>>.
<<<
<<<
.
## Hello World
";
assert_eq!(preprocess(prgm), expected);
}
}