Skip to content

Commit

Permalink
Merge pull request #195 from mrodz/194-use-of-nested-continue-will-no…
Browse files Browse the repository at this point in the history
…t-compile

194 use of nested continue will not compile
  • Loading branch information
mrodz authored Apr 20, 2024
2 parents bf9faad + d5da0f3 commit 1aba9d7
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 418 deletions.
32 changes: 32 additions & 0 deletions bytecode/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub enum BuiltInFunction {
MapKeys,
MapValues,
MapPairs,
MapRemove,
MapClear,
VecClear,
}

type BuiltInFunctionReturnBundle = (
Expand Down Expand Up @@ -1010,6 +1013,35 @@ impl BuiltInFunction {
None,
))
}
Self::MapRemove => {
let (Some(Primitive::Map(map)), Some(key)) = (arguments.first(), arguments.get(1))
else {
unreachable!()
};

Ok((
Some(Primitive::Optional(map.remove(key.clone())?.map(Box::new))),
None,
))
}
Self::MapClear => {
let Some(Primitive::Map(map)) = arguments.first() else {
unreachable!()
};

map.clear();

Ok((None, None))
}
Self::VecClear => {
let Some(Primitive::Vector(vector)) = arguments.first() else {
unreachable!()
};

vector.0.borrow_mut().clear();

Ok((None, None))
}
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions bytecode/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ static_module_generator! {
map_keys(BuiltInFunction::MapKeys),
map_values(BuiltInFunction::MapValues),
map_pairs(BuiltInFunction::MapPairs),
map_remove(BuiltInFunction::MapRemove),
map_clear(BuiltInFunction::MapClear),
vector_clear(BuiltInFunction::VecClear),
}

impl PrimitiveFlagsPair {
Expand All @@ -140,18 +143,13 @@ impl PrimitiveFlagsPair {

pub fn primitive(&self) -> GcCellRef<Primitive> {
GcCellRef::map(self.0.borrow(), |x| &x.0)
// unsafe { &(*self.0).0 }
}

pub fn flags(&self) -> GcCellRef<VariableFlags> {
GcCellRef::map(self.0.borrow(), |x| &x.1)
// unsafe { &(*self.0.get()).1 }
}

pub fn set_primitive(&self, new_value: Primitive) -> Primitive {
// let ptr = self.0.get();
// let ptr = unsafe { &mut (*ptr).0 };
// std::mem::replace(ptr, new_value)
let mut mutable_gc_view = self.0.borrow_mut();
std::mem::replace(&mut mutable_gc_view.0, new_value)
}
Expand Down
14 changes: 14 additions & 0 deletions bytecode/src/variables/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,17 @@ impl GcMap {
.map(|kv| (kv.0.clone(), kv.1.clone()))
.collect::<Vec<_>>()
}

pub fn clear(&self) {
self.0.borrow_mut().clear();
}

pub fn remove(&self, key: Primitive) -> Result<Option<Primitive>> {
Ok(self
.0
.borrow_mut()
.remove(&key.move_out_of_heap_primitive()?))
}
}

#[derive(Clone, Trace, Finalize, Debug, Default)]
Expand Down Expand Up @@ -490,6 +501,7 @@ impl Primitive {
"push" => Ok(PRIMITIVE_MODULE.vector_push()),
"join" => Ok(PRIMITIVE_MODULE.vector_join()),
"index_of" => Ok(PRIMITIVE_MODULE.vector_index_of()),
"clear" => Ok(PRIMITIVE_MODULE.vector_clear()),
_ => Err(ret),
}),
ret @ P::Str(..) => Ok(match property {
Expand Down Expand Up @@ -540,6 +552,8 @@ impl Primitive {
"keys" => Ok(PRIMITIVE_MODULE.map_keys()),
"values" => Ok(PRIMITIVE_MODULE.map_values()),
"pairs" => Ok(PRIMITIVE_MODULE.map_pairs()),
"clear" => Ok(PRIMITIVE_MODULE.map_clear()),
"remove" => Ok(PRIMITIVE_MODULE.map_remove()),
_ => Err(ret),
}),
ret => Ok(Err(ret)),
Expand Down
35 changes: 0 additions & 35 deletions compiler/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,44 +226,9 @@ impl Display for CompiledItem {
}

impl CompiledItem {
pub fn is_loop_instruction(&self) -> bool {
const WHILE_LOOP: u8 = 0x31;
matches!(self, Self::Instruction { id: WHILE_LOOP, .. })
}

pub fn is_done_instruction(&self) -> bool {
const DONE: u8 = 0x27;
const JMP_POP: u8 = 0x32;
matches!(
self,
Self::Instruction {
id: DONE | JMP_POP,
..
}
)
}

pub fn repr(&self, use_string_version: bool) -> Result<String> {
fn fix_arg_if_needed(arg: &str) -> Result<Cow<str>> {
Ok(Cow::Owned("\"".to_owned() + arg + "\""))
// let starts = arg.starts_with('"');
// let ends = arg.ends_with('"') && !arg.ends_with(r#"\""#);
//
// if starts ^ ends {
// bail!("non-matching `\"` on arg {arg} ({starts} & {ends})")
// }
//
// let has_quotes = starts && ends;
//
// if !has_quotes && arg.contains(' ') {
// let mut combined = String::with_capacity(arg.len() + 2);
// combined.push('"');
// combined.push_str(arg);
// combined.push('"');
// Ok(Cow::Owned(combined))
// } else {
// Ok(Cow::Borrowed(arg))
// }
}

match self {
Expand Down
32 changes: 13 additions & 19 deletions compiler/src/ast/number_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,34 +156,28 @@ impl Compile for NumberLoop {
result.push(instruction!(while_loop offset_to_end_of_loop));
body_compiled.push(instruction!(jmp_pop offset_to_start_of_loop));

let mut loop_depth = 0;

let final_body_compiled_len = body_compiled.len();

for (idx, body_item) in body_compiled.into_iter().enumerate() {
if body_item.is_loop_instruction() {
loop_depth += 1;
}

if body_item.is_done_instruction() {
loop_depth -= 1;
}

match body_item {
CompiledItem::Continue(frames_to_pop) if loop_depth == 0 => {
body_compiled
.iter_mut()
.enumerate()
.for_each(|(idx, body_item)| match body_item {
CompiledItem::Continue(ref frames_to_pop) => {
let distance_to_end = final_body_compiled_len - step_compiled_len - idx - 1;

let frames_to_pop = frames_to_pop - 1;

result.push(instruction!(jmp_pop distance_to_end frames_to_pop))
*body_item = instruction!(jmp_pop distance_to_end frames_to_pop);
}
CompiledItem::Break(frames_to_pop) if loop_depth == 0 => {
CompiledItem::Break(ref frames_to_pop) => {
let distance_to_end = final_body_compiled_len - idx;
result.push(instruction!(jmp_pop distance_to_end frames_to_pop))

*body_item = instruction!(jmp_pop distance_to_end frames_to_pop)
}
normal => result.push(normal),
}
}
_ => (),
});

result.append(&mut body_compiled);

if !self.name_is_collision {
result.push(instruction!(delete_name_scoped loop_identity end_loop_register));
Expand Down
Loading

0 comments on commit 1aba9d7

Please sign in to comment.