]> Witch of Git - ivy/blob - src/main.rs
Implement basic code generation
[ivy] / src / main.rs
1 use ess::{self, parser::ParseError, Sexp};
2 use std::io::{self, Read};
3
4 mod ast;
5 mod trans;
6
7 use ast::Ast;
8 use trans::translate;
9
10 fn parse(input: &str) -> Result<Sexp, ParseError> {
11 let (sexp, rest) = ess::parse_one(input)?;
12 if !rest.trim().is_empty() {
13 return Err(ParseError::Unexpected('\0', sexp.get_loc().1));
14 }
15 Ok(sexp)
16 }
17
18 static BUILTINS: &[&str] = &[
19 "debug", "true", "false", "<", ">", "<=", ">=", "==", "!=", "+", "-", "*", "/", "%",
20 ];
21
22 fn main() -> io::Result<()> {
23 let stdin = std::io::stdin();
24 let mut text = String::new();
25 stdin.lock().read_to_string(&mut text)?;
26 let sexp = match parse(&text) {
27 Ok(item) => item,
28 Err(err) => {
29 println!("{:?}", err);
30 std::process::exit(1);
31 }
32 };
33 let (ast, builtins) = match Ast::parse(BUILTINS, &sexp) {
34 Ok(ast) => ast,
35 Err(err) => {
36 println!("{}", err);
37 std::process::exit(1);
38 }
39 };
40 let code = match translate(&ast) {
41 Ok(code) => code,
42 Err(err) => {
43 println!("{}", err);
44 std::process::exit(1);
45 }
46 };
47 let stdout = std::io::stdout();
48 trans::x64::write_compile(&mut stdout.lock(), &code)?;
49 Ok(())
50 }