Minimal workflow for a Hello World

master
Araozu 2024-07-27 18:44:54 -05:00
parent fe7cfe9d5f
commit 0d96efd4d8
5 changed files with 60 additions and 17 deletions

View File

@ -23,13 +23,20 @@
- Include the original tokens in the AST
## v0.0.15
- [ ] Replace all panics with actual errors
- [ ] Remove all old codegen
- [ ] Test codegen
- [ ] Begin work on the code formatter
## v0.0.14
- [x] Define a minimal PHP AST
- [x] Transform THP AST into PHP AST
- [ ] Implement minimal codegen for the PHP AST
- [ ] Remove old codegen
- [ ] Finish the workflow for a hello world
- [x] Implement minimal codegen for the PHP AST
- [x] Finish the workflow for a hello world
## v0.0.13

View File

@ -1,6 +1,6 @@
[package]
name = "thp"
version = "0.0.13"
version = "0.0.14"
edition = "2021"

View File

@ -1,4 +1,6 @@
use crate::php_ast::{PhpAst, PhpStatement};
use std::os::linux::raw::stat;
use crate::php_ast::{PhpAst, PhpExpression, PhpStatement};
use super::Transpilable;
@ -18,10 +20,31 @@ impl Transpilable for PhpStatement<'_> {
fn transpile(&self) -> String {
match self {
PhpStatement::PhpEchoStatement(expr_list) => {
// TODO: Actually generate parameters from the expr_list
"echo \"\";".into()
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<'_> {
fn transpile(&self) -> String {
match self {
PhpExpression::String(value) => {
format!("{}", value)
}
}
}
}

View File

@ -1,5 +1,5 @@
use super::super::PhpAst;
use crate::php_ast::{PhpExpressionList, PhpStatement};
use crate::php_ast::{PhpExpression, PhpExpressionList, PhpStatement};
use crate::syntax::ast::{Expression, ModuleAST, ModuleMembers};
use super::PHPTransformable;
@ -17,10 +17,7 @@ impl<'a> PHPTransformable<'a> for ModuleAST<'_> {
php_statements.push(stmt.into_php_ast());
}
ModuleMembers::Expr(expr) => {
// TODO: a print() function call is technically an
// expression in the AST, but PHP expects it to be an statement.
// transform beforehand?
// TODO: This should be done by the Expression transformer
match expr {
Expression::FunctionCall(fc) => {
let function_expr: &Expression = &*fc.function;
@ -28,8 +25,22 @@ impl<'a> PHPTransformable<'a> for ModuleAST<'_> {
Expression::Identifier(id) if *id == "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::String(v))
},
_ => panic!("Non string expressions not supported")
}
}
php_statements.push(PhpStatement::PhpEchoStatement(PhpExpressionList {
expressions: vec![]
expressions
}));
},
_ => todo!("Not implemented: AST transformation for function call that is not an identifier")

View File

@ -62,6 +62,8 @@ pub struct Parameter<'a> {
pub enum Expression<'a> {
Int(&'a String),
Float(&'a String),
// TODO: Specify if this contains or not the original quotes ""
// TODO: After this fix where neccesary
String(&'a String),
Boolean(bool),
Identifier(&'a String),