Skip to content

Commit 1aac75d

Browse files
committed
rt: Small modifications made to the runtime values.
1 parent 5a51b81 commit 1aac75d

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public PiccodeArray(List<PiccodeValue> nodes) {
1515
this.nodes = nodes;
1616
}
1717

18+
1819
@Override
1920
public String toString() {
2021
StringBuilder sb = new StringBuilder();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @author hexaredecimal
1212
*/
1313
public class PiccodeClosure implements PiccodeValue {
14-
14+
1515
List<Arg> params; // Full list of args (with name and default val)
1616
Map<String, PiccodeValue> appliedArgs; // Applied named args
1717
int positionalIndex; // How many positional args have been applied so far

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @author hexaredecimal
1111
*/
1212
public class PiccodeModule implements PiccodeValue{
13+
1314
public String name;
1415
public List<Ast> nodes;
1516

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @author hexaredecimal
99
*/
1010
public class PiccodeString implements PiccodeValue {
11+
public static final Type TYPE = Type.STRING;
1112
private String string;
1213

1314
public PiccodeString(String str) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* @author hexaredecimal
77
*/
88
public class PiccodeUnit implements PiccodeValue {
9+
910
@Override
1011
public Object raw() {
1112
return Void.TYPE;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
11
package org.piccode.rt;
22

3+
import com.github.tomaslanger.chalk.Chalk;
4+
import org.piccode.ast.Ast;
5+
36
/**
47
*
58
* @author hexaredecimal
69
*/
710
public interface PiccodeValue {
811
public Object raw();
912
public String type();
13+
14+
public static void verifyType(Ast caller, PiccodeValue value, Type type) {
15+
var typeOfValue = Type.of(value);
16+
if (typeOfValue != type) {
17+
throw new PiccodeException(
18+
caller.file,
19+
caller.line,
20+
caller.column,
21+
"Invalid type. Expected type " + Chalk.on(type.toString()).green()
22+
+ " but got " + Chalk.on(typeOfValue.toString()).red()
23+
);
24+
}
25+
}
26+
27+
public static enum Type {
28+
NUMBER,
29+
STRING,
30+
BOOLEAN,
31+
ARRAY,
32+
TYPLE,
33+
OBJECT,
34+
CLOSURE,
35+
MODULE,
36+
UNIT,
37+
VALUE,
38+
NATIVEFUNCTION;
39+
40+
public static Type of(PiccodeValue value) {
41+
if (value instanceof PiccodeNumber ) return NUMBER;
42+
if (value instanceof PiccodeString ) return STRING;
43+
if (value instanceof PiccodeBoolean) return BOOLEAN;
44+
if (value instanceof PiccodeObject ) return OBJECT;
45+
if (value instanceof PiccodeTuple ) return TYPLE;
46+
if (value instanceof PiccodeClosure) return CLOSURE;
47+
return VALUE;
48+
}
49+
}
1050
}

0 commit comments

Comments
 (0)