Skip to content

Commit

Permalink
add sensing_resettimer
Browse files Browse the repository at this point in the history
  • Loading branch information
pufferfish101007 committed Aug 27, 2023
1 parent eb849fe commit f1edb2a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl IrOpcode {
operator_join => BlockDescriptor::new(vec![Any, Any], Text),
operator_letter_of => BlockDescriptor::new(vec![Number, Any], Text),
operator_length => BlockDescriptor::new(vec![Any], Number),
hq_goto { .. } => BlockDescriptor::new(vec![], Stack),
hq_goto { .. } | sensing_resettimer => BlockDescriptor::new(vec![], Stack),
hq_goto_if { .. } => BlockDescriptor::new(vec![Boolean], Stack),
hq_drop(n) => BlockDescriptor::new(vec![Any; *n], Stack),
_ => todo!("{:?}", &self),
Expand Down Expand Up @@ -641,6 +641,7 @@ impl IrBlockVec for Vec<IrBlock> {

self.append(&mut (match block_info.opcode {
BlockOpcode::sensing_timer => vec![IrOpcode::sensing_timer],
BlockOpcode::sensing_resettimer => vec![IrOpcode::sensing_resettimer],
BlockOpcode::looks_say => vec![IrOpcode::looks_say],
BlockOpcode::looks_think => vec![IrOpcode::looks_think],
BlockOpcode::operator_add => vec![IrOpcode::operator_add],
Expand Down
26 changes: 17 additions & 9 deletions src/targets/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ fn instructions(
_ => panic!("invalid OPERATOR field (E041)"),
},
sensing_timer => vec![Call(func_indices::SENSING_TIMER)],
sensing_resettimer => vec![Call(func_indices::SENSING_RESETTIMER)],
hq_drop(n) => vec![Drop; 2 * *n],
hq_goto { step: None, .. } => {
let vars_num: i32 = context
Expand Down Expand Up @@ -536,19 +537,20 @@ pub mod func_indices {
pub const MATHOP_POW_E: u32 = 22;
pub const MATHOP_POW10: u32 = 23;
pub const SENSING_TIMER: u32 = 24;
pub const SENSING_RESETTIMER: u32 = 24;

/* wasm funcs */
pub const FMOD: u32 = 25;
pub const CAST_FLOAT_BOOL: u32 = 26;
pub const CAST_BOOL_FLOAT: u32 = 27;
pub const CAST_BOOL_STRING: u32 = 28;
pub const CAST_ANY_STRING: u32 = 29;
pub const CAST_ANY_FLOAT: u32 = 30;
pub const CAST_ANY_BOOL: u32 = 31;
pub const TABLE_ADD_STRING: u32 = 32;
pub const CAST_FLOAT_BOOL: u32 = 27;
pub const CAST_BOOL_FLOAT: u32 = 28;
pub const CAST_BOOL_STRING: u32 = 29;
pub const CAST_ANY_STRING: u32 = 30;
pub const CAST_ANY_FLOAT: u32 = 31;
pub const CAST_ANY_BOOL: u32 = 32;
pub const TABLE_ADD_STRING: u32 = 33;
}
pub const BUILTIN_FUNCS: u32 = 33;
pub const IMPORTED_FUNCS: u32 = 25;
pub const BUILTIN_FUNCS: u32 = 34;
pub const IMPORTED_FUNCS: u32 = 26;

pub mod types {
#![allow(non_upper_case_globals)]
Expand Down Expand Up @@ -790,6 +792,11 @@ impl From<IrProject> for WebWasmFile {
"sensing_timer",
EntityType::Function(types::NOPARAM_F64),
);
imports.import(
"runtime",
"sensing_resettimer",
EntityType::Function(types::NOPARAM_NORESULT),
);

functions.function(types::F64x2_F64);
let mut fmod_func = Function::new(vec![]);
Expand Down Expand Up @@ -1290,6 +1297,7 @@ impl From<IrProject> for WebWasmFile {
mathop_pow_e: (n) => Math.exp(n),
mathop_pow10: (n) => Math.pow(10, n),
sensing_timer: () => (Date.now() - start_time) / 1000,
sensing_resettimer: () => start_time = Date.now(),
}},
cast: {{
stringtofloat: parseFloat,
Expand Down

0 comments on commit f1edb2a

Please sign in to comment.