Update to use new parser's enum for operators.

This commit is contained in:
abbie 2024-08-21 21:58:16 +01:00
parent 90aaeb8adb
commit d36d8da0a7
Signed by: threeoh6000
GPG key ID: 801FE4AD456E922C
3 changed files with 23 additions and 23 deletions

2
Cargo.lock generated
View file

@ -120,7 +120,7 @@ checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]] [[package]]
name = "parsegranite" name = "parsegranite"
version = "1.1.0-develop" version = "1.1.0-develop"
source = "git+https://git.colean.cc/threeoh6000/parsegranite?rev=5eaca18#5eaca1898f64ea890092a3a33e8f3c1655eab7e3" source = "git+https://git.colean.cc/threeoh6000/parsegranite?rev=1a345d4#1a345d469398afdbfa0ab93608279ea802455c46"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"

View file

@ -5,4 +5,4 @@ edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.5.15", features = ["derive"] } clap = { version = "4.5.15", features = ["derive"] }
parsegranite = { git = "https://git.colean.cc/threeoh6000/parsegranite", rev = "5eaca18", version = "1.1.0-develop" } parsegranite = { git = "https://git.colean.cc/threeoh6000/parsegranite", rev = "1a345d4", version = "1.1.0-develop" }

View file

@ -4,7 +4,7 @@ use std::process::exit;
use std::io; use std::io;
use std::io::Write; use std::io::Write;
use std::fs; use std::fs;
use parsegranite::{parse, Program}; use parsegranite::{parse, Program, Operator};
/// A toy programming language runtime. /// A toy programming language runtime.
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -44,9 +44,9 @@ fn execute(program: Program) {
loop { loop {
if current_statement >= statements.len() { break; } if current_statement >= statements.len() { break; }
let statement = &statements[current_statement]; let statement = &statements[current_statement];
if statement.operator == ">" { if statement.operator == Operator::SetVariable {
vars.insert(statement.arguments[1].clone(), statement.arguments[0].clone()); vars.insert(statement.arguments[1].clone(), statement.arguments[0].clone());
} else if statement.operator == "^" { } else if statement.operator == Operator::Increment {
if vars.contains_key(&statement.arguments[0]) { if vars.contains_key(&statement.arguments[0]) {
let value = vars.get(&statement.arguments[0]).unwrap(); let value = vars.get(&statement.arguments[0]).unwrap();
let binding = value.parse::<i64>(); let binding = value.parse::<i64>();
@ -62,7 +62,7 @@ fn execute(program: Program) {
println!("Missing variable {}.", statement.arguments[0]); println!("Missing variable {}.", statement.arguments[0]);
exit(1); exit(1);
} }
} else if statement.operator == "v" { } else if statement.operator == Operator::Decrement {
if vars.contains_key(&statement.arguments[0]) { if vars.contains_key(&statement.arguments[0]) {
let value = vars.get(&statement.arguments[0]).unwrap(); let value = vars.get(&statement.arguments[0]).unwrap();
let binding = value.parse::<i64>(); let binding = value.parse::<i64>();
@ -78,7 +78,7 @@ fn execute(program: Program) {
println!("Missing variable {}.", statement.arguments[0]); println!("Missing variable {}.", statement.arguments[0]);
exit(1); exit(1);
} }
} else if statement.operator == "!" { } else if statement.operator == Operator::Print {
if vars.contains_key(&statement.arguments[0]) { if vars.contains_key(&statement.arguments[0]) {
let value = vars.get(&statement.arguments[0]).unwrap(); let value = vars.get(&statement.arguments[0]).unwrap();
println!("{}", value); println!("{}", value);
@ -86,7 +86,7 @@ fn execute(program: Program) {
println!("Missing variable {}.", statement.arguments[0]); println!("Missing variable {}.", statement.arguments[0]);
exit(1); exit(1);
} }
} else if statement.operator == "*" { } else if statement.operator == Operator::StringConcatenation {
if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) { if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) {
let left = vars.get(&statement.arguments[0]).unwrap(); let left = vars.get(&statement.arguments[0]).unwrap();
let right = vars.get(&statement.arguments[1]).unwrap(); let right = vars.get(&statement.arguments[1]).unwrap();
@ -95,7 +95,7 @@ fn execute(program: Program) {
println!("Missing variable."); println!("Missing variable.");
exit(1); exit(1);
} }
} else if statement.operator == "+" { } else if statement.operator == Operator::JumpIfGreaterThan {
if vars.contains_key(&statement.arguments[1]) && labels.contains_key(&statement.arguments[0]) { if vars.contains_key(&statement.arguments[1]) && labels.contains_key(&statement.arguments[0]) {
let binding; let binding;
if vars.contains_key(&statement.arguments[2]) { if vars.contains_key(&statement.arguments[2]) {
@ -119,7 +119,7 @@ fn execute(program: Program) {
println!("Missing variable or label."); println!("Missing variable or label.");
exit(1); exit(1);
} }
} else if statement.operator == "-" { } else if statement.operator == Operator::JumpIfLessThan {
if vars.contains_key(&statement.arguments[1]) && labels.contains_key(&statement.arguments[0]) { if vars.contains_key(&statement.arguments[1]) && labels.contains_key(&statement.arguments[0]) {
let binding; let binding;
if vars.contains_key(&statement.arguments[2]) { if vars.contains_key(&statement.arguments[2]) {
@ -144,7 +144,7 @@ fn execute(program: Program) {
println!("Missing variable or label."); println!("Missing variable or label.");
exit(1); exit(1);
} }
} else if statement.operator == "=" { } else if statement.operator == Operator::JumpIfEqualTo {
if vars.contains_key(&statement.arguments[1]) && labels.contains_key(&statement.arguments[0]) { if vars.contains_key(&statement.arguments[1]) && labels.contains_key(&statement.arguments[0]) {
let binding; let binding;
if vars.contains_key(&statement.arguments[2]) { if vars.contains_key(&statement.arguments[2]) {
@ -168,7 +168,7 @@ fn execute(program: Program) {
println!("Missing variable or label."); println!("Missing variable or label.");
exit(1); exit(1);
} }
} else if statement.operator == "<" { } else if statement.operator == Operator::UnconditionalJump {
if labels.contains_key(&statement.arguments[0]) { if labels.contains_key(&statement.arguments[0]) {
let label_loc = labels.get(&statement.arguments[0]).unwrap(); let label_loc = labels.get(&statement.arguments[0]).unwrap();
current_statement = label_loc.clone(); current_statement = label_loc.clone();
@ -176,7 +176,7 @@ fn execute(program: Program) {
println!("Numerical type issues."); println!("Numerical type issues.");
exit(1); exit(1);
} }
} else if statement.operator == "a" { } else if statement.operator == Operator::ArithmeticAdd {
if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) { if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) {
let value = vars.get(&statement.arguments[0]).unwrap(); let value = vars.get(&statement.arguments[0]).unwrap();
let value1 = vars.get(&statement.arguments[1]).unwrap(); let value1 = vars.get(&statement.arguments[1]).unwrap();
@ -193,7 +193,7 @@ fn execute(program: Program) {
println!("Missing variables."); println!("Missing variables.");
exit(1); exit(1);
} }
} else if statement.operator == "s" { } else if statement.operator == Operator::ArithmeticSubtract {
if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) { if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) {
let value = vars.get(&statement.arguments[0]).unwrap(); let value = vars.get(&statement.arguments[0]).unwrap();
let value1 = vars.get(&statement.arguments[1]).unwrap(); let value1 = vars.get(&statement.arguments[1]).unwrap();
@ -210,7 +210,7 @@ fn execute(program: Program) {
println!("Missing variables."); println!("Missing variables.");
exit(1); exit(1);
} }
} else if statement.operator == "m" { } else if statement.operator == Operator::ArithmeticMultiply {
if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) { if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) {
let value = vars.get(&statement.arguments[0]).unwrap(); let value = vars.get(&statement.arguments[0]).unwrap();
let value1 = vars.get(&statement.arguments[1]).unwrap(); let value1 = vars.get(&statement.arguments[1]).unwrap();
@ -227,7 +227,7 @@ fn execute(program: Program) {
println!("Missing variables."); println!("Missing variables.");
exit(1); exit(1);
} }
} else if statement.operator == "d" { } else if statement.operator == Operator::ArithmeticDivide {
if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) { if vars.contains_key(&statement.arguments[0]) && vars.contains_key(&statement.arguments[1]) {
let value = vars.get(&statement.arguments[0]).unwrap(); let value = vars.get(&statement.arguments[0]).unwrap();
let value1 = vars.get(&statement.arguments[1]).unwrap(); let value1 = vars.get(&statement.arguments[1]).unwrap();
@ -244,9 +244,9 @@ fn execute(program: Program) {
println!("Missing variables."); println!("Missing variables.");
exit(1); exit(1);
} }
} else if statement.operator == "~" { } else if statement.operator == Operator::ExplicitExit {
exit(0); exit(0);
} else if statement.operator == "?" { } else if statement.operator == Operator::TakeInput {
if vars.contains_key(&statement.arguments[0]) { if vars.contains_key(&statement.arguments[0]) {
let value = vars.get(&statement.arguments[0]).unwrap(); let value = vars.get(&statement.arguments[0]).unwrap();
print!("{}", value); print!("{}", value);
@ -257,7 +257,7 @@ fn execute(program: Program) {
let mut input = String::new(); let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line!"); io::stdin().read_line(&mut input).expect("Failed to read line!");
vars.insert(statement.arguments[0].clone(), input.trim().to_string()); vars.insert(statement.arguments[0].clone(), input.trim().to_string());
} else if statement.operator == "&" { } else if statement.operator == Operator::TypeCheck {
if vars.contains_key(&statement.arguments[0]) { if vars.contains_key(&statement.arguments[0]) {
let value = vars.get(&statement.arguments[0]).unwrap(); let value = vars.get(&statement.arguments[0]).unwrap();
let binding = value.parse::<i64>(); let binding = value.parse::<i64>();
@ -269,7 +269,7 @@ fn execute(program: Program) {
} else { } else {
vars.insert(statement.arguments[1].clone(), "-1".to_string()); vars.insert(statement.arguments[1].clone(), "-1".to_string());
} }
} else if statement.operator == "%" { } else if statement.operator == Operator::VariableClone {
if vars.contains_key(&statement.arguments[0]) { if vars.contains_key(&statement.arguments[0]) {
let value = vars.get(&statement.arguments[0]).unwrap(); let value = vars.get(&statement.arguments[0]).unwrap();
vars.insert(statement.arguments[1].clone(), value.clone()); vars.insert(statement.arguments[1].clone(), value.clone());
@ -277,11 +277,11 @@ fn execute(program: Program) {
println!("Missing variable on clone."); println!("Missing variable on clone.");
exit(1); exit(1);
} }
} else if statement.operator == "$" { } else if statement.operator == Operator::VariableDrop {
vars.remove(&statement.arguments[0]); vars.remove(&statement.arguments[0]);
} else if statement.operator == "#" { } else if statement.operator == Operator::StackPush {
stack.push(current_statement); stack.push(current_statement);
} else if statement.operator == "|" { } else if statement.operator == Operator::Return {
current_statement = stack.pop().expect("Stack underflow."); current_statement = stack.pop().expect("Stack underflow.");
current_statement = current_statement + 1; current_statement = current_statement + 1;
} }