mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Put constants in Kal
This commit is contained in:
@@ -5,7 +5,10 @@ use valuescript_vm::{
|
||||
vs_value::{number_to_index, ToVal, Val},
|
||||
};
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap},
|
||||
mem::take,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
asm::{self, Builtin, Number, Pointer, Register, Value},
|
||||
@@ -95,7 +98,7 @@ impl Kal {
|
||||
}
|
||||
}
|
||||
|
||||
fn from_value(value: &Value) -> Self {
|
||||
pub fn from_value(value: &Value) -> Self {
|
||||
match value {
|
||||
Value::Void => Kal::Void,
|
||||
Value::Undefined => Kal::Undefined,
|
||||
@@ -232,11 +235,29 @@ impl Kal {
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FnState {
|
||||
pub pointer_kals: HashMap<Pointer, Kal>,
|
||||
pub mutable_this_established: bool,
|
||||
pub registers: BTreeMap<String, Kal>,
|
||||
}
|
||||
|
||||
impl FnState {
|
||||
pub fn new(pointer_kals: HashMap<Pointer, Kal>) -> Self {
|
||||
FnState {
|
||||
pointer_kals,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_local(&mut self) {
|
||||
let pointer_kals = take(&mut self.pointer_kals);
|
||||
|
||||
*self = Self {
|
||||
pointer_kals,
|
||||
mutable_this_established: Default::default(),
|
||||
registers: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_mut(&mut self, reg_name: String) -> &mut Kal {
|
||||
self.registers.entry(reg_name).or_default()
|
||||
}
|
||||
@@ -451,8 +472,16 @@ impl FnState {
|
||||
| Value::Number(_)
|
||||
| Value::BigInt(_)
|
||||
| Value::String(_)
|
||||
| Value::Pointer(_)
|
||||
| Value::Builtin(_) => Kal::from_value(arg),
|
||||
Value::Pointer(p) => match self.pointer_kals.get(p) {
|
||||
Some(kal) => {
|
||||
if let Some(new_arg) = kal.try_to_value() {
|
||||
*arg = new_arg;
|
||||
}
|
||||
kal.clone()
|
||||
}
|
||||
None => Kal::Pointer(p.clone()),
|
||||
},
|
||||
Value::Array(array) => {
|
||||
let mut values = Vec::<Kal>::new();
|
||||
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
use std::mem::take;
|
||||
use std::{collections::HashMap, mem::take};
|
||||
|
||||
use crate::asm::{DefinitionContent, FnLine, Function, Instruction, Module, Register};
|
||||
use crate::asm::{DefinitionContent, FnLine, Function, Instruction, Module, Pointer, Register};
|
||||
|
||||
use super::kal::FnState;
|
||||
use super::kal::{FnState, Kal};
|
||||
|
||||
pub fn simplify(module: &mut Module) {
|
||||
let mut pointer_kals = HashMap::<Pointer, Kal>::new();
|
||||
|
||||
for defn in &mut module.definitions {
|
||||
if let DefinitionContent::Value(value) = &defn.content {
|
||||
pointer_kals.insert(defn.pointer.clone(), Kal::from_value(value));
|
||||
}
|
||||
}
|
||||
|
||||
for defn in &mut module.definitions {
|
||||
match &mut defn.content {
|
||||
DefinitionContent::Function(fn_) => simplify_fn(FnState::default(), fn_),
|
||||
DefinitionContent::Function(fn_) => simplify_fn(FnState::new(pointer_kals.clone()), fn_),
|
||||
DefinitionContent::Class(_) => {}
|
||||
DefinitionContent::Value(_) => {}
|
||||
DefinitionContent::Lazy(_) => {}
|
||||
@@ -115,7 +123,7 @@ fn simplify_fn(mut state: FnState, fn_: &mut Function) {
|
||||
|
||||
match &mut fn_.body[i] {
|
||||
FnLine::Instruction(instr) => state.eval_instruction(instr),
|
||||
FnLine::Label(_) => state = FnState::default(),
|
||||
FnLine::Label(_) => state.clear_local(),
|
||||
FnLine::Empty | FnLine::Comment(_) => {}
|
||||
FnLine::Release(reg) => pending_releases.push(reg.clone()),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user