summaryrefslogtreecommitdiff
path: root/src/parser.rs
diff options
context:
space:
mode:
authorJJ2023-04-06 21:24:38 +0000
committerJJ2023-04-06 21:24:38 +0000
commit95ce33948581a10c2d55793b6317c7ce273f936a (patch)
tree410383a56b51af9644fa309bbe3f03d9d2368bb6 /src/parser.rs
parent3188a0ec2174945a6b004db78b534f80c7927796 (diff)
rename project, write parser tests
Diffstat (limited to 'src/parser.rs')
-rw-r--r--src/parser.rs35
1 files changed, 21 insertions, 14 deletions
diff --git a/src/parser.rs b/src/parser.rs
index cb86ea7..2f918a8 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -15,13 +15,29 @@ pub fn parse_str(input: &str) -> Result<Expression, peg::error::ParseError<peg::
= i:['a'..='z' | 'A'..='Z' | '0'..='9']+ {
i.iter().collect::<String>()
}
+ rule constant() -> Expression
+ = p:"-"? c:['0'..='9']+ {
+ let value = c.iter().collect::<String>().parse::<Value>().unwrap();
+ Expression::Constant {
+ term: Term {
+ val: if let Some(_) = p {
+ value.wrapping_neg()
+ } else {
+ value
+ },
+ kind: Type::Empty
+ }
+ }
+ }
rule kind() -> Type
= k:identifier() {
match k.as_str() {
+ "empty" => Type::Empty,
"unit" => Type::Unit,
"bool" => Type::Bool,
- "int" => Type::Natural,
- _ => panic!("invalid type")
+ "nat" => Type::Natural,
+ "int" => Type::Integer,
+ _ => panic!("invalid type"), // fixme: raise an error
}
}
rule annotation() -> Expression
@@ -31,31 +47,22 @@ pub fn parse_str(input: &str) -> Result<Expression, peg::error::ParseError<peg::
kind: k
}
}
- rule constant() -> Expression
- = c:['0'..='9']+ {
- Expression::Constant {
- term: Term {
- val: c.iter().collect::<String>().parse::<Value>().unwrap(),
- kind: Type::Empty
- }
- }
- }
rule variable() -> Expression
= v:identifier() {
Expression::Variable {
id: v
}
}
- // fixme: lambda is causing problems with rust-peg
rule abstraction() -> Expression
- = "λ" " "* p:identifier() " "+ "." " "+ f:expression() {
+ = ("λ" / "lambda ") " "* p:identifier() " "* "." " "* f:expression() {
Expression::Abstraction {
param: p,
func: Box::new(f)
}
}
+ // fixme: more cases should parse, but how?
rule application() -> Expression
- = "(" f:(abstraction() / annotation()) ")" " "+ a:expression() {
+ = "(" f:(annotation() / abstraction()) ")" " "* a:expression() {
Expression::Application {
func: Box::new(f),
arg: Box::new(a)