|
| 1 | +package week3.machines; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.charset.StandardCharsets; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +public class MiniAktkMachines { |
| 14 | + |
| 15 | + public static List<String> tokenize(String input) { |
| 16 | + List<String> tokens = new ArrayList<>(); |
| 17 | + StringBuilder currentToken = new StringBuilder(); // Tühi sõne tähistab seisundit ¬TOK |
| 18 | + for (char c : input.toCharArray()) { |
| 19 | + if (c == '#') break; |
| 20 | + if (c == '+' || c == '-' || c == '=' || c == ' ') { // lisame siia '=' ja tühikud. |
| 21 | + if (!currentToken.isEmpty()) { |
| 22 | + tokens.add(currentToken.toString()); // push |
| 23 | + currentToken = new StringBuilder(); |
| 24 | + } |
| 25 | + if (c != ' ') tokens.add(Character.toString(c)); // echo (v.a. tühikud) |
| 26 | + } else { |
| 27 | + currentToken.append(c); // acc |
| 28 | + } |
| 29 | + } |
| 30 | + // Ja lõpuks EOF puhul ka push seisundis TOK: |
| 31 | + if (!currentToken.isEmpty()) tokens.add(currentToken.toString()); |
| 32 | + return tokens; |
| 33 | + } |
| 34 | + |
| 35 | + public static int compute(List<String> tokens, Map<Character, Integer> environment) { |
| 36 | + int sign = 1; |
| 37 | + int sum = 0; |
| 38 | + boolean wasValue = false; // was the previous token a value |
| 39 | + for (String token : tokens) { |
| 40 | + switch (token) { |
| 41 | + case "-": |
| 42 | + sign *= -1; |
| 43 | + // intentional fallthrough |
| 44 | + case "+": |
| 45 | + wasValue = false; |
| 46 | + break; |
| 47 | + default: |
| 48 | + if (wasValue) throw new RuntimeException("Expected operator!"); |
| 49 | + sum += sign * getValue(token, environment); |
| 50 | + sign = 1; |
| 51 | + wasValue = true; |
| 52 | + } |
| 53 | + } |
| 54 | + if (!wasValue) throw new RuntimeException("Malformed expression!"); |
| 55 | + return sum; |
| 56 | + } |
| 57 | + |
| 58 | + private static int getValue(String token, Map<Character, Integer> environment) { |
| 59 | + try { |
| 60 | + return Integer.parseInt(token); |
| 61 | + } catch (NumberFormatException e) { |
| 62 | + char variableName = token.charAt(0); |
| 63 | + return environment.get(variableName); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + static void main(String[] args) throws IOException { |
| 68 | + Map<Character, Integer> environment = new HashMap<>(); |
| 69 | + try (BufferedReader reader = Files.newBufferedReader(Paths.get(args[0]), StandardCharsets.UTF_8)) { |
| 70 | + for (String line; (line = reader.readLine()) != null; ) { |
| 71 | + if (!line.isEmpty() && line.charAt(0) == ' ') throw new RuntimeException("Begins with space!"); |
| 72 | + List<String> tokens = tokenize(line); |
| 73 | + if (tokens.isEmpty()) continue; |
| 74 | + String firstToken = tokens.get(0); |
| 75 | + if (firstToken.equals("print")) { |
| 76 | + System.out.println(compute(tokens.subList(1, tokens.size()), environment)); |
| 77 | + } else if (tokens.get(1).equals("=")) { |
| 78 | + if (firstToken.length() > 1) throw new RuntimeException("Too long var!"); |
| 79 | + char variableName = firstToken.charAt(0); |
| 80 | + if (!Character.isLetter(variableName)) throw new RuntimeException("Invalid var name."); |
| 81 | + environment.put(variableName, compute(tokens.subList(2, tokens.size()), environment)); |
| 82 | + } else { |
| 83 | + throw new RuntimeException("Unexpected operation!"); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments