Format code and fix warnings.
This commit is contained in:
parent
2c426b5b2e
commit
a02acc009b
@ -20,8 +20,8 @@ import java.io.FileNotFoundException;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disassembles the TAM code in the given file, and displays the
|
* Disassembles the TAM code in the given file, and displays the instructions on
|
||||||
* instructions on standard output.
|
* standard output.
|
||||||
*
|
*
|
||||||
* For example:
|
* For example:
|
||||||
*
|
*
|
||||||
@ -43,8 +43,8 @@ public class Disassembler {
|
|||||||
static int CT;
|
static int CT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes the r-field of an instruction in the form "l<I>reg</I>r", where
|
* Writes the r-field of an instruction in the form "l<I>reg</I>r", where l and
|
||||||
* l and r are the bracket characters to use.
|
* r are the bracket characters to use.
|
||||||
*
|
*
|
||||||
* @param leftbracket the character to print before the register.
|
* @param leftbracket the character to print before the register.
|
||||||
* @param r the number of the register.
|
* @param r the number of the register.
|
||||||
|
@ -29,16 +29,13 @@ public class Interpreter {
|
|||||||
|
|
||||||
// DATA STORE REGISTERS AND OTHER REGISTERS
|
// DATA STORE REGISTERS AND OTHER REGISTERS
|
||||||
|
|
||||||
final static int CB = 0,
|
final static int CB = 0, SB = 0, HB = 1024; // = upper bound of data array + 1
|
||||||
SB = 0,
|
|
||||||
HB = 1024; // = upper bound of data array + 1
|
|
||||||
|
|
||||||
static int CT, CP, ST, HT, LB, status;
|
static int CT, CP, ST, HT, LB, status;
|
||||||
|
|
||||||
// status values
|
// status values
|
||||||
final static int running = 0, halted = 1, failedDataStoreFull = 2, failedInvalidCodeAddress = 3,
|
final static int running = 0, halted = 1, failedDataStoreFull = 2, failedInvalidCodeAddress = 3,
|
||||||
failedInvalidInstruction = 4, failedOverflow = 5, failedZeroDivide = 6,
|
failedInvalidInstruction = 4, failedOverflow = 5, failedZeroDivide = 6, failedIOError = 7;
|
||||||
failedIOError = 7;
|
|
||||||
|
|
||||||
static long accumulator;
|
static long accumulator;
|
||||||
|
|
||||||
@ -88,8 +85,7 @@ public class Interpreter {
|
|||||||
|
|
||||||
static void dump() {
|
static void dump() {
|
||||||
// Writes a summary of the machine state.
|
// Writes a summary of the machine state.
|
||||||
int addr, staticLink, dynamicLink,
|
int addr, staticLink, dynamicLink, localRegNum;
|
||||||
localRegNum;
|
|
||||||
|
|
||||||
System.out.println("");
|
System.out.println("");
|
||||||
System.out.println("State of data store and registers:");
|
System.out.println("State of data store and registers:");
|
||||||
|
@ -32,21 +32,8 @@ public final class Machine {
|
|||||||
// INSTRUCTIONS
|
// INSTRUCTIONS
|
||||||
|
|
||||||
// Operation codes
|
// Operation codes
|
||||||
public final static int LOADop = 0,
|
public final static int LOADop = 0, LOADAop = 1, LOADIop = 2, LOADLop = 3, STOREop = 4, STOREIop = 5, CALLop = 6,
|
||||||
LOADAop = 1,
|
CALLIop = 7, RETURNop = 8, PUSHop = 10, POPop = 11, JUMPop = 12, JUMPIop = 13, JUMPIFop = 14, HALTop = 15;
|
||||||
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
|
// CODE STORE
|
||||||
|
|
||||||
@ -54,72 +41,31 @@ public final class Machine {
|
|||||||
|
|
||||||
// CODE STORE REGISTERS
|
// CODE STORE REGISTERS
|
||||||
|
|
||||||
public final static int CB = 0,
|
public final static int CB = 0, PB = 1024, // = upper bound of code array + 1
|
||||||
PB = 1024, // = upper bound of code array + 1
|
|
||||||
PT = 1052; // = PB + 28
|
PT = 1052; // = PB + 28
|
||||||
|
|
||||||
// REGISTER NUMBERS
|
// REGISTER NUMBERS
|
||||||
|
|
||||||
public final static int CBr = 0,
|
public final static int CBr = 0, CTr = 1, PBr = 2, PTr = 3, SBr = 4, STr = 5, HBr = 6, HTr = 7, LBr = 8,
|
||||||
CTr = 1,
|
L1r = LBr + 1, L2r = LBr + 2, L3r = LBr + 3, L4r = LBr + 4, L5r = LBr + 5, L6r = LBr + 6, CPr = 15;
|
||||||
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
|
// DATA REPRESENTATION
|
||||||
|
|
||||||
public final static int booleanSize = 1,
|
public final static int booleanSize = 1, characterSize = 1, integerSize = 1, addressSize = 1,
|
||||||
characterSize = 1,
|
|
||||||
integerSize = 1,
|
|
||||||
addressSize = 1,
|
|
||||||
closureSize = 2 * addressSize,
|
closureSize = 2 * addressSize,
|
||||||
|
|
||||||
linkDataSize = 3 * addressSize,
|
linkDataSize = 3 * addressSize,
|
||||||
|
|
||||||
falseRep = 0,
|
falseRep = 0, trueRep = 1, maxintRep = 32767;
|
||||||
trueRep = 1,
|
|
||||||
maxintRep = 32767;
|
|
||||||
|
|
||||||
// ADDRESSES OF PRIMITIVE ROUTINES
|
// ADDRESSES OF PRIMITIVE ROUTINES
|
||||||
|
|
||||||
public final static int idDisplacement = 1,
|
public final static int idDisplacement = 1, notDisplacement = 2, andDisplacement = 3, orDisplacement = 4,
|
||||||
notDisplacement = 2,
|
succDisplacement = 5, predDisplacement = 6, negDisplacement = 7, addDisplacement = 8, subDisplacement = 9,
|
||||||
andDisplacement = 3,
|
multDisplacement = 10, divDisplacement = 11, modDisplacement = 12, ltDisplacement = 13, leDisplacement = 14,
|
||||||
orDisplacement = 4,
|
geDisplacement = 15, gtDisplacement = 16, eqDisplacement = 17, neDisplacement = 18, eolDisplacement = 19,
|
||||||
succDisplacement = 5,
|
eofDisplacement = 20, getDisplacement = 21, putDisplacement = 22, geteolDisplacement = 23,
|
||||||
predDisplacement = 6,
|
puteolDisplacement = 24, getintDisplacement = 25, putintDisplacement = 26, newDisplacement = 27,
|
||||||
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;
|
disposeDisplacement = 28;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class ArrayExpression extends Expression {
|
public class ArrayExpression extends Expression {
|
||||||
|
|
||||||
public ArrayExpression(ArrayAggregate aaAST,
|
public ArrayExpression(ArrayAggregate aaAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
AA = aaAST;
|
AA = aaAST;
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class ArrayTypeDenoter extends TypeDenoter {
|
public class ArrayTypeDenoter extends TypeDenoter {
|
||||||
|
|
||||||
public ArrayTypeDenoter(IntegerLiteral ilAST, TypeDenoter tAST,
|
public ArrayTypeDenoter(IntegerLiteral ilAST, TypeDenoter tAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
IL = ilAST;
|
IL = ilAST;
|
||||||
T = tAST;
|
T = tAST;
|
||||||
@ -35,8 +34,8 @@ public boolean equals(Object obj) {
|
|||||||
if (obj != null && obj instanceof ErrorTypeDenoter)
|
if (obj != null && obj instanceof ErrorTypeDenoter)
|
||||||
return true;
|
return true;
|
||||||
else if (obj != null && obj instanceof ArrayTypeDenoter)
|
else if (obj != null && obj instanceof ArrayTypeDenoter)
|
||||||
return this.IL.spelling.compareTo(((ArrayTypeDenoter) obj).IL.spelling) == 0 &&
|
return this.IL.spelling.compareTo(((ArrayTypeDenoter) obj).IL.spelling) == 0
|
||||||
this.T.equals(((ArrayTypeDenoter) obj).T);
|
&& this.T.equals(((ArrayTypeDenoter) obj).T);
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class BinaryExpression extends Expression {
|
public class BinaryExpression extends Expression {
|
||||||
|
|
||||||
public BinaryExpression(Expression e1AST, Operator oAST, Expression e2AST,
|
public BinaryExpression(Expression e1AST, Operator oAST, Expression e2AST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
O = oAST;
|
O = oAST;
|
||||||
E1 = e1AST;
|
E1 = e1AST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class BinaryOperatorDeclaration extends Declaration {
|
public class BinaryOperatorDeclaration extends Declaration {
|
||||||
|
|
||||||
public BinaryOperatorDeclaration(Operator oAST, TypeDenoter arg1AST,
|
public BinaryOperatorDeclaration(Operator oAST, TypeDenoter arg1AST, TypeDenoter arg2AST, TypeDenoter resultAST,
|
||||||
TypeDenoter arg2AST, TypeDenoter resultAST,
|
|
||||||
SourcePosition thePosition) {
|
SourcePosition thePosition) {
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
O = oAST;
|
O = oAST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class CallCommand extends Command {
|
public class CallCommand extends Command {
|
||||||
|
|
||||||
public CallCommand(Identifier iAST, ActualParameterSequence apsAST,
|
public CallCommand(Identifier iAST, ActualParameterSequence apsAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
APS = apsAST;
|
APS = apsAST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class CallExpression extends Expression {
|
public class CallExpression extends Expression {
|
||||||
|
|
||||||
public CallExpression(Identifier iAST, ActualParameterSequence apsAST,
|
public CallExpression(Identifier iAST, ActualParameterSequence apsAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
APS = apsAST;
|
APS = apsAST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class ConstDeclaration extends Declaration {
|
public class ConstDeclaration extends Declaration {
|
||||||
|
|
||||||
public ConstDeclaration(Identifier iAST, Expression eAST,
|
public ConstDeclaration(Identifier iAST, Expression eAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
E = eAST;
|
E = eAST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class ConstFormalParameter extends FormalParameter {
|
public class ConstFormalParameter extends FormalParameter {
|
||||||
|
|
||||||
public ConstFormalParameter(Identifier iAST, TypeDenoter tAST,
|
public ConstFormalParameter(Identifier iAST, TypeDenoter tAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
T = tAST;
|
T = tAST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class FuncDeclaration extends Declaration {
|
public class FuncDeclaration extends Declaration {
|
||||||
|
|
||||||
public FuncDeclaration(Identifier iAST, FormalParameterSequence fpsAST,
|
public FuncDeclaration(Identifier iAST, FormalParameterSequence fpsAST, TypeDenoter tAST, Expression eAST,
|
||||||
TypeDenoter tAST, Expression eAST,
|
|
||||||
SourcePosition thePosition) {
|
SourcePosition thePosition) {
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
|
@ -18,8 +18,8 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class FuncFormalParameter extends FormalParameter {
|
public class FuncFormalParameter extends FormalParameter {
|
||||||
|
|
||||||
public FuncFormalParameter(Identifier iAST, FormalParameterSequence fpsAST,
|
public FuncFormalParameter(Identifier iAST, FormalParameterSequence fpsAST, TypeDenoter tAST,
|
||||||
TypeDenoter tAST, SourcePosition thePosition) {
|
SourcePosition thePosition) {
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
FPS = fpsAST;
|
FPS = fpsAST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class IfCommand extends Command {
|
public class IfCommand extends Command {
|
||||||
|
|
||||||
public IfCommand(Expression eAST, Command c1AST, Command c2AST,
|
public IfCommand(Expression eAST, Command c1AST, Command c2AST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
E = eAST;
|
E = eAST;
|
||||||
C1 = c1AST;
|
C1 = c1AST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class IfExpression extends Expression {
|
public class IfExpression extends Expression {
|
||||||
|
|
||||||
public IfExpression(Expression e1AST, Expression e2AST, Expression e3AST,
|
public IfExpression(Expression e1AST, Expression e2AST, Expression e3AST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
E1 = e1AST;
|
E1 = e1AST;
|
||||||
E2 = e2AST;
|
E2 = e2AST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class MultipleArrayAggregate extends ArrayAggregate {
|
public class MultipleArrayAggregate extends ArrayAggregate {
|
||||||
|
|
||||||
public MultipleArrayAggregate(Expression eAST, ArrayAggregate aaAST,
|
public MultipleArrayAggregate(Expression eAST, ArrayAggregate aaAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
E = eAST;
|
E = eAST;
|
||||||
AA = aaAST;
|
AA = aaAST;
|
||||||
|
@ -35,9 +35,7 @@ public Object visit(Visitor v, Object o) {
|
|||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj != null && obj instanceof MultipleFieldTypeDenoter) {
|
if (obj != null && obj instanceof MultipleFieldTypeDenoter) {
|
||||||
MultipleFieldTypeDenoter ft = (MultipleFieldTypeDenoter) obj;
|
MultipleFieldTypeDenoter ft = (MultipleFieldTypeDenoter) obj;
|
||||||
return (this.I.spelling.compareTo(ft.I.spelling) == 0) &&
|
return (this.I.spelling.compareTo(ft.I.spelling) == 0) && this.T.equals(ft.T) && this.FT.equals(ft.FT);
|
||||||
this.T.equals(ft.T) &&
|
|
||||||
this.FT.equals(ft.FT);
|
|
||||||
} else
|
} else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class ProcDeclaration extends Declaration {
|
public class ProcDeclaration extends Declaration {
|
||||||
|
|
||||||
public ProcDeclaration(Identifier iAST, FormalParameterSequence fpsAST,
|
public ProcDeclaration(Identifier iAST, FormalParameterSequence fpsAST, Command cAST, SourcePosition thePosition) {
|
||||||
Command cAST, SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
FPS = fpsAST;
|
FPS = fpsAST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class ProcFormalParameter extends FormalParameter {
|
public class ProcFormalParameter extends FormalParameter {
|
||||||
|
|
||||||
public ProcFormalParameter(Identifier iAST, FormalParameterSequence fpsAST,
|
public ProcFormalParameter(Identifier iAST, FormalParameterSequence fpsAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
FPS = fpsAST;
|
FPS = fpsAST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class SequentialDeclaration extends Declaration {
|
public class SequentialDeclaration extends Declaration {
|
||||||
|
|
||||||
public SequentialDeclaration(Declaration d1AST, Declaration d2AST,
|
public SequentialDeclaration(Declaration d1AST, Declaration d2AST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
D1 = d1AST;
|
D1 = d1AST;
|
||||||
D2 = d2AST;
|
D2 = d2AST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class SingleActualParameterSequence extends ActualParameterSequence {
|
public class SingleActualParameterSequence extends ActualParameterSequence {
|
||||||
|
|
||||||
public SingleActualParameterSequence(ActualParameter apAST,
|
public SingleActualParameterSequence(ActualParameter apAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
AP = apAST;
|
AP = apAST;
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class SingleArrayAggregate extends ArrayAggregate {
|
public class SingleArrayAggregate extends ArrayAggregate {
|
||||||
|
|
||||||
public SingleArrayAggregate(Expression eAST,
|
public SingleArrayAggregate(Expression eAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
E = eAST;
|
E = eAST;
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class SingleFieldTypeDenoter extends FieldTypeDenoter {
|
public class SingleFieldTypeDenoter extends FieldTypeDenoter {
|
||||||
|
|
||||||
public SingleFieldTypeDenoter(Identifier iAST, TypeDenoter tAST,
|
public SingleFieldTypeDenoter(Identifier iAST, TypeDenoter tAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
T = tAST;
|
T = tAST;
|
||||||
@ -34,8 +33,7 @@ public Object visit(Visitor v, Object o) {
|
|||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj != null && obj instanceof SingleFieldTypeDenoter) {
|
if (obj != null && obj instanceof SingleFieldTypeDenoter) {
|
||||||
SingleFieldTypeDenoter ft = (SingleFieldTypeDenoter) obj;
|
SingleFieldTypeDenoter ft = (SingleFieldTypeDenoter) obj;
|
||||||
return (this.I.spelling.compareTo(ft.I.spelling) == 0) &&
|
return (this.I.spelling.compareTo(ft.I.spelling) == 0) && this.T.equals(ft.T);
|
||||||
this.T.equals(ft.T);
|
|
||||||
} else
|
} else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class SingleFormalParameterSequence extends FormalParameterSequence {
|
public class SingleFormalParameterSequence extends FormalParameterSequence {
|
||||||
|
|
||||||
public SingleFormalParameterSequence(FormalParameter fpAST,
|
public SingleFormalParameterSequence(FormalParameter fpAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
FP = fpAST;
|
FP = fpAST;
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class SingleRecordAggregate extends RecordAggregate {
|
public class SingleRecordAggregate extends RecordAggregate {
|
||||||
|
|
||||||
public SingleRecordAggregate(Identifier iAST, Expression eAST,
|
public SingleRecordAggregate(Identifier iAST, Expression eAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
E = eAST;
|
E = eAST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class TypeDeclaration extends Declaration {
|
public class TypeDeclaration extends Declaration {
|
||||||
|
|
||||||
public TypeDeclaration(Identifier iAST, TypeDenoter tAST,
|
public TypeDeclaration(Identifier iAST, TypeDenoter tAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
T = tAST;
|
T = tAST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class UnaryExpression extends Expression {
|
public class UnaryExpression extends Expression {
|
||||||
|
|
||||||
public UnaryExpression(Operator oAST, Expression eAST,
|
public UnaryExpression(Operator oAST, Expression eAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
O = oAST;
|
O = oAST;
|
||||||
E = eAST;
|
E = eAST;
|
||||||
|
@ -18,8 +18,8 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class UnaryOperatorDeclaration extends Declaration {
|
public class UnaryOperatorDeclaration extends Declaration {
|
||||||
|
|
||||||
public UnaryOperatorDeclaration(Operator oAST, TypeDenoter argAST,
|
public UnaryOperatorDeclaration(Operator oAST, TypeDenoter argAST, TypeDenoter resultAST,
|
||||||
TypeDenoter resultAST, SourcePosition thePosition) {
|
SourcePosition thePosition) {
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
O = oAST;
|
O = oAST;
|
||||||
ARG = argAST;
|
ARG = argAST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class VarDeclaration extends Declaration {
|
public class VarDeclaration extends Declaration {
|
||||||
|
|
||||||
public VarDeclaration(Identifier iAST, TypeDenoter tAST,
|
public VarDeclaration(Identifier iAST, TypeDenoter tAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
T = tAST;
|
T = tAST;
|
||||||
|
@ -18,8 +18,7 @@ import Triangle.SyntacticAnalyzer.SourcePosition;
|
|||||||
|
|
||||||
public class VarFormalParameter extends FormalParameter {
|
public class VarFormalParameter extends FormalParameter {
|
||||||
|
|
||||||
public VarFormalParameter(Identifier iAST, TypeDenoter tAST,
|
public VarFormalParameter(Identifier iAST, TypeDenoter tAST, SourcePosition thePosition) {
|
||||||
SourcePosition thePosition) {
|
|
||||||
super(thePosition);
|
super(thePosition);
|
||||||
I = iAST;
|
I = iAST;
|
||||||
T = tAST;
|
T = tAST;
|
||||||
|
@ -97,8 +97,7 @@ public final class Encoder implements Visitor {
|
|||||||
public Object visitAssignCommand(AssignCommand ast, Object o) {
|
public Object visitAssignCommand(AssignCommand ast, Object o) {
|
||||||
Frame frame = (Frame) o;
|
Frame frame = (Frame) o;
|
||||||
Integer valSize = (Integer) ast.E.visit(this, frame);
|
Integer valSize = (Integer) ast.E.visit(this, frame);
|
||||||
encodeStore(ast.V, new Frame(frame, valSize.intValue()),
|
encodeStore(ast.V, new Frame(frame, valSize.intValue()), valSize.intValue());
|
||||||
valSize.intValue());
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,8 +192,7 @@ public Object visitCallExpression(CallExpression ast, Object o) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitCharacterExpression(CharacterExpression ast,
|
public Object visitCharacterExpression(CharacterExpression ast, Object o) {
|
||||||
Object o) {
|
|
||||||
Frame frame = (Frame) o;
|
Frame frame = (Frame) o;
|
||||||
Integer valSize = (Integer) ast.type.visit(this, null);
|
Integer valSize = (Integer) ast.type.visit(this, null);
|
||||||
emit(Machine.LOADLop, 0, 0, ast.CL.spelling.charAt(1));
|
emit(Machine.LOADLop, 0, 0, ast.CL.spelling.charAt(1));
|
||||||
@ -270,8 +268,7 @@ public Object visitVnameExpression(VnameExpression ast, Object o) {
|
|||||||
|
|
||||||
// Declarations
|
// Declarations
|
||||||
@Override
|
@Override
|
||||||
public Object visitBinaryOperatorDeclaration(BinaryOperatorDeclaration ast,
|
public Object visitBinaryOperatorDeclaration(BinaryOperatorDeclaration ast, Object o) {
|
||||||
Object o) {
|
|
||||||
return Integer.valueOf(0);
|
return Integer.valueOf(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -282,12 +279,10 @@ public Object visitConstDeclaration(ConstDeclaration ast, Object o) {
|
|||||||
|
|
||||||
if (ast.E instanceof CharacterExpression) {
|
if (ast.E instanceof CharacterExpression) {
|
||||||
CharacterLiteral CL = ((CharacterExpression) ast.E).CL;
|
CharacterLiteral CL = ((CharacterExpression) ast.E).CL;
|
||||||
ast.entity = new KnownValue(Machine.characterSize,
|
ast.entity = new KnownValue(Machine.characterSize, characterValuation(CL.spelling));
|
||||||
characterValuation(CL.spelling));
|
|
||||||
} else if (ast.E instanceof IntegerExpression) {
|
} else if (ast.E instanceof IntegerExpression) {
|
||||||
IntegerLiteral IL = ((IntegerExpression) ast.E).IL;
|
IntegerLiteral IL = ((IntegerExpression) ast.E).IL;
|
||||||
ast.entity = new KnownValue(Machine.integerSize,
|
ast.entity = new KnownValue(Machine.integerSize, Integer.parseInt(IL.spelling));
|
||||||
Integer.parseInt(IL.spelling));
|
|
||||||
} else {
|
} else {
|
||||||
int valSize = ((Integer) ast.E.visit(this, frame)).intValue();
|
int valSize = ((Integer) ast.E.visit(this, frame)).intValue();
|
||||||
ast.entity = new UnknownValue(valSize, frame.level, frame.size);
|
ast.entity = new UnknownValue(valSize, frame.level, frame.size);
|
||||||
@ -326,8 +321,7 @@ public Object visitProcDeclaration(ProcDeclaration ast, Object o) {
|
|||||||
int argsSize = 0;
|
int argsSize = 0;
|
||||||
|
|
||||||
emit(Machine.JUMPop, 0, Machine.CBr, 0);
|
emit(Machine.JUMPop, 0, Machine.CBr, 0);
|
||||||
ast.entity = new KnownRoutine(Machine.closureSize, frame.level,
|
ast.entity = new KnownRoutine(Machine.closureSize, frame.level, nextInstrAddr);
|
||||||
nextInstrAddr);
|
|
||||||
writeTableDetails(ast);
|
writeTableDetails(ast);
|
||||||
if (frame.level == Machine.maxRoutineLevel)
|
if (frame.level == Machine.maxRoutineLevel)
|
||||||
reporter.reportRestriction("can't nest routines so deeply");
|
reporter.reportRestriction("can't nest routines so deeply");
|
||||||
@ -361,8 +355,7 @@ public Object visitTypeDeclaration(TypeDeclaration ast, Object o) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitUnaryOperatorDeclaration(UnaryOperatorDeclaration ast,
|
public Object visitUnaryOperatorDeclaration(UnaryOperatorDeclaration ast, Object o) {
|
||||||
Object o) {
|
|
||||||
return Integer.valueOf(0);
|
return Integer.valueOf(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -380,8 +373,7 @@ public Object visitVarDeclaration(VarDeclaration ast, Object o) {
|
|||||||
|
|
||||||
// Array Aggregates
|
// Array Aggregates
|
||||||
@Override
|
@Override
|
||||||
public Object visitMultipleArrayAggregate(MultipleArrayAggregate ast,
|
public Object visitMultipleArrayAggregate(MultipleArrayAggregate ast, Object o) {
|
||||||
Object o) {
|
|
||||||
Frame frame = (Frame) o;
|
Frame frame = (Frame) o;
|
||||||
int elemSize = ((Integer) ast.E.visit(this, frame)).intValue();
|
int elemSize = ((Integer) ast.E.visit(this, frame)).intValue();
|
||||||
Frame frame1 = new Frame(frame, elemSize);
|
Frame frame1 = new Frame(frame, elemSize);
|
||||||
@ -396,8 +388,7 @@ public Object visitSingleArrayAggregate(SingleArrayAggregate ast, Object o) {
|
|||||||
|
|
||||||
// Record Aggregates
|
// Record Aggregates
|
||||||
@Override
|
@Override
|
||||||
public Object visitMultipleRecordAggregate(MultipleRecordAggregate ast,
|
public Object visitMultipleRecordAggregate(MultipleRecordAggregate ast, Object o) {
|
||||||
Object o) {
|
|
||||||
Frame frame = (Frame) o;
|
Frame frame = (Frame) o;
|
||||||
int fieldSize = ((Integer) ast.E.visit(this, frame)).intValue();
|
int fieldSize = ((Integer) ast.E.visit(this, frame)).intValue();
|
||||||
Frame frame1 = new Frame(frame, fieldSize);
|
Frame frame1 = new Frame(frame, fieldSize);
|
||||||
@ -406,8 +397,7 @@ public Object visitMultipleRecordAggregate(MultipleRecordAggregate ast,
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitSingleRecordAggregate(SingleRecordAggregate ast,
|
public Object visitSingleRecordAggregate(SingleRecordAggregate ast, Object o) {
|
||||||
Object o) {
|
|
||||||
return ast.E.visit(this, o);
|
return ast.E.visit(this, o);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -425,8 +415,7 @@ public Object visitConstFormalParameter(ConstFormalParameter ast, Object o) {
|
|||||||
public Object visitFuncFormalParameter(FuncFormalParameter ast, Object o) {
|
public Object visitFuncFormalParameter(FuncFormalParameter ast, Object o) {
|
||||||
Frame frame = (Frame) o;
|
Frame frame = (Frame) o;
|
||||||
int argsSize = Machine.closureSize;
|
int argsSize = Machine.closureSize;
|
||||||
ast.entity = new UnknownRoutine(Machine.closureSize, frame.level,
|
ast.entity = new UnknownRoutine(Machine.closureSize, frame.level, -frame.size - argsSize);
|
||||||
-frame.size - argsSize);
|
|
||||||
writeTableDetails(ast);
|
writeTableDetails(ast);
|
||||||
return Integer.valueOf(argsSize);
|
return Integer.valueOf(argsSize);
|
||||||
}
|
}
|
||||||
@ -435,8 +424,7 @@ public Object visitFuncFormalParameter(FuncFormalParameter ast, Object o) {
|
|||||||
public Object visitProcFormalParameter(ProcFormalParameter ast, Object o) {
|
public Object visitProcFormalParameter(ProcFormalParameter ast, Object o) {
|
||||||
Frame frame = (Frame) o;
|
Frame frame = (Frame) o;
|
||||||
int argsSize = Machine.closureSize;
|
int argsSize = Machine.closureSize;
|
||||||
ast.entity = new UnknownRoutine(Machine.closureSize, frame.level,
|
ast.entity = new UnknownRoutine(Machine.closureSize, frame.level, -frame.size - argsSize);
|
||||||
-frame.size - argsSize);
|
|
||||||
writeTableDetails(ast);
|
writeTableDetails(ast);
|
||||||
return Integer.valueOf(argsSize);
|
return Integer.valueOf(argsSize);
|
||||||
}
|
}
|
||||||
@ -445,21 +433,18 @@ public Object visitProcFormalParameter(ProcFormalParameter ast, Object o) {
|
|||||||
public Object visitVarFormalParameter(VarFormalParameter ast, Object o) {
|
public Object visitVarFormalParameter(VarFormalParameter ast, Object o) {
|
||||||
Frame frame = (Frame) o;
|
Frame frame = (Frame) o;
|
||||||
ast.T.visit(this, null);
|
ast.T.visit(this, null);
|
||||||
ast.entity = new UnknownAddress(Machine.addressSize, frame.level,
|
ast.entity = new UnknownAddress(Machine.addressSize, frame.level, -frame.size - Machine.addressSize);
|
||||||
-frame.size - Machine.addressSize);
|
|
||||||
writeTableDetails(ast);
|
writeTableDetails(ast);
|
||||||
return Integer.valueOf(Machine.addressSize);
|
return Integer.valueOf(Machine.addressSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitEmptyFormalParameterSequence(
|
public Object visitEmptyFormalParameterSequence(EmptyFormalParameterSequence ast, Object o) {
|
||||||
EmptyFormalParameterSequence ast, Object o) {
|
|
||||||
return Integer.valueOf(0);
|
return Integer.valueOf(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitMultipleFormalParameterSequence(
|
public Object visitMultipleFormalParameterSequence(MultipleFormalParameterSequence ast, Object o) {
|
||||||
MultipleFormalParameterSequence ast, Object o) {
|
|
||||||
Frame frame = (Frame) o;
|
Frame frame = (Frame) o;
|
||||||
int argsSize1 = ((Integer) ast.FPS.visit(this, frame)).intValue();
|
int argsSize1 = ((Integer) ast.FPS.visit(this, frame)).intValue();
|
||||||
Frame frame1 = new Frame(frame, argsSize1);
|
Frame frame1 = new Frame(frame, argsSize1);
|
||||||
@ -468,8 +453,7 @@ public Object visitMultipleFormalParameterSequence(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitSingleFormalParameterSequence(
|
public Object visitSingleFormalParameterSequence(SingleFormalParameterSequence ast, Object o) {
|
||||||
SingleFormalParameterSequence ast, Object o) {
|
|
||||||
return ast.FP.visit(this, o);
|
return ast.FP.visit(this, o);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -489,8 +473,8 @@ public Object visitFuncActualParameter(FuncActualParameter ast, Object o) {
|
|||||||
emit(Machine.LOADAop, 0, Machine.CBr, address.displacement);
|
emit(Machine.LOADAop, 0, Machine.CBr, address.displacement);
|
||||||
} else if (ast.I.decl.entity instanceof UnknownRoutine) {
|
} else if (ast.I.decl.entity instanceof UnknownRoutine) {
|
||||||
ObjectAddress address = ((UnknownRoutine) ast.I.decl.entity).address;
|
ObjectAddress address = ((UnknownRoutine) ast.I.decl.entity).address;
|
||||||
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level,
|
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level, address.level),
|
||||||
address.level), address.displacement);
|
address.displacement);
|
||||||
} else if (ast.I.decl.entity instanceof PrimitiveRoutine) {
|
} else if (ast.I.decl.entity instanceof PrimitiveRoutine) {
|
||||||
int displacement = ((PrimitiveRoutine) ast.I.decl.entity).displacement;
|
int displacement = ((PrimitiveRoutine) ast.I.decl.entity).displacement;
|
||||||
// static link, code address
|
// static link, code address
|
||||||
@ -510,8 +494,8 @@ public Object visitProcActualParameter(ProcActualParameter ast, Object o) {
|
|||||||
emit(Machine.LOADAop, 0, Machine.CBr, address.displacement);
|
emit(Machine.LOADAop, 0, Machine.CBr, address.displacement);
|
||||||
} else if (ast.I.decl.entity instanceof UnknownRoutine) {
|
} else if (ast.I.decl.entity instanceof UnknownRoutine) {
|
||||||
ObjectAddress address = ((UnknownRoutine) ast.I.decl.entity).address;
|
ObjectAddress address = ((UnknownRoutine) ast.I.decl.entity).address;
|
||||||
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level,
|
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level, address.level),
|
||||||
address.level), address.displacement);
|
address.displacement);
|
||||||
} else if (ast.I.decl.entity instanceof PrimitiveRoutine) {
|
} else if (ast.I.decl.entity instanceof PrimitiveRoutine) {
|
||||||
int displacement = ((PrimitiveRoutine) ast.I.decl.entity).displacement;
|
int displacement = ((PrimitiveRoutine) ast.I.decl.entity).displacement;
|
||||||
// static link, code address
|
// static link, code address
|
||||||
@ -528,14 +512,12 @@ public Object visitVarActualParameter(VarActualParameter ast, Object o) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitEmptyActualParameterSequence(
|
public Object visitEmptyActualParameterSequence(EmptyActualParameterSequence ast, Object o) {
|
||||||
EmptyActualParameterSequence ast, Object o) {
|
|
||||||
return Integer.valueOf(0);
|
return Integer.valueOf(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitMultipleActualParameterSequence(
|
public Object visitMultipleActualParameterSequence(MultipleActualParameterSequence ast, Object o) {
|
||||||
MultipleActualParameterSequence ast, Object o) {
|
|
||||||
Frame frame = (Frame) o;
|
Frame frame = (Frame) o;
|
||||||
int argsSize1 = ((Integer) ast.AP.visit(this, frame)).intValue();
|
int argsSize1 = ((Integer) ast.AP.visit(this, frame)).intValue();
|
||||||
Frame frame1 = new Frame(frame, argsSize1);
|
Frame frame1 = new Frame(frame, argsSize1);
|
||||||
@ -544,8 +526,7 @@ public Object visitMultipleActualParameterSequence(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitSingleActualParameterSequence(
|
public Object visitSingleActualParameterSequence(SingleActualParameterSequence ast, Object o) {
|
||||||
SingleActualParameterSequence ast, Object o) {
|
|
||||||
return ast.AP.visit(this, o);
|
return ast.AP.visit(this, o);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -592,8 +573,7 @@ public Object visitErrorTypeDenoter(ErrorTypeDenoter ast, Object o) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitSimpleTypeDenoter(SimpleTypeDenoter ast,
|
public Object visitSimpleTypeDenoter(SimpleTypeDenoter ast, Object o) {
|
||||||
Object o) {
|
|
||||||
return Integer.valueOf(0);
|
return Integer.valueOf(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -619,8 +599,7 @@ public Object visitRecordTypeDenoter(RecordTypeDenoter ast, Object o) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitMultipleFieldTypeDenoter(MultipleFieldTypeDenoter ast,
|
public Object visitMultipleFieldTypeDenoter(MultipleFieldTypeDenoter ast, Object o) {
|
||||||
Object o) {
|
|
||||||
int offset = ((Integer) o).intValue();
|
int offset = ((Integer) o).intValue();
|
||||||
int fieldSize;
|
int fieldSize;
|
||||||
|
|
||||||
@ -637,8 +616,7 @@ public Object visitMultipleFieldTypeDenoter(MultipleFieldTypeDenoter ast,
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitSingleFieldTypeDenoter(SingleFieldTypeDenoter ast,
|
public Object visitSingleFieldTypeDenoter(SingleFieldTypeDenoter ast, Object o) {
|
||||||
Object o) {
|
|
||||||
int offset = ((Integer) o).intValue();
|
int offset = ((Integer) o).intValue();
|
||||||
int fieldSize;
|
int fieldSize;
|
||||||
|
|
||||||
@ -663,12 +641,11 @@ public Object visitIdentifier(Identifier ast, Object o) {
|
|||||||
Frame frame = (Frame) o;
|
Frame frame = (Frame) o;
|
||||||
if (ast.decl.entity instanceof KnownRoutine) {
|
if (ast.decl.entity instanceof KnownRoutine) {
|
||||||
ObjectAddress address = ((KnownRoutine) ast.decl.entity).address;
|
ObjectAddress address = ((KnownRoutine) ast.decl.entity).address;
|
||||||
emit(Machine.CALLop, displayRegister(frame.level, address.level),
|
emit(Machine.CALLop, displayRegister(frame.level, address.level), Machine.CBr, address.displacement);
|
||||||
Machine.CBr, address.displacement);
|
|
||||||
} else if (ast.decl.entity instanceof UnknownRoutine) {
|
} else if (ast.decl.entity instanceof UnknownRoutine) {
|
||||||
ObjectAddress address = ((UnknownRoutine) ast.decl.entity).address;
|
ObjectAddress address = ((UnknownRoutine) ast.decl.entity).address;
|
||||||
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level,
|
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level, address.level),
|
||||||
address.level), address.displacement);
|
address.displacement);
|
||||||
emit(Machine.CALLIop, 0, 0, 0);
|
emit(Machine.CALLIop, 0, 0, 0);
|
||||||
} else if (ast.decl.entity instanceof PrimitiveRoutine) {
|
} else if (ast.decl.entity instanceof PrimitiveRoutine) {
|
||||||
int displacement = ((PrimitiveRoutine) ast.decl.entity).displacement;
|
int displacement = ((PrimitiveRoutine) ast.decl.entity).displacement;
|
||||||
@ -692,12 +669,11 @@ public Object visitOperator(Operator ast, Object o) {
|
|||||||
Frame frame = (Frame) o;
|
Frame frame = (Frame) o;
|
||||||
if (ast.decl.entity instanceof KnownRoutine) {
|
if (ast.decl.entity instanceof KnownRoutine) {
|
||||||
ObjectAddress address = ((KnownRoutine) ast.decl.entity).address;
|
ObjectAddress address = ((KnownRoutine) ast.decl.entity).address;
|
||||||
emit(Machine.CALLop, displayRegister(frame.level, address.level),
|
emit(Machine.CALLop, displayRegister(frame.level, address.level), Machine.CBr, address.displacement);
|
||||||
Machine.CBr, address.displacement);
|
|
||||||
} else if (ast.decl.entity instanceof UnknownRoutine) {
|
} else if (ast.decl.entity instanceof UnknownRoutine) {
|
||||||
ObjectAddress address = ((UnknownRoutine) ast.decl.entity).address;
|
ObjectAddress address = ((UnknownRoutine) ast.decl.entity).address;
|
||||||
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level,
|
emit(Machine.LOADop, Machine.closureSize, displayRegister(frame.level, address.level),
|
||||||
address.level), address.displacement);
|
address.displacement);
|
||||||
emit(Machine.CALLIop, 0, 0, 0);
|
emit(Machine.CALLIop, 0, 0, 0);
|
||||||
} else if (ast.decl.entity instanceof PrimitiveRoutine) {
|
} else if (ast.decl.entity instanceof PrimitiveRoutine) {
|
||||||
int displacement = ((PrimitiveRoutine) ast.decl.entity).displacement;
|
int displacement = ((PrimitiveRoutine) ast.decl.entity).displacement;
|
||||||
@ -749,8 +725,7 @@ public Object visitSubscriptVname(SubscriptVname ast, Object o) {
|
|||||||
indexSize = ((Integer) ast.E.visit(this, frame)).intValue();
|
indexSize = ((Integer) ast.E.visit(this, frame)).intValue();
|
||||||
if (elemSize != 1) {
|
if (elemSize != 1) {
|
||||||
emit(Machine.LOADLop, 0, 0, elemSize);
|
emit(Machine.LOADLop, 0, 0, elemSize);
|
||||||
emit(Machine.CALLop, Machine.SBr, Machine.PBr,
|
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.multDisplacement);
|
||||||
Machine.multDisplacement);
|
|
||||||
}
|
}
|
||||||
if (ast.indexed)
|
if (ast.indexed)
|
||||||
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
||||||
@ -785,8 +760,7 @@ public Object visitProgram(Program ast, Object o) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Decides run-time representation of a standard constant.
|
// Decides run-time representation of a standard constant.
|
||||||
private final void elaborateStdConst(Declaration constDeclaration,
|
private final void elaborateStdConst(Declaration constDeclaration, int value) {
|
||||||
int value) {
|
|
||||||
|
|
||||||
if (constDeclaration instanceof ConstDeclaration) {
|
if (constDeclaration instanceof ConstDeclaration) {
|
||||||
ConstDeclaration decl = (ConstDeclaration) constDeclaration;
|
ConstDeclaration decl = (ConstDeclaration) constDeclaration;
|
||||||
@ -797,20 +771,17 @@ public Object visitProgram(Program ast, Object o) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Decides run-time representation of a standard routine.
|
// Decides run-time representation of a standard routine.
|
||||||
private final void elaborateStdPrimRoutine(Declaration routineDeclaration,
|
private final void elaborateStdPrimRoutine(Declaration routineDeclaration, int routineOffset) {
|
||||||
int routineOffset) {
|
|
||||||
routineDeclaration.entity = new PrimitiveRoutine(Machine.closureSize, routineOffset);
|
routineDeclaration.entity = new PrimitiveRoutine(Machine.closureSize, routineOffset);
|
||||||
writeTableDetails(routineDeclaration);
|
writeTableDetails(routineDeclaration);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final void elaborateStdEqRoutine(Declaration routineDeclaration,
|
private final void elaborateStdEqRoutine(Declaration routineDeclaration, int routineOffset) {
|
||||||
int routineOffset) {
|
|
||||||
routineDeclaration.entity = new EqualityRoutine(Machine.closureSize, routineOffset);
|
routineDeclaration.entity = new EqualityRoutine(Machine.closureSize, routineOffset);
|
||||||
writeTableDetails(routineDeclaration);
|
writeTableDetails(routineDeclaration);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final void elaborateStdRoutine(Declaration routineDeclaration,
|
private final void elaborateStdRoutine(Declaration routineDeclaration, int routineOffset) {
|
||||||
int routineOffset) {
|
|
||||||
routineDeclaration.entity = new KnownRoutine(Machine.closureSize, 0, routineOffset);
|
routineDeclaration.entity = new KnownRoutine(Machine.closureSize, 0, routineOffset);
|
||||||
writeTableDetails(routineDeclaration);
|
writeTableDetails(routineDeclaration);
|
||||||
}
|
}
|
||||||
@ -947,18 +918,17 @@ public Object visitProgram(Program ast, Object o) {
|
|||||||
if (baseObject instanceof KnownAddress) {
|
if (baseObject instanceof KnownAddress) {
|
||||||
ObjectAddress address = ((KnownAddress) baseObject).address;
|
ObjectAddress address = ((KnownAddress) baseObject).address;
|
||||||
if (V.indexed) {
|
if (V.indexed) {
|
||||||
emit(Machine.LOADAop, 0, displayRegister(frame.level, address.level),
|
emit(Machine.LOADAop, 0, displayRegister(frame.level, address.level), address.displacement + V.offset);
|
||||||
address.displacement + V.offset);
|
|
||||||
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
||||||
emit(Machine.STOREIop, valSize, 0, 0);
|
emit(Machine.STOREIop, valSize, 0, 0);
|
||||||
} else {
|
} else {
|
||||||
emit(Machine.STOREop, valSize, displayRegister(frame.level,
|
emit(Machine.STOREop, valSize, displayRegister(frame.level, address.level),
|
||||||
address.level), address.displacement + V.offset);
|
address.displacement + V.offset);
|
||||||
}
|
}
|
||||||
} else if (baseObject instanceof UnknownAddress) {
|
} else if (baseObject instanceof UnknownAddress) {
|
||||||
ObjectAddress address = ((UnknownAddress) baseObject).address;
|
ObjectAddress address = ((UnknownAddress) baseObject).address;
|
||||||
emit(Machine.LOADop, Machine.addressSize, displayRegister(frame.level,
|
emit(Machine.LOADop, Machine.addressSize, displayRegister(frame.level, address.level),
|
||||||
address.level), address.displacement);
|
address.displacement);
|
||||||
if (V.indexed)
|
if (V.indexed)
|
||||||
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
||||||
if (V.offset != 0) {
|
if (V.offset != 0) {
|
||||||
@ -988,22 +958,20 @@ public Object visitProgram(Program ast, Object o) {
|
|||||||
// presumably offset = 0 and indexed = false
|
// presumably offset = 0 and indexed = false
|
||||||
int value = ((KnownValue) baseObject).value;
|
int value = ((KnownValue) baseObject).value;
|
||||||
emit(Machine.LOADLop, 0, 0, value);
|
emit(Machine.LOADLop, 0, 0, value);
|
||||||
} else if ((baseObject instanceof UnknownValue) ||
|
} else if ((baseObject instanceof UnknownValue) || (baseObject instanceof KnownAddress)) {
|
||||||
(baseObject instanceof KnownAddress)) {
|
|
||||||
ObjectAddress address = (baseObject instanceof UnknownValue) ? ((UnknownValue) baseObject).address
|
ObjectAddress address = (baseObject instanceof UnknownValue) ? ((UnknownValue) baseObject).address
|
||||||
: ((KnownAddress) baseObject).address;
|
: ((KnownAddress) baseObject).address;
|
||||||
if (V.indexed) {
|
if (V.indexed) {
|
||||||
emit(Machine.LOADAop, 0, displayRegister(frame.level, address.level),
|
emit(Machine.LOADAop, 0, displayRegister(frame.level, address.level), address.displacement + V.offset);
|
||||||
address.displacement + V.offset);
|
|
||||||
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
||||||
emit(Machine.LOADIop, valSize, 0, 0);
|
emit(Machine.LOADIop, valSize, 0, 0);
|
||||||
} else
|
} else
|
||||||
emit(Machine.LOADop, valSize, displayRegister(frame.level,
|
emit(Machine.LOADop, valSize, displayRegister(frame.level, address.level),
|
||||||
address.level), address.displacement + V.offset);
|
address.displacement + V.offset);
|
||||||
} else if (baseObject instanceof UnknownAddress) {
|
} else if (baseObject instanceof UnknownAddress) {
|
||||||
ObjectAddress address = ((UnknownAddress) baseObject).address;
|
ObjectAddress address = ((UnknownAddress) baseObject).address;
|
||||||
emit(Machine.LOADop, Machine.addressSize, displayRegister(frame.level,
|
emit(Machine.LOADop, Machine.addressSize, displayRegister(frame.level, address.level),
|
||||||
address.level), address.displacement);
|
address.displacement);
|
||||||
if (V.indexed)
|
if (V.indexed)
|
||||||
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
||||||
if (V.offset != 0) {
|
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 indexed = true, code will have been generated to load an index value.
|
||||||
if (baseObject instanceof KnownAddress) {
|
if (baseObject instanceof KnownAddress) {
|
||||||
ObjectAddress address = ((KnownAddress) baseObject).address;
|
ObjectAddress address = ((KnownAddress) baseObject).address;
|
||||||
emit(Machine.LOADAop, 0, displayRegister(frame.level, address.level),
|
emit(Machine.LOADAop, 0, displayRegister(frame.level, address.level), address.displacement + V.offset);
|
||||||
address.displacement + V.offset);
|
|
||||||
if (V.indexed)
|
if (V.indexed)
|
||||||
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
||||||
} else if (baseObject instanceof UnknownAddress) {
|
} else if (baseObject instanceof UnknownAddress) {
|
||||||
ObjectAddress address = ((UnknownAddress) baseObject).address;
|
ObjectAddress address = ((UnknownAddress) baseObject).address;
|
||||||
emit(Machine.LOADop, Machine.addressSize, displayRegister(frame.level,
|
emit(Machine.LOADop, Machine.addressSize, displayRegister(frame.level, address.level),
|
||||||
address.level), address.displacement);
|
address.displacement);
|
||||||
if (V.indexed)
|
if (V.indexed)
|
||||||
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
emit(Machine.CALLop, Machine.SBr, Machine.PBr, Machine.addDisplacement);
|
||||||
if (V.offset != 0) {
|
if (V.offset != 0) {
|
||||||
|
@ -46,27 +46,22 @@ public class Compiler {
|
|||||||
/**
|
/**
|
||||||
* Compile the source program to TAM machine code.
|
* Compile the source program to TAM machine code.
|
||||||
*
|
*
|
||||||
* @param sourceName the name of the file containing the
|
* @param sourceName the name of the file containing the source program.
|
||||||
* source program.
|
* @param objectName the name of the file containing the object program.
|
||||||
* @param objectName the name of the file containing the
|
* @param showingAST true iff the AST is to be displayed after contextual
|
||||||
* object program.
|
* analysis (not currently implemented).
|
||||||
* @param showingAST true iff the AST is to be displayed after
|
* @param showingTable true iff the object description details are to be
|
||||||
* contextual analysis (not currently implemented).
|
* displayed during code generation (not currently
|
||||||
* @param showingTable true iff the object description details are to
|
* implemented).
|
||||||
* be displayed during code generation (not
|
* @return true iff the source program is free of compile-time errors, otherwise
|
||||||
* currently implemented).
|
* false.
|
||||||
* @return true iff the source program is free of compile-time errors,
|
|
||||||
* otherwise false.
|
|
||||||
*/
|
*/
|
||||||
static boolean compileProgram(String sourceName, String objectName,
|
static boolean compileProgram(String sourceName, String objectName, boolean showingAST, boolean showingTable) {
|
||||||
boolean showingAST, boolean showingTable) {
|
|
||||||
|
|
||||||
System.out.println("********** " +
|
System.out.println("********** " + "Triangle Compiler (Java Version 2.1)" + " **********");
|
||||||
"Triangle Compiler (Java Version 2.1)" +
|
|
||||||
" **********");
|
|
||||||
|
|
||||||
System.out.println("Syntactic Analysis ...");
|
System.out.println("Syntactic Analysis ...");
|
||||||
SourceFile source = new SourceFile(sourceName);
|
SourceFile source = SourceFile.ofPath(sourceName);
|
||||||
|
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
System.out.println("Can't access source file " + sourceName);
|
System.out.println("Can't access source file " + sourceName);
|
||||||
@ -110,11 +105,10 @@ public class Compiler {
|
|||||||
/**
|
/**
|
||||||
* Triangle compiler main program.
|
* Triangle compiler main program.
|
||||||
*
|
*
|
||||||
* @param args the only command-line argument to the program specifies
|
* @param args the only command-line argument to the program specifies the
|
||||||
* the source filename.
|
* source filename.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
boolean compiledOK;
|
|
||||||
|
|
||||||
if (args.length != 1) {
|
if (args.length != 1) {
|
||||||
System.out.println("Usage: tc filename");
|
System.out.println("Usage: tc filename");
|
||||||
@ -122,6 +116,8 @@ public class Compiler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String sourceName = args[0];
|
String sourceName = args[0];
|
||||||
compiledOK = compileProgram(sourceName, objectName, false, false);
|
var compiledOK = compileProgram(sourceName, objectName, false, false);
|
||||||
|
|
||||||
|
System.exit(compiledOK ? 0 : 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,8 +115,7 @@ public Object visitCallCommand(CallCommand ast, Object o) {
|
|||||||
} else if (binding instanceof ProcFormalParameter) {
|
} else if (binding instanceof ProcFormalParameter) {
|
||||||
ast.APS.visit(this, ((ProcFormalParameter) binding).FPS);
|
ast.APS.visit(this, ((ProcFormalParameter) binding).FPS);
|
||||||
} else
|
} else
|
||||||
reporter.reportError("\"%\" is not a procedure identifier",
|
reporter.reportError("\"%\" is not a procedure identifier", ast.I.spelling, ast.I.position);
|
||||||
ast.I.spelling, ast.I.position);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,8 +167,7 @@ public Object visitWhileCommand(WhileCommand ast, Object o) {
|
|||||||
@Override
|
@Override
|
||||||
public Object visitArrayExpression(ArrayExpression ast, Object o) {
|
public Object visitArrayExpression(ArrayExpression ast, Object o) {
|
||||||
TypeDenoter elemType = (TypeDenoter) ast.AA.visit(this, null);
|
TypeDenoter elemType = (TypeDenoter) ast.AA.visit(this, null);
|
||||||
IntegerLiteral il = new IntegerLiteral(Integer.valueOf(ast.AA.elemCount).toString(),
|
IntegerLiteral il = new IntegerLiteral(Integer.valueOf(ast.AA.elemCount).toString(), ast.position);
|
||||||
ast.position);
|
|
||||||
ast.type = new ArrayTypeDenoter(il, elemType, ast.position);
|
ast.type = new ArrayTypeDenoter(il, elemType, ast.position);
|
||||||
return ast.type;
|
return ast.type;
|
||||||
}
|
}
|
||||||
@ -185,20 +183,16 @@ public Object visitBinaryExpression(BinaryExpression ast, Object o) {
|
|||||||
reportUndeclared(ast.O);
|
reportUndeclared(ast.O);
|
||||||
else {
|
else {
|
||||||
if (!(binding instanceof BinaryOperatorDeclaration))
|
if (!(binding instanceof BinaryOperatorDeclaration))
|
||||||
reporter.reportError("\"%\" is not a binary operator",
|
reporter.reportError("\"%\" is not a binary operator", ast.O.spelling, ast.O.position);
|
||||||
ast.O.spelling, ast.O.position);
|
|
||||||
BinaryOperatorDeclaration bbinding = (BinaryOperatorDeclaration) binding;
|
BinaryOperatorDeclaration bbinding = (BinaryOperatorDeclaration) binding;
|
||||||
if (bbinding.ARG1 == StdEnvironment.anyType) {
|
if (bbinding.ARG1 == StdEnvironment.anyType) {
|
||||||
// this operator must be "=" or "\="
|
// this operator must be "=" or "\="
|
||||||
if (!e1Type.equals(e2Type))
|
if (!e1Type.equals(e2Type))
|
||||||
reporter.reportError("incompatible argument types for \"%\"",
|
reporter.reportError("incompatible argument types for \"%\"", ast.O.spelling, ast.position);
|
||||||
ast.O.spelling, ast.position);
|
|
||||||
} else if (!e1Type.equals(bbinding.ARG1))
|
} else if (!e1Type.equals(bbinding.ARG1))
|
||||||
reporter.reportError("wrong argument type for \"%\"",
|
reporter.reportError("wrong argument type for \"%\"", ast.O.spelling, ast.E1.position);
|
||||||
ast.O.spelling, ast.E1.position);
|
|
||||||
else if (!e2Type.equals(bbinding.ARG2))
|
else if (!e2Type.equals(bbinding.ARG2))
|
||||||
reporter.reportError("wrong argument type for \"%\"",
|
reporter.reportError("wrong argument type for \"%\"", ast.O.spelling, ast.E2.position);
|
||||||
ast.O.spelling, ast.E2.position);
|
|
||||||
ast.type = bbinding.RES;
|
ast.type = bbinding.RES;
|
||||||
}
|
}
|
||||||
return ast.type;
|
return ast.type;
|
||||||
@ -217,8 +211,7 @@ public Object visitCallExpression(CallExpression ast, Object o) {
|
|||||||
ast.APS.visit(this, ((FuncFormalParameter) binding).FPS);
|
ast.APS.visit(this, ((FuncFormalParameter) binding).FPS);
|
||||||
ast.type = ((FuncFormalParameter) binding).T;
|
ast.type = ((FuncFormalParameter) binding).T;
|
||||||
} else
|
} else
|
||||||
reporter.reportError("\"%\" is not a function identifier",
|
reporter.reportError("\"%\" is not a function identifier", ast.I.spelling, ast.I.position);
|
||||||
ast.I.spelling, ast.I.position);
|
|
||||||
return ast.type;
|
return ast.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -238,8 +231,7 @@ public Object visitEmptyExpression(EmptyExpression ast, Object o) {
|
|||||||
public Object visitIfExpression(IfExpression ast, Object o) {
|
public Object visitIfExpression(IfExpression ast, Object o) {
|
||||||
TypeDenoter e1Type = (TypeDenoter) ast.E1.visit(this, null);
|
TypeDenoter e1Type = (TypeDenoter) ast.E1.visit(this, null);
|
||||||
if (!e1Type.equals(StdEnvironment.booleanType))
|
if (!e1Type.equals(StdEnvironment.booleanType))
|
||||||
reporter.reportError("Boolean expression expected here", "",
|
reporter.reportError("Boolean expression expected here", "", ast.E1.position);
|
||||||
ast.E1.position);
|
|
||||||
TypeDenoter e2Type = (TypeDenoter) ast.E2.visit(this, null);
|
TypeDenoter e2Type = (TypeDenoter) ast.E2.visit(this, null);
|
||||||
TypeDenoter e3Type = (TypeDenoter) ast.E3.visit(this, null);
|
TypeDenoter e3Type = (TypeDenoter) ast.E3.visit(this, null);
|
||||||
if (!e2Type.equals(e3Type))
|
if (!e2Type.equals(e3Type))
|
||||||
@ -279,13 +271,11 @@ public Object visitUnaryExpression(UnaryExpression ast, Object o) {
|
|||||||
reportUndeclared(ast.O);
|
reportUndeclared(ast.O);
|
||||||
ast.type = StdEnvironment.errorType;
|
ast.type = StdEnvironment.errorType;
|
||||||
} else if (!(binding instanceof UnaryOperatorDeclaration))
|
} else if (!(binding instanceof UnaryOperatorDeclaration))
|
||||||
reporter.reportError("\"%\" is not a unary operator",
|
reporter.reportError("\"%\" is not a unary operator", ast.O.spelling, ast.O.position);
|
||||||
ast.O.spelling, ast.O.position);
|
|
||||||
else {
|
else {
|
||||||
UnaryOperatorDeclaration ubinding = (UnaryOperatorDeclaration) binding;
|
UnaryOperatorDeclaration ubinding = (UnaryOperatorDeclaration) binding;
|
||||||
if (!eType.equals(ubinding.ARG))
|
if (!eType.equals(ubinding.ARG))
|
||||||
reporter.reportError("wrong argument type for \"%\"",
|
reporter.reportError("wrong argument type for \"%\"", ast.O.spelling, ast.O.position);
|
||||||
ast.O.spelling, ast.O.position);
|
|
||||||
ast.type = ubinding.RES;
|
ast.type = ubinding.RES;
|
||||||
}
|
}
|
||||||
return ast.type;
|
return ast.type;
|
||||||
@ -310,8 +300,7 @@ public Object visitConstDeclaration(ConstDeclaration ast, Object o) {
|
|||||||
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
|
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
|
||||||
idTable.enter(ast.I.spelling, ast);
|
idTable.enter(ast.I.spelling, ast);
|
||||||
if (ast.duplicated)
|
if (ast.duplicated)
|
||||||
reporter.reportError("identifier \"%\" already declared",
|
reporter.reportError("identifier \"%\" already declared", ast.I.spelling, ast.position);
|
||||||
ast.I.spelling, ast.position);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,15 +309,13 @@ public Object visitFuncDeclaration(FuncDeclaration ast, Object o) {
|
|||||||
ast.T = (TypeDenoter) ast.T.visit(this, null);
|
ast.T = (TypeDenoter) ast.T.visit(this, null);
|
||||||
idTable.enter(ast.I.spelling, ast); // permits recursion
|
idTable.enter(ast.I.spelling, ast); // permits recursion
|
||||||
if (ast.duplicated)
|
if (ast.duplicated)
|
||||||
reporter.reportError("identifier \"%\" already declared",
|
reporter.reportError("identifier \"%\" already declared", ast.I.spelling, ast.position);
|
||||||
ast.I.spelling, ast.position);
|
|
||||||
idTable.openScope();
|
idTable.openScope();
|
||||||
ast.FPS.visit(this, null);
|
ast.FPS.visit(this, null);
|
||||||
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
|
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
|
||||||
idTable.closeScope();
|
idTable.closeScope();
|
||||||
if (!ast.T.equals(eType))
|
if (!ast.T.equals(eType))
|
||||||
reporter.reportError("body of function \"%\" has wrong type",
|
reporter.reportError("body of function \"%\" has wrong type", ast.I.spelling, ast.E.position);
|
||||||
ast.I.spelling, ast.E.position);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,8 +323,7 @@ public Object visitFuncDeclaration(FuncDeclaration ast, Object o) {
|
|||||||
public Object visitProcDeclaration(ProcDeclaration ast, Object o) {
|
public Object visitProcDeclaration(ProcDeclaration ast, Object o) {
|
||||||
idTable.enter(ast.I.spelling, ast); // permits recursion
|
idTable.enter(ast.I.spelling, ast); // permits recursion
|
||||||
if (ast.duplicated)
|
if (ast.duplicated)
|
||||||
reporter.reportError("identifier \"%\" already declared",
|
reporter.reportError("identifier \"%\" already declared", ast.I.spelling, ast.position);
|
||||||
ast.I.spelling, ast.position);
|
|
||||||
idTable.openScope();
|
idTable.openScope();
|
||||||
ast.FPS.visit(this, null);
|
ast.FPS.visit(this, null);
|
||||||
ast.C.visit(this, null);
|
ast.C.visit(this, null);
|
||||||
@ -357,8 +343,7 @@ public Object visitTypeDeclaration(TypeDeclaration ast, Object o) {
|
|||||||
ast.T = (TypeDenoter) ast.T.visit(this, null);
|
ast.T = (TypeDenoter) ast.T.visit(this, null);
|
||||||
idTable.enter(ast.I.spelling, ast);
|
idTable.enter(ast.I.spelling, ast);
|
||||||
if (ast.duplicated)
|
if (ast.duplicated)
|
||||||
reporter.reportError("identifier \"%\" already declared",
|
reporter.reportError("identifier \"%\" already declared", ast.I.spelling, ast.position);
|
||||||
ast.I.spelling, ast.position);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -372,8 +357,7 @@ public Object visitVarDeclaration(VarDeclaration ast, Object o) {
|
|||||||
ast.T = (TypeDenoter) ast.T.visit(this, null);
|
ast.T = (TypeDenoter) ast.T.visit(this, null);
|
||||||
idTable.enter(ast.I.spelling, ast);
|
idTable.enter(ast.I.spelling, ast);
|
||||||
if (ast.duplicated)
|
if (ast.duplicated)
|
||||||
reporter.reportError("identifier \"%\" already declared",
|
reporter.reportError("identifier \"%\" already declared", ast.I.spelling, ast.position);
|
||||||
ast.I.spelling, ast.position);
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -411,8 +395,7 @@ public Object visitMultipleRecordAggregate(MultipleRecordAggregate ast, Object o
|
|||||||
FieldTypeDenoter rType = (FieldTypeDenoter) ast.RA.visit(this, null);
|
FieldTypeDenoter rType = (FieldTypeDenoter) ast.RA.visit(this, null);
|
||||||
TypeDenoter fType = checkFieldIdentifier(rType, ast.I);
|
TypeDenoter fType = checkFieldIdentifier(rType, ast.I);
|
||||||
if (fType != StdEnvironment.errorType)
|
if (fType != StdEnvironment.errorType)
|
||||||
reporter.reportError("duplicate field \"%\" in record",
|
reporter.reportError("duplicate field \"%\" in record", ast.I.spelling, ast.I.position);
|
||||||
ast.I.spelling, ast.I.position);
|
|
||||||
ast.type = new MultipleFieldTypeDenoter(ast.I, eType, rType, ast.position);
|
ast.type = new MultipleFieldTypeDenoter(ast.I, eType, rType, ast.position);
|
||||||
return ast.type;
|
return ast.type;
|
||||||
}
|
}
|
||||||
@ -433,8 +416,7 @@ public Object visitConstFormalParameter(ConstFormalParameter ast, Object o) {
|
|||||||
ast.T = (TypeDenoter) ast.T.visit(this, null);
|
ast.T = (TypeDenoter) ast.T.visit(this, null);
|
||||||
idTable.enter(ast.I.spelling, ast);
|
idTable.enter(ast.I.spelling, ast);
|
||||||
if (ast.duplicated)
|
if (ast.duplicated)
|
||||||
reporter.reportError("duplicated formal parameter \"%\"",
|
reporter.reportError("duplicated formal parameter \"%\"", ast.I.spelling, ast.position);
|
||||||
ast.I.spelling, ast.position);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -446,8 +428,7 @@ public Object visitFuncFormalParameter(FuncFormalParameter ast, Object o) {
|
|||||||
ast.T = (TypeDenoter) ast.T.visit(this, null);
|
ast.T = (TypeDenoter) ast.T.visit(this, null);
|
||||||
idTable.enter(ast.I.spelling, ast);
|
idTable.enter(ast.I.spelling, ast);
|
||||||
if (ast.duplicated)
|
if (ast.duplicated)
|
||||||
reporter.reportError("duplicated formal parameter \"%\"",
|
reporter.reportError("duplicated formal parameter \"%\"", ast.I.spelling, ast.position);
|
||||||
ast.I.spelling, ast.position);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -458,8 +439,7 @@ public Object visitProcFormalParameter(ProcFormalParameter ast, Object o) {
|
|||||||
idTable.closeScope();
|
idTable.closeScope();
|
||||||
idTable.enter(ast.I.spelling, ast);
|
idTable.enter(ast.I.spelling, ast);
|
||||||
if (ast.duplicated)
|
if (ast.duplicated)
|
||||||
reporter.reportError("duplicated formal parameter \"%\"",
|
reporter.reportError("duplicated formal parameter \"%\"", ast.I.spelling, ast.position);
|
||||||
ast.I.spelling, ast.position);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -468,8 +448,7 @@ public Object visitVarFormalParameter(VarFormalParameter ast, Object o) {
|
|||||||
ast.T = (TypeDenoter) ast.T.visit(this, null);
|
ast.T = (TypeDenoter) ast.T.visit(this, null);
|
||||||
idTable.enter(ast.I.spelling, ast);
|
idTable.enter(ast.I.spelling, ast);
|
||||||
if (ast.duplicated)
|
if (ast.duplicated)
|
||||||
reporter.reportError("duplicated formal parameter \"%\"",
|
reporter.reportError("duplicated formal parameter \"%\"", ast.I.spelling, ast.position);
|
||||||
ast.I.spelling, ast.position);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -501,11 +480,9 @@ public Object visitConstActualParameter(ConstActualParameter ast, Object o) {
|
|||||||
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
|
TypeDenoter eType = (TypeDenoter) ast.E.visit(this, null);
|
||||||
|
|
||||||
if (!(fp instanceof ConstFormalParameter))
|
if (!(fp instanceof ConstFormalParameter))
|
||||||
reporter.reportError("const actual parameter not expected here", "",
|
reporter.reportError("const actual parameter not expected here", "", ast.position);
|
||||||
ast.position);
|
|
||||||
else if (!eType.equals(((ConstFormalParameter) fp).T))
|
else if (!eType.equals(((ConstFormalParameter) fp).T))
|
||||||
reporter.reportError("wrong type for const actual parameter", "",
|
reporter.reportError("wrong type for const actual parameter", "", ast.E.position);
|
||||||
ast.E.position);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -516,13 +493,10 @@ public Object visitFuncActualParameter(FuncActualParameter ast, Object o) {
|
|||||||
Declaration binding = (Declaration) ast.I.visit(this, null);
|
Declaration binding = (Declaration) ast.I.visit(this, null);
|
||||||
if (binding == null)
|
if (binding == null)
|
||||||
reportUndeclared(ast.I);
|
reportUndeclared(ast.I);
|
||||||
else if (!(binding instanceof FuncDeclaration ||
|
else if (!(binding instanceof FuncDeclaration || binding instanceof FuncFormalParameter))
|
||||||
binding instanceof FuncFormalParameter))
|
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);
|
|
||||||
else if (!(fp instanceof FuncFormalParameter))
|
else if (!(fp instanceof FuncFormalParameter))
|
||||||
reporter.reportError("func actual parameter not expected here", "",
|
reporter.reportError("func actual parameter not expected here", "", ast.position);
|
||||||
ast.position);
|
|
||||||
else {
|
else {
|
||||||
FormalParameterSequence FPS = null;
|
FormalParameterSequence FPS = null;
|
||||||
TypeDenoter T = null;
|
TypeDenoter T = null;
|
||||||
@ -534,11 +508,9 @@ public Object visitFuncActualParameter(FuncActualParameter ast, Object o) {
|
|||||||
T = ((FuncFormalParameter) binding).T;
|
T = ((FuncFormalParameter) binding).T;
|
||||||
}
|
}
|
||||||
if (!FPS.equals(((FuncFormalParameter) fp).FPS))
|
if (!FPS.equals(((FuncFormalParameter) fp).FPS))
|
||||||
reporter.reportError("wrong signature for function \"%\"",
|
reporter.reportError("wrong signature for function \"%\"", ast.I.spelling, ast.I.position);
|
||||||
ast.I.spelling, ast.I.position);
|
|
||||||
else if (!T.equals(((FuncFormalParameter) fp).T))
|
else if (!T.equals(((FuncFormalParameter) fp).T))
|
||||||
reporter.reportError("wrong type for function \"%\"",
|
reporter.reportError("wrong type for function \"%\"", ast.I.spelling, ast.I.position);
|
||||||
ast.I.spelling, ast.I.position);
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -550,13 +522,10 @@ public Object visitProcActualParameter(ProcActualParameter ast, Object o) {
|
|||||||
Declaration binding = (Declaration) ast.I.visit(this, null);
|
Declaration binding = (Declaration) ast.I.visit(this, null);
|
||||||
if (binding == null)
|
if (binding == null)
|
||||||
reportUndeclared(ast.I);
|
reportUndeclared(ast.I);
|
||||||
else if (!(binding instanceof ProcDeclaration ||
|
else if (!(binding instanceof ProcDeclaration || binding instanceof ProcFormalParameter))
|
||||||
binding instanceof ProcFormalParameter))
|
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);
|
|
||||||
else if (!(fp instanceof ProcFormalParameter))
|
else if (!(fp instanceof ProcFormalParameter))
|
||||||
reporter.reportError("proc actual parameter not expected here", "",
|
reporter.reportError("proc actual parameter not expected here", "", ast.position);
|
||||||
ast.position);
|
|
||||||
else {
|
else {
|
||||||
FormalParameterSequence FPS = null;
|
FormalParameterSequence FPS = null;
|
||||||
if (binding instanceof ProcDeclaration)
|
if (binding instanceof ProcDeclaration)
|
||||||
@ -564,8 +533,7 @@ public Object visitProcActualParameter(ProcActualParameter ast, Object o) {
|
|||||||
else
|
else
|
||||||
FPS = ((ProcFormalParameter) binding).FPS;
|
FPS = ((ProcFormalParameter) binding).FPS;
|
||||||
if (!FPS.equals(((ProcFormalParameter) fp).FPS))
|
if (!FPS.equals(((ProcFormalParameter) fp).FPS))
|
||||||
reporter.reportError("wrong signature for procedure \"%\"",
|
reporter.reportError("wrong signature for procedure \"%\"", ast.I.spelling, ast.I.position);
|
||||||
ast.I.spelling, ast.I.position);
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -576,14 +544,11 @@ public Object visitVarActualParameter(VarActualParameter ast, Object o) {
|
|||||||
|
|
||||||
TypeDenoter vType = (TypeDenoter) ast.V.visit(this, null);
|
TypeDenoter vType = (TypeDenoter) ast.V.visit(this, null);
|
||||||
if (!ast.V.variable)
|
if (!ast.V.variable)
|
||||||
reporter.reportError("actual parameter is not a variable", "",
|
reporter.reportError("actual parameter is not a variable", "", ast.V.position);
|
||||||
ast.V.position);
|
|
||||||
else if (!(fp instanceof VarFormalParameter))
|
else if (!(fp instanceof VarFormalParameter))
|
||||||
reporter.reportError("var actual parameter not expected here", "",
|
reporter.reportError("var actual parameter not expected here", "", ast.V.position);
|
||||||
ast.V.position);
|
|
||||||
else if (!vType.equals(((VarFormalParameter) fp).T))
|
else if (!vType.equals(((VarFormalParameter) fp).T))
|
||||||
reporter.reportError("wrong type for var actual parameter", "",
|
reporter.reportError("wrong type for var actual parameter", "", ast.V.position);
|
||||||
ast.V.position);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -658,8 +623,7 @@ public Object visitSimpleTypeDenoter(SimpleTypeDenoter ast, Object o) {
|
|||||||
reportUndeclared(ast.I);
|
reportUndeclared(ast.I);
|
||||||
return StdEnvironment.errorType;
|
return StdEnvironment.errorType;
|
||||||
} else if (!(binding instanceof TypeDeclaration)) {
|
} else if (!(binding instanceof TypeDeclaration)) {
|
||||||
reporter.reportError("\"%\" is not a type identifier",
|
reporter.reportError("\"%\" is not a type identifier", ast.I.spelling, ast.I.position);
|
||||||
ast.I.spelling, ast.I.position);
|
|
||||||
return StdEnvironment.errorType;
|
return StdEnvironment.errorType;
|
||||||
}
|
}
|
||||||
return ((TypeDeclaration) binding).T;
|
return ((TypeDeclaration) binding).T;
|
||||||
@ -747,8 +711,7 @@ public Object visitDotVname(DotVname ast, Object o) {
|
|||||||
else {
|
else {
|
||||||
ast.type = checkFieldIdentifier(((RecordTypeDenoter) vType).FT, ast.I);
|
ast.type = checkFieldIdentifier(((RecordTypeDenoter) vType).FT, ast.I);
|
||||||
if (ast.type == StdEnvironment.errorType)
|
if (ast.type == StdEnvironment.errorType)
|
||||||
reporter.reportError("no field \"%\" in this record type",
|
reporter.reportError("no field \"%\" in this record type", ast.I.spelling, ast.I.position);
|
||||||
ast.I.spelling, ast.I.position);
|
|
||||||
}
|
}
|
||||||
return ast.type;
|
return ast.type;
|
||||||
}
|
}
|
||||||
@ -773,8 +736,7 @@ public Object visitSimpleVname(SimpleVname ast, Object o) {
|
|||||||
ast.type = ((VarFormalParameter) binding).T;
|
ast.type = ((VarFormalParameter) binding).T;
|
||||||
ast.variable = true;
|
ast.variable = true;
|
||||||
} else
|
} else
|
||||||
reporter.reportError("\"%\" is not a const or var identifier",
|
reporter.reportError("\"%\" is not a const or var identifier", ast.I.spelling, ast.I.position);
|
||||||
ast.I.spelling, ast.I.position);
|
|
||||||
return ast.type;
|
return ast.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -788,8 +750,7 @@ public Object visitSubscriptVname(SubscriptVname ast, Object o) {
|
|||||||
reporter.reportError("array expected here", "", ast.V.position);
|
reporter.reportError("array expected here", "", ast.V.position);
|
||||||
else {
|
else {
|
||||||
if (!eType.equals(StdEnvironment.integerType))
|
if (!eType.equals(StdEnvironment.integerType))
|
||||||
reporter.reportError("Integer expression expected here", "",
|
reporter.reportError("Integer expression expected here", "", ast.E.position);
|
||||||
ast.E.position);
|
|
||||||
ast.type = ((ArrayTypeDenoter) vType).T;
|
ast.type = ((ArrayTypeDenoter) vType).T;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -890,8 +851,7 @@ public Object visitProgram(Program ast, Object o) {
|
|||||||
|
|
||||||
ProcDeclaration binding;
|
ProcDeclaration binding;
|
||||||
|
|
||||||
binding = new ProcDeclaration(new Identifier(id, dummyPos), fps,
|
binding = new ProcDeclaration(new Identifier(id, dummyPos), fps, new EmptyCommand(dummyPos), dummyPos);
|
||||||
new EmptyCommand(dummyPos), dummyPos);
|
|
||||||
idTable.enter(id, binding);
|
idTable.enter(id, binding);
|
||||||
return 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
|
// Creates a small AST to represent the "declaration" of a standard
|
||||||
// type, and enters it in the identification table.
|
// type, and enters it in the identification table.
|
||||||
|
|
||||||
private FuncDeclaration declareStdFunc(String id, FormalParameterSequence fps,
|
private FuncDeclaration declareStdFunc(String id, FormalParameterSequence fps, TypeDenoter resultType) {
|
||||||
TypeDenoter resultType) {
|
|
||||||
|
|
||||||
FuncDeclaration binding;
|
FuncDeclaration binding;
|
||||||
|
|
||||||
binding = new FuncDeclaration(new Identifier(id, dummyPos), fps, resultType,
|
binding = new FuncDeclaration(new Identifier(id, dummyPos), fps, resultType, new EmptyExpression(dummyPos),
|
||||||
new EmptyExpression(dummyPos), dummyPos);
|
dummyPos);
|
||||||
idTable.enter(id, binding);
|
idTable.enter(id, binding);
|
||||||
return binding;
|
return binding;
|
||||||
}
|
}
|
||||||
@ -918,8 +877,7 @@ public Object visitProgram(Program ast, Object o) {
|
|||||||
|
|
||||||
UnaryOperatorDeclaration binding;
|
UnaryOperatorDeclaration binding;
|
||||||
|
|
||||||
binding = new UnaryOperatorDeclaration(new Operator(op, dummyPos),
|
binding = new UnaryOperatorDeclaration(new Operator(op, dummyPos), argType, resultType, dummyPos);
|
||||||
argType, resultType, dummyPos);
|
|
||||||
idTable.enter(op, binding);
|
idTable.enter(op, binding);
|
||||||
return binding;
|
return binding;
|
||||||
}
|
}
|
||||||
@ -933,8 +891,7 @@ public Object visitProgram(Program ast, Object o) {
|
|||||||
|
|
||||||
BinaryOperatorDeclaration binding;
|
BinaryOperatorDeclaration binding;
|
||||||
|
|
||||||
binding = new BinaryOperatorDeclaration(new Operator(op, dummyPos),
|
binding = new BinaryOperatorDeclaration(new Operator(op, dummyPos), arg1Type, arg2type, resultType, dummyPos);
|
||||||
arg1Type, arg2type, resultType, dummyPos);
|
|
||||||
idTable.enter(op, binding);
|
idTable.enter(op, binding);
|
||||||
return binding;
|
return binding;
|
||||||
}
|
}
|
||||||
@ -986,10 +943,14 @@ public Object visitProgram(Program ast, Object o) {
|
|||||||
StdEnvironment.booleanType);
|
StdEnvironment.booleanType);
|
||||||
|
|
||||||
StdEnvironment.charDecl = declareStdType("Char", StdEnvironment.charType);
|
StdEnvironment.charDecl = declareStdType("Char", StdEnvironment.charType);
|
||||||
StdEnvironment.chrDecl = declareStdFunc("chr", new SingleFormalParameterSequence(
|
StdEnvironment.chrDecl = declareStdFunc("chr",
|
||||||
new ConstFormalParameter(dummyI, StdEnvironment.integerType, dummyPos), dummyPos), StdEnvironment.charType);
|
new SingleFormalParameterSequence(
|
||||||
StdEnvironment.ordDecl = declareStdFunc("ord", new SingleFormalParameterSequence(
|
new ConstFormalParameter(dummyI, StdEnvironment.integerType, dummyPos), dummyPos),
|
||||||
new ConstFormalParameter(dummyI, StdEnvironment.charType, dummyPos), dummyPos), StdEnvironment.integerType);
|
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.eofDecl = declareStdFunc("eof", new EmptyFormalParameterSequence(dummyPos),
|
||||||
StdEnvironment.booleanType);
|
StdEnvironment.booleanType);
|
||||||
StdEnvironment.eolDecl = declareStdFunc("eol", new EmptyFormalParameterSequence(dummyPos),
|
StdEnvironment.eolDecl = declareStdFunc("eol", new EmptyFormalParameterSequence(dummyPos),
|
||||||
|
@ -36,9 +36,8 @@ public final class StdEnvironment {
|
|||||||
|
|
||||||
public static UnaryOperatorDeclaration notDecl;
|
public static UnaryOperatorDeclaration notDecl;
|
||||||
|
|
||||||
public static BinaryOperatorDeclaration andDecl, orDecl,
|
public static BinaryOperatorDeclaration andDecl, orDecl, addDecl, subtractDecl, multiplyDecl, divideDecl,
|
||||||
addDecl, subtractDecl, multiplyDecl, divideDecl, moduloDecl,
|
moduloDecl, equalDecl, unequalDecl, lessDecl, notlessDecl, greaterDecl, notgreaterDecl;
|
||||||
equalDecl, unequalDecl, lessDecl, notlessDecl, greaterDecl, notgreaterDecl;
|
|
||||||
|
|
||||||
public static ProcDeclaration getDecl, putDecl, getintDecl, putintDecl, geteolDecl, puteolDecl;
|
public static ProcDeclaration getDecl, putDecl, getintDecl, putintDecl, geteolDecl, puteolDecl;
|
||||||
|
|
||||||
|
@ -153,8 +153,7 @@ public class Parser {
|
|||||||
Command cAST = parseCommand();
|
Command cAST = parseCommand();
|
||||||
programAST = new Program(cAST, previousTokenPosition);
|
programAST = new Program(cAST, previousTokenPosition);
|
||||||
if (currentToken.kind != Token.EOT) {
|
if (currentToken.kind != Token.EOT) {
|
||||||
syntacticError("\"%\" not expected after end of program",
|
syntacticError("\"%\" not expected after end of program", currentToken.spelling);
|
||||||
currentToken.spelling);
|
|
||||||
}
|
}
|
||||||
} catch (SyntaxError s) {
|
} catch (SyntaxError s) {
|
||||||
return null;
|
return null;
|
||||||
@ -342,8 +341,7 @@ public class Parser {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
syntacticError("\"%\" cannot start a command",
|
syntacticError("\"%\" cannot start a command", currentToken.spelling);
|
||||||
currentToken.spelling);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -405,8 +403,7 @@ public class Parser {
|
|||||||
while (currentToken.kind == Token.OPERATOR) {
|
while (currentToken.kind == Token.OPERATOR) {
|
||||||
Operator opAST = parseOperator();
|
Operator opAST = parseOperator();
|
||||||
Expression e2AST = parsePrimaryExpression();
|
Expression e2AST = parsePrimaryExpression();
|
||||||
expressionAST = new BinaryExpression(expressionAST, opAST, e2AST,
|
expressionAST = new BinaryExpression(expressionAST, opAST, e2AST, expressionPos);
|
||||||
expressionPos);
|
|
||||||
}
|
}
|
||||||
return expressionAST;
|
return expressionAST;
|
||||||
}
|
}
|
||||||
@ -483,8 +480,7 @@ public class Parser {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
syntacticError("\"%\" cannot start an expression",
|
syntacticError("\"%\" cannot start an expression", currentToken.spelling);
|
||||||
currentToken.spelling);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -550,8 +546,7 @@ public class Parser {
|
|||||||
vnamePos = identifierAST.position;
|
vnamePos = identifierAST.position;
|
||||||
Vname vAST = new SimpleVname(identifierAST, vnamePos);
|
Vname vAST = new SimpleVname(identifierAST, vnamePos);
|
||||||
|
|
||||||
while (currentToken.kind == Token.DOT ||
|
while (currentToken.kind == Token.DOT || currentToken.kind == Token.LBRACKET) {
|
||||||
currentToken.kind == Token.LBRACKET) {
|
|
||||||
|
|
||||||
if (currentToken.kind == Token.DOT) {
|
if (currentToken.kind == Token.DOT) {
|
||||||
acceptIt();
|
acceptIt();
|
||||||
@ -584,8 +579,7 @@ public class Parser {
|
|||||||
acceptIt();
|
acceptIt();
|
||||||
Declaration d2AST = parseSingleDeclaration();
|
Declaration d2AST = parseSingleDeclaration();
|
||||||
finish(declarationPos);
|
finish(declarationPos);
|
||||||
declarationAST = new SequentialDeclaration(declarationAST, d2AST,
|
declarationAST = new SequentialDeclaration(declarationAST, d2AST, declarationPos);
|
||||||
declarationPos);
|
|
||||||
}
|
}
|
||||||
return declarationAST;
|
return declarationAST;
|
||||||
}
|
}
|
||||||
@ -642,8 +636,7 @@ public class Parser {
|
|||||||
accept(Token.IS);
|
accept(Token.IS);
|
||||||
Expression eAST = parseExpression();
|
Expression eAST = parseExpression();
|
||||||
finish(declarationPos);
|
finish(declarationPos);
|
||||||
declarationAST = new FuncDeclaration(iAST, fpsAST, tAST, eAST,
|
declarationAST = new FuncDeclaration(iAST, fpsAST, tAST, eAST, declarationPos);
|
||||||
declarationPos);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -658,8 +651,7 @@ public class Parser {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
syntacticError("\"%\" cannot start a declaration",
|
syntacticError("\"%\" cannot start a declaration", currentToken.spelling);
|
||||||
currentToken.spelling);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -698,8 +690,7 @@ public class Parser {
|
|||||||
acceptIt();
|
acceptIt();
|
||||||
FormalParameterSequence fpsAST = parseProperFormalParameterSequence();
|
FormalParameterSequence fpsAST = parseProperFormalParameterSequence();
|
||||||
finish(formalsPos);
|
finish(formalsPos);
|
||||||
formalsAST = new MultipleFormalParameterSequence(fpAST, fpsAST,
|
formalsAST = new MultipleFormalParameterSequence(fpAST, fpsAST, formalsPos);
|
||||||
formalsPos);
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
finish(formalsPos);
|
finish(formalsPos);
|
||||||
@ -760,8 +751,7 @@ public class Parser {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
syntacticError("\"%\" cannot start a formal parameter",
|
syntacticError("\"%\" cannot start a formal parameter", currentToken.spelling);
|
||||||
currentToken.spelling);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -795,8 +785,7 @@ public class Parser {
|
|||||||
acceptIt();
|
acceptIt();
|
||||||
ActualParameterSequence apsAST = parseProperActualParameterSequence();
|
ActualParameterSequence apsAST = parseProperActualParameterSequence();
|
||||||
finish(actualsPos);
|
finish(actualsPos);
|
||||||
actualsAST = new MultipleActualParameterSequence(apAST, apsAST,
|
actualsAST = new MultipleActualParameterSequence(apAST, apsAST, actualsPos);
|
||||||
actualsPos);
|
|
||||||
} else {
|
} else {
|
||||||
finish(actualsPos);
|
finish(actualsPos);
|
||||||
actualsAST = new SingleActualParameterSequence(apAST, actualsPos);
|
actualsAST = new SingleActualParameterSequence(apAST, actualsPos);
|
||||||
@ -853,8 +842,7 @@ public class Parser {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
syntacticError("\"%\" cannot start an actual parameter",
|
syntacticError("\"%\" cannot start an actual parameter", currentToken.spelling);
|
||||||
currentToken.spelling);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -902,8 +890,7 @@ public class Parser {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
syntacticError("\"%\" cannot start a type denoter",
|
syntacticError("\"%\" cannot start a type denoter", currentToken.spelling);
|
||||||
currentToken.spelling);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -34,10 +34,8 @@ public final class Scanner {
|
|||||||
// isOperator returns true iff the given character is an operator character.
|
// isOperator returns true iff the given character is an operator character.
|
||||||
|
|
||||||
private boolean isOperator(char c) {
|
private boolean isOperator(char c) {
|
||||||
return (c == '+' || c == '-' || c == '*' || c == '/' ||
|
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '=' || c == '<' || c == '>' || c == '\\'
|
||||||
c == '=' || c == '<' || c == '>' || c == '\\' ||
|
|| c == '&' || c == '@' || c == '%' || c == '^' || c == '?');
|
||||||
c == '&' || c == '@' || c == '%' || c == '^' ||
|
|
||||||
c == '?');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@ -249,10 +247,7 @@ public final class Scanner {
|
|||||||
int kind;
|
int kind;
|
||||||
|
|
||||||
currentlyScanningToken = false;
|
currentlyScanningToken = false;
|
||||||
while (currentChar == '!'
|
while (currentChar == '!' || currentChar == ' ' || currentChar == '\n' || currentChar == '\r'
|
||||||
|| currentChar == ' '
|
|
||||||
|| currentChar == '\n'
|
|
||||||
|| currentChar == '\r'
|
|
||||||
|| currentChar == '\t')
|
|| currentChar == '\t')
|
||||||
scanSeparator();
|
scanSeparator();
|
||||||
|
|
||||||
|
@ -23,16 +23,18 @@ public class SourceFile {
|
|||||||
java.io.FileInputStream source;
|
java.io.FileInputStream source;
|
||||||
int currentLine;
|
int currentLine;
|
||||||
|
|
||||||
public SourceFile(String filename) {
|
public static SourceFile ofPath(String pathname) {
|
||||||
try {
|
try {
|
||||||
sourceFile = new java.io.File(filename);
|
return new SourceFile(pathname);
|
||||||
|
} catch (java.io.IOException s) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SourceFile(String pathname) throws java.io.FileNotFoundException {
|
||||||
|
sourceFile = new java.io.File(pathname);
|
||||||
source = new java.io.FileInputStream(sourceFile);
|
source = new java.io.FileInputStream(sourceFile);
|
||||||
currentLine = 1;
|
currentLine = 1;
|
||||||
} catch (java.io.IOException s) {
|
|
||||||
sourceFile = null;
|
|
||||||
source = null;
|
|
||||||
currentLine = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char getSource() {
|
char getSource() {
|
||||||
|
@ -52,8 +52,7 @@ final class Token extends Object {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Kind=" + kind + ", spelling=" + spelling +
|
return "Kind=" + kind + ", spelling=" + spelling + ", position=" + position;
|
||||||
", position=" + position;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Token classes...
|
// Token classes...
|
||||||
@ -61,89 +60,25 @@ public String toString() {
|
|||||||
public static final int
|
public static final int
|
||||||
|
|
||||||
// literals, identifiers, operators...
|
// literals, identifiers, operators...
|
||||||
INTLITERAL = 0,
|
INTLITERAL = 0, CHARLITERAL = 1, IDENTIFIER = 2, OPERATOR = 3,
|
||||||
CHARLITERAL = 1,
|
|
||||||
IDENTIFIER = 2,
|
|
||||||
OPERATOR = 3,
|
|
||||||
|
|
||||||
// reserved words - must be in alphabetical order...
|
// reserved words - must be in alphabetical order...
|
||||||
ARRAY = 4,
|
ARRAY = 4, BEGIN = 5, CONST = 6, DO = 7, ELSE = 8, END = 9, FUNC = 10, IF = 11, IN = 12, LET = 13, OF = 14,
|
||||||
BEGIN = 5,
|
PROC = 15, RECORD = 16, THEN = 17, TYPE = 18, VAR = 19, WHILE = 20,
|
||||||
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...
|
// punctuation...
|
||||||
DOT = 21,
|
DOT = 21, COLON = 22, SEMICOLON = 23, COMMA = 24, BECOMES = 25, IS = 26,
|
||||||
COLON = 22,
|
|
||||||
SEMICOLON = 23,
|
|
||||||
COMMA = 24,
|
|
||||||
BECOMES = 25,
|
|
||||||
IS = 26,
|
|
||||||
|
|
||||||
// brackets...
|
// brackets...
|
||||||
LPAREN = 27,
|
LPAREN = 27, RPAREN = 28, LBRACKET = 29, RBRACKET = 30, LCURLY = 31, RCURLY = 32,
|
||||||
RPAREN = 28,
|
|
||||||
LBRACKET = 29,
|
|
||||||
RBRACKET = 30,
|
|
||||||
LCURLY = 31,
|
|
||||||
RCURLY = 32,
|
|
||||||
|
|
||||||
// special tokens...
|
// special tokens...
|
||||||
EOT = 33,
|
EOT = 33, ERROR = 34;
|
||||||
ERROR = 34;
|
|
||||||
|
|
||||||
private static String[] tokenTable = new String[] {
|
private static String[] tokenTable = new String[] { "<int>", "<char>", "<identifier>", "<operator>", "array",
|
||||||
"<int>",
|
"begin", "const", "do", "else", "end", "func", "if", "in", "let", "of", "proc", "record", "then", "type",
|
||||||
"<char>",
|
"var", "while", ".", ":", ";", ",", ":=", "~", "(", ")", "[", "]", "{", "}", "", "<error>" };
|
||||||
"<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,
|
private final static int firstReservedWord = Token.ARRAY, lastReservedWord = Token.WHILE;
|
||||||
lastReservedWord = Token.WHILE;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -42,8 +42,7 @@ public DrawerFrame(JPanel panel) {
|
|||||||
// Image img = tk.getImage("icon.gif");
|
// Image img = tk.getImage("icon.gif");
|
||||||
// setIconImage(img);
|
// setIconImage(img);
|
||||||
|
|
||||||
addWindowListener(
|
addWindowListener(new WindowAdapter() {
|
||||||
new WindowAdapter() {
|
|
||||||
@Override
|
@Override
|
||||||
public void windowClosing(WindowEvent e) {
|
public void windowClosing(WindowEvent e) {
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
|
@ -53,8 +53,7 @@ public class DrawingTree {
|
|||||||
graphics.fillRect(pos.x, pos.y, width, height);
|
graphics.fillRect(pos.x, pos.y, width, height);
|
||||||
graphics.setColor(Color.black);
|
graphics.setColor(Color.black);
|
||||||
graphics.drawRect(pos.x, pos.y, width - 1, height - 1);
|
graphics.drawRect(pos.x, pos.y, width - 1, height - 1);
|
||||||
graphics.drawString(caption, pos.x + 2,
|
graphics.drawString(caption, pos.x + 2, pos.y + (height + FIXED_FONT_HEIGHT) / 2);
|
||||||
pos.y + (height + FIXED_FONT_HEIGHT) / 2);
|
|
||||||
|
|
||||||
if (children != null) {
|
if (children != null) {
|
||||||
for (DrawingTree child : children) {
|
for (DrawingTree child : children) {
|
||||||
@ -63,9 +62,7 @@ public class DrawingTree {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (parent != null) {
|
if (parent != null) {
|
||||||
graphics.drawLine(pos.x + width / 2, pos.y,
|
graphics.drawLine(pos.x + width / 2, pos.y, parent.pos.x + parent.width / 2, parent.pos.y + parent.height);
|
||||||
parent.pos.x + parent.width / 2,
|
|
||||||
parent.pos.y + parent.height);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,8 +404,7 @@ public Object visitSimpleVname(SimpleVname ast, Object obj) {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitSubscriptVname(SubscriptVname ast, Object obj) {
|
public Object visitSubscriptVname(SubscriptVname ast, Object obj) {
|
||||||
return layoutBinary("Sub.Vname",
|
return layoutBinary("Sub.Vname", ast.V, ast.E);
|
||||||
ast.V, ast.E);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Programs
|
// Programs
|
||||||
@ -446,8 +445,7 @@ public Object visitProgram(Program ast, Object obj) {
|
|||||||
return dt;
|
return dt;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DrawingTree layoutTernary(String name, AST child1, AST child2,
|
private DrawingTree layoutTernary(String name, AST child1, AST child2, AST child3) {
|
||||||
AST child3) {
|
|
||||||
DrawingTree dt = layoutCaption(name);
|
DrawingTree dt = layoutCaption(name);
|
||||||
DrawingTree d1 = (DrawingTree) child1.visit(this, null);
|
DrawingTree d1 = (DrawingTree) child1.visit(this, null);
|
||||||
DrawingTree d2 = (DrawingTree) child2.visit(this, null);
|
DrawingTree d2 = (DrawingTree) child2.visit(this, null);
|
||||||
@ -457,8 +455,7 @@ public Object visitProgram(Program ast, Object obj) {
|
|||||||
return dt;
|
return dt;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DrawingTree layoutQuaternary(String name, AST child1, AST child2,
|
private DrawingTree layoutQuaternary(String name, AST child1, AST child2, AST child3, AST child4) {
|
||||||
AST child3, AST child4) {
|
|
||||||
DrawingTree dt = layoutCaption(name);
|
DrawingTree dt = layoutCaption(name);
|
||||||
DrawingTree d1 = (DrawingTree) child1.visit(this, null);
|
DrawingTree d1 = (DrawingTree) child1.visit(this, null);
|
||||||
DrawingTree d2 = (DrawingTree) child2.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.y = y + dt.height;
|
||||||
dt.children[0].offset.x = x1;
|
dt.children[0].offset.x = x1;
|
||||||
dt.contour.upper_head = new Polyline(0, dt.height,
|
dt.contour.upper_head = new Polyline(0, dt.height, new Polyline(x1, y, dt.contour.upper_head));
|
||||||
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.lower_head = new Polyline(0, dt.height,
|
|
||||||
new Polyline(x2, y, dt.contour.lower_head));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int join(DrawingTree dt) {
|
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,
|
private Polyline bridge(Polyline line1, int x1, int y1, Polyline line2, int x2, int y2) {
|
||||||
Polyline line2, int x2, int y2) {
|
|
||||||
int dy, dx, s;
|
int dy, dx, s;
|
||||||
Polyline r;
|
Polyline r;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user