diff --git a/src/codegen/php/expression/assignment.rs b/src/codegen/php/expression/assignment.rs index f83a98e..9e65313 100644 --- a/src/codegen/php/expression/assignment.rs +++ b/src/codegen/php/expression/assignment.rs @@ -1,7 +1,4 @@ -use crate::{ - codegen::Transpilable, - php_ast::{php_ast_2::PSimpleAssignment, PhpAssignmentExpression}, -}; +use crate::{codegen::Transpilable, php_ast::PSimpleAssignment}; impl Transpilable for PSimpleAssignment<'_> { fn transpile(&self) -> String { @@ -14,10 +11,6 @@ impl Transpilable for PSimpleAssignment<'_> { #[cfg(test)] mod tests { - use crate::{ - codegen::Transpilable, - php_ast::{PhpAssignmentExpression, PhpPrimaryExpression, PhpSimpleAssignment}, - }; /* #[test] diff --git a/src/codegen/php/expression/mod.rs b/src/codegen/php/expression/mod.rs index 275707a..acf6352 100644 --- a/src/codegen/php/expression/mod.rs +++ b/src/codegen/php/expression/mod.rs @@ -1,4 +1,4 @@ -use crate::{codegen::Transpilable, php_ast::php_ast_2::PExpresssion}; +use crate::{codegen::Transpilable, php_ast::PExpresssion}; use PExpresssion::*; mod assignment; diff --git a/src/codegen/php/expression/primary_expression.rs b/src/codegen/php/expression/primary_expression.rs index d075cc3..624b3d1 100644 --- a/src/codegen/php/expression/primary_expression.rs +++ b/src/codegen/php/expression/primary_expression.rs @@ -1,7 +1,4 @@ -use crate::{ - codegen::Transpilable, - php_ast::{php_ast_2::PPrimary, PhpPrimaryExpression}, -}; +use crate::{codegen::Transpilable, php_ast::PPrimary}; impl Transpilable for PPrimary<'_> { fn transpile(&self) -> String { @@ -17,7 +14,6 @@ impl Transpilable for PPrimary<'_> { #[cfg(test)] mod tests { - use crate::{codegen::Transpilable, php_ast::PhpPrimaryExpression}; /* #[test] diff --git a/src/codegen/php/function/mod.rs b/src/codegen/php/function/mod.rs index dddb260..f003bdc 100644 --- a/src/codegen/php/function/mod.rs +++ b/src/codegen/php/function/mod.rs @@ -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<'_> { fn transpile(&self) -> String { diff --git a/src/codegen/php/mod.rs b/src/codegen/php/mod.rs index 5a5ef5e..afb5956 100644 --- a/src/codegen/php/mod.rs +++ b/src/codegen/php/mod.rs @@ -1,7 +1,4 @@ -use super::Transpilable; -use crate::php_ast::PhpExpression; - mod expression; +mod function; pub mod statement; pub mod statement_list; -mod function; diff --git a/src/codegen/php/statement/mod.rs b/src/codegen/php/statement/mod.rs index aa24515..fa64cb3 100644 --- a/src/codegen/php/statement/mod.rs +++ b/src/codegen/php/statement/mod.rs @@ -1,7 +1,4 @@ -use crate::{ - codegen::Transpilable, - php_ast::{php_ast_2::PStatement, PhpStatement}, -}; +use crate::{codegen::Transpilable, php_ast::PStatement}; impl Transpilable for PStatement<'_> { fn transpile(&self) -> String { @@ -16,13 +13,6 @@ impl Transpilable for PStatement<'_> { #[cfg(test)] mod tests { - use crate::{ - codegen::Transpilable, - php_ast::{ - PhpAssignmentExpression, PhpExpression, PhpExpressionList, PhpPrimaryExpression, - PhpStatement, - }, - }; /* #[test] diff --git a/src/codegen/php/statement_list.rs b/src/codegen/php/statement_list.rs index 7b5f1be..83e4d80 100644 --- a/src/codegen/php/statement_list.rs +++ b/src/codegen/php/statement_list.rs @@ -1,7 +1,4 @@ -use crate::{ - codegen::Transpilable, - php_ast::{php_ast_2::PFile, PhpAst}, -}; +use crate::{codegen::Transpilable, php_ast::PFile}; impl Transpilable for PFile<'_> { fn transpile(&self) -> String { @@ -17,13 +14,7 @@ impl Transpilable for PFile<'_> { #[cfg(test)] mod tests { - use crate::{ - codegen::Transpilable, - php_ast::{ - php_ast_2::PFile, PhpAssignmentExpression, PhpAst, PhpExpression, PhpPrimaryExpression, - PhpStatement, - }, - }; + use crate::{codegen::Transpilable, php_ast::PFile}; #[test] fn should_transpile_empty_file() { diff --git a/src/php_ast/mod.rs b/src/php_ast/mod.rs index 77cdf0b..ef9bde6 100644 --- a/src/php_ast/mod.rs +++ b/src/php_ast/mod.rs @@ -1,5 +1,3 @@ -use crate::codegen::Transpilable; - /// This AST implements a subset of the PHP AST as defined /// by https://phplang.org/spec/19-grammar.html#syntactic-grammar /// @@ -7,56 +5,59 @@ use crate::codegen::Transpilable; /// THP pub mod transformers; -pub mod php_ast_2; - -type TranspilableBox<'a> = Box<(dyn Transpilable + 'a)>; - -/// Represents `statement-list` on the grammar, -/// and thus a whole PHP source file -pub struct PhpAst<'a> { - pub statements: Vec>, +/// A single PHP source code file +pub struct PFile<'a> { + pub statements: Vec>, } -/// 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: -/// echo-statement -pub enum PhpStatement<'a> { - PhpEchoStatement(PhpExpressionList<'a>), - PhpExpressionStatement(PhpExpression<'a>), +/// ```php +/// 10; +/// "hello"; +/// ``` +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 expressions: Vec>, +pub struct PSimpleAssignment<'a> { + pub variable: &'a String, + pub assignment: Box>, } -pub enum PhpExpression<'a> { - Assignment(PhpAssignmentExpression<'a>), +/// A function call as an expression +pub struct PFunctionCall<'a> { + /// Arbitrary expressions that compute into + /// a function not supported + pub function_name: &'a String, + pub arguments: Vec>, } -pub enum PhpAssignmentExpression<'a> { - Primary(PhpPrimaryExpression<'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> { +/// A Primary expression: literals and variables +pub enum PPrimary<'a> { IntegerLiteral(&'a String), FloatingLiteral(&'a String), StringLiteral(&'a String), /// https://phplang.org/spec/19-grammar.html#grammar-variable /// /// Supports only variable -> callable-variable -> simple-variable -> variable-name + /// + /// This is a $variable Variable(&'a String), + /// This is a symbol, e.g. a function name + Symbol(&'a String), } diff --git a/src/php_ast/transformers/expression.rs b/src/php_ast/transformers/expression.rs index e39fbeb..208a632 100644 --- a/src/php_ast/transformers/expression.rs +++ b/src/php_ast/transformers/expression.rs @@ -1,10 +1,5 @@ -use super::super::PhpExpression; use crate::{ - codegen::Transpilable, - php_ast::{ - php_ast_2::{PExpresssion, PFunctionCall, PPrimary}, - PhpAssignmentExpression, PhpPrimaryExpression, - }, + php_ast::{PExpresssion, PPrimary}, syntax::ast::Expression, }; @@ -36,9 +31,7 @@ impl<'a> PHPTransformable<'a> for Expression<'_> { PExpresssion::FunctionCall(fn_call_expr) } - Expression::Identifier(i) => { - PExpresssion::Primary(PPrimary::Variable(&i.value)) - } + Expression::Identifier(i) => PExpresssion::Primary(PPrimary::Variable(&i.value)), _ => todo!("transformation for expression: {:?}", self), } } diff --git a/src/php_ast/transformers/functions.rs b/src/php_ast/transformers/functions.rs index 827c97f..ed67154 100644 --- a/src/php_ast/transformers/functions.rs +++ b/src/php_ast/transformers/functions.rs @@ -1,5 +1,5 @@ use crate::{ - php_ast::php_ast_2::PFunctionCall, + php_ast::PFunctionCall, syntax::ast::{functions::FunctionCall, Expression}, }; @@ -11,7 +11,9 @@ impl<'a> PHPTransformable<'a> for FunctionCall<'a> { fn into_php_ast(&'a self) -> Self::Item { let function_expr = match *self.function { 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 diff --git a/src/php_ast/transformers/mod.rs b/src/php_ast/transformers/mod.rs index 71f1b4d..817294b 100644 --- a/src/php_ast/transformers/mod.rs +++ b/src/php_ast/transformers/mod.rs @@ -1,9 +1,7 @@ -use crate::codegen::Transpilable; - pub mod expression; +pub mod functions; pub mod module_ast; pub mod statement; -pub mod functions; /// Implemented by AST nodes that can be transformed to PHP pub trait PHPTransformable<'a> { diff --git a/src/php_ast/transformers/module_ast.rs b/src/php_ast/transformers/module_ast.rs index 6895695..30af215 100644 --- a/src/php_ast/transformers/module_ast.rs +++ b/src/php_ast/transformers/module_ast.rs @@ -1,10 +1,5 @@ -use super::super::PhpAst; -use crate::codegen::Transpilable; -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 crate::php_ast::{PFile, PStatement}; +use crate::syntax::ast::{ModuleAST, ModuleMembers}; use super::PHPTransformable; diff --git a/src/php_ast/transformers/statement.rs b/src/php_ast/transformers/statement.rs index 0b89429..efb3fa9 100644 --- a/src/php_ast/transformers/statement.rs +++ b/src/php_ast/transformers/statement.rs @@ -1,10 +1,5 @@ -use super::super::PhpStatement; use crate::{ - codegen::Transpilable, - php_ast::{ - php_ast_2::{PExpresssion, PSimpleAssignment, PStatement}, - PhpAssignmentExpression, PhpExpression, PhpSimpleAssignment, - }, + php_ast::{PExpresssion, PSimpleAssignment, PStatement}, syntax::ast::Statement, };