From 0a471ed71c32f58a2d5f56ccca65d256044c741a Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 22 Nov 2021 01:59:05 -0800 Subject: [PATCH] For expression stuff --- schala-lang/src/ast/mod.rs | 3 ++- schala-lang/src/parsing/combinator.rs | 5 +++-- schala-lang/src/parsing/peg_parser.rs | 4 ++-- schala-lang/src/parsing/test.rs | 12 ++++++++++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/schala-lang/src/ast/mod.rs b/schala-lang/src/ast/mod.rs index 6500ae5..eb85983 100644 --- a/schala-lang/src/ast/mod.rs +++ b/schala-lang/src/ast/mod.rs @@ -267,8 +267,9 @@ pub enum PatternLiteral { #[derive(Debug, PartialEq, Clone)] pub struct Enumerator { - pub id: Rc, //TODO rename this field + pub identifier: Rc, pub generator: Expression, + pub assignment: bool, //true if `=`, false if `<-` } #[derive(Debug, PartialEq, Clone)] diff --git a/schala-lang/src/parsing/combinator.rs b/schala-lang/src/parsing/combinator.rs index 5e14998..68d3ec0 100644 --- a/schala-lang/src/parsing/combinator.rs +++ b/schala-lang/src/parsing/combinator.rs @@ -791,12 +791,13 @@ fn for_expr(input: Span) -> ParseResult { fn enumerator(input: Span) -> ParseResult { alt(( map(separated_pair(identifier, kw("<-"), expression_no_struct), |(ident, generator)| { - Enumerator { id: rc_string(ident.fragment()), generator } + Enumerator { identifier: rc_string(ident.fragment()), generator, assignment: false } }), //TODO distinguish these two cases in AST map(separated_pair(identifier, kw("="), expression_no_struct), |(ident, generator)| Enumerator { - id: rc_string(ident.fragment()), + identifier: rc_string(ident.fragment()), generator, + assignment: true, }), ))(input) } diff --git a/schala-lang/src/parsing/peg_parser.rs b/schala-lang/src/parsing/peg_parser.rs index e81b3dc..9a6c034 100644 --- a/schala-lang/src/parsing/peg_parser.rs +++ b/schala-lang/src/parsing/peg_parser.rs @@ -298,11 +298,11 @@ peg::parser! { //TODO add guards, etc. rule enumerator(parser: &mut Parser) -> Enumerator = ident:identifier() _ "<-" _ generator:expression_no_struct(parser) { - Enumerator { id: Rc::new(ident.to_string()), generator } + Enumerator { identifier: Rc::new(ident.to_string()), generator, assignment: false } } / //TODO need to distinguish these two cases in AST ident:identifier() _ "=" _ generator:expression_no_struct(parser) { - Enumerator { id: Rc::new(ident.to_string()), generator } + Enumerator { identifier: Rc::new(ident.to_string()), generator, assignment: true } } rule for_body(parser: &mut Parser) -> Box = diff --git a/schala-lang/src/parsing/test.rs b/schala-lang/src/parsing/test.rs index e23078f..6765a24 100644 --- a/schala-lang/src/parsing/test.rs +++ b/schala-lang/src/parsing/test.rs @@ -405,7 +405,11 @@ fn for_expression() { assert_expr!( "for { a <- garodzny::maybeValue } return 1", expr(ForExpression { - enumerators: vec![Enumerator { id: rc("a"), generator: expr(Value(qn!(garodzny, maybeValue))) }], + enumerators: vec![Enumerator { + identifier: rc("a"), + assignment: false, + generator: expr(Value(qn!(garodzny, maybeValue))) + }], body: bx(ForBody::MonadicReturn(expr(NatLiteral(1)))) }) ); @@ -413,7 +417,11 @@ fn for_expression() { assert_expr!( "for n <- someRange { f(n) ; }", expr(ForExpression { - enumerators: vec![Enumerator { id: rc("n"), generator: expr(Value(qn!(someRange))) }], + enumerators: vec![Enumerator { + identifier: rc("n"), + assignment: false, + generator: expr(Value(qn!(someRange))) + }], body: bx(ForBody::StatementBlock( vec![stmt(StatementKind::Expression(expr(Call { f: bx(expr(Value(qn!(f)))),