Merge branch 'main' into java11

java11
Sandy Brownlee 2 years ago
commit 3de9d5e0cc
  1. 6
      Triangle.Compiler/src/main/java/triangle/codeGenerator/Encoder.java
  2. 7
      Triangle.Compiler/src/main/java/triangle/contextualAnalyzer/Checker.java
  3. 10
      Triangle.Compiler/src/main/java/triangle/contextualAnalyzer/IdentificationTable.java
  4. 7
      Triangle.Compiler/src/main/java/triangle/treeDrawer/Drawer.java
  5. 3
      Triangle.Compiler/src/main/java/triangle/treeDrawer/Polyline.java
  6. 5
      programs/assignments.tri
  7. 9
      programs/ifdemo.tri
  8. 10
      programs/simpleadding.tri

@ -545,7 +545,7 @@ public final class Encoder implements ActualParameterVisitor<Frame, Integer>,
public Integer visitRecordTypeDenoter(RecordTypeDenoter ast, Frame frame) {
int typeSize;
if (ast.entity == null) {
typeSize = ast.FT.visit(this, null);
typeSize = ast.FT.visit(this, frame);
ast.entity = new TypeRepresentation(typeSize);
writeTableDetails(ast);
} else {
@ -556,6 +556,10 @@ public final class Encoder implements ActualParameterVisitor<Frame, Integer>,
@Override
public Integer visitMultipleFieldTypeDenoter(MultipleFieldTypeDenoter ast, Frame frame) {
if (frame == null) { // in this case, we're just using the frame to wrap up the size
frame = Frame.Initial;
}
var offset = frame.getSize();
int fieldSize;
if (ast.entity == null) {

@ -206,7 +206,7 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
if (binding instanceof BinaryOperatorDeclaration) {
BinaryOperatorDeclaration bbinding = (BinaryOperatorDeclaration)binding;
if (bbinding.ARG1.equals(StdEnvironment.anyType)) {
if (bbinding.ARG1 == StdEnvironment.anyType) {
// this operator must be "=" or "\="
checkAndReportError(e1Type.equals(e2Type), "incompatible argument types for \"%\"", ast.O, ast);
} else {
@ -691,10 +691,11 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
public TypeDenoter visitDotVname(DotVname ast, Void arg) {
ast.type = null;
var vType = ast.V.visit(this);
ast.variable = ast.V.variable;
if (vType instanceof RecordTypeDenoter) {
RecordTypeDenoter record = (RecordTypeDenoter)vType;
ast.type = checkFieldIdentifier(record.FT, ast.I);
checkAndReportError(!ast.type.equals(StdEnvironment.errorType), "no field \"%\" in this record type",
checkAndReportError(ast.type != StdEnvironment.errorType, "no field \"%\" in this record type",
ast.I);
} else {
reportError("record expected here", ast.V);
@ -710,9 +711,11 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
var binding = ast.I.visit(this);
if (binding instanceof ConstantDeclaration) {
ConstantDeclaration constant = (ConstantDeclaration)binding;
ast.variable = false;
return ast.type = constant.getType();
} else if (binding instanceof VariableDeclaration) {
VariableDeclaration variable = (VariableDeclaration)binding;
ast.variable = true;
return ast.type = variable.getType();
}

@ -53,7 +53,7 @@ public final class IdentificationTable {
// same identifier at the current level.
public void enter(String id, Declaration attr) {
attr.duplicated = retrieve(id) != null;
attr.duplicated = retrieve(id, true) != null;
this.latest = new IdEntry(id, attr, this.level, this.latest);
}
@ -64,9 +64,15 @@ public final class IdentificationTable {
// otherwise returns the attribute field of the entry found.
public Declaration retrieve(String id) {
return retrieve(id, false);
}
// thisLevelOnly limits the search to only the current level
public Declaration retrieve(String id, boolean thisLevelOnly) {
var entry = this.latest;
while (true) {
if (entry == null) {
if (entry == null || (thisLevelOnly && entry.level < this.level)) {
break;
} else if (entry.id.equals(id)) {
return entry.attr;

@ -42,6 +42,13 @@ public class Drawer {
FontMetrics fontMetrics = frame.getFontMetrics(font);
// another class of visitor is used for drawing the tree: LayoutVisitor
// LayoutVisitor is passed to the AST which, in turn, calls visitProgram
// and then each AST node is visited. This ultimately constructs a
// DrawingTree, which is structurally the same as the AST but is decorated
// with coordinates (and has only DrawingTree objects as nodes)
// Each DrawingTree object knows how to paint itself, so it's passed to a
// DrawerPanel and DrawerFrame for display
LayoutVisitor layout = new LayoutVisitor(fontMetrics);
theDrawing = (DrawingTree) theAST.visit(layout, null);
theDrawing.position(new Point(2048, 10));

@ -14,6 +14,9 @@
package triangle.treeDrawer;
/**
* used to keep track of the position for components in the tree to be drawn
*/
class Polyline {
int dx, dy;
Polyline link;

@ -1,7 +1,8 @@
let
var n : Integer;
var c : Char
var n : Integer;
var c : Char
in
begin
c := '&';
n := n + 1

@ -0,0 +1,9 @@
let
var a : Integer;
var n : Integer
in
begin
if a < 0
then n := 0
else n := 1
end

@ -0,0 +1,10 @@
let
var a : Integer;
var b : Integer;
var c : Integer
in
begin
a := 1; b := 2; c := a / b;
end