Archived
Compare commits
1
Commits
3f58bdfad9
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c3d038dd35 |
@@ -60,6 +60,3 @@ local.properties
|
|||||||
# Typically, this file would be tracked if it contains build/dependency configurations:
|
# Typically, this file would be tracked if it contains build/dependency configurations:
|
||||||
#.project
|
#.project
|
||||||
/.gradle/
|
/.gradle/
|
||||||
|
|
||||||
# tam files
|
|
||||||
*.tam
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
apply plugin: 'java'
|
apply plugin: 'java'
|
||||||
apply plugin: 'application'
|
apply plugin: 'application'
|
||||||
|
|
||||||
sourceCompatibility = 11
|
sourceCompatibility = 17
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation project(':Triangle.AbstractMachine')
|
implementation project(':Triangle.AbstractMachine')
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
apply plugin: 'java'
|
apply plugin: 'java'
|
||||||
apply plugin: 'application'
|
apply plugin: 'application'
|
||||||
|
|
||||||
sourceCompatibility = 11
|
sourceCompatibility = 17
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation project(':Triangle.AbstractMachine')
|
implementation project(':Triangle.AbstractMachine')
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
apply plugin: 'java-library'
|
apply plugin: 'java-library'
|
||||||
apply plugin: 'eclipse'
|
apply plugin: 'eclipse'
|
||||||
|
|
||||||
sourceCompatibility = 11
|
sourceCompatibility = 17
|
||||||
@@ -1,21 +1,12 @@
|
|||||||
apply plugin: 'java'
|
apply plugin: 'java'
|
||||||
apply plugin: 'application'
|
apply plugin: 'application'
|
||||||
|
|
||||||
sourceCompatibility = 11
|
sourceCompatibility = 17
|
||||||
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation project(':Triangle.AbstractMachine')
|
implementation project(':Triangle.AbstractMachine')
|
||||||
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
application {
|
application {
|
||||||
mainClass = 'Triangle.Compiler'
|
mainClass = 'Triangle.Compiler'
|
||||||
}
|
}
|
||||||
|
|
||||||
// allow access to programs for unit tests
|
|
||||||
sourceSets.test.resources.srcDir file("$rootDir/programs")
|
|
||||||
@@ -34,8 +34,6 @@ public class Compiler {
|
|||||||
/** The filename for the object program, normally obj.tam. */
|
/** The filename for the object program, normally obj.tam. */
|
||||||
static String objectName = "obj.tam";
|
static String objectName = "obj.tam";
|
||||||
|
|
||||||
static boolean showTree = false;
|
|
||||||
|
|
||||||
private static Scanner scanner;
|
private static Scanner scanner;
|
||||||
private static Parser parser;
|
private static Parser parser;
|
||||||
private static Checker checker;
|
private static Checker checker;
|
||||||
@@ -73,7 +71,7 @@ public class Compiler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
scanner = new Scanner(source);
|
scanner = new Scanner(source);
|
||||||
reporter = new ErrorReporter(false);
|
reporter = new ErrorReporter();
|
||||||
parser = new Parser(scanner, reporter);
|
parser = new Parser(scanner, reporter);
|
||||||
checker = new Checker(reporter);
|
checker = new Checker(reporter);
|
||||||
emitter = new Emitter(reporter);
|
emitter = new Emitter(reporter);
|
||||||
@@ -82,7 +80,7 @@ public class Compiler {
|
|||||||
|
|
||||||
// scanner.enableDebugging();
|
// scanner.enableDebugging();
|
||||||
theAST = parser.parseProgram(); // 1st pass
|
theAST = parser.parseProgram(); // 1st pass
|
||||||
if (reporter.getNumErrors() == 0) {
|
if (reporter.numErrors == 0) {
|
||||||
// if (showingAST) {
|
// if (showingAST) {
|
||||||
// drawer.draw(theAST);
|
// drawer.draw(theAST);
|
||||||
// }
|
// }
|
||||||
@@ -91,13 +89,13 @@ public class Compiler {
|
|||||||
if (showingAST) {
|
if (showingAST) {
|
||||||
drawer.draw(theAST);
|
drawer.draw(theAST);
|
||||||
}
|
}
|
||||||
if (reporter.getNumErrors() == 0) {
|
if (reporter.numErrors == 0) {
|
||||||
System.out.println("Code Generation ...");
|
System.out.println("Code Generation ...");
|
||||||
encoder.encodeRun(theAST, showingTable); // 3rd pass
|
encoder.encodeRun(theAST, showingTable); // 3rd pass
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean successful = (reporter.getNumErrors() == 0);
|
boolean successful = (reporter.numErrors == 0);
|
||||||
if (successful) {
|
if (successful) {
|
||||||
emitter.saveObjectProgram(objectName);
|
emitter.saveObjectProgram(objectName);
|
||||||
System.out.println("Compilation was successful.");
|
System.out.println("Compilation was successful.");
|
||||||
@@ -116,29 +114,16 @@ public class Compiler {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
if (args.length < 1) {
|
if (args.length < 1) {
|
||||||
System.out.println("Usage: tc filename [-o=outputfilename] [tree]");
|
System.out.println("Usage: tc filename [tree]");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
parseArgs(args);
|
|
||||||
|
|
||||||
String sourceName = args[0];
|
String sourceName = args[0];
|
||||||
|
boolean tree = (args.length > 1 && args[1].equalsIgnoreCase("tree"));
|
||||||
|
var compiledOK = compileProgram(sourceName, objectName, tree, false);
|
||||||
|
|
||||||
var compiledOK = compileProgram(sourceName, objectName, showTree, false);
|
if (!tree) {
|
||||||
|
|
||||||
if (!showTree) {
|
|
||||||
System.exit(compiledOK ? 0 : 1);
|
System.exit(compiledOK ? 0 : 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void parseArgs(String[] args) {
|
|
||||||
for (String s : args) {
|
|
||||||
var sl = s.toLowerCase();
|
|
||||||
if (sl.equals("tree")) {
|
|
||||||
showTree = true;
|
|
||||||
} else if (sl.startsWith("-o=")) {
|
|
||||||
objectName = s.substring(3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,44 +18,25 @@ import triangle.syntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class ErrorReporter {
|
public class ErrorReporter {
|
||||||
|
|
||||||
private int numErrors;
|
int numErrors;
|
||||||
|
|
||||||
private boolean throwExceptions;
|
ErrorReporter() {
|
||||||
|
|
||||||
/**
|
|
||||||
* @param throwExceptions if true, throw exceptions (good for unit tests) otherwise write to stdout
|
|
||||||
*/
|
|
||||||
public ErrorReporter(boolean throwExceptions) {
|
|
||||||
numErrors = 0;
|
numErrors = 0;
|
||||||
this.throwExceptions = throwExceptions;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reportError(String message, String tokenName, SourcePosition pos) {
|
public void reportError(String message, String tokenName, SourcePosition pos) {
|
||||||
|
System.out.print("ERROR: ");
|
||||||
numErrors++;
|
|
||||||
|
|
||||||
String s = ("ERROR: ");
|
|
||||||
|
|
||||||
for (int p = 0; p < message.length(); p++)
|
for (int p = 0; p < message.length(); p++)
|
||||||
if (message.charAt(p) == '%')
|
if (message.charAt(p) == '%')
|
||||||
s += tokenName;
|
System.out.print(tokenName);
|
||||||
else
|
else
|
||||||
s += message.charAt(p);
|
System.out.print(message.charAt(p));
|
||||||
s += (" " + pos.start + ".." + pos.finish);
|
System.out.println(" " + pos.start + ".." + pos.finish);
|
||||||
|
numErrors++;
|
||||||
if (throwExceptions) {
|
|
||||||
throw new RuntimeException(s);
|
|
||||||
} else {
|
|
||||||
System.out.println(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reportRestriction(String message) {
|
public void reportRestriction(String message) {
|
||||||
System.out.println("RESTRICTION: " + message);
|
System.out.println("RESTRICTION: " + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNumErrors() {
|
|
||||||
return numErrors;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -39,8 +39,8 @@ public class ConstFormalParameter extends FormalParameter implements ConstantDec
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object fpAST) {
|
public boolean equals(Object fpAST) {
|
||||||
if (fpAST instanceof ConstFormalParameter) {
|
if (fpAST instanceof ConstFormalParameter cfpAST) {
|
||||||
return T.equals(((ConstFormalParameter)fpAST).T);
|
return T.equals(cfpAST.T);
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -39,8 +39,8 @@ public class VarFormalParameter extends FormalParameter implements VariableDecla
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object fpAST) {
|
public boolean equals(Object fpAST) {
|
||||||
if (fpAST instanceof VarFormalParameter) {
|
if (fpAST instanceof VarFormalParameter vfpAST) {
|
||||||
return T.equals(((VarFormalParameter)fpAST).T);
|
return T.equals(vfpAST.T);
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,8 +132,7 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
public Void visitCallCommand(CallCommand ast, Void arg) {
|
public Void visitCallCommand(CallCommand ast, Void arg) {
|
||||||
var binding = ast.I.visit(this);
|
var binding = ast.I.visit(this);
|
||||||
|
|
||||||
if (binding instanceof ProcedureDeclaration) {
|
if (binding instanceof ProcedureDeclaration procedure) {
|
||||||
ProcedureDeclaration procedure = (ProcedureDeclaration)binding;
|
|
||||||
ast.APS.visit(this, procedure.getFormals());
|
ast.APS.visit(this, procedure.getFormals());
|
||||||
} else {
|
} else {
|
||||||
reportUndeclaredOrError(binding, ast.I, "\"%\" is not a procedure identifier");
|
reportUndeclaredOrError(binding, ast.I, "\"%\" is not a procedure identifier");
|
||||||
@@ -204,8 +203,7 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
var e2Type = ast.E2.visit(this);
|
var e2Type = ast.E2.visit(this);
|
||||||
var binding = ast.O.visit(this);
|
var binding = ast.O.visit(this);
|
||||||
|
|
||||||
if (binding instanceof BinaryOperatorDeclaration) {
|
if (binding instanceof BinaryOperatorDeclaration bbinding) {
|
||||||
BinaryOperatorDeclaration bbinding = (BinaryOperatorDeclaration)binding;
|
|
||||||
if (bbinding.ARG1 == StdEnvironment.anyType) {
|
if (bbinding.ARG1 == StdEnvironment.anyType) {
|
||||||
// this operator must be "=" or "\="
|
// this operator must be "=" or "\="
|
||||||
checkAndReportError(e1Type.equals(e2Type), "incompatible argument types for \"%\"", ast.O, ast);
|
checkAndReportError(e1Type.equals(e2Type), "incompatible argument types for \"%\"", ast.O, ast);
|
||||||
@@ -224,8 +222,7 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
public TypeDenoter visitCallExpression(CallExpression ast, Void arg) {
|
public TypeDenoter visitCallExpression(CallExpression ast, Void arg) {
|
||||||
var binding = ast.I.visit(this);
|
var binding = ast.I.visit(this);
|
||||||
|
|
||||||
if (binding instanceof FunctionDeclaration) {
|
if (binding instanceof FunctionDeclaration function) {
|
||||||
FunctionDeclaration function = (FunctionDeclaration)binding;
|
|
||||||
ast.APS.visit(this, function.getFormals());
|
ast.APS.visit(this, function.getFormals());
|
||||||
return ast.type = function.getType();
|
return ast.type = function.getType();
|
||||||
}
|
}
|
||||||
@@ -280,8 +277,7 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
var eType = ast.E.visit(this);
|
var eType = ast.E.visit(this);
|
||||||
var binding = ast.O.visit(this);
|
var binding = ast.O.visit(this);
|
||||||
|
|
||||||
if (binding instanceof UnaryOperatorDeclaration) {
|
if (binding instanceof UnaryOperatorDeclaration ubinding) {
|
||||||
UnaryOperatorDeclaration ubinding = (UnaryOperatorDeclaration)binding;
|
|
||||||
checkAndReportError(eType.equals(ubinding.ARG), "wrong argument type for \"%\"", ast.O);
|
checkAndReportError(eType.equals(ubinding.ARG), "wrong argument type for \"%\"", ast.O);
|
||||||
return ast.type = ubinding.RES;
|
return ast.type = ubinding.RES;
|
||||||
}
|
}
|
||||||
@@ -476,9 +472,8 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
@Override
|
@Override
|
||||||
public Void visitConstActualParameter(ConstActualParameter ast, FormalParameter arg) {
|
public Void visitConstActualParameter(ConstActualParameter ast, FormalParameter arg) {
|
||||||
var eType = ast.E.visit(this);
|
var eType = ast.E.visit(this);
|
||||||
if (arg instanceof ConstFormalParameter) {
|
if (arg instanceof ConstFormalParameter param) {
|
||||||
ConstFormalParameter param = (ConstFormalParameter)arg;
|
checkAndReportError(eType.equals(param.T), "wrong type for const actual parameter", ast.E);
|
||||||
checkAndReportError(eType.equals(((ConstFormalParameter)arg).T), "wrong type for const actual parameter", ast.E);
|
|
||||||
} else {
|
} else {
|
||||||
reportError("const actual parameter not expected here", ast);
|
reportError("const actual parameter not expected here", ast);
|
||||||
}
|
}
|
||||||
@@ -488,12 +483,10 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
@Override
|
@Override
|
||||||
public Void visitFuncActualParameter(FuncActualParameter ast, FormalParameter arg) {
|
public Void visitFuncActualParameter(FuncActualParameter ast, FormalParameter arg) {
|
||||||
var binding = ast.I.visit(this);
|
var binding = ast.I.visit(this);
|
||||||
if (binding instanceof FunctionDeclaration) {
|
if (binding instanceof FunctionDeclaration function) {
|
||||||
FunctionDeclaration function = (FunctionDeclaration)binding;
|
|
||||||
var formals = function.getFormals();
|
var formals = function.getFormals();
|
||||||
var functionType = function.getType();
|
var functionType = function.getType();
|
||||||
if (arg instanceof FuncFormalParameter) {
|
if (arg instanceof FuncFormalParameter param) {
|
||||||
FuncFormalParameter param = (FuncFormalParameter)arg;
|
|
||||||
if (!formals.equals(param.getFormals())) {
|
if (!formals.equals(param.getFormals())) {
|
||||||
reportError("wrong signature for function \"%\"", ast.I);
|
reportError("wrong signature for function \"%\"", ast.I);
|
||||||
} else if (!functionType.equals(param.T)) {
|
} else if (!functionType.equals(param.T)) {
|
||||||
@@ -511,11 +504,9 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
@Override
|
@Override
|
||||||
public Void visitProcActualParameter(ProcActualParameter ast, FormalParameter arg) {
|
public Void visitProcActualParameter(ProcActualParameter ast, FormalParameter arg) {
|
||||||
var binding = ast.I.visit(this);
|
var binding = ast.I.visit(this);
|
||||||
if (binding instanceof ProcedureDeclaration) {
|
if (binding instanceof ProcedureDeclaration procedure) {
|
||||||
ProcedureDeclaration procedure = (ProcedureDeclaration)binding;
|
|
||||||
var formals = procedure.getFormals();
|
var formals = procedure.getFormals();
|
||||||
if (arg instanceof ProcFormalParameter) {
|
if (arg instanceof ProcFormalParameter param) {
|
||||||
ProcFormalParameter param = (ProcFormalParameter)arg;
|
|
||||||
checkAndReportError(formals.equals(param.getFormals()), "wrong signature for procedure \"%\"", ast.I);
|
checkAndReportError(formals.equals(param.getFormals()), "wrong signature for procedure \"%\"", ast.I);
|
||||||
} else {
|
} else {
|
||||||
reportError("proc actual parameter not expected here", ast);
|
reportError("proc actual parameter not expected here", ast);
|
||||||
@@ -531,8 +522,7 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
var vType = ast.V.visit(this);
|
var vType = ast.V.visit(this);
|
||||||
if (!ast.V.variable) {
|
if (!ast.V.variable) {
|
||||||
reportError("actual parameter is not a variable", ast.V);
|
reportError("actual parameter is not a variable", ast.V);
|
||||||
} else if (arg instanceof VarFormalParameter) {
|
} else if (arg instanceof VarFormalParameter parameter) {
|
||||||
VarFormalParameter parameter = (VarFormalParameter)arg;
|
|
||||||
checkAndReportError(vType.equals(parameter.T), "wrong type for var actual parameter", ast.V);
|
checkAndReportError(vType.equals(parameter.T), "wrong type for var actual parameter", ast.V);
|
||||||
} else {
|
} else {
|
||||||
reportError("var actual parameter not expected here", ast.V);
|
reportError("var actual parameter not expected here", ast.V);
|
||||||
@@ -548,8 +538,7 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visitMultipleActualParameterSequence(MultipleActualParameterSequence ast, FormalParameterSequence arg) {
|
public Void visitMultipleActualParameterSequence(MultipleActualParameterSequence ast, FormalParameterSequence arg) {
|
||||||
if (arg instanceof MultipleFormalParameterSequence) {
|
if (arg instanceof MultipleFormalParameterSequence formals) {
|
||||||
MultipleFormalParameterSequence formals = (MultipleFormalParameterSequence)arg;
|
|
||||||
ast.AP.visit(this, formals.FP);
|
ast.AP.visit(this, formals.FP);
|
||||||
ast.APS.visit(this, formals.FPS);
|
ast.APS.visit(this, formals.FPS);
|
||||||
} else {
|
} else {
|
||||||
@@ -560,8 +549,7 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visitSingleActualParameterSequence(SingleActualParameterSequence ast, FormalParameterSequence arg) {
|
public Void visitSingleActualParameterSequence(SingleActualParameterSequence ast, FormalParameterSequence arg) {
|
||||||
if (arg instanceof SingleFormalParameterSequence) {
|
if (arg instanceof SingleFormalParameterSequence formal) {
|
||||||
SingleFormalParameterSequence formal = (SingleFormalParameterSequence)arg;
|
|
||||||
ast.AP.visit(this, formal.FP);
|
ast.AP.visit(this, formal.FP);
|
||||||
} else {
|
} else {
|
||||||
reportError("incorrect number of actual parameters", ast);
|
reportError("incorrect number of actual parameters", ast);
|
||||||
@@ -604,8 +592,7 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
@Override
|
@Override
|
||||||
public TypeDenoter visitSimpleTypeDenoter(SimpleTypeDenoter ast, Void arg) {
|
public TypeDenoter visitSimpleTypeDenoter(SimpleTypeDenoter ast, Void arg) {
|
||||||
var binding = ast.I.visit(this);
|
var binding = ast.I.visit(this);
|
||||||
if (binding instanceof TypeDeclaration) {
|
if (binding instanceof TypeDeclaration decl) {
|
||||||
TypeDeclaration decl = (TypeDeclaration)binding;
|
|
||||||
return decl.T;
|
return decl.T;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -692,8 +679,7 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
ast.type = null;
|
ast.type = null;
|
||||||
var vType = ast.V.visit(this);
|
var vType = ast.V.visit(this);
|
||||||
ast.variable = ast.V.variable;
|
ast.variable = ast.V.variable;
|
||||||
if (vType instanceof RecordTypeDenoter) {
|
if (vType instanceof RecordTypeDenoter record) {
|
||||||
RecordTypeDenoter record = (RecordTypeDenoter)vType;
|
|
||||||
ast.type = checkFieldIdentifier(record.FT, ast.I);
|
ast.type = checkFieldIdentifier(record.FT, ast.I);
|
||||||
checkAndReportError(ast.type != StdEnvironment.errorType, "no field \"%\" in this record type",
|
checkAndReportError(ast.type != StdEnvironment.errorType, "no field \"%\" in this record type",
|
||||||
ast.I);
|
ast.I);
|
||||||
@@ -709,12 +695,10 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
ast.type = StdEnvironment.errorType;
|
ast.type = StdEnvironment.errorType;
|
||||||
|
|
||||||
var binding = ast.I.visit(this);
|
var binding = ast.I.visit(this);
|
||||||
if (binding instanceof ConstantDeclaration) {
|
if (binding instanceof ConstantDeclaration constant) {
|
||||||
ConstantDeclaration constant = (ConstantDeclaration)binding;
|
|
||||||
ast.variable = false;
|
ast.variable = false;
|
||||||
return ast.type = constant.getType();
|
return ast.type = constant.getType();
|
||||||
} else if (binding instanceof VariableDeclaration) {
|
} else if (binding instanceof VariableDeclaration variable) {
|
||||||
VariableDeclaration variable = (VariableDeclaration)binding;
|
|
||||||
ast.variable = true;
|
ast.variable = true;
|
||||||
return ast.type = variable.getType();
|
return ast.type = variable.getType();
|
||||||
}
|
}
|
||||||
@@ -730,8 +714,7 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
|
|
||||||
var eType = ast.E.visit(this);
|
var eType = ast.E.visit(this);
|
||||||
if (vType != StdEnvironment.errorType) {
|
if (vType != StdEnvironment.errorType) {
|
||||||
if (vType instanceof ArrayTypeDenoter) {
|
if (vType instanceof ArrayTypeDenoter arrayType) {
|
||||||
ArrayTypeDenoter arrayType = (ArrayTypeDenoter)vType;
|
|
||||||
checkAndReportError(eType.equals(StdEnvironment.integerType), "Integer expression expected here",
|
checkAndReportError(eType.equals(StdEnvironment.integerType), "Integer expression expected here",
|
||||||
ast.E);
|
ast.E);
|
||||||
ast.type = arrayType.T;
|
ast.type = arrayType.T;
|
||||||
@@ -816,16 +799,14 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static TypeDenoter checkFieldIdentifier(FieldTypeDenoter ast, Identifier I) {
|
private static TypeDenoter checkFieldIdentifier(FieldTypeDenoter ast, Identifier I) {
|
||||||
if (ast instanceof MultipleFieldTypeDenoter) {
|
if (ast instanceof MultipleFieldTypeDenoter ft) {
|
||||||
MultipleFieldTypeDenoter ft = (MultipleFieldTypeDenoter)ast;
|
|
||||||
if (ft.I.spelling.compareTo(I.spelling) == 0) {
|
if (ft.I.spelling.compareTo(I.spelling) == 0) {
|
||||||
I.decl = ast;
|
I.decl = ast;
|
||||||
return ft.T;
|
return ft.T;
|
||||||
} else {
|
} else {
|
||||||
return checkFieldIdentifier(ft.FT, I);
|
return checkFieldIdentifier(ft.FT, I);
|
||||||
}
|
}
|
||||||
} else if (ast instanceof SingleFieldTypeDenoter) {
|
} else if (ast instanceof SingleFieldTypeDenoter ft) {
|
||||||
SingleFieldTypeDenoter ft = (SingleFieldTypeDenoter)ast;
|
|
||||||
if (ft.I.spelling.compareTo(I.spelling) == 0) {
|
if (ft.I.spelling.compareTo(I.spelling) == 0) {
|
||||||
I.decl = ast;
|
I.decl = ast;
|
||||||
return ft.T;
|
return ft.T;
|
||||||
|
|||||||
@@ -14,36 +14,26 @@
|
|||||||
|
|
||||||
package triangle.syntacticAnalyzer;
|
package triangle.syntacticAnalyzer;
|
||||||
|
|
||||||
import java.net.URL;
|
|
||||||
|
|
||||||
public class SourceFile {
|
public class SourceFile {
|
||||||
|
|
||||||
public static final char EOL = '\n';
|
public static final char EOL = '\n';
|
||||||
public static final char EOT = '\u0000';
|
public static final char EOT = '\u0000';
|
||||||
|
|
||||||
java.io.File sourceFile;
|
java.io.File sourceFile;
|
||||||
java.io.InputStream source;
|
java.io.FileInputStream source;
|
||||||
int currentLine;
|
int currentLine;
|
||||||
|
|
||||||
public static SourceFile ofPath(String pathname) {
|
public static SourceFile ofPath(String pathname) {
|
||||||
try {
|
try {
|
||||||
SourceFile sf = new SourceFile();
|
return new SourceFile(pathname);
|
||||||
sf.sourceFile = new java.io.File(pathname);
|
|
||||||
sf.source = new java.io.FileInputStream(sf.sourceFile);
|
|
||||||
return sf;
|
|
||||||
} catch (java.io.IOException s) {
|
} catch (java.io.IOException s) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SourceFile fromResource(String handle) {
|
private SourceFile(String pathname) throws java.io.FileNotFoundException {
|
||||||
SourceFile sf = new SourceFile();
|
sourceFile = new java.io.File(pathname);
|
||||||
//sf.sourceFile = new java.io.File(pathname);
|
source = new java.io.FileInputStream(sourceFile);
|
||||||
sf.source = sf.getClass().getResourceAsStream(handle);
|
|
||||||
return sf;
|
|
||||||
}
|
|
||||||
|
|
||||||
private SourceFile() {
|
|
||||||
currentLine = 1;
|
currentLine = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,13 +42,6 @@ public class Drawer {
|
|||||||
|
|
||||||
FontMetrics fontMetrics = frame.getFontMetrics(font);
|
FontMetrics fontMetrics = frame.getFontMetrics(font);
|
||||||
|
|
||||||
// another class of visitor is used for drawing the tree: LayoutVisitor
|
|
||||||
// LayoutVisitor is passed to the AST which, in turn, calls visitProgram
|
|
||||||
// and then each AST node is visited. This ultimately constructs a
|
|
||||||
// DrawingTree, which is structurally the same as the AST but is decorated
|
|
||||||
// with coordinates (and has only DrawingTree objects as nodes)
|
|
||||||
// Each DrawingTree object knows how to paint itself, so it's passed to a
|
|
||||||
// DrawerPanel and DrawerFrame for display
|
|
||||||
LayoutVisitor layout = new LayoutVisitor(fontMetrics);
|
LayoutVisitor layout = new LayoutVisitor(fontMetrics);
|
||||||
theDrawing = (DrawingTree) theAST.visit(layout, null);
|
theDrawing = (DrawingTree) theAST.visit(layout, null);
|
||||||
theDrawing.position(new Point(2048, 10));
|
theDrawing.position(new Point(2048, 10));
|
||||||
|
|||||||
@@ -14,9 +14,6 @@
|
|||||||
|
|
||||||
package triangle.treeDrawer;
|
package triangle.treeDrawer;
|
||||||
|
|
||||||
/**
|
|
||||||
* used to keep track of the position for components in the tree to be drawn
|
|
||||||
*/
|
|
||||||
class Polyline {
|
class Polyline {
|
||||||
int dx, dy;
|
int dx, dy;
|
||||||
Polyline link;
|
Polyline link;
|
||||||
|
|||||||
@@ -1,84 +0,0 @@
|
|||||||
package triangle.syntacticAnalyser;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNotEquals;
|
|
||||||
import static org.junit.Assert.assertThrows;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.function.ThrowingRunnable;
|
|
||||||
|
|
||||||
import triangle.ErrorReporter;
|
|
||||||
import triangle.syntacticAnalyzer.Parser;
|
|
||||||
import triangle.syntacticAnalyzer.Scanner;
|
|
||||||
import triangle.syntacticAnalyzer.SourceFile;
|
|
||||||
|
|
||||||
public class TestScanner {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testHi() {
|
|
||||||
compileExpectSuccess("/hi.tri");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testHiNewComment() {
|
|
||||||
compileExpectFailure("/hi-newcomment.tri");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testHiNewComment2() {
|
|
||||||
compileExpectFailure("/hi-newcomment2.tri");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testBarDemo() {
|
|
||||||
compileExpectFailure("/bardemo.tri");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testRepeatUntil() {
|
|
||||||
compileExpectFailure("/repeatuntil.tri");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void compileExpectSuccess(String filename) {
|
|
||||||
// build.gradle has a line sourceSets.test.resources.srcDir file("$rootDir/programs")
|
|
||||||
// which adds the programs directory to the list of places Java can easily find files
|
|
||||||
// getResource() below searches for a file, which is in /programs
|
|
||||||
//SourceFile source = SourceFile.ofPath(this.getClass().getResource(filename).getFile().toString());
|
|
||||||
SourceFile source = SourceFile.fromResource(filename);
|
|
||||||
|
|
||||||
Scanner scanner = new Scanner(source);
|
|
||||||
ErrorReporter reporter = new ErrorReporter(true);
|
|
||||||
Parser parser = new Parser(scanner, reporter);
|
|
||||||
|
|
||||||
parser.parseProgram();
|
|
||||||
|
|
||||||
// we should get to here with no exceptions
|
|
||||||
|
|
||||||
assertEquals("Problem compiling " + filename, 0, reporter.getNumErrors());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void compileExpectFailure(String filename) {
|
|
||||||
//SourceFile source = SourceFile.ofPath(this.getClass().getResource(filename).getFile().toString());
|
|
||||||
SourceFile source = SourceFile.fromResource(filename);
|
|
||||||
Scanner scanner = new Scanner(source);
|
|
||||||
ErrorReporter reporter = new ErrorReporter(true);
|
|
||||||
Parser parser = new Parser(scanner, reporter);
|
|
||||||
|
|
||||||
// we expect an exception here as the program has invalid syntax
|
|
||||||
assertThrows(RuntimeException.class, new ThrowingRunnable() {
|
|
||||||
public void run(){
|
|
||||||
parser.parseProgram();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// currently this program will fail
|
|
||||||
assertNotEquals("Problem compiling " + filename, 0, reporter.getNumErrors());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
apply plugin: 'java'
|
|
||||||
|
|
||||||
sourceCompatibility = 11
|
|
||||||
|
|
||||||
subprojects.each { subproject ->
|
|
||||||
evaluationDependsOn(subproject.path)
|
|
||||||
}
|
|
||||||
|
|
||||||
jar {
|
|
||||||
from subprojects.sourceSets.main.output
|
|
||||||
}
|
|
||||||
Binary file not shown.
@@ -1,15 +0,0 @@
|
|||||||
! this won't work until after some work in the practicals
|
|
||||||
let
|
|
||||||
var a : Integer;
|
|
||||||
var b : Integer
|
|
||||||
|
|
||||||
in
|
|
||||||
begin
|
|
||||||
a := 1;
|
|
||||||
b := 2;
|
|
||||||
|
|
||||||
putint(a);
|
|
||||||
putint(b);
|
|
||||||
putint(|a);
|
|
||||||
putint(|b)
|
|
||||||
end
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
! this won't work until after some work in the practicals
|
|
||||||
|
|
||||||
# new comment
|
|
||||||
|
|
||||||
begin
|
|
||||||
put('H'); put('i'); put('!')
|
|
||||||
end
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
! this won't work until after some work in the practicals
|
|
||||||
|
|
||||||
# new comment
|
|
||||||
|
|
||||||
$
|
|
||||||
another new comment
|
|
||||||
$
|
|
||||||
|
|
||||||
begin
|
|
||||||
put('H'); put('i'); put('!')
|
|
||||||
end
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
! this won't work until after some lab work
|
|
||||||
|
|
||||||
let
|
|
||||||
var a : Integer
|
|
||||||
in
|
|
||||||
begin
|
|
||||||
a := 0;
|
|
||||||
repeat
|
|
||||||
begin
|
|
||||||
put('a');
|
|
||||||
a := a + 1;
|
|
||||||
end
|
|
||||||
until a < 5
|
|
||||||
end
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
let
|
|
||||||
var a : Integer
|
|
||||||
in
|
|
||||||
begin
|
|
||||||
a := 0;
|
|
||||||
while a < 5 do
|
|
||||||
begin
|
|
||||||
put('a');
|
|
||||||
a := a + 1;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
RED='\033[0;31m' # Red
|
|
||||||
GRN='\033[0;32m' # Green
|
|
||||||
NC='\033[0m' # No Color
|
|
||||||
|
|
||||||
file=""
|
|
||||||
|
|
||||||
if [ -z "$1" ]
|
|
||||||
then
|
|
||||||
printf "${RED}Usage: $0 file${NC}"
|
|
||||||
exit 1 # Exit with code 1, failure
|
|
||||||
else
|
|
||||||
file=$1
|
|
||||||
fi
|
|
||||||
|
|
||||||
printf "${GRN}Compiling file: $1.tri to $1.tam ...${NC}\n"
|
|
||||||
# Compile to tam
|
|
||||||
java -cp build/libs/Triangle-Tools.jar triangle.Compiler programs/$1.tri -o=$1.tam &> /dev/null
|
|
||||||
|
|
||||||
printf "${GRN}Running file: $1.tam ...${NC}\n"
|
|
||||||
# Run the Program
|
|
||||||
java -cp build/libs/Triangle-Tools.jar triangle.abstractMachine.Interpreter $1.tam
|
|
||||||
Reference in New Issue
Block a user