refactor: reenable tests
This commit is contained in:
parent
6965eda6a7
commit
28d48c8b2f
@ -17,7 +17,7 @@ pub fn compile_command(arguments: Vec<String>) -> Result<(), ()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let argument = &arguments[0];
|
let argument = &arguments[0];
|
||||||
if argument.starts_with("-") {
|
if argument.starts_with('-') {
|
||||||
let opt_str = argument.as_str();
|
let opt_str = argument.as_str();
|
||||||
|
|
||||||
println!("{}", compile_help());
|
println!("{}", compile_help());
|
||||||
|
@ -39,10 +39,10 @@ pub fn empty_command(arguments: Vec<String>) -> Result<(), ()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expand_option(option: &String) -> Result<EmptyOptions, String> {
|
fn expand_option(option: &str) -> Result<EmptyOptions, String> {
|
||||||
match option.as_str() {
|
match option {
|
||||||
"-h" | "--help" => Ok(EmptyOptions::Help),
|
"-h" | "--help" => Ok(EmptyOptions::Help),
|
||||||
"-v" | "--version" => Ok(EmptyOptions::Version),
|
"-v" | "--version" => Ok(EmptyOptions::Version),
|
||||||
_ => Err(option.clone()),
|
_ => Err(option.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ use colored::*;
|
|||||||
pub fn help_command(arguments: Vec<String>) -> Result<(), ()> {
|
pub fn help_command(arguments: Vec<String>) -> Result<(), ()> {
|
||||||
println!("{}", get_help_text());
|
println!("{}", get_help_text());
|
||||||
|
|
||||||
if arguments.len() > 0 {
|
if !arguments.is_empty() {
|
||||||
println!(
|
println!(
|
||||||
"{}: {}",
|
"{}: {}",
|
||||||
"warning".yellow(),
|
"warning".yellow(),
|
||||||
|
@ -61,7 +61,7 @@ fn parse_args() -> Result<(CommandType, Vec<String>), String> {
|
|||||||
// Remove the first argument, which is the path to the executable
|
// Remove the first argument, which is the path to the executable
|
||||||
args.remove(0);
|
args.remove(0);
|
||||||
|
|
||||||
let command = match args.get(0) {
|
let command = match args.first() {
|
||||||
Some(command) if !command.starts_with('-') => match command.as_str() {
|
Some(command) if !command.starts_with('-') => match command.as_str() {
|
||||||
"c" | "compile" => CommandType::Compile,
|
"c" | "compile" => CommandType::Compile,
|
||||||
"f" | "format" => CommandType::Format,
|
"f" | "format" => CommandType::Format,
|
||||||
|
@ -11,21 +11,23 @@ impl Transpilable for PSimpleAssignment<'_> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use crate::{
|
||||||
|
codegen::Transpilable,
|
||||||
|
php_ast::{PExpresssion, PPrimary, PSimpleAssignment},
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_gen_assignment() {
|
fn should_gen_assignment() {
|
||||||
let variable = String::from("name");
|
let variable = &String::from("name");
|
||||||
let value = String::from("John");
|
let value = String::from("John");
|
||||||
let assignment = PhpPrimaryExpression::StringLiteral(&value);
|
let assignment = PExpresssion::Primary(PPrimary::StringLiteral(&value));
|
||||||
|
|
||||||
let ast = PhpAssignmentExpression::SimpleAssignment(PhpSimpleAssignment {
|
let ast = PSimpleAssignment {
|
||||||
variable,
|
variable,
|
||||||
assignment,
|
assignment: Box::new(assignment),
|
||||||
});
|
};
|
||||||
let output = ast.transpile();
|
let output = ast.transpile();
|
||||||
|
|
||||||
assert_eq!("$name = \"John\"", output)
|
assert_eq!("$name = \"John\"", output)
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
@ -14,12 +14,12 @@ impl Transpilable for PPrimary<'_> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use crate::{codegen::Transpilable, php_ast::PPrimary};
|
||||||
|
|
||||||
/*
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_transpile_empty_string() {
|
fn should_transpile_empty_string() {
|
||||||
let input = String::from("");
|
let input = String::from("");
|
||||||
let ast = PhpPrimaryExpression::StringLiteral(&input);
|
let ast = PPrimary::StringLiteral(&input);
|
||||||
let output = ast.transpile();
|
let output = ast.transpile();
|
||||||
|
|
||||||
assert_eq!("\"\"", output)
|
assert_eq!("\"\"", output)
|
||||||
@ -28,7 +28,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn should_transpile_string() {
|
fn should_transpile_string() {
|
||||||
let input = String::from("abc");
|
let input = String::from("abc");
|
||||||
let ast = PhpPrimaryExpression::StringLiteral(&input);
|
let ast = PPrimary::StringLiteral(&input);
|
||||||
let output = ast.transpile();
|
let output = ast.transpile();
|
||||||
|
|
||||||
assert_eq!("\"abc\"", output)
|
assert_eq!("\"abc\"", output)
|
||||||
@ -37,7 +37,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn should_transpile_string_with_quotes() {
|
fn should_transpile_string_with_quotes() {
|
||||||
let input = String::from("a\\\"b\\\"c");
|
let input = String::from("a\\\"b\\\"c");
|
||||||
let ast = PhpPrimaryExpression::StringLiteral(&input);
|
let ast = PPrimary::StringLiteral(&input);
|
||||||
let output = ast.transpile();
|
let output = ast.transpile();
|
||||||
|
|
||||||
assert_eq!("\"a\\\"b\\\"c\"", output)
|
assert_eq!("\"a\\\"b\\\"c\"", output)
|
||||||
@ -46,7 +46,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn should_transpile_int() {
|
fn should_transpile_int() {
|
||||||
let input = String::from("322");
|
let input = String::from("322");
|
||||||
let ast = PhpPrimaryExpression::IntegerLiteral(&input);
|
let ast = PPrimary::IntegerLiteral(&input);
|
||||||
let output = ast.transpile();
|
let output = ast.transpile();
|
||||||
|
|
||||||
assert_eq!("322", output)
|
assert_eq!("322", output)
|
||||||
@ -55,7 +55,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn should_transpile_floating() {
|
fn should_transpile_floating() {
|
||||||
let input = String::from("322.644");
|
let input = String::from("322.644");
|
||||||
let ast = PhpPrimaryExpression::FloatingLiteral(&input);
|
let ast = PPrimary::FloatingLiteral(&input);
|
||||||
let output = ast.transpile();
|
let output = ast.transpile();
|
||||||
|
|
||||||
assert_eq!("322.644", output)
|
assert_eq!("322.644", output)
|
||||||
@ -64,10 +64,9 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn should_transpile_variable() {
|
fn should_transpile_variable() {
|
||||||
let input = String::from("name");
|
let input = String::from("name");
|
||||||
let ast = PhpPrimaryExpression::Variable(&input);
|
let ast = PPrimary::Variable(&input);
|
||||||
let output = ast.transpile();
|
let output = ast.transpile();
|
||||||
|
|
||||||
assert_eq!("$name", output)
|
assert_eq!("$name", output)
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
@ -10,67 +10,3 @@ impl Transpilable for PStatement<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
|
|
||||||
/*
|
|
||||||
#[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::Assignment(PhpAssignmentExpression::Primary(
|
|
||||||
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::Assignment(PhpAssignmentExpression::Primary(exp_1)),
|
|
||||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(exp_2)),
|
|
||||||
],
|
|
||||||
};
|
|
||||||
let ast = PhpStatement::PhpEchoStatement(expressions);
|
|
||||||
let output = ast.transpile();
|
|
||||||
|
|
||||||
assert_eq!("echo 322, \"Hai world\";", output)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn should_gen_expression_stmt() {
|
|
||||||
let input = String::from("Hi!");
|
|
||||||
let exp_1 = PhpPrimaryExpression::StringLiteral(&input);
|
|
||||||
let ast = PhpStatement::PhpExpressionStatement(PhpExpression::Assignment(
|
|
||||||
PhpAssignmentExpression::Primary(exp_1),
|
|
||||||
));
|
|
||||||
let output = ast.transpile();
|
|
||||||
|
|
||||||
assert_eq!("\"Hi!\";", output)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
@ -14,7 +14,10 @@ impl Transpilable for PFile<'_> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{codegen::Transpilable, php_ast::PFile};
|
use crate::{
|
||||||
|
codegen::Transpilable,
|
||||||
|
php_ast::{PExpresssion, PFile, PPrimary, PStatement},
|
||||||
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_transpile_empty_file() {
|
fn should_transpile_empty_file() {
|
||||||
@ -24,20 +27,16 @@ mod tests {
|
|||||||
assert_eq!("<?php\n", output);
|
assert_eq!("<?php\n", output);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_transpile_expr_statement() {
|
fn should_transpile_expr_statement() {
|
||||||
let value = String::from("Hello world!");
|
let value = String::from("Hello world!");
|
||||||
let ast = PhpAst {
|
let ast = PFile {
|
||||||
statements: vec![PhpStatement::PhpExpressionStatement(
|
statements: vec![PStatement::ExpressionStatement(PExpresssion::Primary(
|
||||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(
|
PPrimary::StringLiteral(&value),
|
||||||
PhpPrimaryExpression::StringLiteral(&value),
|
))],
|
||||||
)),
|
|
||||||
)],
|
|
||||||
};
|
};
|
||||||
let output = ast.transpile();
|
let output = ast.transpile();
|
||||||
|
|
||||||
assert_eq!("<?php\n\"Hello world!\";", output);
|
assert_eq!("<?php\n\"Hello world!\";", output);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,7 @@ fn compile(input: &String) -> Result<String, String> {
|
|||||||
let tokens = match lexic::get_tokens(input) {
|
let tokens = match lexic::get_tokens(input) {
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
let chars: Vec<char> = input.chars().into_iter().collect();
|
let chars: Vec<char> = input.chars().collect();
|
||||||
return Err(error.get_error_str(&chars));
|
return Err(error.get_error_str(&chars));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -78,7 +78,7 @@ fn compile(input: &String) -> Result<String, String> {
|
|||||||
let ast = match syntax::build_ast(&tokens) {
|
let ast = match syntax::build_ast(&tokens) {
|
||||||
Ok(ast) => ast,
|
Ok(ast) => ast,
|
||||||
Err(reason) => {
|
Err(reason) => {
|
||||||
let chars: Vec<char> = input.chars().into_iter().collect();
|
let chars: Vec<char> = input.chars().collect();
|
||||||
return Err(reason.get_error_str(&chars));
|
return Err(reason.get_error_str(&chars));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -90,7 +90,7 @@ fn compile(input: &String) -> Result<String, String> {
|
|||||||
match res1 {
|
match res1 {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(reason) => {
|
Err(reason) => {
|
||||||
let chars: Vec<char> = input.chars().into_iter().collect();
|
let chars: Vec<char> = input.chars().collect();
|
||||||
let error = format!("{}: {}", "error".on_red(), reason.get_error_str(&chars));
|
let error = format!("{}: {}", "error".on_red(), reason.get_error_str(&chars));
|
||||||
return Err(error);
|
return Err(error);
|
||||||
}
|
}
|
||||||
|
@ -1,56 +0,0 @@
|
|||||||
/// A single PHP source code file
|
|
||||||
pub struct PFile<'a> {
|
|
||||||
pub statements: Vec<PStatement<'a>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A PHP statement
|
|
||||||
pub enum PStatement<'a> {
|
|
||||||
ExpressionStatement(PExpressionStatement<'a>),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A statement composed of a single expression,
|
|
||||||
/// whose value is discarded
|
|
||||||
///
|
|
||||||
/// ## Examples
|
|
||||||
///
|
|
||||||
/// ```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 PSimpleAssignment<'a> {
|
|
||||||
pub variable: &'a String,
|
|
||||||
pub assignment: Box<PExpresssion<'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<PExpresssion<'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),
|
|
||||||
}
|
|
@ -37,15 +37,11 @@ impl<'a> PHPTransformable<'a> for Expression<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{
|
use crate::{
|
||||||
lexic::token::Token,
|
lexic::token::Token,
|
||||||
php_ast::{
|
php_ast::{transformers::PHPTransformable, PExpresssion, PPrimary},
|
||||||
transformers::PHPTransformable, PhpAssignmentExpression, PhpExpression,
|
|
||||||
PhpPrimaryExpression,
|
|
||||||
},
|
|
||||||
syntax::ast::Expression,
|
syntax::ast::Expression,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -56,9 +52,7 @@ mod tests {
|
|||||||
let output = input.into_php_ast();
|
let output = input.into_php_ast();
|
||||||
|
|
||||||
match output {
|
match output {
|
||||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(
|
PExpresssion::Primary(PPrimary::StringLiteral(value)) => {
|
||||||
PhpPrimaryExpression::StringLiteral(value),
|
|
||||||
)) => {
|
|
||||||
assert_eq!("Hello", value)
|
assert_eq!("Hello", value)
|
||||||
}
|
}
|
||||||
_ => panic!("Expected a String literal"),
|
_ => panic!("Expected a String literal"),
|
||||||
@ -72,9 +66,7 @@ mod tests {
|
|||||||
let output = input.into_php_ast();
|
let output = input.into_php_ast();
|
||||||
|
|
||||||
match output {
|
match output {
|
||||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(
|
PExpresssion::Primary(PPrimary::IntegerLiteral(value)) => {
|
||||||
PhpPrimaryExpression::IntegerLiteral(value),
|
|
||||||
)) => {
|
|
||||||
assert_eq!("322", value)
|
assert_eq!("322", value)
|
||||||
}
|
}
|
||||||
_ => panic!("Expected a Int literal"),
|
_ => panic!("Expected a Int literal"),
|
||||||
@ -88,13 +80,10 @@ mod tests {
|
|||||||
let output = input.into_php_ast();
|
let output = input.into_php_ast();
|
||||||
|
|
||||||
match output {
|
match output {
|
||||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(
|
PExpresssion::Primary(PPrimary::FloatingLiteral(value)) => {
|
||||||
PhpPrimaryExpression::FloatingLiteral(value),
|
|
||||||
)) => {
|
|
||||||
assert_eq!("322.644", value)
|
assert_eq!("322.644", value)
|
||||||
}
|
}
|
||||||
_ => panic!("Expected a Float literal"),
|
_ => panic!("Expected a Float literal"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
@ -19,68 +19,6 @@ impl<'a> PHPTransformable<'a> for ModuleAST<'_> {
|
|||||||
let p_expression = expr.into_php_ast();
|
let p_expression = expr.into_php_ast();
|
||||||
|
|
||||||
php_statements.push(PStatement::ExpressionStatement(p_expression));
|
php_statements.push(PStatement::ExpressionStatement(p_expression));
|
||||||
|
|
||||||
/*
|
|
||||||
match expr {
|
|
||||||
Expression::FunctionCall(fc) => {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// TODO: This definitely needs refactoring
|
|
||||||
let function_expr: &Expression = &*fc.function;
|
|
||||||
match function_expr {
|
|
||||||
Expression::Identifier(id) if id.value == "print" => {
|
|
||||||
// transform to print() expression
|
|
||||||
// no parameters supported
|
|
||||||
|
|
||||||
// transform parameters, expect them all to be strings
|
|
||||||
|
|
||||||
let mut expressions = Vec::<PhpExpression>::new();
|
|
||||||
|
|
||||||
for e in fc.arguments.arguments.iter() {
|
|
||||||
match e {
|
|
||||||
Expression::String(v) => {
|
|
||||||
expressions.push(
|
|
||||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(PhpPrimaryExpression::StringLiteral(&v.value)))
|
|
||||||
)
|
|
||||||
},
|
|
||||||
_ => todo!("Non string expressions not supported")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
php_statements.push(Box::new(PhpStatement::PhpEchoStatement(PhpExpressionList {
|
|
||||||
expressions
|
|
||||||
})));
|
|
||||||
},
|
|
||||||
_ => todo!("Not implemented: AST transformation for function call that is not an identifier")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Expression::Int(value) => {
|
|
||||||
php_statements.push(Box::new(PhpStatement::PhpExpressionStatement(
|
|
||||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(
|
|
||||||
PhpPrimaryExpression::IntegerLiteral(&value.value),
|
|
||||||
)),
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
Expression::Float(value) => {
|
|
||||||
php_statements.push(Box::new(PhpStatement::PhpExpressionStatement(
|
|
||||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(
|
|
||||||
PhpPrimaryExpression::FloatingLiteral(&value.value),
|
|
||||||
)),
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
Expression::String(value) => {
|
|
||||||
php_statements.push(Box::new(PhpStatement::PhpExpressionStatement(
|
|
||||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(
|
|
||||||
PhpPrimaryExpression::StringLiteral(&value.value),
|
|
||||||
)),
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
todo!("not implemented: AST transform for expression {:?}", expr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -91,7 +29,6 @@ impl<'a> PHPTransformable<'a> for ModuleAST<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{php_ast::transformers::PHPTransformable, syntax::ast::ModuleAST};
|
use crate::{php_ast::transformers::PHPTransformable, syntax::ast::ModuleAST};
|
||||||
@ -106,4 +43,3 @@ mod tests {
|
|||||||
assert!(output.statements.is_empty())
|
assert!(output.statements.is_empty())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
@ -24,15 +24,11 @@ impl<'a> PHPTransformable<'a> for Statement<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{
|
use crate::{
|
||||||
lexic::token::{Token, TokenType},
|
lexic::token::{Token, TokenType},
|
||||||
php_ast::{
|
php_ast::{transformers::PHPTransformable, PExpresssion, PPrimary, PStatement},
|
||||||
transformers::PHPTransformable, PhpAssignmentExpression, PhpExpression,
|
|
||||||
PhpPrimaryExpression, PhpStatement,
|
|
||||||
},
|
|
||||||
syntax::ast::{var_binding::VariableBinding, Expression, Statement},
|
syntax::ast::{var_binding::VariableBinding, Expression, Statement},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -55,13 +51,11 @@ mod tests {
|
|||||||
let output = binding.into_php_ast();
|
let output = binding.into_php_ast();
|
||||||
|
|
||||||
match output {
|
match output {
|
||||||
PhpStatement::PhpExpressionStatement(PhpExpression::Assignment(
|
PStatement::ExpressionStatement(PExpresssion::Assignment(assignment)) => {
|
||||||
PhpAssignmentExpression::SimpleAssignment(assignment),
|
|
||||||
)) => {
|
|
||||||
assert_eq!("name", assignment.variable);
|
assert_eq!("name", assignment.variable);
|
||||||
|
|
||||||
match assignment.assignment {
|
match *assignment.assignment {
|
||||||
PhpPrimaryExpression::StringLiteral(value) => {
|
PExpresssion::Primary(PPrimary::StringLiteral(value)) => {
|
||||||
assert_eq!("Hello", value);
|
assert_eq!("Hello", value);
|
||||||
}
|
}
|
||||||
_ => panic!("Expected a String literal as the value of the assignment"),
|
_ => panic!("Expected a String literal as the value of the assignment"),
|
||||||
@ -71,4 +65,3 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
@ -46,7 +46,7 @@ fn compile(input: &String) {
|
|||||||
let tokens = match lexic::get_tokens(input) {
|
let tokens = match lexic::get_tokens(input) {
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
let chars: Vec<char> = input.chars().into_iter().collect();
|
let chars: Vec<char> = input.chars().collect();
|
||||||
eprintln!("{}", error.get_error_str(&chars));
|
eprintln!("{}", error.get_error_str(&chars));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -58,7 +58,7 @@ fn compile(input: &String) {
|
|||||||
let ast = match syntax::build_ast(&tokens) {
|
let ast = match syntax::build_ast(&tokens) {
|
||||||
Ok(ast) => ast,
|
Ok(ast) => ast,
|
||||||
Err(reason) => {
|
Err(reason) => {
|
||||||
let chars: Vec<char> = input.chars().into_iter().collect();
|
let chars: Vec<char> = input.chars().collect();
|
||||||
eprintln!("{}", reason.get_error_str(&chars));
|
eprintln!("{}", reason.get_error_str(&chars));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -71,7 +71,7 @@ fn compile(input: &String) {
|
|||||||
match res1 {
|
match res1 {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(reason) => {
|
Err(reason) => {
|
||||||
let chars: Vec<char> = input.chars().into_iter().collect();
|
let chars: Vec<char> = input.chars().collect();
|
||||||
let error = format!("{}: {}", "error".on_red(), reason.get_error_str(&chars));
|
let error = format!("{}: {}", "error".on_red(), reason.get_error_str(&chars));
|
||||||
eprintln!("{}", error);
|
eprintln!("{}", error);
|
||||||
return;
|
return;
|
||||||
|
@ -9,7 +9,7 @@ use crate::{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParsingResult<ArgumentsList> {
|
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<ArgumentsList> {
|
||||||
let mut current_pos = pos;
|
let mut current_pos = pos;
|
||||||
|
|
||||||
let (opening_paren, next_pos) =
|
let (opening_paren, next_pos) =
|
||||||
|
@ -17,14 +17,14 @@ params list = "("
|
|||||||
|
|
||||||
datatype pair = datatype, identifier;
|
datatype pair = datatype, identifier;
|
||||||
*/
|
*/
|
||||||
pub fn parse_params_list<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParsingResult<ParamsList> {
|
pub fn parse_params_list(tokens: &Vec<Token>, pos: usize) -> ParsingResult<ParamsList> {
|
||||||
let mut current_pos = pos;
|
let mut current_pos = pos;
|
||||||
|
|
||||||
let (opening_paren, next_pos) =
|
let (opening_paren, next_pos) =
|
||||||
match parse_token_type(tokens, current_pos, TokenType::LeftParen) {
|
match parse_token_type(tokens, current_pos, TokenType::LeftParen) {
|
||||||
Ok((t, next)) => (t, next),
|
Ok((t, next)) => (t, next),
|
||||||
Err(ParsingError::Err(err)) => return Err(ParsingError::Err(err)),
|
Err(ParsingError::Err(err)) => return Err(ParsingError::Err(err)),
|
||||||
Err(ParsingError::Mismatch(t)) => return Err(ParsingError::Mismatch(&t)),
|
Err(ParsingError::Mismatch(t)) => return Err(ParsingError::Mismatch(t)),
|
||||||
Err(ParsingError::Unmatched) => return Err(ParsingError::Unmatched),
|
Err(ParsingError::Unmatched) => return Err(ParsingError::Unmatched),
|
||||||
};
|
};
|
||||||
current_pos = next_pos;
|
current_pos = next_pos;
|
||||||
@ -91,7 +91,7 @@ pub fn parse_params_list<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParsingResul
|
|||||||
/// - `Type identifier = default_value`
|
/// - `Type identifier = default_value`
|
||||||
/// - `FunctionType identifier`
|
/// - `FunctionType identifier`
|
||||||
/// - `Pattern identifier` (e.g. `Some[String] value`)?
|
/// - `Pattern identifier` (e.g. `Some[String] value`)?
|
||||||
fn parse_param_definition<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParsingResult<Parameter> {
|
fn parse_param_definition(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Parameter> {
|
||||||
let mut current_pos = pos;
|
let mut current_pos = pos;
|
||||||
let (datatype, next_pos) =
|
let (datatype, next_pos) =
|
||||||
match utils::parse_token_type(tokens, current_pos, TokenType::Datatype) {
|
match utils::parse_token_type(tokens, current_pos, TokenType::Datatype) {
|
||||||
|
@ -12,8 +12,6 @@ impl<'a> Parseable<'a> for VariableBinding<'a> {
|
|||||||
type Item = VariableBinding<'a>;
|
type Item = VariableBinding<'a>;
|
||||||
|
|
||||||
fn try_parse(tokens: &'a Vec<Token>, current_pos: usize) -> ParsingResult<'a, Self::Item> {
|
fn try_parse(tokens: &'a Vec<Token>, current_pos: usize) -> ParsingResult<'a, Self::Item> {
|
||||||
let current_pos = current_pos;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* val/var keyword
|
* val/var keyword
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user