Skip to content

Commit

Permalink
Added classfile parser and advanced run options
Browse files Browse the repository at this point in the history
  • Loading branch information
tkohout committed Dec 14, 2015
1 parent 5d0f6e5 commit 3c3c195
Show file tree
Hide file tree
Showing 17 changed files with 580 additions and 121 deletions.
10 changes: 7 additions & 3 deletions osrc/SATSolver/Ostrava.cyp
Expand Up @@ -6,12 +6,16 @@ banik pyco

tryda Ostrava {
rynek(){
rynek(chachar[] vstup){
kaj (vstup == chuj){
Konzola.pravit("Chybi vstupni soubor") pyco
davaj pyco
}

toz Parsovac p = zrob Parsovac() pyco
toz CNF cnf = p.vyparsuj("resources/sats/06.txt") pyco
toz CNF cnf = p.vyparsuj(zrob Dryst(vstup)) pyco
Konzola.pravit("Vyparsovano") pyco
Konzola.pravit("Zacinam resit") pyco
dechrobok pyco

toz Resic r = zrob Resic(cnf) pyco
r.rubej() pyco
Expand Down
171 changes: 171 additions & 0 deletions src/cz/cvut/fit/ostrajava/Compile.java
@@ -0,0 +1,171 @@
package cz.cvut.fit.ostrajava;

/**
* Created by tomaskohout on 12/13/15.
*/

import cz.cvut.fit.ostrajava.Compiler.*;
import cz.cvut.fit.ostrajava.Interpreter.ClassPool;
import cz.cvut.fit.ostrajava.Interpreter.Memory.Array;
import cz.cvut.fit.ostrajava.Parser.ASTCompilationUnit;
import cz.cvut.fit.ostrajava.Parser.Node;
import cz.cvut.fit.ostrajava.Parser.OSTRAJavaParser;
import cz.cvut.fit.ostrajava.Parser.ParseException;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cz.cvut.fit.ostrajava.Compiler.Class;

public class Compile {

final static String CLASS_TYPE_EXTENSION = "tryda";

public static void printHelp(){
System.out.println("Pouziti: ostrajavac <moznosti> <soubory zdrojove bo slozka>\n"+
"kaj moznosti muzu byt: \n" +
"-d slozka pro vygenerovane .tryda soubory \n");
}

public static void exec(String[] args) throws Exception
{
if (args.length == 0) {
printHelp();
System.exit(0);
}

List<String> filenames = new ArrayList<>(Arrays.asList(args));
String outputDirectory = ".";

for (int i = 0; i < args.length-1; i ++) {
String param = args[i];
String value = args[i+1];

if (param.equals("-d")){
filenames.remove(i);
filenames.remove(i);
outputDirectory = value;
}
}

if (filenames.size() == 0){
printHelp();
System.exit(0);
}


List<Node> rootNodeList = parse(filenames);

List<Class> classList = new ArrayList<>();
OSTRAJavaCompiler compiler = new OSTRAJavaCompiler();

//First stage - precompilation
for (Node node: rootNodeList){
classList.addAll(compiler.precompile(node));
}

//Second stage - compilation
ClassPool classPool = new ClassPool(classList);

classList.clear();

for (Node node: rootNodeList){
classList.addAll(compiler.compile(node, classPool));
}

removeClassfiles(outputDirectory);

//Generate files
for (Class clazz: classList){
Classfile.toFile(clazz, outputDirectory + clazz.getClassName() + "." + CLASS_TYPE_EXTENSION);
}


}

protected static void removeClassfiles(String dir){
File file = new File(dir);
if (!file.isDirectory()){
System.out.println(dir + " is not a directory");
System.exit(0);
}

for (File dirFile: file.listFiles()){
String extension = "";

int i = dirFile.getName().lastIndexOf('.');
if (i > 0) {
extension = dirFile.getName().substring(i + 1);
}

if (extension.equals(Compile.CLASS_TYPE_EXTENSION)) {
dirFile.delete();
}

}


}

protected static List<File> listAllFiles(List<String> filenames){
List<File> files = new ArrayList<>();

for (String fileName: filenames) {
File file = new File(fileName);
files.addAll(getFilesRecursively(file));
}

return files;
}

protected static List<File> getFilesRecursively(File file){
List<File> files = new ArrayList<>();

if (file.isDirectory()){
File[] dirFiles = file.listFiles();
for (File dirFile: dirFiles){
files.addAll(getFilesRecursively(dirFile));
}

}else{
files.add(file);
}

return files;
}

protected static List<Node> parse(List<String> filenames) throws FileNotFoundException, ParseException {
Reader fr = null;
OSTRAJavaParser jp = null;

List<Node> rootNodeList = new ArrayList<>();

for (File file: listAllFiles(filenames)) {

fr = new InputStreamReader(new FileInputStream(file));

if (jp == null){
jp = new OSTRAJavaParser(fr);
}else{
jp.ReInit(fr);
}

try {
//Parse
jp.CompilationUnit();
ASTCompilationUnit node = (ASTCompilationUnit) jp.rootNode();

rootNodeList.add(node);
} catch (ParseException e) {
System.out.println("Parsing exception in file " + file.getName());
throw e;
}
}

return rootNodeList;
}


}
2 changes: 1 addition & 1 deletion src/cz/cvut/fit/ostrajava/Compiler/ByteCode.java
Expand Up @@ -41,7 +41,7 @@ public String toString() {

int in = 0;
for (Instruction i: instructions){
sb.append(in + ": " + i + "\n");
sb.append(i + System.getProperty("line.separator"));
in++;
}

Expand Down
16 changes: 1 addition & 15 deletions src/cz/cvut/fit/ostrajava/Compiler/Class.java
Expand Up @@ -19,8 +19,6 @@
*/
public class Class {

final String MAGIC_HEADER = "BANIK";

protected List<String> flags;

protected String className;
Expand Down Expand Up @@ -84,7 +82,7 @@ public void setFields(List<Field> fields) {
this.fields = fields;
}

public void addFields(Field field) {
public void addField(Field field) {
this.fields.add(field);
}

Expand Down Expand Up @@ -217,18 +215,6 @@ public String toString() {
sb.append(className + ">" + superName);

return sb.toString();
/*
sb.append(MAGIC_HEADER + "\n");
for (String flag : flags){
sb.append(flag + "|");
}
sb.append("\n");
sb.append(className + ">" + superName);
sb.append("\n");

return sb.toString();*/
}
}

0 comments on commit 3c3c195

Please sign in to comment.