- compiler works for basic maths expressions and functions

- basic pointers and reading values from pointers works
- writing to pointers not yet implemented (looks painful so a problem
  for tomorrow)
- updated print library. the compiler has this hardcoded in all programs
  for now
This commit is contained in:
2026-02-03 02:11:30 +00:00
parent 5573c5a609
commit 3afeafc9d4
17 changed files with 2009 additions and 807 deletions
+40 -8
View File
@@ -3,18 +3,33 @@
use std::{fs, path::Path};
pub mod lexer;
pub mod parserprototype;
use parserprototype::Parser;
pub mod parser;
use parser::Parser;
pub mod codegen;
mod registers;
mod semantic_analyser;
use crate::parserprototype::ParseResult;
use crate::{codegen::CodeGenerator, parser::ParseResult, semantic_analyser::Analyser};
fn main() {
println!("Hello, world!");
// read from input file: syntax "c_compiler <src.c> [output.dsa]"
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
eprintln!("Usage: c_compiler <src.c> [output.dsa]");
return;
}
let path = Path::new("../resources/dsc/example.dsc");
let contents = fs::read_to_string(path).expect("Failed to read file");
let input_file = &args[1];
let output_file = if args.len() > 2 {
&args[2]
} else {
"output.dsa"
};
let lexer = lexer::Lexer::new(&contents);
// read input
let input = std::fs::read_to_string(input_file).expect("Failed to read input file");
let lexer = lexer::Lexer::new(&input);
let tokens = lexer.collect::<Vec<_>>();
println!("{tokens:?}");
@@ -29,5 +44,22 @@ fn main() {
panic!("Parser denied parsing")
}
};
println!("{ast:?}");
println!("{ast:#?}");
let analyser = Analyser::new();
analyser.analyse(ast.clone()).unwrap();
// Code Gen
let mut generator = CodeGenerator::new(ast);
let result = match generator.generate() {
Ok(code) => code,
Err(e) => {
eprintln!("Parsing error: {:?}", e);
return;
}
};
println!("{result}");
std::fs::write(output_file, &result).expect("Failed to write output");
println!("Result written to {}", output_file);
}