task 6 in progress!

main
simonkellet 1 year ago
parent a59e60d2bc
commit f13138827f
  1. 7
      Triangle.Compiler/.project
  2. 41
      Triangle.Compiler/src/main/java/triangle/abstractSyntaxTrees/commands/LoopWhile.java
  3. 3
      Triangle.Compiler/src/main/java/triangle/abstractSyntaxTrees/visitors/CommandVisitor.java
  4. 7
      Triangle.Compiler/src/main/java/triangle/codeGenerator/Encoder.java
  5. 7
      Triangle.Compiler/src/main/java/triangle/contextualAnalyzer/Checker.java
  6. 7
      Triangle.Compiler/src/main/java/triangle/optimiser/ConstantFolder.java
  7. 7
      Triangle.Compiler/src/main/java/triangle/optimiser/SummaryStats.java
  8. 24
      Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/Parser.java
  9. 12
      Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/Token.java
  10. 7
      Triangle.Compiler/src/main/java/triangle/treeDrawer/LayoutVisitor.java
  11. BIN
      build/libs/Triangle-Tools.jar
  12. BIN
      every.tri
  13. BIN
      loopwhile.tam
  14. 10
      programs/checkstats.tri
  15. 16
      programs/loopwhile.tri

@ -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-2/Ass1/src/a5-triangle-tools/programs</location>
</link>
</linkedResources>
</projectDescription> </projectDescription>

@ -0,0 +1,41 @@
/*
* @(#)WhileCommand.java
*
* Revisions and updates (c) 2022-2023 Sandy Brownlee. alexander.brownlee@stir.ac.uk
*
* Original release:
*
* 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 LoopWhile extends Command {
public LoopWhile(Expression eAST,Command c1AST, Command c2AST, SourcePosition position) {
super(position);
E = eAST;
C1 = c1AST;
C2 = c2AST;
}
public <TArg, TResult> TResult visit(CommandVisitor<TArg, TResult> v, TArg arg) {
return v.visitLoopWhile(this, arg);
}
public Expression E;
public final Command C1;
public final Command C2;
}

@ -5,6 +5,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.LoopWhile;
import triangle.abstractSyntaxTrees.commands.SequentialCommand; import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand; import triangle.abstractSyntaxTrees.commands.WhileCommand;
@ -24,4 +25,6 @@ public interface CommandVisitor<TArg, TResult> {
TResult visitWhileCommand(WhileCommand ast, TArg arg); TResult visitWhileCommand(WhileCommand ast, TArg arg);
TResult visitLoopWhile(LoopWhile loopWhile, TArg arg);
} }

@ -42,6 +42,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.LoopWhile;
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;
@ -793,4 +794,10 @@ public final class Encoder implements ActualParameterVisitor<Frame, Integer>,
var baseObject = (AddressableEntity) V.visit(this, frame); var baseObject = (AddressableEntity) V.visit(this, frame);
baseObject.encodeFetchAddress(emitter, frame, V); baseObject.encodeFetchAddress(emitter, frame, V);
} }
@Override
public Void visitLoopWhile(LoopWhile loopWhile, Frame arg) {
// TODO Auto-generated method stub
return null;
}
} }

@ -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.LoopWhile;
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;
@ -981,4 +982,10 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
StdEnvironment.booleanType); StdEnvironment.booleanType);
} }
@Override
public Void visitLoopWhile(LoopWhile loopWhile, Void arg) {
// TODO Auto-generated method stub
return null;
}
} }

@ -19,6 +19,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.LoopWhile;
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;
@ -596,4 +597,10 @@ public class ConstantFolder implements ActualParameterVisitor<Void, AbstractSynt
return null; return null;
} }
@Override
public AbstractSyntaxTree visitLoopWhile(LoopWhile loopWhile, Void arg) {
// TODO Auto-generated method stub
return null;
}
} }

@ -19,6 +19,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.LoopWhile;
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;
@ -611,4 +612,10 @@ public class SummaryStats implements ActualParameterVisitor<Void, AbstractSyntax
return null; return null;
} }
@Override
public AbstractSyntaxTree visitLoopWhile(LoopWhile loopWhile, Void arg) {
// TODO Auto-generated method stub
return null;
}
} }

@ -41,6 +41,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.LoopWhile;
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;
@ -357,6 +358,29 @@ public class Parser {
accept(Token.RCURLY); accept(Token.RCURLY);
break; break;
//Task 6
case Token.LOOP: {
acceptIt();
accept(Token.BEGIN);
Command c1AST = parseCommand(); //C1 = LOOP
accept(Token.END);
System.out.println("c1AST: " + c1AST.getPosition());
accept(Token.WHILE);
Expression eAST = parseExpression(); //E = WHILE
System.out.println("eAST: " + eAST.getPosition());
accept(Token.DO);
Command c2AST = parseSingleCommand(); //C2 = DO
System.out.println("c2AST: " + c2AST.getPosition());
finish(commandPos);
commandAST = new LoopWhile(eAST, c1AST, c2AST, commandPos);
}
break;
case Token.SEMICOLON: case Token.SEMICOLON:
case Token.END: case Token.END:
case Token.ELSE: case Token.ELSE:

@ -67,20 +67,20 @@ final class Token extends Object {
INTLITERAL = 0, CHARLITERAL = 1, IDENTIFIER = 2, OPERATOR = 3, INTLITERAL = 0, CHARLITERAL = 1, IDENTIFIER = 2, OPERATOR = 3,
// 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, LOOP = 14, OF = 15,
PROC = 15, RECORD = 16, THEN = 17, TYPE = 18, VAR = 19, WHILE = 20, PROC = 16, RECORD = 17, THEN = 18, TYPE = 19, VAR = 20, WHILE = 21,
// punctuation... // punctuation...
DOT = 21, COLON = 22, SEMICOLON = 23, COMMA = 24, BECOMES = 25, IS = 26, DOT = 22, COLON = 23, SEMICOLON = 24, COMMA = 25, BECOMES = 26, IS = 27,
// brackets... // brackets...
LPAREN = 27, RPAREN = 28, LBRACKET = 29, RBRACKET = 30, LCURLY = 31, RCURLY = 32, LPAREN = 28, RPAREN = 29, LBRACKET = 30, RBRACKET = 31, LCURLY = 32, RCURLY = 33,
// special tokens... // special tokens...
EOT = 33, ERROR = 34; EOT = 34, ERROR = 35;
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", "loop", "of", "proc", "record", "then", "type",
"var", "while", ".", ":", ";", ",", ":=", "~", "(", ")", "[", "]", "{", "}", "", "<error>" }; "var", "while", ".", ":", ";", ",", ":=", "~", "(", ")", "[", "]", "{", "}", "", "<error>" };
private final static int firstReservedWord = Token.ARRAY, lastReservedWord = Token.WHILE; private final static int firstReservedWord = Token.ARRAY, lastReservedWord = Token.WHILE;

@ -37,6 +37,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.LoopWhile;
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;
@ -681,4 +682,10 @@ public class LayoutVisitor implements ActualParameterVisitor<Void, DrawingTree>,
return r; return r;
} }
@Override
public DrawingTree visitLoopWhile(LoopWhile loopWhile, Void arg) {
// TODO Auto-generated method stub
return null;
}
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -2,17 +2,11 @@ let
var a : Integer; var a : Integer;
var b : Integer; var b : Integer;
var c : Integer; var c : Integer;
var x : Char; var x : Char;
var y : Char var y : Char
in in
{ {
a := 1; a := 1; b := 2; a**;
b := 2;
a**;
b := 100; b := 100;
x := 'A'; y := 'Z';
x := 'A';
y := 'Z';
} }

@ -0,0 +1,16 @@
! print out ababababa
let
var a : Integer
in
begin
a := 0;
loop
begin
put('a');
a := a + 1;
end
while a < 5 do
put('b');
end
Loading…
Cancel
Save