Created set_angle_unit() builder function for the parser context.

This commit is contained in:
PaddiM8 2020-06-04 18:56:49 +02:00
parent 1e5159f84f
commit bccbbee2c4
3 changed files with 11 additions and 15 deletions

7
.vscode/launch.json vendored
View File

@ -1,7 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}

View File

@ -13,7 +13,7 @@ pub struct Context<'a> {
} }
impl<'a> Context<'a> { impl<'a> Context<'a> {
pub fn new(symbol_table: &'a mut SymbolTable, angle_unit: Unit, precision: u32) -> Self { pub fn new(symbol_table: &'a mut SymbolTable, angle_unit: &Unit, precision: u32) -> Self {
Context { Context {
angle_unit: angle_unit.clone(), angle_unit: angle_unit.clone(),
symbol_table, symbol_table,

View File

@ -11,6 +11,7 @@ pub struct Context {
tokens: Vec<Token>, tokens: Vec<Token>,
pos: usize, pos: usize,
symbol_table: SymbolTable, symbol_table: SymbolTable,
angle_unit: Unit,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Unit { pub enum Unit {
@ -24,16 +25,18 @@ impl Context {
tokens: Vec::new(), tokens: Vec::new(),
pos: 0, pos: 0,
symbol_table: SymbolTable::new(), symbol_table: SymbolTable::new(),
} angle_unit: Unit::Radians,
} }
} }
pub fn parse( pub fn set_angle_unit(mut self, unit: Unit) -> Self {
context: &mut Context, self.angle_unit = unit;
input: &str,
angle_unit: Unit, self
precision: u32, }
) -> Result<Option<Float>, String> { }
pub fn parse(context: &mut Context, input: &str, precision: u32) -> Result<Option<Float>, String> {
context.tokens = Lexer::lex(input); context.tokens = Lexer::lex(input);
context.pos = 0; context.pos = 0;
@ -42,7 +45,7 @@ pub fn parse(
statements.push(parse_stmt(context)?); statements.push(parse_stmt(context)?);
} }
let mut interpreter = let mut interpreter =
interpreter::Context::new(&mut context.symbol_table, angle_unit, precision); interpreter::Context::new(&mut context.symbol_table, &context.angle_unit, precision);
interpreter.interpret(statements) interpreter.interpret(statements)
} }