| @@ -0,0 +1,7 @@ | |||||
| a := 2; | |||||
| b := 4; | |||||
| r := 0; | |||||
| while (a) { | |||||
| r := r + b ; | |||||
| a := a - 1 | |||||
| } | |||||
| @@ -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 class Main { | ||||
| public static void main(String[] args) { | 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<String, Integer> valuation = interpreter.getValuation(); | |||||
| System.out.println(valuation); | |||||
| } catch (SyntaxException se) { | |||||
| System.err.println(se); | |||||
| } catch (InterpreterException ie) { | |||||
| System.err.println(ie); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -10,6 +10,7 @@ import program.Composition; | |||||
| import program.Loop; | import program.Loop; | ||||
| import program.Program; | import program.Program; | ||||
| import java.util.HashMap; | |||||
| import java.util.Map; | import java.util.Map; | ||||
| import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
| @@ -33,4 +34,21 @@ public class InterpreterTest { | |||||
| assertEquals(0, valuation.get("a").intValue()); | assertEquals(0, valuation.get("a").intValue()); | ||||
| assertEquals(4, valuation.get("b").intValue()); | assertEquals(4, valuation.get("b").intValue()); | ||||
| } | } | ||||
| @Test | |||||
| public void testSemWithValuation() { | |||||
| Map<String, Integer> 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()); | |||||
| } | |||||
| } | } | ||||
| @@ -4,11 +4,8 @@ import static org.junit.Assert.assertEquals; | |||||
| import org.junit.Test; | import org.junit.Test; | ||||
| import expression.*; | import expression.*; | ||||
| import parser.Parser.Operator; | |||||
| import program.*; | import program.*; | ||||
| import java.util.Optional; | |||||
| public class ParserTest { | public class ParserTest { | ||||
| final String loopCode = "while (a) { r := r + b ; a := a - 1 }"; | 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))))); | 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))))); | ||||