refactor: remove old code

master
Araozu 2024-08-26 16:25:20 -05:00
parent 974c380eaf
commit 6965eda6a7
13 changed files with 56 additions and 105 deletions

View File

@ -1,7 +1,4 @@
use crate::{ use crate::{codegen::Transpilable, php_ast::PSimpleAssignment};
codegen::Transpilable,
php_ast::{php_ast_2::PSimpleAssignment, PhpAssignmentExpression},
};
impl Transpilable for PSimpleAssignment<'_> { impl Transpilable for PSimpleAssignment<'_> {
fn transpile(&self) -> String { fn transpile(&self) -> String {
@ -14,10 +11,6 @@ impl Transpilable for PSimpleAssignment<'_> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{
codegen::Transpilable,
php_ast::{PhpAssignmentExpression, PhpPrimaryExpression, PhpSimpleAssignment},
};
/* /*
#[test] #[test]

View File

@ -1,4 +1,4 @@
use crate::{codegen::Transpilable, php_ast::php_ast_2::PExpresssion}; use crate::{codegen::Transpilable, php_ast::PExpresssion};
use PExpresssion::*; use PExpresssion::*;
mod assignment; mod assignment;

View File

@ -1,7 +1,4 @@
use crate::{ use crate::{codegen::Transpilable, php_ast::PPrimary};
codegen::Transpilable,
php_ast::{php_ast_2::PPrimary, PhpPrimaryExpression},
};
impl Transpilable for PPrimary<'_> { impl Transpilable for PPrimary<'_> {
fn transpile(&self) -> String { fn transpile(&self) -> String {
@ -17,7 +14,6 @@ impl Transpilable for PPrimary<'_> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{codegen::Transpilable, php_ast::PhpPrimaryExpression};
/* /*
#[test] #[test]

View File

@ -1,4 +1,4 @@
use crate::{codegen::Transpilable, php_ast::php_ast_2::PFunctionCall}; use crate::{codegen::Transpilable, php_ast::PFunctionCall};
impl Transpilable for PFunctionCall<'_> { impl Transpilable for PFunctionCall<'_> {
fn transpile(&self) -> String { fn transpile(&self) -> String {

View File

@ -1,7 +1,4 @@
use super::Transpilable;
use crate::php_ast::PhpExpression;
mod expression; mod expression;
mod function;
pub mod statement; pub mod statement;
pub mod statement_list; pub mod statement_list;
mod function;

View File

@ -1,7 +1,4 @@
use crate::{ use crate::{codegen::Transpilable, php_ast::PStatement};
codegen::Transpilable,
php_ast::{php_ast_2::PStatement, PhpStatement},
};
impl Transpilable for PStatement<'_> { impl Transpilable for PStatement<'_> {
fn transpile(&self) -> String { fn transpile(&self) -> String {
@ -16,13 +13,6 @@ impl Transpilable for PStatement<'_> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{
codegen::Transpilable,
php_ast::{
PhpAssignmentExpression, PhpExpression, PhpExpressionList, PhpPrimaryExpression,
PhpStatement,
},
};
/* /*
#[test] #[test]

View File

@ -1,7 +1,4 @@
use crate::{ use crate::{codegen::Transpilable, php_ast::PFile};
codegen::Transpilable,
php_ast::{php_ast_2::PFile, PhpAst},
};
impl Transpilable for PFile<'_> { impl Transpilable for PFile<'_> {
fn transpile(&self) -> String { fn transpile(&self) -> String {
@ -17,13 +14,7 @@ impl Transpilable for PFile<'_> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{codegen::Transpilable, php_ast::PFile};
codegen::Transpilable,
php_ast::{
php_ast_2::PFile, PhpAssignmentExpression, PhpAst, PhpExpression, PhpPrimaryExpression,
PhpStatement,
},
};
#[test] #[test]
fn should_transpile_empty_file() { fn should_transpile_empty_file() {

View File

@ -1,5 +1,3 @@
use crate::codegen::Transpilable;
/// This AST implements a subset of the PHP AST as defined /// This AST implements a subset of the PHP AST as defined
/// by https://phplang.org/spec/19-grammar.html#syntactic-grammar /// by https://phplang.org/spec/19-grammar.html#syntactic-grammar
/// ///
@ -7,56 +5,59 @@ use crate::codegen::Transpilable;
/// THP /// THP
pub mod transformers; pub mod transformers;
pub mod php_ast_2; /// A single PHP source code file
pub struct PFile<'a> {
type TranspilableBox<'a> = Box<(dyn Transpilable + 'a)>; pub statements: Vec<PStatement<'a>>,
/// Represents `statement-list` on the grammar,
/// and thus a whole PHP source file
pub struct PhpAst<'a> {
pub statements: Vec<TranspilableBox<'a>>,
} }
/// https://phplang.org/spec/19-grammar.html#grammar-statement /// A PHP statement
pub enum PStatement<'a> {
ExpressionStatement(PExpressionStatement<'a>),
}
/// A statement composed of a single expression,
/// whose value is discarded
/// ///
/// Not fully implemented /// ## Examples
/// ///
/// statement: /// ```php
/// echo-statement /// 10;
pub enum PhpStatement<'a> { /// "hello";
PhpEchoStatement(PhpExpressionList<'a>), /// ```
PhpExpressionStatement(PhpExpression<'a>), pub type PExpressionStatement<'a> = PExpresssion<'a>;
/// A generic PHP expression
pub enum PExpresssion<'a> {
FunctionCall(PFunctionCall<'a>),
Primary(PPrimary<'a>),
/// This comes from a THP binding
Assignment(PSimpleAssignment<'a>),
} }
pub struct PhpExpressionList<'a> { pub struct PSimpleAssignment<'a> {
pub expressions: Vec<PhpExpression<'a>>, pub variable: &'a String,
pub assignment: Box<PExpresssion<'a>>,
} }
pub enum PhpExpression<'a> { /// A function call as an expression
Assignment(PhpAssignmentExpression<'a>), pub struct PFunctionCall<'a> {
/// Arbitrary expressions that compute into
/// a function not supported
pub function_name: &'a String,
pub arguments: Vec<PExpresssion<'a>>,
} }
pub enum PhpAssignmentExpression<'a> { /// A Primary expression: literals and variables
Primary(PhpPrimaryExpression<'a>), pub enum PPrimary<'a> {
SimpleAssignment(PhpSimpleAssignment<'a>),
}
pub struct PhpSimpleAssignment<'a> {
pub variable: String,
pub assignment: TranspilableBox<'a>,
}
/// https://phplang.org/spec/19-grammar.html#grammar-primary-expression
///
/// primary-expression:
/// literal
/// variable
pub enum PhpPrimaryExpression<'a> {
IntegerLiteral(&'a String), IntegerLiteral(&'a String),
FloatingLiteral(&'a String), FloatingLiteral(&'a String),
StringLiteral(&'a String), StringLiteral(&'a String),
/// https://phplang.org/spec/19-grammar.html#grammar-variable /// https://phplang.org/spec/19-grammar.html#grammar-variable
/// ///
/// Supports only variable -> callable-variable -> simple-variable -> variable-name /// Supports only variable -> callable-variable -> simple-variable -> variable-name
///
/// This is a $variable
Variable(&'a String), Variable(&'a String),
/// This is a symbol, e.g. a function name
Symbol(&'a String),
} }

View File

@ -1,10 +1,5 @@
use super::super::PhpExpression;
use crate::{ use crate::{
codegen::Transpilable, php_ast::{PExpresssion, PPrimary},
php_ast::{
php_ast_2::{PExpresssion, PFunctionCall, PPrimary},
PhpAssignmentExpression, PhpPrimaryExpression,
},
syntax::ast::Expression, syntax::ast::Expression,
}; };
@ -36,9 +31,7 @@ impl<'a> PHPTransformable<'a> for Expression<'_> {
PExpresssion::FunctionCall(fn_call_expr) PExpresssion::FunctionCall(fn_call_expr)
} }
Expression::Identifier(i) => { Expression::Identifier(i) => PExpresssion::Primary(PPrimary::Variable(&i.value)),
PExpresssion::Primary(PPrimary::Variable(&i.value))
}
_ => todo!("transformation for expression: {:?}", self), _ => todo!("transformation for expression: {:?}", self),
} }
} }

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
php_ast::php_ast_2::PFunctionCall, php_ast::PFunctionCall,
syntax::ast::{functions::FunctionCall, Expression}, syntax::ast::{functions::FunctionCall, Expression},
}; };
@ -11,7 +11,9 @@ impl<'a> PHPTransformable<'a> for FunctionCall<'a> {
fn into_php_ast(&'a self) -> Self::Item { fn into_php_ast(&'a self) -> Self::Item {
let function_expr = match *self.function { let function_expr = match *self.function {
Expression::Identifier(i) => &i.value, Expression::Identifier(i) => &i.value,
_ => panic!("Cannot use an arbitrary expression as a function, only identifiers (for now)"), _ => panic!(
"Cannot use an arbitrary expression as a function, only identifiers (for now)"
),
}; };
let expressions: Vec<_> = self let expressions: Vec<_> = self

View File

@ -1,9 +1,7 @@
use crate::codegen::Transpilable;
pub mod expression; pub mod expression;
pub mod functions;
pub mod module_ast; pub mod module_ast;
pub mod statement; pub mod statement;
pub mod functions;
/// Implemented by AST nodes that can be transformed to PHP /// Implemented by AST nodes that can be transformed to PHP
pub trait PHPTransformable<'a> { pub trait PHPTransformable<'a> {

View File

@ -1,10 +1,5 @@
use super::super::PhpAst; use crate::php_ast::{PFile, PStatement};
use crate::codegen::Transpilable; use crate::syntax::ast::{ModuleAST, ModuleMembers};
use crate::php_ast::php_ast_2::{PFile, PStatement};
use crate::php_ast::{
PhpAssignmentExpression, PhpExpression, PhpExpressionList, PhpPrimaryExpression, PhpStatement,
};
use crate::syntax::ast::{Expression, ModuleAST, ModuleMembers};
use super::PHPTransformable; use super::PHPTransformable;

View File

@ -1,10 +1,5 @@
use super::super::PhpStatement;
use crate::{ use crate::{
codegen::Transpilable, php_ast::{PExpresssion, PSimpleAssignment, PStatement},
php_ast::{
php_ast_2::{PExpresssion, PSimpleAssignment, PStatement},
PhpAssignmentExpression, PhpExpression, PhpSimpleAssignment,
},
syntax::ast::Statement, syntax::ast::Statement,
}; };