diff --git a/compiler/CHANGELOG.md b/compiler/CHANGELOG.md index f9591f0..79c5c8b 100644 --- a/compiler/CHANGELOG.md +++ b/compiler/CHANGELOG.md @@ -2,14 +2,16 @@ ## TODO -- [ ] Automatic semicolon insertion -- [ ] Improve error messages -- [ ] Parse other language constructions -- [ ] Type checking -- [ ] Check for conflicting identifiers -- [ ] Namespace identifiers in the symbol table -- [ ] Stdlib -- [ ] Document code +- Parse multiple bindings +- Compile from file +- Automatic semicolon insertion +- Improve error messages +- Parse other language constructions +- Type checking +- Check for conflicting identifiers +- Namespace identifiers in the symbol table +- Stdlib +- Document code ## v0.0.5 diff --git a/compiler/Cargo.lock b/compiler/Cargo.lock index ea81896..08911e8 100644 --- a/compiler/Cargo.lock +++ b/compiler/Cargo.lock @@ -160,7 +160,7 @@ checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" [[package]] name = "misti" -version = "0.0.4" +version = "0.0.5" dependencies = [ "clap", "colored", diff --git a/compiler/Cargo.toml b/compiler/Cargo.toml index b2fb68d..34d4a41 100644 --- a/compiler/Cargo.toml +++ b/compiler/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "misti" -version = "0.0.4" +version = "0.0.5" edition = "2021" diff --git a/compiler/examples/val_binding.misti b/compiler/examples/val_binding.misti deleted file mode 100644 index f97cbed..0000000 --- a/compiler/examples/val_binding.misti +++ /dev/null @@ -1 +0,0 @@ -val identifier = 322 \ No newline at end of file diff --git a/compiler/src/file/mod.rs b/compiler/src/file/mod.rs new file mode 100644 index 0000000..2944a7e --- /dev/null +++ b/compiler/src/file/mod.rs @@ -0,0 +1,62 @@ +use std::{ + fs, + path::{Path, PathBuf}, +}; + +use crate::{ + codegen, + error_handling::PrintableError, + lexic, semantic, + symbol_table::{self, SymbolTable}, + syntax, + token::Token, +}; + +pub fn compile_file(input: &String, output: &String) { + let input_path = Path::new(input); + + if !input_path.is_file() { + panic!("Input path is not a valid file") + } + + let bytes = fs::read(input_path).expect("INPUT_PATH should be valid"); + let contents = String::from_utf8(bytes).expect("INPUT_PATH's encoding MUST be UTF-8"); + + let js_code = compile(&contents); + + let output_path = Path::new(output); + fs::write(output_path, js_code).expect("Error writing to output path"); +} + +/// Executes Lexical analysis, handles errors and calls build_ast for the next phase +fn compile(input: &String) -> String { + let tokens = lexic::get_tokens(input); + + match tokens { + Ok(tokens) => build_ast(input, tokens), + Err(error) => { + let chars: Vec = input.chars().into_iter().collect(); + panic!("{}", error.get_error_str(&chars)) + } + } +} + +/// Executes Syntax analysis, and for now, Semantic analysis and Code generation. +/// +/// Prints the generated code in stdin +fn build_ast(input: &String, tokens: Vec) -> String { + let ast = syntax::construct_ast(&tokens); + + match ast { + Ok(ast) => { + let mut symbol_table = SymbolTable::new(); + semantic::check_ast(&ast, &mut symbol_table); + + codegen::codegen(&ast) + } + Err(reason) => { + let chars: Vec = input.chars().into_iter().collect(); + panic!("{}", reason.get_error_str(&chars)) + } + } +} diff --git a/compiler/src/main.rs b/compiler/src/main.rs index 358daed..ad0fc95 100755 --- a/compiler/src/main.rs +++ b/compiler/src/main.rs @@ -2,6 +2,8 @@ use clap::{Parser, Subcommand}; // Module to handle the repl and its compilation mod repl; +// Module to handle file compilation +mod file; // Defines the types of tokens and provides functions to create them mod token; // Module to handle lexical analysis @@ -29,7 +31,7 @@ struct Cli { #[derive(Subcommand)] enum Commands { - /// compiles a single file + /// Compiles a single file C { /// File to compile file: String, @@ -41,7 +43,7 @@ enum Commands { R {}, } -const VERSION: &str = "0.0.1"; +const VERSION: &str = "0.0.5"; fn get_copyright() -> String { format!( @@ -57,15 +59,14 @@ fn get_copyright() -> String { /// - `misti w, --watch, -w` : Starts the compiler in watch mode /// - `misti -c FILE -o OUTPUT` : Compiles FILE and writes the result in OUTPUT fn main() { - println!("{}", get_copyright()); - let cli = Cli::parse(); match &cli.command { - Some(Commands::C { file: _, output: _ }) => { - println!("Compile a file: Not implemented") + Some(Commands::C { file: input, output }) => { + file::compile_file(input, output) } Some(Commands::R {}) => { + println!("{}", get_copyright()); let _ = repl::run(); } None => { diff --git a/compiler/src/repl/mod.rs b/compiler/src/repl/mod.rs index e17a128..51302f1 100755 --- a/compiler/src/repl/mod.rs +++ b/compiler/src/repl/mod.rs @@ -31,8 +31,8 @@ fn build_ast(input: &String, tokens: Vec, symbol_table: &mut SymbolTable) let ast = syntax::construct_ast(&tokens); match ast { - Ok( ast) => { - semantic::check_ast(& ast, symbol_table); + Ok(ast) => { + semantic::check_ast(&ast, symbol_table); let js_code = codegen::codegen(&ast); println!("{}", js_code) diff --git a/compiler/src/syntax/mod.rs b/compiler/src/syntax/mod.rs index 5088113..ab114a1 100755 --- a/compiler/src/syntax/mod.rs +++ b/compiler/src/syntax/mod.rs @@ -42,7 +42,5 @@ pub fn construct_ast<'a>(tokens: &'a Vec) -> Result, MistiE fn next_construct<'a>(tokens: &'a Vec, current_pos: usize) -> SyntaxResult { None.or_else(|| binding::try_parse(tokens, current_pos)) - .unwrap_or_else(|| { - SyntaxResult::None - }) + .unwrap_or_else(|| SyntaxResult::None) } diff --git a/doc-generator/Cargo.lock b/doc-generator/Cargo.lock index 8f453a6..f93bfc7 100644 --- a/doc-generator/Cargo.lock +++ b/doc-generator/Cargo.lock @@ -2,15 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - [[package]] name = "anstream" version = "0.2.6" @@ -74,39 +65,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "bumpalo" -version = "3.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" - [[package]] name = "cc" version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "chrono" -version = "0.4.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b" -dependencies = [ - "iana-time-zone", - "js-sys", - "num-integer", - "num-traits", - "time", - "wasm-bindgen", - "winapi", -] - [[package]] name = "clap" version = "4.2.0" @@ -140,7 +104,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.11", + "syn", ] [[package]] @@ -149,16 +113,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" -[[package]] -name = "codespan-reporting" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" -dependencies = [ - "termcolor", - "unicode-width", -] - [[package]] name = "colored" version = "2.0.0" @@ -185,56 +139,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "core-foundation-sys" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" - -[[package]] -name = "cxx" -version = "1.0.94" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f61f1b6389c3fe1c316bf8a4dccc90a38208354b330925bce1f74a6c4756eb93" -dependencies = [ - "cc", - "cxxbridge-flags", - "cxxbridge-macro", - "link-cplusplus", -] - -[[package]] -name = "cxx-build" -version = "1.0.94" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12cee708e8962df2aeb38f594aae5d827c022b6460ac71a7a3e2c3c2aae5a07b" -dependencies = [ - "cc", - "codespan-reporting", - "once_cell", - "proc-macro2", - "quote", - "scratch", - "syn 2.0.11", -] - -[[package]] -name = "cxxbridge-flags" -version = "1.0.94" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7944172ae7e4068c533afbb984114a56c46e9ccddda550499caa222902c7f7bb" - -[[package]] -name = "cxxbridge-macro" -version = "1.0.94" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.11", -] - [[package]] name = "doc-generator" version = "0.1.0" @@ -294,30 +198,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" -[[package]] -name = "iana-time-zone" -version = "0.1.54" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c17cc76786e99f8d2f055c11159e7f0091c42474dcc3189fbab96072e873e6d" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" -dependencies = [ - "cxx", - "cxx-build", -] - [[package]] name = "indexmap" version = "1.9.3" @@ -351,15 +231,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "js-sys" -version = "0.3.61" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730" -dependencies = [ - "wasm-bindgen", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -372,15 +243,6 @@ version = "0.2.140" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" -[[package]] -name = "link-cplusplus" -version = "1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" -dependencies = [ - "cc", -] - [[package]] name = "linked-hash-map" version = "0.5.6" @@ -393,15 +255,6 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" -[[package]] -name = "log" -version = "0.4.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] - [[package]] name = "markdown" version = "1.0.0-alpha.7" @@ -419,32 +272,12 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "misti" -version = "0.0.4" +version = "0.0.5" dependencies = [ - "chrono", "clap", "colored", ] -[[package]] -name = "num-integer" -version = "0.1.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" -dependencies = [ - "autocfg", - "num-traits", -] - -[[package]] -name = "num-traits" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" -dependencies = [ - "autocfg", -] - [[package]] name = "once_cell" version = "1.17.1" @@ -483,12 +316,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "scratch" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1" - [[package]] name = "serde" version = "1.0.160" @@ -510,17 +337,6 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.11" @@ -532,26 +348,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "termcolor" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" -dependencies = [ - "winapi-util", -] - -[[package]] -name = "time" -version = "0.1.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" -dependencies = [ - "libc", - "wasi", - "winapi", -] - [[package]] name = "toml" version = "0.7.3" @@ -598,78 +394,12 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" -[[package]] -name = "unicode-width" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" - [[package]] name = "utf8parse" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - -[[package]] -name = "wasm-bindgen" -version = "0.2.84" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b" -dependencies = [ - "cfg-if", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.84" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn 1.0.109", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.84" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.84" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.84" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d" - [[package]] name = "winapi" version = "0.3.9" @@ -686,30 +416,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows" -version = "0.46.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdacb41e6a96a052c6cb63a144f24900236121c6f63f4f8219fef5977ecb0c25" -dependencies = [ - "windows-targets", -] - [[package]] name = "windows-sys" version = "0.45.0" diff --git a/doc-generator/src/pages/mod.rs b/doc-generator/src/pages/mod.rs index 25b7738..12dbc7e 100644 --- a/doc-generator/src/pages/mod.rs +++ b/doc-generator/src/pages/mod.rs @@ -86,8 +86,8 @@ pub fn generate_pages_html(file_tree: &Node, current_path: &Path) -> String { Node::File(file) => { if file.path == "index" { format!( - "
  • - Index + "
  • + Index
  • ", current_path.to_str().unwrap() ) @@ -95,8 +95,8 @@ pub fn generate_pages_html(file_tree: &Node, current_path: &Path) -> String { String::from("") } else { format!( - "
  • - {} + "
  • + {}
  • ", current_path.to_str().unwrap(), file.path, @@ -119,7 +119,7 @@ pub fn generate_pages_html(file_tree: &Node, current_path: &Path) -> String { format!("
      {}
    ", sub_nodes_html.join("")) } else { format!( - "
  • + "
  • {}
      {}
  • ", diff --git a/doc-generator/static/template.html b/doc-generator/static/template.html index 31762d2..d03930a 100644 --- a/doc-generator/static/template.html +++ b/doc-generator/static/template.html @@ -73,10 +73,11 @@ document.head.appendChild(linkEl); +