Skip to content

Commit

Permalink
modules: Use java 21 where possible for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
hexaredecimal committed Jul 6, 2024
1 parent fe26e09 commit 9bd4c36
Showing 1 changed file with 21 additions and 47 deletions.
68 changes: 21 additions & 47 deletions src/com/erlava/runtime/Modules.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ private static void initIo() {
final String format = args[0].toString();
final Object[] values = new Object[args.length - 1];
for (int i = 1; i < args.length; i++) {
if (args[i] instanceof BarleyNumber) {
values[i - 1] = args[i].asFloat().intValue();
if (args[i] instanceof BarleyNumber number) {
values[i - 1] = number.asFloat().intValue();
} else {
values[i - 1] = args[i].toString();
}
Expand All @@ -130,8 +130,8 @@ private static void initIo() {
final String format = args[0].toString();
final Object[] values = new Object[args.length - 1];
for (int i = 1; i < args.length; i++) {
if (args[i] instanceof BarleyNumber) {
values[i - 1] = args[i].asFloat().intValue();
if (args[i] instanceof BarleyNumber number) {
values[i - 1] = number.asFloat().intValue();
} else {
values[i - 1] = args[i].toString();
}
Expand All @@ -145,8 +145,8 @@ private static void initIo() {
final String format = args[0].toString();
final Object[] values = new Object[args.length - 1];
for (int i = 1; i < args.length; i++) {
if (args[i] instanceof BarleyNumber) {
values[i - 1] = args[i].asFloat().doubleValue();
if (args[i] instanceof BarleyNumber number) {
values[i - 1] = number.asFloat().doubleValue();
} else {
values[i - 1] = args[i].toString();
}
Expand Down Expand Up @@ -424,12 +424,13 @@ private static void initBarley() {
}
return new BarleyAtom(AtomTable.put("error"));
});
shell.put("compile", args -> {

shell.put("compile_ast", args -> {
Arguments.check(1, args.length);
try {
List<AST> parsed = Handler.parseAST(SourceLoader.readSource(args[0].toString()));
byte[] binary = SerializeUtils.serialize((Serializable) parsed);
try (FileWriter writer = new FileWriter(args[0].toString().split("\\.")[0] + ".app", false)) {
try (FileWriter writer = new FileWriter(args[0].toString().split("\\.")[0] + ".ast", false)) {
for (byte b : binary) {
writer.append(String.valueOf(b)).append(" ");
}
Expand All @@ -440,45 +441,6 @@ private static void initBarley() {
}
return new BarleyAtom(AtomTable.put("error"));
});
shell.put("execute_app", args -> {
Arguments.check(1, args.length);
try {
String path = args[0].toString();
FileUtils.expectExtention(path, "app");
String bytes = SourceLoader.readSource(args[0].toString());
String[] bs = bytes.split(" ");

ArrayList<Byte> bts = new ArrayList<>();
for (String b : bs) {
bts.add(Byte.parseByte(b));
}
byte[] binary = toPrimitives(bts.toArray(new Byte[]{}));
List<AST> ast = SerializeUtils.deserialize(binary);
// System.out.println("after parsing");

ArgParser argp = new ArgParser(new String[]{"entry", path});
Config conf = new Config();
conf.setProgram("Erlava");
conf.setVersion(Handler.RUNTIME_VERSION);
conf.parse(argp);

for (AST node : ast) {
if (node instanceof MethodAST) {
MethodAST n = (MethodAST) node;
if (n.getName().equals("main")) {
CallAST call = (CallAST) Parser.buildCallV(conf.getEntry_module(), "main", new ArrayList<>());
System.out.println("Entry: " + call.execute());
}
} else {
node.execute();
}
}
return new BarleyAtom(AtomTable.put("ok"));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return new BarleyAtom(AtomTable.put("error"));
});

shell.put("atoms", args -> {
Arguments.check(0, args.length);
Expand Down Expand Up @@ -701,6 +663,18 @@ private static void initString() {
return new BarleyNumber(args[0].toString().length());
});

string.put("repeat", args -> {
Arguments.check(2, args.length);
String first = args[0].toString();
BarleyValue second = args[1];
if (!(second instanceof BarleyNumber)) {
throw new BarleyException("Runtime", "Expected type of argument 2 to be a number, but found " + second);
}

int count = second.asInteger().intValue();
return new BarleyString(first.repeat(count));
});

string.put("from_end_extract_until", args -> {
Arguments.check(2, args.length);
String path = args[0].toString();
Expand Down

0 comments on commit 9bd4c36

Please sign in to comment.