From 9f393b2eb7b3fb8f1924e80095bad3384049cc67 Mon Sep 17 00:00:00 2001 From: David Li Date: Sat, 4 Nov 2017 20:15:20 -0400 Subject: Parse let expressions --- src/ast.rs | 19 +++++++++++++++++++ src/taiga.lalrpop | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'src') diff --git a/src/ast.rs b/src/ast.rs index 22d968e..7edf9c7 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -60,6 +60,7 @@ pub enum BinOp { #[derive(Debug)] pub enum Expression { + Let(Vec>, Box>), UnaryOp(UnaryOp, Box>), BinOp(BinOp, Box>, Box>), Number(u64), @@ -67,3 +68,21 @@ pub enum Expression { Name(String), Nil, } + +#[derive(Debug)] +pub struct VarDec { + name: WithLocation, + type_: Option, + value: Box>, +} + +impl VarDec { + pub fn new(name: WithLocation, type_: Option, + value: Box>) -> VarDec { + VarDec { + name: name, + type_: type_, + value: value, + } + } +} diff --git a/src/taiga.lalrpop b/src/taiga.lalrpop index be47147..851cb24 100644 --- a/src/taiga.lalrpop +++ b/src/taiga.lalrpop @@ -7,6 +7,19 @@ pub Program: ast::Program = { Expression => ast::Program(<>), }; +VarDec: Box> = + "var" "=" => + Box::new(WithLocation::new(ast::VarDec::new(name, None, exp), l, r)); + +DeclarationsList: Vec> = { + => { + let mut h = h; + h.push(*t); + h + }, + => vec![*<>], +}; + Expression: Box> = { "&&" => Box::new(e1.join_map(*e2, |v1, v2| { ast::Expression::BinOp(ast::BinOp::And, Box::new(v1), Box::new(v2)) @@ -87,6 +100,8 @@ ExpressionBase: Box> = { String => Box::new(<>.map(|v| ast::Expression::String(v))), Name => Box::new(<>.map(|v| ast::Expression::Name(v))), Spanned => Box::new(<>.map(|v| ast::Expression::Nil)), + "let" "in" "end" => + Box::new(WithLocation::new(ast::Expression::Let(d, e), l, r)), "(" ")" => <>, }; @@ -102,6 +117,10 @@ Spanned: WithLocation = { match { "nil", + "var", + "let", + "in", + "end", } else { r"[[:alpha:]_][[:alpha:]_0-9]*", -- cgit v1.2.3