Fixed bug with max() function, tested math syscalls

This commit is contained in:
2025-12-06 22:46:51 -07:00
parent 19d6679229
commit f53f2f878a
3 changed files with 388 additions and 13 deletions

View File

@@ -3,6 +3,386 @@ use anyhow::Result;
use indoc::indoc; use indoc::indoc;
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
#[test]
fn test_acos() -> Result<()> { fn test_acos() -> Result<()> {
todo!() let compiled = compile! {
debug
"
let i = acos(123);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
acos r15 123
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_asin() -> Result<()> {
let compiled = compile! {
debug
"
let i = asin(123);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
asin r15 123
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_atan() -> Result<()> {
let compiled = compile! {
debug
"
let i = atan(123);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
atan r15 123
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_atan2() -> Result<()> {
let compiled = compile! {
debug
"
let i = atan2(123, 456);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
atan2 r15 123 456
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_abs() -> Result<()> {
let compiled = compile! {
debug
"
let i = abs(-123);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
abs r15 -123
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_ceil() -> Result<()> {
let compiled = compile! {
debug
"
let i = ceil(123.90);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
ceil r15 123.90
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_cos() -> Result<()> {
let compiled = compile! {
debug
"
let i = cos(123);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
cos r15 123
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_floor() -> Result<()> {
let compiled = compile! {
debug
"
let i = floor(123);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
floor r15 123
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_log() -> Result<()> {
let compiled = compile! {
debug
"
let i = log(123);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
log r15 123
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_max() -> Result<()> {
let compiled = compile! {
debug
"
let i = max(123, 456);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
max r15 123 456
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_min() -> Result<()> {
let compiled = compile! {
debug
"
let i = min(123, 456);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
min r15 123 456
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_rand() -> Result<()> {
let compiled = compile! {
debug
"
let i = rand();
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
rand r15
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_sin() -> Result<()> {
let compiled = compile! {
debug
"
let i = sin(3);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
sin r15 3
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_sqrt() -> Result<()> {
let compiled = compile! {
debug
"
let i = sqrt(3);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
sqrt r15 3
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_tan() -> Result<()> {
let compiled = compile! {
debug
"
let i = tan(3);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
tan r15 3
move r8 r15 #i
"
}
);
Ok(())
}
#[test]
fn test_trunc() -> Result<()> {
let compiled = compile! {
debug
"
let i = trunc(3.234);
"
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
trunc r15 3.234
move r8 r15 #i
"
}
);
Ok(())
} }

View File

@@ -552,19 +552,14 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
} }
Expression::Syscall(spanned_call) => { Expression::Syscall(spanned_call) => {
let sys_call = spanned_call.node; let sys_call = spanned_call.node;
let SysCall::System(call) = sys_call else { let res = match sys_call {
// Math syscalls might be handled differently or here SysCall::System(s) => {
// For now assuming System returns value self.expression_syscall_system(s, spanned_call.span, scope)?
return Err(Error::Unknown( }
"Math syscall not yet supported in declaration".into(), SysCall::Math(m) => self.expression_syscall_math(m, scope)?,
Some(spanned_call.span),
));
}; };
if self if res.is_none() {
.expression_syscall_system(call, spanned_call.span, scope)?
.is_none()
{
return Err(Error::Unknown( return Err(Error::Unknown(
"SysCall did not return a value".into(), "SysCall did not return a value".into(),
Some(spanned_call.span), Some(spanned_call.span),

View File

@@ -1889,7 +1889,7 @@ impl<'a> Parser<'a> {
Ok(SysCall::Math(Math::Log(boxed!(arg)))) Ok(SysCall::Math(Math::Log(boxed!(arg))))
} }
"max" => { "max" => {
check_length(self, &invocation.arguments, 1)?; check_length(self, &invocation.arguments, 2)?;
let mut args = invocation.arguments.into_iter(); let mut args = invocation.arguments.into_iter();
let arg1 = args.next().ok_or(Error::UnexpectedEOF)?; let arg1 = args.next().ok_or(Error::UnexpectedEOF)?;
let arg2 = args.next().ok_or(Error::UnexpectedEOF)?; let arg2 = args.next().ok_or(Error::UnexpectedEOF)?;