Skip to content

Commit

Permalink
empty compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
dhconnelly committed Nov 15, 2023
1 parent 8538b66 commit 6a8a8fe
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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]
Expand Down
13 changes: 13 additions & 0 deletions src/compiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::ast::*;
use crate::wasm::*;
use std::result;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {}

type Result<T> = result::Result<T, Error>;

pub fn compile(_program: TypedProgram) -> Result<Vec<Wasm>> {
Ok(Vec::new())
}
23 changes: 13 additions & 10 deletions src/driver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::analyzer;
use crate::compiler;
use crate::parser;
use crate::scanner;
use std::fs;
Expand All @@ -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<T> = result::Result<T, Error>;

pub fn compile<R: io::Read, W: io::Write>(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<path::PathBuf> {
let mut output_path = p.to_owned();
if output_path.set_extension("wasm") {
Expand All @@ -41,6 +35,15 @@ fn make_output_path(p: &path::Path) -> Result<path::PathBuf> {
}
}

pub fn compile<W: io::Write, R: io::Read>(_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<P: Into<path::PathBuf>>(input_path: P) -> Result<()> {
let input_path = input_path.into();
let output_path = make_output_path(&input_path)?;
Expand All @@ -49,5 +52,5 @@ pub fn compile_file<P: Into<path::PathBuf>>(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)
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 8 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ pub struct Parser<R: io::BufRead> {
}

impl<R: io::BufRead> Parser<R> {
fn eof(&mut self) -> bool {
pub fn new(scanner: scanner::Scanner<R>) -> Parser<R> {
Parser { scanner: scanner.peekable() }
}

pub fn eof(&mut self) -> bool {
self.scanner.peek().is_none()
}

Expand Down Expand Up @@ -165,17 +169,16 @@ impl<R: io::BufRead> Parser<R> {
}
}

pub fn parse<R: io::BufRead>(scanner: scanner::Scanner<R>) -> Parser<R> {
Parser { scanner: scanner.peekable() }
pub fn parse<R: io::BufRead>(scanner: scanner::Scanner<R>) -> Result<Program> {
Parser::new(scanner).program()
}

#[cfg(test)]
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]
Expand Down
2 changes: 2 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[derive(Debug)]
pub enum Wasm {}

0 comments on commit 6a8a8fe

Please sign in to comment.