35 Commits
Author SHA1 Message Date
simonkellet 4341d02572 end of practical 2 2022-09-23 16:59:48 +01:00
simonkellet 32bd99d78b new lines to check if it is still working 2022-09-23 15:33:28 +01:00
simonkellet fab24be627 added success test for multi-line 2022-09-23 15:32:59 +01:00
simonkellet 6b7fdd3bb0 added multi-line comment support 2022-09-23 15:32:37 +01:00
simonkellet f69942533f works with error code 2022-09-23 14:14:35 +01:00
simonkellet a647259dc2 added readme 2022-09-20 18:28:06 +01:00
simonkellet befe8f93a2 eclipse and gradle changes 2022-09-20 18:24:46 +01:00
simonkellet 7c64cb7eff added new tri files 2022-09-20 18:24:12 +01:00
simonkellet a4c304f58d changes to tam 2022-09-20 18:23:45 +01:00
simonkellet 36f3fac111 added build folder ignore 2022-09-20 18:23:15 +01:00
simonkellet cd280e3c4b added test case for new comment 2022-09-20 18:22:25 +01:00
simonkellet d28b3c7403 added new comment 2022-09-20 18:22:03 +01:00
simonkellet 3f58bdfad9 added bash file to compile and run files quicker 2022-09-20 12:16:52 +01:00
simonkellet 6377dd8250 added ignore to .tam files 2022-09-20 12:06:27 +01:00
simonkellet 955517af29 added build dir 2022-09-20 11:59:31 +01:00
Sandy Brownlee 3dade998ba possible fix for windows 2022-09-15 17:42:16 +01:00
Sandy Brownlee 048cbac0b4 Added bardemo 2022-09-15 15:54:24 +01:00
Sandy Brownlee 77846e5800 Merge branch 'java11' of github.com:sandybrownlee/Triangle-Tools into java11 2022-09-15 11:27:06 +01:00
Sandy Brownlee f4e1a08f61 minor fix to tests 2022-09-09 17:08:30 +01:00
Sandy Brownlee c3c2076d9e repeat until 2022-09-09 16:33:13 +01:00
Sandy Brownlee 64c25f1486 Added pounddemo 2022-09-09 15:46:22 +01:00
Sandy Brownlee c469297712 Amended comment 2 type 2022-09-09 15:26:16 +01:00
Sandy Brownlee a2cac87f81 Changed master project to Java 11 2022-09-09 14:39:53 +01:00
Sandy Brownlee b3375e2050 Added single jar build to root project 2022-09-09 12:20:49 +01:00
Sandy Brownlee 11be265081 Added object filename option to Compiler 2022-09-09 12:02:55 +01:00
Sandy Brownlee 6820b55cf1 Streamlined unit tests 2022-09-08 19:47:22 +01:00
Sandy Brownlee f709234c4e Added unit tests for bad syntax in hi-newcomment.tri 2022-09-08 19:38:02 +01:00
Sandy Brownlee 07b0d85bdf added test 2022-09-08 17:57:48 +01:00
Sandy Brownlee e8780a5b11 Merge branch 'java11' of github.com:sandybrownlee/Triangle-Tools into java11 2022-09-08 09:23:21 +01:00
Sandy Brownlee 90977fb64f added comment tests 2022-09-08 09:23:04 +01:00
Sandy BrownleeandGitHub 3150ae8fa9 Merge branch 'deryckb:main' into java11 2022-09-07 19:56:44 +01:00
Sandy Brownlee a026ec3840 add while example 2022-09-05 17:15:08 +01:00
Sandy Brownlee 3de9d5e0cc Merge branch 'main' into java11 2022-09-01 16:48:54 +01:00
Sandy Brownlee ee57c9f711 added assignments example 2022-08-19 15:00:32 +01:00
Sandy Brownlee 01f468f07d Java 11 compatibility 2022-08-18 13:22:06 +01:00
35 changed files with 518 additions and 61 deletions
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>
+6
View File
@@ -60,3 +60,9 @@ 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
# Build folder
/build/
+6
View File
@@ -5,6 +5,11 @@
<projects> <projects>
</projects> </projects>
<buildSpec> <buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand> <buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name> <name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments> <arguments>
@@ -12,6 +17,7 @@
</buildCommand> </buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature> <nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures> </natures>
</projectDescription> </projectDescription>
+2
View File
@@ -0,0 +1,2 @@
# A5 Triangle Compiler
@@ -1,7 +1,7 @@
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'application' apply plugin: 'application'
sourceCompatibility = 17 sourceCompatibility = 11
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 = 17 sourceCompatibility = 11
dependencies { dependencies {
implementation project(':Triangle.AbstractMachine') implementation project(':Triangle.AbstractMachine')
+1 -1
View File
@@ -1,4 +1,4 @@
apply plugin: 'java-library' apply plugin: 'java-library'
apply plugin: 'eclipse' apply plugin: 'eclipse'
sourceCompatibility = 17 sourceCompatibility = 11
+7
View File
@@ -20,4 +20,11 @@
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature> <nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures> </natures>
<linkedResources>
<link>
<name>programs</name>
<type>2</type>
<location>/home/simon/Documents/Uni/CS/CSCU9A5/Triangle-Tools/programs</location>
</link>
</linkedResources>
</projectDescription> </projectDescription>
+10 -1
View File
@@ -1,12 +1,21 @@
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'application' apply plugin: 'application'
sourceCompatibility = 17 sourceCompatibility = 11
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,6 +34,8 @@ 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;
@@ -71,7 +73,7 @@ public class Compiler {
} }
scanner = new Scanner(source); scanner = new Scanner(source);
reporter = new ErrorReporter(); reporter = new ErrorReporter(false);
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);
@@ -80,7 +82,7 @@ public class Compiler {
// scanner.enableDebugging(); // scanner.enableDebugging();
theAST = parser.parseProgram(); // 1st pass theAST = parser.parseProgram(); // 1st pass
if (reporter.numErrors == 0) { if (reporter.getNumErrors() == 0) {
// if (showingAST) { // if (showingAST) {
// drawer.draw(theAST); // drawer.draw(theAST);
// } // }
@@ -89,13 +91,13 @@ public class Compiler {
if (showingAST) { if (showingAST) {
drawer.draw(theAST); drawer.draw(theAST);
} }
if (reporter.numErrors == 0) { if (reporter.getNumErrors() == 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.numErrors == 0); boolean successful = (reporter.getNumErrors() == 0);
if (successful) { if (successful) {
emitter.saveObjectProgram(objectName); emitter.saveObjectProgram(objectName);
System.out.println("Compilation was successful."); System.out.println("Compilation was successful.");
@@ -114,16 +116,29 @@ 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 [tree]"); System.out.println("Usage: tc filename [-o=outputfilename] [tree]");
System.exit(1); System.exit(1);
} }
String sourceName = args[0]; parseArgs(args);
boolean tree = (args.length > 1 && args[1].equalsIgnoreCase("tree"));
var compiledOK = compileProgram(sourceName, objectName, tree, false);
if (!tree) { String sourceName = args[0];
var compiledOK = compileProgram(sourceName, objectName, showTree, false);
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,25 +18,44 @@ import triangle.syntacticAnalyzer.SourcePosition;
public class ErrorReporter { public class ErrorReporter {
int numErrors; private int numErrors;
ErrorReporter() { private boolean throwExceptions;
/**
* @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) == '%')
System.out.print(tokenName); s += tokenName;
else else
System.out.print(message.charAt(p)); s += message.charAt(p);
System.out.println(" " + pos.start + ".." + pos.finish); s += (" " + 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;
}
} }
@@ -0,0 +1,21 @@
package triangle.abstractSyntaxTrees.commands;
import triangle.abstractSyntaxTrees.expressions.Expression;
import triangle.abstractSyntaxTrees.visitors.CommandVisitor;
import triangle.syntacticAnalyzer.SourcePosition;
public class RepeatCommand extends Command {
public RepeatCommand(Expression eAST, Command cAST, SourcePosition position) {
super(position);
E = eAST;
C = cAST;
}
public <TArg, TResult> TResult visit(CommandVisitor<TArg, TResult> v, TArg arg) {
return v.visitRepeatCommand(this, arg);
}
public final Expression E;
public final Command C;
}
@@ -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 cfpAST) { if (fpAST instanceof ConstFormalParameter) {
return T.equals(cfpAST.T); return T.equals(((ConstFormalParameter)fpAST).T);
} else { } else {
return false; return false;
} }
@@ -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 vfpAST) { if (fpAST instanceof VarFormalParameter) {
return T.equals(vfpAST.T); return T.equals(((VarFormalParameter)fpAST).T);
} else { } else {
return false; return false;
} }
@@ -5,11 +5,11 @@ import triangle.abstractSyntaxTrees.commands.CallCommand;
import triangle.abstractSyntaxTrees.commands.EmptyCommand; import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand; import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand; import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand; import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand; import triangle.abstractSyntaxTrees.commands.WhileCommand;
public interface CommandVisitor<TArg, TResult> { public interface CommandVisitor<TArg, TResult> {
TResult visitAssignCommand(AssignCommand ast, TArg arg); TResult visitAssignCommand(AssignCommand ast, TArg arg);
TResult visitCallCommand(CallCommand ast, TArg arg); TResult visitCallCommand(CallCommand ast, TArg arg);
@@ -24,4 +24,6 @@ public interface CommandVisitor<TArg, TResult> {
TResult visitWhileCommand(WhileCommand ast, TArg arg); TResult visitWhileCommand(WhileCommand ast, TArg arg);
TResult visitRepeatCommand(RepeatCommand ast, TArg arg);
} }
@@ -38,6 +38,7 @@ import triangle.abstractSyntaxTrees.commands.CallCommand;
import triangle.abstractSyntaxTrees.commands.EmptyCommand; import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand; import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand; import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand; import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand; import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration; import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration;
@@ -181,6 +182,11 @@ public final class Encoder implements ActualParameterVisitor<Frame, Integer>,
return null; return null;
} }
@Override
public Void visitRepeatCommand(RepeatCommand ast, Frame frame) {
return null;
}
// Expressions // Expressions
@Override @Override
public Integer visitArrayExpression(ArrayExpression ast, Frame frame) { public Integer visitArrayExpression(ArrayExpression ast, Frame frame) {
@@ -34,6 +34,7 @@ import triangle.abstractSyntaxTrees.commands.CallCommand;
import triangle.abstractSyntaxTrees.commands.EmptyCommand; import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand; import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand; import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand; import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand; import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration; import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration;
@@ -132,7 +133,8 @@ 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 procedure) { if (binding instanceof ProcedureDeclaration) {
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");
@@ -184,6 +186,11 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
return null; return null;
} }
@Override
public Void visitRepeatCommand(RepeatCommand ast, Void arg) {
return null;
}
// Expressions // Expressions
// Returns the TypeDenoter denoting the type of the expression. Does // Returns the TypeDenoter denoting the type of the expression. Does
@@ -203,7 +210,8 @@ 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 bbinding) { if (binding instanceof BinaryOperatorDeclaration) {
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);
@@ -222,7 +230,8 @@ 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 function) { if (binding instanceof FunctionDeclaration) {
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();
} }
@@ -277,7 +286,8 @@ 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 ubinding) { if (binding instanceof UnaryOperatorDeclaration) {
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;
} }
@@ -472,8 +482,9 @@ 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 param) { if (arg instanceof ConstFormalParameter) {
checkAndReportError(eType.equals(param.T), "wrong type for const actual parameter", ast.E); ConstFormalParameter param = (ConstFormalParameter)arg;
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);
} }
@@ -483,10 +494,12 @@ 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 function) { if (binding instanceof FunctionDeclaration) {
FunctionDeclaration function = (FunctionDeclaration)binding;
var formals = function.getFormals(); var formals = function.getFormals();
var functionType = function.getType(); var functionType = function.getType();
if (arg instanceof FuncFormalParameter param) { if (arg instanceof FuncFormalParameter) {
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)) {
@@ -504,9 +517,11 @@ 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 procedure) { if (binding instanceof ProcedureDeclaration) {
ProcedureDeclaration procedure = (ProcedureDeclaration)binding;
var formals = procedure.getFormals(); var formals = procedure.getFormals();
if (arg instanceof ProcFormalParameter param) { if (arg instanceof ProcFormalParameter) {
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);
@@ -522,7 +537,8 @@ 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 parameter) { } else if (arg instanceof VarFormalParameter) {
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);
@@ -538,7 +554,8 @@ 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 formals) { if (arg instanceof MultipleFormalParameterSequence) {
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 {
@@ -549,7 +566,8 @@ 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 formal) { if (arg instanceof SingleFormalParameterSequence) {
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);
@@ -592,7 +610,8 @@ 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 decl) { if (binding instanceof TypeDeclaration) {
TypeDeclaration decl = (TypeDeclaration)binding;
return decl.T; return decl.T;
} }
@@ -679,7 +698,8 @@ 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 record) { if (vType instanceof RecordTypeDenoter) {
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);
@@ -695,10 +715,12 @@ 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 constant) { if (binding instanceof ConstantDeclaration) {
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 variable) { } else if (binding instanceof VariableDeclaration) {
VariableDeclaration variable = (VariableDeclaration)binding;
ast.variable = true; ast.variable = true;
return ast.type = variable.getType(); return ast.type = variable.getType();
} }
@@ -714,7 +736,8 @@ 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 arrayType) { if (vType instanceof ArrayTypeDenoter) {
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;
@@ -799,14 +822,16 @@ 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 ft) { if (ast instanceof MultipleFieldTypeDenoter) {
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 ft) { } else if (ast instanceof SingleFieldTypeDenoter) {
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;
@@ -37,6 +37,7 @@ import triangle.abstractSyntaxTrees.commands.Command;
import triangle.abstractSyntaxTrees.commands.EmptyCommand; import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand; import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand; import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand; import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand; import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.ConstDeclaration; import triangle.abstractSyntaxTrees.declarations.ConstDeclaration;
@@ -320,7 +321,7 @@ public class Parser {
} }
break; break;
case Token.WHILE: { case Token.WHILE: { //while expr. do command
acceptIt(); acceptIt();
Expression eAST = parseExpression(); Expression eAST = parseExpression();
accept(Token.DO); accept(Token.DO);
@@ -330,6 +331,16 @@ public class Parser {
} }
break; break;
case Token.REPEAT: { //repeat command until expr.
acceptIt();
Command cAST = parseSingleCommand();
accept(Token.UNTIL); //check that there is a "until"
Expression eAST = parseExpression();
finish(commandPos);
commandAST = new RepeatCommand(eAST, cAST, commandPos);
}
break;
case Token.SEMICOLON: case Token.SEMICOLON:
case Token.END: case Token.END:
case Token.ELSE: case Token.ELSE:
@@ -35,7 +35,7 @@ public final class Scanner {
private boolean isOperator(char c) { private boolean isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '=' || c == '<' || c == '>' || c == '\\' return (c == '+' || c == '-' || c == '*' || c == '/' || c == '=' || c == '<' || c == '>' || c == '\\'
|| c == '&' || c == '@' || c == '%' || c == '^' || c == '?'); || c == '&' || c == '@' || c == '%' || c == '^' || c == '?' || c == '|');
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@@ -64,7 +64,7 @@ public final class Scanner {
private void scanSeparator() { private void scanSeparator() {
switch (currentChar) { switch (currentChar) {
// comment // ! comment
case '!': { case '!': {
takeIt(); takeIt();
while ((currentChar != SourceFile.EOL) && (currentChar != SourceFile.EOT)) while ((currentChar != SourceFile.EOL) && (currentChar != SourceFile.EOT))
@@ -74,6 +74,28 @@ public final class Scanner {
} }
break; break;
// new type of comment, the # comment, same as before when it comes to code
case '#':{
takeIt();
while((currentChar != SourceFile.EOL) && (currentChar != SourceFile.EOT))
takeIt();
if(currentChar == SourceFile.EOL)
takeIt();
}
break;
// new type of comment, the multi-line $ comment
case '$':{
takeIt();
//check if it is not the comment char or EOT, skipping new lines (no EOL)
while((currentChar != '$') && (currentChar != SourceFile.EOT))
takeIt();
//if we reach another $, takeIt and move on, we've reached the end of the multi-line comment
if(currentChar == '$')
takeIt();
}
break;
// whitespace // whitespace
case ' ': case ' ':
case '\n': case '\n':
@@ -178,6 +200,13 @@ public final class Scanner {
takeIt(); takeIt();
return Token.OPERATOR; return Token.OPERATOR;
// TODO: Bar operator, week 3
case '|':
takeIt();
while(isOperator(currentChar))
takeIt();
return Token.OPERATOR;
case '\'': case '\'':
takeIt(); takeIt();
takeIt(); // the quoted character takeIt(); // the quoted character
@@ -252,7 +281,7 @@ public final class Scanner {
currentlyScanningToken = false; currentlyScanningToken = false;
// skip any whitespace or comments // skip any whitespace or comments
while (currentChar == '!' || currentChar == ' ' || currentChar == '\n' || currentChar == '\r' while (currentChar == '!' || currentChar == ' ' || currentChar == '\n' || currentChar == '\r'
|| currentChar == '\t') || currentChar == '\t' || currentChar == '#' || currentChar == '$')
scanSeparator(); scanSeparator();
currentlyScanningToken = true; currentlyScanningToken = true;
@@ -14,26 +14,36 @@
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.FileInputStream source; java.io.InputStream source;
int currentLine; int currentLine;
public static SourceFile ofPath(String pathname) { public static SourceFile ofPath(String pathname) {
try { try {
return new SourceFile(pathname); SourceFile sf = new SourceFile();
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;
} }
} }
private SourceFile(String pathname) throws java.io.FileNotFoundException { public static SourceFile fromResource(String handle) {
sourceFile = new java.io.File(pathname); SourceFile sf = new SourceFile();
source = new java.io.FileInputStream(sourceFile); //sf.sourceFile = new java.io.File(pathname);
sf.source = sf.getClass().getResourceAsStream(handle);
return sf;
}
private SourceFile() {
currentLine = 1; currentLine = 1;
} }
@@ -64,20 +64,20 @@ final class Token extends Object {
// reserved words - must be in alphabetical order... // reserved words - must be in alphabetical order...
ARRAY = 4, BEGIN = 5, CONST = 6, DO = 7, ELSE = 8, END = 9, FUNC = 10, IF = 11, IN = 12, LET = 13, OF = 14, ARRAY = 4, BEGIN = 5, CONST = 6, DO = 7, ELSE = 8, END = 9, FUNC = 10, IF = 11, IN = 12, LET = 13, OF = 14,
PROC = 15, RECORD = 16, THEN = 17, TYPE = 18, VAR = 19, WHILE = 20, PROC = 15, RECORD = 16, REPEAT = 17, THEN = 18, TYPE = 19, UNTIL = 20, VAR = 21, WHILE = 22,
// punctuation... // punctuation...
DOT = 21, COLON = 22, SEMICOLON = 23, COMMA = 24, BECOMES = 25, IS = 26, DOT = 23, COLON = 24, SEMICOLON = 25, COMMA = 26, BECOMES = 27, IS = 28,
// brackets... // brackets...
LPAREN = 27, RPAREN = 28, LBRACKET = 29, RBRACKET = 30, LCURLY = 31, RCURLY = 32, LPAREN = 29, RPAREN = 30, LBRACKET = 31, RBRACKET = 32, LCURLY = 33, RCURLY = 34,
// special tokens... // special tokens...
EOT = 33, ERROR = 34; EOT = 35, ERROR = 36;
private static String[] tokenTable = new String[] { "<int>", "<char>", "<identifier>", "<operator>", "array", private static String[] tokenTable = new String[] { "<int>", "<char>", "<identifier>", "<operator>", "array",
"begin", "const", "do", "else", "end", "func", "if", "in", "let", "of", "proc", "record", "then", "type", "begin", "const", "do", "else", "end", "func", "if", "in", "let", "of", "proc", "record", "repeat", "then", "type",
"var", "while", ".", ":", ";", ",", ":=", "~", "(", ")", "[", "]", "{", "}", "", "<error>" }; "until", "var", "while", ".", ":", ";", ",", ":=", "~", "(", ")", "[", "]", "{", "}", "", "<error>" };
private final static int firstReservedWord = Token.ARRAY, lastReservedWord = Token.WHILE; private final static int firstReservedWord = Token.ARRAY, lastReservedWord = Token.WHILE;
@@ -42,6 +42,13 @@ 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));
@@ -33,6 +33,7 @@ import triangle.abstractSyntaxTrees.commands.CallCommand;
import triangle.abstractSyntaxTrees.commands.EmptyCommand; import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand; import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand; import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand; import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand; import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration; import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration;
@@ -159,6 +160,11 @@ public class LayoutVisitor implements ActualParameterVisitor<Void, DrawingTree>,
return layoutBinary("WhileCom.", d1, d2); return layoutBinary("WhileCom.", d1, d2);
} }
@Override
public DrawingTree visitRepeatCommand(RepeatCommand ast, Void obj) {
return null;
}
// Expressions // Expressions
@Override @Override
public DrawingTree visitArrayExpression(ArrayExpression ast, Void obj) { public DrawingTree visitArrayExpression(ArrayExpression ast, Void obj) {
@@ -14,6 +14,9 @@
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;
@@ -0,0 +1,90 @@
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() {
compileExpectSuccess("/hi-newcomment.tri");
}
@Test
public void testHiNewComment2() {
compileExpectSuccess("/hi-newcomment2.tri");
}
@Test
public void testBarDemo() {
compileExpectSuccess("/bardemo.tri"); //TODO: Change this to failure
}
@Test
public void testRepeatUntil() {
compileExpectSuccess("/repeatuntil.tri");
}
@Test
public void testAdd() {
//compileExpectFailure("/addd.tri"); //fail here!
compileExpectFailure("/add.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());
}
}
+11
View File
@@ -0,0 +1,11 @@
apply plugin: 'java'
sourceCompatibility = 11
subprojects.each { subproject ->
evaluationDependsOn(subproject.path)
}
jar {
from subprojects.sourceSets.main.output
}
Binary file not shown.
+46
View File
@@ -0,0 +1,46 @@
! Program that will: read two ints, print their sum and their product. If they are the same, print "Same"
! Global Consts, Funcs.
let
type String ~ array 6 of Char; ! String type implemented, not used yet
var num1 : Integer;
var num2 : Integer;
func sum(x : Integer, y : Integer) : Integer ~
x + y; ! Return x + y
func product(x : Integer, y : Integer) : Integer ~
x * y; ! Return x * y
func isSame(x : Integer, y : Integer) : Boolean ~
if x = y then true else false
! Main Loop
in
begin
num1 := 0;
num2 := 0; ! Init. values to 0
getint(var num1);
getint(var num2); ! Get user input
! Sum
putint(num1); put('+'); put(' '); putint(num2); put(' '); put('='); put(' ');
putint(sum(num1, num2)); puteol();
puteol();
! Product
putint(num1); put(' '); put('*'); put(' '); putint(num2); put(' '); put('='); put(' ');
putint(product(num1, num2)); puteol();
if isSame(num1, num2) = true then
put('S'); put('a'); put('m'); put('e'); puteol();
else
puteol();
end
+15
View File
@@ -0,0 +1,15 @@
! 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
+30
View File
@@ -0,0 +1,30 @@
! Program with a variety of contextual errors.
let
type String ~ array 4 of Char;
type Name ~ array 3 of String;
type Rec ~ record x: Integer, x: Integer end;
var me: Name;
var silly : maxint;
var silly: Rec;
proc putstr (s: String) ~
let var i: Integer
in
begin
s[4] := ' ';
i := 0;
while i do
begin i := i+true;
put (s[\i])
end
end
in
begin
me[true] := ['T','i','n','y'];
me[2][2] := 0;
put (me[1]); put (4); put ();
putstr (initials (me)); puteol ()
end
+7
View File
@@ -0,0 +1,7 @@
! this won't work until after some work in the practicals
# new comment
begin
put('H'); put('i'); put('!')
end
+14
View File
@@ -0,0 +1,14 @@
! this won't work until after some work in the practicals
# new comment
$
another new comment
another new comment
another new comment
another new comment
$
begin
put('H'); put('i'); put('!')
end
+14
View File
@@ -0,0 +1,14 @@
! 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
+11
View File
@@ -0,0 +1,11 @@
let
var a : Integer
in
begin
a := 0;
while a < 5 do
begin
put('a');
a := a + 1;
end
end
Executable
+29
View File
@@ -0,0 +1,29 @@
#!/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}[INFO] Compiling file: $1.tri to $1.tam ...${NC}\n"
# Compile to tam
if java -cp build/libs/Triangle-Tools.jar triangle.Compiler programs/$1.tri -o=$1.tam &> /dev/null #quiet
then
printf "${GRN}[INFO] Running file: $1.tam ...${NC}\n"
java -cp build/libs/Triangle-Tools.jar triangle.abstractMachine.Interpreter $1.tam
else
printf "${RED}[ERROR] Could not complie $1.tri ...${NC}"
err=$(java -cp build/libs/Triangle-Tools.jar triangle.Compiler programs/$1.tri -o=$1.tam)
printf "${RED}\n$err\n${NC}"
exit 1
fi