Renamed parser::parse to parser::eval and created a new parser::parse function that does not evalutate the statements.

This commit is contained in:
PaddiM8 2020-06-10 13:55:43 +02:00
parent b4f129835a
commit 42a9e7e82a
3 changed files with 13 additions and 6 deletions

View File

@ -54,11 +54,19 @@ pub enum CalcError {
Unknown,
}
pub fn parse(
pub fn eval(
context: &mut Context,
input: &str,
precision: u32,
) -> Result<Option<Float>, CalcError> {
let statements = parse(context, input)?;
let mut interpreter =
interpreter::Context::new(&mut context.symbol_table, &context.angle_unit, precision);
interpreter.interpret(statements)
}
pub fn parse(context: &mut Context, input: &str) -> Result<Vec<Stmt>, CalcError> {
context.tokens = Lexer::lex(input);
context.pos = 0;
@ -66,9 +74,8 @@ pub fn parse(
while !is_at_end(context) {
statements.push(parse_stmt(context)?);
}
let mut interpreter =
interpreter::Context::new(&mut context.symbol_table, &context.angle_unit, precision);
interpreter.interpret(statements)
Ok(statements)
}
fn parse_stmt(context: &mut Context) -> Result<Stmt, CalcError> {

View File

@ -31,7 +31,7 @@ fn main() {
// Parse the input file content, resulting in the symbol table being filled out.
// Output is not needed here.
parser::parse(&mut parser_context, &file_content, 53)
parser::eval(&mut parser_context, &file_content, 53)
.expect("Failed to parse input file.");
} else {
// Main argument. This is expected to be a maths expression.

View File

@ -2,7 +2,7 @@ use ansi_term::Colour::Red;
use kalk::parser::{self, CalcError, CalcError::*};
pub fn eval(parser: &mut parser::Context, input: &str) {
match parser::parse(parser, input, 53) {
match parser::eval(parser, input, 53) {
Ok(Some(result)) => {
let (_, digits, exp_option) = result.to_sign_string_exp(10, None);
let exp = if let Some(exp) = exp_option { exp } else { 0 };