Skip to content

Commit

Permalink
Merge branch 'v0.2-beta' into 'main'
Browse files Browse the repository at this point in the history
V0.2.19-beta

See merge request mech-lang/mech!82
  • Loading branch information
cmontella committed Nov 12, 2024
2 parents 8f7396c + 6493dcd commit 12b1ebe
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 172 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mech"
version = "0.2.18"
version = "0.2.19"
authors = ["Corey Montella <[email protected]>"]
description = "Mech is a reactive programming language for building robots, games, and animations."
documentation = "https://mech-lang.org/docs"
Expand All @@ -18,8 +18,8 @@ gitlab = { repository = "mech-lang/mech", branch = "main" }
maintenance = { status = "actively-developed" }

[dependencies]
mech-core = "0.2.18"
mech-syntax = "0.2.18"
mech-core = "0.2.19"
mech-syntax = "0.2.19"
#mech-program = "0.2.2"
#mech-utilities = "0.2.2"

Expand Down Expand Up @@ -69,7 +69,7 @@ mech-utilities = { path = 'src/utilities'}
mech-wasm = { path = 'src/wasm'}

[patch.'https://gitlab.com/mech-lang/core']
mech-core = { path = 'src/core', version = '0.2.18' }
mech-core = { path = 'src/core', version = '0.2.19' }

[patch.'https://gitlab.com/mech-lang/syntax']
mech-syntax = { path = 'src/syntax', version = '0.2.18' }
mech-syntax = { path = 'src/syntax', version = '0.2.19' }
2 changes: 1 addition & 1 deletion src/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mech-core"
version = "0.2.18"
version = "0.2.19"
authors = ["Corey Montella <[email protected]>"]
description = "The Mech language runtime."
documentation = "http://docs.mech-lang.org"
Expand Down
27 changes: 26 additions & 1 deletion src/core/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,39 @@ use std::time::{Instant};
use std::collections::VecDeque;
use mech_core::*;
//use mech_core::function::table;
use nalgebra::DMatrix;
use nalgebra::*;

use std::fmt::*;
use num_traits::*;
use std::ops::*;
extern crate time;

fn main() -> std::result::Result<(),MechError> {

let n = 10000;
//let x = new_ref(Matrix6::from_element(F64::new(1.0)));
let x = new_ref(DMatrix::from_element(n,4,F64::new(1.0)));

let source = F64::new(5.0);
let source_c = source.clone();

unsafe {
//let sink = x.as_ptr();
let sink = &mut *(x.as_ptr());
let mut column = sink.column_mut(0);
let now = Instant::now();
for _ in 0..1e6 as usize {
for i in 0..n {
//(*sink).column_mut(0)[i] = source_c;
column[i] = source_c;
}
}
let elapsed_time = now.elapsed();
let cycle_duration = elapsed_time.as_nanos() as f64;
println!("{:0.2?} ns", cycle_duration / 1000000.0);

}

/*
//let now = Instant::now();
let n = 1e7 as usize;
Expand Down
223 changes: 145 additions & 78 deletions src/core/src/stdlib/matrix.rs

Large diffs are not rendered by default.

120 changes: 60 additions & 60 deletions src/core/src/stdlib/mod.rs

Large diffs are not rendered by default.

47 changes: 23 additions & 24 deletions src/core/src/stdlib/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,26 @@ impl<T> MechFunction for RecordSet<T>

// Table Set ------------------------------------------------------------------

/*macro_rules! impl_col_set_fxn {
macro_rules! impl_col_set_fxn {
($fxn_name:ident, $vector_size:ident, $out_type:ty) => {
#[derive(Debug)]
struct $fxn_name {
source: Matrix<Value>,
out: Ref<$vector_size<$out_type>>,
source: Ref<$vector_size<$out_type>>,
sink: Ref<$vector_size<Value>>,
}
impl MechFunction for $fxn_name {
fn solve(&self) {
let out_ptr = self.out.as_ptr();
let source_ptr = self.source.as_ptr();
let sink_ptr = self.sink.as_ptr();
unsafe {
for i in 1..=self.source.shape()[0] {
for i in 0..(*source_ptr).len() {
paste! {
(*out_ptr)[i-1] = self.source.index1d(i).[<as_ $out_type:lower>]().unwrap().borrow().clone();
(*sink_ptr)[i] = Value::[<$out_type:camel>](new_ref((*source_ptr).index(i).clone()));
}
}
}
}
fn out(&self) -> Value { self.out.to_value() }
fn out(&self) -> Value { Value::MatrixValue(Matrix::$vector_size(self.sink.clone())) }
fn to_string(&self) -> String { format!("{:?}", self) }
}
}
Expand All @@ -96,11 +97,11 @@ impl<T> MechFunction for RecordSet<T>
macro_rules! impl_col_set_fxn_shapes {
($type:ident) => {
paste!{
impl_col_set_fxn!([<TableSetCol $type:camel M1>], Matrix1, [<$type>]);
impl_col_set_fxn!([<TableSetCol $type:camel V2>], Vector2, [<$type>]);
impl_col_set_fxn!([<TableSetCol $type:camel V3>], Vector3, [<$type>]);
impl_col_set_fxn!([<TableSetCol $type:camel V4>], Vector4, [<$type>]);
impl_col_set_fxn!([<TableSetCol $type:camel VD>], DVector, [<$type>]);
impl_col_set_fxn!([<TableSetCol $type:camel M1>], Matrix1, $type);
impl_col_set_fxn!([<TableSetCol $type:camel V2>], Vector2, $type);
impl_col_set_fxn!([<TableSetCol $type:camel V3>], Vector3, $type);
impl_col_set_fxn!([<TableSetCol $type:camel V4>], Vector4, $type);
impl_col_set_fxn!([<TableSetCol $type:camel VD>], DVector, $type);
}
}
}
Expand All @@ -117,7 +118,7 @@ impl_col_set_fxn_shapes!(u32);
impl_col_set_fxn_shapes!(u64);
impl_col_set_fxn_shapes!(u128);
impl_col_set_fxn_shapes!(F32);
impl_col_set_fxn_shapes!(F64);*/
impl_col_set_fxn_shapes!(F64);

macro_rules! impl_set_column_match_arms {
($arg:expr, $($lhs_type:ident, $($default:expr),+);+ $(;)?) => {
Expand All @@ -142,21 +143,19 @@ macro_rules! impl_set_column_match_arms {
_ => return Err(MechError{tokens: vec![], msg: file!().to_string(), id: line!(), kind: MechErrorKind::UndefinedField(k)}),
}
}
/*(Value::Table(tbl),source,Value::Id(k)) => {
(Value::Table(tbl),source,Value::Id(k)) => {
let key = Value::Id(k);
match (tbl.data.get(&key),tbl.rows) {
match (tbl.data.get(&key),tbl.rows,source) {
$(
$(
(Some((ValueKind::$lhs_type,value)),1) => Ok(Box::new([<TableAccessCol $lhs_type M1>]{source: value.clone(), out: new_ref(Matrix1::from_element($default)) })),
(Some((ValueKind::$lhs_type,value)),2) => Ok(Box::new([<TableAccessCol $lhs_type V2>]{source: value.clone(), out: new_ref(Vector2::from_element($default)) })),
(Some((ValueKind::$lhs_type,value)),3) => Ok(Box::new([<TableAccessCol $lhs_type V3>]{source: value.clone(), out: new_ref(Vector3::from_element($default)) })),
(Some((ValueKind::$lhs_type,value)),4) => Ok(Box::new([<TableAccessCol $lhs_type V4>]{source: value.clone(), out: new_ref(Vector4::from_element($default)) })),
(Some((ValueKind::$lhs_type,value)),n) => Ok(Box::new([<TableAccessCol $lhs_type VD>]{source: value.clone(), out: new_ref(DVector::from_element(n,$default)) })),
)+
(Some((ValueKind::$lhs_type,Matrix::Matrix1(sink))),1,Value::[<Matrix $lhs_type>](Matrix::Matrix1(source))) => Ok(Box::new([<TableSetCol $lhs_type M1>]{source: source.clone(), sink: sink.clone() })),
(Some((ValueKind::$lhs_type,Matrix::Vector2(sink))),2,Value::[<Matrix $lhs_type>](Matrix::Vector2(source))) => Ok(Box::new([<TableSetCol $lhs_type V2>]{source: source.clone(), sink: sink.clone() })),
(Some((ValueKind::$lhs_type,Matrix::Vector3(sink))),3,Value::[<Matrix $lhs_type>](Matrix::Vector3(source))) => Ok(Box::new([<TableSetCol $lhs_type V3>]{source: source.clone(), sink: sink.clone() })),
(Some((ValueKind::$lhs_type,Matrix::Vector4(sink))),4,Value::[<Matrix $lhs_type>](Matrix::Vector4(source))) => Ok(Box::new([<TableSetCol $lhs_type V4>]{source: source.clone(), sink: sink.clone() })),
(Some((ValueKind::$lhs_type,Matrix::DVector(sink))),n,Value::[<Matrix $lhs_type>](Matrix::DVector(source))) => Ok(Box::new([<TableSetCol $lhs_type VD>]{source: source.clone(), sink: sink.clone() })),
)+
_ => return Err(MechError{tokens: vec![], msg: file!().to_string(), id: line!(), kind: MechErrorKind::UndefinedField(k)}),
x => return Err(MechError{tokens: vec![], msg: file!().to_string(), id: line!(), kind: MechErrorKind::UndefinedField(k)}),
}
}*/
}
x => Err(MechError { tokens: vec![], msg: file!().to_string(), id: line!(), kind: MechErrorKind::UnhandledFunctionArgumentKind }),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/syntax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mech-syntax"
version = "0.2.18"
version = "0.2.19"
authors = ["Corey Montella <[email protected]>"]
description = "A toolchain for compiling textual syntax into Mech blocks."
documentation = "http://docs.mech-lang.org"
Expand All @@ -21,7 +21,7 @@ default = []
no-std = ["mech-core/no-std", "rlibc"]

[dependencies]
mech-core = "0.2.18"
mech-core = "0.2.19"

hashbrown = "0.15.1"
lazy_static = "1.5.0"
Expand Down
17 changes: 16 additions & 1 deletion src/syntax/benches/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@ use mech_core::interpreter::*;
use mech_syntax::parser::{parse};
use nalgebra::{Vector3, DVector, RowDVector, Matrix1, Matrix3, Matrix4, RowVector3, RowVector4, RowVector2, DMatrix, Rotation3, Matrix2x3, Matrix6, Matrix2};


#[bench]
fn set_column(b:&mut Bencher){
let s = r#"a := [1 2; 3 4]
a[:,1] = 4"#;
match parser::parse(&s) {
Ok(tree) => {
let mut intrp = Interpreter::new();
let result = intrp.interpret(&tree);
let fxn = &intrp.plan.borrow()[0];
b.iter(|| {
let result = fxn.solve();
});
}
_ => (),
}
}

#[bench]
fn matrix_multiply(b:&mut Bencher){
Expand Down
7 changes: 7 additions & 0 deletions src/syntax/tests/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,16 @@ test_interpreter!(interpret_set_value_slice,"x := [1 2 3 4]; x[[1 3]] = 42; x[1]
test_interpreter!(interpret_set_value_scalar_slice,"x := [1 2 3; 4 5 6; 7 8 9]; x[1,[1,3]] = 42; x[1] + x[7];", Value::F64(new_ref(F64::new(84.0))));
test_interpreter!(interpret_set_value_slice_slice,"x := [1 2 3; 5 6 7; 9 10 11]; x[1..3,1..3] = 42; x[1] + x[2] + x[4] + x[5]", Value::F64(new_ref(F64::new(168.0))));
test_interpreter!(interpret_set_value_all_slice,"x := [1 2 3; 5 6 7]; x[:,1..3] = 42; x[1] + x[2] + x[3] + x[4] + x[5] + x[6]", Value::F64(new_ref(F64::new(178.0))));
test_interpreter!(interpret_set_value_all_slice_vec,"x := [1;6]; x[:,1] = [4;5]; x[1] + x[2];", Value::F64(new_ref(F64::new(9.0))));
test_interpreter!(interpret_set_value_slice_all,"x := [1 2 3; 5 6 7]'; x[1..3,:] = 42; x[1] + x[2] + x[3] + x[4] + x[5] + x[6]", Value::F64(new_ref(F64::new(178.0))));
test_interpreter!(interpret_set_value_slice_vec,"x := [1 2 3 4]; x[1..=3] = [10 20 30]; x[1] + x[2] + x[3] + x[4]", Value::F64(new_ref(F64::new(64.0))));

test_interpreter!(interpret_set_record_field,"x := {a: 1, b: true}; x.a = 2; x.a;", Value::F64(new_ref(F64::new(2.0))));
test_interpreter!(interpret_set_record_field2,"x := {a: 1, b: true}; x.b = false; x.b;", Value::Bool(new_ref(false)));
test_interpreter!(interpret_set_record_field3,"x := {a: 1<u64>, b: true}; x.a = 2<u64>; x.a;", Value::U64(new_ref(2)));

test_interpreter!(interpret_set_table_col,"x := { x<f64> y<f64> | 1 2; 3 4 }; x.x = [42;46]; y := x.x; y[1] + y[2]", Value::F64(new_ref(F64::new(88.0))));
test_interpreter!(interpret_set_table_col2,"x := { x<f64> y<f64> | 1 2; 3 4; 5 6; 7 8}; x.x = [42;46;47;48]; y := x.x; y[1] + y[2] + y[3] + y[4]; ", Value::F64(new_ref(F64::new(183.0))));

test_interpreter!(interpret_set_logical,"x := [1 2 3]; ix := [true false true]; x[ix] = 4; x[1] + x[2] + x[3];", Value::F64(new_ref(F64::new(10.0))));
test_interpreter!(interpret_set_logical2,"x := [1 2 3 4]; ix := [true false true true]; x[ix] = 5; x[1] + x[2] + x[3] + x[4];", Value::F64(new_ref(F64::new(17.0))));

0 comments on commit 12b1ebe

Please sign in to comment.