Skip to content

Commit

Permalink
More natives
Browse files Browse the repository at this point in the history
  • Loading branch information
Quaqqer committed Dec 17, 2024
1 parent 5f6f827 commit 8624c1b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
57 changes: 45 additions & 12 deletions crates/saft-bytecode/src/natives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@ use crate::value::Cast;
use crate::value::NativeFunction;
use crate::value::Value;
use crate::vm;
use crate::vm::exotic;
use saft_macro::native_function;
use saft_syntax::span::Span;

pub struct NativeRes(pub Result<Value, vm::Error>);

impl From<Value> for NativeRes {
fn from(value: Value) -> Self {
NativeRes(Ok(value))
impl<T: Into<Value>> From<Result<T, vm::Error>> for NativeRes {
fn from(value: Result<T, vm::Error>) -> Self {
NativeRes(value.map(Into::into))
}
}

impl From<Result<Value, vm::Error>> for NativeRes {
fn from(value: Result<Value, vm::Error>) -> Self {
NativeRes(value)
}
}

impl From<()> for NativeRes {
fn from(_value: ()) -> Self {
NativeRes(Ok(Value::Nil))
impl<T: Into<Value>> From<T> for NativeRes {
fn from(value: T) -> Self {
NativeRes(Ok(value.into()))
}
}

Expand All @@ -32,3 +27,41 @@ pub fn print(v: Value) {
_ => println!("{}", v.repr()),
}
}

#[native_function]
pub fn sin(v: f64) -> f64 {
v.sin()
}

#[native_function]
pub fn cos(v: f64) -> f64 {
v.cos()
}

#[native_function]
pub fn reverse_bits(v: i64) -> i64 {
v.reverse_bits()
}

#[native_function]
pub fn hypot(x: f64, y: f64) -> f64 {
f64::hypot(x, y)
}

#[native_function]
pub fn fib(v: i64, span: &Span) -> Result<i64, vm::Error> {
if v < 0 {
exotic!(
"Only positive numbers can be used in the fibonacci sequence.",
span
);
} else {
let (mut a, mut b) = (0, 1);

for _ in 0..v {
(a, b) = (b, a + b);
}

Ok(a)
}
}
4 changes: 2 additions & 2 deletions crates/saft-bytecode/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl Cast<bool> for Value {

impl Cast<i64> for Value {
fn name() -> String {
"bool".into()
"int".into()
}

fn cast(&self) -> Option<i64> {
Expand All @@ -288,7 +288,7 @@ impl Cast<i64> for Value {

impl Cast<f64> for Value {
fn name() -> String {
"bool".into()
"float".into()
}

fn cast(&self) -> Option<f64> {
Expand Down
6 changes: 4 additions & 2 deletions crates/saft-bytecode/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,24 @@ impl Error {

macro_rules! exotic {
($msg:expr, $span:expr) => {
return Err(Error::Exotic {
return Err(crate::vm::Error::Exotic {
message: $msg.into(),
span: $span.clone(),
note: None,
})
};

($msg:expr, $span:expr, $note:expr) => {
return Err(Error::Exotic {
return Err(crate::vm::Error::Exotic {
message: $msg.into(),
span: $span.clone(),
note: Some($note.into()),
})
};
}

pub(crate) use exotic;

pub struct Vm {
call_stack: Vec<CallFrame>,
stack: Vec<Value>,
Expand Down
5 changes: 5 additions & 0 deletions crates/saft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ impl Saft {
pub fn new_with_std() -> Self {
let mut saft = Self::new();
saft.add_native(saft_bytecode::natives::print);
saft.add_native(saft_bytecode::natives::sin);
saft.add_native(saft_bytecode::natives::cos);
saft.add_native(saft_bytecode::natives::reverse_bits);
saft.add_native(saft_bytecode::natives::hypot);
saft.add_native(saft_bytecode::natives::fib);
saft
}

Expand Down
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"

0 comments on commit 8624c1b

Please sign in to comment.