replaced if with unless

This commit is contained in:
Erin 2022-04-18 21:42:26 +02:00 committed by ondra05
parent 6dd32793bb
commit ea13c79fc4
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)] #[derive(Debug, PartialEq, Clone, Hash)]
pub enum Stmt { pub enum Stmt {
// Control flow // Control flow
If { Unless {
cond: Spanned<Expr>, cond: Spanned<Expr>,
body: Block, body: Block,
}, },

View file

@ -247,8 +247,8 @@ impl ExecEnv {
}), }),
); );
} }
Stmt::If { cond, body } => { Stmt::Unless { cond, body } => {
if self.eval_expr(cond)?.into_abool().to_bool() { if !self.eval_expr(cond)?.into_abool().to_bool() {
return self.eval_stmts_hs(body, true); return self.eval_stmts_hs(body, true);
} }
} }
@ -743,7 +743,7 @@ mod tests {
let mut env = ExecEnv::new(); let mut env = ExecEnv::new();
eval( eval(
&mut env, &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(); .unwrap();

View file

@ -87,8 +87,8 @@ pub enum Token {
TDark, TDark,
// Control flow keywords // Control flow keywords
#[token("if")] #[token("unless")]
If, Unless,
#[token("loop")] #[token("loop")]
Loop, Loop,
@ -156,7 +156,7 @@ mod tests {
#[test] #[test]
fn simple_fn() { 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 = &[ let expected = &[
Functio, Functio,
Identifier("test".to_owned()), Identifier("test".to_owned()),
@ -167,10 +167,10 @@ mod tests {
Identifier("var".to_owned()), Identifier("var".to_owned()),
Integer(3), Integer(3),
Semicolon, Semicolon,
If, Unless,
LeftParen, LeftParen,
Identifier("var".to_owned()), Identifier("var".to_owned()),
Equals, Aint,
Integer(3), Integer(3),
RightParen, RightParen,
LeftCurly, LeftCurly,

View file

@ -66,7 +66,7 @@ impl<'source> Parser<'source> {
let start = self.lexer.span().start; let start = self.lexer.span().start;
match token { 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( Token::Functio => Ok(Spanned::new(
self.functio_flow()?, self.functio_flow()?,
start..self.lexer.span().end, start..self.lexer.span().end,
@ -419,9 +419,9 @@ impl<'source> Parser<'source> {
/// Parse If flow /// Parse If flow
/// ///
/// Consists of condition and block, there is no else /// 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)?; self.require(Token::LeftParen)?;
Ok(Stmt::If { Ok(Stmt::Unless {
cond: self.expr_flow(Token::RightParen)?, cond: self.expr_flow(Token::RightParen)?,
body: self.get_block()?, body: self.get_block()?,
}) })
@ -674,32 +674,32 @@ mod tests {
#[test] #[test]
fn if_flow() { fn if_flow() {
let code = "if (var = always) { /*Buy Able products!*/ print; }"; let code = "unless (never + never) { /*Buy Able products!*/ print; }";
let expected = &[Spanned { let expected = &[Spanned {
item: Stmt::If { item: Stmt::Unless {
cond: Spanned { cond: Spanned {
item: Expr::BinOp { item: Expr::BinOp {
lhs: Box::new(Spanned { lhs: Box::new(Spanned {
item: Expr::Variable("var".to_owned()), item: Expr::Variable("never".to_owned()),
span: 4..5, span: 8..13,
}), }),
rhs: Box::new(Spanned { rhs: Box::new(Spanned {
item: Expr::Variable("always".to_owned()), item: Expr::Variable("never".to_owned()),
span: 9..15, span: 16..21,
}), }),
kind: BinOpKind::Equal, kind: BinOpKind::Add,
}, },
span: 4..15, span: 8..21,
}, },
body: vec![Spanned { body: vec![Spanned {
item: Stmt::Print(Spanned { item: Stmt::Print(Spanned {
item: Expr::Literal(Literal::Str("Buy Able products!".to_owned())), 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(); let ast = Parser::new(code).init().unwrap();

View file

@ -12,12 +12,12 @@ dim bar /*world*/;
swap(foo, bar); swap(foo, bar);
if (foo ain't /*world*/) { unless (foo = /*world*/) {
/*FAILED*/ print; /*FAILED*/ print;
/*foo should be 'world', is actually:*/ print; /*foo should be 'world', is actually:*/ print;
foo print; foo print;
} }
if (foo = /*world*/) { unless (foo ain't /*world*/) {
/*OK*/ print; /*OK*/ print;
} }