analyzer, check function arity, catch redefinitions

This commit is contained in:
2025-07-27 13:26:23 +02:00
parent 4e43d23263
commit dc3abc36a1
3 changed files with 168 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
mod analyzer;
mod codegen_x86_64;
mod parser;
mod tokenizer;
@@ -13,6 +14,7 @@ use tokenizer::ZernError;
use clap::Parser;
fn compile_file_to(
analyzer: &mut analyzer::Analyzer,
codegen: &mut codegen_x86_64::CodegenX86_64,
filename: &str,
source: String,
@@ -23,6 +25,14 @@ fn compile_file_to(
let parser = parser::Parser::new(tokens);
let statements = parser.parse()?;
for stmt in &statements {
analyzer.register_function(stmt)?;
}
for stmt in &statements {
analyzer.analyze_stmt(stmt)?;
}
for stmt in statements {
codegen.compile_stmt(&mut codegen_x86_64::Env::new(), stmt)?;
}
@@ -51,10 +61,16 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
let filename = Path::new(&args.path).file_name().unwrap().to_str().unwrap();
let mut analyzer = analyzer::Analyzer::new();
let mut codegen = codegen_x86_64::CodegenX86_64::new();
codegen.emit_prologue()?;
compile_file_to(&mut codegen, "std.zr", include_str!("std.zr").into())?;
compile_file_to(&mut codegen, filename, source)?;
compile_file_to(
&mut analyzer,
&mut codegen,
"std.zr",
include_str!("std.zr").into(),
)?;
compile_file_to(&mut analyzer, &mut codegen, filename, source)?;
if !args.output_asm {
fs::write(format!("{}.s", args.out), codegen.get_output()).unwrap();