Skip to content

Commit e282798

Browse files
committed
modules: Implement the file module and clean up the others.
1 parent 7b3464e commit e282798

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed
Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
11
package org.piccode.rt.modules;
22

3+
import java.io.File;
4+
import java.util.HashMap;
35
import java.util.List;
46
import org.piccode.rt.Context;
7+
import org.piccode.rt.PiccodeBoolean;
58
import org.piccode.rt.PiccodeNumber;
9+
import org.piccode.rt.PiccodeObject;
10+
import org.piccode.rt.PiccodeString;
611
import org.piccode.rt.PiccodeValue;
712
import org.piccode.rt.PiccodeValue.Type;
813

9-
1014
/**
1115
*
1216
* @author hexaredecimal
1317
*/
1418
public class PiccodeFileModule {
19+
20+
private static PiccodeValue makeFile(File fp) {
21+
var obj = new HashMap<String, PiccodeValue>();
22+
obj.put("hash", new PiccodeNumber(fp.hashCode()));
23+
obj.put("path", new PiccodeString(fp.getPath()));
24+
obj.put("parent", new PiccodeString(fp.getParent()));
25+
obj.put("exists", new PiccodeBoolean(fp.exists()));
26+
obj.put("file", new PiccodeBoolean(fp.isFile()));
27+
obj.put("hidden", new PiccodeBoolean(fp.isHidden()));
28+
return new PiccodeObject(obj);
29+
}
30+
1531
public static void addFunctions() {
16-
17-
NativeFunctionFactory.create("new", List.of("path"), (args, namedArgs, frame) -> {
18-
var ctx = frame == null ?
19-
Context.top
20-
: Context.getContextAt(frame);
21-
22-
var scope = ctx.getTopFrame();
23-
var ms = namedArgs.get("ms");
24-
PiccodeValue.verifyType(scope.caller, ms, Type.NUMBER);
25-
var path = namedArgs.get("path");
26-
return new PiccodeNumber("" + path);
32+
33+
NativeFunctionFactory.create("filenew", List.of("path"), (args, namedArgs, frame) -> {
34+
var ctx = frame == null
35+
? Context.top
36+
: Context.getContextAt(frame);
37+
38+
var scope = ctx.getTopFrame();
39+
var _path = namedArgs.get("path");
40+
PiccodeValue.verifyType(scope.caller, _path, Type.STRING);
41+
42+
var path = _path.raw().toString();
43+
var fp = new File(path);
44+
var id = Context.allocate(fp);
45+
46+
return makeFile(fp);
2747
}, null);
28-
29-
48+
3049
}
3150
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package org.piccode.rt.modules;
22

3-
import java.util.ArrayList;
4-
import java.util.Arrays;
53
import java.util.HashMap;
64
import java.util.List;
7-
import javax.swing.text.AbstractDocument;
8-
import org.piccode.ast.IdentifierAst;
95
import org.piccode.rt.Context;
10-
import org.piccode.rt.PiccodeArray;
116
import org.piccode.rt.PiccodeClosure;
127
import org.piccode.rt.PiccodeException;
13-
import org.piccode.rt.PiccodeNumber;
148
import org.piccode.rt.PiccodeObject;
159
import org.piccode.rt.PiccodeString;
16-
import org.piccode.rt.PiccodeTuple;
1710
import org.piccode.rt.PiccodeUnit;
1811
import org.piccode.rt.PiccodeValue;
1912
import org.piccode.rt.PiccodeValue.Type;

0 commit comments

Comments
 (0)