Refactor Binding
This commit is contained in:
parent
3e592392a8
commit
2830a1befd
@ -4,38 +4,27 @@ use crate::syntax::ast::var_binding::Binding;
|
||||
impl Transpilable for Binding {
|
||||
/// Transpiles val and var bindings into PHP.
|
||||
fn transpile(&self) -> String {
|
||||
match self {
|
||||
Binding::Val(val_binding) => {
|
||||
let expression_str = val_binding.expression.transpile();
|
||||
let expression_str = self.expression.transpile();
|
||||
|
||||
format!("${} = {};", val_binding.identifier, expression_str)
|
||||
}
|
||||
Binding::Var(var_binding) => {
|
||||
let expression_str = var_binding.expression.transpile();
|
||||
|
||||
format!("${} = {};", var_binding.identifier, expression_str)
|
||||
}
|
||||
}
|
||||
format!("${} = {};", self.identifier, expression_str)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::syntax::ast::{
|
||||
var_binding::{Binding, ValBinding},
|
||||
Expression,
|
||||
};
|
||||
use crate::syntax::ast::{var_binding::Binding, Expression};
|
||||
|
||||
#[test]
|
||||
fn binding_should_transpile() {
|
||||
let id = String::from("identifier");
|
||||
let value = String::from("322");
|
||||
let binding = Binding::Val(ValBinding {
|
||||
let binding = Binding {
|
||||
datatype: None,
|
||||
identifier: Box::new(id),
|
||||
expression: Expression::Number(Box::new(value)),
|
||||
});
|
||||
is_mutable: false,
|
||||
};
|
||||
|
||||
let result = binding.transpile();
|
||||
|
||||
|
@ -18,20 +18,18 @@ impl Transpilable for ModuleAST {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::syntax::ast::{
|
||||
var_binding::{Binding, ValBinding},
|
||||
Expression, TopLevelDeclaration,
|
||||
};
|
||||
use crate::syntax::ast::{var_binding::Binding, Expression, TopLevelDeclaration};
|
||||
|
||||
#[test]
|
||||
fn module_ast_should_transpile() {
|
||||
let id = String::from("identifier");
|
||||
let value = String::from("322");
|
||||
let binding = Binding::Val(ValBinding {
|
||||
let binding = Binding {
|
||||
datatype: None,
|
||||
identifier: Box::new(id),
|
||||
expression: Expression::Number(Box::new(value)),
|
||||
});
|
||||
is_mutable: false,
|
||||
};
|
||||
|
||||
let module = ModuleAST {
|
||||
declarations: vec![TopLevelDeclaration::Binding(binding)],
|
||||
|
@ -1,21 +1,9 @@
|
||||
use super::Expression;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum 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 struct Binding {
|
||||
pub datatype: Option<String>,
|
||||
pub identifier: Box<String>,
|
||||
pub expression: Expression,
|
||||
pub is_mutable: bool,
|
||||
}
|
||||
|
@ -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::{expression, ParseResult};
|
||||
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;
|
||||
|
||||
let binding = if !is_mutable {
|
||||
Binding::Val(ValBinding {
|
||||
let binding = Binding {
|
||||
datatype: None,
|
||||
identifier: Box::new(identifier.value.clone()),
|
||||
expression,
|
||||
})
|
||||
} else {
|
||||
Binding::Var(VarBinding {
|
||||
datatype: None,
|
||||
identifier: Box::new(identifier.value.clone()),
|
||||
expression,
|
||||
})
|
||||
is_mutable,
|
||||
};
|
||||
|
||||
ParseResult::Ok(binding, current_pos)
|
||||
@ -120,7 +113,7 @@ mod tests {
|
||||
#[test]
|
||||
fn should_parse_val_binding() {
|
||||
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!()
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user