Advertisement
stirante

SimpleScript

Feb 3rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.92 KB | None | 0 0
  1. package com.stirante.Everything.scripter;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Scanner;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. public class MainTest {
  9.  
  10.     public static final boolean DEBUG = false;
  11.  
  12.     public static final String TEST_CODE = "string zmienna = \"wartosc stringa\";\n" +
  13.             "writeString(\"Wpisz wejscie: \");\n" +
  14.             "string wejscie = readString();\n" +
  15.             "writeString(wejscie);\n" +
  16.             "writeString(\"Zmienne w pamieci: \");\n" +
  17.             "printVars();";
  18.  
  19.     private static final Pattern VARIABLE = Pattern.compile("([a-zA-Z0-9]+) *([a-zA-Z0-9]+);");
  20.     private static final Pattern ASSIGN = Pattern.compile("([a-zA-Z0-9]+) *([a-zA-Z0-9]+) *= *(.+);");
  21.     private static final Pattern REASSIGN = Pattern.compile("([a-zA-Z0-9]+) *= *(.+);");
  22.     private static final Pattern EXECUTE = Pattern.compile("([a-zA-Z0-9]+)\\((.*)\\);?");
  23.     private static final Pattern STRING = Pattern.compile("\"(.+)\"");
  24.     private static final Pattern INTEGER = Pattern.compile("([0-9]+)");
  25.     private static final Pattern DOUBLE = Pattern.compile("([0-9]+(?:\\.[0-9]+)?)");
  26.     private static HashMap<String, Variable> variables;
  27.     private static HashMap<String, Function> functions;
  28.  
  29.     private static void d(Object msg) {
  30.         if (DEBUG) System.out.println(msg);
  31.     }
  32.  
  33.     public static void main(String[] args) {
  34.         variables = new HashMap<>();
  35.         functions = new HashMap<>();
  36.         registerFunctions();
  37.         runScript(TEST_CODE);
  38.     }
  39.  
  40.     public static void runScript(String script) {
  41.         String[] lines = script.split("\n");
  42.         for (String line : lines) {
  43.             evaluate(line);
  44.         }
  45.     }
  46.  
  47.     private static void registerFunctions() {
  48.         functions.put("readString", new Function(new Class[0]) {
  49.             @Override
  50.             public Object execute(Object[] args) {
  51.                 d("Executing readString");
  52.                 Scanner sc = new Scanner(System.in);
  53.                 return sc.nextLine();
  54.             }
  55.         });
  56.         functions.put("writeString", new Function(new Class[]{Object.class}) {
  57.             @Override
  58.             public Object execute(Object[] args) {
  59.                 d("Executing writeString");
  60.                 System.out.println(args[0]);
  61.                 return null;
  62.             }
  63.         });
  64.         functions.put("printVars", new Function(new Class[0]) {
  65.             @Override
  66.             public Object execute(Object[] args) {
  67.                 d("Executing printVars");
  68.                 for (String s : variables.keySet()) {
  69.                     Variable var = variables.get(s);
  70.                     System.out.println(var.type + " " + s + " = " + var.value);
  71.                 }
  72.                 return null;
  73.             }
  74.         });
  75.     }
  76.  
  77.     private static Object evaluate(String line) {
  78.         Matcher matcher = ASSIGN.matcher(line);
  79.         if (matcher.matches()) {
  80.             d("Found assignment: " + line);
  81.             String type = matcher.group(1);
  82.             String name = matcher.group(2);
  83.             String value = matcher.group(3);
  84.             if (variables.containsKey(name)) throw new RuntimeException("Duplicate variables!");
  85.             variables.put(name, new Variable(type, evaluate(value)));
  86.             return null;
  87.         }
  88.         matcher = REASSIGN.matcher(line);
  89.         if (matcher.matches()) {
  90.             d("Found reassignment: " + line);
  91.             String name = matcher.group(1);
  92.             String value = matcher.group(2);
  93.             if (!variables.containsKey(name)) throw new RuntimeException("Variable not found!");
  94.             variables.get(name).setValue(value);
  95.             return null;
  96.         }
  97.         matcher = VARIABLE.matcher(line);
  98.         if (matcher.matches()) {
  99.             d("Found variable: " + line);
  100.             String name = matcher.group(2);
  101.             String type = matcher.group(1);
  102.             if (variables.containsKey(name)) throw new RuntimeException("Duplicate variables!");
  103.             variables.put(name, new Variable(type, null));
  104.             return null;
  105.         }
  106.         matcher = EXECUTE.matcher(line);
  107.         if (matcher.matches()) {
  108.             d("Found execution: " + line);
  109.             String functionName = matcher.group(1);
  110.             String[] params = matcher.group(2).split(" *, *");
  111.             if (params[0].isEmpty()) return executeFunction(functionName, new Object[0]);
  112.             Object[] evaluatedParams = new Object[params.length];
  113.             for (int i = 0; i < params.length; i++) {
  114.                 evaluatedParams[i] = evaluate(params[i]);
  115.             }
  116.             return executeFunction(functionName, evaluatedParams);
  117.         }
  118.         matcher = STRING.matcher(line);
  119.         if (matcher.matches()) {
  120.             d("Found string: " + line);
  121.             return matcher.group(1);
  122.         }
  123.         matcher = INTEGER.matcher(line);
  124.         if (matcher.matches()) {
  125.             d("Found int: " + line);
  126.             return Integer.parseInt(matcher.group(1));
  127.         }
  128.         matcher = DOUBLE.matcher(line);
  129.         if (matcher.matches()) {
  130.             d("Found double: " + line);
  131.             return Double.parseDouble(matcher.group(1));
  132.         }
  133.         if (variables.containsKey(line)) {
  134.             d("Found variable: " + line);
  135.             return variables.get(line).value;
  136.         }
  137.         System.err.println("Failed to execute \"" + line + "\"!");
  138.         return null;
  139.     }
  140.  
  141.     private static Object executeFunction(String functionName, Object[] params) {
  142.         if (!functions.containsKey(functionName)) {
  143.             throw new RuntimeException("Function \"" + functionName + "\" not found!");
  144.         }
  145.         Function fun = functions.get(functionName);
  146.         if (!fun.verifyParams(params)) {
  147.             throw new RuntimeException("Invalid parameters for function \"" + functionName + "\"");
  148.         }
  149.         return fun.execute(params);
  150.     }
  151.  
  152.     static class Variable {
  153.         private String type;
  154.         private Object value;
  155.  
  156.         private Variable(String type, Object value) {
  157.             this.type = type;
  158.             this.value = value;
  159.         }
  160.  
  161.         public void setValue(Object value) {
  162.             this.value = value;
  163.         }
  164.  
  165.         public String getType() {
  166.             return type;
  167.         }
  168.  
  169.         public Object getValue() {
  170.             return value;
  171.         }
  172.     }
  173.  
  174.     static abstract class Function {
  175.  
  176.         private Class[] types;
  177.  
  178.         public Function(Class[] types) {
  179.             this.types = types;
  180.         }
  181.  
  182.         private boolean verifyParams(Object[] args) {
  183.             if (args.length != types.length) return false;
  184.             for (int i = 0; i < types.length; i++) {
  185.                 if (!types[i].isInstance(args[i]) && args[i] != null) return false;
  186.             }
  187.             return true;
  188.         }
  189.  
  190.         public abstract Object execute(Object[] args);
  191.     }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement