diff --git a/SLS_Rust/sls/src/builtin.rs b/SLS_Rust/sls/src/builtin.rs index 2e67d40..37a3cfd 100644 --- a/SLS_Rust/sls/src/builtin.rs +++ b/SLS_Rust/sls/src/builtin.rs @@ -185,7 +185,8 @@ pub fn builtin_addition(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); let result = if type_flags.contains(NumericType::FLOAT) { numeric_to_token(b.float_val + a.float_val, 0, type_flags) @@ -202,7 +203,8 @@ pub fn builtin_subtraction(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); let result = if type_flags.contains(NumericType::FLOAT) { numeric_to_token(b.float_val - a.float_val, 0, type_flags) @@ -219,7 +221,8 @@ pub fn builtin_multiplication(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); let result = if type_flags.contains(NumericType::FLOAT) { numeric_to_token(b.float_val * a.float_val, 0, type_flags) @@ -240,7 +243,8 @@ pub fn builtin_division(state: &mut InterpreterState) -> bool { return false; } - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); state.stack.push(StackValue::F64(b.float_val / a.float_val)); true } @@ -254,7 +258,8 @@ pub fn builtin_modulus(state: &mut InterpreterState) -> bool { return false; } - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); let result = numeric_to_token(0.0, b.int_val % a.int_val, type_flags); state.push_token(&result); @@ -266,7 +271,8 @@ pub fn builtin_exponential(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); state.stack.push(StackValue::F64(b.float_val.powf(a.float_val))); true } @@ -276,7 +282,8 @@ pub fn builtin_greater_than(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); state.stack.push(StackValue::Boolean(b.float_val > a.float_val)); true } @@ -286,7 +293,8 @@ pub fn builtin_greater_than_or_equal_to(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); state.stack.push(StackValue::Boolean(b.float_val >= a.float_val)); true } @@ -296,7 +304,8 @@ pub fn builtin_less_than(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); state.stack.push(StackValue::Boolean(b.float_val < a.float_val)); true } @@ -306,7 +315,8 @@ pub fn builtin_less_than_or_equal_to(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); state.stack.push(StackValue::Boolean(b.float_val <= a.float_val)); true } @@ -316,7 +326,8 @@ pub fn builtin_equal_to(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); state.stack.push(StackValue::Boolean(b.float_val == a.float_val)); true } @@ -326,7 +337,8 @@ pub fn builtin_not_equal_to(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); state.stack.push(StackValue::Boolean(b.float_val != a.float_val)); true } @@ -336,9 +348,8 @@ pub fn builtin_abs(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; if !val.type_flags.contains(NumericType::SIGNED | NumericType::FLOAT) { return false; @@ -361,9 +372,8 @@ pub fn builtin_acos(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; if val.float_val < -1.0 || val.float_val > 1.0 { return false; @@ -379,7 +389,8 @@ pub fn builtin_and(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); let result = if type_flags.contains(NumericType::FLOAT) { b.float_val != 0.0 && a.float_val != 0.0 @@ -396,9 +407,8 @@ pub fn builtin_asin(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; if val.float_val < -1.0 || val.float_val > 1.0 { return false; @@ -414,9 +424,8 @@ pub fn builtin_atan(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; state.stack.pop(); state.stack.push(StackValue::F64(val.float_val.atan())); @@ -428,7 +437,8 @@ pub fn builtin_atan2(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); state.stack.push(StackValue::F64(b.float_val.atan2(a.float_val))); true } @@ -443,7 +453,8 @@ pub fn builtin_bitand(state: &mut InterpreterState) -> bool { return false; } - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); let result = numeric_to_token(0.0, b.int_val & a.int_val, type_flags); state.push_token(&result); true @@ -454,9 +465,8 @@ pub fn builtin_bitnot(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; if val.type_flags.contains(NumericType::FLOAT) { return false; @@ -477,7 +487,8 @@ pub fn builtin_bitor(state: &mut InterpreterState) -> bool { return false; } - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); let result = numeric_to_token(0.0, b.int_val | a.int_val, type_flags); state.push_token(&result); true @@ -492,7 +503,8 @@ pub fn builtin_bitxor(state: &mut InterpreterState) -> bool { return false; } - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); let result = numeric_to_token(0.0, b.int_val ^ a.int_val, type_flags); state.push_token(&result); true @@ -504,9 +516,8 @@ pub fn builtin_ceil(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; state.stack.pop(); state.stack.push(StackValue::F64(val.float_val.ceil())); @@ -518,9 +529,8 @@ pub fn builtin_floor(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; state.stack.pop(); state.stack.push(StackValue::F64(val.float_val.floor())); @@ -532,9 +542,8 @@ pub fn builtin_round(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; state.stack.pop(); state.stack.push(StackValue::F64(val.float_val.round())); @@ -546,9 +555,8 @@ pub fn builtin_sqrt(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; if val.float_val < 0.0 { return false; @@ -564,9 +572,8 @@ pub fn builtin_sin(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; state.stack.pop(); state.stack.push(StackValue::F64(val.float_val.sin())); @@ -578,9 +585,8 @@ pub fn builtin_cos(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; state.stack.pop(); state.stack.push(StackValue::F64(val.float_val.cos())); @@ -592,9 +598,8 @@ pub fn builtin_tan(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; state.stack.pop(); state.stack.push(StackValue::F64(val.float_val.tan())); @@ -606,9 +611,8 @@ pub fn builtin_ln(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; if val.float_val <= 0.0 { return false; @@ -624,9 +628,8 @@ pub fn builtin_log(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; if val.float_val <= 0.0 { return false; @@ -704,7 +707,8 @@ pub fn builtin_roll(state: &mut InterpreterState) -> bool { return false; } - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); // Rotate the top 'count' items 'times' times for _ in 0..times { @@ -732,7 +736,8 @@ pub fn builtin_or(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); let result = if type_flags.contains(NumericType::FLOAT) { b.float_val != 0.0 || a.float_val != 0.0 @@ -754,7 +759,8 @@ pub fn builtin_shl(state: &mut InterpreterState) -> bool { return false; } - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); let result = numeric_to_token(0.0, b.int_val << a.int_val, type_flags); state.push_token(&result); true @@ -769,7 +775,8 @@ pub fn builtin_shr(state: &mut InterpreterState) -> bool { return false; } - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); let result = numeric_to_token(0.0, b.int_val >> a.int_val, type_flags); state.push_token(&result); true @@ -788,9 +795,8 @@ pub fn builtin_seed(state: &mut InterpreterState) -> bool { return false; } - let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { - return false; - }; + let Some(last) = state.stack.last() else { return false }; + let Some(val) = extract_numeric_type(last) else { return false }; if val.type_flags.contains(NumericType::FLOAT) { return false; @@ -820,7 +826,9 @@ pub fn builtin_if(state: &mut InterpreterState) -> bool { let condition = is_truthy(&state.stack[state.stack.len()-3]); - state.stack.drain(0..3); + state.stack.pop(); + state.stack.pop(); + state.stack.pop(); if condition { state.execute_token_string(&then_block) @@ -844,7 +852,8 @@ pub fn builtin_while(state: &mut InterpreterState) -> bool { _ => return false, }; - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); loop { if !state.execute_token_string(&cond_block) { @@ -888,7 +897,9 @@ pub fn builtin_for(state: &mut InterpreterState) -> bool { return false; }; - state.stack.drain(0..3); + state.stack.pop(); + state.stack.pop(); + state.stack.pop(); for i in start..end { state.stack.push(StackValue::I64(i as i64)); @@ -954,7 +965,8 @@ pub fn builtin_const(state: &mut InterpreterState) -> bool { return false; } - state.stack.drain(0..2); + state.stack.pop(); + state.stack.pop(); state.functions.insert(name, FunctionItem::TokenString(value)); @@ -1093,7 +1105,7 @@ pub fn load_builtins(state: &mut InterpreterState) -> bool { break; } if !state.execute(&token) { - println!("Error in __builtin.sls"); + eprintln!("Error in __builtin.sls"); return false; } } diff --git a/SLS_Rust/sls/src/file.rs b/SLS_Rust/sls/src/file.rs index 4ee584d..5539174 100644 --- a/SLS_Rust/sls/src/file.rs +++ b/SLS_Rust/sls/src/file.rs @@ -29,7 +29,7 @@ pub fn exec_file(interpreter: &mut InterpreterState, filename: &str) -> bool { } LexResult::Err(err) => { - eprintln!("{}", err.message); + dbg!(err); false } } diff --git a/SLS_Rust/sls/src/interpreter.rs b/SLS_Rust/sls/src/interpreter.rs index 5c8e4e1..9c7bfc4 100644 --- a/SLS_Rust/sls/src/interpreter.rs +++ b/SLS_Rust/sls/src/interpreter.rs @@ -129,6 +129,6 @@ impl InterpreterState { } pub fn stack_top(&self) -> Option<&StackValue> { - self.stack.get(self.stack.len() - 1) + self.stack.last() } } diff --git a/SLS_Rust/sls/src/repl.rs b/SLS_Rust/sls/src/repl.rs index 46c4b53..32d2728 100644 --- a/SLS_Rust/sls/src/repl.rs +++ b/SLS_Rust/sls/src/repl.rs @@ -70,7 +70,7 @@ pub fn repl() -> i32 { LexResult::Ok(tokens) => { for token in tokens { if !interpreter.execute(&token) { - println!("A runtime error occured!"); + eprintln!("A runtime error occured!"); break; } } @@ -79,7 +79,7 @@ pub fn repl() -> i32 { } LexResult::Err(err) => { - println!("{}", err.message); + dbg!(err); } } }