diff --git a/src/expression.rs b/src/expression.rs index e42a336..d8d5fd2 100644 --- a/src/expression.rs +++ b/src/expression.rs @@ -112,33 +112,22 @@ impl Expression for Not { } } -/// -/// 打印换行 -#[derive(Debug)] -pub struct Println { - /// 要打印换行的表达式对象 - pub expression: Box, -} - -impl Expression for Println { - fn evaluate(&self, ctx: &mut Context) -> Result { - let res = self.expression.evaluate(ctx).unwrap(); - ctx.output.push(format!("{}\n", res.to_string())); - Ok(Value::Void) - } -} - -/// 打印不换行 +/// 打印 #[derive(Debug)] pub struct Print { /// 要打印的表达式对象 pub expression: Box, + /// 是否换行 + pub is_newline: bool, } impl Expression for Print { fn evaluate(&self, ctx: &mut Context) -> Result { let res = self.expression.evaluate(ctx).unwrap(); ctx.output.push(res.to_string()); + if self.is_newline { + ctx.output.push(String::from("\n")); + } Ok(Value::Void) } } diff --git a/src/parse.rs b/src/parse.rs index 2bb00b0..af313ef 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,3 +1,5 @@ +#![deny(missing_docs)] + use std::collections::VecDeque; use crate::*; @@ -150,13 +152,8 @@ pub fn parse_sequence( v.push_back(var.1); start_line = var.0 + 1; } - Token::StdFunction(StdFunction::Println) => { - let var = parse_println(&lines[start_line])?; - v.push_back(var); - start_line += 1; - } - Token::StdFunction(StdFunction::Print) => { - let var = parse_print(&lines[start_line])?; + Token::StdFunction(StdFunction::Print(is_newline)) => { + let var = parse_print(&lines[start_line], *is_newline)?; v.push_back(var); start_line += 1; } @@ -224,18 +221,11 @@ pub fn parse_for( return Ok((cmd.0, box loop_expr)); } -fn parse_println(line: &[Token]) -> Result, failure::Error> { - debug!("{:?}", line); - let expression = parse_expression(&line[2..(line.len() - 1)])?; - Ok(box Println { - expression: expression, - }) -} - -fn parse_print(line: &[Token]) -> Result, failure::Error> { +fn parse_print(line: &[Token], is_newline: bool) -> Result, failure::Error> { debug!("{:?}", line); let expression = parse_expression(&line[2..(line.len() - 1)])?; Ok(box Print { - expression: expression, + expression, + is_newline, }) } diff --git a/src/token.rs b/src/token.rs index 4c54902..063b770 100644 --- a/src/token.rs +++ b/src/token.rs @@ -52,10 +52,8 @@ pub enum Operator { /// 标准库函数 #[derive(Debug, Eq, PartialEq, Clone)] pub enum StdFunction { - /// println - Println, - /// print - Print, + /// print bool表示是否换行 + Print(bool), } /// token 类型 @@ -113,7 +111,6 @@ pub fn tokenlizer(code: String) -> Result, failure::Error> { continue; } '\r' | '\n' => (Token::NewLine, 1), - '{' => (Token::LBig, 1), '}' => (Token::RBig, 1), '[' => (Token::LSquare, 1), @@ -174,8 +171,8 @@ pub fn tokenlizer(code: String) -> Result, failure::Error> { } let s: String = chars.as_slice()[i..j].iter().collect(); let token = match s.as_str() { - "println" => Token::StdFunction(StdFunction::Println), - "print" => Token::StdFunction(StdFunction::Print), + "println" => Token::StdFunction(StdFunction::Print(true)), + "print" => Token::StdFunction(StdFunction::Print(false)), "int" => Token::Keyword(Keyword::INT), "if" => Token::Keyword(Keyword::IF), "else" => Token::Keyword(Keyword::ELSE),