Skip to content

Commit 737f548

Browse files
committed
antlr4: Fixed the support for reference values
1 parent c7263b2 commit 737f548

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

src/main/antlr4/PiccodeScript.g4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ unary:
101101
| SUB expr
102102
| RETURN_TOK expr
103103
| TILDE expr
104-
| BAND expr;
104+
| BAND expr
105+
| MUL expr;
105106

106107
if_expr:
107108
IF expr expr (ELSE expr)?;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
package org.piccode.rt;
3+
4+
import java.util.Objects;
5+
6+
/**
7+
*
8+
* @author hexaredecimal
9+
*/
10+
public class PiccodeString implements PiccodeValue {
11+
public static final Type TYPE = Type.STRING;
12+
private String string;
13+
14+
public PiccodeString(String str) {
15+
this.string = str;
16+
}
17+
18+
@Override
19+
public Object raw() {
20+
return string;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return string;
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
int hash = 5;
31+
hash = 47 * hash + Objects.hashCode(this.string);
32+
return hash;
33+
}
34+
35+
@Override
36+
public boolean equals(Object obj) {
37+
if (this == obj) {
38+
return true;
39+
}
40+
if (obj == null) {
41+
return false;
42+
}
43+
if (getClass() != obj.getClass()) {
44+
return false;
45+
}
46+
final PiccodeString other = (PiccodeString) obj;
47+
return Objects.equals(this.string, other.string);
48+
}
49+
50+
@Override
51+
public String type() {
52+
return "String";
53+
}
54+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.piccode.rt.modules;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import org.piccode.rt.Context;
7+
import org.piccode.rt.PiccodeArray;
8+
import org.piccode.rt.PiccodeNumber;
9+
import org.piccode.rt.PiccodeTuple;
10+
import org.piccode.rt.PiccodeValue;
11+
import org.piccode.rt.PiccodeValue.Type;
12+
13+
14+
/**
15+
*
16+
* @author hexaredecimal
17+
*/
18+
public class PiccodeTupleModule {
19+
public static void addFunctions() {
20+
21+
NativeFunctionFactory.create("tuplesize", List.of("tuple"), (args, namedArgs, frame) -> {
22+
var ctx = frame == null ?
23+
Context.top
24+
: Context.getContextAt(frame);
25+
var caller = ctx.getTopFrame().caller;
26+
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;
38+
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);
45+
}, null);
46+
47+
}
48+
}

0 commit comments

Comments
 (0)