Browse Source

Add main method

pull/1/head
Malte Schmitz 9 years ago
parent
commit
f1edea938f
4 changed files with 67 additions and 3 deletions
  1. +7
    -0
      mult.whl
  2. +42
    -0
      src/Main.java
  3. +18
    -0
      test/interpreter/InterpreterTest.java
  4. +0
    -3
      test/parser/ParserTest.java

+ 7
- 0
mult.whl View File

@@ -0,0 +1,7 @@
a := 2;
b := 4;
r := 0;
while (a) {
r := r + b ;
a := a - 1
}

+ 42
- 0
src/Main.java View File

@@ -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<String, Integer> valuation = interpreter.getValuation();
System.out.println(valuation);
} catch (SyntaxException se) {
System.err.println(se);
} catch (InterpreterException ie) {
System.err.println(ie);
}
}
}

+ 18
- 0
test/interpreter/InterpreterTest.java View File

@@ -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<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());
}
}

+ 0
- 3
test/parser/ParserTest.java View File

@@ -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)))));


Loading…
Cancel
Save