From 4992213f216645e771c206455a58709b6b5ee842 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Sat, 16 Apr 2022 00:44:52 +0200 Subject: [PATCH] add fields to OOP --- bootstrap/ISBPL.java | 29 +++++++++++++++++++++++++++-- std.isbpl | 3 ++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/bootstrap/ISBPL.java b/bootstrap/ISBPL.java index 16668c6..cbf8048 100644 --- a/bootstrap/ISBPL.java +++ b/bootstrap/ISBPL.java @@ -850,7 +850,7 @@ public class ISBPL { } }; break; - case "def": + case "deffunc": func = (stack) -> { ISBPLObject str = stack.pop(); ISBPLObject callable = stack.pop(); @@ -868,7 +868,19 @@ public class ISBPL { callable.checkType(getType("func")); ISBPLType t = types.get((int) type.object); String s = toJavaString(str); - t.methods.put(s, (ISBPLCallable) callable.object); + addFunction(t, s, (ISBPLCallable) callable.object); + }; + break; + case "deffield": + func = (stack) -> { + ISBPLObject type = stack.pop(); + ISBPLObject str = stack.pop(); + type.checkType(getType("int")); + ISBPLType t = types.get((int) type.object); + String s = toJavaString(str); + Object var = new Object(); + addFunction(t, s, (stack1) -> stack1.push(t.varget(stack1.pop()).get(var))); + addFunction(t, "=" + s, (stack1) -> t.varget(stack1.pop()).put(var, stack1.pop())); }; break; default: @@ -877,6 +889,11 @@ public class ISBPL { } addFunction(name, func); } + + public void addFunction(ISBPLType type, String name, ISBPLCallable callable) { + type.methods.put(name, callable); + type.methods.put("&" + name, stack -> stack.push(new ISBPLObject(getType("func"), callable))); + } public void addFunction(String name, ISBPLCallable callable) { functionStack.get().peek().put(name, callable); @@ -1201,10 +1218,18 @@ class ISBPLType { int id = gid++; String name; HashMap methods = new HashMap<>(); + HashMap> vars = new HashMap<>(); public ISBPLType(String name) { this.name = name; } + + public HashMap varget(ISBPLObject o) { + if(!vars.containsKey(o)) { + vars.put(o, new HashMap<>()); + } + return vars.get(o); + } @Override public boolean equals(Object o) { diff --git a/std.isbpl b/std.isbpl index 042c2d5..72aa6c2 100644 --- a/std.isbpl +++ b/std.isbpl @@ -64,8 +64,9 @@ native % native ^ native fcall -native def +native deffunc native defmethod +native deffield func ++ { 1 + } func -- { 1 - }