This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d8f1bf
commit 4d6407f
Showing
12 changed files
with
117 additions
and
123 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/jcompiler/compiler/standard/statements/Print.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package jcompiler.compiler.standard.statements; | ||
|
||
import jcompiler.compiler.JohnnyInstruction; | ||
import jcompiler.compiler.Statement; | ||
import jcompiler.compiler.standard.statements.expressions.NumericExpression; | ||
|
||
public class Print implements Statement { | ||
|
||
private final String expressionToPrint; | ||
|
||
public Print(String expressionToPrint) { | ||
this.expressionToPrint = expressionToPrint; | ||
} | ||
|
||
public JohnnyInstruction[] compile() { | ||
return new NumericExpression(expressionToPrint).compile(); | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
src/main/java/jcompiler/compiler/standard/statements/PrintNumber.java
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
src/main/java/jcompiler/compiler/standard/statements/PrintVariable.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/jcompiler/compiler/standard/statements/expressions/NumericExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package jcompiler.compiler.standard.statements.expressions; | ||
|
||
import jcompiler.Memory; | ||
import jcompiler.compiler.JohnnyInstruction; | ||
import jcompiler.compiler.Statement; | ||
import jcompiler.compiler.standard.johnnyinstructions.Add; | ||
import jcompiler.compiler.standard.johnnyinstructions.Sub; | ||
import jcompiler.compiler.standard.johnnyinstructions.Take; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class NumericExpression implements Statement { | ||
|
||
public static final String VAR_REGEX = "[a-zA-Z]+"; | ||
public static final String NUM_REGEX = "[0-9]+"; | ||
|
||
private final String expression; | ||
|
||
public NumericExpression(String expression) { | ||
this.expression = expression; | ||
} | ||
|
||
@Override | ||
public JohnnyInstruction[] compile() { | ||
boolean first = true; | ||
|
||
List<JohnnyInstruction> instructionList = new ArrayList<>(); | ||
|
||
String[] splitValues = expression.split("([+\\- ])+"); | ||
String operators = expression.replaceAll("[\\w\\d ]", ""); | ||
|
||
List<String> toAdd = new ArrayList<>(); | ||
List<String> toSub = new ArrayList<>(); | ||
for (int i = 0; i < splitValues.length; i++) { | ||
String s = splitValues[i]; | ||
boolean add; | ||
if (first) { | ||
add = true; | ||
first = false; | ||
} else { | ||
add = operators.charAt(i - 1) == '+'; | ||
} | ||
if (add) { | ||
toAdd.add(s); | ||
} else { | ||
toSub.add(s); | ||
} | ||
} | ||
first = true; | ||
for (String s : toAdd) { | ||
if (s.matches(VAR_REGEX)) { | ||
if (!Memory.variableExists(s)) { | ||
throw new IllegalStateException("Variable " + s + " does not exist!"); | ||
} | ||
instructionList.add(first ? new Take(Memory.resolveId(s)) : new Add(Memory.resolveId(s))); | ||
} else if (s.matches(NUM_REGEX)) { | ||
instructionList.add(first ? new Take(Memory.addVariable(Integer.parseInt(s))) : new Add(Memory.addVariable(Integer.parseInt(s)))); | ||
} else { | ||
throw new IllegalArgumentException("Invalid numeric expression syntax: " + expression + " at " + s); | ||
} | ||
if (first) { | ||
first = false; | ||
} | ||
} | ||
for (String s : toSub) { | ||
if (s.matches(VAR_REGEX)) { | ||
if (!Memory.variableExists(s)) { | ||
throw new IllegalStateException("Variable " + s + " does not exist!"); | ||
} | ||
|
||
instructionList.add(new Sub(Memory.resolveId(s))); | ||
} else if (s.matches(NUM_REGEX)) { | ||
instructionList.add(new Sub(Memory.addVariable(Integer.parseInt(s)))); | ||
} else { | ||
throw new IllegalArgumentException("Invalid numeric expression syntax: " + expression + " at " + s); | ||
} | ||
} | ||
return instructionList.toArray(new JohnnyInstruction[0]); | ||
} | ||
} |
Binary file modified
BIN
-187 Bytes
(97%)
target/classes/jcompiler/compiler/standard/StandardCompiler.class
Binary file not shown.
Binary file removed
BIN
-734 Bytes
target/classes/jcompiler/compiler/standard/statements/PrintNumber.class
Binary file not shown.
Binary file removed
BIN
-1.13 KB
target/classes/jcompiler/compiler/standard/statements/PrintVariable.class
Binary file not shown.
Binary file modified
BIN
-1.39 KB
(66%)
target/classes/jcompiler/compiler/standard/statements/VariableAssignment.class
Binary file not shown.