Skip to content

Commit

Permalink
Loop variable 'it' tracks the repetition count in the scope closing #5,…
Browse files Browse the repository at this point in the history
… also closes #4
  • Loading branch information
SpencerPark committed Nov 30, 2016
1 parent 188fc30 commit 53a30a8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/main/antlr/MellowDParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ value
//Each mapping is put into the compiler's symbol table. Adding a `*` after the assignment token
//parses the value as if it was inside a percussion block.
varDeclaration
: IDENTIFIER ASSIGNMENT STAR? value
: identifier ASSIGNMENT STAR? value
;

//Dynamics are what change the velocity of a note. Mellow D supports the main dynamic identifiers
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/cas/cs4tb3/mellowd/intermediate/RepeatedPhrase.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import cas.cs4tb3.mellowd.intermediate.Output;
import cas.cs4tb3.mellowd.intermediate.executable.expressions.Expression;
import cas.cs4tb3.mellowd.intermediate.variables.Memory;
import cas.cs4tb3.mellowd.parser.ExecutionEnvironment;

public class RepeatedCodeBlock extends CodeBlock {
public static final String IMPLICIT_LOOP_COUNTER_ID = "it";

protected final Expression<Number> repetitions;

public RepeatedCodeBlock(Expression<Number> repetitions) {
Expand All @@ -15,10 +18,16 @@ public RepeatedCodeBlock(Expression<Number> repetitions) {
@Override
public void execute(ExecutionEnvironment environment, Output output) {
int repetitions = this.repetitions.evaluate(environment).intValue();
Memory memory = environment.getMemory();
Object old = memory.get(IMPLICIT_LOOP_COUNTER_ID);

for (int i = 0; i < repetitions; i++) {
memory.set(IMPLICIT_LOOP_COUNTER_ID, i);
for (Statement stmt : super.statements) {
stmt.execute(environment, output);
}
}

memory.set(IMPLICIT_LOOP_COUNTER_ID, old);
}
}
6 changes: 4 additions & 2 deletions src/main/java/cas/cs4tb3/mellowd/parser/MellowDCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public Integer visitNumber(MellowDParser.NumberContext ctx) {
@Override
public Expression<Integer> visitNumberOrId(MellowDParser.NumberOrIdContext ctx) {
MellowDParser.IdentifierContext idCtx = ctx.identifier();
if (idCtx != null) return lookupIdentifier(idCtx, Integer.class);
if (idCtx != null) return new RuntimeNullCheck<>(idCtx.getText(), lookupIdentifier(idCtx, Integer.class), idCtx);
return new Constant<>(visitNumber(ctx.number()));
}

Expand Down Expand Up @@ -416,9 +416,11 @@ public Expression<?> visitValue(MellowDParser.ValueContext ctx) {
public Statement visitVarDeclaration(MellowDParser.VarDeclarationContext ctx, boolean isField) {
boolean percussionToggle = ctx.STAR() != null;

Identifier identifier = visitIdentifier(ctx.identifier());

Expression<?> valueExpr = visitValue(ctx.value());

return new AssignmentStatement(new String[0], ctx.IDENTIFIER().getText(), valueExpr, isField, percussionToggle);
return new AssignmentStatement(identifier.qualifier, identifier.name, valueExpr, isField, percussionToggle);
}

@Override
Expand Down

0 comments on commit 53a30a8

Please sign in to comment.