Skip to content

Commit 8f8ee37

Browse files
Refactor: Clean code, refactor docs and address linter comments.
1 parent 4d66bc7 commit 8f8ee37

File tree

7 files changed

+16
-24
lines changed

7 files changed

+16
-24
lines changed

compiler/src/modules/parser/literals.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use super::types::{OpCode, Value, SSAChunk};
55
use super::types::builtin;
66
use crate::modules::lexer::{Token, TokenType};
77
use alloc::{string::{String, ToString}, vec::Vec, format};
8-
use hashbrown::HashMap;
98

109
impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
1110

@@ -323,7 +322,7 @@ impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
323322

324323
self.compile_block();
325324

326-
let mut body = core::mem::take(&mut self.chunk);
325+
let body = core::mem::take(&mut self.chunk);
327326
self.chunk = saved_chunk;
328327
self.ssa_versions = saved_ver;
329328

compiler/src/modules/parser/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ fn unescape(s: &str) -> String {
196196
let mut out = String::with_capacity(s.len());
197197
let mut chars = s.chars().peekable();
198198

199-
let mut take_hex = |chars: &mut core::iter::Peekable<core::str::Chars>, n: usize| -> char {
199+
let take_hex = |chars: &mut core::iter::Peekable<core::str::Chars>, n: usize| -> char {
200200
let hex: String = chars.by_ref().take(n).collect();
201201
u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32).unwrap_or('\u{FFFD}')
202202
};

compiler/src/modules/vm/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'a> VM<'a> {
110110

111111
pub fn call_float(&mut self) -> Result<(), VmErr> {
112112
let o = self.pop()?;
113-
let f = if o.is_float() { o.as_float() }
113+
let f = if o.is_float() { o.as_float() }
114114
else if o.is_int() { o.as_int() as f64 }
115115
else if o.is_heap() { match self.heap.get(o) {
116116
HeapObj::Str(s) => s.trim().parse().map_err(|_| VmErr::Value(format!("float: '{}'", s)))?,

compiler/src/modules/vm/cache.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// vm/cache.rs
2+
23
use super::types::{Val, eq_vals_deep};
34
use crate::modules::parser::OpCode;
45
use alloc::{vec, vec::Vec};
@@ -64,10 +65,6 @@ impl OpcodeCache {
6465
if let Some(s) = self.slots.get_mut(ip) { *s = CacheSlot::empty(); }
6566
}
6667

67-
pub fn specialized_count(&self) -> usize {
68-
self.slots.iter().filter(|s| s.hot_fast.is_some()).count()
69-
}
70-
7168
fn specialize(opcode: &OpCode, ta: u8, tb: u8) -> Option<FastOp> {
7269
match (opcode, ta, tb) {
7370
(OpCode::Add, 1, 1) => Some(FastOp::AddInt), (OpCode::Add, 2, 2) => Some(FastOp::AddFloat),

compiler/src/modules/vm/collections.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ impl<'a> VM<'a> {
3030
*/
3131

3232
pub fn build_slice(&mut self, op: u16) -> Result<(), VmErr> {
33-
let step = if op == 3 { self.pop()? } else { Val::none() };
34-
let stop = self.pop()?;
33+
let step = if op == 3 { self.pop()? } else { Val::none() };
34+
let stop = self.pop()?;
3535
let start = self.pop()?;
3636
let val = self.heap.alloc(HeapObj::Slice(start, stop, step))?;
3737
self.push(val); Ok(())
@@ -52,7 +52,7 @@ impl<'a> VM<'a> {
5252
_ => return Err(VmErr::Type("cannot unpack".into())),
5353
};
5454
let before = (op >> 8) as usize;
55-
let after = (op & 0xFF) as usize;
55+
let after = (op & 0xFF) as usize;
5656
if items.len() < before + after {
5757
return Err(VmErr::Value("not enough values to unpack".into()));
5858
}
@@ -239,9 +239,9 @@ impl<'a> VM<'a> {
239239
*/
240240

241241
pub fn store_item(&mut self) -> Result<(), VmErr> {
242-
let value = self.pop()?;
242+
let value = self.pop()?;
243243
let idx_val = self.pop()?;
244-
let cont = self.pop()?;
244+
let cont = self.pop()?;
245245
if !cont.is_heap() { return Err(VmErr::Type("item assignment on non-container".into())); }
246246
match self.heap.get_mut(cont) {
247247
HeapObj::List(v) => {

compiler/src/modules/vm/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ pub struct VM<'a> {
3030
pub(crate) chunk: &'a SSAChunk,
3131
pub(crate) globals: HashMap<String, Val>,
3232
pub(crate) live_slots: Vec<Val>,
33+
pub output: Vec<String>,
3334
templates: Templates,
3435
budget:usize,
3536
depth: usize,
36-
max_calls: usize,
37-
pub output: Vec<String>,
37+
max_calls: usize
3838
}
3939

4040
impl<'a> VM<'a> {
@@ -83,7 +83,6 @@ impl<'a> VM<'a> {
8383
HeapObj::Set(s) => IterFrame::Seq { items: s.borrow().clone(), idx: 0 },
8484
HeapObj::Str(s) => {
8585
let chars: Vec<char> = s.chars().collect();
86-
drop(s);
8786
let mut items = Vec::with_capacity(chars.len());
8887
for c in chars { items.push(self.heap.alloc(HeapObj::Str(c.to_string()))?); }
8988
IterFrame::Seq { items, idx: 0 }
@@ -105,7 +104,6 @@ impl<'a> VM<'a> {
105104
HeapObj::Tuple(v) => v.clone(),
106105
HeapObj::Str(s) => {
107106
let chars: Vec<char> = s.chars().collect();
108-
drop(s);
109107
if chars.len() != expected {
110108
return Err(VmErr::Value(format!("expected {} values to unpack, got {}", expected, chars.len())));
111109
}
@@ -168,7 +166,8 @@ impl<'a> VM<'a> {
168166
self.exec(self.chunk, &mut slots)
169167
}
170168

171-
fn collect(&mut self, current_slots: &[Option<Val>]) { // Marks all reachable values from stack, globals, iterators and slots, then sweeps.
169+
// Marks all reachable values from stack, globals, iterators and slots, then sweeps.
170+
fn collect(&mut self, current_slots: &[Option<Val>]) {
172171
for &v in &self.stack { self.heap.mark(v); }
173172
for &v in self.globals.values() { self.heap.mark(v); }
174173
for frame in &self.iter_stack {

compiler/src/modules/vm/ops.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<'a> VM<'a> {
9797
.map(|x| self.repr(*x)).collect::<Vec<_>>().join(", ")),
9898
HeapObj::Slice(s, e, st) => format!("slice({}, {}, {})",
9999
self.display(*s), self.display(*e), self.display(*st)),
100-
HeapObj::BigInt(b) => b.to_decimal(),
100+
HeapObj::BigInt(b) => b.to_decimal(),
101101
HeapObj::Type(name) => format!("<class '{}'>", name)
102102
}
103103
}
@@ -127,7 +127,8 @@ impl<'a> VM<'a> {
127127
Err(VmErr::Type(format!("'<' not supported between '{}' and '{}'", self.type_name(a), self.type_name(b))))
128128
}
129129

130-
pub fn contains(&self, container: Val, item: Val) -> bool { // Checks item presence in list, tuple, dict, set, or substring in str
130+
// Checks item presence in list, tuple, dict, set, or substring in string.
131+
pub fn contains(&self, container: Val, item: Val) -> bool {
131132
if !container.is_heap() { return false; }
132133
match self.heap.get(container) {
133134
HeapObj::List(v) => v.borrow().iter().any(|x| self.eq_vals(*x, item)),
@@ -216,10 +217,6 @@ impl<'a> VM<'a> {
216217
None
217218
}
218219

219-
pub(crate) fn is_int_type(&self, v: Val) -> bool {
220-
v.is_int() || (v.is_heap() && matches!(self.heap.get(v), HeapObj::BigInt(_)))
221-
}
222-
223220
pub(crate) fn bigint_to_val(&mut self, b: BigInt) -> Result<Val, VmErr> {
224221
if let Some(i) = b.to_i64_checked() {
225222
if i >= Val::INT_MIN && i <= Val::INT_MAX { return Ok(Val::int(i)); }

0 commit comments

Comments
 (0)