diff --git a/out/artifacts/jungle_jar/jungle.jar b/out/artifacts/jungle_jar/jungle.jar index 1566469..b375006 100644 Binary files a/out/artifacts/jungle_jar/jungle.jar and b/out/artifacts/jungle_jar/jungle.jar differ diff --git a/src/main/java/com/jungle/ast/Node.java b/src/main/java/com/jungle/ast/Node.java index 37643b6..cfe32d7 100644 --- a/src/main/java/com/jungle/ast/Node.java +++ b/src/main/java/com/jungle/ast/Node.java @@ -166,7 +166,7 @@ public String toString() { // region Save AST @NotNull - private static final FileLogger saveLog = new FileLogger("SaveAst"); + private static final FileLogger saveLogger = new FileLogger("SaveAst"); public static void save(@NotNull BufferedWriter writer, @Nullable INode node) { // Non-recursive traversal @@ -174,36 +174,36 @@ public static void save(@NotNull BufferedWriter writer, @Nullable INode node) { nodeStack.push(node); while (nodeStack.size() > 0) { INode nextNode = nodeStack.pop(); - saveLog.debug("---"); + saveLogger.debug("---"); if (nextNode == null) { - saveLog.debug("node is terminal"); + saveLogger.debug("node is terminal"); try { writer.write(TERMINAL); } catch (IOException e) { throw new SaveError("failed to write terminal", e); } } else { - saveLog.debug("type: " + nextNode.getType()); - saveLog.debug("value: " + nextNode.getRawValue()); + saveLogger.debug("type: " + nextNode.getType()); + saveLogger.debug("value: " + nextNode.getRawValue()); try { writer.write(nextNode.getType().name()); } catch (IOException e) { String message = "failed to write node type"; - saveLog.error(message, e); + saveLogger.error(message, e); throw new SaveError(message); } if (nextNode.isLeaf()) { - saveLog.debug("node is leaf"); + saveLogger.debug("node is leaf"); try { writer.write(DELIMITER_FIELD); writer.write(nextNode.getRawValue()); } catch (IOException e) { String message = "failed to write node value"; - saveLog.error(message, e); + saveLogger.error(message, e); throw new SaveError(message); } } else { - saveLog.debug("node is parent"); + saveLogger.debug("node is parent"); nodeStack.push(nextNode.getRight()); nodeStack.push(nextNode.getLeft()); } @@ -212,7 +212,7 @@ public static void save(@NotNull BufferedWriter writer, @Nullable INode node) { writer.write(DELIMITER_LINE); } catch (IOException e) { String message = "failed to write line delimiter"; - saveLog.error(message, e); + saveLogger.error(message, e); throw new SaveError(message); } } @@ -222,7 +222,7 @@ public static void save(@NotNull BufferedWriter writer, @Nullable INode node) { // region Load AST - private static final FileLogger loadLog = new FileLogger("LoadAst"); + private static final FileLogger loadLogger = new FileLogger("LoadAst"); @Nullable public static INode load(@NotNull BufferedReader reader) { @@ -235,21 +235,21 @@ public static INode load(@NotNull BufferedReader reader) { line = reader.readLine(); } catch (IOException e) { String message = "failed to read file"; - loadLog.error(message, e); + loadLogger.error(message, e); throw new LoadError(message); } boolean hasStreamEnded = line == null; if (hasStreamEnded) { - loadLog.warn("end of stream reached"); + loadLogger.warn("end of stream reached"); return null; } boolean hasEmptyLine = line.length() == 0; if (hasEmptyLine) { - loadLog.debug("line is empty"); + loadLogger.debug("line is empty"); return null; } if (line.trim().startsWith(Character.toString(TERMINAL))) { - loadLog.debug("line is terminal"); + loadLogger.debug("line is terminal"); return null; } int splitIndex = line.indexOf(DELIMITER_FIELD); @@ -260,22 +260,22 @@ public static INode load(@NotNull BufferedReader reader) { value = null; } else if (splitIndex == 0) { String message = "line missing type"; - loadLog.error(message); + loadLogger.error(message); throw new LoadError(message); } else { type = line.substring(0, splitIndex); value = line.substring(splitIndex + 1); } type = type.trim(); - loadLog.debug("---"); - loadLog.debug("type: " + type); - loadLog.debug("value: " + value); + loadLogger.debug("---"); + loadLogger.debug("type: " + type); + loadLogger.debug("value: " + value); Node node; try { node = new Node(NodeType.valueOf(type)); } catch (IllegalArgumentException e) { String message = "line type invalid"; - loadLog.error(message, e); + loadLogger.error(message, e); throw new LoadError(message); } boolean isLeafNode = value != null; diff --git a/src/main/java/com/jungle/common/ClassLoader.java b/src/main/java/com/jungle/common/ClassLoader.java index 7e7eb66..9744ed2 100644 --- a/src/main/java/com/jungle/common/ClassLoader.java +++ b/src/main/java/com/jungle/common/ClassLoader.java @@ -10,8 +10,13 @@ import org.jetbrains.annotations.NotNull; +import com.jungle.logger.FileLogger; + public class ClassLoader { + @NotNull + private static final FileLogger logger = new FileLogger(ClassLoader.class.getName()); + public static Class load(@NotNull String classPaths, @NotNull String className) throws MalformedURLException, ClassNotFoundException { @@ -42,7 +47,7 @@ public static Class load(@NotNull String className) if (classPath == null) { classPath = "."; } - System.out.println(String.format("loading class from %s", classPath)); + logger.debug(String.format("loading class from %s", classPath)); return load(classPath, className); } } diff --git a/src/main/java/com/jungle/compiler/Compiler.java b/src/main/java/com/jungle/compiler/Compiler.java index 9c2f71f..778baf7 100644 --- a/src/main/java/com/jungle/compiler/Compiler.java +++ b/src/main/java/com/jungle/compiler/Compiler.java @@ -19,8 +19,8 @@ import static org.objectweb.asm.Opcodes.*; public class Compiler { - - public static final FileLogger log = new FileLogger(Compiler.class.getSimpleName()); + @NotNull + private static final FileLogger logger = new FileLogger(Compiler.class.getSimpleName()); // region Helpers @@ -30,7 +30,7 @@ public static void writeClassFile(@NotNull String className, byte[] classData) { Files.write(classPath, classData); } catch (IOException e) { String message = "failed to write to class file"; - log.error(message, e); + logger.error(message, e); throw new CompilerError(message); } } @@ -41,17 +41,17 @@ public void compile(@NotNull String mainClassName, @NotNull IVisitor mainVisitor // TODO: handle multi-class if (ast == null) { String message = "AST is null"; - log.error(message); + logger.error(message); throw new CompilerError(message); } - log.debug("generating entrypoint class from template"); + logger.debug("generating entrypoint class from template"); ClassWriter initialClassWriter = visitMainClass(mainClassName); // template ClassReader classReader = new ClassReader(initialClassWriter.toByteArray()); ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); ClassVisitor entrypoint = new MainClassVisitor(classWriter, mainVisitor, ast); - log.debug("traversing AST"); + logger.debug("traversing AST"); classReader.accept(entrypoint, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); - log.debug("writing class file"); + logger.debug("writing class file"); writeClassFile(mainClassName, classWriter.toByteArray()); } @@ -60,7 +60,7 @@ public void compile(@NotNull String mainClassName, @NotNull IVisitor mainVisitor @NotNull protected ClassWriter visitMainClass(@NotNull String className) { // class Entrypoint extends Object {} - log.debug("visit main class"); + logger.debug("visit main class"); int flags = ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES; ClassWriter cw = new ClassWriter(flags); cw.visit( @@ -82,7 +82,7 @@ protected ClassWriter visitMainClass(@NotNull String className) { @NotNull public static MethodVisitor visitDefaultConstructor(@NotNull ClassWriter cw) { // public ClassConstructor() { Object::super(); return; } - log.debug("visit default constructor"); + logger.debug("visit default constructor"); MethodVisitor mv = cw.visitMethod( ACC_PUBLIC, "", @@ -106,7 +106,7 @@ public static MethodVisitor visitDefaultConstructor(@NotNull ClassWriter cw) { @NotNull public static MethodVisitor visitMainMethod(@NotNull ClassWriter cw) { // public static void main(String[]) { return; } - log.debug("visit main method"); + logger.debug("visit main method"); MethodVisitor mv = cw.visitMethod( ACC_PUBLIC + ACC_STATIC, MainMethodVisitor.MAIN_METHOD_NAME, diff --git a/src/main/java/com/jungle/compiler/MainClassVisitor.java b/src/main/java/com/jungle/compiler/MainClassVisitor.java index b6933b7..584d81d 100644 --- a/src/main/java/com/jungle/compiler/MainClassVisitor.java +++ b/src/main/java/com/jungle/compiler/MainClassVisitor.java @@ -2,12 +2,17 @@ import com.jungle.ast.INode; import com.jungle.compiler.visitor.IVisitor; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; public class MainClassVisitor extends ClassVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(MainClassVisitor.class.getSimpleName()); + @NotNull private final IVisitor mainVisitor; @@ -25,7 +30,7 @@ public MethodVisitor visitMethod(int flags, String name, String desc, String sig MethodVisitor mv = super.visitMethod(flags, name, desc, signature, exceptions); boolean isMainMethod = MainMethodVisitor.MAIN_METHOD_NAME.equals(name); if (isMainMethod) { - Compiler.log.debug("emit custom instructions in main class"); + logger.debug("emit custom instructions in main class"); return new MainMethodVisitor(mv, mainVisitor, ast); } return mv; diff --git a/src/main/java/com/jungle/compiler/MainMethodVisitor.java b/src/main/java/com/jungle/compiler/MainMethodVisitor.java index dabeedd..b2e331c 100644 --- a/src/main/java/com/jungle/compiler/MainMethodVisitor.java +++ b/src/main/java/com/jungle/compiler/MainMethodVisitor.java @@ -2,11 +2,16 @@ import com.jungle.ast.INode; import com.jungle.compiler.visitor.IVisitor; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; public class MainMethodVisitor extends MethodVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(MainMethodVisitor.class.getSimpleName()); + public static final String MAIN_METHOD_NAME = "main"; @NotNull @@ -24,7 +29,7 @@ public MainMethodVisitor(MethodVisitor mv, @NotNull IVisitor mainVisitor, @NotNu @Override public void visitInsn(final int opcode) { if (opcode == Opcodes.RETURN) { - Compiler.log.debug("emit instructions just before existing RETURN operation"); + logger.debug("emit instructions just before existing RETURN operation"); mainVisitor.visit(this, ast); } super.visitInsn(opcode); diff --git a/src/main/java/com/jungle/compiler/operand/OperandStackContext.java b/src/main/java/com/jungle/compiler/operand/OperandStackContext.java index 2f4ca33..d980108 100644 --- a/src/main/java/com/jungle/compiler/operand/OperandStackContext.java +++ b/src/main/java/com/jungle/compiler/operand/OperandStackContext.java @@ -3,6 +3,8 @@ import com.jungle.compiler.symbol.SymbolEntry; import com.jungle.compiler.symbol.SymbolTable; import com.jungle.compiler.symbol.SymbolType; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; @@ -10,6 +12,9 @@ import java.util.Stack; public class OperandStackContext { + @NotNull + private static final FileLogger logger = new FileLogger(OperandStackContext.class.getName()); + // region Singleton Factory @Nullable private static OperandStackContext operandStackContext = null; @@ -60,7 +65,7 @@ public void visitLoad( @NotNull String variableName ) { // Get local variable index value and push variable value onto the operand stack - System.out.println("visit load " + variableName); + logger.debug("visit load " + variableName); SymbolEntry entry = getSymbolTable().get(variableName); if (entry == null) { @@ -84,7 +89,7 @@ public void visitStore( @NotNull String variableName ) { // Pop value off the operand stack and set local variable index value - System.out.println("visit store " + variableName); + logger.debug("visit store " + variableName); OperandStackType variableType = pop(); SymbolType variableSymbolType = variableType.getSymbolType(); diff --git a/src/main/java/com/jungle/compiler/visitor/AssertVisitor.java b/src/main/java/com/jungle/compiler/visitor/AssertVisitor.java index a4f2511..723ae5c 100644 --- a/src/main/java/com/jungle/compiler/visitor/AssertVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/AssertVisitor.java @@ -4,6 +4,8 @@ import com.jungle.ast.NodeType; import com.jungle.compiler.operand.OperandStackContext; import com.jungle.compiler.operand.OperandStackType; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.Label; @@ -11,6 +13,13 @@ import org.objectweb.asm.Opcodes; public class AssertVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(AssertVisitor.class.getName()); + + /* TODO + * It might be possible to use reflection to provide more context on the assertion. + * For example: which method, which line, which variables, etc. + */ @Nullable private OperandStackContext operandStackContext; @@ -52,7 +61,7 @@ public boolean canVisit(@NotNull INode ast) { @Override public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { - System.out.println("visit assert " + ast); + logger.debug("visit assert " + ast); if (!canVisit(ast)) { throw new Error("expected assert"); diff --git a/src/main/java/com/jungle/compiler/visitor/AssignmentVisitor.java b/src/main/java/com/jungle/compiler/visitor/AssignmentVisitor.java index 7643d87..03c090a 100644 --- a/src/main/java/com/jungle/compiler/visitor/AssignmentVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/AssignmentVisitor.java @@ -3,11 +3,16 @@ import com.jungle.ast.INode; import com.jungle.ast.NodeType; import com.jungle.compiler.operand.OperandStackContext; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; public class AssignmentVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(AssignmentVisitor.class.getName()); + @Nullable private OperandStackContext operandStackContext; @@ -49,7 +54,7 @@ public boolean canVisit(@NotNull INode ast) { @Override public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { - System.out.println("visit assignment " + ast); + logger.debug("visit assignment " + ast); if (!canVisit(ast)) { throw new Error("expected assignment"); diff --git a/src/main/java/com/jungle/compiler/visitor/BlockVisitor.java b/src/main/java/com/jungle/compiler/visitor/BlockVisitor.java index 305de72..6a36f34 100644 --- a/src/main/java/com/jungle/compiler/visitor/BlockVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/BlockVisitor.java @@ -2,11 +2,16 @@ import com.jungle.ast.INode; import com.jungle.ast.NodeType; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; public class BlockVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(BlockVisitor.class.getName()); + @Nullable private MainVisitor mainVisitor; @@ -33,7 +38,7 @@ public boolean canVisit(@NotNull INode ast) { @Override public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { - System.out.println("visit block " + ast); + logger.debug("visit block " + ast); if (!canVisit(ast)) { throw new Error("expected block"); diff --git a/src/main/java/com/jungle/compiler/visitor/BooleanOperatorVisitor.java b/src/main/java/com/jungle/compiler/visitor/BooleanOperatorVisitor.java index 2c6eaaf..f65c23b 100644 --- a/src/main/java/com/jungle/compiler/visitor/BooleanOperatorVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/BooleanOperatorVisitor.java @@ -3,6 +3,8 @@ import com.jungle.ast.INode; import com.jungle.ast.Node; import com.jungle.ast.NodeType; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; @@ -12,6 +14,9 @@ import java.util.Set; public class BooleanOperatorVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(BooleanOperatorVisitor.class.getName()); + @NotNull private static final INode PUSH_TRUE_NODE = new Node(NodeType.LITERAL_INTEGER).withRawValue("1"); @@ -58,7 +63,7 @@ public boolean canVisit(@NotNull INode ast) { @Override public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { - System.out.println("visit boolean operator " + ast); + logger.debug("visit boolean operator " + ast); // Optimization idea: // if the left expression has a shorter depth (fewer operations) than the right, diff --git a/src/main/java/com/jungle/compiler/visitor/CastIntegerVisitor.java b/src/main/java/com/jungle/compiler/visitor/CastIntegerVisitor.java index 319c5f4..49c8c4d 100644 --- a/src/main/java/com/jungle/compiler/visitor/CastIntegerVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/CastIntegerVisitor.java @@ -4,12 +4,17 @@ import com.jungle.ast.NodeType; import com.jungle.compiler.operand.OperandStackContext; import com.jungle.compiler.operand.OperandStackType; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; public class CastIntegerVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(CastIntegerVisitor.class.getName()); + @Nullable private OperandStackContext operandStackContext; @@ -51,7 +56,7 @@ public boolean canVisit(@NotNull INode ast) { @Override public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { - System.out.println("visit cast integer " + ast); + logger.debug("visit cast integer " + ast); if (!canVisit(ast)) { throw new Error("expected cast integer"); @@ -65,7 +70,7 @@ public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { OperandStackType type = getOperandStackContext().pop(); switch (type) { case INTEGER: { - System.out.println("WARN: value is already an integer"); + logger.warn("value is already an integer"); } break; case FLOAT: { mv.visitInsn(Opcodes.F2I); diff --git a/src/main/java/com/jungle/compiler/visitor/ExpressionVisitor.java b/src/main/java/com/jungle/compiler/visitor/ExpressionVisitor.java index dd44ed7..ae6adfc 100644 --- a/src/main/java/com/jungle/compiler/visitor/ExpressionVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/ExpressionVisitor.java @@ -1,11 +1,16 @@ package com.jungle.compiler.visitor; import com.jungle.ast.INode; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; public class ExpressionVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(ExpressionVisitor.class.getName()); + @Nullable private IdentifierVisitor identifierVisitor; @@ -86,7 +91,7 @@ public boolean canVisit(@NotNull INode ast) { @Override public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { - System.out.println("visit expression " + ast); + logger.debug("visit expression " + ast); if (!canVisit(ast)) { throw new Error("expected expression"); diff --git a/src/main/java/com/jungle/compiler/visitor/IdentifierVisitor.java b/src/main/java/com/jungle/compiler/visitor/IdentifierVisitor.java index 236bcb4..14b1656 100644 --- a/src/main/java/com/jungle/compiler/visitor/IdentifierVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/IdentifierVisitor.java @@ -3,11 +3,16 @@ import com.jungle.ast.INode; import com.jungle.ast.NodeType; import com.jungle.compiler.operand.OperandStackContext; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; public class IdentifierVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(IdentifierVisitor.class.getName()); + @Nullable private OperandStackContext operandStackContext; @@ -35,7 +40,7 @@ public boolean canVisit(@NotNull INode node) { @Override public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { - System.out.println("visit identifier " + ast); + logger.debug("visit identifier " + ast); if (!canVisit(ast)) { throw new Error("expected identifier"); diff --git a/src/main/java/com/jungle/compiler/visitor/IfVisitor.java b/src/main/java/com/jungle/compiler/visitor/IfVisitor.java index 3ef0f47..bb10cfd 100644 --- a/src/main/java/com/jungle/compiler/visitor/IfVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/IfVisitor.java @@ -5,6 +5,8 @@ import com.jungle.ast.NodeType; import com.jungle.compiler.operand.OperandStackContext; import com.jungle.compiler.operand.OperandStackType; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.Label; @@ -12,6 +14,9 @@ import org.objectweb.asm.Opcodes; public class IfVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(IfVisitor.class.getName()); + @Nullable private OperandStackContext operandStackContext; @@ -70,7 +75,7 @@ public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { } protected void visit(@NotNull MethodVisitor mv, @NotNull CompareTo compareTo, @NotNull INode ast) { - System.out.println("visit if " + ast); + logger.debug("visit if " + ast); if (!canVisit(ast)) { throw new Error("expected if"); diff --git a/src/main/java/com/jungle/compiler/visitor/LiteralVisitor.java b/src/main/java/com/jungle/compiler/visitor/LiteralVisitor.java index 7fd64f6..d2a0787 100644 --- a/src/main/java/com/jungle/compiler/visitor/LiteralVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/LiteralVisitor.java @@ -4,6 +4,8 @@ import com.jungle.ast.NodeType; import com.jungle.compiler.operand.OperandStackContext; import com.jungle.compiler.operand.OperandStackType; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; @@ -15,6 +17,9 @@ import static com.jungle.ast.NodeType.*; public class LiteralVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(LiteralVisitor.class.getName()); + private static final Set LITERALS = new HashSet<>(Arrays.asList( LITERAL_BOOLEAN, LITERAL_CHARACTER, @@ -49,7 +54,7 @@ public boolean canVisit(@NotNull INode ast) { @Override public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { - System.out.println("visit literal " + ast); + logger.debug("visit literal " + ast); if (!canVisit(ast)) { throw new Error("expected literal"); diff --git a/src/main/java/com/jungle/compiler/visitor/LoopVisitor.java b/src/main/java/com/jungle/compiler/visitor/LoopVisitor.java index f55b0ef..6d28855 100644 --- a/src/main/java/com/jungle/compiler/visitor/LoopVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/LoopVisitor.java @@ -4,6 +4,8 @@ import com.jungle.ast.NodeType; import com.jungle.compiler.operand.OperandStackContext; import com.jungle.compiler.operand.OperandStackType; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.Label; @@ -11,6 +13,9 @@ import org.objectweb.asm.Opcodes; public class LoopVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(LoopVisitor.class.getName()); + @Nullable private OperandStackContext operandStackContext; @@ -70,7 +75,7 @@ public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { GOTO #loop end: ... */ - System.out.println("visit loop " + ast); + logger.debug("visit loop " + ast); if (!canVisit(ast)) { throw new Error("expected loop"); diff --git a/src/main/java/com/jungle/compiler/visitor/MainVisitor.java b/src/main/java/com/jungle/compiler/visitor/MainVisitor.java index d153c26..5849e9a 100644 --- a/src/main/java/com/jungle/compiler/visitor/MainVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/MainVisitor.java @@ -1,11 +1,17 @@ package com.jungle.compiler.visitor; import com.jungle.ast.INode; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; public class MainVisitor implements IVisitor { + + @NotNull + private static final FileLogger logger = new FileLogger(MainVisitor.class.getSimpleName()); + @Nullable private LiteralVisitor literalVisitor; @@ -138,7 +144,7 @@ public boolean canVisit(@NotNull INode ast) { @Override public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { - System.out.println("visit main " + ast); + logger.debug("visit main " + ast); if (!canVisit(ast)) { throw new Error("expected main"); diff --git a/src/main/java/com/jungle/compiler/visitor/NumericOperatorVisitor.java b/src/main/java/com/jungle/compiler/visitor/NumericOperatorVisitor.java index 3bd71be..e4c90cb 100644 --- a/src/main/java/com/jungle/compiler/visitor/NumericOperatorVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/NumericOperatorVisitor.java @@ -5,6 +5,8 @@ import com.jungle.compiler.operand.OperandStackContext; import com.jungle.compiler.operand.OperandStackType; import com.jungle.compiler.symbol.SymbolType; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; @@ -17,6 +19,9 @@ import static com.jungle.ast.NodeType.*; public class NumericOperatorVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(NumericOperatorVisitor.class.getName()); + private static final Set NUMERIC_OPERATORS = new HashSet<>(Arrays.asList( OPERATOR_ADD, OPERATOR_SUBTRACT, @@ -66,7 +71,7 @@ public boolean canVisit(@NotNull INode node) { @Override public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { - System.out.println("visit binary operator " + ast); + logger.debug("visit binary operator " + ast); if (!canVisit(ast)) { throw new Error("expected numeric operator"); diff --git a/src/main/java/com/jungle/compiler/visitor/PrintVisitor.java b/src/main/java/com/jungle/compiler/visitor/PrintVisitor.java index 9bcf62a..93b568a 100644 --- a/src/main/java/com/jungle/compiler/visitor/PrintVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/PrintVisitor.java @@ -4,6 +4,8 @@ import com.jungle.ast.NodeType; import com.jungle.compiler.operand.OperandStackContext; import com.jungle.compiler.operand.OperandStackType; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; @@ -11,6 +13,9 @@ import static org.objectweb.asm.Opcodes.*; public class PrintVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(NumericOperatorVisitor.class.getName()); + @Nullable private OperandStackContext operandStackContext; @@ -52,7 +57,7 @@ public boolean canVisit(@NotNull INode ast) { @Override public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { - System.out.println("visit print " + ast); + logger.debug("visit print " + ast); if (!canVisit(ast)) { throw new Error("expected print"); diff --git a/src/main/java/com/jungle/compiler/visitor/SequenceVisitor.java b/src/main/java/com/jungle/compiler/visitor/SequenceVisitor.java index 13804b5..c5275c9 100644 --- a/src/main/java/com/jungle/compiler/visitor/SequenceVisitor.java +++ b/src/main/java/com/jungle/compiler/visitor/SequenceVisitor.java @@ -2,11 +2,16 @@ import com.jungle.ast.INode; import com.jungle.ast.NodeType; +import com.jungle.logger.FileLogger; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.MethodVisitor; public class SequenceVisitor implements IVisitor { + @NotNull + private static final FileLogger logger = new FileLogger(SequenceVisitor.class.getSimpleName()); + @Nullable private MainVisitor mainVisitor; @@ -33,7 +38,7 @@ public boolean canVisit(@NotNull INode ast) { @Override public void visit(@NotNull MethodVisitor mv, @NotNull INode ast) { - System.out.println("visit sequence " + ast); + logger.debug("visit sequence " + ast); if (!canVisit(ast)) { throw new Error("expected sequence"); diff --git a/src/main/java/com/jungle/logger/AbstractLogger.java b/src/main/java/com/jungle/logger/AbstractLogger.java index 09d9c36..e2bf93f 100644 --- a/src/main/java/com/jungle/logger/AbstractLogger.java +++ b/src/main/java/com/jungle/logger/AbstractLogger.java @@ -7,7 +7,7 @@ public abstract class AbstractLogger implements ILogger { @NotNull - public static final LogLevel DEFAULT_LOG_LEVEL = LogLevel.INFO; + public static final LogLevel DEFAULT_LOG_LEVEL = LogLevel.WARN; @NotNull public static LogLevel getLogLevel() { @@ -16,7 +16,7 @@ public static LogLevel getLogLevel() { return DEFAULT_LOG_LEVEL; } try { - return LogLevel.valueOf(logLevelString); + return LogLevel.valueOf(logLevelString.toUpperCase()); } catch (IllegalArgumentException e) { return DEFAULT_LOG_LEVEL; }