Skip to content

Commit

Permalink
Make MGenc a little less mutable
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Aug 25, 2020
1 parent 16d08aa commit 7b2697d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
32 changes: 19 additions & 13 deletions src/som/compiler/MethodGenerationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@

public class MethodGenerationContext {

private ClassGenerationContext holderGenc;
private MethodGenerationContext outerGenc;
private boolean blockMethod;
private final ClassGenerationContext holderGenc;
private final MethodGenerationContext outerGenc;
private final boolean blockMethod;

private SSymbol signature;
private final List<String> arguments = new ArrayList<String>();
private boolean primitive;
Expand All @@ -66,8 +67,21 @@ public class MethodGenerationContext {
private boolean finished;
private final ArrayList<Byte> bytecode = new ArrayList<>();

public void setHolder(final ClassGenerationContext cgenc) {
holderGenc = cgenc;
/**
* Constructor used for block methods.
*/
public MethodGenerationContext(final ClassGenerationContext holderGenc,
final MethodGenerationContext outerGenc) {
this.holderGenc = holderGenc;
this.outerGenc = outerGenc;
blockMethod = outerGenc != null;
}

/**
* Constructor used for normal methods.
*/
public MethodGenerationContext(final ClassGenerationContext holderGenc) {
this(holderGenc, null);
}

public void addArgument(final String arg) {
Expand Down Expand Up @@ -229,18 +243,10 @@ public boolean addLiteralIfAbsent(final SAbstractObject lit) {
return true;
}

public void setIsBlockMethod(final boolean isBlock) {
blockMethod = isBlock;
}

public ClassGenerationContext getHolder() {
return holderGenc;
}

public void setOuter(final MethodGenerationContext mgenc) {
outerGenc = mgenc;
}

public byte addLiteral(final SAbstractObject lit) {
int i = literals.size();
assert i < 128;
Expand Down
12 changes: 3 additions & 9 deletions src/som/compiler/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ public void classdef(final ClassGenerationContext cgenc) throws ProgramDefinitio
instanceFields(cgenc);
while (sym == Identifier || sym == Keyword || sym == OperatorSequence
|| symIn(binaryOpSyms)) {
MethodGenerationContext mgenc = new MethodGenerationContext();
mgenc.setHolder(cgenc);
MethodGenerationContext mgenc = new MethodGenerationContext(cgenc);
mgenc.addArgument("self");

method(mgenc);
Expand All @@ -220,8 +219,7 @@ public void classdef(final ClassGenerationContext cgenc) throws ProgramDefinitio
classFields(cgenc);
while (sym == Identifier || sym == Keyword || sym == OperatorSequence
|| symIn(binaryOpSyms)) {
MethodGenerationContext mgenc = new MethodGenerationContext();
mgenc.setHolder(cgenc);
MethodGenerationContext mgenc = new MethodGenerationContext(cgenc);
mgenc.addArgument("self");

method(mgenc);
Expand Down Expand Up @@ -561,11 +559,7 @@ private boolean primary(final MethodGenerationContext mgenc)
nestedTerm(mgenc);
break;
case NewBlock: {
MethodGenerationContext bgenc = new MethodGenerationContext();
bgenc.setIsBlockMethod(true);
bgenc.setHolder(mgenc.getHolder());
bgenc.setOuter(mgenc);

MethodGenerationContext bgenc = new MethodGenerationContext(mgenc.getHolder(), mgenc);
nestedBlock(bgenc);

SMethod blockMethod = bgenc.assemble(universe);
Expand Down

0 comments on commit 7b2697d

Please sign in to comment.