diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Barebone.txt b/Barebone.txt new file mode 100644 index 0000000..2f5cb82 --- /dev/null +++ b/Barebone.txt @@ -0,0 +1,21 @@ +clear X; +incr X; +incr X; +clear Y; +incr Y; +incr Y; +incr Y; +clear Z; +while X not 0 do; + clear W; + while Y not 0 do; + incr Z; + incr W; + decr Y; + end; + while W not 0 do; + incr Y; + decr W; + end; + decr X; +end; \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..973b9d2 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ECS-BareBones" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..eabb9fc --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ECS-BareBones" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..8b4eb2d --- /dev/null +++ b/src/main.rs @@ -0,0 +1,80 @@ +use core::panic; +use std::{collections::HashMap, fs::File, io::{BufRead, BufReader}, path::Path}; + +struct Interpreter { + memory: HashMap, +} + +impl Interpreter { + fn new() -> Interpreter { + Interpreter { + memory: HashMap::new(), + } + } + + fn execute(&mut self, lines: &[String], start: usize) -> usize { + let mut i = start; + while i < lines.len() { + println!("{:?}", self.memory); + let line = lines[i].trim(); + let tokens: Vec<&str> = line.split_whitespace().collect(); + match tokens[0] { + "clear" => { + self.memory.insert(tokens[1].trim_end_matches(';').to_owned(), 0); + } + "incr" => { + let counter = self.memory.entry(tokens[1].trim_end_matches(';').to_owned()).or_insert(0); + *counter += 1; + } + "decr" => { + let counter = self.memory.entry(tokens[1].trim_end_matches(';').to_owned()).or_insert(0); + *counter -= 1; + } + "while" => { + let mut loop_end = i + 1; + let mut depth = 1; + while depth > 0 && loop_end < lines.len() { + let loop_line = lines[loop_end].trim(); + if loop_line.starts_with("while") { + depth += 1; + } else if loop_line.trim_end_matches(';') == "end" { + depth -= 1; + } + loop_end += 1; + } + if depth != 0 { + panic!("Unmatched while loop at line {}", i + 1); + } + while *self.memory.entry(tokens[1].to_owned()).or_insert(0) != 0 { + self.execute(lines, i + 1); + } + i = loop_end - 1; // Skip to the end of the loop + } + "end;" => { + return i; // Return from the current loop or function + } + _ => { + if !line.is_empty() { + panic!("Unknown command: {}", tokens[0]); + } + } + } + i += 1; + } + i + } +} + +fn main() { + let path = Path::new("Barebone.txt"); + if !path.exists() { + panic!("Path does not exist"); + } + + let file = File::open(path).unwrap(); + let reader = BufReader::new(file); + let lines: Vec = reader.lines().map(|l| l.unwrap()).collect(); + + let mut interpreter = Interpreter::new(); + interpreter.execute(&lines, 0); +} \ No newline at end of file