feat: codegen of top level expressions

This commit is contained in:
Araozu 2024-08-01 15:37:00 -05:00
parent 8e18458125
commit 9cd1b70103
4 changed files with 31 additions and 19 deletions

View File

@ -25,6 +25,10 @@
- Begin work on the code formatter
## v0.1.1
- [x] Top level expressions as statements
## v0.1.0
- [x] Complete workflow for "Hello world"

View File

@ -22,6 +22,7 @@ impl<'a> PHPTransformable<'a> for ModuleAST<'_> {
// TODO: This should be done by the Expression transformer
match expr {
Expression::FunctionCall(fc) => {
// TODO: This definitely needs refactoring
let function_expr: &Expression = &*fc.function;
match function_expr {
Expression::Identifier(id) if *id == "print" => {
@ -50,6 +51,27 @@ impl<'a> PHPTransformable<'a> for ModuleAST<'_> {
_ => todo!("Not implemented: AST transformation for function call that is not an identifier")
}
}
Expression::Int(value) => {
php_statements.push(PhpStatement::PhpExpressionStatement(
PhpExpression::Assignment(PhpAssignmentExpression::Primary(
PhpPrimaryExpression::IntegerLiteral(value),
)),
));
}
Expression::Float(value) => {
php_statements.push(PhpStatement::PhpExpressionStatement(
PhpExpression::Assignment(PhpAssignmentExpression::Primary(
PhpPrimaryExpression::FloatingLiteral(value),
)),
));
}
Expression::String(value) => {
php_statements.push(PhpStatement::PhpExpressionStatement(
PhpExpression::Assignment(PhpAssignmentExpression::Primary(
PhpPrimaryExpression::StringLiteral(value),
)),
));
}
_ => {
todo!("not implemented: AST transform for expression {:?}", expr)
}

View File

@ -33,22 +33,6 @@ impl SemanticCheck for Statement<'_> {
// TODO: Move to its own file when it grows
impl SemanticCheck for Expression<'_> {
fn check_semantics(&self, scope: &SymbolTable) -> Result<(), MistiError> {
// How to get the global definition into the symbol table?
// maybe just when creating the symbol table inject all
// the global elements at once?
// Store the global elements as binary/JSON
// and load them along with the symbol table
// then for efficiency they could be grouped by module?
// and stored as binary files?
// then the binary files are searched for and loaded when
// requested?
// For a function call:
// check that the function exists
// check its signature
// check parameters
match self {
Expression::FunctionCall(f) => {
let fun = &*f.function;
@ -104,7 +88,11 @@ impl SemanticCheck for Expression<'_> {
}
}
}
_ => todo!("Check semantics for expression other than function call"),
Expression::Int(_) => {},
Expression::Float(_) => {},
Expression::String(_) => {},
Expression::Boolean(_) => {},
_ => todo!("Check semantics for expression other than function call and primitive"),
}
Ok(())

View File

@ -62,8 +62,6 @@ 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),