Initial commit of main compiler sources (or should I say ... SAUCES!)
This commit is contained in:
39
slangc/streams/ArrayOutput.sauce
Normal file
39
slangc/streams/ArrayOutput.sauce
Normal file
@@ -0,0 +1,39 @@
|
||||
package slangc.streams;
|
||||
|
||||
import slang.streams.SyncOutput;
|
||||
|
||||
public class ArrayOutput implements SyncOutput<byte> {
|
||||
byte[] buffer;
|
||||
//int top;
|
||||
|
||||
public ArrayOutput() {
|
||||
buffer = new byte[0];
|
||||
}
|
||||
|
||||
public void innerResize(int newsz) {
|
||||
byte[] nbuf = new byte[newsz];
|
||||
for (int i = 0; i < nbuf.length && i < buffer.length; i++) {
|
||||
nbuf[i] = buffer[i];
|
||||
}
|
||||
buffer = nbuf;
|
||||
}
|
||||
|
||||
public int writeBuffer(byte[] b, int from, int max) {
|
||||
if (from >= b.length) {
|
||||
return 0;
|
||||
}
|
||||
if (from + max > b.length) {
|
||||
max = b.length - from;
|
||||
}
|
||||
int base = buffer.length;
|
||||
innerResize(buffer.length + max);
|
||||
for (int i = 0; i < max; i++) {
|
||||
buffer[base + i] = b[from + i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
|
||||
}
|
||||
}
|
53
slangc/streams/File.sauce
Normal file
53
slangc/streams/File.sauce
Normal file
@@ -0,0 +1,53 @@
|
||||
package slangc.streams;
|
||||
|
||||
public class File {
|
||||
private VFS vfs;
|
||||
private String path;
|
||||
|
||||
File(VFS vfs, String path) {
|
||||
this.vfs = vfs;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public VFS getVFS() {
|
||||
return vfs;
|
||||
}
|
||||
|
||||
FileInput openInput() throws Error {
|
||||
return vfs.openInput(path);
|
||||
}
|
||||
|
||||
FileOutput openOutput() throws Error {
|
||||
return vfs.openOutput(path);
|
||||
}
|
||||
|
||||
File[] list() throws Error {
|
||||
return vfs.list(path);
|
||||
}
|
||||
|
||||
public String path() {
|
||||
return path;
|
||||
}
|
||||
|
||||
private static String trimPathSeparator(String path, String sep) {
|
||||
int l = path.searchLast(sep);
|
||||
if (l >= 0) {
|
||||
return path.sub(l + sep.ints().length);
|
||||
} else {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
public String name() {
|
||||
String p = path;
|
||||
String[] seps = vfs.allSeparators();
|
||||
for (int i = 0; i < seps.length; i++) {
|
||||
p = trimPathSeparator(p, seps[i]);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
public long size() {
|
||||
return vfs.size(path);
|
||||
}
|
||||
}
|
33
slangc/streams/FileInput.sauce
Normal file
33
slangc/streams/FileInput.sauce
Normal file
@@ -0,0 +1,33 @@
|
||||
package slangc.streams;
|
||||
|
||||
import slang.streams.SyncInput;
|
||||
|
||||
public abstract class FileInput implements SyncInput<byte> {
|
||||
private VFS vfs;
|
||||
private String path;
|
||||
|
||||
protected FileInput(VFS vfs, String path) {
|
||||
this.vfs = vfs;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public VFS vfs() {
|
||||
return vfs;
|
||||
}
|
||||
|
||||
public String path() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public int read(byte[] buffer) {
|
||||
return readBuffer(buffer, 0, buffer.length);
|
||||
}
|
||||
|
||||
public int readBuffer(byte[] buffer, int beginAt, int maxLength) {
|
||||
throw new Error("TODO:readBuffer");
|
||||
}
|
||||
|
||||
public void close() {
|
||||
throw new Error("TODO:close");
|
||||
}
|
||||
}
|
17
slangc/streams/FileOutput.sauce
Normal file
17
slangc/streams/FileOutput.sauce
Normal file
@@ -0,0 +1,17 @@
|
||||
package slangc.streams;
|
||||
|
||||
import slang.streams.SyncOutput;
|
||||
|
||||
public class FileOutput implements SyncOutput<byte> {
|
||||
private VFS vfs;
|
||||
private String path;
|
||||
|
||||
protected FileInput(VFS vfs, String path) {
|
||||
this.vfs = vfs;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
throw new Error("TODO:close");
|
||||
}
|
||||
}
|
18
slangc/streams/LogWriter.sauce
Normal file
18
slangc/streams/LogWriter.sauce
Normal file
@@ -0,0 +1,18 @@
|
||||
package slangc.streams;
|
||||
|
||||
public class LogWriter {
|
||||
#common:print
|
||||
public void part(String s) {
|
||||
throw new Error("TODO:part");
|
||||
}
|
||||
|
||||
#common:println
|
||||
public void line() {
|
||||
throw new Error("TODO:line");
|
||||
}
|
||||
|
||||
#common:println
|
||||
public void line(String s) {
|
||||
throw new Error("TODO:line");
|
||||
}
|
||||
}
|
95
slangc/streams/SimpleVFS.sauce
Normal file
95
slangc/streams/SimpleVFS.sauce
Normal file
@@ -0,0 +1,95 @@
|
||||
package slangc.streams;
|
||||
|
||||
import slang.vm.SystemCall;
|
||||
import slang.vm.DeviceHandle;
|
||||
|
||||
public class SimpleVFS extends VFS {
|
||||
public String separator() {
|
||||
return "/";
|
||||
}
|
||||
|
||||
public File[] list(String path) throws Error {
|
||||
String[] names = new String[0];
|
||||
int n = SystemCall.fileList(path, names);
|
||||
if (n < 0) {
|
||||
return null;
|
||||
}
|
||||
names = new String[n];
|
||||
SystemCall.fileList(path, names);
|
||||
File[] files = new File[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
files[i] = file(path + separator() + names[i]);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
public long size(String path) throws Error {
|
||||
duck[] stats = new duck[2];
|
||||
SystemCall.fileStat(path, stats);
|
||||
if (stats[0] == 1) {
|
||||
return (long) stats[1];
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static class SimpleInput extends FileInput {
|
||||
DeviceHandle dev;
|
||||
public SimpleInput(VFS vfs, String path, DeviceHandle dev) {
|
||||
super(vfs, path);
|
||||
this.dev = dev;
|
||||
}
|
||||
|
||||
|
||||
public int readBuffer(byte[] buffer, int beginAt, int maxLength) {
|
||||
uint8[] internal = new uint8[buffer.length];
|
||||
int total = SystemCall.readBytes(dev, internal, beginAt, maxLength);
|
||||
for (int i = 0; i < total; i++) {
|
||||
buffer[beginAt + i] = (byte) internal[beginAt + i];
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
dev.close();
|
||||
}
|
||||
}
|
||||
|
||||
public FileInput openInput(String path) throws Error {
|
||||
DeviceHandle dev = SystemCall.fileOpen(path, "rb");
|
||||
if (dev == null) {
|
||||
throw new Error("Unable to open file '" + path + "' for reading");
|
||||
}
|
||||
return new SimpleInput(this, path, dev);
|
||||
}
|
||||
|
||||
public static class SimpleOutput extends FileOutput {
|
||||
DeviceHandle dev;
|
||||
public SimpleOutput(VFS vfs, String path, DeviceHandle dev) {
|
||||
super(vfs, path);
|
||||
this.dev = dev;
|
||||
}
|
||||
|
||||
public int writeBuffer(byte[] buffer, int beginAt, int maxLength) {
|
||||
uint8[] internal = new uint8[buffer.length];
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
internal[beginAt + i] = (uint8) buffer[beginAt + i];
|
||||
}
|
||||
int total = SystemCall.writeBytes(dev, internal, beginAt, maxLength);
|
||||
return total;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
dev.close();
|
||||
//SystemCall.devicePoll(dev, true);
|
||||
}
|
||||
}
|
||||
|
||||
public FileOutput openOutput(String path) throws Error {
|
||||
DeviceHandle dev = SystemCall.fileOpen(path, "wb");
|
||||
if (dev == null) {
|
||||
throw new Error("Unable to open file '" + path + "' for writing");
|
||||
}
|
||||
return new SimpleOutput(this, path, dev);
|
||||
}
|
||||
}
|
21
slangc/streams/VFS.sauce
Normal file
21
slangc/streams/VFS.sauce
Normal file
@@ -0,0 +1,21 @@
|
||||
package slangc.streams;
|
||||
|
||||
public abstract class VFS {
|
||||
public File file(String path) {
|
||||
return new File(this, path);
|
||||
}
|
||||
|
||||
public abstract String separator();
|
||||
|
||||
public String[] allSeparators() {
|
||||
return new String[]{separator()};
|
||||
}
|
||||
|
||||
public abstract File[] list(String path) throws Error;
|
||||
|
||||
public long size(String path) throws Error;
|
||||
|
||||
public abstract FileInput openInput(String path) throws Error;
|
||||
|
||||
public abstract FileOutput openOutput(String path) throws Error;
|
||||
}
|
Reference in New Issue
Block a user