Added non-newline print.

This commit is contained in:
Erin 2022-05-03 23:13:17 +02:00 committed by ondra05
parent fd97655e04
commit 6ad0ab5e29
3 changed files with 134 additions and 99 deletions

View file

@ -136,7 +136,10 @@ pub enum Stmt {
expr: Spanned<Expr>, expr: Spanned<Expr>,
args: Vec<Spanned<Expr>>, args: Vec<Spanned<Expr>>,
}, },
Print(Spanned<Expr>), Print {
expr: Spanned<Expr>,
newline: bool,
},
Read(Assignable), Read(Assignable),
Melo(Spanned<String>), Melo(Spanned<String>),
Rlyeh, Rlyeh,

View file

@ -214,8 +214,14 @@ impl ExecEnv {
/// Perform the action indicated by a statement. /// Perform the action indicated by a statement.
fn eval_stmt(&mut self, stmt: &Spanned<Stmt>) -> Result<HaltStatus, Error> { fn eval_stmt(&mut self, stmt: &Spanned<Stmt>) -> Result<HaltStatus, Error> {
match &stmt.item { match &stmt.item {
Stmt::Print(expr) => { Stmt::Print { expr, newline } => {
println!("{}", self.eval_expr(expr)?); let value = self.eval_expr(expr)?;
if *newline {
println!("{value}");
} else {
print!("{value}");
stdout().lock().flush()?;
}
} }
Stmt::Dim { ident, init } => { Stmt::Dim { ident, init } => {
let init = match init { let init = match init {

View file

@ -363,10 +363,24 @@ impl<'source> Parser<'source> {
match self.checked_next()? { match self.checked_next()? {
// Print to stdout // Print to stdout
Token::Print => { Token::Print => {
let stmt = Stmt::Print(buf.take().ok_or_else(|| { break Stmt::Print {
expr: buf.take().ok_or_else(|| {
Error::new(ErrorKind::UnexpectedToken(Token::Print), self.lexer.span()) Error::new(ErrorKind::UnexpectedToken(Token::Print), self.lexer.span())
})?); })?,
break self.semicolon_terminated(stmt)?; newline: match self.checked_next()? {
Token::Semicolon => true,
Token::Minus => {
self.require(Token::Semicolon)?;
false
}
token => {
return Err(Error::new(
ErrorKind::UnexpectedToken(token),
self.lexer.span(),
));
}
},
};
} }
// Functio call // Functio call
@ -642,7 +656,8 @@ mod tests {
fn simple_math() { fn simple_math() {
let code = "1 * (num + 3) / 666 print;"; let code = "1 * (num + 3) / 666 print;";
let expected = &[Spanned { let expected = &[Spanned {
item: Stmt::Print(Spanned { item: Stmt::Print {
expr: Spanned {
item: Expr::BinOp { item: Expr::BinOp {
lhs: Box::new(Spanned { lhs: Box::new(Spanned {
item: Expr::BinOp { item: Expr::BinOp {
@ -675,7 +690,9 @@ mod tests {
kind: BinOpKind::Divide, kind: BinOpKind::Divide,
}, },
span: 0..17, span: 0..17,
}), },
newline: true,
},
span: 0..24, span: 0..24,
}]; }];
@ -724,10 +741,13 @@ mod tests {
span: 8..21, span: 8..21,
}, },
body: vec![Spanned { body: vec![Spanned {
item: Stmt::Print(Spanned { item: Stmt::Print {
expr: Spanned {
item: Expr::Literal(Literal::Str("Buy Able products!".to_owned())), item: Expr::Literal(Literal::Str("Buy Able products!".to_owned())),
span: 25..47, span: 25..47,
}), },
newline: true,
},
span: 25..54, span: 25..54,
}], }],
}, },
@ -773,7 +793,8 @@ mod tests {
fn cart_construction() { fn cart_construction() {
let code = "[/*able*/ <= 1, /*script*/ <= 3 - 1] print;"; let code = "[/*able*/ <= 1, /*script*/ <= 3 - 1] print;";
let expected = &[Spanned { let expected = &[Spanned {
item: Stmt::Print(Spanned { item: Stmt::Print {
expr: Spanned {
item: Expr::Cart(vec![ item: Expr::Cart(vec![
( (
Spanned { Spanned {
@ -807,7 +828,9 @@ mod tests {
), ),
]), ]),
span: 0..32, span: 0..32,
}), },
newline: true,
},
span: 0..39, span: 0..39,
}]; }];
@ -819,7 +842,8 @@ mod tests {
fn cart_index() { fn cart_index() {
let code = "[/*able*/ <= /*ablecorp*/][/*ablecorp*/] print;"; let code = "[/*able*/ <= /*ablecorp*/][/*ablecorp*/] print;";
let expected = &[Spanned { let expected = &[Spanned {
item: Stmt::Print(Spanned { item: Stmt::Print {
expr: Spanned {
item: Expr::Index { item: Expr::Index {
expr: Box::new(Spanned { expr: Box::new(Spanned {
item: Expr::Cart(vec![( item: Expr::Cart(vec![(
@ -840,7 +864,9 @@ mod tests {
}), }),
}, },
span: 0..34, span: 0..34,
}), },
newline: true,
},
span: 0..41, span: 0..41,
}]; }];