Add support in the parser for the various Math syscalls
This commit is contained in:
@@ -4,7 +4,7 @@ mod test;
|
||||
pub mod sys_call;
|
||||
pub mod tree_node;
|
||||
|
||||
use crate::sys_call::System;
|
||||
use crate::sys_call::{Math, System};
|
||||
use quick_error::quick_error;
|
||||
use std::io::SeekFrom;
|
||||
use sys_call::SysCall;
|
||||
@@ -1649,6 +1649,7 @@ impl<'a> Parser<'a> {
|
||||
let invocation = self.invocation()?;
|
||||
|
||||
match invocation.name.node.as_str() {
|
||||
// System SysCalls
|
||||
"yield" => {
|
||||
check_length(self, &invocation.arguments, 0)?;
|
||||
Ok(SysCall::System(sys_call::System::Yield))
|
||||
@@ -1822,6 +1823,119 @@ impl<'a> Parser<'a> {
|
||||
expr,
|
||||
)))
|
||||
}
|
||||
// Math SysCalls
|
||||
"acos" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let tmp = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Acos(boxed!(tmp))))
|
||||
}
|
||||
"asin" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let tmp = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Asin(boxed!(tmp))))
|
||||
}
|
||||
"atan" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let expr = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Atan(boxed!(expr))))
|
||||
}
|
||||
"atan2" => {
|
||||
check_length(self, &invocation.arguments, 2)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg1 = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
let arg2 = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Atan2(boxed!(arg1), boxed!(arg2))))
|
||||
}
|
||||
"abs" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let expr = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Abs(boxed!(expr))))
|
||||
}
|
||||
"ceil" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Ceil(boxed!(arg))))
|
||||
}
|
||||
"cos" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Cos(boxed!(arg))))
|
||||
}
|
||||
"floor" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Floor(boxed!(arg))))
|
||||
}
|
||||
"log" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Log(boxed!(arg))))
|
||||
}
|
||||
"max" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg1 = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
let arg2 = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Max(boxed!(arg1), boxed!(arg2))))
|
||||
}
|
||||
"min" => {
|
||||
check_length(self, &invocation.arguments, 2)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg1 = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
let arg2 = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Min(boxed!(arg1), boxed!(arg2))))
|
||||
}
|
||||
"rand" => {
|
||||
check_length(self, &invocation.arguments, 0)?;
|
||||
Ok(SysCall::Math(Math::Rand))
|
||||
}
|
||||
"sin" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Sin(boxed!(arg))))
|
||||
}
|
||||
"sqrt" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Sqrt(boxed!(arg))))
|
||||
}
|
||||
"tan" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Tan(boxed!(arg))))
|
||||
}
|
||||
"trunc" => {
|
||||
check_length(self, &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Trunc(boxed!(arg))))
|
||||
}
|
||||
_ => Err(Error::UnsupportedKeyword(
|
||||
self.current_span(),
|
||||
self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
|
||||
|
||||
Reference in New Issue
Block a user