diff --git a/src/interpreter.rs b/src/interpreter.rs index 589618d..b0665f0 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -1,8 +1,8 @@ use miette::SourceSpan; -use tracing::{info_span, span}; +use tracing::info_span; use crate::{ - ast::{Ast, BinaryOp, Expr, Literal, UnaryOp}, + ast::{Ast, BinaryOp, Expr, UnaryOp}, span::{Span, Spanned}, value::Value, }; @@ -14,8 +14,8 @@ pub fn interpret(ast: &Ast) -> Result { } #[cfg(test)] -pub fn interpret_expr(expr: Spanned) -> Result { - Interpreter.interpret_expr(&expr) +pub fn interpret_expr(expr: &Spanned) -> Result { + Interpreter.interpret_expr(expr) } struct Interpreter; @@ -42,25 +42,25 @@ impl Interpreter { BinaryOp::Add => match (&left_val, &right_val) { (Value::String(left_str), Value::String(right_str)) => { let mut s = left_str.clone(); - s.push_str(&right_str); + s.push_str(right_str); Ok(Value::String(s)) } (Value::String(_), _) => Err(RuntimeError::non_string_concat( - right_val, + &right_val, right.span(), op.span(), )), _ => { let left_num = left_val.as_number().ok_or_else(|| { RuntimeError::non_number_arithmetic( - left_val, + &left_val, left.span(), op.span(), ) })?; let right_num = right_val.as_number().ok_or_else(|| { RuntimeError::non_number_arithmetic( - right_val, + &right_val, right.span(), op.span(), ) @@ -70,65 +70,65 @@ impl Interpreter { }, BinaryOp::Sub => { let left_num = left_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(left_val, left.span(), op.span()) + RuntimeError::non_number_arithmetic(&left_val, left.span(), op.span()) })?; let right_num = right_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(right_val, left.span(), op.span()) + RuntimeError::non_number_arithmetic(&right_val, left.span(), op.span()) })?; Ok(Value::Number(left_num - right_num)) } BinaryOp::Mult => { let left_num = left_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(left_val, left.span(), op.span()) + RuntimeError::non_number_arithmetic(&left_val, left.span(), op.span()) })?; let right_num = right_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(right_val, right.span(), op.span()) + RuntimeError::non_number_arithmetic(&right_val, right.span(), op.span()) })?; Ok(Value::Number(left_num * right_num)) } BinaryOp::Div => { let left_num = left_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(left_val, left.span(), op.span()) + RuntimeError::non_number_arithmetic(&left_val, left.span(), op.span()) })?; let right_num = right_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(right_val, right.span(), op.span()) + RuntimeError::non_number_arithmetic(&right_val, right.span(), op.span()) })?; Ok(Value::Number(left_num / right_num)) } BinaryOp::Greater => { let left_num = left_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(left_val, left.span(), op.span()) + RuntimeError::non_number_arithmetic(&left_val, left.span(), op.span()) })?; let right_num = right_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(right_val, right.span(), op.span()) + RuntimeError::non_number_arithmetic(&right_val, right.span(), op.span()) })?; Ok(Value::Bool(left_num > right_num)) } BinaryOp::GreaterEq => { let left_num = left_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(left_val, left.span(), op.span()) + RuntimeError::non_number_arithmetic(&left_val, left.span(), op.span()) })?; let right_num = right_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(right_val, right.span(), op.span()) + RuntimeError::non_number_arithmetic(&right_val, right.span(), op.span()) })?; Ok(Value::Bool(left_num >= right_num)) } BinaryOp::Less => { let left_num = left_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(left_val, left.span(), op.span()) + RuntimeError::non_number_arithmetic(&left_val, left.span(), op.span()) })?; let right_num = right_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(right_val, right.span(), op.span()) + RuntimeError::non_number_arithmetic(&right_val, right.span(), op.span()) })?; Ok(Value::Bool(left_num < right_num)) } BinaryOp::LessEq => { let left_num = left_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(left_val, left.span(), op.span()) + RuntimeError::non_number_arithmetic(&left_val, left.span(), op.span()) })?; let right_num = right_val.as_number().ok_or_else(|| { - RuntimeError::non_number_arithmetic(right_val, right.span(), op.span()) + RuntimeError::non_number_arithmetic(&right_val, right.span(), op.span()) })?; Ok(Value::Bool(left_num <= right_num)) } @@ -200,7 +200,7 @@ pub enum RuntimeError { } impl RuntimeError { - fn non_string_concat(value: Value, value_span: Span, op_span: Span) -> Self { + fn non_string_concat(value: &Value, value_span: Span, op_span: Span) -> Self { Self::NonStringConcat { actual_type: value.type_str(), value_span: value_span.into(), @@ -208,7 +208,7 @@ impl RuntimeError { } } - fn non_number_arithmetic(value: Value, value_span: Span, op_span: Span) -> Self { + fn non_number_arithmetic(value: &Value, value_span: Span, op_span: Span) -> Self { Self::NonNumberArithmetic { actual_type: value.type_str(), value_span: value_span.into(), @@ -228,7 +228,7 @@ mod test { scan(input).unwrap_or_else(|e| panic!("input `{input}` should scan. error: {e:?}")); let ast = parse(tokens).unwrap_or_else(|e| panic!("input `{input}` should parse. error: {e:?}")); - interpret_expr(ast.0) + interpret_expr(&ast.0) .unwrap_or_else(|e| panic!("input `{input}` to be interpreted. error: {e:?}")) } @@ -237,7 +237,7 @@ mod test { scan(input).unwrap_or_else(|e| panic!("input `{input}` should scan. error: {e:?}")); let ast = parse(tokens).unwrap_or_else(|e| panic!("input `{input}` should parse. error: {e:?}")); - interpret_expr(ast.0).unwrap_err() + interpret_expr(&ast.0).unwrap_err() } #[test] @@ -256,7 +256,7 @@ mod test { #[test] fn interprets_concatenation() { let value = interpret_expr_to_value(r#""hello " + "world""#); - assert_eq!(value, Value::String("hello world".to_string())) + assert_eq!(value, Value::String("hello world".to_string())); } #[test] @@ -357,7 +357,7 @@ mod test { interpret_expr_to_err(input), RuntimeError::NonNumberArithmetic { .. }, "input: {input}", - ) + ); } } @@ -368,7 +368,7 @@ mod test { interpret_expr_to_err(input), RuntimeError::NonStringConcat { .. }, "input: {input}" - ) + ); } } diff --git a/src/runners/repl.rs b/src/runners/repl.rs index e3ae67e..c94b9a9 100644 --- a/src/runners/repl.rs +++ b/src/runners/repl.rs @@ -43,22 +43,21 @@ pub fn run_repl() -> Result<()> { } }; - let ast = if !tokens.is_empty() { - match parse(tokens) { - Ok(ast) => ast, - Err(e) => { - let report = Report::new(e).with_source_code(line.clone()); - eprintln!("{report:?}"); - - prompt()?; - continue; - } - } - } else { + if tokens.is_empty() { prompt()?; continue; + } + + let ast = match parse(tokens) { + Ok(ast) => ast, + Err(e) => { + let report = Report::new(e).with_source_code(line.clone()); + eprintln!("{report:?}"); + + prompt()?; + continue; + } }; - //dbg!(&ast); println!("{}", ast.print_rpn()); let final_value = match interpret(&ast) {