From d36d8da0a7ce4593b9ddfacd340786162ba9dff6 Mon Sep 17 00:00:00 2001 From: threeoh6000 Date: Wed, 21 Aug 2024 21:58:16 +0100 Subject: [PATCH] Update to use new parser's enum for operators. --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 42 +++++++++++++++++++++--------------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 54b805b..c11908b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -120,7 +120,7 @@ checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "parsegranite" 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]] name = "proc-macro2" diff --git a/Cargo.toml b/Cargo.toml index 4797c7b..d1fb921 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,4 @@ edition = "2021" [dependencies] 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" } diff --git a/src/main.rs b/src/main.rs index 029b555..ed1b760 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use std::process::exit; use std::io; use std::io::Write; use std::fs; -use parsegranite::{parse, Program}; +use parsegranite::{parse, Program, Operator}; /// A toy programming language runtime. #[derive(Parser, Debug)] @@ -44,9 +44,9 @@ fn execute(program: Program) { loop { if current_statement >= statements.len() { break; } let statement = &statements[current_statement]; - if statement.operator == ">" { + if statement.operator == Operator::SetVariable { 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]) { let value = vars.get(&statement.arguments[0]).unwrap(); let binding = value.parse::(); @@ -62,7 +62,7 @@ fn execute(program: Program) { println!("Missing variable {}.", statement.arguments[0]); exit(1); } - } else if statement.operator == "v" { + } else if statement.operator == Operator::Decrement { if vars.contains_key(&statement.arguments[0]) { let value = vars.get(&statement.arguments[0]).unwrap(); let binding = value.parse::(); @@ -78,7 +78,7 @@ fn execute(program: Program) { println!("Missing variable {}.", statement.arguments[0]); exit(1); } - } else if statement.operator == "!" { + } else if statement.operator == Operator::Print { if vars.contains_key(&statement.arguments[0]) { let value = vars.get(&statement.arguments[0]).unwrap(); println!("{}", value); @@ -86,7 +86,7 @@ fn execute(program: Program) { println!("Missing variable {}.", statement.arguments[0]); 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]) { let left = vars.get(&statement.arguments[0]).unwrap(); let right = vars.get(&statement.arguments[1]).unwrap(); @@ -95,7 +95,7 @@ fn execute(program: Program) { println!("Missing variable."); 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]) { let binding; if vars.contains_key(&statement.arguments[2]) { @@ -119,7 +119,7 @@ fn execute(program: Program) { println!("Missing variable or label."); 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]) { let binding; if vars.contains_key(&statement.arguments[2]) { @@ -144,7 +144,7 @@ fn execute(program: Program) { println!("Missing variable or label."); 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]) { let binding; if vars.contains_key(&statement.arguments[2]) { @@ -168,7 +168,7 @@ fn execute(program: Program) { println!("Missing variable or label."); exit(1); } - } else if statement.operator == "<" { + } else if statement.operator == Operator::UnconditionalJump { if labels.contains_key(&statement.arguments[0]) { let label_loc = labels.get(&statement.arguments[0]).unwrap(); current_statement = label_loc.clone(); @@ -176,7 +176,7 @@ fn execute(program: Program) { println!("Numerical type issues."); 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]) { let value = vars.get(&statement.arguments[0]).unwrap(); let value1 = vars.get(&statement.arguments[1]).unwrap(); @@ -193,7 +193,7 @@ fn execute(program: Program) { println!("Missing variables."); 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]) { let value = vars.get(&statement.arguments[0]).unwrap(); let value1 = vars.get(&statement.arguments[1]).unwrap(); @@ -210,7 +210,7 @@ fn execute(program: Program) { println!("Missing variables."); 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]) { let value = vars.get(&statement.arguments[0]).unwrap(); let value1 = vars.get(&statement.arguments[1]).unwrap(); @@ -227,7 +227,7 @@ fn execute(program: Program) { println!("Missing variables."); 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]) { let value = vars.get(&statement.arguments[0]).unwrap(); let value1 = vars.get(&statement.arguments[1]).unwrap(); @@ -244,9 +244,9 @@ fn execute(program: Program) { println!("Missing variables."); exit(1); } - } else if statement.operator == "~" { + } else if statement.operator == Operator::ExplicitExit { exit(0); - } else if statement.operator == "?" { + } else if statement.operator == Operator::TakeInput { if vars.contains_key(&statement.arguments[0]) { let value = vars.get(&statement.arguments[0]).unwrap(); print!("{}", value); @@ -257,7 +257,7 @@ fn execute(program: Program) { 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 == "&" { + } else if statement.operator == Operator::TypeCheck { if vars.contains_key(&statement.arguments[0]) { let value = vars.get(&statement.arguments[0]).unwrap(); let binding = value.parse::(); @@ -269,7 +269,7 @@ fn execute(program: Program) { } else { 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]) { let value = vars.get(&statement.arguments[0]).unwrap(); vars.insert(statement.arguments[1].clone(), value.clone()); @@ -277,11 +277,11 @@ fn execute(program: Program) { println!("Missing variable on clone."); exit(1); } - } else if statement.operator == "$" { + } else if statement.operator == Operator::VariableDrop { vars.remove(&statement.arguments[0]); - } else if statement.operator == "#" { + } else if statement.operator == Operator::StackPush { stack.push(current_statement); - } else if statement.operator == "|" { + } else if statement.operator == Operator::Return { current_statement = stack.pop().expect("Stack underflow."); current_statement = current_statement + 1; }