Skip to content

Commit 3d8b19f

Browse files
committed
Implement popen, fix createDirectories, shorten function name for 0.4.0
1 parent ee637b3 commit 3d8b19f

File tree

12 files changed

+78
-30
lines changed

12 files changed

+78
-30
lines changed

idris-jvm-assembler/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.github.mmhelloworld</groupId>
77
<artifactId>idris-jvm</artifactId>
8-
<version>0.3.0</version>
8+
<version>0.3.1-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

idris-jvm-assembler/src/main/java/io/github/mmhelloworld/idrisjvm/assembler/IdrisName.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public final class IdrisName {
2222
private static final Map<Character, String> replacements = new HashMap<>();
2323

2424
static {
25-
replacements.put(' ', "$spc");
25+
replacements.put(' ', "$s");
2626
replacements.put('!', "$not");
27-
replacements.put('"', "$dquo");
27+
replacements.put('"', "$dq");
2828
replacements.put('#', "$hash");
2929
replacements.put('%', "$mod");
3030
replacements.put('&', "$and");
31-
replacements.put('\'', "$squo");
31+
replacements.put('\'', "$q");
3232
replacements.put('(', "$lpar");
3333
replacements.put(')', "$rpar");
3434
replacements.put('*', "$mul");

idris-jvm-compiler/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.github.mmhelloworld</groupId>
77
<artifactId>idris-jvm</artifactId>
8-
<version>0.3.0</version>
8+
<version>0.3.1-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

idris-jvm-runtime/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.github.mmhelloworld</groupId>
77
<artifactId>idris-jvm</artifactId>
8-
<version>0.3.0</version>
8+
<version>0.3.1-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

idris-jvm-runtime/src/main/java/io/github/mmhelloworld/idrisjvm/runtime/ChannelIo.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import java.io.Closeable;
44
import java.io.EOFException;
5+
import java.io.File;
56
import java.io.FileNotFoundException;
67
import java.io.IOException;
78
import java.nio.ByteBuffer;
89
import java.nio.channels.Channel;
10+
import java.nio.channels.Channels;
911
import java.nio.channels.FileChannel;
1012
import java.nio.channels.ReadableByteChannel;
1113
import java.nio.channels.SeekableByteChannel;
@@ -27,6 +29,7 @@
2729
import java.util.Set;
2830
import java.util.function.Function;
2931

32+
import static io.github.mmhelloworld.idrisjvm.runtime.Directories.workingDir;
3033
import static java.nio.charset.StandardCharsets.UTF_8;
3134
import static java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE;
3235
import static java.nio.file.attribute.PosixFilePermission.GROUP_READ;
@@ -120,6 +123,14 @@ public static ChannelIo open(String name, String mode) {
120123
}
121124
}
122125

126+
public static ChannelIo popen(String command, String mode) throws IOException {
127+
Process process = new ProcessBuilder(IdrisSystem.getCommand(command))
128+
.directory(new File(workingDir))
129+
.start();
130+
return new ChannelIo(null, new ReadableWritableChannel(Channels.newChannel(process.getInputStream()),
131+
Channels.newChannel(process.getOutputStream())));
132+
}
133+
123134
public static void close(ChannelIo file) {
124135
file.close();
125136
}
@@ -129,17 +140,17 @@ public static int chmod(String file, int mode) {
129140
.chmod(mode);
130141
}
131142

132-
public static void createDirectories(Path dirPath) throws IOException {
133-
if (dirPath != null) {
134-
java.nio.file.Files.createDirectories(dirPath);
143+
public static void createDirectories(Path path) throws IOException {
144+
if (path != null && (!Files.isSymbolicLink(path) || !Files.exists(path))) {
145+
Files.createDirectories(path);
135146
}
136147
}
137148

138149
public static void writeFile(String pathString, String content) throws IOException {
139150
Path path = Paths.createPath(pathString);
140151
byte[] bytes = content.getBytes(UTF_8);
141152
createDirectories(path.getParent());
142-
java.nio.file.Files.write(path, bytes);
153+
Files.write(path, bytes);
143154
}
144155

145156
public static int flush(ChannelIo file) {
@@ -334,9 +345,7 @@ public int write(ByteBuffer src) throws IOException {
334345
}
335346

336347
private static ChannelIo open(Path path, OpenOption... openOptions) throws IOException {
337-
if (path.getParent() != null) {
338-
java.nio.file.Files.createDirectories(path.getParent());
339-
}
348+
ensureParentDirectory(path);
340349
return new ChannelIo(path, FileChannel.open(path, openOptions));
341350
}
342351

idris-jvm-runtime/src/main/java/io/github/mmhelloworld/idrisjvm/runtime/Directories.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static void closeDirectory(Object directory) {
8282
public static Object getNextDirectoryEntry(Object directory) {
8383
Iterator<Path> iterator = ((Directory) directory).getIterator();
8484
if (iterator.hasNext()) {
85-
return iterator.next().toString();
85+
return iterator.next().getFileName().toString();
8686
} else {
8787
Runtime.setErrorNumber(2);
8888
return null;

idris-jvm-runtime/src/main/java/io/github/mmhelloworld/idrisjvm/runtime/IdrisSystem.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,7 @@ public static Object malloc(int length) {
100100
return new byte[length];
101101
}
102102

103-
private static String getOsNameProperty() {
104-
try {
105-
return System.getProperty("os.name").toLowerCase(Locale.ROOT);
106-
} catch (SecurityException exception) {
107-
return "";
108-
}
109-
}
110-
111-
private static String[] getCommand(String command) {
103+
public static String[] getCommand(String command) {
112104
boolean isWindows = OS_NAME.equals("windows");
113105
String shell = isWindows ? "cmd.exe" : "sh";
114106
String shellSwitch = isWindows ? "/c" : "-c";
@@ -134,4 +126,12 @@ private static String[] getCommand(String command) {
134126
}
135127
return new String[] {shell, shellSwitch, command};
136128
}
129+
130+
private static String getOsNameProperty() {
131+
try {
132+
return System.getProperty("os.name").toLowerCase(Locale.ROOT);
133+
} catch (SecurityException exception) {
134+
return "";
135+
}
136+
}
137137
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.mmhelloworld.idrisjvm.runtime;
2+
3+
import java.io.IOException;
4+
import java.nio.ByteBuffer;
5+
import java.nio.channels.ReadableByteChannel;
6+
import java.nio.channels.WritableByteChannel;
7+
8+
final class ReadableWritableChannel implements ReadableByteChannel, WritableByteChannel {
9+
private final ReadableByteChannel reader;
10+
private final WritableByteChannel writer;
11+
12+
ReadableWritableChannel(ReadableByteChannel reader, WritableByteChannel writer) {
13+
this.reader = reader;
14+
this.writer = writer;
15+
}
16+
17+
@Override
18+
public int read(ByteBuffer byteBuffer) throws IOException {
19+
return reader.read(byteBuffer);
20+
}
21+
22+
@Override
23+
public int write(ByteBuffer byteBuffer) throws IOException {
24+
return writer.write(byteBuffer);
25+
}
26+
27+
@Override
28+
public boolean isOpen() {
29+
return reader.isOpen() || writer.isOpen();
30+
}
31+
32+
@Override
33+
public void close() throws IOException {
34+
reader.close();
35+
writer.close();
36+
}
37+
}

idris-jvm-tests/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.github.mmhelloworld</groupId>
77
<artifactId>idris-jvm</artifactId>
8-
<version>0.3.0</version>
8+
<version>0.3.1-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

libs/base/System/File.idr

+2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ prim__eof : FilePtr -> PrimIO Int
6767
jvm' fileClass "flush" fileClass "int"
6868
prim__flush : FilePtr -> PrimIO Int
6969
%foreign support "idris2_popen"
70+
jvm' fileClass "popen" "String String" fileClass
7071
prim__popen : String -> String -> PrimIO FilePtr
7172
%foreign support "idris2_pclose"
73+
jvm' fileClass "close" fileClass "void"
7274
prim__pclose : FilePtr -> PrimIO ()
7375

7476
%foreign support "idris2_removeFile"

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.mmhelloworld</groupId>
88
<artifactId>idris-jvm</artifactId>
9-
<version>0.3.0</version>
9+
<version>0.3.1-SNAPSHOT</version>
1010
<packaging>pom</packaging>
1111
<name>Idris 2 JVM</name>
1212

src/Compiler/Jvm/Jname.idr

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ jvmName : Name -> Jname
5050
jvmName (NS ns n) = Jqualified (replace (cleanupIdentifier $ showNSWithSep "$" ns) '$' '/') $ getSimpleName (jvmName n)
5151
jvmName (UN n) = Jsimple $ cleanupIdentifier n
5252
jvmName (MN n i) = Jsimple $ cleanupIdentifier n ++ "$" ++ show i
53-
jvmName (PV n d) = Jsimple $ "$patvar" ++ getSimpleName (jvmName n)
53+
jvmName (PV n d) = Jsimple $ "$p" ++ getSimpleName (jvmName n)
5454
jvmName (DN str n) = Jsimple $ cleanupIdentifier str ++ getSimpleName (jvmName n)
5555
jvmName (RF str) = Jsimple $ cleanupIdentifier str
56-
jvmName (Nested (i, x) n) = Jsimple $ "$nested" ++ show i ++ "$" ++ show x ++ "$" ++ getSimpleName (jvmName n)
57-
jvmName (CaseBlock x y) = Jsimple $ "$case" ++ cleanupIdentifier (show x) ++ "$" ++ show y
58-
jvmName (WithBlock x y) = Jsimple $ "$with" ++ cleanupIdentifier (show x) ++ "$" ++ show y
59-
jvmName (Resolved i) = Jsimple $ "$resolved" ++ show i
56+
jvmName (Nested (i, x) n) = Jsimple $ "$n" ++ show i ++ "$" ++ show x ++ "$" ++ getSimpleName (jvmName n)
57+
jvmName (CaseBlock x y) = Jsimple $ "$c" ++ cleanupIdentifier (show x) ++ "$" ++ show y
58+
jvmName (WithBlock x y) = Jsimple $ "$w" ++ cleanupIdentifier (show x) ++ "$" ++ show y
59+
jvmName (Resolved i) = Jsimple $ "$r" ++ show i
6060

6161
export
6262
jvmSimpleName : Name -> String

0 commit comments

Comments
 (0)