more improvements on compiler
This commit is contained in:
@@ -48,14 +48,48 @@ impl Codegen {
|
||||
}
|
||||
|
||||
pub fn compile_program(&mut self, program: &Program) -> Result<Vec<u8>> {
|
||||
// Find tick function
|
||||
let mut tick_fn = None;
|
||||
for item in &program.body {
|
||||
if let Statement::ExportNamedDeclaration(decl) = item {
|
||||
if let Some(Declaration::FunctionDeclaration(f)) = &decl.declaration {
|
||||
self.compile_programs(vec![(self.file_name.clone(), self.source_text.clone(), program)])
|
||||
}
|
||||
|
||||
pub fn compile_programs(&mut self, programs: Vec<(String, String, &Program)>) -> Result<Vec<u8>> {
|
||||
// First pass: collect all functions and their indices
|
||||
let mut all_functions = Vec::new();
|
||||
for (file, source, program) in &programs {
|
||||
for item in &program.body {
|
||||
match item {
|
||||
Statement::FunctionDeclaration(f) => {
|
||||
all_functions.push((file.clone(), source.clone(), f.as_ref()));
|
||||
}
|
||||
Statement::ExportNamedDeclaration(decl) => {
|
||||
if let Some(Declaration::FunctionDeclaration(f)) = &decl.declaration {
|
||||
all_functions.push((file.clone(), source.clone(), f.as_ref()));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Find tick function (should be in the first program, which is the entry)
|
||||
let mut tick_fn_name = None;
|
||||
if let Some((_, _, entry_program)) = programs.first() {
|
||||
for item in &entry_program.body {
|
||||
let f_opt = match item {
|
||||
Statement::FunctionDeclaration(f) => Some(f.as_ref()),
|
||||
Statement::ExportNamedDeclaration(decl) => {
|
||||
if let Some(Declaration::FunctionDeclaration(f)) = &decl.declaration {
|
||||
Some(f.as_ref())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
if let Some(f) = f_opt {
|
||||
if let Some(ident) = &f.id {
|
||||
if ident.name == "tick" {
|
||||
tick_fn = Some(f);
|
||||
tick_fn_name = Some(ident.name.to_string());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -63,19 +97,26 @@ impl Codegen {
|
||||
}
|
||||
}
|
||||
|
||||
let tick_fn = tick_fn.ok_or_else(|| anyhow!("export function tick() not found"))?;
|
||||
let tick_fn_name = tick_fn_name.ok_or_else(|| anyhow!("export function tick() not found in entry file"))?;
|
||||
|
||||
// Entry point: loop calling tick
|
||||
self.emit_label("entry".to_string());
|
||||
self.emit_op(OpCode::Call, vec![Operand::Label("tick".to_string()), Operand::U32(0)], Span::default());
|
||||
self.emit_op(OpCode::Call, vec![Operand::Label(tick_fn_name), Operand::U32(0)], Span::default());
|
||||
self.emit_op(OpCode::Pop, vec![], Span::default());
|
||||
self.emit_op(OpCode::FrameSync, vec![], Span::default());
|
||||
self.emit_op(OpCode::Jmp, vec![Operand::Label("entry".to_string())], Span::default());
|
||||
|
||||
// Function tick
|
||||
self.emit_label("tick".to_string());
|
||||
self.compile_function(tick_fn)?;
|
||||
self.emit_op(OpCode::Ret, vec![], Span::default());
|
||||
// Compile all functions
|
||||
for (file, source, f) in all_functions {
|
||||
self.file_name = file;
|
||||
self.source_text = source;
|
||||
if let Some(ident) = &f.id {
|
||||
let name = ident.name.to_string();
|
||||
self.emit_label(name);
|
||||
self.compile_function(f)?;
|
||||
self.emit_op(OpCode::Ret, vec![], Span::default());
|
||||
}
|
||||
}
|
||||
|
||||
let asm_vec: Vec<Asm> = self.instructions.iter().map(|(a, _)| a.clone()).collect();
|
||||
let bytecode = assemble(&asm_vec).map_err(|e| anyhow!("Assemble error: {}", e))?;
|
||||
@@ -95,6 +136,19 @@ impl Codegen {
|
||||
self.locals.clear();
|
||||
self.next_local = 0;
|
||||
|
||||
// Start scope for parameters and local variables
|
||||
self.emit_op(OpCode::PushScope, vec![], f.span);
|
||||
|
||||
// Map parameters to locals
|
||||
for param in &f.params.items {
|
||||
if let BindingPattern::BindingIdentifier(ident) = ¶m.pattern {
|
||||
let name = ident.name.to_string();
|
||||
let id = self.next_local;
|
||||
self.locals.insert(name, id);
|
||||
self.next_local += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(body) = &f.body {
|
||||
for stmt in &body.statements {
|
||||
self.compile_stmt(stmt)?;
|
||||
@@ -105,6 +159,7 @@ impl Codegen {
|
||||
// If the function doesn't have a return statement, we push Null.
|
||||
// For now, we always push Null at the end if it's not a return.
|
||||
// In a more complex compiler, we would check if all paths return.
|
||||
self.emit_op(OpCode::PopScope, vec![], Span::default());
|
||||
self.emit_op(OpCode::PushConst, vec![Operand::U32(0)], Span::default()); // Index 0 is Null in PBC
|
||||
Ok(())
|
||||
}
|
||||
@@ -148,9 +203,11 @@ impl Codegen {
|
||||
self.emit_label(end_label);
|
||||
}
|
||||
Statement::BlockStatement(block) => {
|
||||
self.emit_op(OpCode::PushScope, vec![], block.span);
|
||||
for stmt in &block.body {
|
||||
self.compile_stmt(stmt)?;
|
||||
}
|
||||
self.emit_op(OpCode::PopScope, vec![], block.span);
|
||||
}
|
||||
_ => return Err(anyhow!("Unsupported statement type at {:?}", stmt.span())),
|
||||
}
|
||||
@@ -190,8 +247,8 @@ impl Codegen {
|
||||
let name = ident.name.to_string();
|
||||
self.compile_expr(&assign.right)?;
|
||||
if let Some(&id) = self.locals.get(&name) {
|
||||
self.emit_op(OpCode::Dup, vec![], assign.span);
|
||||
self.emit_op(OpCode::SetLocal, vec![Operand::U32(id)], assign.span);
|
||||
self.emit_op(OpCode::GetLocal, vec![Operand::U32(id)], assign.span);
|
||||
} else {
|
||||
return Err(anyhow!("Undefined variable: {} at {:?}", name, ident.span));
|
||||
}
|
||||
@@ -287,7 +344,13 @@ impl Codegen {
|
||||
self.emit_op(OpCode::Syscall, vec![Operand::U32(syscall_id)], call.span);
|
||||
}
|
||||
} else {
|
||||
return Err(anyhow!("Unsupported function call: {} at {:?}", name, call.span));
|
||||
// Local function call
|
||||
for arg in &call.arguments {
|
||||
if let Some(expr) = arg.as_expression() {
|
||||
self.compile_expr(expr)?;
|
||||
}
|
||||
}
|
||||
self.emit_op(OpCode::Call, vec![Operand::Label(name), Operand::U32(call.arguments.len() as u32)], call.span);
|
||||
}
|
||||
}
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
|
||||
@@ -7,30 +7,51 @@ use crate::codegen::ast_util;
|
||||
|
||||
pub struct Validator {
|
||||
errors: Vec<String>,
|
||||
local_functions: std::collections::HashSet<String>,
|
||||
}
|
||||
|
||||
impl Validator {
|
||||
pub fn validate(program: &Program) -> Result<()> {
|
||||
let mut validator = Self { errors: Vec::new() };
|
||||
let mut validator = Self {
|
||||
errors: Vec::new(),
|
||||
local_functions: std::collections::HashSet::new(),
|
||||
};
|
||||
|
||||
// 1. Check for exported tick function
|
||||
let mut tick_fn_found = false;
|
||||
// 1. Collect all local function names
|
||||
for item in &program.body {
|
||||
if let Statement::ExportNamedDeclaration(decl) = item {
|
||||
if let Some(Declaration::FunctionDeclaration(f)) = &decl.declaration {
|
||||
match item {
|
||||
Statement::FunctionDeclaration(f) => {
|
||||
if let Some(ident) = &f.id {
|
||||
if ident.name == "tick" {
|
||||
tick_fn_found = true;
|
||||
break;
|
||||
validator.local_functions.insert(ident.name.to_string());
|
||||
}
|
||||
}
|
||||
Statement::ExportNamedDeclaration(decl) => {
|
||||
if let Some(Declaration::FunctionDeclaration(f)) = &decl.declaration {
|
||||
if let Some(ident) = &f.id {
|
||||
validator.local_functions.insert(ident.name.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
Statement::ImportDeclaration(decl) => {
|
||||
if let Some(specifiers) = &decl.specifiers {
|
||||
for specifier in specifiers {
|
||||
match specifier {
|
||||
ImportDeclarationSpecifier::ImportSpecifier(s) => {
|
||||
validator.local_functions.insert(s.local.name.to_string());
|
||||
}
|
||||
ImportDeclarationSpecifier::ImportDefaultSpecifier(s) => {
|
||||
validator.local_functions.insert(s.local.name.to_string());
|
||||
}
|
||||
ImportDeclarationSpecifier::ImportNamespaceSpecifier(s) => {
|
||||
validator.local_functions.insert(s.local.name.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if !tick_fn_found {
|
||||
validator.errors.push("export function tick() not found".to_string());
|
||||
}
|
||||
|
||||
// 2. Recursive validation of the AST
|
||||
validator.visit_program(program);
|
||||
@@ -51,6 +72,7 @@ impl<'a> Visit<'a> for Validator {
|
||||
Statement::IfStatement(_) |
|
||||
Statement::BlockStatement(_) |
|
||||
Statement::ExportNamedDeclaration(_) |
|
||||
Statement::ImportDeclaration(_) |
|
||||
Statement::FunctionDeclaration(_) => {
|
||||
// Supported
|
||||
walk::walk_statement(self, stmt);
|
||||
@@ -118,7 +140,7 @@ impl<'a> Visit<'a> for Validator {
|
||||
|
||||
fn visit_call_expression(&mut self, expr: &CallExpression<'a>) {
|
||||
if let Ok(name) = ast_util::get_callee_name(&expr.callee) {
|
||||
if syscall_map::map_syscall(&name).is_none() {
|
||||
if syscall_map::map_syscall(&name).is_none() && !self.local_functions.contains(&name) {
|
||||
self.errors.push(format!("Unsupported function call: {} at {:?}", name, expr.span));
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
use std::path::Path;
|
||||
use anyhow::{Result, Context};
|
||||
use std::path::{Path, PathBuf};
|
||||
use anyhow::{Result, Context, anyhow};
|
||||
use oxc_allocator::Allocator;
|
||||
use oxc_parser::Parser;
|
||||
use oxc_span::SourceType;
|
||||
use oxc_ast::ast::*;
|
||||
use crate::codegen::Codegen;
|
||||
use crate::codegen::validator::Validator;
|
||||
use std::fs;
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use prometeu_bytecode::disasm::disasm;
|
||||
use serde::Serialize;
|
||||
|
||||
@@ -67,28 +69,83 @@ impl CompilationUnit {
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_import(base_path: &Path, import_str: &str) -> Result<PathBuf> {
|
||||
let mut path = base_path.parent().unwrap().join(import_str);
|
||||
if !path.exists() {
|
||||
if path.with_extension("ts").exists() {
|
||||
path.set_extension("ts");
|
||||
} else if path.with_extension("js").exists() {
|
||||
path.set_extension("js");
|
||||
}
|
||||
}
|
||||
if !path.exists() {
|
||||
return Err(anyhow!("Cannot resolve import '{}' from {:?}", import_str, base_path));
|
||||
}
|
||||
Ok(path.canonicalize()?)
|
||||
}
|
||||
|
||||
pub fn compile(entry: &Path) -> Result<CompilationUnit> {
|
||||
let source_text = fs::read_to_string(entry)
|
||||
.with_context(|| format!("Failed to read entry file: {:?}", entry))?;
|
||||
|
||||
let allocator = Allocator::default();
|
||||
let source_type = SourceType::from_path(entry).unwrap_or_default();
|
||||
let mut modules = HashMap::new();
|
||||
let mut queue = VecDeque::new();
|
||||
|
||||
let mut codegen = Codegen::new(entry.to_string_lossy().to_string(), source_text.clone());
|
||||
let rom = {
|
||||
let ret = Parser::new(&allocator, &source_text, source_type).parse();
|
||||
let entry_abs = entry.canonicalize()
|
||||
.with_context(|| format!("Failed to canonicalize entry path: {:?}", entry))?;
|
||||
queue.push_back(entry_abs.clone());
|
||||
|
||||
if !ret.errors.is_empty() {
|
||||
for error in ret.errors {
|
||||
eprintln!("{:?}", error);
|
||||
}
|
||||
return Err(anyhow::anyhow!("Failed to parse module"));
|
||||
while let Some(path) = queue.pop_front() {
|
||||
let path_str = path.to_string_lossy().to_string();
|
||||
if modules.contains_key(&path_str) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Validator::validate(&ret.program)?;
|
||||
let source_text = fs::read_to_string(&path)
|
||||
.with_context(|| format!("Failed to read file: {:?}", path))?;
|
||||
|
||||
// Allocate source_text in the allocator to keep it alive
|
||||
let source_text_ptr: &str = allocator.alloc_str(&source_text);
|
||||
|
||||
let source_type = SourceType::from_path(&path).unwrap_or_default();
|
||||
let parser_ret = Parser::new(&allocator, source_text_ptr, source_type).parse();
|
||||
|
||||
codegen.compile_program(&ret.program)?
|
||||
};
|
||||
if !parser_ret.errors.is_empty() {
|
||||
for error in parser_ret.errors {
|
||||
eprintln!("{:?}", error);
|
||||
}
|
||||
return Err(anyhow!("Failed to parse module: {:?}", path));
|
||||
}
|
||||
|
||||
// Validate individual module
|
||||
Validator::validate(&parser_ret.program)?;
|
||||
|
||||
// Find imports to add to queue
|
||||
for item in &parser_ret.program.body {
|
||||
if let Statement::ImportDeclaration(decl) = item {
|
||||
let import_path = decl.source.value.as_str();
|
||||
let resolved = resolve_import(&path, import_path)?;
|
||||
queue.push_back(resolved);
|
||||
}
|
||||
}
|
||||
|
||||
modules.insert(path_str, (source_text_ptr, parser_ret.program));
|
||||
}
|
||||
|
||||
let entry_str = entry_abs.to_string_lossy().to_string();
|
||||
let mut program_list = Vec::new();
|
||||
|
||||
// Add entry program first
|
||||
let entry_data = modules.get(&entry_str).ok_or_else(|| anyhow!("Entry module not found after loading"))?;
|
||||
program_list.push((entry_str.clone(), entry_data.0.to_string(), &entry_data.1));
|
||||
|
||||
// Add all other programs
|
||||
for (path, (source, program)) in &modules {
|
||||
if path != &entry_str {
|
||||
program_list.push((path.clone(), source.to_string(), program));
|
||||
}
|
||||
}
|
||||
|
||||
let mut codegen = Codegen::new(entry_str.clone(), entry_data.0.to_string());
|
||||
let rom = codegen.compile_programs(program_list)?;
|
||||
|
||||
Ok(CompilationUnit {
|
||||
rom,
|
||||
|
||||
Reference in New Issue
Block a user