From ba65669225f3cba7480e9ee0f7a1ccff0a9b20bc Mon Sep 17 00:00:00 2001 From: Andrew Segavac Date: Sun, 19 Apr 2020 22:22:15 -0600 Subject: [PATCH] setup main to specify an entry point --- Cargo.lock | 54 ++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 ++- examples/math/main.bl | 11 +++++++++ src/ast.rs | 21 ----------------- src/compiler.rs | 2 -- src/main.rs | 47 +++++++++++++++++++++++++++---------- 6 files changed, 101 insertions(+), 37 deletions(-) create mode 100644 examples/math/main.bl diff --git a/Cargo.lock b/Cargo.lock index 8c66a3c..eb0d4f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,14 @@ dependencies = [ "memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "arrayref" version = "0.3.6" @@ -100,8 +108,9 @@ dependencies = [ [[package]] name = "boring-lang" -version = "0.1.0" +version = "0.0.1" dependencies = [ + "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "inkwell 0.1.0 (git+https://github.com/TheDan64/inkwell?branch=llvm7-0)", "lalrpop 0.18.1 (registry+https://github.com/rust-lang/crates.io-index)", "lalrpop-util 0.18.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -128,6 +137,20 @@ name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "clap" +version = "2.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -685,6 +708,11 @@ name = "string_cache_shared" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "strsim" version = "0.9.3" @@ -720,6 +748,14 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "thread_local" version = "1.0.1" @@ -733,6 +769,11 @@ name = "typenum" version = "1.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-xid" version = "0.1.0" @@ -743,6 +784,11 @@ name = "unicode-xid" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "vec_map" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" @@ -769,6 +815,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum aho-corasick 0.7.10 (registry+https://github.com/rust-lang/crates.io-index)" = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada" +"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum arrayref 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" "checksum arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" "checksum ascii-canvas 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff8eb72df928aafb99fe5d37b383f2fe25bd2a765e3e5f7c365916b6f2463a29" @@ -786,6 +833,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" "checksum cc 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)" = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum constant_time_eq 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" "checksum crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" @@ -853,14 +901,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum string_cache 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)" = "89c058a82f9fd69b1becf8c274f412281038877c553182f1d02eb027045a2d67" "checksum string_cache_codegen 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0f45ed1b65bf9a4bf2f7b7dc59212d1926e9eaf00fa998988e420fd124467c6" "checksum string_cache_shared 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b1884d1bc09741d466d9b14e6d37ac89d6909cbcac41dd9ae982d4d063bbedfc" +"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum strsim 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" "checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" "checksum syn 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03" "checksum term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42" +"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" "checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" +"checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" +"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" diff --git a/Cargo.toml b/Cargo.toml index 4cbe6af..8a0639c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "boring-lang" -version = "0.1.0" +version = "0.0.1" authors = ["asegavac"] edition = "2018" @@ -14,3 +14,4 @@ features = ["lexer"] lalrpop-util = "0.18.1" regex = "1" inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "llvm7-0" } +clap = "2.33.0" diff --git a/examples/math/main.bl b/examples/math/main.bl new file mode 100644 index 0000000..6896bcb --- /dev/null +++ b/examples/math/main.bl @@ -0,0 +1,11 @@ +fn add(a, b) { + a + b +} + +fn subtract(a, b) { + a - b +} + +fn main() { + add(4, subtract(5, 2)) +} diff --git a/src/ast.rs b/src/ast.rs index c27409d..e19d387 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -5,14 +5,6 @@ pub enum Operator { Div, Plus, Minus, - // Gt, - // Gte, - // Lt, - // Lte, - // Eq, - // Mod, - // Exp, - // FloorDiv, } @@ -20,10 +12,6 @@ pub struct LiteralInt { pub value: i64 } -// pub struct LiteralString { -// value: String -// } - pub struct Identifier { pub name: String } @@ -35,7 +23,6 @@ pub struct FunctionCall { pub enum Expression { LiteralInt(LiteralInt), - // LiteralString(LiteralString), FunctionCall(FunctionCall), Identifier(Identifier), Op(Box, Operator, Box), @@ -47,23 +34,15 @@ pub struct Block { pub struct VariableDeclaration { pub name: Identifier, - // type: Identifier, } pub struct Function { pub name: Identifier, - // return_type: Identifier, pub arguments: Vec, pub block: Block, } -// pub struct Assignment { -// variable: VariableDeclaration, -// expression: Expression, -// } - - pub struct Module { pub functions: Vec, } diff --git a/src/compiler.rs b/src/compiler.rs index a6dc6c8..232e3aa 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -62,7 +62,6 @@ impl<'ctx> ModuleCodeGen<'ctx> { } pub fn gen_function_call(&mut self, scope: &Scope<'ctx>, function_call: &ast::FunctionCall) -> IntValue<'ctx> { - println!("Calling function: {}", &function_call.name.name); let fn_value = self.module.get_function(&function_call.name.name).unwrap(); let mut arguments = Vec::new(); for expression in (&function_call.arguments).into_iter() { @@ -89,7 +88,6 @@ impl<'ctx> ModuleCodeGen<'ctx> { args.push(self.context.i64_type().into()); } let fn_type = self.context.i64_type().fn_type(&args, false); - println!("Adding function: {}", &function.name.name); let fn_value = self.module.add_function(&function.name.name, fn_type, None); fn_value } diff --git a/src/main.rs b/src/main.rs index a97ab6e..526b182 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,26 +3,49 @@ mod ast; lalrpop_mod!(pub grammar); // synthesized by LALRPOP +use std::fs; +use std::io::Write; mod compiler; use inkwell::context::Context; +extern crate clap; +use clap::{Arg, App}; + fn main() { - let module_ast = grammar::ModuleParser::new().parse(" - fn add(a, b) { - a + b - } - fn subtract(a, b) { - a - b - } - fn main() { - add(4, subtract(5, 2)) - } - ").unwrap(); + let matches = App::new("Boring Language Compiler") + .version("0.0.1") + .author("Andrew Segavac") + .about("Compiles boring language files to LLVM IR.") + .arg(Arg::with_name("OUTPUT") + .short("o") + .long("out") + .value_name("OUTOUT") + .help("Sets an output file") + .takes_value(true)) + .arg(Arg::with_name("INPUT") + .help("Sets the input file") + .required(true) + .index(1)) + .arg(Arg::with_name("v") + .short("v") + .multiple(true) + .help("Sets the level of verbosity")) + .get_matches(); + let input = matches.value_of("INPUT").unwrap(); + + let default_output = input.rsplitn(2, ".").collect::>().last().unwrap().clone(); + let output = matches.value_of("OUTPUT").unwrap_or(default_output); + + let contents = fs::read_to_string(input).expect("input file not found"); + let module_ast = grammar::ModuleParser::new().parse(&contents).unwrap(); //TODO: convert to error + let context = Context::create(); let mut code_gen = compiler::ModuleCodeGen::new(&context, "main".to_string()); code_gen.gen_module(module_ast); - println!("{}", code_gen.dump()); + + let mut f = fs::File::create(output).expect("Unable to create out file"); + f.write_all(code_gen.dump().as_bytes()).expect("Unable to write data"); }