Refactor Binding

master
Araozu 2023-12-16 20:51:12 -05:00
parent 3e592392a8
commit 2830a1befd
4 changed files with 19 additions and 51 deletions

View File

@ -4,38 +4,27 @@ use crate::syntax::ast::var_binding::Binding;
impl Transpilable for Binding { impl Transpilable for Binding {
/// Transpiles val and var bindings into PHP. /// Transpiles val and var bindings into PHP.
fn transpile(&self) -> String { fn transpile(&self) -> String {
match self { let expression_str = self.expression.transpile();
Binding::Val(val_binding) => {
let expression_str = val_binding.expression.transpile();
format!("${} = {};", val_binding.identifier, expression_str) format!("${} = {};", self.identifier, expression_str)
}
Binding::Var(var_binding) => {
let expression_str = var_binding.expression.transpile();
format!("${} = {};", var_binding.identifier, expression_str)
}
}
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::syntax::ast::{ use crate::syntax::ast::{var_binding::Binding, Expression};
var_binding::{Binding, ValBinding},
Expression,
};
#[test] #[test]
fn binding_should_transpile() { fn binding_should_transpile() {
let id = String::from("identifier"); let id = String::from("identifier");
let value = String::from("322"); let value = String::from("322");
let binding = Binding::Val(ValBinding { let binding = Binding {
datatype: None, datatype: None,
identifier: Box::new(id), identifier: Box::new(id),
expression: Expression::Number(Box::new(value)), expression: Expression::Number(Box::new(value)),
}); is_mutable: false,
};
let result = binding.transpile(); let result = binding.transpile();

View File

@ -18,20 +18,18 @@ impl Transpilable for ModuleAST {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::syntax::ast::{ use crate::syntax::ast::{var_binding::Binding, Expression, TopLevelDeclaration};
var_binding::{Binding, ValBinding},
Expression, TopLevelDeclaration,
};
#[test] #[test]
fn module_ast_should_transpile() { fn module_ast_should_transpile() {
let id = String::from("identifier"); let id = String::from("identifier");
let value = String::from("322"); let value = String::from("322");
let binding = Binding::Val(ValBinding { let binding = Binding {
datatype: None, datatype: None,
identifier: Box::new(id), identifier: Box::new(id),
expression: Expression::Number(Box::new(value)), expression: Expression::Number(Box::new(value)),
}); is_mutable: false,
};
let module = ModuleAST { let module = ModuleAST {
declarations: vec![TopLevelDeclaration::Binding(binding)], declarations: vec![TopLevelDeclaration::Binding(binding)],

View File

@ -1,21 +1,9 @@
use super::Expression; use super::Expression;
#[derive(Debug)] #[derive(Debug)]
pub enum Binding { pub struct Binding {
Val(ValBinding),
Var(VarBinding),
}
#[derive(Debug)]
pub struct ValBinding {
pub datatype: Option<String>,
pub identifier: Box<String>,
pub expression: Expression,
}
#[derive(Debug)]
pub struct VarBinding {
pub datatype: Option<String>, pub datatype: Option<String>,
pub identifier: Box<String>, pub identifier: Box<String>,
pub expression: Expression, pub expression: Expression,
pub is_mutable: bool,
} }

View File

@ -1,4 +1,4 @@
use super::ast::var_binding::{Binding, ValBinding, VarBinding}; use super::ast::var_binding::Binding;
use super::utils::{parse_token_type, try_operator}; use super::utils::{parse_token_type, try_operator};
use super::{expression, ParseResult}; use super::{expression, ParseResult};
use crate::error_handling::SyntaxError; use crate::error_handling::SyntaxError;
@ -95,18 +95,11 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParseResult<Binding,
}; };
current_pos = next_pos; current_pos = next_pos;
let binding = if !is_mutable { let binding = Binding {
Binding::Val(ValBinding { datatype: None,
datatype: None, identifier: Box::new(identifier.value.clone()),
identifier: Box::new(identifier.value.clone()), expression,
expression, is_mutable,
})
} else {
Binding::Var(VarBinding {
datatype: None,
identifier: Box::new(identifier.value.clone()),
expression,
})
}; };
ParseResult::Ok(binding, current_pos) ParseResult::Ok(binding, current_pos)
@ -120,7 +113,7 @@ mod tests {
#[test] #[test]
fn should_parse_val_binding() { fn should_parse_val_binding() {
let tokens = get_tokens(&String::from("let identifier = 20")).unwrap(); let tokens = get_tokens(&String::from("let identifier = 20")).unwrap();
let ParseResult::Ok(Binding::Val(binding), _) = try_parse(&tokens, 0) else { let ParseResult::Ok(binding, _) = try_parse(&tokens, 0) else {
panic!() panic!()
}; };