Skip to content

Commit 7a126bf

Browse files
committed
modules: Added more functions to the File module
1 parent 1fb3c79 commit 7a126bf

File tree

1 file changed

+167
-2
lines changed

1 file changed

+167
-2
lines changed

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

Lines changed: 167 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package org.piccode.rt.modules;
22

3+
import com.github.tomaslanger.chalk.Chalk;
34
import java.io.File;
5+
import java.io.IOException;
6+
import java.util.ArrayList;
47
import java.util.HashMap;
58
import java.util.List;
69
import org.piccode.rt.Context;
10+
import org.piccode.rt.PiccodeArray;
711
import org.piccode.rt.PiccodeBoolean;
12+
import org.piccode.rt.PiccodeException;
813
import org.piccode.rt.PiccodeNumber;
914
import org.piccode.rt.PiccodeObject;
1015
import org.piccode.rt.PiccodeString;
@@ -23,7 +28,7 @@ private static PiccodeValue makeFile(File fp) {
2328
obj.put("path", new PiccodeString(fp.getPath()));
2429
obj.put("parent", new PiccodeString(fp.getParent()));
2530
obj.put("exists", new PiccodeBoolean(fp.exists()));
26-
obj.put("file", new PiccodeBoolean(fp.isFile()));
31+
obj.put("isFile", new PiccodeBoolean(fp.isFile()));
2732
obj.put("hidden", new PiccodeBoolean(fp.isHidden()));
2833
return new PiccodeObject(obj);
2934
}
@@ -41,10 +46,170 @@ public static void addFunctions() {
4146

4247
var path = _path.raw().toString();
4348
var fp = new File(path);
44-
var id = Context.allocate(fp);
49+
Context.allocate(fp);
4550

4651
return makeFile(fp);
4752
}, null);
4853

54+
55+
NativeFunctionFactory.create("create", List.of("object"), (args, namedArgs, frame) -> {
56+
var ctx = frame == null
57+
? Context.top
58+
: Context.getContextAt(frame);
59+
60+
var scope = ctx.getTopFrame();
61+
var obj = namedArgs.get("object");
62+
PiccodeValue.verifyType(scope.caller, obj , Type.OBJECT);
63+
64+
var object = (PiccodeObject) obj;
65+
var hash = object.obj.get("hash");
66+
if (hash == null) {
67+
var node = scope.caller;
68+
throw new PiccodeException(node.file, node.line, node.column, "Value is not a native object");
69+
}
70+
71+
if (!(hash instanceof PiccodeNumber)) {
72+
var node = scope.caller;
73+
throw new PiccodeException(node.file, node.line, node.column, "Invalid native object. Hash code is not a number, found " + Chalk.on(hash.type()).red());
74+
}
75+
76+
var allocatedObject = Context.getObject((int) (double) hash.raw());
77+
if (allocatedObject == null) {
78+
var node = scope.caller;
79+
throw new PiccodeException(node.file, node.line, node.column, String.format("No object is allocated at 0x%04d", hash.raw()));
80+
}
81+
82+
if (!(allocatedObject instanceof File)) {
83+
var node = scope.caller;
84+
throw new PiccodeException(node.file, node.line, node.column, String.format("Object allocated at 0x%04d is not a File", hash.raw()));
85+
}
86+
87+
var file = (File) allocatedObject;
88+
try {
89+
file.createNewFile();
90+
return makeFile(file);
91+
} catch (IOException ex) {
92+
return obj;
93+
}
94+
}, null);
95+
96+
NativeFunctionFactory.create("delete", List.of("object"), (args, namedArgs, frame) -> {
97+
var ctx = frame == null
98+
? Context.top
99+
: Context.getContextAt(frame);
100+
101+
var scope = ctx.getTopFrame();
102+
var obj = namedArgs.get("object");
103+
PiccodeValue.verifyType(scope.caller, obj , Type.OBJECT);
104+
105+
var object = (PiccodeObject) obj;
106+
var hash = object.obj.get("hash");
107+
if (hash == null) {
108+
var node = scope.caller;
109+
throw new PiccodeException(node.file, node.line, node.column, "Value is not a native object");
110+
}
111+
112+
if (!(hash instanceof PiccodeNumber)) {
113+
var node = scope.caller;
114+
throw new PiccodeException(node.file, node.line, node.column, "Invalid native object. Hash code is not a number, found " + Chalk.on(hash.type()).red());
115+
}
116+
117+
var allocatedObject = Context.getObject((int) (double) hash.raw());
118+
if (allocatedObject == null) {
119+
var node = scope.caller;
120+
throw new PiccodeException(node.file, node.line, node.column, String.format("No object is allocated at 0x%04d", hash.raw()));
121+
}
122+
123+
if (!(allocatedObject instanceof File)) {
124+
var node = scope.caller;
125+
throw new PiccodeException(node.file, node.line, node.column, String.format("Object allocated at 0x%04d is not a File", hash.raw()));
126+
}
127+
128+
var file = (File) allocatedObject;
129+
file.delete();
130+
return makeFile(file);
131+
}, null);
132+
133+
NativeFunctionFactory.create("listfiles", List.of("object"), (args, namedArgs, frame) -> {
134+
var ctx = frame == null
135+
? Context.top
136+
: Context.getContextAt(frame);
137+
138+
var scope = ctx.getTopFrame();
139+
var obj = namedArgs.get("object");
140+
PiccodeValue.verifyType(scope.caller, obj , Type.OBJECT);
141+
142+
var object = (PiccodeObject) obj;
143+
var hash = object.obj.get("hash");
144+
if (hash == null) {
145+
var node = scope.caller;
146+
throw new PiccodeException(node.file, node.line, node.column, "Value is not a native object");
147+
}
148+
149+
if (!(hash instanceof PiccodeNumber)) {
150+
var node = scope.caller;
151+
throw new PiccodeException(node.file, node.line, node.column, "Invalid native object. Hash code is not a number, found " + Chalk.on(hash.type()).red());
152+
}
153+
154+
var allocatedObject = Context.getObject((int) (double) hash.raw());
155+
if (allocatedObject == null) {
156+
var node = scope.caller;
157+
throw new PiccodeException(node.file, node.line, node.column, String.format("No object is allocated at 0x%04d", hash.raw()));
158+
}
159+
160+
if (!(allocatedObject instanceof File)) {
161+
var node = scope.caller;
162+
throw new PiccodeException(node.file, node.line, node.column, String.format("Object allocated at 0x%04d is not a File", hash.raw()));
163+
}
164+
165+
var elements = new ArrayList<PiccodeValue>();
166+
var file = (File) allocatedObject;
167+
for (var fp: file.listFiles()) {
168+
var _file = makeFile(fp);
169+
elements.add(_file);
170+
}
171+
return new PiccodeArray(elements);
172+
}, null);
173+
174+
NativeFunctionFactory.create("listpaths", List.of("object"), (args, namedArgs, frame) -> {
175+
var ctx = frame == null
176+
? Context.top
177+
: Context.getContextAt(frame);
178+
179+
var scope = ctx.getTopFrame();
180+
var obj = namedArgs.get("object");
181+
PiccodeValue.verifyType(scope.caller, obj , Type.OBJECT);
182+
183+
var object = (PiccodeObject) obj;
184+
var hash = object.obj.get("hash");
185+
if (hash == null) {
186+
var node = scope.caller;
187+
throw new PiccodeException(node.file, node.line, node.column, "Value is not a native object");
188+
}
189+
190+
if (!(hash instanceof PiccodeNumber)) {
191+
var node = scope.caller;
192+
throw new PiccodeException(node.file, node.line, node.column, "Invalid native object. Hash code is not a number, found " + Chalk.on(hash.type()).red());
193+
}
194+
195+
var allocatedObject = Context.getObject((int) (double) hash.raw());
196+
if (allocatedObject == null) {
197+
var node = scope.caller;
198+
throw new PiccodeException(node.file, node.line, node.column, String.format("No object is allocated at 0x%04d", hash.raw()));
199+
}
200+
201+
if (!(allocatedObject instanceof File)) {
202+
var node = scope.caller;
203+
throw new PiccodeException(node.file, node.line, node.column, String.format("Object allocated at 0x%04d is not a File", hash.raw()));
204+
}
205+
206+
var elements = new ArrayList<PiccodeValue>();
207+
var file = (File) allocatedObject;
208+
for (var st: file.list()) {
209+
elements.add(new PiccodeString(st));
210+
}
211+
return new PiccodeArray(elements);
212+
}, null);
213+
49214
}
50215
}

0 commit comments

Comments
 (0)