more working syscalls
This commit is contained in:
@@ -33,6 +33,7 @@ fn test_sleep() -> anyhow::Result<()> {
|
||||
sleep(3);
|
||||
let sleepAmount = 15;
|
||||
sleep(sleepAmount);
|
||||
sleep(sleepAmount * 2);
|
||||
"
|
||||
};
|
||||
|
||||
@@ -45,6 +46,8 @@ fn test_sleep() -> anyhow::Result<()> {
|
||||
sleep 3
|
||||
move r8 15 #sleepAmount
|
||||
sleep r8
|
||||
mul r1 r8 2
|
||||
sleep r1
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -60,8 +63,7 @@ fn test_set_on_device() -> anyhow::Result<()> {
|
||||
device airConditioner = "d0";
|
||||
let internalTemp = 20c;
|
||||
|
||||
let shouldToggleOn = internalTemp > 25c;
|
||||
setOnDevice(airConditioner, "On", shouldToggleOn);
|
||||
setOnDevice(airConditioner, "On", internalTemp > 25c);
|
||||
"#
|
||||
};
|
||||
|
||||
@@ -73,8 +75,7 @@ fn test_set_on_device() -> anyhow::Result<()> {
|
||||
main:
|
||||
move r8 293.15 #internalTemp
|
||||
sgt r1 r8 298.15
|
||||
move r9 r1 #shouldToggleOn
|
||||
s d0 On r9
|
||||
s d0 On r1
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -82,6 +83,31 @@ fn test_set_on_device() -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_on_device_batched() -> anyhow::Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
r#"
|
||||
let doorHash = hash("Door");
|
||||
setOnDeviceBatched(doorHash, "Lock", true);
|
||||
"#
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
r#"
|
||||
j main
|
||||
main:
|
||||
move r15 HASH("Door") #hash_ret
|
||||
move r8 r15 #doorHash
|
||||
sb r8 Lock 1
|
||||
"#
|
||||
}
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_from_device() -> anyhow::Result<()> {
|
||||
let compiled = compile! {
|
||||
@@ -107,3 +133,27 @@ fn test_load_from_device() -> anyhow::Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hash() -> anyhow::Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
r#"
|
||||
let nameHash = hash("testValue");
|
||||
"#
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
r#"
|
||||
j main
|
||||
main:
|
||||
move r15 HASH("testValue") #hash_ret
|
||||
move r8 r15 #nameHash
|
||||
"#
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1023,7 +1023,7 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
|
||||
Ok(None)
|
||||
}
|
||||
System::Sleep(amt) => {
|
||||
let (var, cleanup) = self.compile_literal_or_variable(amt, scope)?;
|
||||
let (var, cleanup) = self.compile_operand(*amt, scope)?;
|
||||
self.write_output(format!("sleep {var}"))?;
|
||||
if let Some(temp) = cleanup {
|
||||
scope.free_temp(temp)?;
|
||||
@@ -1031,8 +1031,23 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
System::Hash(hash_arg) => {
|
||||
let Literal::String(str_lit) = hash_arg else {
|
||||
return Err(Error::AgrumentMismatch(
|
||||
"Arg1 expected to be a string literal.".into(),
|
||||
));
|
||||
};
|
||||
|
||||
let loc = VariableLocation::Persistant(VariableScope::RETURN_REGISTER);
|
||||
self.emit_variable_assignment("hash_ret", &loc, format!(r#"HASH("{}")"#, str_lit))?;
|
||||
|
||||
Ok(Some(CompilationResult {
|
||||
location: loc,
|
||||
temp_name: None,
|
||||
}))
|
||||
}
|
||||
System::SetOnDevice(device, logic_type, variable) => {
|
||||
let (variable, var_cleanup) = self.compile_literal_or_variable(variable, scope)?;
|
||||
let (variable, var_cleanup) = self.compile_operand(*variable, scope)?;
|
||||
|
||||
let LiteralOrVariable::Variable(device) = device else {
|
||||
return Err(Error::AgrumentMismatch(
|
||||
@@ -1058,6 +1073,28 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
System::SetOnDeviceBatched(device_hash, logic_type, variable) => {
|
||||
let (var, var_cleanup) = self.compile_operand(*variable, scope)?;
|
||||
let (device_hash, device_hash_cleanup) =
|
||||
self.compile_literal_or_variable(device_hash, scope)?;
|
||||
let Literal::String(logic_type) = logic_type else {
|
||||
return Err(Error::AgrumentMismatch(
|
||||
"Arg2 expected to be a string".into(),
|
||||
));
|
||||
};
|
||||
|
||||
self.write_output(format!("sb {} {} {}", device_hash, logic_type, var))?;
|
||||
|
||||
if let Some(var_cleanup) = var_cleanup {
|
||||
scope.free_temp(var_cleanup)?;
|
||||
}
|
||||
|
||||
if let Some(device_cleanup) = device_hash_cleanup {
|
||||
scope.free_temp(device_cleanup)?;
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
System::LoadFromDevice(device, logic_type) => {
|
||||
let LiteralOrVariable::Variable(device) = device else {
|
||||
return Err(Error::AgrumentMismatch(
|
||||
|
||||
Reference in New Issue
Block a user