mirror of
https://github.com/Noratrieb/dilaria.git
synced 2026-01-15 18:05:03 +01:00
print and more
This commit is contained in:
parent
ebbc675ffd
commit
9e643b8acd
9 changed files with 150 additions and 54 deletions
|
|
@ -111,6 +111,7 @@ impl<'code> Parser<'code> {
|
|||
TokenType::While => self.while_stmt(),
|
||||
TokenType::Break => self.break_stmt(),
|
||||
TokenType::Return => self.return_stmt(),
|
||||
TokenType::Print => self.print_stmt(),
|
||||
TokenType::BraceO => Ok(Stmt::Block(self.block()?)),
|
||||
_ => {
|
||||
let stmt = self.assignment()?;
|
||||
|
|
@ -291,6 +292,20 @@ impl<'code> Parser<'code> {
|
|||
}
|
||||
}
|
||||
|
||||
fn print_stmt(&mut self) -> ParseResult<'code, Stmt> {
|
||||
enter_parse!(self);
|
||||
|
||||
let print_span = self.expect(TokenType::Print)?.span;
|
||||
|
||||
let expr = self.expression()?;
|
||||
|
||||
let semi_span = self.expect(TokenType::Semi)?.span;
|
||||
|
||||
exit_parse!(self);
|
||||
|
||||
Ok(Stmt::Print(expr, print_span.extend(semi_span)))
|
||||
}
|
||||
|
||||
fn assignment(&mut self) -> ParseResult<'code, Stmt> {
|
||||
enter_parse!(self);
|
||||
|
||||
|
|
|
|||
|
|
@ -292,6 +292,28 @@ mod r#if {
|
|||
}
|
||||
}
|
||||
|
||||
mod print {
|
||||
use super::prelude::*;
|
||||
|
||||
fn parse_print(tokens: Vec<Token>) -> Stmt {
|
||||
let mut parser = parser(tokens);
|
||||
parser.print_stmt().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn print_true() {
|
||||
let tokens = [Print, True].map(token).into();
|
||||
let ast = parse_print(tokens);
|
||||
assert_eq!(
|
||||
Stmt::Print(
|
||||
Expr::Literal(Literal::Boolean(true, Span::dummy())),
|
||||
Span::dummy()
|
||||
),
|
||||
ast
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
mod r#while {
|
||||
use super::prelude::*;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue