update crap lol what
This commit is contained in:
parent
a464957606
commit
6807806a54
3 changed files with 635 additions and 3 deletions
237
Cargo.lock
generated
Normal file
237
Cargo.lock
generated
Normal file
|
@ -0,0 +1,237 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstream"
|
||||||
|
version = "0.6.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
"anstyle-parse",
|
||||||
|
"anstyle-query",
|
||||||
|
"anstyle-wincon",
|
||||||
|
"colorchoice",
|
||||||
|
"is_terminal_polyfill",
|
||||||
|
"utf8parse",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle"
|
||||||
|
version = "1.0.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-parse"
|
||||||
|
version = "0.2.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb"
|
||||||
|
dependencies = [
|
||||||
|
"utf8parse",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-query"
|
||||||
|
version = "1.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a"
|
||||||
|
dependencies = [
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-wincon"
|
||||||
|
version = "3.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap"
|
||||||
|
version = "4.5.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "11d8838454fda655dafd3accb2b6e2bea645b9e4078abe84a22ceb947235c5cc"
|
||||||
|
dependencies = [
|
||||||
|
"clap_builder",
|
||||||
|
"clap_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_builder"
|
||||||
|
version = "4.5.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "216aec2b177652e3846684cbfe25c9964d18ec45234f0f5da5157b207ed1aab6"
|
||||||
|
dependencies = [
|
||||||
|
"anstream",
|
||||||
|
"anstyle",
|
||||||
|
"clap_lex",
|
||||||
|
"strsim",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_derive"
|
||||||
|
version = "4.5.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0"
|
||||||
|
dependencies = [
|
||||||
|
"heck",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_lex"
|
||||||
|
version = "0.7.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "colorchoice"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "granite"
|
||||||
|
version = "1.0.0"
|
||||||
|
dependencies = [
|
||||||
|
"clap",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "heck"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "is_terminal_polyfill"
|
||||||
|
version = "1.70.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.86"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
|
||||||
|
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 = "strsim"
|
||||||
|
version = "0.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "2.0.74"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1fceb41e3d546d0bd83421d3409b1460cc7444cd389341a4c880fe7a042cb3d7"
|
||||||
|
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.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||||
|
|
||||||
|
[[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.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||||
|
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.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_msvc"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnu"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnullvm"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_msvc"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnu"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnullvm"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_msvc"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
|
@ -1,6 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "granite"
|
name = "granite"
|
||||||
version = "0.1.0"
|
version = "1.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = { version = "4.5.15", features = ["derive"] }
|
||||||
|
|
398
src/main.rs
398
src/main.rs
|
@ -1,3 +1,397 @@
|
||||||
fn main() {
|
use clap::Parser;
|
||||||
println!("Hello, world!");
|
use std::collections::HashMap;
|
||||||
|
use std::process::exit;
|
||||||
|
use std::io;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Statement {
|
||||||
|
operator: String,
|
||||||
|
arguments: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Program {
|
||||||
|
statements: Vec<Statement>,
|
||||||
|
labels: HashMap<String, usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A toy programming language runtime.
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
#[command(version, about, long_about = None)]
|
||||||
|
struct Args {
|
||||||
|
/// Program to execute.
|
||||||
|
#[arg(short, long)]
|
||||||
|
input: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args = Args::parse();
|
||||||
|
run(args.input);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(name: String) {
|
||||||
|
let program = fs::read_to_string(name).expect("Program read failed");
|
||||||
|
let parsed_program = parse(&program);
|
||||||
|
if parsed_program.is_ok() {
|
||||||
|
execute(parsed_program.unwrap());
|
||||||
|
} else {
|
||||||
|
let err = parsed_program.err();
|
||||||
|
println!("An error has occured during parsing.");
|
||||||
|
if err.is_some() {
|
||||||
|
println!("{}", err.unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn execute(program: Program) {
|
||||||
|
let labels = program.labels;
|
||||||
|
let statements = program.statements;
|
||||||
|
let mut vars: HashMap<String, String> = HashMap::new();
|
||||||
|
let mut current_statement: usize = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if current_statement >= statements.len() { break; }
|
||||||
|
let statement = &statements[current_statement];
|
||||||
|
if statement.operator == ">" {
|
||||||
|
vars.insert(statement.arguments[1].clone(), statement.arguments[0].clone());
|
||||||
|
} else if statement.operator == "^" {
|
||||||
|
if vars.contains_key(&statement.arguments[0]) {
|
||||||
|
let value = vars.get(&statement.arguments[0]).unwrap();
|
||||||
|
let binding = value.parse::<i64>();
|
||||||
|
if binding.is_ok() {
|
||||||
|
let mut x = binding.unwrap();
|
||||||
|
x = x + 1;
|
||||||
|
vars.insert(statement.arguments[0].clone(), x.to_string());
|
||||||
|
} else {
|
||||||
|
println!("Non-number variable {} on increment.", statement.arguments[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Missing variable {}.", statement.arguments[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "v" {
|
||||||
|
if vars.contains_key(&statement.arguments[0]) {
|
||||||
|
let value = vars.get(&statement.arguments[0]).unwrap();
|
||||||
|
let binding = value.parse::<i64>();
|
||||||
|
if binding.is_ok() {
|
||||||
|
let mut x = binding.unwrap();
|
||||||
|
x = x - 1;
|
||||||
|
vars.insert(statement.arguments[0].clone(), x.to_string());
|
||||||
|
} else {
|
||||||
|
println!("Non-number variable {} on increment.", statement.arguments[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Missing variable {}.", statement.arguments[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "!" {
|
||||||
|
if vars.contains_key(&statement.arguments[0]) {
|
||||||
|
let value = vars.get(&statement.arguments[0]).unwrap();
|
||||||
|
println!("{}", value);
|
||||||
|
} else {
|
||||||
|
println!("Missing variable {}.", statement.arguments[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "*" {
|
||||||
|
if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) {
|
||||||
|
let left = vars.get(&statement.arguments[0]).unwrap();
|
||||||
|
let right = vars.get(&statement.arguments[1]).unwrap();
|
||||||
|
vars.insert(statement.arguments[2].clone(), format!("{}{}", left, right));
|
||||||
|
} else {
|
||||||
|
println!("Missing variable.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "+" {
|
||||||
|
if vars.contains_key(&statement.arguments[1]) && labels.contains_key(&statement.arguments[0]) {
|
||||||
|
let binding;
|
||||||
|
if vars.contains_key(&statement.arguments[2]) {
|
||||||
|
let tempvar = vars.get(&statement.arguments[2]).unwrap();
|
||||||
|
binding = tempvar.parse::<i64>();
|
||||||
|
} else {
|
||||||
|
binding = statement.arguments[2].clone().parse::<i64>();
|
||||||
|
}
|
||||||
|
let label_loc = labels.get(&statement.arguments[0]).unwrap();
|
||||||
|
let var = vars.get(&statement.arguments[1]).unwrap();
|
||||||
|
let binding2 = var.parse::<i64>();
|
||||||
|
if binding.is_ok() && binding2.is_ok() {
|
||||||
|
if binding2.unwrap() > binding.unwrap() {
|
||||||
|
current_statement = label_loc.clone();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Numerical type issues.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Missing variable or label.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "-" {
|
||||||
|
if vars.contains_key(&statement.arguments[1]) && labels.contains_key(&statement.arguments[0]) {
|
||||||
|
let binding;
|
||||||
|
if vars.contains_key(&statement.arguments[2]) {
|
||||||
|
let tempvar = vars.get(&statement.arguments[2]).unwrap();
|
||||||
|
binding = tempvar.parse::<i64>();
|
||||||
|
} else {
|
||||||
|
binding = statement.arguments[2].clone().parse::<i64>();
|
||||||
|
}
|
||||||
|
let label_loc = labels.get(&statement.arguments[0]).unwrap();
|
||||||
|
let var = vars.get(&statement.arguments[1]).unwrap();
|
||||||
|
let binding2 = var.parse::<i64>();
|
||||||
|
|
||||||
|
if binding.is_ok() && binding2.is_ok() {
|
||||||
|
if binding2.unwrap() < binding.unwrap() {
|
||||||
|
current_statement = label_loc.clone();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Numerical type issues.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Missing variable or label.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "=" {
|
||||||
|
if vars.contains_key(&statement.arguments[1]) && labels.contains_key(&statement.arguments[0]) {
|
||||||
|
let binding;
|
||||||
|
if vars.contains_key(&statement.arguments[2]) {
|
||||||
|
let tempvar = vars.get(&statement.arguments[2]).unwrap();
|
||||||
|
binding = tempvar.parse::<i64>();
|
||||||
|
} else {
|
||||||
|
binding = statement.arguments[2].clone().parse::<i64>();
|
||||||
|
}
|
||||||
|
let label_loc = labels.get(&statement.arguments[0]).unwrap();
|
||||||
|
let var = vars.get(&statement.arguments[1]).unwrap();
|
||||||
|
let binding2 = var.parse::<i64>();
|
||||||
|
if binding.is_ok() && binding2.is_ok() {
|
||||||
|
if binding.unwrap() == binding2.unwrap() {
|
||||||
|
current_statement = label_loc.clone();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Numerical type issues.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Missing variable or label.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "<" {
|
||||||
|
if labels.contains_key(&statement.arguments[0]) {
|
||||||
|
let label_loc = labels.get(&statement.arguments[0]).unwrap();
|
||||||
|
current_statement = label_loc.clone();
|
||||||
|
} else {
|
||||||
|
println!("Numerical type issues.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "a" {
|
||||||
|
if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) {
|
||||||
|
let value = vars.get(&statement.arguments[0]).unwrap();
|
||||||
|
let value1 = vars.get(&statement.arguments[1]).unwrap();
|
||||||
|
let binding = value.parse::<i64>();
|
||||||
|
let binding1 = value1.parse::<i64>();
|
||||||
|
if binding.is_ok() {
|
||||||
|
let x = binding.unwrap() + binding1.unwrap();
|
||||||
|
vars.insert(statement.arguments[2].clone(), x.to_string());
|
||||||
|
} else {
|
||||||
|
println!("Non-number variables on math operation.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Missing variables.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "s" {
|
||||||
|
if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) {
|
||||||
|
let value = vars.get(&statement.arguments[0]).unwrap();
|
||||||
|
let value1 = vars.get(&statement.arguments[1]).unwrap();
|
||||||
|
let binding = value.parse::<i64>();
|
||||||
|
let binding1 = value1.parse::<i64>();
|
||||||
|
if binding.is_ok() {
|
||||||
|
let x = binding.unwrap() - binding1.unwrap();
|
||||||
|
vars.insert(statement.arguments[2].clone(), x.to_string());
|
||||||
|
} else {
|
||||||
|
println!("Non-number variables on math operation.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Missing variables.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "m" {
|
||||||
|
if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) {
|
||||||
|
let value = vars.get(&statement.arguments[0]).unwrap();
|
||||||
|
let value1 = vars.get(&statement.arguments[1]).unwrap();
|
||||||
|
let binding = value.parse::<i64>();
|
||||||
|
let binding1 = value1.parse::<i64>();
|
||||||
|
if binding.is_ok() {
|
||||||
|
let x = binding.unwrap() * binding1.unwrap();
|
||||||
|
vars.insert(statement.arguments[2].clone(), x.to_string());
|
||||||
|
} else {
|
||||||
|
println!("Non-number variables on math operation.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Missing variables.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "d" {
|
||||||
|
if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) {
|
||||||
|
let value = vars.get(&statement.arguments[0]).unwrap();
|
||||||
|
let value1 = vars.get(&statement.arguments[1]).unwrap();
|
||||||
|
let binding = value.parse::<i64>();
|
||||||
|
let binding1 = value1.parse::<i64>();
|
||||||
|
if binding.is_ok() {
|
||||||
|
let x = binding.unwrap() / binding1.unwrap();
|
||||||
|
vars.insert(statement.arguments[2].clone(), x.to_string());
|
||||||
|
} else {
|
||||||
|
println!("Non-number variables on math operation.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Missing variables.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "~" {
|
||||||
|
exit(0);
|
||||||
|
} else if statement.operator == "?" {
|
||||||
|
if vars.contains_key(&statement.arguments[0]) {
|
||||||
|
let value = vars.get(&statement.arguments[0]).unwrap();
|
||||||
|
print!("{}", value);
|
||||||
|
} else {
|
||||||
|
print!("? ");
|
||||||
|
}
|
||||||
|
let _ = io::stdout().flush();
|
||||||
|
let mut input = String::new();
|
||||||
|
io::stdin().read_line(&mut input).expect("Failed to read line!");
|
||||||
|
vars.insert(statement.arguments[0].clone(), input.trim().to_string());
|
||||||
|
} else if statement.operator == "&" {
|
||||||
|
if vars.contains_key(&statement.arguments[0]) {
|
||||||
|
let value = vars.get(&statement.arguments[0]).unwrap();
|
||||||
|
let binding = value.parse::<i64>();
|
||||||
|
if binding.is_ok() {
|
||||||
|
vars.insert(statement.arguments[1].clone(), "1".to_string());
|
||||||
|
} else {
|
||||||
|
vars.insert(statement.arguments[1].clone(), "0".to_string());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Missing variable on int check.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if statement.operator == "$" {
|
||||||
|
vars.remove(&statement.arguments[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
current_statement = current_statement + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(data: &str) -> Result<Program, String> {
|
||||||
|
let mut program_begun = false;
|
||||||
|
let mut operator: Option<String> = None;
|
||||||
|
let mut arguments: Vec<String> = vec![];
|
||||||
|
let mut expected_arguments: i32 = -1;
|
||||||
|
let mut argument_builder: String = "".to_string();
|
||||||
|
let mut statements: Vec<Statement> = vec![];
|
||||||
|
let mut labels: HashMap<String, usize> = HashMap::new();
|
||||||
|
|
||||||
|
let mut inside_string = false;
|
||||||
|
let mut string_escape = false;
|
||||||
|
|
||||||
|
for char in data.chars() {
|
||||||
|
if !program_begun && char == ':' {
|
||||||
|
program_begun = true;
|
||||||
|
} else if program_begun {
|
||||||
|
if operator.is_none() {
|
||||||
|
let potential_argument_length = operator_to_arglength(char.clone());
|
||||||
|
if potential_argument_length.is_none() { return Err(format!("Invalid operator {}.", char)); }
|
||||||
|
expected_arguments = potential_argument_length.unwrap();
|
||||||
|
operator = Some(char.to_string());
|
||||||
|
} else if expected_arguments > 0 {
|
||||||
|
if inside_string {
|
||||||
|
if char == '"' && !string_escape {
|
||||||
|
inside_string = false;
|
||||||
|
} else {
|
||||||
|
if char == '\\' {
|
||||||
|
if !string_escape { string_escape = true; }
|
||||||
|
else { argument_builder = format!("{}{}", argument_builder, char); }
|
||||||
|
} else {
|
||||||
|
argument_builder = format!("{}{}", argument_builder, char);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if char == ',' {
|
||||||
|
expected_arguments = expected_arguments - 1;
|
||||||
|
arguments.push(argument_builder);
|
||||||
|
argument_builder = "".to_string();
|
||||||
|
} else if char == ':' {
|
||||||
|
expected_arguments = expected_arguments - 1;
|
||||||
|
arguments.push(argument_builder);
|
||||||
|
argument_builder = "".to_string();
|
||||||
|
if expected_arguments != 0 {
|
||||||
|
return Err(format!("Invalid amount of arguments for operator {}.", operator.unwrap()));
|
||||||
|
}
|
||||||
|
} else if char == '"' {
|
||||||
|
if argument_builder.len() != 0 { return Err("Cannot start string where argument already exists.".to_string()); }
|
||||||
|
inside_string = true;
|
||||||
|
} else if char == '\n' && !inside_string {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
argument_builder = format!("{}{}", argument_builder, char);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if operator.is_some() && char == ':' {
|
||||||
|
let new_statement = Statement { operator: operator.clone().unwrap(), arguments: arguments.clone() };
|
||||||
|
statements.push(new_statement);
|
||||||
|
if operator.clone().unwrap() == "@" {
|
||||||
|
labels.insert(statements[statements.len()-1].arguments[0].clone(), statements.len()-1);
|
||||||
|
}
|
||||||
|
operator = None;
|
||||||
|
arguments = vec![];
|
||||||
|
argument_builder = "".to_string();
|
||||||
|
expected_arguments = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if argument_builder.len() != 0 {
|
||||||
|
arguments.push(argument_builder);
|
||||||
|
expected_arguments = expected_arguments - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if expected_arguments != 0 {
|
||||||
|
return Err(format!("Invalid amount of arguments for operator {}.", operator.unwrap()));
|
||||||
|
}
|
||||||
|
let new_statement = Statement { operator: operator.unwrap().clone(), arguments: arguments.clone() };
|
||||||
|
statements.push(new_statement);
|
||||||
|
|
||||||
|
let program = Program { statements: statements, labels: labels };
|
||||||
|
|
||||||
|
Ok(program)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn operator_to_arglength(operator: char) -> Option<i32> {
|
||||||
|
match operator {
|
||||||
|
'>' => Some(2),
|
||||||
|
'?' => Some(1),
|
||||||
|
'!' => Some(1),
|
||||||
|
'~' => Some(0),
|
||||||
|
'<' => Some(1),
|
||||||
|
'^' => Some(1),
|
||||||
|
'v' => Some(1),
|
||||||
|
'@' => Some(1),
|
||||||
|
'+' => Some(3),
|
||||||
|
'-' => Some(3),
|
||||||
|
'=' => Some(3),
|
||||||
|
'*' => Some(3),
|
||||||
|
'a' => Some(3),
|
||||||
|
's' => Some(3),
|
||||||
|
'm' => Some(3),
|
||||||
|
'd' => Some(3),
|
||||||
|
'0' => Some(1),
|
||||||
|
'&' => Some(2),
|
||||||
|
'$' => Some(1),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue