From e5c6400f75029124474ee989388bf2c0abdc7e0a Mon Sep 17 00:00:00 2001
From: David Li
Date: Sun, 5 Nov 2017 21:12:44 -0500
Subject: Parse type and variable declarations
---
src/taiga.lalrpop | 34 +++++++++++++++++++++++++++++-----
1 file changed, 29 insertions(+), 5 deletions(-)
(limited to 'src/taiga.lalrpop')
diff --git a/src/taiga.lalrpop b/src/taiga.lalrpop
index 851cb24..aa5e50e 100644
--- a/src/taiga.lalrpop
+++ b/src/taiga.lalrpop
@@ -7,17 +7,39 @@ pub Program: ast::Program = {
Expression => ast::Program(<>),
};
-VarDec: Box> =
+RecordFields: Vec> = {
+ ":" "," => {
+ let mut rest = rest;
+ rest.push(WithLocation::new(ast::RecordField::new(n, v), l, r));
+ rest
+ },
+ ":" "," => vec![WithLocation::new(ast::RecordField::new(n, v), l, r)],
+ ":" => vec![WithLocation::new(ast::RecordField::new(n, v), l, r)],
+ "" => vec![],
+};
+
+Ty: WithLocation = {
+ => <>.map(|v| ast::Ty::Name(v)),
+ "array" "of" => <>.map(|v| ast::Ty::Array(Box::new(ast::Ty::Name(v)))),
+ "{" "}" => WithLocation::new(ast::Ty::Record(v), l, r),
+};
+
+Declaration: Box> = {
+ "var" ":" "=" =>
+ Box::new(WithLocation::new(ast::Declaration::new_var(name, Some(ty), exp), l, r)),
"var" "=" =>
- Box::new(WithLocation::new(ast::VarDec::new(name, None, exp), l, r));
+ Box::new(WithLocation::new(ast::Declaration::new_var(name, None, exp), l, r)),
+ "type" "=" =>
+ Box::new(WithLocation::new(ast::Declaration::new_ty(name, ty), l, r)),
+};
-DeclarationsList: Vec> = {
- => {
+DeclarationsList: Vec> = {
+ => {
let mut h = h;
h.push(*t);
h
},
- => vec![*<>],
+ => vec![*<>],
};
Expression: Box> = {
@@ -121,6 +143,8 @@ match {
"let",
"in",
"end",
+ "type",
+ "function",
}
else {
r"[[:alpha:]_][[:alpha:]_0-9]*",
--
cgit v1.2.3