Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
NumericExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
florianhartung committed Nov 28, 2020
1 parent 3d8f1bf commit 4d6407f
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 123 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 4 additions & 17 deletions src/main/java/jcompiler/compiler/standard/StandardCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class StandardCompiler extends Compiler {
public void compile(BufferedReader input, BufferedWriter output) throws MemoryOverflowException, IOException {

StringBuilder currentStatement = new StringBuilder();
int buffer = -1;
int buffer;
while ((buffer = input.read()) != -1) {
switch ((char) buffer) {
case '\n':
Expand All @@ -36,26 +36,18 @@ public void compile(BufferedReader input, BufferedWriter output) throws MemoryOv
}
if (currentStatement.length() > 0) {
compileStatement(currentStatement.toString());
currentStatement = new StringBuilder();
}

List<JohnnyInstruction> johnnyInstructions = new ArrayList<>();
for (int i = 0; i < statements.size(); i++) {
Statement statement = statements.get(i);

for (Statement statement : statements) {
if (statement instanceof IfEnd) {
System.out.println(".");
int found = 0;
IfState.Operation operation = IfState.endIfStructure();
System.out.println("operation = " + operation);
System.out.println("johnnyInstructions.size() = " + johnnyInstructions.size());
for (int j = johnnyInstructions.size() - 1; ; j--) {
if (johnnyInstructions.get(j) instanceof Jump && ((Jump) johnnyInstructions.get(j)).getJumpToAdr() == -1) {
if (found == 1) {
if (operation == IfState.Operation.GT || operation == IfState.Operation.LT) {
System.out.println("j = " + j);
((Jump) johnnyInstructions.get(j)).setJumpToAdr(j + 2);
System.out.println("new: " + (j + 2));
} else {
((Jump) johnnyInstructions.get(j)).setJumpToAdr(j + 5);
}
Expand Down Expand Up @@ -91,15 +83,10 @@ public void compile(BufferedReader input, BufferedWriter output) throws MemoryOv

private void compileStatement(String statement) {
if (statement.toLowerCase().startsWith("print")) {
String valueToPrint = statement.split(" ")[1];
String valueToPrint = statement.substring(6);

if (valueToPrint.matches("\\d+")) {
statements.add(new PrintNumber(Integer.parseInt(valueToPrint)));
} else {
statements.add(new PrintVariable(valueToPrint));
}
statements.add(new Print(valueToPrint));
} else if (statement.equalsIgnoreCase("exit")) {
System.out.println();
statements.add(new Exit());
} else if (statement.matches("\\w+ ?=.*")) {
String[] splitStatement = statement.split(" ?= ?");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public IfStart(String condition) {//y>x means y sub x then non null, one step (b

@Override
public JohnnyInstruction[] compile() {
System.out.println(1253);
String[] splitCondition = condition.split(" ?(<|>|<=|>=|==) ?");
String operation = condition.replaceAll("[\\w\\d ]", "");

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/jcompiler/compiler/standard/statements/Print.java
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();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@
import jcompiler.Memory;
import jcompiler.compiler.Statement;
import jcompiler.compiler.JohnnyInstruction;
import jcompiler.compiler.standard.johnnyinstructions.Add;
import jcompiler.compiler.standard.johnnyinstructions.Save;
import jcompiler.compiler.standard.johnnyinstructions.Sub;
import jcompiler.compiler.standard.johnnyinstructions.Take;
import jcompiler.compiler.standard.statements.expressions.NumericExpression;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class VariableAssignment implements Statement {

public static final String VAR_REGEX = "[a-zA-Z]+";
public static final String NUM_REGEX = "[0-9]+";
public static final String VALUE_REGEX = "(" + NUM_REGEX + "|" + VAR_REGEX + ")";
public static final String OPERATOR_REGEX = "([+|\\-])";


private final String id;
private final String value;

Expand All @@ -30,12 +24,11 @@ public VariableAssignment(String id, String value) {

public JohnnyInstruction[] compile() {
String[] splitValues = value.split("([+\\- ])+");
System.out.println("splitValues = " + splitValues.length);
if (splitValues.length == 1) {
if (splitValues[0].matches(NUM_REGEX)) {
if (splitValues[0].matches("[0-9]+")) {
Memory.addVariable(id, Integer.parseInt(splitValues[0]));
return new JohnnyInstruction[0];
} else if (splitValues[0].matches(VAR_REGEX)) {
} else if (splitValues[0].matches("[a-zA-Z]+")) {
if (!Memory.variableExists(splitValues[0])) {
throw new IllegalStateException("Variable " + splitValues[0] + " does not exist!");
}
Expand All @@ -48,59 +41,11 @@ public JohnnyInstruction[] compile() {
Memory.addVariable(id, 0);
}

boolean first = true;

List<JohnnyInstruction> instructionList = new ArrayList<>();


String operators = value.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 syntax: " + id + " = " + value + " 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!");
}
//move value of expression to db
JohnnyInstruction[] expression = new NumericExpression(value).compile();
List<JohnnyInstruction> instructionList = new ArrayList<>(Arrays.asList(expression));

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 syntax: " + id + " = " + value + " at " + s);
}
}
//save to (new) identifier
instructionList.add(new Save(Memory.resolveId(id)));

return instructionList.toArray(new JohnnyInstruction[0]);
Expand Down
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 target/classes/jcompiler/compiler/standard/StandardCompiler.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 4d6407f

Please sign in to comment.