mech_interpreter/stdlib/access/
tuple.rs1#[macro_use]
2use crate::stdlib::*;
3
4#[derive(Debug)]
7struct TupleAccessElement {
8 out: Value,
9}
10
11impl MechFunctionImpl for TupleAccessElement {
12 fn solve(&self) {
13 ()
14 }
15 fn out(&self) -> Value { self.out.clone() }
16 fn to_string(&self) -> String { format!("{:#?}", self) }
17}
18#[cfg(feature = "compiler")]
19impl MechFunctionCompiler for TupleAccessElement {
20 fn compile(&self, ctx: &mut CompileCtx) -> MResult<Register> {
21 todo!();
22 }
23}
24
25pub struct TupleAccess {}
26impl NativeFunctionCompiler for TupleAccess{
27 fn compile(&self, arguments: &Vec<Value>) -> MResult<Box<dyn MechFunction>> {
28 if arguments.len() < 2 {
29 return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::IncorrectNumberOfArguments});
30 }
31 let ix = &arguments[1];
32 let src = &arguments[0];
33 match (src,ix) {
34 (Value::Tuple(tpl), Value::Index(ix)) => {
35 let ix_brrw = ix.borrow();
36 if *ix_brrw > tpl.elements.len() || *ix_brrw < 1 {
37 return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::IndexOutOfBounds});
38 }
39 let element = tpl.elements[*ix_brrw - 1].clone();
40 let new_fxn = TupleAccessElement{ out: *element };
41 Ok(Box::new(new_fxn))
42 },
43 (Value::MutableReference(tpl), Value::Index(ix)) => {
44 match &*tpl.borrow() {
45 Value::Tuple(ref tpl) => {
46 let ix_brrw = ix.borrow();
47 if *ix_brrw > tpl.elements.len() || *ix_brrw < 1 {
48 return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::IndexOutOfBounds});
49 }
50 let element = tpl.elements[*ix_brrw - 1].clone();
51 let new_fxn = TupleAccessElement{ out: *element };
52 Ok(Box::new(new_fxn))
53 },
54 _ => Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::UnhandledFunctionArgumentKind }),
55 }
56 },
57 _ => todo!(),
58 }
59 }
60}