replaced if with unless

This commit is contained in:
Erin 2022-04-18 21:42:26 +02:00 committed by ondra05
parent b346995bfd
commit 152c5c0f88
5 changed files with 25 additions and 25 deletions

View file

@ -103,7 +103,7 @@ pub type Block = Vec<Spanned<Stmt>>;
#[derive(Debug, PartialEq, Clone, Hash)]
pub enum Stmt {
// Control flow
If {
Unless {
cond: Spanned<Expr>,
body: Block,
},

View file

@ -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();

View file

@ -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,

View file

@ -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<Stmt, Error> {
fn unless_flow(&mut self) -> Result<Stmt, Error> {
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();

View file

@ -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;
}