Skip to content

Commit

Permalink
打印换行缩减为一行
Browse files Browse the repository at this point in the history
  • Loading branch information
zuisong committed Jun 27, 2019
1 parent c36db31 commit 5c09276
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 41 deletions.
23 changes: 6 additions & 17 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,33 +112,22 @@ impl Expression for Not {
}
}

///
/// 打印换行
#[derive(Debug)]
pub struct Println {
/// 要打印换行的表达式对象
pub expression: Box<dyn Expression>,
}

impl Expression for Println {
fn evaluate(&self, ctx: &mut Context) -> Result<Value, failure::Error> {
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<dyn Expression>,
/// 是否换行
pub is_newline: bool,
}

impl Expression for Print {
fn evaluate(&self, ctx: &mut Context) -> Result<Value, failure::Error> {
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)
}
}
Expand Down
24 changes: 7 additions & 17 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![deny(missing_docs)]

use std::collections::VecDeque;

use crate::*;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -224,18 +221,11 @@ pub fn parse_for(
return Ok((cmd.0, box loop_expr));
}

fn parse_println(line: &[Token]) -> Result<Box<dyn Expression>, failure::Error> {
debug!("{:?}", line);
let expression = parse_expression(&line[2..(line.len() - 1)])?;
Ok(box Println {
expression: expression,
})
}

fn parse_print(line: &[Token]) -> Result<Box<dyn Expression>, failure::Error> {
fn parse_print(line: &[Token], is_newline: bool) -> Result<Box<dyn Expression>, failure::Error> {
debug!("{:?}", line);
let expression = parse_expression(&line[2..(line.len() - 1)])?;
Ok(box Print {
expression: expression,
expression,
is_newline,
})
}
11 changes: 4 additions & 7 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ pub enum Operator {
/// 标准库函数
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum StdFunction {
/// println
Println,
/// print
Print,
/// print bool表示是否换行
Print(bool),
}

/// token 类型
Expand Down Expand Up @@ -113,7 +111,6 @@ pub fn tokenlizer(code: String) -> Result<Vec<Token>, failure::Error> {
continue;
}
'\r' | '\n' => (Token::NewLine, 1),

'{' => (Token::LBig, 1),
'}' => (Token::RBig, 1),
'[' => (Token::LSquare, 1),
Expand Down Expand Up @@ -174,8 +171,8 @@ pub fn tokenlizer(code: String) -> Result<Vec<Token>, 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),
Expand Down

0 comments on commit 5c09276

Please sign in to comment.