Skip to content
This repository was archived by the owner on Jan 6, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ pub mod ast {
c.cpush32(n as u32);
}
_ => {
c.cpushop(VmOpcode::OP_PUSH64);
c.cpush64(n);
unimplemented!()
//c.cpushop(VmOpcode::OP_PUSH64);
//c.cpush64(n);
}
}
emit_end!(c, _smap_begin);
Expand Down Expand Up @@ -191,8 +192,8 @@ pub mod ast {
c.cpushop(VmOpcode::OP_PUSH8);
c.cpush8(self.exprs.len() as u8);
} else {
c.cpushop(VmOpcode::OP_PUSH64);
c.cpush64(self.exprs.len() as u64);
c.cpushop(VmOpcode::OP_PUSH32);
c.cpush32(self.exprs.len() as u32);
}
c.cpushop(VmOpcode::OP_ARRAY_LOAD);
emit_end!(c, _smap_begin);
Expand Down Expand Up @@ -315,8 +316,8 @@ pub mod ast {
c.cpushop(VmOpcode::OP_PUSH8);
c.cpush8(self.stmts.len() as u8);
} else {
c.cpushop(VmOpcode::OP_PUSH64);
c.cpush64(self.stmts.len() as u64);
c.cpushop(VmOpcode::OP_PUSH32);
c.cpush32(self.stmts.len() as u32);
}
c.cpushop(VmOpcode::OP_DICT_LOAD);
emit_end!(c, _smap_begin);
Expand Down
50 changes: 15 additions & 35 deletions src/hanayo/array.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Provides Array record for handling arrays
use std::cmp::Ordering;

use crate::vmbindings::cnativeval::{NativeValue, NativeValueType};
use crate::vmbindings::cnativeval::{NativeValue};
use crate::vmbindings::value::Value;
use crate::vmbindings::vm::Vm;

Expand All @@ -23,21 +23,21 @@ pub extern "C" fn constructor(cvm: *mut Vm, nargs: u16) {

#[hana_function()]
fn length(array: Value::Array) -> Value {
Value::Int(array.as_ref().len() as i64)
Value::Int(array.as_ref().len() as i32)
}

#[hana_function()]
fn insert_(array: Value::Array, pos: Value::Int, elem: Value::Any) -> Value {
array.as_mut().insert(pos as usize, elem.wrap());
Value::Int(array.as_ref().len() as i64)
Value::Int(array.as_ref().len() as i32)
}

#[hana_function()]
fn delete_(array: Value::Array, from_pos: Value::Int, nelems: Value::Int) -> Value {
array
.as_mut()
.drain((from_pos as usize)..((nelems as usize) + 1));
Value::Int(array.as_ref().len() as i64)
Value::Int(array.as_ref().len() as i32)
}

// stack manipulation
Expand All @@ -53,34 +53,21 @@ fn pop(array: Value::Array) -> Value {
}

extern "C" {
fn value_gt(result: *mut NativeValue, left: NativeValue, right: NativeValue);
fn value_lt(result: *mut NativeValue, left: NativeValue, right: NativeValue);
fn value_gt(left: NativeValue, right: NativeValue) -> NativeValue;
fn value_lt(left: NativeValue, right: NativeValue) -> NativeValue;
}

// sorting
fn value_cmp(left: &NativeValue, right: &NativeValue) -> Ordering {
let left = left.clone();
let right = right.clone();
let mut val = NativeValue {
data: 0,
r#type: NativeValueType::TYPE_NIL,
};

unsafe {
value_gt(&mut val, left, right);
}
if val.data == 1 {
return Ordering::Greater;
}

unsafe {
value_lt(&mut val, left, right);
if unsafe { value_gt(left, right) }.unwrap().int() == 1 {
Ordering::Greater
} else if unsafe { value_lt(left, right) }.unwrap().int() == 1 {
Ordering::Less
} else {
Ordering::Equal
}
if val.data == 1 {
return Ordering::Less;
}

Ordering::Equal
}

#[hana_function()]
Expand Down Expand Up @@ -151,21 +138,14 @@ fn reduce(array: Value::Array, fun: Value::Any, acc_: Value::Any) -> Value {

// search
extern "C" {
fn value_eq(result: *mut NativeValue, left: NativeValue, right: NativeValue);
fn value_eq(left: NativeValue, right: NativeValue) -> NativeValue;
}
#[hana_function()]
fn index(array: Value::Array, elem: Value::Any) -> Value {
let array = array.as_ref();
for i in 0..(array.len() - 1) {
let mut val = NativeValue {
data: 0,
r#type: NativeValueType::TYPE_NIL,
};
unsafe {
value_eq(&mut val, array[i], elem.wrap());
}
if val.data == 1 {
return Value::Int(i as i64);
if unsafe { value_eq(array[i], elem.wrap()) }.unwrap().int() == 1 {
return Value::Int(i as i32);
}
}
Value::Int(-1)
Expand Down
12 changes: 6 additions & 6 deletions src/hanayo/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn write(file: Value::Record, buf: Value::Str) -> Value {
let file = file.as_mut();
if let Some(field) = file.native_field.as_mut() {
let file = field.downcast_mut::<File>().unwrap();
Value::Int(file.write_all(buf.as_ref().as_bytes()).is_ok() as i64)
Value::Int(file.write_all(buf.as_ref().as_bytes()).is_ok() as i32)
} else {
Value::Int(0)
}
Expand All @@ -100,8 +100,8 @@ fn seek(file: Value::Record, pos: Value::Int) -> Value {
let file = file.as_mut();
if let Some(field) = file.native_field.as_mut() {
let file = field.downcast_mut::<File>().unwrap();
if let Ok(result) = file.seek(SeekFrom::Current(pos)) {
Value::Int(result as i64)
if let Ok(result) = file.seek(SeekFrom::Current(pos as i64)) {
Value::Int(result as i32)
} else {
Value::Int(-1)
}
Expand All @@ -116,7 +116,7 @@ fn seek_from_start(file: Value::Record, pos: Value::Int) -> Value {
if let Some(field) = file.native_field.as_mut() {
let file = field.downcast_mut::<File>().unwrap();
if let Ok(result) = file.seek(SeekFrom::Start(pos as u64)) {
Value::Int(result as i64)
Value::Int(result as i32)
} else {
Value::Int(-1)
}
Expand All @@ -130,8 +130,8 @@ fn seek_from_end(file: Value::Record, pos: Value::Int) -> Value {
let file = file.as_mut();
if let Some(field) = file.native_field.as_mut() {
let file = field.downcast_mut::<File>().unwrap();
if let Ok(result) = file.seek(SeekFrom::End(pos)) {
Value::Int(result as i64)
if let Ok(result) = file.seek(SeekFrom::End(pos as i64)) {
Value::Int(result as i32)
} else {
Value::Int(-1)
}
Expand Down
4 changes: 2 additions & 2 deletions src/hanayo/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use std::str::FromStr;
fn constructor(val: Value::Any) -> Value {
match val {
Value::Int(n) => Value::Int(n),
Value::Float(n) => Value::Int(n as i64),
Value::Str(s) => match i64::from_str(s.as_ref()) {
Value::Float(n) => Value::Int(n as i32),
Value::Str(s) => match i32::from_str(s.as_ref()) {
Ok(n) => Value::Int(n),
Err(_) => {
hana_raise!(vm, {
Expand Down
2 changes: 1 addition & 1 deletion src/hanayo/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn wait(process: Value::Record) -> Value {
match p.wait() {
Ok(e) => {
if let Some(code) = e.code() {
Value::Int(code as i64)
Value::Int(code as i32)
} else {
Value::Int(0)
}
Expand Down
12 changes: 6 additions & 6 deletions src/hanayo/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ pub extern "C" fn constructor(cvm: *mut Vm, nargs: u16) {
// length
#[hana_function()]
fn length(s: Value::Str) -> Value {
Value::Int(s.as_ref().graphemes(true).count() as i64)
Value::Int(s.as_ref().graphemes(true).count() as i32)
}
#[hana_function()]
fn bytesize(s: Value::Str) -> Value {
Value::Int(s.as_ref().len() as i64)
Value::Int(s.as_ref().len() as i32)
}

// check
#[hana_function()]
fn startswith(s: Value::Str, left: Value::Str) -> Value {
Value::Int(s.as_ref().starts_with(left.as_ref()) as i64)
Value::Int(s.as_ref().starts_with(left.as_ref()) as i32)
}
#[hana_function()]
fn endswith(s: Value::Str, left: Value::Str) -> Value {
Value::Int(s.as_ref().ends_with(left.as_ref()) as i64)
Value::Int(s.as_ref().ends_with(left.as_ref()) as i32)
}

// basic manip
Expand Down Expand Up @@ -141,7 +141,7 @@ fn index(s: Value::Str, needle: Value::Str) -> Value {
})
.next()
{
(idx_grapheme - 1) as i64
(idx_grapheme - 1) as i32
} else {
-1
}
Expand All @@ -164,7 +164,7 @@ fn chars(s: Value::Str) -> Value {
fn ord(s: Value::Str) -> Value {
let s = s.as_ref();
if let Some(ch) = s.chars().next() {
Value::Int(ch as i64)
Value::Int(ch as i32)
} else {
Value::Int(0)
}
Expand Down
8 changes: 4 additions & 4 deletions src/hanayo/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ fn since(left: Value::Record, right: Value::Record) -> Value {
fn secs(time: Value::Record) -> Value {
let tref = time.as_ref().native_field.as_ref().unwrap();
let time = tref.downcast_ref::<Duration>().unwrap();
Value::Int(time.as_secs() as i64)
Value::Int(time.as_secs() as i32)
}
#[hana_function()]
fn millis(time: Value::Record) -> Value {
let tref = time.as_ref().native_field.as_ref().unwrap();
let time = tref.downcast_ref::<Duration>().unwrap();
Value::Int(time.as_millis() as i64)
Value::Int(time.as_millis() as i32)
}
#[hana_function()]
fn micros(time: Value::Record) -> Value {
let tref = time.as_ref().native_field.as_ref().unwrap();
let time = tref.downcast_ref::<Duration>().unwrap();
Value::Int(time.as_micros() as i64)
Value::Int(time.as_micros() as i32)
}
#[hana_function()]
fn nanos(time: Value::Record) -> Value {
let tref = time.as_ref().native_field.as_ref().unwrap();
let time = tref.downcast_ref::<Duration>().unwrap();
Value::Int(time.as_nanos() as i64)
Value::Int(time.as_nanos() as i32)
}

// other
Expand Down
5 changes: 0 additions & 5 deletions src/vm/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ void *rrealloc(void *ptr, size_t nelems, size_t size, size_t new_size);
.length = n, \
}

#define array_free(array) \
do { \
free(array.data); \
} while (0)

#define array_push(array, element) \
do { \
if (array.length == array.capacity) { \
Expand Down
Loading