From 6a8a8fe5822e62d34b91e61d83ea0eb3eb71e25d Mon Sep 17 00:00:00 2001 From: Daniel Connelly Date: Wed, 15 Nov 2023 22:53:35 +0100 Subject: [PATCH] empty compiler --- src/analyzer.rs | 5 ++--- src/compiler.rs | 13 +++++++++++++ src/driver.rs | 23 +++++++++++++---------- src/lib.rs | 2 ++ src/parser.rs | 13 ++++++++----- src/wasm.rs | 2 ++ 6 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 src/compiler.rs create mode 100644 src/wasm.rs diff --git a/src/analyzer.rs b/src/analyzer.rs index fe83fd4..52b2763 100644 --- a/src/analyzer.rs +++ b/src/analyzer.rs @@ -69,7 +69,7 @@ impl Analyzer { let body = self.block(f.body)?; let ret = Box::new(self.typ(&f.ret)?); let cargo = FnType { params: param_types, ret }; - let func = TypedFunc { name: f.name, params, body, ret: f.ret, cargo }; + let func = Func { name: f.name, params, body, ret: f.ret, cargo }; self.ctx.pop_frame(); Ok(func) } @@ -172,8 +172,7 @@ mod test { use crate::scanner; fn parse(input: &[u8]) -> parser::Parser<&[u8]> { - let s = scanner::scan(input); - parser::parse(s) + parser::Parser::new(scanner::scan(input)) } #[test] diff --git a/src/compiler.rs b/src/compiler.rs new file mode 100644 index 0000000..19243f3 --- /dev/null +++ b/src/compiler.rs @@ -0,0 +1,13 @@ +use crate::ast::*; +use crate::wasm::*; +use std::result; +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum Error {} + +type Result = result::Result; + +pub fn compile(_program: TypedProgram) -> Result> { + Ok(Vec::new()) +} diff --git a/src/driver.rs b/src/driver.rs index 3738cdf..69a117e 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -1,4 +1,5 @@ use crate::analyzer; +use crate::compiler; use crate::parser; use crate::scanner; use std::fs; @@ -19,19 +20,12 @@ pub enum Error { ParserError(#[from] parser::Error), #[error("analyzer: {0}")] AnalyzerError(#[from] analyzer::Error), + #[error("compiler: {0}")] + CompilerError(#[from] compiler::Error), } pub type Result = result::Result; -pub fn compile(r: R, _w: W) -> Result<()> { - let s = scanner::scan(io::BufReader::new(r)); - let mut p = parser::parse(s); - let ast = p.program()?; - let typed_ast = analyzer::analyze(ast)?; - println!("{:#?}", typed_ast); - Ok(()) -} - fn make_output_path(p: &path::Path) -> Result { let mut output_path = p.to_owned(); if output_path.set_extension("wasm") { @@ -41,6 +35,15 @@ fn make_output_path(p: &path::Path) -> Result { } } +pub fn compile(_w: W, r: R) -> Result<()> { + let toks = scanner::scan(io::BufReader::new(r)); + let ast = parser::parse(toks)?; + let typed_ast = analyzer::analyze(ast)?; + let wasm = compiler::compile(typed_ast)?; + println!("{:#?}", wasm); + Ok(()) +} + pub fn compile_file>(input_path: P) -> Result<()> { let input_path = input_path.into(); let output_path = make_output_path(&input_path)?; @@ -49,5 +52,5 @@ pub fn compile_file>(input_path: P) -> Result<()> { let r = io::BufReader::new(f); let w = fs::File::create(&output_path) .map_err(|err| Error::IOError { path: output_path, err })?; - compile(r, w) + compile(w, r) } diff --git a/src/lib.rs b/src/lib.rs index 696a085..b31f15b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,11 @@ pub mod analyzer; pub mod ast; pub mod builtins; +pub mod compiler; pub mod driver; pub mod parser; pub mod scanner; pub mod symbol_table; pub mod token; pub mod types; +pub mod wasm; diff --git a/src/parser.rs b/src/parser.rs index 6408e8d..edb14be 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -24,7 +24,11 @@ pub struct Parser { } impl Parser { - fn eof(&mut self) -> bool { + pub fn new(scanner: scanner::Scanner) -> Parser { + Parser { scanner: scanner.peekable() } + } + + pub fn eof(&mut self) -> bool { self.scanner.peek().is_none() } @@ -165,8 +169,8 @@ impl Parser { } } -pub fn parse(scanner: scanner::Scanner) -> Parser { - Parser { scanner: scanner.peekable() } +pub fn parse(scanner: scanner::Scanner) -> Result { + Parser::new(scanner).program() } #[cfg(test)] @@ -174,8 +178,7 @@ mod test { use super::*; fn parse(input: &[u8]) -> Parser<&[u8]> { - let s = scanner::scan(input); - super::parse(s) + Parser { scanner: scanner::scan(input).peekable() } } #[test] diff --git a/src/wasm.rs b/src/wasm.rs new file mode 100644 index 0000000..cfdd089 --- /dev/null +++ b/src/wasm.rs @@ -0,0 +1,2 @@ +#[derive(Debug)] +pub enum Wasm {}