Fixed errors in builtin and interpreter

This commit is contained in:
Kyler Olsen 2025-12-03 10:53:46 -07:00
parent e76287b9a4
commit 1c62f064a4
4 changed files with 87 additions and 75 deletions

View File

@ -185,7 +185,8 @@ pub fn builtin_addition(state: &mut InterpreterState) -> bool {
return false; return false;
}; };
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
let result = if type_flags.contains(NumericType::FLOAT) { let result = if type_flags.contains(NumericType::FLOAT) {
numeric_to_token(b.float_val + a.float_val, 0, type_flags) 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; return false;
}; };
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
let result = if type_flags.contains(NumericType::FLOAT) { let result = if type_flags.contains(NumericType::FLOAT) {
numeric_to_token(b.float_val - a.float_val, 0, type_flags) 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; return false;
}; };
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
let result = if type_flags.contains(NumericType::FLOAT) { let result = if type_flags.contains(NumericType::FLOAT) {
numeric_to_token(b.float_val * a.float_val, 0, type_flags) 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; return false;
} }
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
state.stack.push(StackValue::F64(b.float_val / a.float_val)); state.stack.push(StackValue::F64(b.float_val / a.float_val));
true true
} }
@ -254,7 +258,8 @@ pub fn builtin_modulus(state: &mut InterpreterState) -> bool {
return false; 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); let result = numeric_to_token(0.0, b.int_val % a.int_val, type_flags);
state.push_token(&result); state.push_token(&result);
@ -266,7 +271,8 @@ pub fn builtin_exponential(state: &mut InterpreterState) -> bool {
return false; 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))); state.stack.push(StackValue::F64(b.float_val.powf(a.float_val)));
true true
} }
@ -276,7 +282,8 @@ pub fn builtin_greater_than(state: &mut InterpreterState) -> bool {
return false; return false;
}; };
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
state.stack.push(StackValue::Boolean(b.float_val > a.float_val)); state.stack.push(StackValue::Boolean(b.float_val > a.float_val));
true true
} }
@ -286,7 +293,8 @@ pub fn builtin_greater_than_or_equal_to(state: &mut InterpreterState) -> bool {
return false; return false;
}; };
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
state.stack.push(StackValue::Boolean(b.float_val >= a.float_val)); state.stack.push(StackValue::Boolean(b.float_val >= a.float_val));
true true
} }
@ -296,7 +304,8 @@ pub fn builtin_less_than(state: &mut InterpreterState) -> bool {
return false; return false;
}; };
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
state.stack.push(StackValue::Boolean(b.float_val < a.float_val)); state.stack.push(StackValue::Boolean(b.float_val < a.float_val));
true true
} }
@ -306,7 +315,8 @@ pub fn builtin_less_than_or_equal_to(state: &mut InterpreterState) -> bool {
return false; return false;
}; };
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
state.stack.push(StackValue::Boolean(b.float_val <= a.float_val)); state.stack.push(StackValue::Boolean(b.float_val <= a.float_val));
true true
} }
@ -316,7 +326,8 @@ pub fn builtin_equal_to(state: &mut InterpreterState) -> bool {
return false; return false;
}; };
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
state.stack.push(StackValue::Boolean(b.float_val == a.float_val)); state.stack.push(StackValue::Boolean(b.float_val == a.float_val));
true true
} }
@ -326,7 +337,8 @@ pub fn builtin_not_equal_to(state: &mut InterpreterState) -> bool {
return false; return false;
}; };
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
state.stack.push(StackValue::Boolean(b.float_val != a.float_val)); state.stack.push(StackValue::Boolean(b.float_val != a.float_val));
true true
} }
@ -336,9 +348,8 @@ pub fn builtin_abs(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
if !val.type_flags.contains(NumericType::SIGNED | NumericType::FLOAT) { if !val.type_flags.contains(NumericType::SIGNED | NumericType::FLOAT) {
return false; return false;
@ -361,9 +372,8 @@ pub fn builtin_acos(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
if val.float_val < -1.0 || val.float_val > 1.0 { if val.float_val < -1.0 || val.float_val > 1.0 {
return false; return false;
@ -379,7 +389,8 @@ pub fn builtin_and(state: &mut InterpreterState) -> bool {
return false; return false;
}; };
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
let result = if type_flags.contains(NumericType::FLOAT) { let result = if type_flags.contains(NumericType::FLOAT) {
b.float_val != 0.0 && a.float_val != 0.0 b.float_val != 0.0 && a.float_val != 0.0
@ -396,9 +407,8 @@ pub fn builtin_asin(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
if val.float_val < -1.0 || val.float_val > 1.0 { if val.float_val < -1.0 || val.float_val > 1.0 {
return false; return false;
@ -414,9 +424,8 @@ pub fn builtin_atan(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
state.stack.pop(); state.stack.pop();
state.stack.push(StackValue::F64(val.float_val.atan())); state.stack.push(StackValue::F64(val.float_val.atan()));
@ -428,7 +437,8 @@ pub fn builtin_atan2(state: &mut InterpreterState) -> bool {
return false; 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))); state.stack.push(StackValue::F64(b.float_val.atan2(a.float_val)));
true true
} }
@ -443,7 +453,8 @@ pub fn builtin_bitand(state: &mut InterpreterState) -> bool {
return false; 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); let result = numeric_to_token(0.0, b.int_val & a.int_val, type_flags);
state.push_token(&result); state.push_token(&result);
true true
@ -454,9 +465,8 @@ pub fn builtin_bitnot(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
if val.type_flags.contains(NumericType::FLOAT) { if val.type_flags.contains(NumericType::FLOAT) {
return false; return false;
@ -477,7 +487,8 @@ pub fn builtin_bitor(state: &mut InterpreterState) -> bool {
return false; 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); let result = numeric_to_token(0.0, b.int_val | a.int_val, type_flags);
state.push_token(&result); state.push_token(&result);
true true
@ -492,7 +503,8 @@ pub fn builtin_bitxor(state: &mut InterpreterState) -> bool {
return false; 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); let result = numeric_to_token(0.0, b.int_val ^ a.int_val, type_flags);
state.push_token(&result); state.push_token(&result);
true true
@ -504,9 +516,8 @@ pub fn builtin_ceil(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
state.stack.pop(); state.stack.pop();
state.stack.push(StackValue::F64(val.float_val.ceil())); state.stack.push(StackValue::F64(val.float_val.ceil()));
@ -518,9 +529,8 @@ pub fn builtin_floor(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
state.stack.pop(); state.stack.pop();
state.stack.push(StackValue::F64(val.float_val.floor())); state.stack.push(StackValue::F64(val.float_val.floor()));
@ -532,9 +542,8 @@ pub fn builtin_round(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
state.stack.pop(); state.stack.pop();
state.stack.push(StackValue::F64(val.float_val.round())); state.stack.push(StackValue::F64(val.float_val.round()));
@ -546,9 +555,8 @@ pub fn builtin_sqrt(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
if val.float_val < 0.0 { if val.float_val < 0.0 {
return false; return false;
@ -564,9 +572,8 @@ pub fn builtin_sin(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
state.stack.pop(); state.stack.pop();
state.stack.push(StackValue::F64(val.float_val.sin())); state.stack.push(StackValue::F64(val.float_val.sin()));
@ -578,9 +585,8 @@ pub fn builtin_cos(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
state.stack.pop(); state.stack.pop();
state.stack.push(StackValue::F64(val.float_val.cos())); state.stack.push(StackValue::F64(val.float_val.cos()));
@ -592,9 +598,8 @@ pub fn builtin_tan(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
state.stack.pop(); state.stack.pop();
state.stack.push(StackValue::F64(val.float_val.tan())); state.stack.push(StackValue::F64(val.float_val.tan()));
@ -606,9 +611,8 @@ pub fn builtin_ln(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
if val.float_val <= 0.0 { if val.float_val <= 0.0 {
return false; return false;
@ -624,9 +628,8 @@ pub fn builtin_log(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
if val.float_val <= 0.0 { if val.float_val <= 0.0 {
return false; return false;
@ -704,7 +707,8 @@ pub fn builtin_roll(state: &mut InterpreterState) -> bool {
return false; return false;
} }
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
// Rotate the top 'count' items 'times' times // Rotate the top 'count' items 'times' times
for _ in 0..times { for _ in 0..times {
@ -732,7 +736,8 @@ pub fn builtin_or(state: &mut InterpreterState) -> bool {
return false; return false;
}; };
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
let result = if type_flags.contains(NumericType::FLOAT) { let result = if type_flags.contains(NumericType::FLOAT) {
b.float_val != 0.0 || a.float_val != 0.0 b.float_val != 0.0 || a.float_val != 0.0
@ -754,7 +759,8 @@ pub fn builtin_shl(state: &mut InterpreterState) -> bool {
return false; 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); let result = numeric_to_token(0.0, b.int_val << a.int_val, type_flags);
state.push_token(&result); state.push_token(&result);
true true
@ -769,7 +775,8 @@ pub fn builtin_shr(state: &mut InterpreterState) -> bool {
return false; 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); let result = numeric_to_token(0.0, b.int_val >> a.int_val, type_flags);
state.push_token(&result); state.push_token(&result);
true true
@ -788,9 +795,8 @@ pub fn builtin_seed(state: &mut InterpreterState) -> bool {
return false; return false;
} }
let Some(val) = extract_numeric_type(&state.stack[state.stack.len()-1]) else { let Some(last) = state.stack.last() else { return false };
return false; let Some(val) = extract_numeric_type(last) else { return false };
};
if val.type_flags.contains(NumericType::FLOAT) { if val.type_flags.contains(NumericType::FLOAT) {
return false; return false;
@ -820,7 +826,9 @@ pub fn builtin_if(state: &mut InterpreterState) -> bool {
let condition = is_truthy(&state.stack[state.stack.len()-3]); 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 { if condition {
state.execute_token_string(&then_block) state.execute_token_string(&then_block)
@ -844,7 +852,8 @@ pub fn builtin_while(state: &mut InterpreterState) -> bool {
_ => return false, _ => return false,
}; };
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
loop { loop {
if !state.execute_token_string(&cond_block) { if !state.execute_token_string(&cond_block) {
@ -888,7 +897,9 @@ pub fn builtin_for(state: &mut InterpreterState) -> bool {
return false; return false;
}; };
state.stack.drain(0..3); state.stack.pop();
state.stack.pop();
state.stack.pop();
for i in start..end { for i in start..end {
state.stack.push(StackValue::I64(i as i64)); state.stack.push(StackValue::I64(i as i64));
@ -954,7 +965,8 @@ pub fn builtin_const(state: &mut InterpreterState) -> bool {
return false; return false;
} }
state.stack.drain(0..2); state.stack.pop();
state.stack.pop();
state.functions.insert(name, FunctionItem::TokenString(value)); state.functions.insert(name, FunctionItem::TokenString(value));
@ -1093,7 +1105,7 @@ pub fn load_builtins(state: &mut InterpreterState) -> bool {
break; break;
} }
if !state.execute(&token) { if !state.execute(&token) {
println!("Error in __builtin.sls"); eprintln!("Error in __builtin.sls");
return false; return false;
} }
} }

View File

@ -29,7 +29,7 @@ pub fn exec_file(interpreter: &mut InterpreterState, filename: &str) -> bool {
} }
LexResult::Err(err) => { LexResult::Err(err) => {
eprintln!("{}", err.message); dbg!(err);
false false
} }
} }

View File

@ -129,6 +129,6 @@ impl InterpreterState {
} }
pub fn stack_top(&self) -> Option<&StackValue> { pub fn stack_top(&self) -> Option<&StackValue> {
self.stack.get(self.stack.len() - 1) self.stack.last()
} }
} }

View File

@ -70,7 +70,7 @@ pub fn repl() -> i32 {
LexResult::Ok(tokens) => { LexResult::Ok(tokens) => {
for token in tokens { for token in tokens {
if !interpreter.execute(&token) { if !interpreter.execute(&token) {
println!("A runtime error occured!"); eprintln!("A runtime error occured!");
break; break;
} }
} }
@ -79,7 +79,7 @@ pub fn repl() -> i32 {
} }
LexResult::Err(err) => { LexResult::Err(err) => {
println!("{}", err.message); dbg!(err);
} }
} }
} }