refactor: remove old codegen, test new codegen nodes

master
Araozu 2024-07-31 09:58:51 -05:00
parent 23b3ece588
commit a62d08455b
19 changed files with 207 additions and 327 deletions

View File

@ -1,41 +0,0 @@
use super::Transpilable;
use crate::syntax::ast::var_binding::VariableBinding;
impl Transpilable for VariableBinding<'_> {
/// Transpiles val and var bindings into PHP.
fn transpile(&self) -> String {
let expression_str = self.expression.transpile();
format!("${} = {}", self.identifier.value, expression_str)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
lexic::token::{Token, TokenType},
syntax::ast::{var_binding::VariableBinding, Expression},
};
#[test]
fn binding_should_transpile() {
let id = String::from("identifier");
let id_token = Token {
token_type: TokenType::Identifier,
value: id,
position: 0,
};
let value = String::from("322");
let binding = VariableBinding {
datatype: None,
identifier: &id_token,
expression: Expression::Int(&value),
is_mutable: false,
};
let result = binding.transpile();
assert_eq!("$identifier = 322", result);
}
}

View File

@ -1,17 +0,0 @@
use crate::syntax::ast::Block;
use super::Transpilable;
impl Transpilable for Block<'_> {
fn transpile(&self) -> String {
// TODO: Handle indentation
todo!("transpilation for block");
/*
self.members
.iter()
.map(|x| x.transpile())
.collect::<Vec<_>>()
.join("\n")
*/
}
}

View File

@ -1,76 +0,0 @@
use super::Transpilable;
use crate::syntax::ast::Expression;
impl Transpilable for Expression<'_> {
/// Transpiles an Expression to PHP
///
/// Right now the expressions in the grammar are:
/// - Number
/// - String
/// - Boolean
/// - Identifier
fn transpile(&self) -> String {
match self {
Expression::Int(value) => format!("{}", value),
Expression::Float(value) => format!("{}", value),
Expression::String(value) => {
format!("{}", *value)
}
Expression::Boolean(value) => String::from(if *value { "true" } else { "false" }),
Expression::Identifier(value) => format!("{}", *value),
Expression::FunctionCall(f) => f.transpile(),
Expression::BinaryOperator(left_expr, right_expr, operator) => {
format!(
"{}{}{}",
left_expr.transpile(),
operator,
right_expr.transpile()
)
}
Expression::UnaryOperator(operator, expression) => {
format!("{}{}", operator, expression.transpile())
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::syntax::ast::Expression;
#[test]
fn should_transpile_number() {
let str = String::from("42");
let exp = Expression::Int(&str);
let result = exp.transpile();
assert_eq!("42", result);
}
#[test]
fn should_transpile_string() {
let str = String::from("\"Hello world\"");
let exp = Expression::String(&str);
let result = exp.transpile();
assert_eq!("\"Hello world\"", result);
}
#[test]
fn should_transpile_boolean() {
let exp = Expression::Boolean(true);
let result = exp.transpile();
assert_eq!("true", result);
}
#[test]
fn should_transpile_identifier() {
let s = String::from("newValue");
let exp = Expression::Identifier(&s);
let result = exp.transpile();
assert_eq!("newValue", result);
}
}

View File

@ -1,17 +0,0 @@
use crate::syntax::ast::functions::FunctionCall;
use super::Transpilable;
impl Transpilable for FunctionCall<'_> {
fn transpile(&self) -> String {
let parameters = &self
.arguments
.arguments
.iter()
.map(|expr| expr.transpile())
.collect::<Vec<_>>()
.join(", ");
format!("{}({})", self.function.transpile(), parameters)
}
}

View File

@ -1,43 +0,0 @@
use crate::syntax::ast::FunctionDeclaration;
use super::Transpilable;
impl Transpilable for FunctionDeclaration<'_> {
fn transpile(&self) -> String {
format!(
"function {}() {{\n{}\n}}",
self.identifier.value,
self.block.transpile()
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
lexic::get_tokens,
syntax::{
ast::{ModuleMembers, Statement},
build_ast,
},
};
/* TODO: reimplement
#[test]
fn should_transpile() {
let tokens = get_tokens(&String::from("fun id() {}")).unwrap();
let result = build_ast(&tokens).unwrap();
let fun_dec = result.productions.get(0).unwrap();
match fun_dec {
ModuleMembers::Stmt(Statement::FnDecl(fun_decl)) => {
let transpiled = fun_decl.transpile();
assert_eq!("function id() {\n\n}", transpiled);
}
_ => panic!("Expected a function declaration"),
}
}*/
}

View File

@ -1,13 +1,5 @@
// TODO: These are for the THP AST. Eventually replace this // TODO: These are for the THP AST. Eventually replace this
// with the PHP AST // with the PHP AST
mod binding;
mod block;
mod expression;
mod function_call;
mod function_declaration;
mod module_ast;
mod statement;
mod top_level_construct;
mod php; mod php;

View File

@ -1,50 +0,0 @@
use super::Transpilable;
use crate::syntax::ast::ModuleAST;
impl Transpilable for ModuleAST<'_> {
/// Transpiles the whole AST into PHP, using this same trait on the
/// nodes and leaves of the AST
fn transpile(&self) -> String {
let bindings_str: Vec<String> = self
.productions
.iter()
.map(|binding| binding.transpile())
.collect();
format!("<?php\n\n{}\n", bindings_str.join("\n"))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
lexic::token::{Token, TokenType},
syntax::ast::{var_binding::VariableBinding, Expression, ModuleMembers, Statement},
};
#[test]
fn module_ast_should_transpile() {
let id = String::from("identifier");
let id_token = Token {
token_type: TokenType::Identifier,
value: id,
position: 0,
};
let value = String::from("322");
let binding = VariableBinding {
datatype: None,
identifier: &id_token,
expression: Expression::Int(&value),
is_mutable: false,
};
let module = ModuleAST {
productions: vec![ModuleMembers::Stmt(Statement::Binding(binding))],
};
let result = module.transpile();
assert_eq!("<?php\n\n$identifier = 322\n", result);
}
}

View File

@ -0,0 +1 @@
mod primary_expression;

View File

@ -0,0 +1,61 @@
use crate::{codegen::Transpilable, php_ast::PhpPrimaryExpression};
impl Transpilable for PhpPrimaryExpression<'_> {
fn transpile(&self) -> String {
match self {
PhpPrimaryExpression::IntegerLiteral(value) => value.to_string(),
PhpPrimaryExpression::FloatingLiteral(value) => value.to_string(),
PhpPrimaryExpression::StringLiteral(value) => format!("\"{}\"", value),
}
}
}
#[cfg(test)]
mod tests {
use crate::{codegen::Transpilable, php_ast::PhpPrimaryExpression};
#[test]
fn should_transpile_empty_string() {
let input = String::from("");
let ast = PhpPrimaryExpression::StringLiteral(&input);
let output = ast.transpile();
assert_eq!("\"\"", output)
}
#[test]
fn should_transpile_string() {
let input = String::from("abc");
let ast = PhpPrimaryExpression::StringLiteral(&input);
let output = ast.transpile();
assert_eq!("\"abc\"", output)
}
#[test]
fn should_transpile_string_with_quotes() {
let input = String::from("a\\\"b\\\"c");
let ast = PhpPrimaryExpression::StringLiteral(&input);
let output = ast.transpile();
assert_eq!("\"a\\\"b\\\"c\"", output)
}
#[test]
fn should_transpile_int() {
let input = String::from("322");
let ast = PhpPrimaryExpression::IntegerLiteral(&input);
let output = ast.transpile();
assert_eq!("322", output)
}
#[test]
fn should_transpile_floating() {
let input = String::from("322.644");
let ast = PhpPrimaryExpression::FloatingLiteral(&input);
let output = ast.transpile();
assert_eq!("322.644", output)
}
}

View File

@ -1,49 +1,14 @@
use std::os::linux::raw::stat;
use crate::php_ast::{PhpAst, PhpExpression, PhpStatement};
use super::Transpilable; use super::Transpilable;
use crate::php_ast::PhpExpression;
impl Transpilable for PhpAst<'_> { pub mod statement;
fn transpile(&self) -> String { pub mod statement_list;
let mut fragments = vec![String::from("<?php\n")]; mod expression;
for statement in self.statements.iter() {
fragments.push(statement.transpile());
}
fragments.join("")
}
}
impl Transpilable for PhpStatement<'_> {
fn transpile(&self) -> String {
match self {
PhpStatement::PhpEchoStatement(expr_list) => {
let expressions_vec = expr_list
.expressions
.iter()
.map(|e| e.transpile())
.collect::<Vec<_>>();
let expressions_str = if expressions_vec.is_empty() {
"\"\"".into()
} else {
expressions_vec.join(", ")
};
format!("echo {};", expressions_str)
}
}
}
}
impl Transpilable for PhpExpression<'_> { impl Transpilable for PhpExpression<'_> {
fn transpile(&self) -> String { fn transpile(&self) -> String {
match self { match self {
PhpExpression::String(value) => { PhpExpression::PrimaryExpression(p) => p.transpile(),
format!("{}", value)
}
} }
} }
} }

View File

@ -0,0 +1,77 @@
use crate::{codegen::Transpilable, php_ast::PhpStatement};
mod echo_statement;
impl Transpilable for PhpStatement<'_> {
fn transpile(&self) -> String {
match self {
PhpStatement::PhpEchoStatement(expr_list) => {
let expressions_vec = expr_list
.expressions
.iter()
.map(|e| e.transpile())
.collect::<Vec<_>>();
let expressions_str = if expressions_vec.is_empty() {
"\"\"".into()
} else {
expressions_vec.join(", ")
};
format!("echo {};", expressions_str)
}
}
}
}
#[cfg(test)]
mod tests {
use crate::{
codegen::Transpilable,
php_ast::{PhpExpression, PhpExpressionList, PhpPrimaryExpression, PhpStatement},
};
#[test]
fn should_gen_empty_echo_statement() {
let expressions = PhpExpressionList {
expressions: vec![],
};
let ast = PhpStatement::PhpEchoStatement(expressions);
let output = ast.transpile();
assert_eq!("echo \"\";", output)
}
#[test]
fn should_gen_echo_with_expr() {
let input = String::from("322");
let exp_1 = PhpPrimaryExpression::FloatingLiteral(&input);
let expressions = PhpExpressionList {
expressions: vec![PhpExpression::PrimaryExpression(exp_1)],
};
let ast = PhpStatement::PhpEchoStatement(expressions);
let output = ast.transpile();
assert_eq!("echo 322;", output)
}
#[test]
fn should_gen_echo_with_multiple_expr() {
let input = String::from("322");
let exp_1 = PhpPrimaryExpression::FloatingLiteral(&input);
let input = String::from("Hai world");
let exp_2 = PhpPrimaryExpression::StringLiteral(&input);
let expressions = PhpExpressionList {
expressions: vec![
PhpExpression::PrimaryExpression(exp_1),
PhpExpression::PrimaryExpression(exp_2),
],
};
let ast = PhpStatement::PhpEchoStatement(expressions);
let output = ast.transpile();
assert_eq!("echo 322, \"Hai world\";", output)
}
}

View File

@ -0,0 +1,27 @@
use crate::{codegen::Transpilable, php_ast::PhpAst};
impl Transpilable for PhpAst<'_> {
fn transpile(&self) -> String {
let mut fragments = vec![String::from("<?php\n")];
for statement in self.statements.iter() {
fragments.push(statement.transpile());
}
fragments.join("")
}
}
#[cfg(test)]
mod tests {
use crate::{codegen::Transpilable, php_ast::PhpAst};
#[test]
fn should_transpile_empty_file() {
let ast = PhpAst {statements: vec![]};
let output = ast.transpile();
assert_eq!("<?php\n", output);
}
}

View File

@ -1,14 +0,0 @@
use crate::syntax::ast::Statement;
use super::Transpilable;
impl Transpilable for Statement<'_> {
fn transpile(&self) -> String {
let stmt = match self {
Statement::FnDecl(f) => f.transpile(),
Statement::Binding(b) => b.transpile(),
};
format!("{stmt};")
}
}

View File

@ -1,13 +0,0 @@
use crate::syntax::ast::{ModuleMembers, Statement};
use super::Transpilable;
impl Transpilable for ModuleMembers<'_> {
fn transpile(&self) -> String {
match self {
ModuleMembers::Stmt(Statement::Binding(b)) => b.transpile(),
ModuleMembers::Stmt(Statement::FnDecl(f)) => f.transpile(),
_ => todo!("Not implemented: Transpilable for Expression"),
}
}
}

View File

@ -101,5 +101,6 @@ fn build_ast(input: &String, tokens: Vec<Token>) -> Result<String, String> {
} }
}; };
Ok(codegen::codegen(&ast)) Err("Code generation disabled: rewriting into PHP AST".into())
// Ok(codegen::codegen(&ast))
} }

View File

@ -1,12 +1,23 @@
// Follows https://phplang.org/spec/19-grammar.html#syntactic-grammar /// This AST implements a subset of the PHP AST as defined
/// by https://phplang.org/spec/19-grammar.html#syntactic-grammar
///
/// This subset only includes nodes that can be generated by
/// THP
pub mod transformers; pub mod transformers;
/// Represents `statement-list` on the grammar /// Represents `statement-list` on the grammar,
/// and thus a whole PHP source file
pub struct PhpAst<'a> { pub struct PhpAst<'a> {
pub statements: Vec<PhpStatement<'a>>, pub statements: Vec<PhpStatement<'a>>,
} }
/// https://phplang.org/spec/19-grammar.html#grammar-statement
///
/// Not fully implemented
///
/// statement:
/// echo-statement
pub enum PhpStatement<'a> { pub enum PhpStatement<'a> {
PhpEchoStatement(PhpExpressionList<'a>), PhpEchoStatement(PhpExpressionList<'a>),
} }
@ -16,5 +27,16 @@ pub struct PhpExpressionList<'a> {
} }
pub enum PhpExpression<'a> { pub enum PhpExpression<'a> {
String(&'a String), PrimaryExpression(PhpPrimaryExpression<'a>),
} }
/// https://phplang.org/spec/19-grammar.html#grammar-primary-expression
///
/// primary-expression:
/// literal
pub enum PhpPrimaryExpression<'a> {
IntegerLiteral(&'a String),
FloatingLiteral(&'a String),
StringLiteral(&'a String),
}

View File

@ -1,5 +1,5 @@
use super::super::PhpExpression; use super::super::PhpExpression;
use crate::syntax::ast::Expression; use crate::{php_ast::PhpPrimaryExpression, syntax::ast::Expression};
use super::PHPTransformable; use super::PHPTransformable;
@ -9,7 +9,10 @@ impl<'a> PHPTransformable<'a> for Expression<'_> {
fn into_php_ast(&'a self) -> Self::Item { fn into_php_ast(&'a self) -> Self::Item {
match self { match self {
Expression::String(value) => PhpExpression::String(value), Expression::String(value) => {
let expr = PhpPrimaryExpression::StringLiteral(value);
PhpExpression::PrimaryExpression(expr)
},
_ => todo!("transformation for expression: {:?}", self), _ => todo!("transformation for expression: {:?}", self),
} }
} }

View File

@ -1,5 +1,5 @@
use super::super::PhpAst; use super::super::PhpAst;
use crate::php_ast::{PhpExpression, PhpExpressionList, PhpStatement}; use crate::php_ast::{PhpExpression, PhpExpressionList, PhpPrimaryExpression, PhpStatement};
use crate::syntax::ast::{Expression, ModuleAST, ModuleMembers}; use crate::syntax::ast::{Expression, ModuleAST, ModuleMembers};
use super::PHPTransformable; use super::PHPTransformable;
@ -33,7 +33,9 @@ impl<'a> PHPTransformable<'a> for ModuleAST<'_> {
for e in fc.arguments.arguments.iter() { for e in fc.arguments.arguments.iter() {
match e { match e {
Expression::String(v) => { Expression::String(v) => {
expressions.push(PhpExpression::String(v)) expressions.push(
PhpExpression::PrimaryExpression(PhpPrimaryExpression::StringLiteral(v.clone()))
)
}, },
_ => panic!("Non string expressions not supported") _ => panic!("Non string expressions not supported")
} }