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};
|
use crate::{parser::{Expr, BinOpType, UnOpType, Ast, Stmt, parse}, lexer::lex};
|
||||||
|
|
||||||
@ -41,6 +41,10 @@ impl Interpreter {
|
|||||||
let result = self.resolve_expr(expr);
|
let result = self.resolve_expr(expr);
|
||||||
println!("{:?}", result);
|
println!("{:?}", result);
|
||||||
}
|
}
|
||||||
|
Stmt::Print(expr) => {
|
||||||
|
let result = self.resolve_expr(expr);
|
||||||
|
print!("{}", result);
|
||||||
|
}
|
||||||
Stmt::Let(name, rhs) => {
|
Stmt::Let(name, rhs) => {
|
||||||
let result = self.resolve_expr(rhs);
|
let result = self.resolve_expr(rhs);
|
||||||
self.vartable.insert(name.clone(), result);
|
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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::{Interpreter, Value};
|
use super::{Interpreter, Value};
|
||||||
|
|||||||
@ -28,6 +28,9 @@ pub enum Token {
|
|||||||
/// Dollar sign ($)
|
/// Dollar sign ($)
|
||||||
Dollar,
|
Dollar,
|
||||||
|
|
||||||
|
/// Double Dollar sign ($$)
|
||||||
|
DoubleDollar,
|
||||||
|
|
||||||
/// Let identifier (let)
|
/// Let identifier (let)
|
||||||
Let,
|
Let,
|
||||||
|
|
||||||
@ -164,6 +167,10 @@ impl<'a> Lexer<'a> {
|
|||||||
self.next();
|
self.next();
|
||||||
tokens.push(Token::Ge);
|
tokens.push(Token::Ge);
|
||||||
}
|
}
|
||||||
|
'$' if matches!(self.peek(), Some('$')) => {
|
||||||
|
self.next();
|
||||||
|
tokens.push(Token::DoubleDollar);
|
||||||
|
}
|
||||||
|
|
||||||
'+' => tokens.push(Token::Add),
|
'+' => tokens.push(Token::Add),
|
||||||
'-' => tokens.push(Token::Sub),
|
'-' => tokens.push(Token::Sub),
|
||||||
|
|||||||
@ -76,6 +76,7 @@ pub enum Stmt {
|
|||||||
While(Expr, Ast),
|
While(Expr, Ast),
|
||||||
If(Expr, Ast, Ast),
|
If(Expr, Ast, Ast),
|
||||||
DbgPrint(Expr),
|
DbgPrint(Expr),
|
||||||
|
Print(Expr),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
@ -142,6 +143,10 @@ impl<T: Iterator<Item = Token>> Parser<T> {
|
|||||||
Token::While => self.parse_while(),
|
Token::While => self.parse_while(),
|
||||||
Token::If => self.parse_if(),
|
Token::If => self.parse_if(),
|
||||||
Token::Dollar => {
|
Token::Dollar => {
|
||||||
|
self.next();
|
||||||
|
Stmt::Print(self.parse_expr())
|
||||||
|
}
|
||||||
|
Token::DoubleDollar => {
|
||||||
self.next();
|
self.next();
|
||||||
Stmt::DbgPrint(self.parse_expr())
|
Stmt::DbgPrint(self.parse_expr())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user