Commit ba5d60f3 authored by Custom2043's avatar Custom2043

m

parent eaa54e0c
This diff is collapsed.
......@@ -11,68 +11,69 @@ import language.parser.parser.Token;
import language.parser.tokens.Access;
import language.parser.tokens.UnitDeclaration;
public class FileUnitSystem extends UnitSystem
{
//file:/C:/Program%20Files/Java/jdk1.8.0_181/jre/lib/rt.jar
public void addArchive(File archive)
{
try
{
public class FileUnitSystem extends UnitSystem {
// file:/C:/Program%20Files/Java/jdk1.8.0_181/jre/lib/rt.jar
public void addArchive(File archive) {
try {
ZipFile zipFile = new ZipFile(archive);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while(entries.hasMoreElements()){
ZipEntry entry = entries.nextElement();
InputStream stream = zipFile.getInputStream(entry);
if (entry.getName().endsWith(".class"))
{
Access access = Access.generate(entry.getName().replace("/", ".").substring(0, entry.getName().length()-6));
super.addClassUnit(stream, access);
}
}
zipFile.close();
}catch(Exception e) {e.printStackTrace();}
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
InputStream stream = zipFile.getInputStream(entry);
if (entry.getName().endsWith(".class")) {
Access access = Access
.generate(entry.getName().replace("/", ".").substring(0, entry.getName().length() - 6));
super.addClassUnit(stream, access);
}
}
zipFile.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void addClassUnit(File classFile, Access access)
{
try
{
public void addClassUnit(File classFile, Access access) {
try {
super.addClassUnit(new FileInputStream(classFile), access);
}catch(IOException e) {System.out.println("Couldn't find class : "+access);e.printStackTrace();}
}
catch (IOException e) {
System.out.println("Couldn't find class : " + access);
e.printStackTrace();
}
}
public FileUnitSystem addFile(File... files)
{
for (File f : files)
{
public FileUnitSystem addFile(File... files) {
for (File f : files) {
try {
InputStream fis = new BufferedInputStream(new FileInputStream(f));
char c[] = new char[fis.available()];
for (int i=0;i<c.length;i++)
c[i] = (char)fis.read();
for (int i = 0; i < c.length; i++)
c[i] = (char) fis.read();
fis.close();
List<Token> tokens = Parser.parse(c);
if (tokens.size() == 1 && tokens.get(0) instanceof UnitDeclaration)
this.addUnitDeclaration((UnitDeclaration)tokens.get(0), f);
else
{
UnitCompiler.report("Not a unit : "+f.toString());
this.addUnitDeclaration((UnitDeclaration) tokens.get(0), f);
else {
UnitCompiler.report("Not a unit : " + f.toString());
for (Token t : tokens)
System.out.println(t);
}
} catch(IOException e) {UnitCompiler.report("Couldn't read "+f.getPath());}
}
catch (IOException e) {
UnitCompiler.report("Couldn't read " + f.getPath());
}
}
return this;
}
public FileUnitSystem addUnitDeclaration(UnitDeclaration unit, File f)
{
public FileUnitSystem addUnitDeclaration(UnitDeclaration unit, File f) {
Access name = Access.fromPath(f.toPath());
super.addUnitDeclaration(unit, name);
return this;
......
......@@ -3,49 +3,42 @@ package compiler;
import language.parser.tokens.GenericsDeclaration;
import language.parser.tokens.MyType;
public class Generic
{
public class Generic {
public static final Generic wildcard = new Generic(null, 0);
public final GenericsDeclaration dec;
public final int index;
public Generic(GenericsDeclaration d, int i)
{
public Generic(GenericsDeclaration d, int i) {
this.dec = d;
this.index = i;
}
public String getName()
{
public String getName() {
return this.dec.names[this.index].toString();
}
public boolean isTuple()
{
public boolean isTuple() {
return this.dec.isTuple(this.index);
}
public boolean isWildcard()
{
public boolean isWildcard() {
return this == wildcard;
}
/**
* Can not return a generic type with generic == Generic.wildcard
*/
public MyType getClosestType()
{
public MyType getClosestType() {
if (this.dec.ext != null && this.dec.ext[this.index] != null)
return this.dec.ext[this.index];
return MyType.objectType;
}
public boolean equals(Generic args)
{
public boolean equals(Generic args) {
if (args == null)
return false;
return this.dec == args.dec && this.index == args.index;
}
}
\ No newline at end of file
......@@ -10,59 +10,58 @@ import language.parser.tokens.Access;
import language.parser.tokens.UnitDeclaration;
import main.Config;
public class JavaSourceGenerator
{
public class JavaSourceGenerator {
private final UnitSystem system;
private final Config config;
public JavaSourceGenerator(UnitSystem system, Config config)
{
public JavaSourceGenerator(UnitSystem system, Config config) {
this.system = system;
this.config = config;
}
public void generate()
{
public void generate() {
new File("generatedBin/").mkdir();
for (UnitDeclaration d : this.system.sourceUnits)
{
for (UnitDeclaration d : this.system.sourceUnits) {
MyClassWriter writer = new MyClassWriter();
d.writeBytecode(writer);
for (Entry<Access, ClassWriter> e : writer.getWriters().entrySet())
{
for (Entry<Access, ClassWriter> e : writer.getWriters().entrySet()) {
byte myGeneratedClass[] = e.getValue().toByteArray();
try {
FileOutputStream fos = new FileOutputStream(new File("generatedBin/"+e.getKey().toInternalName()+".class"));
FileOutputStream fos = new FileOutputStream(
new File("generatedBin/" + e.getKey().toInternalName() + ".class"));
fos.write(myGeneratedClass);
fos.close();
} catch(IOException ee) {ee.printStackTrace();}
}
catch (IOException ee) {
ee.printStackTrace();
}
}
}
try
{
ProcessBuilder builder = new ProcessBuilder(this.config.buildCommand()); //without .class suffix
try {
ProcessBuilder builder = new ProcessBuilder(this.config.buildCommand()); // without .class suffix
Process process = builder.start();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
// Read the output from the command
String s = null;
System.out.println("Output : ");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// Read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
System.err.println(s);
}
System.out.println("Output End.");
// Read the output from the command
String s = null;
System.out.println("Output : ");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// Read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
System.err.println(s);
}
System.out.println("Output End.");
}
catch (Exception e) {
e.printStackTrace();
}
catch(Exception e) {e.printStackTrace();}
}
}
......@@ -6,21 +6,19 @@ import language.parser.tokens.MyType;
import language.parser.tokens.VariableDeclaration;
import namingSpace.ClassNamingSpace;
public class MethodCompiler
{
public class MethodCompiler {
private final UnitCompiler unit;
public MethodCompiler(UnitCompiler compiler)
{
public MethodCompiler(UnitCompiler compiler) {
this.unit = compiler;
}
/**
* Compile the method, do nothing if it is already compiled
* Lib methods are already compiled
* Compile the method, do nothing if it is already compiled Lib methods are
* already compiled
*/
public void compile(MethodDeclaration dec)
{
if (dec.hasBeenCompiled)
public void compile(MethodDeclaration dec) {
if (dec.hasBeenCompiled)
return;
dec.hasBeenCompiled = true;
......@@ -29,45 +27,45 @@ public class MethodCompiler
dec.compileTypes(this.unit);
return;
}
if (dec.classArgs != null)
{
if (dec.classArgs != null) {
dec.classArgs.compile(this.unit, dec.getNamingSpace());
for (VariableDeclaration d : dec.classArgs.vars)
this.unit.statementCompiler.compile(d, dec.getNamingSpace());
}
dec.compileTypes(this.unit);
if (dec.cons != null)
{
if (dec.cons != null) {
if (dec.cons.classDeclaration == null)
dec.cons.classDeclaration = dec.getNamingSpace().getFirstUpperNamingSpace(ClassNamingSpace.class).getClassDeclaration();
dec.cons.classDeclaration = dec.getNamingSpace().getFirstUpperNamingSpace(ClassNamingSpace.class)
.getClassDeclaration();
dec.getNamingSpace().addClassArgsValue();
}
for (VariableDeclaration d : dec.args.vars)
this.unit.statementCompiler.compile(d, dec.getNamingSpace());
dec.asserts.compile(this.unit, dec.getNamingSpace());
this.unit.compile(dec.scope, dec.getNamingSpace());
MethodDeclaration superMethod = MethodResolver.getSuperMethod(this.unit, dec);
if (superMethod != null)
if (dec.returnType.isResolved() && !ExpressionCompiler.isTypeParentOf(superMethod.returnType, dec.returnType))
if (dec.returnType.isResolved()
&& !ExpressionCompiler.isTypeParentOf(superMethod.returnType, dec.returnType))
UnitCompiler.report("The return type is not a child of the super method's return type");
// On vérifie juste qu'il y a un return, le type est vérifié dans StatementCompiler
// On vérifie juste qu'il y a un return, le type est vérifié dans
// StatementCompiler
boolean b = !dec.scope.canEscape();
if (!b && !dec.returnType.equals(MyType.voidType))
UnitCompiler.report("No sure return statement");
}
public void compile(ConstructorDeclaration dec, ClassNamingSpace space)
{
public void compile(ConstructorDeclaration dec, ClassNamingSpace space) {
dec.setClassDeclaration(space.getClassDeclaration());
MethodDeclaration m = dec.getAssociatedMethod();
this.unit.methodCompiler.compile(m);
}
}
......@@ -11,117 +11,109 @@ import namingSpace.NamingSpace;
import namingSpace.UnitNamingSpace;
/**
* Searches methods
* Constructors are named new
* Searches methods Constructors are named new
*/
public class MethodResolver
{
public class MethodResolver {
/**
* caller is null if the wanted method is static
*/
private static MethodDeclaration getMethodDeclaration(UnitCompiler system, NamingSpace topLevelSpace, MyType caller, Name name, GenericArgs typeArgs, MyType[] args, boolean thisSpaceOnly)
{
private static MethodDeclaration getMethodDeclaration(UnitCompiler system, NamingSpace topLevelSpace, MyType caller,
Name name, GenericArgs typeArgs, MyType[] args, boolean thisSpaceOnly) {
LinkedList<MethodDeclaration> decs = new LinkedList<>();
NamingSpace space = topLevelSpace;
do
{
for (MethodDeclaration dec : space.getMethodsDeclaredInThisScope())
{
do {
for (MethodDeclaration dec : space.getMethodsDeclaredInThisScope()) {
system.compile(dec, space);
if (dec.equals(name, args, false) && !decs.contains(dec) && dec.isCompatible(caller, typeArgs, args))
decs.add(dec);
}
if (caller != null)
caller = caller.getSuperType();
space = space.getSuperSpace();
} while (space != null && thisSpaceOnly);
}
while (space != null && thisSpaceOnly);
return getBestMethod(decs, args);
}
private static MethodDeclaration getBestMethod(List<MethodDeclaration> list, MyType[] args)
{
private static MethodDeclaration getBestMethod(List<MethodDeclaration> list, MyType[] args) {
final int[] workAround = new int[1];
for (int index=0;index<args.length;index++)
{
for (int index = 0; index < args.length; index++) {
workAround[0] = index;
OptionalInt minValue = list.stream().mapToInt(dec -> getIndex(dec, args, workAround[0])).min();
list = list.stream().filter(dec -> getIndex(dec, args, workAround[0]) == minValue.getAsInt()).collect(Collectors.toList());
list = list.stream().filter(dec -> getIndex(dec, args, workAround[0]) == minValue.getAsInt())
.collect(Collectors.toList());
}
if (list.size() == 0)
return null;
return list.get(0);
}
private static int getIndex(MethodDeclaration dec, MyType[] args, int index)
{
return args[index].getSuperClassIndex(dec.getArgumentsType()[index]);
private static int getIndex(MethodDeclaration dec, MyType[] args, int index) {
return args[index].getSuperClassIndex(dec.getArgumentsType()[index]);
}
public static MethodDeclaration getSuperMethod(UnitCompiler compiler, MethodDeclaration methodDec)
{
public static MethodDeclaration getSuperMethod(UnitCompiler compiler, MethodDeclaration methodDec) {
NamingSpace superSpace = methodDec.getNamingSpace().getSuperSpace();
if (!(superSpace instanceof ClassNamingSpace)) // No super method in units, only in classes
return null;
NamingSpace cns = ((ClassNamingSpace)superSpace).getSuperSpace();
while (cns != null)
{
for (MethodDeclaration dec : cns.getMethodsDeclaredInThisScope())
{
NamingSpace cns = ((ClassNamingSpace) superSpace).getSuperSpace();
while (cns != null) {
for (MethodDeclaration dec : cns.getMethodsDeclaredInThisScope()) {
compiler.compile(dec, cns);
if (dec.equals(methodDec.name, methodDec.getArgumentsType()))
return dec;
}
cns = cns.getSuperSpace();
}
return null;
}
public static MethodDeclaration getConstructor(UnitCompiler system, MyType caller, GenericArgs typeArgs, MyType[] args)
{
if (caller.isTupleType())
{
public static MethodDeclaration getConstructor(UnitCompiler system, MyType caller, GenericArgs typeArgs,
MyType[] args) {
if (caller.isTupleType()) {
if (caller.typeArgs.types.length != args.length)
return null;
for (int i=0;i<args.length;i++)
for (int i = 0; i < args.length; i++)
if (!ExpressionCompiler.isTypeParentOf(caller.typeArgs.types[i], args[i]))
return null;
return ConstructorDeclaration.tupleConstructor;
}
else if (caller.getClassDeclaration().getFullAccess().equals(Access.arrayAccess))
{
else if (caller.getClassDeclaration().getFullAccess().equals(Access.arrayAccess)) {
if (typeArgs.types.length != 0 || caller.typeArgs.types.length != 1 || caller.typeArgs.args.length != 1)
return null;
for (int i=0;i<args.length;i++)
for (int i = 0; i < args.length; i++)
if (!ExpressionCompiler.isTypeParentOf(caller.typeArgs.types[0], args[i]))
return null;
return ConstructorDeclaration.arrayConstructor;
}
return getMethodDeclaration(system, caller.getClassDeclaration().getNamingSpace(), caller, ConstructorDeclaration.constructorName, typeArgs, args, true);
return getMethodDeclaration(system, caller.getClassDeclaration().getNamingSpace(), caller,
ConstructorDeclaration.constructorName, typeArgs, args, true);
}
public static MethodDeclaration getStaticMethod(UnitCompiler system, UnitNamingSpace unit, Name name, GenericArgs typeArgs, MyType[] args)
{
public static MethodDeclaration getStaticMethod(UnitCompiler system, UnitNamingSpace unit, Name name,
GenericArgs typeArgs, MyType[] args) {
return getMethodDeclaration(system, unit, null, name, typeArgs, args, true);
}
public static MethodDeclaration getMethod(UnitCompiler system, MyType caller, Name name, GenericArgs typeArgs, MyType[] args)
{
return getMethodDeclaration(system, caller.getClassDeclaration().getNamingSpace(), caller, name, typeArgs, args, false);
}
public static MethodDeclaration getMethod(UnitCompiler system, MyType caller, Name name, GenericArgs typeArgs,
MyType[] args) {
return getMethodDeclaration(system, caller.getClassDeclaration().getNamingSpace(), caller, name, typeArgs, args,
false);
}
}
\ No newline at end of file
......@@ -2,39 +2,33 @@ package compiler;
import java.util.Arrays;
import inflow.ArrayOperation;
import language.parser.tokens.*;
import namingSpace.NamingSpace;
import namingSpace.UnitNamingSpace;
public class ReferenceCompiler
{
public class ReferenceCompiler {
private final NamingSpace referenceSpace;
private final UnitCompiler unit;
private final Reference ref;
public static MyType resolve(Reference ref, NamingSpace referenceSpace, UnitCompiler unit)
{
public static MyType resolve(Reference ref, NamingSpace referenceSpace, UnitCompiler unit) {
return new ReferenceCompiler(ref, referenceSpace, unit).compile();
}
private ReferenceCompiler(Reference ref, NamingSpace referenceSpace, UnitCompiler unit)
{
private ReferenceCompiler(Reference ref, NamingSpace referenceSpace, UnitCompiler unit) {
this.unit = unit;
this.referenceSpace = referenceSpace;
this.ref = ref;
}
private MyType compile()
{
private MyType compile() {
NamingSpace currentSpace = this.referenceSpace;
MyType currentType = null;
if (this.ref.unitAccess != null) // Unit reference
{
UnitDeclaration d = this.unit.system.getUnit(currentSpace.getUnit(), this.ref.unitAccess);
if (d != null)
{
if (d != null) {
currentSpace = d.space;
d.load(this.unit);
}
......@@ -43,47 +37,35 @@ public class ReferenceCompiler
currentType = this.unit.expressionCompiler.compile(this.referenceSpace, this.ref.exp);
else if (this.ref.thi != null)
currentSpace = this.referenceSpace;
for (int i=0;i<this.ref.names.length;i++)
{
if (currentType != null)
{
for (int i = 0; i < this.ref.names.length; i++) {
if (currentType != null) {
if (!currentType.isResolved())
return currentType;
currentSpace = currentType.getClassDeclaration().getNamingSpace();
}
currentType = this.compilePart(i, currentType, currentSpace);
this.ref.completedTypes[i] = currentType;
}
if (!this.ref.toOperationList().operationList.isEmpty())
if (this.ref.toOperationList().operationList.getLast() instanceof ArrayOperation)
{
ArrayOperation op = (ArrayOperation)this.ref.toOperationList().operationList.getLast();
if (!op.get)
this.referenceSpace.removeInfosOn(this.ref.toOperationList().copy(this.ref.toOperationList().operationList.size()));
}
return currentType;