Implement non-debug print
This commit is contained in:
parent
abf9eb73c8
commit
d2daa7ae6d
@ -1,4 +1,4 @@
|
||||
use std::{collections::HashMap, rc::Rc};
|
||||
use std::{collections::HashMap, rc::Rc, fmt::Display};
|
||||
|
||||
use crate::{parser::{Expr, BinOpType, UnOpType, Ast, Stmt, parse}, lexer::lex};
|
||||
|
||||
@ -41,6 +41,10 @@ impl Interpreter {
|
||||
let result = self.resolve_expr(expr);
|
||||
println!("{:?}", result);
|
||||
}
|
||||
Stmt::Print(expr) => {
|
||||
let result = self.resolve_expr(expr);
|
||||
print!("{}", result);
|
||||
}
|
||||
Stmt::Let(name, rhs) => {
|
||||
let result = self.resolve_expr(rhs);
|
||||
self.vartable.insert(name.clone(), result);
|
||||
@ -139,6 +143,16 @@ impl Interpreter {
|
||||
|
||||
}
|
||||
|
||||
impl Display for Value {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Value::I64(val) => write!(f, "{}", val),
|
||||
Value::Str(text) => write!(f, "{}", text),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{Interpreter, Value};
|
||||
|
||||
@ -28,6 +28,9 @@ pub enum Token {
|
||||
/// Dollar sign ($)
|
||||
Dollar,
|
||||
|
||||
/// Double Dollar sign ($$)
|
||||
DoubleDollar,
|
||||
|
||||
/// Let identifier (let)
|
||||
Let,
|
||||
|
||||
@ -164,6 +167,10 @@ impl<'a> Lexer<'a> {
|
||||
self.next();
|
||||
tokens.push(Token::Ge);
|
||||
}
|
||||
'$' if matches!(self.peek(), Some('$')) => {
|
||||
self.next();
|
||||
tokens.push(Token::DoubleDollar);
|
||||
}
|
||||
|
||||
'+' => tokens.push(Token::Add),
|
||||
'-' => tokens.push(Token::Sub),
|
||||
|
||||
@ -76,6 +76,7 @@ pub enum Stmt {
|
||||
While(Expr, Ast),
|
||||
If(Expr, Ast, Ast),
|
||||
DbgPrint(Expr),
|
||||
Print(Expr),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
@ -142,6 +143,10 @@ impl<T: Iterator<Item = Token>> Parser<T> {
|
||||
Token::While => self.parse_while(),
|
||||
Token::If => self.parse_if(),
|
||||
Token::Dollar => {
|
||||
self.next();
|
||||
Stmt::Print(self.parse_expr())
|
||||
}
|
||||
Token::DoubleDollar => {
|
||||
self.next();
|
||||
Stmt::DbgPrint(self.parse_expr())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user