End of Prac2

main
simonkellet 1 year ago
parent 1a339eb900
commit 841a7c9af2
  1. 6
      .classpath
  2. 1
      .gitignore
  3. 23
      .project
  4. 13
      .settings/org.eclipse.buildship.core.prefs
  5. 4
      .settings/org.eclipse.jdt.core.prefs
  6. 2
      Triangle.AbstractMachine.Disassembler/.settings/org.eclipse.buildship.core.prefs
  7. 4
      Triangle.AbstractMachine.Disassembler/.settings/org.eclipse.jdt.core.prefs
  8. 1
      Triangle.AbstractMachine.Disassembler/bin/.gitignore
  9. 2
      Triangle.AbstractMachine.Interpreter/.settings/org.eclipse.buildship.core.prefs
  10. 4
      Triangle.AbstractMachine.Interpreter/.settings/org.eclipse.jdt.core.prefs
  11. 1
      Triangle.AbstractMachine.Interpreter/bin/.gitignore
  12. 2
      Triangle.AbstractMachine/.settings/org.eclipse.buildship.core.prefs
  13. 4
      Triangle.AbstractMachine/.settings/org.eclipse.jdt.core.prefs
  14. 1
      Triangle.AbstractMachine/bin/.gitignore
  15. 7
      Triangle.Compiler/.project
  16. 2
      Triangle.Compiler/.settings/org.eclipse.buildship.core.prefs
  17. 4
      Triangle.Compiler/.settings/org.eclipse.jdt.core.prefs
  18. 2
      Triangle.Compiler/bin/.gitignore
  19. 35
      Triangle.Compiler/src/main/java/triangle/abstractSyntaxTrees/commands/RepeatCommand.java
  20. 3
      Triangle.Compiler/src/main/java/triangle/abstractSyntaxTrees/visitors/CommandVisitor.java
  21. 6
      Triangle.Compiler/src/main/java/triangle/codeGenerator/Encoder.java
  22. 6
      Triangle.Compiler/src/main/java/triangle/contextualAnalyzer/Checker.java
  23. 26
      Triangle.Compiler/src/main/java/triangle/optimiser/ConstantFolder.java
  24. 11
      Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/Parser.java
  25. 20
      Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/Scanner.java
  26. 12
      Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/Token.java
  27. 6
      Triangle.Compiler/src/main/java/triangle/treeDrawer/LayoutVisitor.java
  28. 16
      Triangle.Compiler/src/test/java/triangle/syntacticAnalyser/TestScanner.java
  29. BIN
      build/libs/Triangle-Tools.jar
  30. BIN
      programs/add.tam
  31. 22
      programs/add.tri
  32. BIN
      programs/adddeep.tam
  33. 2
      programs/errors.tri
  34. BIN
      programs/hi-newcomment.tam
  35. BIN
      programs/hi-newcomment2.tam
  36. BIN
      programs/repeatuntil.tam
  37. 1
      programs/triangle.tri

@ -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>

1
.gitignore vendored

@ -5,3 +5,4 @@ tmp/
.gradle/
/build/

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Triangle-Tools</name>
<comment>Project Triangle-Tools created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>

@ -0,0 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=
jvm.arguments=
offline.mode=false
override.workspace.settings=false
show.console.view=false
show.executions.view=false

@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.source=11

@ -0,0 +1,2 @@
connection.project.dir=..
eclipse.preferences.version=1

@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.source=11

@ -0,0 +1,2 @@
connection.project.dir=..
eclipse.preferences.version=1

@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.source=11

@ -0,0 +1,2 @@
connection.project.dir=..
eclipse.preferences.version=1

@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.source=11

@ -20,4 +20,11 @@
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<linkedResources>
<link>
<name>programs</name>
<type>2</type>
<location>/home/simon/Documents/Uni/CS/CSCU9A5-2/Triangle-Tools/programs</location>
</link>
</linkedResources>
</projectDescription>

@ -0,0 +1,2 @@
connection.project.dir=..
eclipse.preferences.version=1

@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.source=11

@ -0,0 +1,2 @@
/main/
/test/

@ -0,0 +1,35 @@
/*
* @(#)WhileCommand.java 2.1 2003/10/07
*
* Copyright (C) 1999, 2003 D.A. Watt and D.F. Brown
* Dept. of Computing Science, University of Glasgow, Glasgow G12 8QQ Scotland
* and School of Computer and Math Sciences, The Robert Gordon University,
* St. Andrew Street, Aberdeen AB25 1HG, Scotland.
* All rights reserved.
*
* This software is provided free for educational use only. It may
* not be used for commercial purposes without the prior written permission
* of the authors.
*/
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 Expression E;
public final Command C;
}

@ -5,6 +5,7 @@ import triangle.abstractSyntaxTrees.commands.CallCommand;
import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand;
@ -24,4 +25,6 @@ public interface CommandVisitor<TArg, TResult> {
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.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration;
@ -181,6 +182,11 @@ public final class Encoder implements ActualParameterVisitor<Frame, Integer>,
return null;
}
@Override
public Void visitRepeatCommand(RepeatCommand ast, Frame frame) {
return null;
}
// Expressions
@Override
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.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration;
@ -185,6 +186,11 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
return null;
}
@Override
public Void visitRepeatCommand(RepeatCommand ast, Void arg) {
return null;
}
// Expressions
// Returns the TypeDenoter denoting the type of the expression. Does

@ -19,6 +19,7 @@ import triangle.abstractSyntaxTrees.commands.CallCommand;
import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration;
@ -496,16 +497,23 @@ public class ConstantFolder implements ActualParameterVisitor<Void, AbstractSynt
return null;
}
/*
@Override
public AbstractSyntaxTree visitRepeatCommand(RepeatCommand ast, Void arg) {
return null;
}
*/
// TODO uncomment if you've implemented the repeat command
// @Override
// public AbstractSyntaxTree visitRepeatCommand(RepeatCommand ast, Void arg) {
// ast.C.visit(this);
// AbstractSyntaxTree replacement = ast.E.visit(this);
// if (replacement != null) {
// ast.E = (Expression) replacement;
// }
// return null;
// }
@Override
public AbstractSyntaxTree visitRepeatCommand(RepeatCommand ast, Void arg) {
ast.C.visit(this);
AbstractSyntaxTree replacement = ast.E.visit(this);
if (replacement != null) {
ast.E = (Expression) replacement;
}
return null;
}
@Override
public AbstractSyntaxTree visitMultipleArrayAggregate(MultipleArrayAggregate ast, Void arg) {

@ -37,6 +37,7 @@ import triangle.abstractSyntaxTrees.commands.Command;
import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.ConstDeclaration;
@ -330,6 +331,16 @@ public class Parser {
}
break;
case Token.REPEAT: {
acceptIt();
Command cAST = parseSingleCommand();
accept(Token.UNTIL);
Expression eAST = parseExpression();
finish(commandPos);
commandAST = new RepeatCommand(eAST, cAST, commandPos);
}
break;
case Token.SEMICOLON:
case Token.END:
case Token.ELSE:

@ -35,7 +35,7 @@ public final class Scanner {
private boolean isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '=' || c == '<' || c == '>' || c == '\\'
|| c == '&' || c == '@' || c == '%' || c == '^' || c == '?');
|| c == '&' || c == '@' || c == '%' || c == '^' || c == '?' || c == '|'); //[PRAC2:P2:6] added |
}
///////////////////////////////////////////////////////////////////////////////
@ -65,15 +65,26 @@ public final class Scanner {
switch (currentChar) {
// comment
case '!': {
case '#': //[PRAC2:P2:3]
case '!':
takeIt();
while ((currentChar != SourceFile.EOL) && (currentChar != SourceFile.EOT))
takeIt();
if (currentChar == SourceFile.EOL)
takeIt();
}
break;
// multi-line comments
// Similar to the single line comment but we are ignoring new lines
case '$':
takeIt();
while ((currentChar != '$') && (currentChar != SourceFile.EOT))
takeIt();
if(currentChar == '$')
takeIt();
break;
// whitespace
case ' ':
case '\n':
@ -173,6 +184,7 @@ public final class Scanner {
case '%':
case '^':
case '?':
case '|': //[PRAC2:P2:6]
takeIt();
while (isOperator(currentChar))
takeIt();
@ -252,7 +264,7 @@ public final class Scanner {
currentlyScanningToken = false;
// skip any whitespace or comments
while (currentChar == '!' || currentChar == ' ' || currentChar == '\n' || currentChar == '\r'
|| currentChar == '\t')
|| currentChar == '\t' || currentChar == '#' || currentChar == '$') //[PRAC2:P2:3 && 5]
scanSeparator();
currentlyScanningToken = true;

@ -64,20 +64,20 @@ final class Token extends Object {
// 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,
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...
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...
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...
EOT = 33, ERROR = 34;
EOT = 35, ERROR = 36;
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",
"var", "while", ".", ":", ";", ",", ":=", "~", "(", ")", "[", "]", "{", "}", "", "<error>" };
"begin", "const", "do", "else", "end", "func", "if", "in", "let", "of", "proc", "record", "repeat", "then", "type",
"until", "var", "while", ".", ":", ";", ",", ":=", "~", "(", ")", "[", "]", "{", "}", "", "<error>" };
private final static int firstReservedWord = Token.ARRAY, lastReservedWord = Token.WHILE;

@ -33,6 +33,7 @@ import triangle.abstractSyntaxTrees.commands.CallCommand;
import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration;
@ -159,6 +160,11 @@ public class LayoutVisitor implements ActualParameterVisitor<Void, DrawingTree>,
return layoutBinary("WhileCom.", d1, d2);
}
@Override
public DrawingTree visitRepeatCommand(RepeatCommand ast, Void obj) {
return null;
}
// Expressions
@Override
public DrawingTree visitArrayExpression(ArrayExpression ast, Void obj) {

@ -22,28 +22,32 @@ public class TestScanner {
@Test
public void testHiNewComment() {
compileExpectFailure("/hi-newcomment.tri");
compileExpectSuccess("/hi-newcomment.tri");
}
//[PRAC2:P2:5]
@Test
public void testHiNewComment2() {
compileExpectFailure("/hi-newcomment2.tri");
compileExpectSuccess("/hi-newcomment2.tri");
}
@Test
public void testBarDemo() {
compileExpectFailure("/bardemo.tri");
compileExpectSuccess("/bardemo.tri");
}
@Test
public void testRepeatUntil() {
compileExpectFailure("/repeatuntil.tri");
compileExpectSuccess("/repeatuntil.tri");
}
//[PRAC2:P1:8]
@Test
public void testAdd() {
compileExpectSuccess("/add.tri");
}
private void compileExpectSuccess(String filename) {
// build.gradle has a line sourceSets.test.resources.srcDir file("$rootDir/programs")

Binary file not shown.

Binary file not shown.

@ -0,0 +1,22 @@
let
var a: Integer;
var b: Integer
in
begin
! Get user input for both values
put('>'); put(' '); getint(var a);
put('>'); put(' '); getint(var b);
! Sum
putint(a+b);
puteol();
! Prod.
putint(a*b);
puteol();
! Check if the same
if a = b
then begin put('S'); put('a'); put('m'); put('e'); put('!'); end
else !nothing
end

Binary file not shown.

@ -7,7 +7,7 @@ let
var me: Name;
var silly : maxint;
var silly: Rec;
! var silly: Rec; ! [PRAC2:5] Fixed error about "silly"
proc putstr (s: String) ~
let var i: Integer

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -1,5 +1,4 @@
let
proc putmany (n: Integer, c: Char) ~
let
var i: Integer

Loading…
Cancel
Save