mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-03-01 06:41:15 +01:00
Renamed parser::parse to parser::eval and created a new parser::parse function that does not evalutate the statements.
This commit is contained in:
parent
b4f129835a
commit
42a9e7e82a
@ -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> {
|
||||
|
@ -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.
|
||||
|
@ -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 };
|
||||
|
Loading…
Reference in New Issue
Block a user