diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/jcompiler/compiler/standard/StandardCompiler.java b/src/main/java/jcompiler/compiler/standard/StandardCompiler.java index 8a93830..3662927 100644 --- a/src/main/java/jcompiler/compiler/standard/StandardCompiler.java +++ b/src/main/java/jcompiler/compiler/standard/StandardCompiler.java @@ -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': @@ -36,26 +36,18 @@ public void compile(BufferedReader input, BufferedWriter output) throws MemoryOv } if (currentStatement.length() > 0) { compileStatement(currentStatement.toString()); - currentStatement = new StringBuilder(); } List 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); } @@ -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(" ?= ?"); diff --git a/src/main/java/jcompiler/compiler/standard/statements/IfStart.java b/src/main/java/jcompiler/compiler/standard/statements/IfStart.java index b9e8316..a7702f0 100644 --- a/src/main/java/jcompiler/compiler/standard/statements/IfStart.java +++ b/src/main/java/jcompiler/compiler/standard/statements/IfStart.java @@ -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 ]", ""); diff --git a/src/main/java/jcompiler/compiler/standard/statements/Print.java b/src/main/java/jcompiler/compiler/standard/statements/Print.java new file mode 100644 index 0000000..d2aa946 --- /dev/null +++ b/src/main/java/jcompiler/compiler/standard/statements/Print.java @@ -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(); + } +} diff --git a/src/main/java/jcompiler/compiler/standard/statements/PrintNumber.java b/src/main/java/jcompiler/compiler/standard/statements/PrintNumber.java deleted file mode 100644 index 37805c1..0000000 --- a/src/main/java/jcompiler/compiler/standard/statements/PrintNumber.java +++ /dev/null @@ -1,19 +0,0 @@ -package jcompiler.compiler.standard.statements; - -import jcompiler.Memory; -import jcompiler.compiler.Statement; -import jcompiler.compiler.JohnnyInstruction; -import jcompiler.compiler.standard.johnnyinstructions.Take; - -public class PrintNumber implements Statement { - - private final int numberToPrint; - - public PrintNumber(int numberToPrint) { - this.numberToPrint = numberToPrint; - } - - public JohnnyInstruction[] compile() { - return new JohnnyInstruction[]{new Take(Memory.addVariable(numberToPrint))}; - } -} diff --git a/src/main/java/jcompiler/compiler/standard/statements/PrintVariable.java b/src/main/java/jcompiler/compiler/standard/statements/PrintVariable.java deleted file mode 100644 index 2dbe5b6..0000000 --- a/src/main/java/jcompiler/compiler/standard/statements/PrintVariable.java +++ /dev/null @@ -1,23 +0,0 @@ -package jcompiler.compiler.standard.statements; - -import jcompiler.Memory; -import jcompiler.compiler.Statement; -import jcompiler.compiler.JohnnyInstruction; -import jcompiler.compiler.standard.johnnyinstructions.Take; - -public class PrintVariable implements Statement { - - private final String variableToPrint; - - public PrintVariable(String variableToPrint) { - this.variableToPrint = variableToPrint; - } - - public JohnnyInstruction[] compile() { - if(!Memory.variableExists(variableToPrint)) { - throw new IllegalStateException("Variable " + variableToPrint + " does not exist!"); - } - - return new JohnnyInstruction[]{new Take(Memory.resolveId(variableToPrint))}; - } -} diff --git a/src/main/java/jcompiler/compiler/standard/statements/VariableAssignment.java b/src/main/java/jcompiler/compiler/standard/statements/VariableAssignment.java index 9a0f800..5c8dbda 100644 --- a/src/main/java/jcompiler/compiler/standard/statements/VariableAssignment.java +++ b/src/main/java/jcompiler/compiler/standard/statements/VariableAssignment.java @@ -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; @@ -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!"); } @@ -48,59 +41,11 @@ public JohnnyInstruction[] compile() { Memory.addVariable(id, 0); } - boolean first = true; - - List instructionList = new ArrayList<>(); - - - String operators = value.replaceAll("[\\w\\d ]", ""); - - List toAdd = new ArrayList<>(); - List 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 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]); diff --git a/src/main/java/jcompiler/compiler/standard/statements/expressions/NumericExpression.java b/src/main/java/jcompiler/compiler/standard/statements/expressions/NumericExpression.java new file mode 100644 index 0000000..9b8c4fb --- /dev/null +++ b/src/main/java/jcompiler/compiler/standard/statements/expressions/NumericExpression.java @@ -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 instructionList = new ArrayList<>(); + + String[] splitValues = expression.split("([+\\- ])+"); + String operators = expression.replaceAll("[\\w\\d ]", ""); + + List toAdd = new ArrayList<>(); + List 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]); + } +} diff --git a/target/classes/jcompiler/compiler/standard/StandardCompiler.class b/target/classes/jcompiler/compiler/standard/StandardCompiler.class index 2fcc569..dfbc994 100644 Binary files a/target/classes/jcompiler/compiler/standard/StandardCompiler.class and b/target/classes/jcompiler/compiler/standard/StandardCompiler.class differ diff --git a/target/classes/jcompiler/compiler/standard/statements/PrintNumber.class b/target/classes/jcompiler/compiler/standard/statements/PrintNumber.class deleted file mode 100644 index ba0c703..0000000 Binary files a/target/classes/jcompiler/compiler/standard/statements/PrintNumber.class and /dev/null differ diff --git a/target/classes/jcompiler/compiler/standard/statements/PrintVariable.class b/target/classes/jcompiler/compiler/standard/statements/PrintVariable.class deleted file mode 100644 index 59db517..0000000 Binary files a/target/classes/jcompiler/compiler/standard/statements/PrintVariable.class and /dev/null differ diff --git a/target/classes/jcompiler/compiler/standard/statements/VariableAssignment.class b/target/classes/jcompiler/compiler/standard/statements/VariableAssignment.class index 689512b..5b3ac08 100644 Binary files a/target/classes/jcompiler/compiler/standard/statements/VariableAssignment.class and b/target/classes/jcompiler/compiler/standard/statements/VariableAssignment.class differ