improve type system greatly

This commit is contained in:
Daniella / Tove 2022-05-14 16:10:47 +02:00
parent 2458fad5ff
commit 86bbb04219
2 changed files with 72 additions and 12 deletions

View file

@ -209,6 +209,11 @@ public class ISBPL {
// Create type // Create type
ISBPLType type = registerType(typename); ISBPLType type = registerType(typename);
for(; !words[idx].equals("{"); idx++) {
ISBPLType t = getType(words[idx]);
if(!type.superTypes.contains(t))
type.superTypes.add(t);
}
AtomicInteger i = new AtomicInteger(idx); AtomicInteger i = new AtomicInteger(idx);
String[] words2 = readBlock(i, words, file); String[] words2 = readBlock(i, words, file);
boolean definingMethods = false; boolean definingMethods = false;
@ -504,6 +509,11 @@ public class ISBPL {
stack.push(toISBPLString(types.get(((int) i.object)).name)); stack.push(toISBPLString(types.get(((int) i.object)).name));
}; };
break; break;
case "typeid":
func = (stack) -> {
stack.push(new ISBPLObject(getType("int"), getType(toJavaString(stack.pop())).id));
};
break;
case "gettype": case "gettype":
func = (Stack<ISBPLObject> stack) -> { func = (Stack<ISBPLObject> stack) -> {
ISBPLObject o = stack.pop(); ISBPLObject o = stack.pop();
@ -966,6 +976,17 @@ public class ISBPL {
addFunction(t, "=" + s, (stack1) -> t.varget(stack1.pop()).put(var, stack1.pop())); addFunction(t, "=" + s, (stack1) -> t.varget(stack1.pop()).put(var, stack1.pop()));
}; };
break; break;
case "defsuper":
func = (stack) -> {
ISBPLObject type = stack.pop();
ISBPLObject otherType = stack.pop();
type.checkType(getType("int"));
otherType.checkType(getType("int"));
ISBPLType t = types.get((int) type.object);
ISBPLType s = types.get((int) otherType.object);
t.superTypes.add(s);
};
break;
case "callmethod": case "callmethod":
func = (stack) -> { func = (stack) -> {
ISBPLObject obj = stack.pop(); ISBPLObject obj = stack.pop();
@ -1014,7 +1035,11 @@ public class ISBPL {
case "mkinstance": case "mkinstance":
func = (stack) -> { func = (stack) -> {
ISBPLObject type = stack.pop(); ISBPLObject type = stack.pop();
stack.push(new ISBPLObject(types.get((int) type.toLong()), new Object())); ISBPLType t = types.get((int) type.toLong());
stack.push(new ISBPLObject(t, new Object()));
if(t.methods.containsKey("construct")) {
t.methods.get("construct").call(stack);
}
}; };
break; break;
default: default:
@ -1583,7 +1608,15 @@ public class ISBPL {
} }
} }
words.add(word.toString()); words.add(word.toString());
return words.toArray(new String[0]);
ArrayList<String> cleanWords = new ArrayList<>();
for(int i = 0; i < words.size(); i++) {
if(!words.get(i).isEmpty()) {
cleanWords.add(words.get(i));
}
}
return cleanWords.toArray(new String[0]);
} }
private String cleanCode(String code) { private String cleanCode(String code) {
@ -1795,24 +1828,33 @@ class ISBPLObject {
} }
public void checkType(ISBPLType wanted) { public void checkType(ISBPLType wanted) {
if(wanted.id != type.id) { Queue<ISBPLType> types = new LinkedList<>();
throw new ISBPLError("IncompatibleTypes", "Incompatible types: " + type.name + " - " + wanted.name); types.add(type);
while (!types.isEmpty()) {
type = types.poll();
types.addAll(type.superTypes);
if(type.id == wanted.id) {
return;
}
} }
throw new ISBPLError("IncompatibleTypes", "Incompatible types: " + type.name + " - " + wanted.name);
} }
public void checkTypeMulti(ISBPLType... wanted) { public void checkTypeMulti(ISBPLType... wanted) {
int f = -1;
StringBuilder wantedNames = new StringBuilder(); StringBuilder wantedNames = new StringBuilder();
for (int i = 0 ; i < wanted.length ; i++) { for (int i = 0 ; i < wanted.length ; i++) {
wantedNames.append(" ").append(wanted[i].name); wantedNames.append(" ").append(wanted[i].name);
if(wanted[i].id == type.id) { Queue<ISBPLType> types = new LinkedList<>();
f = i; types.add(type);
break; while (!types.isEmpty()) {
type = types.poll();
types.addAll(type.superTypes);
if(type.id == wanted[i].id) {
return;
}
} }
} }
if(f == -1) { throw new ISBPLError("IncompatibleTypes", "Incompatible types: " + type.name + " - " + wantedNames.substring(1));
throw new ISBPLError("IncompatibleTypes", "Incompatible types: " + type.name + " - " + wantedNames.substring(1));
}
} }
@Override @Override

View file

@ -45,6 +45,7 @@ native mktype
native mkinstance native mkinstance
native getos native getos
native typename native typename
native typeid
native gettype native gettype
native settype native settype
"try and do are keywords, not functions" # "try and do are keywords, not functions" #
@ -70,6 +71,7 @@ native fcall
native deffunc native deffunc
native defmethod native defmethod
native deffield native deffield
native defsuper
native delete "deletes all field values from an object" # native delete "deletes all field values from an object" #
native callmethod native callmethod
@ -82,10 +84,14 @@ func ) { }
func ! { dup } func ! { dup }
"The TYPE_ prefix is deprecated, but will be kept for backwards compatibility." #
"int must be registered first." # "int must be registered first." #
def TYPE_INT construct int { def TYPE_INT construct int {
# "no fields needed" # "no fields needed"
; ;
construct {
pop 0
}
new { new {
"int has the new method because all types are ints" # "int has the new method because all types are ints" #
mkinstance mkinstance
@ -99,9 +105,13 @@ def TYPE_DOUBLE "double" mktype =TYPE_DOUBLE
def TYPE_NULL "null" mktype =TYPE_NULL def TYPE_NULL "null" mktype =TYPE_NULL
native null native null
def TYPE_FUNCTION "func" mktype =TYPE_FUNCTION def TYPE_FUNCTION "func" mktype =TYPE_FUNCTION
def Function TYPE_FUNCTION =Function
def TYPE_ARRAY construct array { def TYPE_ARRAY construct array {
; ;
construct {
pop anew
}
foreach { foreach {
def this =this def this =this
def lambda =lambda def lambda =lambda
@ -109,9 +119,17 @@ def TYPE_ARRAY construct array {
while { i this alen lt } { this i aget lambda fcall i ++ =i } while { i this alen lt } { this i aget lambda fcall i ++ =i }
} }
} =TYPE_ARRAY } =TYPE_ARRAY
def TYPE_STRING "string" mktype =TYPE_STRING def Array TYPE_ARRAY =Array
def TYPE_STRING construct string array {
;
_array {
Array settype
}
} =TYPE_STRING
def String TYPE_STRING =String
def TYPE_JIO construct jio { def TYPE_JIO construct jio {
jioclass
; ;
class { class {
pop pop