europalang / Europa-Lang

A fun and simple language with NO classes whatsoever!
MIT License
22 stars 3 forks source link

Implementing References #20

Closed cursorweb closed 3 years ago

cursorweb commented 3 years ago

As arrays and maps become increasingly hard to implement due to a big problem: references.

Take this example:

var a = {{ array: [1, 2, 3] }};
a."array"[1] = 3;

(figure 1)

at the moment with what we have, implementing this will be pretty much impossible, albeit I haven't implemented it yet.

Also take this planned example:

var a = 5, b = 6;

fn swap(ref a, ref b) {
  var t_a = cpy a, t_b = cpy b;

  a = t_b;
  b = t_a;
}

(figure 2)

Proposed solutions

References is a 'type', and it is:

Ref(Token)

where Token is the variable where the original data is stored.

Pros: figure 2 Cons: figure 1

References are in a new HashMap called the 'heap'. They include values that can get mutated.

Pros: Solves both figures 1 and 2 if implemented correctly. Cons: Very very hard to implement correctly

We make a mutable reference of the environment and just mutate them around Pros: Solves both figures 1 and 2 if implemented correctly. Cons: The ref type still might be needed

19wintersp commented 3 years ago

Why can't you just store this as a normal type in the regular variable store, and have that type be

Ref(Arc<Type>)

so when an assign is made to a type of Ref, it will instead mutate the inner type? There are a lot of problems with doing this, and I'd also like to note that Rust allows mutation of &mut T's with this syntax: (untested, probably some kind of error)

let mut foo = 1;
let foo_ref = &mut foo;
*foo_ref = 2; // note the * here
assert_eq!(foo, 2);

This is important because it distinguishes between mutating the (mutably) borrowed data (of foo) and changing what foo_ref is pointing to. I suppose the equivalent would be

ref a = t_a;

?

You'll need to ponder the implications of this syntax.

19wintersp commented 3 years ago

Amazingly, that Rust code worked first try :D

cursorweb commented 3 years ago

amazing, I'll look into it!

cursorweb commented 3 years ago

good bye stupid bug

darkdarcool commented 3 years ago

Hahahah