From 9aa15dfbab5e0f17c7603c5aa5e5229bfe2327f9 Mon Sep 17 00:00:00 2001 From: MCorange Date: Sun, 26 May 2024 23:38:06 +0300 Subject: [PATCH] Initial --- .gitignore | 1 + Cargo.lock | 346 +++++++++++++++++++++++++++++++++ Cargo.toml | 4 + bfox-asmgen/.gitignore | 1 + bfox-asmgen/Cargo.toml | 8 + bfox-asmgen/src/lib.rs | 0 bfox-parser/.gitignore | 1 + bfox-parser/Cargo.toml | 10 + bfox-parser/src/lib.rs | 56 ++++++ bfox-parser/src/types/mod.rs | 27 +++ bfox-parser/src/types/token.rs | 20 ++ bfox/.gitignore | 1 + bfox/Cargo.toml | 15 ++ bfox/src/cli.rs | 16 ++ bfox/src/main.rs | 29 +++ hello_word.bf | 1 + 16 files changed, 536 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 bfox-asmgen/.gitignore create mode 100644 bfox-asmgen/Cargo.toml create mode 100644 bfox-asmgen/src/lib.rs create mode 100644 bfox-parser/.gitignore create mode 100644 bfox-parser/Cargo.toml create mode 100644 bfox-parser/src/lib.rs create mode 100644 bfox-parser/src/types/mod.rs create mode 100644 bfox-parser/src/types/token.rs create mode 100644 bfox/.gitignore create mode 100644 bfox/Cargo.toml create mode 100644 bfox/src/cli.rs create mode 100644 bfox/src/main.rs create mode 100644 hello_word.bf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..93b068a --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,346 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "anyhow" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" + +[[package]] +name = "bfox" +version = "0.1.0" +dependencies = [ + "anyhow", + "bfox-asmgen", + "bfox-parser", + "camino", + "clap", + "env_logger", + "log", +] + +[[package]] +name = "bfox-asmgen" +version = "0.1.0" + +[[package]] +name = "bfox-parser" +version = "0.1.0" +dependencies = [ + "anyhow", + "log", +] + +[[package]] +name = "camino" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0ec6b951b160caa93cc0c7b209e5a3bff7aae9062213451ac99493cd844c239" + +[[package]] +name = "clap" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" + +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + +[[package]] +name = "memchr" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" + +[[package]] +name = "proc-macro2" +version = "1.0.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec96c6a92621310b51366f1e28d05ef11489516e93be030060e5fc12024a49d6" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7691720 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,4 @@ + +[workspace] +members = [ "bfox", "bfox-asmgen","bfox-parser"] +resolver = "2" diff --git a/bfox-asmgen/.gitignore b/bfox-asmgen/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/bfox-asmgen/.gitignore @@ -0,0 +1 @@ +/target diff --git a/bfox-asmgen/Cargo.toml b/bfox-asmgen/Cargo.toml new file mode 100644 index 0000000..fb6a097 --- /dev/null +++ b/bfox-asmgen/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "bfox-asmgen" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/bfox-asmgen/src/lib.rs b/bfox-asmgen/src/lib.rs new file mode 100644 index 0000000..e69de29 diff --git a/bfox-parser/.gitignore b/bfox-parser/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/bfox-parser/.gitignore @@ -0,0 +1 @@ +/target diff --git a/bfox-parser/Cargo.toml b/bfox-parser/Cargo.toml new file mode 100644 index 0000000..eb1db6b --- /dev/null +++ b/bfox-parser/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "bfox-parser" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.86" +log = "0.4.21" diff --git a/bfox-parser/src/lib.rs b/bfox-parser/src/lib.rs new file mode 100644 index 0000000..03806f6 --- /dev/null +++ b/bfox-parser/src/lib.rs @@ -0,0 +1,56 @@ +use types::{token::Token, Loc}; + + + +mod types; + +pub struct Parser { + tokens: Vec, + chars: Vec, + loc: Loc, +} + +impl Parser { + pub fn new() -> Self { + Self { + tokens: Vec::new(), + chars: Vec::new(), + loc: Loc::default(), + } + } + + pub fn parse(&mut self, code: String, file_name: String) -> anyhow::Result<()> { + self.loc.file = file_name; + self.loc.col = 0; + self.loc.line = 0; + + self.chars = code.chars().collect(); + + // Parse here + + Ok(()) + } + + /// Pops one token + fn next(&mut self) -> Option { + if let Some(c) = self.chars.pop() { + match c { + '\n' => { + self.loc.line += 1; + self.loc.col = 0; + } + _ => { + self.loc.col += 1; + } + } + return Some(c); + } + + None + } + + pub fn tokens(&self) -> &[Token] { + &self.tokens + } + +} \ No newline at end of file diff --git a/bfox-parser/src/types/mod.rs b/bfox-parser/src/types/mod.rs new file mode 100644 index 0000000..313bb81 --- /dev/null +++ b/bfox-parser/src/types/mod.rs @@ -0,0 +1,27 @@ +pub mod token; + +use std::fmt::Display; + +#[derive(Debug, Default, Clone)] +pub struct Loc { + pub file: String, + pub line: usize, + pub col: usize +} + +impl Loc { + pub fn new(file: S, line: usize, col: usize) -> Self { + Self{ + file: file.to_string(), + line, + col, + } + } +} + +impl Display for Loc { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}:{}:{}", self.file, self.line, self.col)?; + Ok(()) + } +} \ No newline at end of file diff --git a/bfox-parser/src/types/token.rs b/bfox-parser/src/types/token.rs new file mode 100644 index 0000000..6b07c96 --- /dev/null +++ b/bfox-parser/src/types/token.rs @@ -0,0 +1,20 @@ +use super::Loc; + + +#[derive(Debug, Clone)] +pub struct Token { + pub typ: TokenType, + pub loc: Loc, +} + +#[derive(Debug, Clone, Copy)] +pub enum TokenType { + Add, + Sub, + Left, + Right, + LoopR, + LoopL, + Print, + Input +} \ No newline at end of file diff --git a/bfox/.gitignore b/bfox/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/bfox/.gitignore @@ -0,0 +1 @@ +/target diff --git a/bfox/Cargo.toml b/bfox/Cargo.toml new file mode 100644 index 0000000..ffc6f2d --- /dev/null +++ b/bfox/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "bfox" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.86" +bfox-asmgen = { path = "../bfox-asmgen" } +bfox-parser = { path = "../bfox-parser" } +camino = "1.1.7" +clap = { version = "4.5.4", features = ["derive"] } +env_logger = "0.11.3" +log = "0.4.21" diff --git a/bfox/src/cli.rs b/bfox/src/cli.rs new file mode 100644 index 0000000..4d7fbcb --- /dev/null +++ b/bfox/src/cli.rs @@ -0,0 +1,16 @@ +use clap::Parser; + + +#[derive(Debug, Parser)] +pub(crate) struct CliArgs { + /// Only generate assembly + #[arg(long, short)] + pub assemly: bool, + + /// Output file + #[arg(long, short, default_value="./a.out")] + pub output: camino::Utf8PathBuf, + + /// Input code + pub input: camino::Utf8PathBuf +} \ No newline at end of file diff --git a/bfox/src/main.rs b/bfox/src/main.rs new file mode 100644 index 0000000..3a5c287 --- /dev/null +++ b/bfox/src/main.rs @@ -0,0 +1,29 @@ +use clap::Parser; + +mod cli; + +fn main() -> anyhow::Result<()> { + let cli = cli::CliArgs::parse(); + + std::env::set_var("RUST_LOG", "debug"); + env_logger::init(); + + let mut parser = bfox_parser::Parser::new(); + + let Ok(input_code) = std::fs::read_to_string(&cli.input) else { + log::error!("Couldnt read input file: {}", cli.input); + return Ok(()); + }; + + parser.parse(input_code, cli.input.to_string())?; + + let tokens = parser.tokens(); + + log::debug!("{tokens:?}"); + + if !cli.assemly { + //TODO: Compile here + } + + Ok(()) +} diff --git a/hello_word.bf b/hello_word.bf new file mode 100644 index 0000000..ea2b641 --- /dev/null +++ b/hello_word.bf @@ -0,0 +1 @@ +++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++. \ No newline at end of file