Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Reviem0 committed Oct 3, 2024
0 parents commit b4ef15c
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
21 changes: 21 additions & 0 deletions Barebone.txt
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "ECS-BareBones"
version = "0.1.0"
edition = "2021"

[dependencies]
80 changes: 80 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use core::panic;
use std::{collections::HashMap, fs::File, io::{BufRead, BufReader}, path::Path};

struct Interpreter {
memory: HashMap<String, i32>,
}

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<String> = reader.lines().map(|l| l.unwrap()).collect();

let mut interpreter = Interpreter::new();
interpreter.execute(&lines, 0);
}

0 comments on commit b4ef15c

Please sign in to comment.