mech_interpreter/stdlib/access/
table.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#[macro_use]
use crate::stdlib::*;
use crate::stdlib::access::*;

// Table Access ---------------------------------------------------------------

macro_rules! impl_col_access_fxn {
    ($fxn_name:ident, $vector_size:ident, $out_type:ty) => {
      #[derive(Debug)]
      struct $fxn_name {
        source: Matrix<Value>,
        out: Ref<$vector_size<$out_type>>,
      }
      impl MechFunction for $fxn_name {
        fn solve(&self) {
          let out_ptr = self.out.as_ptr();
          unsafe { 
            for i in 1..=self.source.shape()[0] {
              paste! {
                (*out_ptr)[i-1] = self.source.index1d(i).[<as_ $out_type:lower>]().unwrap().borrow().clone();
              }
            }
          }
        }
        fn out(&self) -> Value { self.out.to_value() }
        fn to_string(&self) -> String { format!("{:#?}", self) }
      }
    }
  }
  
  macro_rules! impl_col_access_fxn_shapes {
    ($type:ident) => {
      paste!{
        impl_col_access_fxn!([<TableAccessCol $type:camel M1>], Matrix1, [<$type>]);
        impl_col_access_fxn!([<TableAccessCol $type:camel V2>], Vector2, [<$type>]);
        impl_col_access_fxn!([<TableAccessCol $type:camel V3>], Vector3, [<$type>]);
        impl_col_access_fxn!([<TableAccessCol $type:camel V4>], Vector4, [<$type>]);
        impl_col_access_fxn!([<TableAccessCol $type:camel VD>], DVector, [<$type>]);
      }
    }
  }
  
  impl_col_access_fxn_shapes!(bool);
  impl_col_access_fxn_shapes!(i8);
  impl_col_access_fxn_shapes!(i16);
  impl_col_access_fxn_shapes!(i32);
  impl_col_access_fxn_shapes!(i64);
  impl_col_access_fxn_shapes!(i128);
  impl_col_access_fxn_shapes!(u8);
  impl_col_access_fxn_shapes!(u16);
  impl_col_access_fxn_shapes!(u32);
  impl_col_access_fxn_shapes!(u64);
  impl_col_access_fxn_shapes!(u128);
  impl_col_access_fxn_shapes!(F32);
  impl_col_access_fxn_shapes!(F64);
  
  macro_rules! impl_access_column_match_arms {
    ($arg:expr, $($lhs_type:ident, $($default:expr),+);+ $(;)?) => {
      paste!{
        match $arg {
          (Value::Record(rcrd),Value::Id(k)) => {
            match rcrd.data.get(&k) {
              Some(value) => Ok(Box::new(RecordAccess{source: value.clone()})),
              _ => return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::UndefinedField(k)}),
            }
          }
          (Value::Table(tbl),Value::Id(k)) => {
            let key = Value::Id(k);
            match (tbl.data.get(&key),tbl.rows) {
              $(
                $(
                  (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)) })),
                )+
              )+
              _ => return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::UndefinedField(k)}),
            }
          }
          x => Err(MechError{file: file!().to_string(),  tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::UnhandledFunctionArgumentKind }),
        }
      }
    }
  }
  
  fn impl_access_column_fxn(source: Value, key: Value) -> Result<Box<dyn MechFunction>, MechError> {
    impl_access_column_match_arms!(
      (source,key),
      Bool,false;
      I8,i8::zero();
      I16,i16::zero();
      I32,i32::zero();
      I64,i64::zero();
      I128,i128::zero();
      U8,u8::zero();
      U16,u16::zero();
      U32,u32::zero();
      U64,u64::zero();
      U128,u128::zero();
      F32,F32::zero();
      F64,F64::zero();
    )
  }
  
  pub struct AccessColumn {}
  impl NativeFunctionCompiler for AccessColumn {
    fn compile(&self, arguments: &Vec<Value>) -> MResult<Box<dyn MechFunction>> {
      if arguments.len() <= 1 {
        return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::IncorrectNumberOfArguments});
      }
      let tbl = arguments[0].clone();
      let key = arguments[1].clone();
      match impl_access_column_fxn(tbl.clone(), key.clone()) {
        Ok(fxn) => Ok(fxn),
        Err(_) => {
          match (tbl,&key) {
            (Value::MutableReference(tbl),_) => { impl_access_column_fxn(tbl.borrow().clone(), key.clone()) }
            x => Err(MechError{file: file!().to_string(),  tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::UnhandledFunctionArgumentKind }),
          }
        }
      }
    }
  }
  
  // Table Access Swizzle -------------------------------------------------------
  
  #[derive(Debug)]
  struct RecordAccessSwizzle {
    source: Value,
  }
  
  impl MechFunction for RecordAccessSwizzle {
    fn solve(&self) {
      ()
    }
    fn out(&self) -> Value { self.source.clone() }
    fn to_string(&self) -> String { format!("{:#?}", self) }
  }
  
  #[derive(Debug)]
  struct TableAccessSwizzle {
    out: Value,
  }
  
  impl MechFunction for TableAccessSwizzle {
    fn solve(&self) {
      ()
    }
    fn out(&self) -> Value { self.out.clone() }
    fn to_string(&self) -> String { format!("{:#?}", self) }
  }
  
  pub struct AccessSwizzle {}
  impl NativeFunctionCompiler for AccessSwizzle {
    fn compile(&self, arguments: &Vec<Value>) -> MResult<Box<dyn MechFunction>> {
      if arguments.len() < 3 {
        return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::IncorrectNumberOfArguments});
      }
      let keys = &arguments.clone().split_off(1);
      let src = &arguments[0];
      match src {
        Value::Record(rcrd) => {
          let mut values = vec![];
          for key in keys {
            let k = key.as_usize().unwrap() as u64;
            match rcrd.data.get(&k) {
              Some(value) => values.push(value.clone()),
              None => { return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::UndefinedField(k)});}
            }
          }
          Ok(Box::new(RecordAccessSwizzle{source: Value::Tuple(MechTuple::from_vec(values))}))
        }
        Value::Table(tbl) => {
          let mut elements = vec![];
          for k in keys {
            match tbl.data.get(k) {
              Some((kind, mat_values)) => {
                elements.push(Box::new(mat_values.to_value()));
              }
              None => { return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::UndefinedField(*k.as_u64().unwrap().borrow())});}
            }
          }
          let tuple = Value::Tuple(MechTuple{elements});
          Ok(Box::new(TableAccessSwizzle{out: tuple}))
        }
        Value::MutableReference(r) => match &*r.borrow() {
          Value::Record(rcrd) => {
            let mut values = vec![];
            for key in keys {
              let k = key.as_usize().unwrap() as u64;
              match rcrd.data.get(&k) {
                Some(value) => values.push(value.clone()),
                None => { return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::UndefinedField(k)});}
              }
            }
            Ok(Box::new(RecordAccessSwizzle{source: Value::Tuple(MechTuple::from_vec(values))}))
          }
          Value::Table(tbl) => {
            let mut elements = vec![];
            for k in keys {
              match tbl.data.get(k) {
                Some((kind, mat_values)) => {
                  elements.push(Box::new(mat_values.to_value()));
                }
                None => { return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::UndefinedField(*k.as_u64().unwrap().borrow())});}
              }
            }
            let tuple = Value::Tuple(MechTuple{elements});
            Ok(Box::new(TableAccessSwizzle{out: tuple}))
          }
          _ => todo!(),
        }
        _ => todo!(),
      }
    }
  }