Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stirante.Everything.scripter;
- import java.util.HashMap;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class MainTest {
- public static final boolean DEBUG = false;
- public static final String TEST_CODE = "string zmienna = \"wartosc stringa\";\n" +
- "writeString(\"Wpisz wejscie: \");\n" +
- "string wejscie = readString();\n" +
- "writeString(wejscie);\n" +
- "writeString(\"Zmienne w pamieci: \");\n" +
- "printVars();";
- private static final Pattern VARIABLE = Pattern.compile("([a-zA-Z0-9]+) *([a-zA-Z0-9]+);");
- private static final Pattern ASSIGN = Pattern.compile("([a-zA-Z0-9]+) *([a-zA-Z0-9]+) *= *(.+);");
- private static final Pattern REASSIGN = Pattern.compile("([a-zA-Z0-9]+) *= *(.+);");
- private static final Pattern EXECUTE = Pattern.compile("([a-zA-Z0-9]+)\\((.*)\\);?");
- private static final Pattern STRING = Pattern.compile("\"(.+)\"");
- private static final Pattern INTEGER = Pattern.compile("([0-9]+)");
- private static final Pattern DOUBLE = Pattern.compile("([0-9]+(?:\\.[0-9]+)?)");
- private static HashMap<String, Variable> variables;
- private static HashMap<String, Function> functions;
- private static void d(Object msg) {
- if (DEBUG) System.out.println(msg);
- }
- public static void main(String[] args) {
- variables = new HashMap<>();
- functions = new HashMap<>();
- registerFunctions();
- runScript(TEST_CODE);
- }
- public static void runScript(String script) {
- String[] lines = script.split("\n");
- for (String line : lines) {
- evaluate(line);
- }
- }
- private static void registerFunctions() {
- functions.put("readString", new Function(new Class[0]) {
- @Override
- public Object execute(Object[] args) {
- d("Executing readString");
- Scanner sc = new Scanner(System.in);
- return sc.nextLine();
- }
- });
- functions.put("writeString", new Function(new Class[]{Object.class}) {
- @Override
- public Object execute(Object[] args) {
- d("Executing writeString");
- System.out.println(args[0]);
- return null;
- }
- });
- functions.put("printVars", new Function(new Class[0]) {
- @Override
- public Object execute(Object[] args) {
- d("Executing printVars");
- for (String s : variables.keySet()) {
- Variable var = variables.get(s);
- System.out.println(var.type + " " + s + " = " + var.value);
- }
- return null;
- }
- });
- }
- private static Object evaluate(String line) {
- Matcher matcher = ASSIGN.matcher(line);
- if (matcher.matches()) {
- d("Found assignment: " + line);
- String type = matcher.group(1);
- String name = matcher.group(2);
- String value = matcher.group(3);
- if (variables.containsKey(name)) throw new RuntimeException("Duplicate variables!");
- variables.put(name, new Variable(type, evaluate(value)));
- return null;
- }
- matcher = REASSIGN.matcher(line);
- if (matcher.matches()) {
- d("Found reassignment: " + line);
- String name = matcher.group(1);
- String value = matcher.group(2);
- if (!variables.containsKey(name)) throw new RuntimeException("Variable not found!");
- variables.get(name).setValue(value);
- return null;
- }
- matcher = VARIABLE.matcher(line);
- if (matcher.matches()) {
- d("Found variable: " + line);
- String name = matcher.group(2);
- String type = matcher.group(1);
- if (variables.containsKey(name)) throw new RuntimeException("Duplicate variables!");
- variables.put(name, new Variable(type, null));
- return null;
- }
- matcher = EXECUTE.matcher(line);
- if (matcher.matches()) {
- d("Found execution: " + line);
- String functionName = matcher.group(1);
- String[] params = matcher.group(2).split(" *, *");
- if (params[0].isEmpty()) return executeFunction(functionName, new Object[0]);
- Object[] evaluatedParams = new Object[params.length];
- for (int i = 0; i < params.length; i++) {
- evaluatedParams[i] = evaluate(params[i]);
- }
- return executeFunction(functionName, evaluatedParams);
- }
- matcher = STRING.matcher(line);
- if (matcher.matches()) {
- d("Found string: " + line);
- return matcher.group(1);
- }
- matcher = INTEGER.matcher(line);
- if (matcher.matches()) {
- d("Found int: " + line);
- return Integer.parseInt(matcher.group(1));
- }
- matcher = DOUBLE.matcher(line);
- if (matcher.matches()) {
- d("Found double: " + line);
- return Double.parseDouble(matcher.group(1));
- }
- if (variables.containsKey(line)) {
- d("Found variable: " + line);
- return variables.get(line).value;
- }
- System.err.println("Failed to execute \"" + line + "\"!");
- return null;
- }
- private static Object executeFunction(String functionName, Object[] params) {
- if (!functions.containsKey(functionName)) {
- throw new RuntimeException("Function \"" + functionName + "\" not found!");
- }
- Function fun = functions.get(functionName);
- if (!fun.verifyParams(params)) {
- throw new RuntimeException("Invalid parameters for function \"" + functionName + "\"");
- }
- return fun.execute(params);
- }
- static class Variable {
- private String type;
- private Object value;
- private Variable(String type, Object value) {
- this.type = type;
- this.value = value;
- }
- public void setValue(Object value) {
- this.value = value;
- }
- public String getType() {
- return type;
- }
- public Object getValue() {
- return value;
- }
- }
- static abstract class Function {
- private Class[] types;
- public Function(Class[] types) {
- this.types = types;
- }
- private boolean verifyParams(Object[] args) {
- if (args.length != types.length) return false;
- for (int i = 0; i < types.length; i++) {
- if (!types[i].isInstance(args[i]) && args[i] != null) return false;
- }
- return true;
- }
- public abstract Object execute(Object[] args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement