From 33400c726857f7f56b95c25a6d7fba90d747d619 Mon Sep 17 00:00:00 2001 From: Sander Hautvast Date: Thu, 19 Nov 2020 22:58:49 +0100 Subject: [PATCH] added test and fixed stuff --- .../sander/beejava/apiv2/SourceCompiler.java | 115 ++++++++++++------ .../nl/sander/beejava/SourceCompilerTest.java | 14 +++ .../java/nl/sander/beejava/TestData2.java | 24 ++-- 3 files changed, 101 insertions(+), 52 deletions(-) create mode 100644 src/test/java/nl/sander/beejava/SourceCompilerTest.java diff --git a/src/main/java/nl/sander/beejava/apiv2/SourceCompiler.java b/src/main/java/nl/sander/beejava/apiv2/SourceCompiler.java index 59525b9..9c65161 100644 --- a/src/main/java/nl/sander/beejava/apiv2/SourceCompiler.java +++ b/src/main/java/nl/sander/beejava/apiv2/SourceCompiler.java @@ -15,7 +15,7 @@ import java.util.regex.Pattern; */ public class SourceCompiler { private final static Pattern firstBlanksplitter = Pattern.compile("(.+?) (.+)"); - private final static Pattern parensplitter = Pattern.compile("(.+?)(\\(.*?\\))"); + private final static Pattern parensplitter = Pattern.compile("(.+?)\\((.*?)\\)"); private final static Pattern returntypesplitter = Pattern.compile("(.+?)->(.*?)"); private final String sourcecode; private final List instructions = new ArrayList<>(); @@ -62,38 +62,47 @@ public class SourceCompiler { private BeeMethod parseMethod(String text) { String[] tokens = split(text, returntypesplitter); - Class returnType; - try { - returnType = Class.forName(tokens[1].trim()); - } catch (ClassNotFoundException e) { - throw new IllegalArgumentException("Not a valid type: "+tokens[1].trim()); + final String first; + final Class returnType; + if (tokens.length > 0) { + try { + returnType = Class.forName(tokens[1].trim()); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException("Not a valid type: " + tokens[1].trim()); + } + first = tokens[0].trim(); + } else { + first = text; + returnType = Void.TYPE; } - String first = tokens[0].trim(); - String[] flagsNameParameters = first.split(" "); + String[] flagsNameParameters = split(first, firstBlanksplitter); Set flags = new HashSet<>(); - int i=0; - Optional maybeFlag = MethodAccessFlag.get(flagsNameParameters[i]); - while (maybeFlag.isPresent()){ + int i = 0; + Optional maybeFlag = MethodAccessFlag.get(flagsNameParameters[i].toUpperCase()); + while (maybeFlag.isPresent()) { flags.add(maybeFlag.get()); - i+=1; + i += 1; maybeFlag = MethodAccessFlag.get(flagsNameParameters[i]); } String[] nameParams = split(flagsNameParameters[i], parensplitter); - String methodName = nameParams[0]; - String params = nameParams[1]; - String[] paramTokens = params.split(","); Set parameters = new HashSet<>(); - for (String paramToken : paramTokens) { - String[] declaration = paramToken.trim().split(" "); - String type = declaration[0]; - String name = declaration[1]; - try { - parameters.add(new BeeParameter(Class.forName(type), name)); - } catch (ClassNotFoundException e) { - throw new IllegalArgumentException("field type " + type + " not found"); + String methodName = ""; + if (nameParams.length > 0) { + methodName = nameParams[0]; + String params = nameParams[1]; + String[] paramTokens = params.split(","); + for (String paramToken : paramTokens) { + String[] declaration = paramToken.trim().split(" "); + String type = declaration[0]; + String name = declaration[1]; + try { + parameters.add(new BeeParameter(getType(type), name)); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException("field type " + type + " not found"); + } } } currentLine += 1; @@ -102,28 +111,42 @@ public class SourceCompiler { while (nextInstruction instanceof CodeLine) { lines.add((CodeLine) nextInstruction); currentLine += 1; + if (currentLine >= instructions.size()){ + break; // too tired to think + } nextInstruction = instructions.get(currentLine); } - return new BeeMethod(methodName,flags, parameters, returnType, lines); + + return new BeeMethod(methodName, flags, parameters, returnType, lines); + } + + private Class getType(String type) throws ClassNotFoundException { + return switch (type) { + case "int" -> int.class; + case "double" -> double.class; + default -> Class.forName(type); + }; } private BeeConstructor parseConstructor(String text) { String[] tokens = split(text, parensplitter); String flag = tokens[0]; - MethodAccessFlag methodAccessFlag = MethodAccessFlag.get(flag).orElseThrow(illegalArgument("Not a valid flag " + flag)); + MethodAccessFlag methodAccessFlag = MethodAccessFlag.get(flag.toUpperCase()).orElseThrow(illegalArgument("Not a valid flag " + flag)); String params = tokens[1]; - String[] paramTokens = params.split(","); Set parameters = new HashSet<>(); - for (String paramToken : paramTokens) { - String[] declaration = paramToken.trim().split(" "); - String type = declaration[0]; - String name = declaration[1]; - try { - parameters.add(new BeeParameter(Class.forName(type), name)); - } catch (ClassNotFoundException e) { - throw new IllegalArgumentException("field type " + type + " not found"); + if (params.length() > 0) { + String[] paramTokens = params.split(","); + for (String paramToken : paramTokens) { + String[] declaration = paramToken.trim().split(" "); + String type = declaration[0]; + String name = declaration[1]; + try { + parameters.add(new BeeParameter(Class.forName(type), name)); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException("field type " + type + " not found"); + } } } currentLine += 1; @@ -133,8 +156,8 @@ public class SourceCompiler { lines.add((CodeLine) nextInstruction); currentLine += 1; nextInstruction = instructions.get(currentLine); - } + } return new BeeConstructor(Set.of(methodAccessFlag), parameters, lines); } @@ -185,20 +208,32 @@ public class SourceCompiler { } private Instruction compileLine(String line) { - String[] tokens = split(line, firstBlanksplitter); - String operation = tokens[0]; - String operand = tokens[1]; if (!line.startsWith(" ")) { + String[] tokens = split(line, firstBlanksplitter); + String operation = tokens[0]; + String operand = tokens[1]; ClassOperation classOperation = ClassOperation.get(operation).orElseThrow(illegalArgument(operation)); return new ClassInstruction(classOperation, operand); } else { - Opcode opcode = Opcode.get(operation).orElseThrow(illegalArgument(operation)); + line = line.substring(2); + String operation; + String operand; + if (line.indexOf(' ') > -1) { + String[] tokens = split(line, firstBlanksplitter); + + operation = tokens[0]; + operand = tokens[1]; + } else { + operation = line; + operand = null; + } + Opcode opcode = Opcode.get(operation).orElseThrow(illegalArgument("Illegal opcode: " + operation)); return new CodeLine(opcode, operand); } } private Supplier illegalArgument(String text) { - return () -> new IllegalArgumentException("Illegal: " + text); + return () -> new IllegalArgumentException(text); } private String[] split(String text, Pattern splitter) { diff --git a/src/test/java/nl/sander/beejava/SourceCompilerTest.java b/src/test/java/nl/sander/beejava/SourceCompilerTest.java new file mode 100644 index 0000000..ee440ac --- /dev/null +++ b/src/test/java/nl/sander/beejava/SourceCompilerTest.java @@ -0,0 +1,14 @@ +package nl.sander.beejava; + +import nl.sander.beejava.apiv2.BeeSource; +import nl.sander.beejava.apiv2.SourceCompiler; +import org.junit.jupiter.api.Test; + +public class SourceCompilerTest { + + @Test + public void test(){ + BeeSource beeSource = new SourceCompiler(TestData2.simpleBean).doCompile(); + System.out.println(beeSource); + } +} diff --git a/src/test/java/nl/sander/beejava/TestData2.java b/src/test/java/nl/sander/beejava/TestData2.java index a2e3cc4..59de549 100644 --- a/src/test/java/nl/sander/beejava/TestData2.java +++ b/src/test/java/nl/sander/beejava/TestData2.java @@ -2,18 +2,18 @@ package nl.sander.beejava; public class TestData2 { - public final String simpleBean= """ - public class simpleBean - field value:int - constructor() - INVOKE super() - RETURN - method getValue() - RETURN this.value - method setValue(int newVale) - LOAD newValue - PUT this.value - RETURN + public final static String simpleBean= """ + class com.acme.SimpleBean(V15) + field public final int value + constructor public() + INVOKE super() + RETURN + method public getValue() -> int + RETURN this.value + method public setValue(int newValue) + LOAD newValue + PUT this.value + RETURN """; String constructor_original = """