Skip to content

Commit

Permalink
parse-directory
Browse files Browse the repository at this point in the history
  • Loading branch information
nenuadrian committed Oct 13, 2024
1 parent 09ab5d8 commit 9d2f0ec
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/main/java/com/example/Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.example;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
Expand All @@ -18,7 +18,7 @@ public class Main {
public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption("h", "help", false, "Show help");
options.addRequiredOption("f", "file", true, "File path");
options.addRequiredOption("i", "input", true, "Input .hades file or directory");

CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
Expand All @@ -41,27 +41,40 @@ public static void main(String[] args) throws Exception {
}
}

private static String readFileContents(String filePath) throws IOException {
private static String readFileContents(File file) throws IOException {
// Read all lines from the file and convert them to a string
String content = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
return content;
}

private static void execute(CommandLine cmd) throws Exception {
if (cmd.hasOption("f")) {
String filePath = cmd.getOptionValue("f");
File file = new File(cmd.getOptionValue("f"));
compile(file);
}

private static void compile(File file) throws Exception {
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) { // Check if the directory is not empty or accessible
for (File childFile : files) {
if (childFile.isFile()) {
compile(file);
}
}
}
} else {

// Check if the file has a .hades extension
if (!filePath.endsWith(".hades")) {
if (!file.getName().endsWith(".hades")) {
System.out.println("Error: The file must have a .hades extension.");
System.exit(1);
}

// Remove the .hades extension and replace it with .class
String classFilePath = filePath.substring(0, filePath.length() - 6) + ".class";
String classFilePath = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 6) + ".class";

// Read the contents of the .hades file
String input = readFileContents(filePath);
String input = readFileContents(file);

// Generate bytecode (assuming the HadesCompiler class exists)
byte[] bytecode = new HadesCompiler().generateBytecode(input);
Expand Down

0 comments on commit 9d2f0ec

Please sign in to comment.