Skip to content

Commit 3d46c94

Browse files
committed
rt: Adds a runtime value for references and a module
1 parent e6d06c3 commit 3d46c94

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

src/main/java/org/piccode/rt/PiccodeReference.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,35 @@
77
*
88
* @author hexaredecimal
99
*/
10-
public class PiccodeString implements PiccodeValue {
10+
public class PiccodeReference implements PiccodeValue {
1111
public static final Type TYPE = Type.STRING;
12-
private String string;
12+
private Object ref;
1313

14-
public PiccodeString(String str) {
15-
this.string = str;
14+
public PiccodeReference(Object ref) {
15+
this.ref = ref;
1616
}
1717

1818
@Override
1919
public Object raw() {
20-
return string;
20+
return this;
2121
}
2222

23+
public Object deref() {
24+
return ref;
25+
}
26+
27+
public void setRef(Object value) {
28+
this.ref = value;
29+
}
2330
@Override
2431
public String toString() {
25-
return string;
32+
return "&Ref<" + ref + ">";
2633
}
2734

2835
@Override
2936
public int hashCode() {
3037
int hash = 5;
31-
hash = 47 * hash + Objects.hashCode(this.string);
38+
hash = 47 * hash + Objects.hashCode(this.ref);
3239
return hash;
3340
}
3441

@@ -43,8 +50,8 @@ public boolean equals(Object obj) {
4350
if (getClass() != obj.getClass()) {
4451
return false;
4552
}
46-
final PiccodeString other = (PiccodeString) obj;
47-
return Objects.equals(this.string, other.string);
53+
final PiccodeReference other = (PiccodeReference) obj;
54+
return Objects.equals(this.ref, other.ref);
4855
}
4956

5057
@Override

src/main/java/org/piccode/rt/PiccodeValue.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public static enum Type {
5454
ARRAY,
5555
TYPLE,
5656
OBJECT,
57+
REFERENCE,
5758
CLOSURE,
5859
MODULE,
5960
UNIT,
@@ -69,6 +70,7 @@ public static Type of(PiccodeValue value) {
6970
if (value instanceof PiccodeArray ) return ARRAY;
7071
if (value instanceof PiccodeClosure) return CLOSURE;
7172
if (value instanceof PiccodeUnit ) return UNIT;
73+
if (value instanceof PiccodeReference) return REFERENCE;
7274
if (value instanceof NativeFunction) return NATIVEFUNCTION;
7375

7476
System.out.println(value + " : " + value.getClass());

src/main/java/org/piccode/rt/modules/PiccodeRefModule.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.piccode.rt.Context;
77
import org.piccode.rt.PiccodeArray;
88
import org.piccode.rt.PiccodeNumber;
9+
import org.piccode.rt.PiccodeReference;
910
import org.piccode.rt.PiccodeTuple;
1011
import org.piccode.rt.PiccodeValue;
1112
import org.piccode.rt.PiccodeValue.Type;
@@ -15,34 +16,25 @@
1516
*
1617
* @author hexaredecimal
1718
*/
18-
public class PiccodeTupleModule {
19+
public class PiccodeRefModule {
1920
public static void addFunctions() {
2021

21-
NativeFunctionFactory.create("tuplesize", List.of("tuple"), (args, namedArgs, frame) -> {
22+
NativeFunctionFactory.create("set_ref", List.of("ref", "value"), (args, namedArgs, frame) -> {
2223
var ctx = frame == null ?
2324
Context.top
2425
: Context.getContextAt(frame);
2526
var caller = ctx.getTopFrame().caller;
2627

27-
var tup = namedArgs.get("tuple");
28-
PiccodeValue.verifyType(caller, tup, Type.TYPLE);
29-
var arr = ((PiccodeTuple) tup).array().length;
30-
return new PiccodeNumber("" + arr);
31-
}, null);
32-
33-
NativeFunctionFactory.create("tupletoarray", List.of("tuple"), (args, namedArgs, frame) -> {
34-
var ctx = frame == null ?
35-
Context.top
36-
: Context.getContextAt(frame);
37-
var caller = ctx.getTopFrame().caller;
28+
var ref = namedArgs.get("ref");
29+
var value = namedArgs.get("value");
30+
PiccodeValue.verifyType(caller, ref, Type.REFERENCE);
3831

39-
var tup = namedArgs.get("tuple");
40-
PiccodeValue.verifyType(caller, tup, Type.TYPLE);
41-
var arr = ((PiccodeTuple) tup).array();
42-
var list = new ArrayList<PiccodeValue>();
43-
list.addAll(Arrays.asList(arr));
44-
return new PiccodeArray(list);
32+
var obj = (PiccodeReference) ref;
33+
obj.setRef(value);
34+
35+
return obj;
4536
}, null);
4637

38+
4739
}
4840
}

0 commit comments

Comments
 (0)