From f1edea938ff6e04e39dc642a35faeced91ca6d9f Mon Sep 17 00:00:00 2001 From: Malte Schmitz Date: Fri, 25 Nov 2016 16:13:36 +0100 Subject: [PATCH] Add main method --- mult.whl | 7 ++++++ src/Main.java | 42 +++++++++++++++++++++++++++++++++++ test/interpreter/InterpreterTest.java | 18 +++++++++++++++ test/parser/ParserTest.java | 3 --- 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 mult.whl diff --git a/mult.whl b/mult.whl new file mode 100644 index 0000000..b962dd9 --- /dev/null +++ b/mult.whl @@ -0,0 +1,7 @@ +a := 2; +b := 4; +r := 0; +while (a) { + r := r + b ; + a := a - 1 +} diff --git a/src/Main.java b/src/Main.java index 6054a64..4dfd0e5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,47 @@ +import interpreter.Interpreter; +import interpreter.InterpreterException; +import parser.Parser; +import parser.SyntaxException; +import program.Program; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Map; + public class Main { public static void main(String[] args) { + if (args.length == 0) { + System.err.println("No file given"); + } else if (args.length > 1) { + System.err.println("Too many arguments"); + } else { + try { + String code = readFile(args[0]); + run(code); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + private static String readFile(String filename) throws IOException { + byte[] encoded = Files.readAllBytes(Paths.get(filename)); + return new String(encoded, StandardCharsets.UTF_8); + } + private static void run(String code) { + try { + Parser parser = new Parser(code); + Program program = parser.parse(); + Interpreter interpreter = new Interpreter(program); + Map valuation = interpreter.getValuation(); + System.out.println(valuation); + } catch (SyntaxException se) { + System.err.println(se); + } catch (InterpreterException ie) { + System.err.println(ie); + } } } diff --git a/test/interpreter/InterpreterTest.java b/test/interpreter/InterpreterTest.java index 81de606..b297aba 100644 --- a/test/interpreter/InterpreterTest.java +++ b/test/interpreter/InterpreterTest.java @@ -10,6 +10,7 @@ import program.Composition; import program.Loop; import program.Program; +import java.util.HashMap; import java.util.Map; import static org.junit.Assert.assertEquals; @@ -33,4 +34,21 @@ public class InterpreterTest { assertEquals(0, valuation.get("a").intValue()); assertEquals(4, valuation.get("b").intValue()); } + + @Test + public void testSemWithValuation() { + Map valuation = new HashMap<>(); + valuation.put("a", 2); + valuation.put("b", 4); + valuation.put("r", 0); + Program body = new Composition( + new Assignment(new Identifier("r"), new Addition(new Identifier("r"), new Identifier("b"))), + new Assignment(new Identifier("a"), new Subtraction(new Identifier("a"), new Int(1)))); + Program loop = new Loop(new Identifier("a"), body); + Interpreter interpreter = new Interpreter(loop, valuation); + valuation = interpreter.getValuation(); + assertEquals(8, valuation.get("r").intValue()); + assertEquals(0, valuation.get("a").intValue()); + assertEquals(4, valuation.get("b").intValue()); + } } diff --git a/test/parser/ParserTest.java b/test/parser/ParserTest.java index 8daef57..cc219e6 100644 --- a/test/parser/ParserTest.java +++ b/test/parser/ParserTest.java @@ -4,11 +4,8 @@ import static org.junit.Assert.assertEquals; import org.junit.Test; import expression.*; -import parser.Parser.Operator; import program.*; -import java.util.Optional; - public class ParserTest { final String loopCode = "while (a) { r := r + b ; a := a - 1 }"; final Loop loop = new Loop(new Identifier("a"), new Composition(new Assignment(new Identifier("r"), new Addition(new Identifier("r"), new Identifier("b"))), new Assignment(new Identifier("a"), new Subtraction(new Identifier("a"), new Int(1)))));