Initial commit

This commit is contained in:
Dimitri Lajou
2025-03-21 17:26:31 +01:00
commit 7114c0e978
115 changed files with 1760 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
package TP2;
import java.nio.file.Paths;
import java.util.Optional;
import java.io.IOException;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import TP2.asd.Program;
public class Main {
public static void main(String[] args) {
try {
// Set input
CharStream input;
if (args.length == 0) {
// From standard input
input = new ANTLRInputStream(System.in);
} else {
// From file name in first argument
input = new ANTLRFileStream(args[0]);
}
// Instantiate Lexer
VSLLexer lexer = new VSLLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
// Instantiate Parser
VSLParser parser = new VSLParser(tokens);
// Parse
Program ast = parser.program();
// Pretty-print the program (to debug parsing)
System.err.println("todo");
// Verify the program semantic
// Generate the intermediate representation
System.out.println("todo");
} catch (IOException | RecognitionException e) {
e.printStackTrace();
throw new RuntimeException("Unable to proceed");
}
}
}

View File

@@ -0,0 +1,5 @@
package TP2.asd;
public record Program() {
}

View File

@@ -0,0 +1,5 @@
package TP2.llvm;
public record LLVMString(String data, int size) {
}

View File

@@ -0,0 +1,81 @@
package TP2.llvm;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Utils {
private static int tmp = 0;
private static int lab = 0;
private static int glob = 0;
private final static Pattern re = Pattern.compile("\\\\n");
/**
* Generate a new unique local identifier (starting with %tmp)
*
* @return the identifier
*/
public static String newtmp() {
return newVar("tmp");
}
/**
* Generate a new unique local identifier (starting with %) for a variable
*
* @return the identifier
*/
public static String newVar(String prefix) {
tmp++;
return "%" + prefix + tmp;
}
/**
* <p>
* generate a new unique label starting with str
* </p>
*
* ex: newlab("if") may return "if24"
*
* @param str the start of the label
* @return the label name
*/
public static String newlab(String str) {
lab++;
return str + lab;
}
/**
* generate a new unique global identifier (starting with @)
*
* @param str the start of the name of the identifier
* @return the name of the identifier
*/
public static String newglob(String str) {
glob++;
return "@" + str + glob;
}
/**
* Transform escaped newlines ('\' 'n') into newline form suitable for LLVM and
* append the NUL character (end of string)
*
* @param str the input string
* @return a pair: the new String, and its size (according to LLVM)
*/
public static LLVMString stringTransform(String str) {
Matcher m = re.matcher(str);
StringBuffer res = new StringBuffer();
int count = 0;
while (m.find()) {
m.appendReplacement(res, "\\\\0A");
count++;
}
m.appendTail(res).append("\\00");
// + 1 for \00
// - 1 by \n because each ('\' '\n') is transformed into one char
return new LLVMString(res.toString(), 1 + str.length() - count);
}
}