From 152c5c0f88d4918c2ad5a43b491602223170152a Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 18 Apr 2022 21:42:26 +0200 Subject: [PATCH] replaced if with unless --- ablescript/src/ast.rs | 2 +- ablescript/src/interpret.rs | 6 +++--- ablescript/src/lexer.rs | 10 +++++----- ablescript/src/parser.rs | 28 ++++++++++++++-------------- examples/pass-by-reference.able | 4 ++-- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/ablescript/src/ast.rs b/ablescript/src/ast.rs index edf276c5..50f9ceb4 100644 --- a/ablescript/src/ast.rs +++ b/ablescript/src/ast.rs @@ -103,7 +103,7 @@ pub type Block = Vec>; #[derive(Debug, PartialEq, Clone, Hash)] pub enum Stmt { // Control flow - If { + Unless { cond: Spanned, body: Block, }, diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index 8134a3e2..52e88942 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -247,8 +247,8 @@ impl ExecEnv { }), ); } - Stmt::If { cond, body } => { - if self.eval_expr(cond)?.into_abool().to_bool() { + Stmt::Unless { cond, body } => { + if !self.eval_expr(cond)?.into_abool().to_bool() { return self.eval_stmts_hs(body, true); } } @@ -743,7 +743,7 @@ mod tests { let mut env = ExecEnv::new(); eval( &mut env, - "dim foo 1; 2 =: foo; if (always) { dim foo 3; 4 =: foo; }", + "dim foo 1; 2 =: foo; unless (never) { dim foo 3; 4 =: foo; }", ) .unwrap(); diff --git a/ablescript/src/lexer.rs b/ablescript/src/lexer.rs index 3da3842d..e95d663c 100644 --- a/ablescript/src/lexer.rs +++ b/ablescript/src/lexer.rs @@ -87,8 +87,8 @@ pub enum Token { TDark, // Control flow keywords - #[token("if")] - If, + #[token("unless")] + Unless, #[token("loop")] Loop, @@ -156,7 +156,7 @@ mod tests { #[test] fn simple_fn() { - let code = "functio test() { dim var 3; if (var = 3) { var print } }"; + let code = "functio test() { dim var 3; unless (var ain't 3) { var print } }"; let expected = &[ Functio, Identifier("test".to_owned()), @@ -167,10 +167,10 @@ mod tests { Identifier("var".to_owned()), Integer(3), Semicolon, - If, + Unless, LeftParen, Identifier("var".to_owned()), - Equals, + Aint, Integer(3), RightParen, LeftCurly, diff --git a/ablescript/src/parser.rs b/ablescript/src/parser.rs index 977fd73a..5256a48d 100644 --- a/ablescript/src/parser.rs +++ b/ablescript/src/parser.rs @@ -66,7 +66,7 @@ impl<'source> Parser<'source> { let start = self.lexer.span().start; match token { - Token::If => Ok(Spanned::new(self.if_flow()?, start..self.lexer.span().end)), + Token::Unless => Ok(Spanned::new(self.unless_flow()?, start..self.lexer.span().end)), Token::Functio => Ok(Spanned::new( self.functio_flow()?, start..self.lexer.span().end, @@ -419,9 +419,9 @@ impl<'source> Parser<'source> { /// Parse If flow /// /// Consists of condition and block, there is no else - fn if_flow(&mut self) -> Result { + fn unless_flow(&mut self) -> Result { self.require(Token::LeftParen)?; - Ok(Stmt::If { + Ok(Stmt::Unless { cond: self.expr_flow(Token::RightParen)?, body: self.get_block()?, }) @@ -674,32 +674,32 @@ mod tests { #[test] fn if_flow() { - let code = "if (var = always) { /*Buy Able products!*/ print; }"; + let code = "unless (never + never) { /*Buy Able products!*/ print; }"; let expected = &[Spanned { - item: Stmt::If { + item: Stmt::Unless { cond: Spanned { item: Expr::BinOp { lhs: Box::new(Spanned { - item: Expr::Variable("var".to_owned()), - span: 4..5, + item: Expr::Variable("never".to_owned()), + span: 8..13, }), rhs: Box::new(Spanned { - item: Expr::Variable("always".to_owned()), - span: 9..15, + item: Expr::Variable("never".to_owned()), + span: 16..21, }), - kind: BinOpKind::Equal, + kind: BinOpKind::Add, }, - span: 4..15, + span: 8..21, }, body: vec![Spanned { item: Stmt::Print(Spanned { item: Expr::Literal(Literal::Str("Buy Able products!".to_owned())), - span: 19..39, + span: 25..47, }), - span: 19..46, + span: 25..54, }], }, - span: 0..48, + span: 0..56, }]; let ast = Parser::new(code).init().unwrap(); diff --git a/examples/pass-by-reference.able b/examples/pass-by-reference.able index 8beefe34..cde0c378 100644 --- a/examples/pass-by-reference.able +++ b/examples/pass-by-reference.able @@ -12,12 +12,12 @@ dim bar /*world*/; swap(foo, bar); -if (foo ain't /*world*/) { +unless (foo = /*world*/) { /*FAILED*/ print; /*foo should be 'world', is actually:*/ print; foo print; } -if (foo = /*world*/) { +unless (foo ain't /*world*/) { /*OK*/ print; }