Format code and fix warnings.

main
Deryck Brown 3 years ago
parent 2c426b5b2e
commit a02acc009b
  1. 8
      Triangle.AbstractMachine.Disassembler/src/main/java/Triangle/AbstractMachine/Disassembler.java
  2. 10
      Triangle.AbstractMachine.Interpreter/src/main/java/Triangle/AbstractMachine/Interpreter.java
  3. 80
      Triangle.AbstractMachine/src/main/java/Triangle/AbstractMachine/Machine.java
  4. 4
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/AnyTypeDenoter.java
  5. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/ArrayExpression.java
  6. 11
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/ArrayTypeDenoter.java
  7. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/AssignCommand.java
  8. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/BinaryExpression.java
  9. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/BinaryOperatorDeclaration.java
  10. 4
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/BoolTypeDenoter.java
  11. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/CallCommand.java
  12. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/CallExpression.java
  13. 4
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/CharTypeDenoter.java
  14. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/CharacterExpression.java
  15. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/CharacterLiteral.java
  16. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/ConstActualParameter.java
  17. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/ConstDeclaration.java
  18. 7
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/ConstFormalParameter.java
  19. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/DotVname.java
  20. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/EmptyActualParameterSequence.java
  21. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/EmptyCommand.java
  22. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/EmptyExpression.java
  23. 4
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/EmptyFormalParameterSequence.java
  24. 4
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/ErrorTypeDenoter.java
  25. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/FieldTypeDenoter.java
  26. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/FormalParameter.java
  27. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/FormalParameterSequence.java
  28. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/FuncActualParameter.java
  29. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/FuncDeclaration.java
  30. 8
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/FuncFormalParameter.java
  31. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/Identifier.java
  32. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/IfCommand.java
  33. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/IfExpression.java
  34. 4
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/IntTypeDenoter.java
  35. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/IntegerExpression.java
  36. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/IntegerLiteral.java
  37. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/LetCommand.java
  38. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/LetExpression.java
  39. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/MultipleActualParameterSequence.java
  40. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/MultipleArrayAggregate.java
  41. 8
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/MultipleFieldTypeDenoter.java
  42. 4
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/MultipleFormalParameterSequence.java
  43. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/MultipleRecordAggregate.java
  44. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/Operator.java
  45. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/ProcActualParameter.java
  46. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/ProcDeclaration.java
  47. 7
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/ProcFormalParameter.java
  48. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/Program.java
  49. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/RecordExpression.java
  50. 4
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/RecordTypeDenoter.java
  51. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/SequentialCommand.java
  52. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/SequentialDeclaration.java
  53. 4
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/SimpleTypeDenoter.java
  54. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/SimpleVname.java
  55. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/SingleActualParameterSequence.java
  56. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/SingleArrayAggregate.java
  57. 10
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/SingleFieldTypeDenoter.java
  58. 7
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/SingleFormalParameterSequence.java
  59. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/SingleRecordAggregate.java
  60. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/SubscriptVname.java
  61. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/TypeDeclaration.java
  62. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/TypeDenoter.java
  63. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/UnaryExpression.java
  64. 6
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/UnaryOperatorDeclaration.java
  65. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/VarActualParameter.java
  66. 5
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/VarDeclaration.java
  67. 7
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/VarFormalParameter.java
  68. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/VnameExpression.java
  69. 2
      Triangle.Compiler/src/main/java/Triangle/AbstractSyntaxTrees/WhileCommand.java
  70. 229
      Triangle.Compiler/src/main/java/Triangle/CodeGenerator/Encoder.java
  71. 38
      Triangle.Compiler/src/main/java/Triangle/Compiler.java
  72. 269
      Triangle.Compiler/src/main/java/Triangle/ContextualAnalyzer/Checker.java
  73. 5
      Triangle.Compiler/src/main/java/Triangle/StdEnvironment.java
  74. 39
      Triangle.Compiler/src/main/java/Triangle/SyntacticAnalyzer/Parser.java
  75. 11
      Triangle.Compiler/src/main/java/Triangle/SyntacticAnalyzer/Scanner.java
  76. 16
      Triangle.Compiler/src/main/java/Triangle/SyntacticAnalyzer/SourceFile.java
  77. 2
      Triangle.Compiler/src/main/java/Triangle/SyntacticAnalyzer/SourcePosition.java
  78. 2
      Triangle.Compiler/src/main/java/Triangle/SyntacticAnalyzer/SyntaxError.java
  79. 93
      Triangle.Compiler/src/main/java/Triangle/SyntacticAnalyzer/Token.java
  80. 5
      Triangle.Compiler/src/main/java/Triangle/TreeDrawer/DrawerFrame.java
  81. 4
      Triangle.Compiler/src/main/java/Triangle/TreeDrawer/DrawerPanel.java
  82. 7
      Triangle.Compiler/src/main/java/Triangle/TreeDrawer/DrawingTree.java
  83. 142
      Triangle.Compiler/src/main/java/Triangle/TreeDrawer/LayoutVisitor.java

@ -20,8 +20,8 @@ import java.io.FileNotFoundException;
import java.io.IOException;
/**
* Disassembles the TAM code in the given file, and displays the
* instructions on standard output.
* Disassembles the TAM code in the given file, and displays the instructions on
* standard output.
*
* For example:
*
@ -43,8 +43,8 @@ public class Disassembler {
static int CT;
/**
* Writes the r-field of an instruction in the form "l<I>reg</I>r", where
* l and r are the bracket characters to use.
* Writes the r-field of an instruction in the form "l<I>reg</I>r", where l and
* r are the bracket characters to use.
*
* @param leftbracket the character to print before the register.
* @param r the number of the register.

@ -29,16 +29,13 @@ public class Interpreter {
// DATA STORE REGISTERS AND OTHER REGISTERS
final static int CB = 0,
SB = 0,
HB = 1024; // = upper bound of data array + 1
final static int CB = 0, SB = 0, HB = 1024; // = upper bound of data array + 1
static int CT, CP, ST, HT, LB, status;
// status values
final static int running = 0, halted = 1, failedDataStoreFull = 2, failedInvalidCodeAddress = 3,
failedInvalidInstruction = 4, failedOverflow = 5, failedZeroDivide = 6,
failedIOError = 7;
failedInvalidInstruction = 4, failedOverflow = 5, failedZeroDivide = 6, failedIOError = 7;
static long accumulator;
@ -88,8 +85,7 @@ public class Interpreter {
static void dump() {
// Writes a summary of the machine state.
int addr, staticLink, dynamicLink,
localRegNum;
int addr, staticLink, dynamicLink, localRegNum;
System.out.println("");
System.out.println("State of data store and registers:");

@ -32,21 +32,8 @@ public final class Machine {
// INSTRUCTIONS
// Operation codes
public final static int LOADop = 0,
LOADAop = 1,
LOADIop = 2,
LOADLop = 3,
STOREop = 4,
STOREIop = 5,
CALLop = 6,
CALLIop = 7,
RETURNop = 8,
PUSHop = 10,
POPop = 11,
JUMPop = 12,
JUMPIop = 13,
JUMPIFop = 14,
HALTop = 15;
public final static int LOADop = 0, LOADAop = 1, LOADIop = 2, LOADLop = 3, STOREop = 4, STOREIop = 5, CALLop = 6,
CALLIop = 7, RETURNop = 8, PUSHop = 10, POPop = 11, JUMPop = 12, JUMPIop = 13, JUMPIFop = 14, HALTop = 15;
// CODE STORE
@ -54,72 +41,31 @@ public final class Machine {
// CODE STORE REGISTERS
public final static int CB = 0,
PB = 1024, // = upper bound of code array + 1
public final static int CB = 0, PB = 1024, // = upper bound of code array + 1
PT = 1052; // = PB + 28
// REGISTER NUMBERS
public final static int CBr = 0,
CTr = 1,
PBr = 2,
PTr = 3,
SBr = 4,
STr = 5,
HBr = 6,
HTr = 7,
LBr = 8,
L1r = LBr + 1,
L2r = LBr + 2,
L3r = LBr + 3,
L4r = LBr + 4,
L5r = LBr + 5,
L6r = LBr + 6,
CPr = 15;
public final static int CBr = 0, CTr = 1, PBr = 2, PTr = 3, SBr = 4, STr = 5, HBr = 6, HTr = 7, LBr = 8,
L1r = LBr + 1, L2r = LBr + 2, L3r = LBr + 3, L4r = LBr + 4, L5r = LBr + 5, L6r = LBr + 6, CPr = 15;
// DATA REPRESENTATION
public final static int booleanSize = 1,
characterSize = 1,
integerSize = 1,
addressSize = 1,
public final static int booleanSize = 1, characterSize = 1, integerSize = 1, addressSize = 1,
closureSize = 2 * addressSize,
linkDataSize = 3 * addressSize,
falseRep = 0,
trueRep = 1,
maxintRep = 32767;
falseRep = 0, trueRep = 1, maxintRep = 32767;
// ADDRESSES OF PRIMITIVE ROUTINES
public final static int idDisplacement = 1,
notDisplacement = 2,
andDisplacement = 3,
orDisplacement = 4,
succDisplacement = 5,
predDisplacement = 6,
negDisplacement = 7,
addDisplacement = 8,
subDisplacement = 9,
multDisplacement = 10,
divDisplacement = 11,
modDisplacement = 12,
ltDisplacement = 13,
leDisplacement = 14,
geDisplacement = 15,
gtDisplacement = 16,
eqDisplacement = 17,
neDisplacement = 18,
eolDisplacement = 19,
eofDisplacement = 20,
getDisplacement = 21,
putDisplacement = 22,
geteolDisplacement = 23,
puteolDisplacement = 24,
getintDisplacement = 25,
putintDisplacement = 26,
newDisplacement = 27,
public final static int idDisplacement = 1, notDisplacement = 2, andDisplacement = 3, orDisplacement = 4,
succDisplacement = 5, predDisplacement = 6, negDisplacement = 7, addDisplacement = 8, subDisplacement = 9,
multDisplacement = 10, divDisplacement = 11, modDisplacement = 12, ltDisplacement = 13, leDisplacement = 14,
geDisplacement = 15, gtDisplacement = 16, eqDisplacement = 17, neDisplacement = 18, eolDisplacement = 19,
eofDisplacement = 20, getDisplacement = 21, putDisplacement = 22, geteolDisplacement = 23,
puteolDisplacement = 24, getintDisplacement = 25, putintDisplacement = 26, newDisplacement = 27,
disposeDisplacement = 28;
}

@ -23,12 +23,12 @@ public class AnyTypeDenoter extends TypeDenoter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitAnyTypeDenoter(this, o);
}
@Override
public boolean equals(Object obj) {
public boolean equals(Object obj) {
return false;
}
}

@ -18,14 +18,13 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class ArrayExpression extends Expression {
public ArrayExpression(ArrayAggregate aaAST,
SourcePosition thePosition) {
public ArrayExpression(ArrayAggregate aaAST, SourcePosition thePosition) {
super(thePosition);
AA = aaAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitArrayExpression(this, o);
}

@ -18,25 +18,24 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class ArrayTypeDenoter extends TypeDenoter {
public ArrayTypeDenoter(IntegerLiteral ilAST, TypeDenoter tAST,
SourcePosition thePosition) {
public ArrayTypeDenoter(IntegerLiteral ilAST, TypeDenoter tAST, SourcePosition thePosition) {
super(thePosition);
IL = ilAST;
T = tAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitArrayTypeDenoter(this, o);
}
@Override
public boolean equals(Object obj) {
public boolean equals(Object obj) {
if (obj != null && obj instanceof ErrorTypeDenoter)
return true;
else if (obj != null && obj instanceof ArrayTypeDenoter)
return this.IL.spelling.compareTo(((ArrayTypeDenoter) obj).IL.spelling) == 0 &&
this.T.equals(((ArrayTypeDenoter) obj).T);
return this.IL.spelling.compareTo(((ArrayTypeDenoter) obj).IL.spelling) == 0
&& this.T.equals(((ArrayTypeDenoter) obj).T);
else
return false;
}

@ -25,7 +25,7 @@ public class AssignCommand extends Command {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitAssignCommand(this, o);
}

@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class BinaryExpression extends Expression {
public BinaryExpression(Expression e1AST, Operator oAST, Expression e2AST,
SourcePosition thePosition) {
public BinaryExpression(Expression e1AST, Operator oAST, Expression e2AST, SourcePosition thePosition) {
super(thePosition);
O = oAST;
E1 = e1AST;
@ -27,7 +26,7 @@ public class BinaryExpression extends Expression {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitBinaryExpression(this, o);
}

@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class BinaryOperatorDeclaration extends Declaration {
public BinaryOperatorDeclaration(Operator oAST, TypeDenoter arg1AST,
TypeDenoter arg2AST, TypeDenoter resultAST,
public BinaryOperatorDeclaration(Operator oAST, TypeDenoter arg1AST, TypeDenoter arg2AST, TypeDenoter resultAST,
SourcePosition thePosition) {
super(thePosition);
O = oAST;
@ -29,7 +28,7 @@ public class BinaryOperatorDeclaration extends Declaration {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitBinaryOperatorDeclaration(this, o);
}

@ -23,12 +23,12 @@ public class BoolTypeDenoter extends TypeDenoter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitBoolTypeDenoter(this, o);
}
@Override
public boolean equals(Object obj) {
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof ErrorTypeDenoter))
return true;
else

@ -18,15 +18,14 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class CallCommand extends Command {
public CallCommand(Identifier iAST, ActualParameterSequence apsAST,
SourcePosition thePosition) {
public CallCommand(Identifier iAST, ActualParameterSequence apsAST, SourcePosition thePosition) {
super(thePosition);
I = iAST;
APS = apsAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitCallCommand(this, o);
}

@ -18,15 +18,14 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class CallExpression extends Expression {
public CallExpression(Identifier iAST, ActualParameterSequence apsAST,
SourcePosition thePosition) {
public CallExpression(Identifier iAST, ActualParameterSequence apsAST, SourcePosition thePosition) {
super(thePosition);
I = iAST;
APS = apsAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitCallExpression(this, o);
}

@ -23,12 +23,12 @@ public class CharTypeDenoter extends TypeDenoter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitCharTypeDenoter(this, o);
}
@Override
public boolean equals(Object obj) {
public boolean equals(Object obj) {
if (obj != null && obj instanceof ErrorTypeDenoter)
return true;
else

@ -24,7 +24,7 @@ public class CharacterExpression extends Expression {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitCharacterExpression(this, o);
}

@ -23,7 +23,7 @@ public class CharacterLiteral extends Terminal {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitCharacterLiteral(this, o);
}

@ -24,7 +24,7 @@ public class ConstActualParameter extends ActualParameter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitConstActualParameter(this, o);
}

@ -18,15 +18,14 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class ConstDeclaration extends Declaration {
public ConstDeclaration(Identifier iAST, Expression eAST,
SourcePosition thePosition) {
public ConstDeclaration(Identifier iAST, Expression eAST, SourcePosition thePosition) {
super(thePosition);
I = iAST;
E = eAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitConstDeclaration(this, o);
}

@ -18,15 +18,14 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class ConstFormalParameter extends FormalParameter {
public ConstFormalParameter(Identifier iAST, TypeDenoter tAST,
SourcePosition thePosition) {
public ConstFormalParameter(Identifier iAST, TypeDenoter tAST, SourcePosition thePosition) {
super(thePosition);
I = iAST;
T = tAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitConstFormalParameter(this, o);
}
@ -34,7 +33,7 @@ public Object visit(Visitor v, Object o) {
public TypeDenoter T;
@Override
public boolean equals(Object fpAST) {
public boolean equals(Object fpAST) {
if (fpAST instanceof ConstFormalParameter) {
ConstFormalParameter cfpAST = (ConstFormalParameter) fpAST;
return T.equals(cfpAST.T);

@ -25,7 +25,7 @@ public class DotVname extends Vname {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitDotVname(this, o);
}

@ -23,7 +23,7 @@ public class EmptyActualParameterSequence extends ActualParameterSequence {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitEmptyActualParameterSequence(this, o);
}
}

@ -23,7 +23,7 @@ public class EmptyCommand extends Command {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitEmptyCommand(this, o);
}
}

@ -23,7 +23,7 @@ public class EmptyExpression extends Expression {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitEmptyExpression(this, o);
}
}

@ -23,12 +23,12 @@ public class EmptyFormalParameterSequence extends FormalParameterSequence {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitEmptyFormalParameterSequence(this, o);
}
@Override
public boolean equals(Object fpsAST) {
public boolean equals(Object fpsAST) {
return (fpsAST instanceof EmptyFormalParameterSequence);
}
}

@ -23,12 +23,12 @@ public class ErrorTypeDenoter extends TypeDenoter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitErrorTypeDenoter(this, o);
}
@Override
public boolean equals(Object obj) {
public boolean equals(Object obj) {
return true;
}
}

@ -23,5 +23,5 @@ public abstract class FieldTypeDenoter extends TypeDenoter {
}
@Override
public abstract boolean equals(Object obj);
public abstract boolean equals(Object obj);
}

@ -23,6 +23,6 @@ public abstract class FormalParameter extends Declaration {
}
@Override
public abstract boolean equals(Object fpAST);
public abstract boolean equals(Object fpAST);
}

@ -23,5 +23,5 @@ public abstract class FormalParameterSequence extends AST {
}
@Override
public abstract boolean equals(Object fpsAST);
public abstract boolean equals(Object fpsAST);
}

@ -24,7 +24,7 @@ public class FuncActualParameter extends ActualParameter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitFuncActualParameter(this, o);
}

@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class FuncDeclaration extends Declaration {
public FuncDeclaration(Identifier iAST, FormalParameterSequence fpsAST,
TypeDenoter tAST, Expression eAST,
public FuncDeclaration(Identifier iAST, FormalParameterSequence fpsAST, TypeDenoter tAST, Expression eAST,
SourcePosition thePosition) {
super(thePosition);
I = iAST;
@ -29,7 +28,7 @@ public class FuncDeclaration extends Declaration {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitFuncDeclaration(this, o);
}

@ -18,8 +18,8 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class FuncFormalParameter extends FormalParameter {
public FuncFormalParameter(Identifier iAST, FormalParameterSequence fpsAST,
TypeDenoter tAST, SourcePosition thePosition) {
public FuncFormalParameter(Identifier iAST, FormalParameterSequence fpsAST, TypeDenoter tAST,
SourcePosition thePosition) {
super(thePosition);
I = iAST;
FPS = fpsAST;
@ -27,12 +27,12 @@ public class FuncFormalParameter extends FormalParameter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitFuncFormalParameter(this, o);
}
@Override
public boolean equals(Object fpAST) {
public boolean equals(Object fpAST) {
if (fpAST instanceof FuncFormalParameter) {
FuncFormalParameter ffpAST = (FuncFormalParameter) fpAST;
return FPS.equals(ffpAST.FPS) && T.equals(ffpAST.T);

@ -25,7 +25,7 @@ public class Identifier extends Terminal {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitIdentifier(this, o);
}

@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class IfCommand extends Command {
public IfCommand(Expression eAST, Command c1AST, Command c2AST,
SourcePosition thePosition) {
public IfCommand(Expression eAST, Command c1AST, Command c2AST, SourcePosition thePosition) {
super(thePosition);
E = eAST;
C1 = c1AST;
@ -27,7 +26,7 @@ public class IfCommand extends Command {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitIfCommand(this, o);
}

@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class IfExpression extends Expression {
public IfExpression(Expression e1AST, Expression e2AST, Expression e3AST,
SourcePosition thePosition) {
public IfExpression(Expression e1AST, Expression e2AST, Expression e3AST, SourcePosition thePosition) {
super(thePosition);
E1 = e1AST;
E2 = e2AST;
@ -27,7 +26,7 @@ public class IfExpression extends Expression {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitIfExpression(this, o);
}

@ -23,12 +23,12 @@ public class IntTypeDenoter extends TypeDenoter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitIntTypeDenoter(this, o);
}
@Override
public boolean equals(Object obj) {
public boolean equals(Object obj) {
if (obj != null && obj instanceof ErrorTypeDenoter)
return true;
else

@ -24,7 +24,7 @@ public class IntegerExpression extends Expression {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitIntegerExpression(this, o);
}

@ -23,7 +23,7 @@ public class IntegerLiteral extends Terminal {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitIntegerLiteral(this, o);
}

@ -25,7 +25,7 @@ public class LetCommand extends Command {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitLetCommand(this, o);
}

@ -25,7 +25,7 @@ public class LetExpression extends Expression {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitLetExpression(this, o);
}

@ -26,7 +26,7 @@ public class MultipleActualParameterSequence extends ActualParameterSequence {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitMultipleActualParameterSequence(this, o);
}

@ -18,15 +18,14 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class MultipleArrayAggregate extends ArrayAggregate {
public MultipleArrayAggregate(Expression eAST, ArrayAggregate aaAST,
SourcePosition thePosition) {
public MultipleArrayAggregate(Expression eAST, ArrayAggregate aaAST, SourcePosition thePosition) {
super(thePosition);
E = eAST;
AA = aaAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitMultipleArrayAggregate(this, o);
}

@ -27,17 +27,15 @@ public class MultipleFieldTypeDenoter extends FieldTypeDenoter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitMultipleFieldTypeDenoter(this, o);
}
@Override
public boolean equals(Object obj) {
public boolean equals(Object obj) {
if (obj != null && obj instanceof MultipleFieldTypeDenoter) {
MultipleFieldTypeDenoter ft = (MultipleFieldTypeDenoter) obj;
return (this.I.spelling.compareTo(ft.I.spelling) == 0) &&
this.T.equals(ft.T) &&
this.FT.equals(ft.FT);
return (this.I.spelling.compareTo(ft.I.spelling) == 0) && this.T.equals(ft.T) && this.FT.equals(ft.FT);
} else
return false;
}

@ -26,12 +26,12 @@ public class MultipleFormalParameterSequence extends FormalParameterSequence {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitMultipleFormalParameterSequence(this, o);
}
@Override
public boolean equals(Object fpsAST) {
public boolean equals(Object fpsAST) {
if (fpsAST instanceof MultipleFormalParameterSequence) {
MultipleFormalParameterSequence mfpsAST = (MultipleFormalParameterSequence) fpsAST;
return FP.equals(mfpsAST.FP) && FPS.equals(mfpsAST.FPS);

@ -27,7 +27,7 @@ public class MultipleRecordAggregate extends RecordAggregate {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitMultipleRecordAggregate(this, o);
}

@ -24,7 +24,7 @@ public class Operator extends Terminal {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitOperator(this, o);
}

@ -24,7 +24,7 @@ public class ProcActualParameter extends ActualParameter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitProcActualParameter(this, o);
}

@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class ProcDeclaration extends Declaration {
public ProcDeclaration(Identifier iAST, FormalParameterSequence fpsAST,
Command cAST, SourcePosition thePosition) {
public ProcDeclaration(Identifier iAST, FormalParameterSequence fpsAST, Command cAST, SourcePosition thePosition) {
super(thePosition);
I = iAST;
FPS = fpsAST;
@ -27,7 +26,7 @@ public class ProcDeclaration extends Declaration {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitProcDeclaration(this, o);
}

@ -18,15 +18,14 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class ProcFormalParameter extends FormalParameter {
public ProcFormalParameter(Identifier iAST, FormalParameterSequence fpsAST,
SourcePosition thePosition) {
public ProcFormalParameter(Identifier iAST, FormalParameterSequence fpsAST, SourcePosition thePosition) {
super(thePosition);
I = iAST;
FPS = fpsAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitProcFormalParameter(this, o);
}
@ -34,7 +33,7 @@ public Object visit(Visitor v, Object o) {
public FormalParameterSequence FPS;
@Override
public boolean equals(Object fpAST) {
public boolean equals(Object fpAST) {
if (fpAST instanceof ProcFormalParameter) {
ProcFormalParameter pfpAST = (ProcFormalParameter) fpAST;
return FPS.equals(pfpAST.FPS);

@ -24,7 +24,7 @@ public class Program extends AST {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitProgram(this, o);
}

@ -24,7 +24,7 @@ public class RecordExpression extends Expression {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitRecordExpression(this, o);
}

@ -24,12 +24,12 @@ public class RecordTypeDenoter extends TypeDenoter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitRecordTypeDenoter(this, o);
}
@Override
public boolean equals(Object obj) {
public boolean equals(Object obj) {
if (obj != null && obj instanceof ErrorTypeDenoter)
return true;
else if (obj != null && obj instanceof RecordTypeDenoter)

@ -25,7 +25,7 @@ public class SequentialCommand extends Command {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitSequentialCommand(this, o);
}

@ -18,15 +18,14 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class SequentialDeclaration extends Declaration {
public SequentialDeclaration(Declaration d1AST, Declaration d2AST,
SourcePosition thePosition) {
public SequentialDeclaration(Declaration d1AST, Declaration d2AST, SourcePosition thePosition) {
super(thePosition);
D1 = d1AST;
D2 = d2AST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitSequentialDeclaration(this, o);
}

@ -24,12 +24,12 @@ public class SimpleTypeDenoter extends TypeDenoter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitSimpleTypeDenoter(this, o);
}
@Override
public boolean equals(Object obj) {
public boolean equals(Object obj) {
return false; // should not happen
}

@ -24,7 +24,7 @@ public class SimpleVname extends Vname {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitSimpleVname(this, o);
}

@ -18,14 +18,13 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class SingleActualParameterSequence extends ActualParameterSequence {
public SingleActualParameterSequence(ActualParameter apAST,
SourcePosition thePosition) {
public SingleActualParameterSequence(ActualParameter apAST, SourcePosition thePosition) {
super(thePosition);
AP = apAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitSingleActualParameterSequence(this, o);
}

@ -18,14 +18,13 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class SingleArrayAggregate extends ArrayAggregate {
public SingleArrayAggregate(Expression eAST,
SourcePosition thePosition) {
public SingleArrayAggregate(Expression eAST, SourcePosition thePosition) {
super(thePosition);
E = eAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitSingleArrayAggregate(this, o);
}

@ -18,24 +18,22 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class SingleFieldTypeDenoter extends FieldTypeDenoter {
public SingleFieldTypeDenoter(Identifier iAST, TypeDenoter tAST,
SourcePosition thePosition) {
public SingleFieldTypeDenoter(Identifier iAST, TypeDenoter tAST, SourcePosition thePosition) {
super(thePosition);
I = iAST;
T = tAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitSingleFieldTypeDenoter(this, o);
}
@Override
public boolean equals(Object obj) {
public boolean equals(Object obj) {
if (obj != null && obj instanceof SingleFieldTypeDenoter) {
SingleFieldTypeDenoter ft = (SingleFieldTypeDenoter) obj;
return (this.I.spelling.compareTo(ft.I.spelling) == 0) &&
this.T.equals(ft.T);
return (this.I.spelling.compareTo(ft.I.spelling) == 0) && this.T.equals(ft.T);
} else
return false;
}

@ -18,19 +18,18 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class SingleFormalParameterSequence extends FormalParameterSequence {
public SingleFormalParameterSequence(FormalParameter fpAST,
SourcePosition thePosition) {
public SingleFormalParameterSequence(FormalParameter fpAST, SourcePosition thePosition) {
super(thePosition);
FP = fpAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitSingleFormalParameterSequence(this, o);
}
@Override
public boolean equals(Object fpsAST) {
public boolean equals(Object fpsAST) {
if (fpsAST instanceof SingleFormalParameterSequence) {
SingleFormalParameterSequence sfpsAST = (SingleFormalParameterSequence) fpsAST;
return FP.equals(sfpsAST.FP);

@ -18,15 +18,14 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class SingleRecordAggregate extends RecordAggregate {
public SingleRecordAggregate(Identifier iAST, Expression eAST,
SourcePosition thePosition) {
public SingleRecordAggregate(Identifier iAST, Expression eAST, SourcePosition thePosition) {
super(thePosition);
I = iAST;
E = eAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitSingleRecordAggregate(this, o);
}

@ -25,7 +25,7 @@ public class SubscriptVname extends Vname {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitSubscriptVname(this, o);
}

@ -18,15 +18,14 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class TypeDeclaration extends Declaration {
public TypeDeclaration(Identifier iAST, TypeDenoter tAST,
SourcePosition thePosition) {
public TypeDeclaration(Identifier iAST, TypeDenoter tAST, SourcePosition thePosition) {
super(thePosition);
I = iAST;
T = tAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitTypeDeclaration(this, o);
}

@ -23,6 +23,6 @@ public abstract class TypeDenoter extends AST {
}
@Override
public abstract boolean equals(Object obj);
public abstract boolean equals(Object obj);
}

@ -18,15 +18,14 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class UnaryExpression extends Expression {
public UnaryExpression(Operator oAST, Expression eAST,
SourcePosition thePosition) {
public UnaryExpression(Operator oAST, Expression eAST, SourcePosition thePosition) {
super(thePosition);
O = oAST;
E = eAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitUnaryExpression(this, o);
}

@ -18,8 +18,8 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class UnaryOperatorDeclaration extends Declaration {
public UnaryOperatorDeclaration(Operator oAST, TypeDenoter argAST,
TypeDenoter resultAST, SourcePosition thePosition) {
public UnaryOperatorDeclaration(Operator oAST, TypeDenoter argAST, TypeDenoter resultAST,
SourcePosition thePosition) {
super(thePosition);
O = oAST;
ARG = argAST;
@ -27,7 +27,7 @@ public class UnaryOperatorDeclaration extends Declaration {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitUnaryOperatorDeclaration(this, o);
}

@ -24,7 +24,7 @@ public class VarActualParameter extends ActualParameter {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitVarActualParameter(this, o);
}

@ -18,15 +18,14 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class VarDeclaration extends Declaration {
public VarDeclaration(Identifier iAST, TypeDenoter tAST,
SourcePosition thePosition) {
public VarDeclaration(Identifier iAST, TypeDenoter tAST, SourcePosition thePosition) {
super(thePosition);
I = iAST;
T = tAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitVarDeclaration(this, o);
}

@ -18,15 +18,14 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
public class VarFormalParameter extends FormalParameter {
public VarFormalParameter(Identifier iAST, TypeDenoter tAST,
SourcePosition thePosition) {
public VarFormalParameter(Identifier iAST, TypeDenoter tAST, SourcePosition thePosition) {
super(thePosition);
I = iAST;
T = tAST;
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitVarFormalParameter(this, o);
}
@ -34,7 +33,7 @@ public Object visit(Visitor v, Object o) {
public TypeDenoter T;
@Override
public boolean equals(Object fpAST) {
public boolean equals(Object fpAST) {
if (fpAST instanceof VarFormalParameter) {
VarFormalParameter vfpAST = (VarFormalParameter) fpAST;
return T.equals(vfpAST.T);

@ -24,7 +24,7 @@ public class VnameExpression extends Expression {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitVnameExpression(this, o);
}

@ -25,7 +25,7 @@ public class WhileCommand extends Command {
}
@Override
public Object visit(Visitor v, Object o) {
public Object visit(Visitor v, Object o) {
return v.visitWhileCommand(this, o);
}

@ -94,16 +94,15 @@ public final class Encoder implements Visitor {
// Commands
@Override
public Object visitAssignCommand(AssignCommand ast, Object o) {
public Object visitAssignCommand(AssignCommand ast, Object o) {
Frame frame = (Frame) o;
Integer valSize = (Integer) ast.E.visit(this, frame);
encodeStore(ast.V, new Frame(frame, valSize.intValue()),
valSize.intValue());
encodeStore(ast.V, new Frame(frame, valSize.intValue()), valSize.intValue());
return null;
}
@Override
public Object visitCallCommand(CallCommand ast, Object o) {
public Object visitCallCommand(CallCommand ast, Object o) {
Frame frame = (Frame) o;
Integer argsSize = (Integer) ast.APS.visit(this, frame);
ast.I.visit(this, new Frame(frame.level, argsSize));
@ -111,12 +110,12 @@ public Object visitCallCommand(CallCommand ast, Object o) {
}
@Override
public Object visitEmptyCommand(EmptyCommand ast, Object o) {
public Object visitEmptyCommand(EmptyCommand ast, Object o) {
return null;
}
@Override
public Object visitIfCommand(IfCommand ast, Object o) {
public Object visitIfCommand(IfCommand ast, Object o) {
Frame frame = (Frame) o;
int jumpifAddr, jumpAddr;
@ -133,7 +132,7 @@ public Object visitIfCommand(IfCommand ast, Object o) {
}
@Override
public Object visitLetCommand(LetCommand ast, Object o) {
public Object visitLetCommand(LetCommand ast, Object o) {
Frame frame = (Frame) o;
int extraSize = ((Integer) ast.D.visit(this, frame)).intValue();
ast.C.visit(this, new Frame(frame, extraSize));
@ -143,14 +142,14 @@ public Object visitLetCommand(LetCommand ast, Object o) {
}
@Override
public Object visitSequentialCommand(SequentialCommand ast, Object o) {
public Object visitSequentialCommand(SequentialCommand ast, Object o) {
ast.C1.visit(this, o);
ast.C2.visit(this, o);
return null;
}
@Override
public Object visitWhileCommand(WhileCommand ast, Object o) {
public Object visitWhileCommand(WhileCommand ast, Object o) {
Frame frame = (Frame) o;
int jumpAddr, loopAddr;
@ -166,13 +165,13 @@ public Object visitWhileCommand(WhileCommand ast, Object o) {
// Expressions
@Override
public Object visitArrayExpression(ArrayExpression ast, Object o) {
public Object visitArrayExpression(ArrayExpression ast, Object o) {
ast.type.visit(this, null);
return ast.AA.visit(this, o);
}
@Override
public Object visitBinaryExpression(BinaryExpression ast, Object o) {
public Object visitBinaryExpression(BinaryExpression ast, Object o) {
Frame frame = (Frame) o;
Integer valSize = (Integer) ast.type.visit(this, null);
int valSize1 = ((Integer) ast.E1.visit(this, frame)).intValue();
@ -184,7 +183,7 @@ public Object visitBinaryExpression(BinaryExpression ast, Object o) {
}
@Override
public Object visitCallExpression(CallExpression ast, Object o) {
public Object visitCallExpression(CallExpression ast, Object o) {
Frame frame = (Frame) o;
Integer valSize = (Integer) ast.type.visit(this, null);
Integer argsSize = (Integer) ast.APS.visit(this, frame);
@ -193,8 +192,7 @@ public Object visitCallExpression(CallExpression ast, Object o) {
}
@Override
public Object visitCharacterExpression(CharacterExpression ast,
Object o) {
public Object visitCharacterExpression(CharacterExpression ast, Object o) {
Frame frame = (Frame) o;
Integer valSize = (Integer) ast.type.visit(this, null);
emit(Machine.LOADLop, 0, 0, ast.CL.spelling.charAt(1));
@ -202,12 +200,12 @@ public Object visitCharacterExpression(CharacterExpression ast,
}
@Override
public Object visitEmptyExpression(EmptyExpression ast, Object o) {
public Object visitEmptyExpression(EmptyExpression ast, Object o) {
return Integer.valueOf(0);
}
@Override
public Object visitIfExpression(IfExpression ast, Object o) {
public Object visitIfExpression(IfExpression ast, Object o) {
Frame frame = (Frame) o;
Integer valSize;
int jumpifAddr, jumpAddr;
@ -226,7 +224,7 @@ public Object visitIfExpression(IfExpression ast, Object o) {
}
@Override
public Object visitIntegerExpression(IntegerExpression ast, Object o) {
public Object visitIntegerExpression(IntegerExpression ast, Object o) {
Frame frame = (Frame) o;
Integer valSize = (Integer) ast.type.visit(this, null);
emit(Machine.LOADLop, 0, 0, Integer.parseInt(ast.IL.spelling));
@ -234,7 +232,7 @@ public Object visitIntegerExpression(IntegerExpression ast, Object o) {
}
@Override
public Object visitLetExpression(LetExpression ast, Object o) {
public Object visitLetExpression(LetExpression ast, Object o) {
Frame frame = (Frame) o;
ast.type.visit(this, null);
int extraSize = ((Integer) ast.D.visit(this, frame)).intValue();
@ -246,13 +244,13 @@ public Object visitLetExpression(LetExpression ast, Object o) {
}
@Override
public Object visitRecordExpression(RecordExpression ast, Object o) {
public Object visitRecordExpression(RecordExpression ast, Object o) {
ast.type.visit(this, null);
return ast.RA.visit(this, o);
}
@Override
public Object visitUnaryExpression(UnaryExpression ast, Object o) {
public Object visitUnaryExpression(UnaryExpression ast, Object o) {
Frame frame = (Frame) o;
Integer valSize = (Integer) ast.type.visit(this, null);
ast.E.visit(this, frame);
@ -261,7 +259,7 @@ public Object visitUnaryExpression(UnaryExpression ast, Object o) {
}
@Override
public Object visitVnameExpression(VnameExpression ast, Object o) {
public Object visitVnameExpression(VnameExpression ast, Object o) {
Frame frame = (Frame) o;
Integer valSize = (Integer) ast.type.visit(this, null);
encodeFetch(ast.V, frame, valSize.intValue());
@ -270,24 +268,21 @@ public Object visitVnameExpression(VnameExpression ast, Object o) {
// Declarations
@Override
public Object visitBinaryOperatorDeclaration(BinaryOperatorDeclaration ast,
Object o) {
public Object visitBinaryOperatorDeclaration(BinaryOperatorDeclaration ast, Object o) {
return Integer.valueOf(0);
}
@Override
public Object visitConstDeclaration(ConstDeclaration ast, Object o) {
public Object visitConstDeclaration(ConstDeclaration ast, Object o) {
Frame frame = (Frame) o;
int extraSize = 0;
if (ast.E instanceof CharacterExpression) {
CharacterLiteral CL = ((CharacterExpression) ast.E).CL;
ast.entity = new KnownValue(Machine.characterSize,
characterValuation(CL.spelling));
ast.entity = new KnownValue(Machine.characterSize, characterValuation(CL.spelling));
} else if (ast.E instanceof IntegerExpression) {
IntegerLiteral IL = ((IntegerExpression) ast.E).IL;
ast.entity = new KnownValue(Machine.integerSize,
Integer.parseInt(IL.spelling));
ast.entity = new KnownValue(Machine.integerSize, Integer.parseInt(IL.spelling));
} else {
int valSize = ((Integer) ast.E.visit(this, frame)).intValue();
ast.entity = new UnknownValue(valSize, frame.level, frame.size);
@ -298,7 +293,7 @@ public Object visitConstDeclaration(ConstDeclaration ast, Object o) {
}
@Override
public Object visitFuncDeclaration(FuncDeclaration ast, Object o) {
public Object visitFuncDeclaration(FuncDeclaration ast, Object o) {
Frame frame = (Frame) o;
int jumpAddr = nextInstrAddr;
int argsSize = 0, valSize = 0;
@ -320,14 +315,13 @@ public Object visitFuncDeclaration(FuncDeclaration ast, Object o) {
}
@Override
public Object visitProcDeclaration(ProcDeclaration ast, Object o) {
public Object visitProcDeclaration(ProcDeclaration ast, Object o) {
Frame frame = (Frame) o;
int jumpAddr = nextInstrAddr;
int argsSize = 0;
emit(Machine.JUMPop, 0, Machine.CBr, 0);
ast.entity = new KnownRoutine(Machine.closureSize, frame.level,
nextInstrAddr);
ast.entity = new KnownRoutine(Machine.closureSize, frame.level, nextInstrAddr);
writeTableDetails(ast);
if (frame.level == Machine.maxRoutineLevel)
reporter.reportRestriction("can't nest routines so deeply");
@ -343,7 +337,7 @@ public Object visitProcDeclaration(ProcDeclaration ast, Object o) {
}
@Override
public Object visitSequentialDeclaration(SequentialDeclaration ast, Object o) {
public Object visitSequentialDeclaration(SequentialDeclaration ast, Object o) {
Frame frame = (Frame) o;
int extraSize1, extraSize2;
@ -354,20 +348,19 @@ public Object visitSequentialDeclaration(SequentialDeclaration ast, Object o) {
}
@Override
public Object visitTypeDeclaration(TypeDeclaration ast, Object o) {
public Object visitTypeDeclaration(TypeDeclaration ast, Object o) {
// just to ensure the type's representation is decided
ast.T.visit(this, null);
return Integer.valueOf(0);
}
@Override
public Object visitUnaryOperatorDeclaration(UnaryOperatorDeclaration ast,
Object o) {
public Object visitUnaryOperatorDeclaration(UnaryOperatorDeclaration ast, Object o) {
return Integer.valueOf(0);
}
@Override
public Object visitVarDeclaration(VarDeclaration ast, Object o) {
public Object visitVarDeclaration(VarDeclaration ast, Object o) {
Frame frame = (Frame) o;
int extraSize;
@ -380,8 +373,7 @@ public Object visitVarDeclaration(VarDeclaration ast, Object o) {
// Array Aggregates
@Override
public Object visitMultipleArrayAggregate(MultipleArrayAggregate ast,
Object o) {
public Object visitMultipleArrayAggregate(MultipleArrayAggregate ast, Object o) {
Frame frame = (Frame) o;
int elemSize = ((Integer) ast.E.visit(this, frame)).intValue();
Frame frame1 = new Frame(frame, elemSize);
@ -390,14 +382,13 @@ public Object visitMultipleArrayAggregate(MultipleArrayAggregate ast,
}
@Override
public Object visitSingleArrayAggregate(SingleArrayAggregate ast, Object o) {
public Object visitSingleArrayAggregate(SingleArrayAggregate ast, Object o) {
return ast.E.visit(this, o);
}
// Record Aggregates
@Override
public Object visitMultipleRecordAggregate(MultipleRecordAggregate ast,
Object o) {
public Object visitMultipleRecordAggregate(MultipleRecordAggregate ast, Object o) {
Frame frame = (Frame) o;
int fieldSize = ((Integer) ast.E.visit(this, frame)).intValue();
Frame frame1 = new Frame(frame, fieldSize);
@ -406,14 +397,13 @@ public Object visitMultipleRecordAggregate(MultipleRecordAggregate ast,
}
@Override
public Object visitSingleRecordAggregate(SingleRecordAggregate ast,
Object o) {
public Object visitSingleRecordAggregate(SingleRecordAggregate ast, Object o) {
return ast.E.visit(this, o);
}
// Formal Parameters
@Override
public Object visitConstFormalParameter(ConstFormalParameter ast, Object o) {
public Object visitConstFormalParameter(ConstFormalParameter ast, Object o) {
Frame frame = (Frame) o;
int valSize = ((Integer) ast.T.visit(this, null)).intValue();
ast.entity = new UnknownValue(valSize, frame.level, -frame.size - valSize);
@ -422,44 +412,39 @@ public Object visitConstFormalParameter(ConstFormalParameter ast, Object o) {
}
@Override
public Object visitFuncFormalParameter(FuncFormalParameter ast, Object o) {
public Object visitFuncFormalParameter(FuncFormalParameter ast, Object o) {
Frame frame = (Frame) o;
int argsSize = Machine.closureSize;
ast.entity = new UnknownRoutine(Machine.closureSize, frame.level,
-frame.size - argsSize);
ast.entity = new UnknownRoutine(Machine.closureSize, frame.level, -frame.size - argsSize);
writeTableDetails(ast);
return Integer.valueOf(argsSize);
}
@Override
public Object visitProcFormalParameter(ProcFormalParameter ast, Object o) {
public Object visitProcFormalParameter(ProcFormalParameter ast, Object o) {
Frame frame = (Frame) o;
int argsSize = Machine.closureSize;
ast.entity = new UnknownRoutine(Machine.closureSize, frame.level,
-frame.size - argsSize);
ast.entity = new UnknownRoutine(Machine.closureSize, frame.level, -frame.size - argsSize);
writeTableDetails(ast);
return Integer.valueOf(argsSize);
}
@Override
public Object visitVarFormalParameter(VarFormalParameter ast, Object o) {
public Object visitVarFormalParameter(VarFormalParameter ast, Object o) {
Frame frame = (Frame) o;
ast.T.visit(this, null);
ast.entity = new UnknownAddress(Machine.addressSize, frame.level,
-frame.size - Machine.addressSize);
ast.entity = new UnknownAddress(Machine.addressSize, frame.level, -frame.size - Machine.addressSize);
writeTableDetails(ast);
return Integer.valueOf(Machine.addressSize);
}
@Override
public Object visitEmptyFormalParameterSequence(
EmptyFormalParameterSequence ast, Object o) {
public Object visitEmptyFormalParameterSequence(EmptyFormalParameterSequence ast, Object o) {
return Integer.valueOf(0);
}
@Override
public Object visitMultipleFormalParameterSequence(
MultipleFormalParameterSequence ast, Object o) {
public Object visitMultipleFormalParameterSequence(MultipleFormalParameterSequence ast, Object o) {
Frame frame = (Frame) o;
int argsSize1 = ((Integer) ast.FPS.visit(this, frame)).intValue();
Frame frame1 = new Frame(frame, argsSize1);
@ -468,19 +453,18 @@ public Object visitMultipleFormalParameterSequence(
}
@Override
public Object visitSingleFormalParameterSequence(
SingleFormalParameterSequence ast, Object o) {
public Object visitSingleFormalParameterSequence(SingleFormalParameterSequence ast, Object o) {
return ast.FP.visit(this, o);
}
// Actual Parameters
@Override
public Object visitConstActualParameter(ConstActualParameter ast, Object o) {
public Object visitConstActualParameter(ConstActualParameter ast, Object o) {
return ast.E.visit(this, o);
}
@Override
public Object visitFuncActualParameter(FuncActualParameter ast, Object o) {
public Object visitFuncActualParameter(FuncActualParameter ast, Object o) {
Frame frame = (Frame) o;
if (ast.I.decl.entity instanceof KnownRoutine) {
ObjectAddress address = ((KnownRoutine) ast.I.decl.entity).address;
@ -489,8 +473,8 @@ public Object visitFuncActualParameter(FuncActualParameter ast, Object o) {
emit(Machine.LOADAop, 0, Machine.CBr, address.displacement);
} else if (ast.I.decl.entity instanceof UnknownRoutine) {
ObjectAddress address = ((UnknownRoutine) ast.I.decl.entity).address;
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level,
address.level), address.displacement);
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level, address.level),
address.displacement);
} else if (ast.I.decl.entity instanceof PrimitiveRoutine) {
int displacement = ((PrimitiveRoutine) ast.I.decl.entity).displacement;
// static link, code address
@ -501,7 +485,7 @@ public Object visitFuncActualParameter(FuncActualParameter ast, Object o) {
}
@Override
public Object visitProcActualParameter(ProcActualParameter ast, Object o) {
public Object visitProcActualParameter(ProcActualParameter ast, Object o) {
Frame frame = (Frame) o;
if (ast.I.decl.entity instanceof KnownRoutine) {
ObjectAddress address = ((KnownRoutine) ast.I.decl.entity).address;
@ -510,8 +494,8 @@ public Object visitProcActualParameter(ProcActualParameter ast, Object o) {
emit(Machine.LOADAop, 0, Machine.CBr, address.displacement);
} else if (ast.I.decl.entity instanceof UnknownRoutine) {
ObjectAddress address = ((UnknownRoutine) ast.I.decl.entity).address;
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level,
address.level), address.displacement);
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level, address.level),
address.displacement);
} else if (ast.I.decl.entity instanceof PrimitiveRoutine) {
int displacement = ((PrimitiveRoutine) ast.I.decl.entity).displacement;
// static link, code address
@ -522,20 +506,18 @@ public Object visitProcActualParameter(ProcActualParameter ast, Object o) {
}
@Override
public Object visitVarActualParameter(VarActualParameter ast, Object o) {
public Object visitVarActualParameter(VarActualParameter ast, Object o) {
encodeFetchAddress(ast.V, (Frame) o);
return Integer.valueOf(Machine.addressSize);
}
@Override
public Object visitEmptyActualParameterSequence(
EmptyActualParameterSequence ast, Object o) {
public Object visitEmptyActualParameterSequence(EmptyActualParameterSequence ast, Object o) {
return Integer.valueOf(0);
}
@Override
public Object visitMultipleActualParameterSequence(
MultipleActualParameterSequence ast, Object o) {
public Object visitMultipleActualParameterSequence(MultipleActualParameterSequence ast, Object o) {
Frame frame = (Frame) o;
int argsSize1 = ((Integer) ast.AP.visit(this, frame)).intValue();
Frame frame1 = new Frame(frame, argsSize1);
@ -544,19 +526,18 @@ public Object visitMultipleActualParameterSequence(
}
@Override
public Object visitSingleActualParameterSequence(
SingleActualParameterSequence ast, Object o) {
public Object visitSingleActualParameterSequence(SingleActualParameterSequence ast, Object o) {
return ast.AP.visit(this, o);
}
// Type Denoters
@Override
public Object visitAnyTypeDenoter(AnyTypeDenoter ast, Object o) {
public Object visitAnyTypeDenoter(AnyTypeDenoter ast, Object o) {
return Integer.valueOf(0);
}
@Override
public Object visitArrayTypeDenoter(ArrayTypeDenoter ast, Object o) {
public Object visitArrayTypeDenoter(ArrayTypeDenoter ast, Object o) {
int typeSize;
if (ast.entity == null) {
int elemSize = ((Integer) ast.T.visit(this, null)).intValue();
@ -569,7 +550,7 @@ public Object visitArrayTypeDenoter(ArrayTypeDenoter ast, Object o) {
}
@Override
public Object visitBoolTypeDenoter(BoolTypeDenoter ast, Object o) {
public Object visitBoolTypeDenoter(BoolTypeDenoter ast, Object o) {
if (ast.entity == null) {
ast.entity = new TypeRepresentation(Machine.booleanSize);
writeTableDetails(ast);
@ -578,7 +559,7 @@ public Object visitBoolTypeDenoter(BoolTypeDenoter ast, Object o) {
}
@Override
public Object visitCharTypeDenoter(CharTypeDenoter ast, Object o) {
public Object visitCharTypeDenoter(CharTypeDenoter ast, Object o) {
if (ast.entity == null) {
ast.entity = new TypeRepresentation(Machine.characterSize);
writeTableDetails(ast);
@ -587,18 +568,17 @@ public Object visitCharTypeDenoter(CharTypeDenoter ast, Object o) {
}
@Override
public Object visitErrorTypeDenoter(ErrorTypeDenoter ast, Object o) {
public Object visitErrorTypeDenoter(ErrorTypeDenoter ast, Object o) {
return Integer.valueOf(0);
}
@Override
public Object visitSimpleTypeDenoter(SimpleTypeDenoter ast,
Object o) {
public Object visitSimpleTypeDenoter(SimpleTypeDenoter ast, Object o) {
return Integer.valueOf(0);
}
@Override
public Object visitIntTypeDenoter(IntTypeDenoter ast, Object o) {
public Object visitIntTypeDenoter(IntTypeDenoter ast, Object o) {
if (ast.entity == null) {
ast.entity = new TypeRepresentation(Machine.integerSize);
writeTableDetails(ast);
@ -607,7 +587,7 @@ public Object visitIntTypeDenoter(IntTypeDenoter ast, Object o) {
}
@Override
public Object visitRecordTypeDenoter(RecordTypeDenoter ast, Object o) {
public Object visitRecordTypeDenoter(RecordTypeDenoter ast, Object o) {
int typeSize;
if (ast.entity == null) {
typeSize = ((Integer) ast.FT.visit(this, Integer.valueOf(0))).intValue();
@ -619,8 +599,7 @@ public Object visitRecordTypeDenoter(RecordTypeDenoter ast, Object o) {
}
@Override
public Object visitMultipleFieldTypeDenoter(MultipleFieldTypeDenoter ast,
Object o) {
public Object visitMultipleFieldTypeDenoter(MultipleFieldTypeDenoter ast, Object o) {
int offset = ((Integer) o).intValue();
int fieldSize;
@ -637,8 +616,7 @@ public Object visitMultipleFieldTypeDenoter(MultipleFieldTypeDenoter ast,
}
@Override
public Object visitSingleFieldTypeDenoter(SingleFieldTypeDenoter ast,
Object o) {
public Object visitSingleFieldTypeDenoter(SingleFieldTypeDenoter ast, Object o) {
int offset = ((Integer) o).intValue();
int fieldSize;
@ -654,21 +632,20 @@ public Object visitSingleFieldTypeDenoter(SingleFieldTypeDenoter ast,
// Literals, Identifiers and Operators
@Override
public Object visitCharacterLiteral(CharacterLiteral ast, Object o) {
public Object visitCharacterLiteral(CharacterLiteral ast, Object o) {
return null;
}
@Override
public Object visitIdentifier(Identifier ast, Object o) {
public Object visitIdentifier(Identifier ast, Object o) {
Frame frame = (Frame) o;
if (ast.decl.entity instanceof KnownRoutine) {
ObjectAddress address = ((KnownRoutine) ast.decl.entity).address;
emit(Machine.CALLop, displayRegister(frame.level, address.level),
Machine.CBr, address.displacement);
emit(Machine.CALLop, displayRegister(frame.level, address.level), Machine.CBr, address.displacement);
} else if (ast.decl.entity instanceof UnknownRoutine) {
ObjectAddress address = ((UnknownRoutine) ast.decl.entity).address;
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level,
address.level), address.displacement);
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level, address.level),
address.displacement);
emit(Machine.CALLIop, 0, 0, 0);
} else if (ast.decl.entity instanceof PrimitiveRoutine) {
int displacement = ((PrimitiveRoutine) ast.decl.entity).displacement;
@ -683,21 +660,20 @@ public Object visitIdentifier(Identifier ast, Object o) {
}
@Override
public Object visitIntegerLiteral(IntegerLiteral ast, Object o) {
public Object visitIntegerLiteral(IntegerLiteral ast, Object o) {
return null;
}
@Override
public Object visitOperator(Operator ast, Object o) {
public Object visitOperator(Operator ast, Object o) {
Frame frame = (Frame) o;
if (ast.decl.entity instanceof KnownRoutine) {
ObjectAddress address = ((KnownRoutine) ast.decl.entity).address;
emit(Machine.CALLop, displayRegister(frame.level, address.level),
Machine.CBr, address.displacement);
emit(Machine.CALLop, displayRegister(frame.level, address.level), Machine.CBr, address.displacement);
} else if (ast.decl.entity instanceof UnknownRoutine) {
ObjectAddress address = ((UnknownRoutine) ast.decl.entity).address;
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level,
address.level), address.displacement);
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level, address.level),
address.displacement);
emit(Machine.CALLIop, 0, 0, 0);
} else if (ast.decl.entity instanceof PrimitiveRoutine) {
int displacement = ((PrimitiveRoutine) ast.decl.entity).displacement;
@ -713,7 +689,7 @@ public Object visitOperator(Operator ast, Object o) {
// Value-or-variable names
@Override
public Object visitDotVname(DotVname ast, Object o) {
public Object visitDotVname(DotVname ast, Object o) {
Frame frame = (Frame) o;
RuntimeEntity baseObject = (RuntimeEntity) ast.V.visit(this, frame);
ast.offset = ast.V.offset + ((Field) ast.I.decl.entity).fieldOffset;
@ -723,14 +699,14 @@ public Object visitDotVname(DotVname ast, Object o) {
}
@Override
public Object visitSimpleVname(SimpleVname ast, Object o) {
public Object visitSimpleVname(SimpleVname ast, Object o) {
ast.offset = 0;
ast.indexed = false;
return ast.I.decl.entity;
}
@Override
public Object visitSubscriptVname(SubscriptVname ast, Object o) {
public Object visitSubscriptVname(SubscriptVname ast, Object o) {
Frame frame = (Frame) o;
RuntimeEntity baseObject;
int elemSize, indexSize;
@ -749,8 +725,7 @@ public Object visitSubscriptVname(SubscriptVname ast, Object o) {
indexSize = ((Integer) ast.E.visit(this, frame)).intValue();
if (elemSize != 1) {
emit(Machine.LOADLop, 0, 0, elemSize);
emit(Machine.CALLop, Machine.SBr, Machine.PBr,
Machine.multDisplacement);
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.multDisplacement);
}
if (ast.indexed)
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
@ -762,7 +737,7 @@ public Object visitSubscriptVname(SubscriptVname ast, Object o) {
// Programs
@Override
public Object visitProgram(Program ast, Object o) {
public Object visitProgram(Program ast, Object o) {
return ast.C.visit(this, o);
}
@ -785,8 +760,7 @@ public Object visitProgram(Program ast, Object o) {
}
// Decides run-time representation of a standard constant.
private final void elaborateStdConst(Declaration constDeclaration,
int value) {
private final void elaborateStdConst(Declaration constDeclaration, int value) {
if (constDeclaration instanceof ConstDeclaration) {
ConstDeclaration decl = (ConstDeclaration) constDeclaration;
@ -797,20 +771,17 @@ public Object visitProgram(Program ast, Object o) {
}
// Decides run-time representation of a standard routine.
private final void elaborateStdPrimRoutine(Declaration routineDeclaration,
int routineOffset) {
private final void elaborateStdPrimRoutine(Declaration routineDeclaration, int routineOffset) {
routineDeclaration.entity = new PrimitiveRoutine(Machine.closureSize, routineOffset);
writeTableDetails(routineDeclaration);
}
private final void elaborateStdEqRoutine(Declaration routineDeclaration,
int routineOffset) {
private final void elaborateStdEqRoutine(Declaration routineDeclaration, int routineOffset) {
routineDeclaration.entity = new EqualityRoutine(Machine.closureSize, routineOffset);
writeTableDetails(routineDeclaration);
}
private final void elaborateStdRoutine(Declaration routineDeclaration,
int routineOffset) {
private final void elaborateStdRoutine(Declaration routineDeclaration, int routineOffset) {
routineDeclaration.entity = new KnownRoutine(Machine.closureSize, 0, routineOffset);
writeTableDetails(routineDeclaration);
}
@ -947,18 +918,17 @@ public Object visitProgram(Program ast, Object o) {
if (baseObject instanceof KnownAddress) {
ObjectAddress address = ((KnownAddress) baseObject).address;
if (V.indexed) {
emit(Machine.LOADAop, 0, displayRegister(frame.level, address.level),
address.displacement + V.offset);
emit(Machine.LOADAop, 0, displayRegister(frame.level, address.level), address.displacement + V.offset);
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
emit(Machine.STOREIop, valSize, 0, 0);
} else {
emit(Machine.STOREop, valSize, displayRegister(frame.level,
address.level), address.displacement + V.offset);
emit(Machine.STOREop, valSize, displayRegister(frame.level, address.level),
address.displacement + V.offset);
}
} else if (baseObject instanceof UnknownAddress) {
ObjectAddress address = ((UnknownAddress) baseObject).address;
emit(Machine.LOADop, Machine.addressSize, displayRegister(frame.level,
address.level), address.displacement);
emit(Machine.LOADop, Machine.addressSize, displayRegister(frame.level, address.level),
address.displacement);
if (V.indexed)
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
if (V.offset != 0) {
@ -988,22 +958,20 @@ public Object visitProgram(Program ast, Object o) {
// presumably offset = 0 and indexed = false
int value = ((KnownValue) baseObject).value;
emit(Machine.LOADLop, 0, 0, value);
} else if ((baseObject instanceof UnknownValue) ||
(baseObject instanceof KnownAddress)) {
} else if ((baseObject instanceof UnknownValue) || (baseObject instanceof KnownAddress)) {
ObjectAddress address = (baseObject instanceof UnknownValue) ? ((UnknownValue) baseObject).address
: ((KnownAddress) baseObject).address;
if (V.indexed) {
emit(Machine.LOADAop, 0, displayRegister(frame.level, address.level),
address.displacement + V.offset);
emit(Machine.LOADAop, 0, displayRegister(frame.level, address.level), address.displacement + V.offset);
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
emit(Machine.LOADIop, valSize, 0, 0);
} else
emit(Machine.LOADop, valSize, displayRegister(frame.level,
address.level), address.displacement + V.offset);
emit(Machine.LOADop, valSize, displayRegister(frame.level, address.level),
address.displacement + V.offset);
} else if (baseObject instanceof UnknownAddress) {
ObjectAddress address = ((UnknownAddress) baseObject).address;
emit(Machine.LOADop, Machine.addressSize, displayRegister(frame.level,
address.level), address.displacement);
emit(Machine.LOADop, Machine.addressSize, displayRegister(frame.level, address.level),
address.displacement);
if (V.indexed)
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
if (V.offset != 0) {
@ -1026,14 +994,13 @@ public Object visitProgram(Program ast, Object o) {
// If indexed = true, code will have been generated to load an index value.
if (baseObject instanceof KnownAddress) {
ObjectAddress address = ((KnownAddress) baseObject).address;
emit(Machine.LOADAop, 0, displayRegister(frame.level, address.level),
address.displacement + V.offset);
emit(Machine.LOADAop, 0, displayRegister(frame.level, address.level), address.displacement + V.offset);
if (V.indexed)
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
} else if (baseObject instanceof UnknownAddress) {
ObjectAddress address = ((UnknownAddress) baseObject).address;
emit(Machine.LOADop, Machine.addressSize, displayRegister(frame.level,
address.level), address.displacement);
emit(Machine.LOADop, Machine.addressSize, displayRegister(frame.level, address.level),
address.displacement);
if (V.indexed)
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
if (V.offset != 0) {

@ -46,27 +46,22 @@ public class Compiler {
/**
* Compile the source program to TAM machine code.
*
* @param sourceName the name of the file containing the
* source program.
* @param objectName the name of the file containing the
* object program.
* @param showingAST true iff the AST is to be displayed after
* contextual analysis (not currently implemented).
* @param showingTable true iff the object description details are to
* be displayed during code generation (not
* currently implemented).
* @return true iff the source program is free of compile-time errors,
* otherwise false.
* @param sourceName the name of the file containing the source program.
* @param objectName the name of the file containing the object program.
* @param showingAST true iff the AST is to be displayed after contextual
* analysis (not currently implemented).
* @param showingTable true iff the object description details are to be
* displayed during code generation (not currently
* implemented).
* @return true iff the source program is free of compile-time errors, otherwise
* false.
*/
static boolean compileProgram(String sourceName, String objectName,
boolean showingAST, boolean showingTable) {
static boolean compileProgram(String sourceName, String objectName, boolean showingAST, boolean showingTable) {
System.out.println("********** " +
"Triangle Compiler (Java Version 2.1)" +
" **********");
System.out.println("********** " + "Triangle Compiler (Java Version 2.1)" + " **********");
System.out.println("Syntactic Analysis ...");
SourceFile source = new SourceFile(sourceName);
SourceFile source = SourceFile.ofPath(sourceName);
if (source == null) {
System.out.println("Can't access source file " + sourceName);
@ -110,11 +105,10 @@ public class Compiler {
/**
* Triangle compiler main program.
*
* @param args the only command-line argument to the program specifies
* the source filename.
* @param args the only command-line argument to the program specifies the
* source filename.
*/
public static void main(String[] args) {
boolean compiledOK;
if (args.length != 1) {
System.out.println("Usage: tc filename");
@ -122,6 +116,8 @@ public class Compiler {
}
String sourceName = args[0];
compiledOK = compileProgram(sourceName, objectName, false, false);
var compiledOK = compileProgram(sourceName, objectName, false, false);
System.exit(compiledOK ? 0 : 1);
}
}

@ -94,7 +94,7 @@ public final class Checker implements Visitor {
// Always returns null. Does not use the given object.
@Override
public Object visitAssignCommand(AssignCommand ast, Object o) {
public Object visitAssignCommand(AssignCommand ast, Object o) {
TypeDenoter vType = (TypeDenoter) ast.V.visit(this, null);
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
if (!ast.V.variable)
@ -105,7 +105,7 @@ public Object visitAssignCommand(AssignCommand ast, Object o) {
}
@Override
public Object visitCallCommand(CallCommand ast, Object o) {
public Object visitCallCommand(CallCommand ast, Object o) {
Declaration binding = (Declaration) ast.I.visit(this, null);
if (binding == null)
@ -115,18 +115,17 @@ public Object visitCallCommand(CallCommand ast, Object o) {
} else if (binding instanceof ProcFormalParameter) {
ast.APS.visit(this, ((ProcFormalParameter) binding).FPS);
} else
reporter.reportError("\"%\" is not a procedure identifier",
ast.I.spelling, ast.I.position);
reporter.reportError("\"%\" is not a procedure identifier", ast.I.spelling, ast.I.position);
return null;
}
@Override
public Object visitEmptyCommand(EmptyCommand ast, Object o) {
public Object visitEmptyCommand(EmptyCommand ast, Object o) {
return null;
}
@Override
public Object visitIfCommand(IfCommand ast, Object o) {
public Object visitIfCommand(IfCommand ast, Object o) {
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
if (!eType.equals(StdEnvironment.booleanType))
reporter.reportError("Boolean expression expected here", "", ast.E.position);
@ -136,7 +135,7 @@ public Object visitIfCommand(IfCommand ast, Object o) {
}
@Override
public Object visitLetCommand(LetCommand ast, Object o) {
public Object visitLetCommand(LetCommand ast, Object o) {
idTable.openScope();
ast.D.visit(this, null);
ast.C.visit(this, null);
@ -145,14 +144,14 @@ public Object visitLetCommand(LetCommand ast, Object o) {
}
@Override
public Object visitSequentialCommand(SequentialCommand ast, Object o) {
public Object visitSequentialCommand(SequentialCommand ast, Object o) {
ast.C1.visit(this, null);
ast.C2.visit(this, null);
return null;
}
@Override
public Object visitWhileCommand(WhileCommand ast, Object o) {
public Object visitWhileCommand(WhileCommand ast, Object o) {
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
if (!eType.equals(StdEnvironment.booleanType))
reporter.reportError("Boolean expression expected here", "", ast.E.position);
@ -166,16 +165,15 @@ public Object visitWhileCommand(WhileCommand ast, Object o) {
// not use the given object.
@Override
public Object visitArrayExpression(ArrayExpression ast, Object o) {
public Object visitArrayExpression(ArrayExpression ast, Object o) {
TypeDenoter elemType = (TypeDenoter) ast.AA.visit(this, null);
IntegerLiteral il = new IntegerLiteral(Integer.valueOf(ast.AA.elemCount).toString(),
ast.position);
IntegerLiteral il = new IntegerLiteral(Integer.valueOf(ast.AA.elemCount).toString(), ast.position);
ast.type = new ArrayTypeDenoter(il, elemType, ast.position);
return ast.type;
}
@Override
public Object visitBinaryExpression(BinaryExpression ast, Object o) {
public Object visitBinaryExpression(BinaryExpression ast, Object o) {
TypeDenoter e1Type = (TypeDenoter) ast.E1.visit(this, null);
TypeDenoter e2Type = (TypeDenoter) ast.E2.visit(this, null);
@ -185,27 +183,23 @@ public Object visitBinaryExpression(BinaryExpression ast, Object o) {
reportUndeclared(ast.O);
else {
if (!(binding instanceof BinaryOperatorDeclaration))
reporter.reportError("\"%\" is not a binary operator",
ast.O.spelling, ast.O.position);
reporter.reportError("\"%\" is not a binary operator", ast.O.spelling, ast.O.position);
BinaryOperatorDeclaration bbinding = (BinaryOperatorDeclaration) binding;
if (bbinding.ARG1 == StdEnvironment.anyType) {
// this operator must be "=" or "\="
if (!e1Type.equals(e2Type))
reporter.reportError("incompatible argument types for \"%\"",
ast.O.spelling, ast.position);
reporter.reportError("incompatible argument types for \"%\"", ast.O.spelling, ast.position);
} else if (!e1Type.equals(bbinding.ARG1))
reporter.reportError("wrong argument type for \"%\"",
ast.O.spelling, ast.E1.position);
reporter.reportError("wrong argument type for \"%\"", ast.O.spelling, ast.E1.position);
else if (!e2Type.equals(bbinding.ARG2))
reporter.reportError("wrong argument type for \"%\"",
ast.O.spelling, ast.E2.position);
reporter.reportError("wrong argument type for \"%\"", ast.O.spelling, ast.E2.position);
ast.type = bbinding.RES;
}
return ast.type;
}
@Override
public Object visitCallExpression(CallExpression ast, Object o) {
public Object visitCallExpression(CallExpression ast, Object o) {
Declaration binding = (Declaration) ast.I.visit(this, null);
if (binding == null) {
reportUndeclared(ast.I);
@ -217,29 +211,27 @@ public Object visitCallExpression(CallExpression ast, Object o) {
ast.APS.visit(this, ((FuncFormalParameter) binding).FPS);
ast.type = ((FuncFormalParameter) binding).T;
} else
reporter.reportError("\"%\" is not a function identifier",
ast.I.spelling, ast.I.position);
reporter.reportError("\"%\" is not a function identifier", ast.I.spelling, ast.I.position);
return ast.type;
}
@Override
public Object visitCharacterExpression(CharacterExpression ast, Object o) {
public Object visitCharacterExpression(CharacterExpression ast, Object o) {
ast.type = StdEnvironment.charType;
return ast.type;
}
@Override
public Object visitEmptyExpression(EmptyExpression ast, Object o) {
public Object visitEmptyExpression(EmptyExpression ast, Object o) {
ast.type = null;
return ast.type;
}
@Override
public Object visitIfExpression(IfExpression ast, Object o) {
public Object visitIfExpression(IfExpression ast, Object o) {
TypeDenoter e1Type = (TypeDenoter) ast.E1.visit(this, null);
if (!e1Type.equals(StdEnvironment.booleanType))
reporter.reportError("Boolean expression expected here", "",
ast.E1.position);
reporter.reportError("Boolean expression expected here", "", ast.E1.position);
TypeDenoter e2Type = (TypeDenoter) ast.E2.visit(this, null);
TypeDenoter e3Type = (TypeDenoter) ast.E3.visit(this, null);
if (!e2Type.equals(e3Type))
@ -249,13 +241,13 @@ public Object visitIfExpression(IfExpression ast, Object o) {
}
@Override
public Object visitIntegerExpression(IntegerExpression ast, Object o) {
public Object visitIntegerExpression(IntegerExpression ast, Object o) {
ast.type = StdEnvironment.integerType;
return ast.type;
}
@Override
public Object visitLetExpression(LetExpression ast, Object o) {
public Object visitLetExpression(LetExpression ast, Object o) {
idTable.openScope();
ast.D.visit(this, null);
ast.type = (TypeDenoter) ast.E.visit(this, null);
@ -264,14 +256,14 @@ public Object visitLetExpression(LetExpression ast, Object o) {
}
@Override
public Object visitRecordExpression(RecordExpression ast, Object o) {
public Object visitRecordExpression(RecordExpression ast, Object o) {
FieldTypeDenoter rType = (FieldTypeDenoter) ast.RA.visit(this, null);
ast.type = new RecordTypeDenoter(rType, ast.position);
return ast.type;
}
@Override
public Object visitUnaryExpression(UnaryExpression ast, Object o) {
public Object visitUnaryExpression(UnaryExpression ast, Object o) {
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
Declaration binding = (Declaration) ast.O.visit(this, null);
@ -279,20 +271,18 @@ public Object visitUnaryExpression(UnaryExpression ast, Object o) {
reportUndeclared(ast.O);
ast.type = StdEnvironment.errorType;
} else if (!(binding instanceof UnaryOperatorDeclaration))
reporter.reportError("\"%\" is not a unary operator",
ast.O.spelling, ast.O.position);
reporter.reportError("\"%\" is not a unary operator", ast.O.spelling, ast.O.position);
else {
UnaryOperatorDeclaration ubinding = (UnaryOperatorDeclaration) binding;
if (!eType.equals(ubinding.ARG))
reporter.reportError("wrong argument type for \"%\"",
ast.O.spelling, ast.O.position);
reporter.reportError("wrong argument type for \"%\"", ast.O.spelling, ast.O.position);
ast.type = ubinding.RES;
}
return ast.type;
}
@Override
public Object visitVnameExpression(VnameExpression ast, Object o) {
public Object visitVnameExpression(VnameExpression ast, Object o) {
ast.type = (TypeDenoter) ast.V.visit(this, null);
return ast.type;
}
@ -301,43 +291,39 @@ public Object visitVnameExpression(VnameExpression ast, Object o) {
// Always returns null. Does not use the given object.
@Override
public Object visitBinaryOperatorDeclaration(BinaryOperatorDeclaration ast, Object o) {
public Object visitBinaryOperatorDeclaration(BinaryOperatorDeclaration ast, Object o) {
return null;
}
@Override
public Object visitConstDeclaration(ConstDeclaration ast, Object o) {
public Object visitConstDeclaration(ConstDeclaration ast, Object o) {
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
idTable.enter(ast.I.spelling, ast);
if (ast.duplicated)
reporter.reportError("identifier \"%\" already declared",
ast.I.spelling, ast.position);
reporter.reportError("identifier \"%\" already declared", ast.I.spelling, ast.position);
return null;
}
@Override
public Object visitFuncDeclaration(FuncDeclaration ast, Object o) {
public Object visitFuncDeclaration(FuncDeclaration ast, Object o) {
ast.T = (TypeDenoter) ast.T.visit(this, null);
idTable.enter(ast.I.spelling, ast); // permits recursion
if (ast.duplicated)
reporter.reportError("identifier \"%\" already declared",
ast.I.spelling, ast.position);
reporter.reportError("identifier \"%\" already declared", ast.I.spelling, ast.position);
idTable.openScope();
ast.FPS.visit(this, null);
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
idTable.closeScope();
if (!ast.T.equals(eType))
reporter.reportError("body of function \"%\" has wrong type",
ast.I.spelling, ast.E.position);
reporter.reportError("body of function \"%\" has wrong type", ast.I.spelling, ast.E.position);
return null;
}
@Override
public Object visitProcDeclaration(ProcDeclaration ast, Object o) {
public Object visitProcDeclaration(ProcDeclaration ast, Object o) {
idTable.enter(ast.I.spelling, ast); // permits recursion
if (ast.duplicated)
reporter.reportError("identifier \"%\" already declared",
ast.I.spelling, ast.position);
reporter.reportError("identifier \"%\" already declared", ast.I.spelling, ast.position);
idTable.openScope();
ast.FPS.visit(this, null);
ast.C.visit(this, null);
@ -346,34 +332,32 @@ public Object visitProcDeclaration(ProcDeclaration ast, Object o) {
}
@Override
public Object visitSequentialDeclaration(SequentialDeclaration ast, Object o) {
public Object visitSequentialDeclaration(SequentialDeclaration ast, Object o) {
ast.D1.visit(this, null);
ast.D2.visit(this, null);
return null;
}
@Override
public Object visitTypeDeclaration(TypeDeclaration ast, Object o) {
public Object visitTypeDeclaration(TypeDeclaration ast, Object o) {
ast.T = (TypeDenoter) ast.T.visit(this, null);
idTable.enter(ast.I.spelling, ast);
if (ast.duplicated)
reporter.reportError("identifier \"%\" already declared",
ast.I.spelling, ast.position);
reporter.reportError("identifier \"%\" already declared", ast.I.spelling, ast.position);
return null;
}
@Override
public Object visitUnaryOperatorDeclaration(UnaryOperatorDeclaration ast, Object o) {
public Object visitUnaryOperatorDeclaration(UnaryOperatorDeclaration ast, Object o) {
return null;
}
@Override
public Object visitVarDeclaration(VarDeclaration ast, Object o) {
public Object visitVarDeclaration(VarDeclaration ast, Object o) {
ast.T = (TypeDenoter) ast.T.visit(this, null);
idTable.enter(ast.I.spelling, ast);
if (ast.duplicated)
reporter.reportError("identifier \"%\" already declared",
ast.I.spelling, ast.position);
reporter.reportError("identifier \"%\" already declared", ast.I.spelling, ast.position);
return null;
}
@ -384,7 +368,7 @@ public Object visitVarDeclaration(VarDeclaration ast, Object o) {
// given object.
@Override
public Object visitMultipleArrayAggregate(MultipleArrayAggregate ast, Object o) {
public Object visitMultipleArrayAggregate(MultipleArrayAggregate ast, Object o) {
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
TypeDenoter elemType = (TypeDenoter) ast.AA.visit(this, null);
ast.elemCount = ast.AA.elemCount + 1;
@ -394,7 +378,7 @@ public Object visitMultipleArrayAggregate(MultipleArrayAggregate ast, Object o)
}
@Override
public Object visitSingleArrayAggregate(SingleArrayAggregate ast, Object o) {
public Object visitSingleArrayAggregate(SingleArrayAggregate ast, Object o) {
TypeDenoter elemType = (TypeDenoter) ast.E.visit(this, null);
ast.elemCount = 1;
return elemType;
@ -406,19 +390,18 @@ public Object visitSingleArrayAggregate(SingleArrayAggregate ast, Object o) {
// given object.
@Override
public Object visitMultipleRecordAggregate(MultipleRecordAggregate ast, Object o) {
public Object visitMultipleRecordAggregate(MultipleRecordAggregate ast, Object o) {
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
FieldTypeDenoter rType = (FieldTypeDenoter) ast.RA.visit(this, null);
TypeDenoter fType = checkFieldIdentifier(rType, ast.I);
if (fType != StdEnvironment.errorType)
reporter.reportError("duplicate field \"%\" in record",
ast.I.spelling, ast.I.position);
reporter.reportError("duplicate field \"%\" in record", ast.I.spelling, ast.I.position);
ast.type = new MultipleFieldTypeDenoter(ast.I, eType, rType, ast.position);
return ast.type;
}
@Override
public Object visitSingleRecordAggregate(SingleRecordAggregate ast, Object o) {
public Object visitSingleRecordAggregate(SingleRecordAggregate ast, Object o) {
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
ast.type = new SingleFieldTypeDenoter(ast.I, eType, ast.position);
return ast.type;
@ -429,64 +412,60 @@ public Object visitSingleRecordAggregate(SingleRecordAggregate ast, Object o) {
// Always returns null. Does not use the given object.
@Override
public Object visitConstFormalParameter(ConstFormalParameter ast, Object o) {
public Object visitConstFormalParameter(ConstFormalParameter ast, Object o) {
ast.T = (TypeDenoter) ast.T.visit(this, null);
idTable.enter(ast.I.spelling, ast);
if (ast.duplicated)
reporter.reportError("duplicated formal parameter \"%\"",
ast.I.spelling, ast.position);
reporter.reportError("duplicated formal parameter \"%\"", ast.I.spelling, ast.position);
return null;
}
@Override
public Object visitFuncFormalParameter(FuncFormalParameter ast, Object o) {
public Object visitFuncFormalParameter(FuncFormalParameter ast, Object o) {
idTable.openScope();
ast.FPS.visit(this, null);
idTable.closeScope();
ast.T = (TypeDenoter) ast.T.visit(this, null);
idTable.enter(ast.I.spelling, ast);
if (ast.duplicated)
reporter.reportError("duplicated formal parameter \"%\"",
ast.I.spelling, ast.position);
reporter.reportError("duplicated formal parameter \"%\"", ast.I.spelling, ast.position);
return null;
}
@Override
public Object visitProcFormalParameter(ProcFormalParameter ast, Object o) {
public Object visitProcFormalParameter(ProcFormalParameter ast, Object o) {
idTable.openScope();
ast.FPS.visit(this, null);
idTable.closeScope();
idTable.enter(ast.I.spelling, ast);
if (ast.duplicated)
reporter.reportError("duplicated formal parameter \"%\"",
ast.I.spelling, ast.position);
reporter.reportError("duplicated formal parameter \"%\"", ast.I.spelling, ast.position);
return null;
}
@Override
public Object visitVarFormalParameter(VarFormalParameter ast, Object o) {
public Object visitVarFormalParameter(VarFormalParameter ast, Object o) {
ast.T = (TypeDenoter) ast.T.visit(this, null);
idTable.enter(ast.I.spelling, ast);
if (ast.duplicated)
reporter.reportError("duplicated formal parameter \"%\"",
ast.I.spelling, ast.position);
reporter.reportError("duplicated formal parameter \"%\"", ast.I.spelling, ast.position);
return null;
}
@Override
public Object visitEmptyFormalParameterSequence(EmptyFormalParameterSequence ast, Object o) {
public Object visitEmptyFormalParameterSequence(EmptyFormalParameterSequence ast, Object o) {
return null;
}
@Override
public Object visitMultipleFormalParameterSequence(MultipleFormalParameterSequence ast, Object o) {
public Object visitMultipleFormalParameterSequence(MultipleFormalParameterSequence ast, Object o) {
ast.FP.visit(this, null);
ast.FPS.visit(this, null);
return null;
}
@Override
public Object visitSingleFormalParameterSequence(SingleFormalParameterSequence ast, Object o) {
public Object visitSingleFormalParameterSequence(SingleFormalParameterSequence ast, Object o) {
ast.FP.visit(this, null);
return null;
}
@ -496,33 +475,28 @@ public Object visitSingleFormalParameterSequence(SingleFormalParameterSequence a
// Always returns null. Uses the given FormalParameter.
@Override
public Object visitConstActualParameter(ConstActualParameter ast, Object o) {
public Object visitConstActualParameter(ConstActualParameter ast, Object o) {
FormalParameter fp = (FormalParameter) o;
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
if (!(fp instanceof ConstFormalParameter))
reporter.reportError("const actual parameter not expected here", "",
ast.position);
reporter.reportError("const actual parameter not expected here", "", ast.position);
else if (!eType.equals(((ConstFormalParameter) fp).T))
reporter.reportError("wrong type for const actual parameter", "",
ast.E.position);
reporter.reportError("wrong type for const actual parameter", "", ast.E.position);
return null;
}
@Override
public Object visitFuncActualParameter(FuncActualParameter ast, Object o) {
public Object visitFuncActualParameter(FuncActualParameter ast, Object o) {
FormalParameter fp = (FormalParameter) o;
Declaration binding = (Declaration) ast.I.visit(this, null);
if (binding == null)
reportUndeclared(ast.I);
else if (!(binding instanceof FuncDeclaration ||
binding instanceof FuncFormalParameter))
reporter.reportError("\"%\" is not a function identifier",
ast.I.spelling, ast.I.position);
else if (!(binding instanceof FuncDeclaration || binding instanceof FuncFormalParameter))
reporter.reportError("\"%\" is not a function identifier", ast.I.spelling, ast.I.position);
else if (!(fp instanceof FuncFormalParameter))
reporter.reportError("func actual parameter not expected here", "",
ast.position);
reporter.reportError("func actual parameter not expected here", "", ast.position);
else {
FormalParameterSequence FPS = null;
TypeDenoter T = null;
@ -534,29 +508,24 @@ public Object visitFuncActualParameter(FuncActualParameter ast, Object o) {
T = ((FuncFormalParameter) binding).T;
}
if (!FPS.equals(((FuncFormalParameter) fp).FPS))
reporter.reportError("wrong signature for function \"%\"",
ast.I.spelling, ast.I.position);
reporter.reportError("wrong signature for function \"%\"", ast.I.spelling, ast.I.position);
else if (!T.equals(((FuncFormalParameter) fp).T))
reporter.reportError("wrong type for function \"%\"",
ast.I.spelling, ast.I.position);
reporter.reportError("wrong type for function \"%\"", ast.I.spelling, ast.I.position);
}
return null;
}
@Override
public Object visitProcActualParameter(ProcActualParameter ast, Object o) {
public Object visitProcActualParameter(ProcActualParameter ast, Object o) {
FormalParameter fp = (FormalParameter) o;
Declaration binding = (Declaration) ast.I.visit(this, null);
if (binding == null)
reportUndeclared(ast.I);
else if (!(binding instanceof ProcDeclaration ||
binding instanceof ProcFormalParameter))
reporter.reportError("\"%\" is not a procedure identifier",
ast.I.spelling, ast.I.position);
else if (!(binding instanceof ProcDeclaration || binding instanceof ProcFormalParameter))
reporter.reportError("\"%\" is not a procedure identifier", ast.I.spelling, ast.I.position);
else if (!(fp instanceof ProcFormalParameter))
reporter.reportError("proc actual parameter not expected here", "",
ast.position);
reporter.reportError("proc actual parameter not expected here", "", ast.position);
else {
FormalParameterSequence FPS = null;
if (binding instanceof ProcDeclaration)
@ -564,31 +533,27 @@ public Object visitProcActualParameter(ProcActualParameter ast, Object o) {
else
FPS = ((ProcFormalParameter) binding).FPS;
if (!FPS.equals(((ProcFormalParameter) fp).FPS))
reporter.reportError("wrong signature for procedure \"%\"",
ast.I.spelling, ast.I.position);
reporter.reportError("wrong signature for procedure \"%\"", ast.I.spelling, ast.I.position);
}
return null;
}
@Override
public Object visitVarActualParameter(VarActualParameter ast, Object o) {
public Object visitVarActualParameter(VarActualParameter ast, Object o) {
FormalParameter fp = (FormalParameter) o;
TypeDenoter vType = (TypeDenoter) ast.V.visit(this, null);
if (!ast.V.variable)
reporter.reportError("actual parameter is not a variable", "",
ast.V.position);
reporter.reportError("actual parameter is not a variable", "", ast.V.position);
else if (!(fp instanceof VarFormalParameter))
reporter.reportError("var actual parameter not expected here", "",
ast.V.position);
reporter.reportError("var actual parameter not expected here", "", ast.V.position);
else if (!vType.equals(((VarFormalParameter) fp).T))
reporter.reportError("wrong type for var actual parameter", "",
ast.V.position);
reporter.reportError("wrong type for var actual parameter", "", ast.V.position);
return null;
}
@Override
public Object visitEmptyActualParameterSequence(EmptyActualParameterSequence ast, Object o) {
public Object visitEmptyActualParameterSequence(EmptyActualParameterSequence ast, Object o) {
FormalParameterSequence fps = (FormalParameterSequence) o;
if (!(fps instanceof EmptyFormalParameterSequence))
reporter.reportError("too few actual parameters", "", ast.position);
@ -596,7 +561,7 @@ public Object visitEmptyActualParameterSequence(EmptyActualParameterSequence ast
}
@Override
public Object visitMultipleActualParameterSequence(MultipleActualParameterSequence ast, Object o) {
public Object visitMultipleActualParameterSequence(MultipleActualParameterSequence ast, Object o) {
FormalParameterSequence fps = (FormalParameterSequence) o;
if (!(fps instanceof MultipleFormalParameterSequence))
reporter.reportError("too many actual parameters", "", ast.position);
@ -608,7 +573,7 @@ public Object visitMultipleActualParameterSequence(MultipleActualParameterSequen
}
@Override
public Object visitSingleActualParameterSequence(SingleActualParameterSequence ast, Object o) {
public Object visitSingleActualParameterSequence(SingleActualParameterSequence ast, Object o) {
FormalParameterSequence fps = (FormalParameterSequence) o;
if (!(fps instanceof SingleFormalParameterSequence))
reporter.reportError("incorrect number of actual parameters", "", ast.position);
@ -624,12 +589,12 @@ public Object visitSingleActualParameterSequence(SingleActualParameterSequence a
// use the given object.
@Override
public Object visitAnyTypeDenoter(AnyTypeDenoter ast, Object o) {
public Object visitAnyTypeDenoter(AnyTypeDenoter ast, Object o) {
return StdEnvironment.anyType;
}
@Override
public Object visitArrayTypeDenoter(ArrayTypeDenoter ast, Object o) {
public Object visitArrayTypeDenoter(ArrayTypeDenoter ast, Object o) {
ast.T = (TypeDenoter) ast.T.visit(this, null);
if ((Integer.valueOf(ast.IL.spelling).intValue()) == 0)
reporter.reportError("arrays must not be empty", "", ast.IL.position);
@ -637,66 +602,65 @@ public Object visitArrayTypeDenoter(ArrayTypeDenoter ast, Object o) {
}
@Override
public Object visitBoolTypeDenoter(BoolTypeDenoter ast, Object o) {
public Object visitBoolTypeDenoter(BoolTypeDenoter ast, Object o) {
return StdEnvironment.booleanType;
}
@Override
public Object visitCharTypeDenoter(CharTypeDenoter ast, Object o) {
public Object visitCharTypeDenoter(CharTypeDenoter ast, Object o) {
return StdEnvironment.charType;
}
@Override
public Object visitErrorTypeDenoter(ErrorTypeDenoter ast, Object o) {
public Object visitErrorTypeDenoter(ErrorTypeDenoter ast, Object o) {
return StdEnvironment.errorType;
}
@Override
public Object visitSimpleTypeDenoter(SimpleTypeDenoter ast, Object o) {
public Object visitSimpleTypeDenoter(SimpleTypeDenoter ast, Object o) {
Declaration binding = (Declaration) ast.I.visit(this, null);
if (binding == null) {
reportUndeclared(ast.I);
return StdEnvironment.errorType;
} else if (!(binding instanceof TypeDeclaration)) {
reporter.reportError("\"%\" is not a type identifier",
ast.I.spelling, ast.I.position);
reporter.reportError("\"%\" is not a type identifier", ast.I.spelling, ast.I.position);
return StdEnvironment.errorType;
}
return ((TypeDeclaration) binding).T;
}
@Override
public Object visitIntTypeDenoter(IntTypeDenoter ast, Object o) {
public Object visitIntTypeDenoter(IntTypeDenoter ast, Object o) {
return StdEnvironment.integerType;
}
@Override
public Object visitRecordTypeDenoter(RecordTypeDenoter ast, Object o) {
public Object visitRecordTypeDenoter(RecordTypeDenoter ast, Object o) {
ast.FT = (FieldTypeDenoter) ast.FT.visit(this, null);
return ast;
}
@Override
public Object visitMultipleFieldTypeDenoter(MultipleFieldTypeDenoter ast, Object o) {
public Object visitMultipleFieldTypeDenoter(MultipleFieldTypeDenoter ast, Object o) {
ast.T = (TypeDenoter) ast.T.visit(this, null);
ast.FT.visit(this, null);
return ast;
}
@Override
public Object visitSingleFieldTypeDenoter(SingleFieldTypeDenoter ast, Object o) {
public Object visitSingleFieldTypeDenoter(SingleFieldTypeDenoter ast, Object o) {
ast.T = (TypeDenoter) ast.T.visit(this, null);
return ast;
}
// Literals, Identifiers and Operators
@Override
public Object visitCharacterLiteral(CharacterLiteral CL, Object o) {
public Object visitCharacterLiteral(CharacterLiteral CL, Object o) {
return StdEnvironment.charType;
}
@Override
public Object visitIdentifier(Identifier I, Object o) {
public Object visitIdentifier(Identifier I, Object o) {
Declaration binding = idTable.retrieve(I.spelling);
if (binding != null)
I.decl = binding;
@ -704,12 +668,12 @@ public Object visitIdentifier(Identifier I, Object o) {
}
@Override
public Object visitIntegerLiteral(IntegerLiteral IL, Object o) {
public Object visitIntegerLiteral(IntegerLiteral IL, Object o) {
return StdEnvironment.integerType;
}
@Override
public Object visitOperator(Operator O, Object o) {
public Object visitOperator(Operator O, Object o) {
Declaration binding = idTable.retrieve(O.spelling);
if (binding != null)
O.decl = binding;
@ -738,7 +702,7 @@ public Object visitOperator(Operator O, Object o) {
// given object.
@Override
public Object visitDotVname(DotVname ast, Object o) {
public Object visitDotVname(DotVname ast, Object o) {
ast.type = null;
TypeDenoter vType = (TypeDenoter) ast.V.visit(this, null);
ast.variable = ast.V.variable;
@ -747,14 +711,13 @@ public Object visitDotVname(DotVname ast, Object o) {
else {
ast.type = checkFieldIdentifier(((RecordTypeDenoter) vType).FT, ast.I);
if (ast.type == StdEnvironment.errorType)
reporter.reportError("no field \"%\" in this record type",
ast.I.spelling, ast.I.position);
reporter.reportError("no field \"%\" in this record type", ast.I.spelling, ast.I.position);
}
return ast.type;
}
@Override
public Object visitSimpleVname(SimpleVname ast, Object o) {
public Object visitSimpleVname(SimpleVname ast, Object o) {
ast.variable = false;
ast.type = StdEnvironment.errorType;
Declaration binding = (Declaration) ast.I.visit(this, null);
@ -773,13 +736,12 @@ public Object visitSimpleVname(SimpleVname ast, Object o) {
ast.type = ((VarFormalParameter) binding).T;
ast.variable = true;
} else
reporter.reportError("\"%\" is not a const or var identifier",
ast.I.spelling, ast.I.position);
reporter.reportError("\"%\" is not a const or var identifier", ast.I.spelling, ast.I.position);
return ast.type;
}
@Override
public Object visitSubscriptVname(SubscriptVname ast, Object o) {
public Object visitSubscriptVname(SubscriptVname ast, Object o) {
TypeDenoter vType = (TypeDenoter) ast.V.visit(this, null);
ast.variable = ast.V.variable;
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
@ -788,8 +750,7 @@ public Object visitSubscriptVname(SubscriptVname ast, Object o) {
reporter.reportError("array expected here", "", ast.V.position);
else {
if (!eType.equals(StdEnvironment.integerType))
reporter.reportError("Integer expression expected here", "",
ast.E.position);
reporter.reportError("Integer expression expected here", "", ast.E.position);
ast.type = ((ArrayTypeDenoter) vType).T;
}
}
@ -799,7 +760,7 @@ public Object visitSubscriptVname(SubscriptVname ast, Object o) {
// Programs
@Override
public Object visitProgram(Program ast, Object o) {
public Object visitProgram(Program ast, Object o) {
ast.C.visit(this, null);
return null;
}
@ -890,8 +851,7 @@ public Object visitProgram(Program ast, Object o) {
ProcDeclaration binding;
binding = new ProcDeclaration(new Identifier(id, dummyPos), fps,
new EmptyCommand(dummyPos), dummyPos);
binding = new ProcDeclaration(new Identifier(id, dummyPos), fps, new EmptyCommand(dummyPos), dummyPos);
idTable.enter(id, binding);
return binding;
}
@ -899,13 +859,12 @@ public Object visitProgram(Program ast, Object o) {
// Creates a small AST to represent the "declaration" of a standard
// type, and enters it in the identification table.
private FuncDeclaration declareStdFunc(String id, FormalParameterSequence fps,
TypeDenoter resultType) {
private FuncDeclaration declareStdFunc(String id, FormalParameterSequence fps, TypeDenoter resultType) {
FuncDeclaration binding;
binding = new FuncDeclaration(new Identifier(id, dummyPos), fps, resultType,
new EmptyExpression(dummyPos), dummyPos);
binding = new FuncDeclaration(new Identifier(id, dummyPos), fps, resultType, new EmptyExpression(dummyPos),
dummyPos);
idTable.enter(id, binding);
return binding;
}
@ -918,8 +877,7 @@ public Object visitProgram(Program ast, Object o) {
UnaryOperatorDeclaration binding;
binding = new UnaryOperatorDeclaration(new Operator(op, dummyPos),
argType, resultType, dummyPos);
binding = new UnaryOperatorDeclaration(new Operator(op, dummyPos), argType, resultType, dummyPos);
idTable.enter(op, binding);
return binding;
}
@ -933,8 +891,7 @@ public Object visitProgram(Program ast, Object o) {
BinaryOperatorDeclaration binding;
binding = new BinaryOperatorDeclaration(new Operator(op, dummyPos),
arg1Type, arg2type, resultType, dummyPos);
binding = new BinaryOperatorDeclaration(new Operator(op, dummyPos), arg1Type, arg2type, resultType, dummyPos);
idTable.enter(op, binding);
return binding;
}
@ -986,10 +943,14 @@ public Object visitProgram(Program ast, Object o) {
StdEnvironment.booleanType);
StdEnvironment.charDecl = declareStdType("Char", StdEnvironment.charType);
StdEnvironment.chrDecl = declareStdFunc("chr", new SingleFormalParameterSequence(
new ConstFormalParameter(dummyI, StdEnvironment.integerType, dummyPos), dummyPos), StdEnvironment.charType);
StdEnvironment.ordDecl = declareStdFunc("ord", new SingleFormalParameterSequence(
new ConstFormalParameter(dummyI, StdEnvironment.charType, dummyPos), dummyPos), StdEnvironment.integerType);
StdEnvironment.chrDecl = declareStdFunc("chr",
new SingleFormalParameterSequence(
new ConstFormalParameter(dummyI, StdEnvironment.integerType, dummyPos), dummyPos),
StdEnvironment.charType);
StdEnvironment.ordDecl = declareStdFunc("ord",
new SingleFormalParameterSequence(new ConstFormalParameter(dummyI, StdEnvironment.charType, dummyPos),
dummyPos),
StdEnvironment.integerType);
StdEnvironment.eofDecl = declareStdFunc("eof", new EmptyFormalParameterSequence(dummyPos),
StdEnvironment.booleanType);
StdEnvironment.eolDecl = declareStdFunc("eol", new EmptyFormalParameterSequence(dummyPos),

@ -36,9 +36,8 @@ public final class StdEnvironment {
public static UnaryOperatorDeclaration notDecl;
public static BinaryOperatorDeclaration andDecl, orDecl,
addDecl, subtractDecl, multiplyDecl, divideDecl, moduloDecl,
equalDecl, unequalDecl, lessDecl, notlessDecl, greaterDecl, notgreaterDecl;
public static BinaryOperatorDeclaration andDecl, orDecl, addDecl, subtractDecl, multiplyDecl, divideDecl,
moduloDecl, equalDecl, unequalDecl, lessDecl, notlessDecl, greaterDecl, notgreaterDecl;
public static ProcDeclaration getDecl, putDecl, getintDecl, putintDecl, geteolDecl, puteolDecl;

@ -153,8 +153,7 @@ public class Parser {
Command cAST = parseCommand();
programAST = new Program(cAST, previousTokenPosition);
if (currentToken.kind != Token.EOT) {
syntacticError("\"%\" not expected after end of program",
currentToken.spelling);
syntacticError("\"%\" not expected after end of program", currentToken.spelling);
}
} catch (SyntaxError s) {
return null;
@ -342,8 +341,7 @@ public class Parser {
break;
default:
syntacticError("\"%\" cannot start a command",
currentToken.spelling);
syntacticError("\"%\" cannot start a command", currentToken.spelling);
break;
}
@ -405,8 +403,7 @@ public class Parser {
while (currentToken.kind == Token.OPERATOR) {
Operator opAST = parseOperator();
Expression e2AST = parsePrimaryExpression();
expressionAST = new BinaryExpression(expressionAST, opAST, e2AST,
expressionPos);
expressionAST = new BinaryExpression(expressionAST, opAST, e2AST, expressionPos);
}
return expressionAST;
}
@ -483,8 +480,7 @@ public class Parser {
break;
default:
syntacticError("\"%\" cannot start an expression",
currentToken.spelling);
syntacticError("\"%\" cannot start an expression", currentToken.spelling);
break;
}
@ -550,8 +546,7 @@ public class Parser {
vnamePos = identifierAST.position;
Vname vAST = new SimpleVname(identifierAST, vnamePos);
while (currentToken.kind == Token.DOT ||
currentToken.kind == Token.LBRACKET) {
while (currentToken.kind == Token.DOT || currentToken.kind == Token.LBRACKET) {
if (currentToken.kind == Token.DOT) {
acceptIt();
@ -584,8 +579,7 @@ public class Parser {
acceptIt();
Declaration d2AST = parseSingleDeclaration();
finish(declarationPos);
declarationAST = new SequentialDeclaration(declarationAST, d2AST,
declarationPos);
declarationAST = new SequentialDeclaration(declarationAST, d2AST, declarationPos);
}
return declarationAST;
}
@ -642,8 +636,7 @@ public class Parser {
accept(Token.IS);
Expression eAST = parseExpression();
finish(declarationPos);
declarationAST = new FuncDeclaration(iAST, fpsAST, tAST, eAST,
declarationPos);
declarationAST = new FuncDeclaration(iAST, fpsAST, tAST, eAST, declarationPos);
}
break;
@ -658,8 +651,7 @@ public class Parser {
break;
default:
syntacticError("\"%\" cannot start a declaration",
currentToken.spelling);
syntacticError("\"%\" cannot start a declaration", currentToken.spelling);
break;
}
@ -698,8 +690,7 @@ public class Parser {
acceptIt();
FormalParameterSequence fpsAST = parseProperFormalParameterSequence();
finish(formalsPos);
formalsAST = new MultipleFormalParameterSequence(fpAST, fpsAST,
formalsPos);
formalsAST = new MultipleFormalParameterSequence(fpAST, fpsAST, formalsPos);
} else {
finish(formalsPos);
@ -760,8 +751,7 @@ public class Parser {
break;
default:
syntacticError("\"%\" cannot start a formal parameter",
currentToken.spelling);
syntacticError("\"%\" cannot start a formal parameter", currentToken.spelling);
break;
}
@ -795,8 +785,7 @@ public class Parser {
acceptIt();
ActualParameterSequence apsAST = parseProperActualParameterSequence();
finish(actualsPos);
actualsAST = new MultipleActualParameterSequence(apAST, apsAST,
actualsPos);
actualsAST = new MultipleActualParameterSequence(apAST, apsAST, actualsPos);
} else {
finish(actualsPos);
actualsAST = new SingleActualParameterSequence(apAST, actualsPos);
@ -853,8 +842,7 @@ public class Parser {
break;
default:
syntacticError("\"%\" cannot start an actual parameter",
currentToken.spelling);
syntacticError("\"%\" cannot start an actual parameter", currentToken.spelling);
break;
}
@ -902,8 +890,7 @@ public class Parser {
break;
default:
syntacticError("\"%\" cannot start a type denoter",
currentToken.spelling);
syntacticError("\"%\" cannot start a type denoter", currentToken.spelling);
break;
}

@ -34,10 +34,8 @@ public final class Scanner {
// isOperator returns true iff the given character is an operator character.
private boolean isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' ||
c == '=' || c == '<' || c == '>' || c == '\\' ||
c == '&' || c == '@' || c == '%' || c == '^' ||
c == '?');
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '=' || c == '<' || c == '>' || c == '\\'
|| c == '&' || c == '@' || c == '%' || c == '^' || c == '?');
}
///////////////////////////////////////////////////////////////////////////////
@ -249,10 +247,7 @@ public final class Scanner {
int kind;
currentlyScanningToken = false;
while (currentChar == '!'
|| currentChar == ' '
|| currentChar == '\n'
|| currentChar == '\r'
while (currentChar == '!' || currentChar == ' ' || currentChar == '\n' || currentChar == '\r'
|| currentChar == '\t')
scanSeparator();

@ -23,16 +23,18 @@ public class SourceFile {
java.io.FileInputStream source;
int currentLine;
public SourceFile(String filename) {
public static SourceFile ofPath(String pathname) {
try {
sourceFile = new java.io.File(filename);
source = new java.io.FileInputStream(sourceFile);
currentLine = 1;
return new SourceFile(pathname);
} catch (java.io.IOException s) {
sourceFile = null;
source = null;
currentLine = 0;
return null;
}
}
private SourceFile(String pathname) throws java.io.FileNotFoundException {
sourceFile = new java.io.File(pathname);
source = new java.io.FileInputStream(sourceFile);
currentLine = 1;
}
char getSource() {

@ -29,7 +29,7 @@ public class SourcePosition {
}
@Override
public String toString() {
public String toString() {
return "(" + start + ", " + finish + ")";
}
}

@ -18,7 +18,7 @@ class SyntaxError extends Exception {
private static final long serialVersionUID = -5280306336102766860L;
SyntaxError() {
SyntaxError() {
super();
}

@ -51,9 +51,8 @@ final class Token extends Object {
}
@Override
public String toString() {
return "Kind=" + kind + ", spelling=" + spelling +
", position=" + position;
public String toString() {
return "Kind=" + kind + ", spelling=" + spelling + ", position=" + position;
}
// Token classes...
@ -61,89 +60,25 @@ public String toString() {
public static final int
// literals, identifiers, operators...
INTLITERAL = 0,
CHARLITERAL = 1,
IDENTIFIER = 2,
OPERATOR = 3,
INTLITERAL = 0, CHARLITERAL = 1, IDENTIFIER = 2, OPERATOR = 3,
// 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,
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,
// punctuation...
DOT = 21,
COLON = 22,
SEMICOLON = 23,
COMMA = 24,
BECOMES = 25,
IS = 26,
DOT = 21, COLON = 22, SEMICOLON = 23, COMMA = 24, BECOMES = 25, IS = 26,
// brackets...
LPAREN = 27,
RPAREN = 28,
LBRACKET = 29,
RBRACKET = 30,
LCURLY = 31,
RCURLY = 32,
LPAREN = 27, RPAREN = 28, LBRACKET = 29, RBRACKET = 30, LCURLY = 31, RCURLY = 32,
// special tokens...
EOT = 33,
ERROR = 34;
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>"
};
private final static int firstReservedWord = Token.ARRAY,
lastReservedWord = Token.WHILE;
EOT = 33, ERROR = 34;
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>" };
private final static int firstReservedWord = Token.ARRAY, lastReservedWord = Token.WHILE;
}

@ -30,7 +30,7 @@ class DrawerFrame extends JFrame {
*/
private static final long serialVersionUID = -3650404598416929282L;
public DrawerFrame(JPanel panel) {
public DrawerFrame(JPanel panel) {
setSize(300, 200);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
@ -42,8 +42,7 @@ public DrawerFrame(JPanel panel) {
// Image img = tk.getImage("icon.gif");
// setIconImage(img);
addWindowListener(
new WindowAdapter() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);

@ -24,7 +24,7 @@ class DrawerPanel extends JPanel {
*
*/
private static final long serialVersionUID = 565914745506889669L;
private Drawer drawer;
private Drawer drawer;
public DrawerPanel(Drawer drawer) {
setPreferredSize(new Dimension(4096, 4096));
@ -32,7 +32,7 @@ private Drawer drawer;
}
@Override
public void paintComponent(Graphics g) {
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawer.paintAST(g);
}

@ -53,8 +53,7 @@ public class DrawingTree {
graphics.fillRect(pos.x, pos.y, width, height);
graphics.setColor(Color.black);
graphics.drawRect(pos.x, pos.y, width - 1, height - 1);
graphics.drawString(caption, pos.x + 2,
pos.y + (height + FIXED_FONT_HEIGHT) / 2);
graphics.drawString(caption, pos.x + 2, pos.y + (height + FIXED_FONT_HEIGHT) / 2);
if (children != null) {
for (DrawingTree child : children) {
@ -63,9 +62,7 @@ public class DrawingTree {
}
if (parent != null) {
graphics.drawLine(pos.x + width / 2, pos.y,
parent.pos.x + parent.width / 2,
parent.pos.y + parent.height);
graphics.drawLine(pos.x + width / 2, pos.y, parent.pos.x + parent.width / 2, parent.pos.y + parent.height);
}
}

@ -94,323 +94,322 @@ public class LayoutVisitor implements Visitor {
// Commands
@Override
public Object visitAssignCommand(AssignCommand ast, Object obj) {
public Object visitAssignCommand(AssignCommand ast, Object obj) {
return layoutBinary("AssignCom.", ast.V, ast.E);
}
@Override
public Object visitCallCommand(CallCommand ast, Object obj) {
public Object visitCallCommand(CallCommand ast, Object obj) {
return layoutBinary("CallCom.", ast.I, ast.APS);
}
@Override
public Object visitEmptyCommand(EmptyCommand ast, Object obj) {
public Object visitEmptyCommand(EmptyCommand ast, Object obj) {
return layoutNullary("EmptyCom.");
}
@Override
public Object visitIfCommand(IfCommand ast, Object obj) {
public Object visitIfCommand(IfCommand ast, Object obj) {
return layoutTernary("IfCom.", ast.E, ast.C1, ast.C2);
}
@Override
public Object visitLetCommand(LetCommand ast, Object obj) {
public Object visitLetCommand(LetCommand ast, Object obj) {
return layoutBinary("LetCom.", ast.D, ast.C);
}
@Override
public Object visitSequentialCommand(SequentialCommand ast, Object obj) {
public Object visitSequentialCommand(SequentialCommand ast, Object obj) {
return layoutBinary("Seq.Com.", ast.C1, ast.C2);
}
@Override
public Object visitWhileCommand(WhileCommand ast, Object obj) {
public Object visitWhileCommand(WhileCommand ast, Object obj) {
return layoutBinary("WhileCom.", ast.E, ast.C);
}
// Expressions
@Override
public Object visitArrayExpression(ArrayExpression ast, Object obj) {
public Object visitArrayExpression(ArrayExpression ast, Object obj) {
return layoutUnary("ArrayExpr.", ast.AA);
}
@Override
public Object visitBinaryExpression(BinaryExpression ast, Object obj) {
public Object visitBinaryExpression(BinaryExpression ast, Object obj) {
return layoutTernary("Bin.Expr.", ast.E1, ast.O, ast.E2);
}
@Override
public Object visitCallExpression(CallExpression ast, Object obj) {
public Object visitCallExpression(CallExpression ast, Object obj) {
return layoutBinary("CallExpr.", ast.I, ast.APS);
}
@Override
public Object visitCharacterExpression(CharacterExpression ast, Object obj) {
public Object visitCharacterExpression(CharacterExpression ast, Object obj) {
return layoutUnary("Char.Expr.", ast.CL);
}
@Override
public Object visitEmptyExpression(EmptyExpression ast, Object obj) {
public Object visitEmptyExpression(EmptyExpression ast, Object obj) {
return layoutNullary("EmptyExpr.");
}
@Override
public Object visitIfExpression(IfExpression ast, Object obj) {
public Object visitIfExpression(IfExpression ast, Object obj) {
return layoutTernary("IfExpr.", ast.E1, ast.E2, ast.E3);
}
@Override
public Object visitIntegerExpression(IntegerExpression ast, Object obj) {
public Object visitIntegerExpression(IntegerExpression ast, Object obj) {
return layoutUnary("Int.Expr.", ast.IL);
}
@Override
public Object visitLetExpression(LetExpression ast, Object obj) {
public Object visitLetExpression(LetExpression ast, Object obj) {
return layoutBinary("LetExpr.", ast.D, ast.E);
}
@Override
public Object visitRecordExpression(RecordExpression ast, Object obj) {
public Object visitRecordExpression(RecordExpression ast, Object obj) {
return layoutUnary("Rec.Expr.", ast.RA);
}
@Override
public Object visitUnaryExpression(UnaryExpression ast, Object obj) {
public Object visitUnaryExpression(UnaryExpression ast, Object obj) {
return layoutBinary("UnaryExpr.", ast.O, ast.E);
}
@Override
public Object visitVnameExpression(VnameExpression ast, Object obj) {
public Object visitVnameExpression(VnameExpression ast, Object obj) {
return layoutUnary("VnameExpr.", ast.V);
}
// Declarations
@Override
public Object visitBinaryOperatorDeclaration(BinaryOperatorDeclaration ast, Object obj) {
public Object visitBinaryOperatorDeclaration(BinaryOperatorDeclaration ast, Object obj) {
return layoutQuaternary("Bin.Op.Decl.", ast.O, ast.ARG1, ast.ARG2, ast.RES);
}
@Override
public Object visitConstDeclaration(ConstDeclaration ast, Object obj) {
public Object visitConstDeclaration(ConstDeclaration ast, Object obj) {
return layoutBinary("ConstDecl.", ast.I, ast.E);
}
@Override
public Object visitFuncDeclaration(FuncDeclaration ast, Object obj) {
public Object visitFuncDeclaration(FuncDeclaration ast, Object obj) {
return layoutQuaternary("FuncDecl.", ast.I, ast.FPS, ast.T, ast.E);
}
@Override
public Object visitProcDeclaration(ProcDeclaration ast, Object obj) {
public Object visitProcDeclaration(ProcDeclaration ast, Object obj) {
return layoutTernary("ProcDecl.", ast.I, ast.FPS, ast.C);
}
@Override
public Object visitSequentialDeclaration(SequentialDeclaration ast, Object obj) {
public Object visitSequentialDeclaration(SequentialDeclaration ast, Object obj) {
return layoutBinary("Seq.Decl.", ast.D1, ast.D2);
}
@Override
public Object visitTypeDeclaration(TypeDeclaration ast, Object obj) {
public Object visitTypeDeclaration(TypeDeclaration ast, Object obj) {
return layoutBinary("TypeDecl.", ast.I, ast.T);
}
@Override
public Object visitUnaryOperatorDeclaration(UnaryOperatorDeclaration ast, Object obj) {
public Object visitUnaryOperatorDeclaration(UnaryOperatorDeclaration ast, Object obj) {
return layoutTernary("UnaryOp.Decl.", ast.O, ast.ARG, ast.RES);
}
@Override
public Object visitVarDeclaration(VarDeclaration ast, Object obj) {
public Object visitVarDeclaration(VarDeclaration ast, Object obj) {
return layoutBinary("VarDecl.", ast.I, ast.T);
}
// Array Aggregates
@Override
public Object visitMultipleArrayAggregate(MultipleArrayAggregate ast, Object obj) {
public Object visitMultipleArrayAggregate(MultipleArrayAggregate ast, Object obj) {
return layoutBinary("Mult.ArrayAgg.", ast.E, ast.AA);
}
@Override
public Object visitSingleArrayAggregate(SingleArrayAggregate ast, Object obj) {
public Object visitSingleArrayAggregate(SingleArrayAggregate ast, Object obj) {
return layoutUnary("Sing.ArrayAgg.", ast.E);
}
// Record Aggregates
@Override
public Object visitMultipleRecordAggregate(MultipleRecordAggregate ast, Object obj) {
public Object visitMultipleRecordAggregate(MultipleRecordAggregate ast, Object obj) {
return layoutTernary("Mult.Rec.Agg.", ast.I, ast.E, ast.RA);
}
@Override
public Object visitSingleRecordAggregate(SingleRecordAggregate ast, Object obj) {
public Object visitSingleRecordAggregate(SingleRecordAggregate ast, Object obj) {
return layoutBinary("Sing.Rec.Agg.", ast.I, ast.E);
}
// Formal Parameters
@Override
public Object visitConstFormalParameter(ConstFormalParameter ast, Object obj) {
public Object visitConstFormalParameter(ConstFormalParameter ast, Object obj) {
return layoutBinary("ConstF.P.", ast.I, ast.T);
}
@Override
public Object visitFuncFormalParameter(FuncFormalParameter ast, Object obj) {
public Object visitFuncFormalParameter(FuncFormalParameter ast, Object obj) {
return layoutTernary("FuncF.P.", ast.I, ast.FPS, ast.T);
}
@Override
public Object visitProcFormalParameter(ProcFormalParameter ast, Object obj) {
public Object visitProcFormalParameter(ProcFormalParameter ast, Object obj) {
return layoutBinary("ProcF.P.", ast.I, ast.FPS);
}
@Override
public Object visitVarFormalParameter(VarFormalParameter ast, Object obj) {
public Object visitVarFormalParameter(VarFormalParameter ast, Object obj) {
return layoutBinary("VarF.P.", ast.I, ast.T);
}
@Override
public Object visitEmptyFormalParameterSequence(EmptyFormalParameterSequence ast, Object obj) {
public Object visitEmptyFormalParameterSequence(EmptyFormalParameterSequence ast, Object obj) {
return layoutNullary("EmptyF.P.S.");
}
@Override
public Object visitMultipleFormalParameterSequence(MultipleFormalParameterSequence ast, Object obj) {
public Object visitMultipleFormalParameterSequence(MultipleFormalParameterSequence ast, Object obj) {
return layoutBinary("Mult.F.P.S.", ast.FP, ast.FPS);
}
@Override
public Object visitSingleFormalParameterSequence(SingleFormalParameterSequence ast, Object obj) {
public Object visitSingleFormalParameterSequence(SingleFormalParameterSequence ast, Object obj) {
return layoutUnary("Sing.F.P.S.", ast.FP);
}
// Actual Parameters
@Override
public Object visitConstActualParameter(ConstActualParameter ast, Object obj) {
public Object visitConstActualParameter(ConstActualParameter ast, Object obj) {
return layoutUnary("ConstA.P.", ast.E);
}
@Override
public Object visitFuncActualParameter(FuncActualParameter ast, Object obj) {
public Object visitFuncActualParameter(FuncActualParameter ast, Object obj) {
return layoutUnary("FuncA.P.", ast.I);
}
@Override
public Object visitProcActualParameter(ProcActualParameter ast, Object obj) {
public Object visitProcActualParameter(ProcActualParameter ast, Object obj) {
return layoutUnary("ProcA.P.", ast.I);
}
@Override
public Object visitVarActualParameter(VarActualParameter ast, Object obj) {
public Object visitVarActualParameter(VarActualParameter ast, Object obj) {
return layoutUnary("VarA.P.", ast.V);
}
@Override
public Object visitEmptyActualParameterSequence(EmptyActualParameterSequence ast, Object obj) {
public Object visitEmptyActualParameterSequence(EmptyActualParameterSequence ast, Object obj) {
return layoutNullary("EmptyA.P.S.");
}
@Override
public Object visitMultipleActualParameterSequence(MultipleActualParameterSequence ast, Object obj) {
public Object visitMultipleActualParameterSequence(MultipleActualParameterSequence ast, Object obj) {
return layoutBinary("Mult.A.P.S.", ast.AP, ast.APS);
}
@Override
public Object visitSingleActualParameterSequence(SingleActualParameterSequence ast, Object obj) {
public Object visitSingleActualParameterSequence(SingleActualParameterSequence ast, Object obj) {
return layoutUnary("Sing.A.P.S.", ast.AP);
}
// Type Denoters
@Override
public Object visitAnyTypeDenoter(AnyTypeDenoter ast, Object obj) {
public Object visitAnyTypeDenoter(AnyTypeDenoter ast, Object obj) {
return layoutNullary("any");
}
@Override
public Object visitArrayTypeDenoter(ArrayTypeDenoter ast, Object obj) {
public Object visitArrayTypeDenoter(ArrayTypeDenoter ast, Object obj) {
return layoutBinary("ArrayTypeD.", ast.IL, ast.T);
}
@Override
public Object visitBoolTypeDenoter(BoolTypeDenoter ast, Object obj) {
public Object visitBoolTypeDenoter(BoolTypeDenoter ast, Object obj) {
return layoutNullary("bool");
}
@Override
public Object visitCharTypeDenoter(CharTypeDenoter ast, Object obj) {
public Object visitCharTypeDenoter(CharTypeDenoter ast, Object obj) {
return layoutNullary("char");
}
@Override
public Object visitErrorTypeDenoter(ErrorTypeDenoter ast, Object obj) {
public Object visitErrorTypeDenoter(ErrorTypeDenoter ast, Object obj) {
return layoutNullary("error");
}
@Override
public Object visitSimpleTypeDenoter(SimpleTypeDenoter ast, Object obj) {
public Object visitSimpleTypeDenoter(SimpleTypeDenoter ast, Object obj) {
return layoutUnary("Sim.TypeD.", ast.I);
}
@Override
public Object visitIntTypeDenoter(IntTypeDenoter ast, Object obj) {
public Object visitIntTypeDenoter(IntTypeDenoter ast, Object obj) {
return layoutNullary("int");
}
@Override
public Object visitRecordTypeDenoter(RecordTypeDenoter ast, Object obj) {
public Object visitRecordTypeDenoter(RecordTypeDenoter ast, Object obj) {
return layoutUnary("Rec.TypeD.", ast.FT);
}
@Override
public Object visitMultipleFieldTypeDenoter(MultipleFieldTypeDenoter ast, Object obj) {
public Object visitMultipleFieldTypeDenoter(MultipleFieldTypeDenoter ast, Object obj) {
return layoutTernary("Mult.F.TypeD.", ast.I, ast.T, ast.FT);
}
@Override
public Object visitSingleFieldTypeDenoter(SingleFieldTypeDenoter ast, Object obj) {
public Object visitSingleFieldTypeDenoter(SingleFieldTypeDenoter ast, Object obj) {
return layoutBinary("Sing.F.TypeD.", ast.I, ast.T);
}
// Literals, Identifiers and Operators
@Override
public Object visitCharacterLiteral(CharacterLiteral ast, Object obj) {
public Object visitCharacterLiteral(CharacterLiteral ast, Object obj) {
return layoutNullary(ast.spelling);
}
@Override
public Object visitIdentifier(Identifier ast, Object obj) {
public Object visitIdentifier(Identifier ast, Object obj) {
return layoutNullary(ast.spelling);
}
@Override
public Object visitIntegerLiteral(IntegerLiteral ast, Object obj) {
public Object visitIntegerLiteral(IntegerLiteral ast, Object obj) {
return layoutNullary(ast.spelling);
}
@Override
public Object visitOperator(Operator ast, Object obj) {
public Object visitOperator(Operator ast, Object obj) {
return layoutNullary(ast.spelling);
}
// Value-or-variable names
@Override
public Object visitDotVname(DotVname ast, Object obj) {
public Object visitDotVname(DotVname ast, Object obj) {
return layoutBinary("DotVname", ast.I, ast.V);
}
@Override
public Object visitSimpleVname(SimpleVname ast, Object obj) {
public Object visitSimpleVname(SimpleVname ast, Object obj) {
return layoutUnary("Sim.Vname", ast.I);
}
@Override
public Object visitSubscriptVname(SubscriptVname ast, Object obj) {
return layoutBinary("Sub.Vname",
ast.V, ast.E);
public Object visitSubscriptVname(SubscriptVname ast, Object obj) {
return layoutBinary("Sub.Vname", ast.V, ast.E);
}
// Programs
@Override
public Object visitProgram(Program ast, Object obj) {
public Object visitProgram(Program ast, Object obj) {
return layoutUnary("Program", ast.C);
}
@ -446,8 +445,7 @@ public Object visitProgram(Program ast, Object obj) {
return dt;
}
private DrawingTree layoutTernary(String name, AST child1, AST child2,
AST child3) {
private DrawingTree layoutTernary(String name, AST child1, AST child2, AST child3) {
DrawingTree dt = layoutCaption(name);
DrawingTree d1 = (DrawingTree) child1.visit(this, null);
DrawingTree d2 = (DrawingTree) child2.visit(this, null);
@ -457,8 +455,7 @@ public Object visitProgram(Program ast, Object obj) {
return dt;
}
private DrawingTree layoutQuaternary(String name, AST child1, AST child2,
AST child3, AST child4) {
private DrawingTree layoutQuaternary(String name, AST child1, AST child2, AST child3, AST child4) {
DrawingTree dt = layoutCaption(name);
DrawingTree d1 = (DrawingTree) child1.visit(this, null);
DrawingTree d2 = (DrawingTree) child2.visit(this, null);
@ -476,10 +473,8 @@ public Object visitProgram(Program ast, Object obj) {
dt.children[0].offset.y = y + dt.height;
dt.children[0].offset.x = x1;
dt.contour.upper_head = new Polyline(0, dt.height,
new Polyline(x1, y, dt.contour.upper_head));
dt.contour.lower_head = new Polyline(0, dt.height,
new Polyline(x2, y, dt.contour.lower_head));
dt.contour.upper_head = new Polyline(0, dt.height, new Polyline(x1, y, dt.contour.upper_head));
dt.contour.lower_head = new Polyline(0, dt.height, new Polyline(x2, y, dt.contour.lower_head));
}
private int join(DrawingTree dt) {
@ -573,8 +568,7 @@ public Object visitProgram(Program ast, Object obj) {
}
}
private Polyline bridge(Polyline line1, int x1, int y1,
Polyline line2, int x2, int y2) {
private Polyline bridge(Polyline line1, int x1, int y1, Polyline line2, int x2, int y2) {
int dy, dx, s;
Polyline r;