diff --git a/src/pp/s1184725/boppi/Annotations.java b/src/pp/s1184725/boppi/Annotations.java index a0b30c5..c21cdc6 100644 --- a/src/pp/s1184725/boppi/Annotations.java +++ b/src/pp/s1184725/boppi/Annotations.java @@ -7,10 +7,11 @@ import org.antlr.v4.runtime.tree.ParseTreeProperty; import pp.iloc.model.Reg; import pp.s1184725.boppi.type.*; +import pp.s1184725.boppi.symboltable.*; /** * This class holds properties of all AST nodes during compilation. - * + * * @author Frank Wibbelink */ public class Annotations { diff --git a/src/pp/s1184725/boppi/BoppiChecker.java b/src/pp/s1184725/boppi/BoppiChecker.java index 4ee18d0..cc3c2e9 100644 --- a/src/pp/s1184725/boppi/BoppiChecker.java +++ b/src/pp/s1184725/boppi/BoppiChecker.java @@ -10,11 +10,12 @@ import pp.s1184725.boppi.antlr.*; import pp.s1184725.boppi.antlr.BoppiParser.*; import pp.s1184725.boppi.exception.*; import pp.s1184725.boppi.type.*; +import pp.s1184725.boppi.symboltable.*; /** * This class performs type checking and variable assignment on a bare parse * tree. - * + * * @author Frank Wibbelink */ public class BoppiChecker extends BoppiBaseVisitor { @@ -25,7 +26,7 @@ public class BoppiChecker extends BoppiBaseVisitor { /** * Checks and annotates a program. Problems are reported to the given * logger. - * + * * @param ast * the program's AST * @param logger @@ -47,7 +48,7 @@ public class BoppiChecker extends BoppiBaseVisitor { /** * Checks whether two types are compatible and log an error if they do not. - * + * * @param type1 * the left hand or actual type * @param type2 @@ -62,7 +63,7 @@ public class BoppiChecker extends BoppiBaseVisitor { /** * Returns an error message for a given parse tree node. - * + * * @param ctx * the parse tree node at which the error occurred * @param message @@ -81,7 +82,7 @@ public class BoppiChecker extends BoppiBaseVisitor { * within a set root (may be {@code null}). Effectively traverses between * {@code current} and {@code root} in the parent hierarchy looking for a * {@code rule} class. - * + * * @param * the rule class * @param ctx @@ -105,7 +106,7 @@ public class BoppiChecker extends BoppiBaseVisitor { /** * Visits the parse tree, then records the returned type in the type * annotations and returns it too. - * + * * @param tree * the tree to visit * @return the evaluated type of the tree @@ -394,7 +395,7 @@ public class BoppiChecker extends BoppiBaseVisitor { @Override public Type visitTypeFunction(TypeFunctionContext ctx) { - return new FunctionType(visit(ctx.type(1)), visit(ctx.type(0))); + return new FunctionType(visit(ctx.type(0)), visit(ctx.type(1))); } @Override diff --git a/src/pp/s1184725/boppi/BoppiGenerator.java b/src/pp/s1184725/boppi/BoppiGenerator.java index b7e532a..427e94e 100644 --- a/src/pp/s1184725/boppi/BoppiGenerator.java +++ b/src/pp/s1184725/boppi/BoppiGenerator.java @@ -13,11 +13,11 @@ import pp.iloc.model.*; import pp.s1184725.boppi.antlr.*; import pp.s1184725.boppi.antlr.BoppiParser.*; import pp.s1184725.boppi.type.*; -import pp.s1184725.boppi.util.RegisterPool; +import pp.s1184725.boppi.symboltable.*; /** * The generator stage of the Boppi toolchain. - * + * * @author Frank Wibbelink */ public class BoppiGenerator extends BoppiBaseVisitor { @@ -83,7 +83,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { /** * Creates a new generator with the given variable annotations and attaches * the given logger. - * + * * @param ast * the program's AST * @param annotations @@ -142,7 +142,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { /** * Increments the chain of ARs. - * + * * @param temp * temporary register to use * @param ar @@ -168,7 +168,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { /** * Decrements the chain of ARs. - * + * * @param temp * temporary register to use * @param ar @@ -195,7 +195,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { /** * Allocates a chunk of memory and returns a pointer to the memory in the * given register. - * + * * @param temp * temporary register to use * @param size @@ -213,7 +213,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { /** * Allocates a chunk of memory and returns a pointer to the memory in the * given register. - * + * * @param temp * temporary register to use * @param size @@ -229,7 +229,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { /** * Frees a chunk of memory pointed at by {@code ptr}. - * + * * @param temp * temporary register to use * @param ptr @@ -244,7 +244,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { /** * Increments the reference count of a memory chunk. - * + * * @param temp * temporary register to use * @param ptr @@ -259,7 +259,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { /** * Returns an error message for a given parse tree node. - * + * * @param ctx * the parse tree node at which the error occurred * @param message @@ -276,7 +276,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { /** * Constructs a new unique label with the given prefix to make it * recognisable in ILOC. - * + * * @param prefix * a short prefix for the label * @return the label @@ -289,7 +289,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { * Increments the reference count to a type if it is a reference type. For * function types this includes all access links. Assumes registers are * reserved. - * + * * @param type * the type of this reference * @param addr @@ -316,7 +316,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { * Decrements the reference count to a type if it is a reference type. For * function types this includes all access links. Assumes registers are * reserved. - * + * * @param type * the type of this reference * @param addr @@ -350,7 +350,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { /** * Decrements all reference-type variables of a function. Must be called at * the end of a function declaration. Assumes registers are reserved. - * + * * @param scope * the function scope with all relevant variables */ @@ -641,7 +641,7 @@ public class BoppiGenerator extends BoppiBaseVisitor { * Loads the value on the address pointed to by the variable context. * Because the array length is not stored by itself, it will be calculated * here from the size property. - * + * * @see BoppiBaseVisitor#visitGetVariable(GetVariableContext) */ @Override diff --git a/src/pp/s1184725/boppi/util/RegisterPool.java b/src/pp/s1184725/boppi/RegisterPool.java similarity index 98% rename from src/pp/s1184725/boppi/util/RegisterPool.java rename to src/pp/s1184725/boppi/RegisterPool.java index 303d92d..2523460 100644 --- a/src/pp/s1184725/boppi/util/RegisterPool.java +++ b/src/pp/s1184725/boppi/RegisterPool.java @@ -1,4 +1,4 @@ -package pp.s1184725.boppi.util; +package pp.s1184725.boppi; import java.util.*; import java.util.function.*; @@ -19,13 +19,13 @@ public class RegisterPool { * Register that must have a value of 0. */ public static final Reg ZERO = new Reg("r_nul"); - + private Logger logger; private List regFree, regInUse; /** * Creates a new register pool - * + * * @param logger * the logger to use */ @@ -48,7 +48,7 @@ public class RegisterPool { /** * Blocks two registers while running some code. - * + * * @param * the return type of the code * @param r1 @@ -65,7 +65,7 @@ public class RegisterPool { /** * Blocks a register while running some code. - * + * * @param r1 * the register to reserve * @param code @@ -79,7 +79,7 @@ public class RegisterPool { /** * Blocks a register while running some code. - * + * * @param * the return type of the code * @param r1 @@ -108,7 +108,7 @@ public class RegisterPool { /** * Returns the list of registers currently in use. - * + * * @return the list of registers in use */ public List getInUse() { @@ -136,7 +136,7 @@ public class RegisterPool { /** * Runs some code with two registers allocated. - * + * * @param code * the code to run * @return the second register used in the code @@ -147,7 +147,7 @@ public class RegisterPool { /** * Runs some code with a single register allocated. - * + * * @param code * the code to run * @return the register used in the code diff --git a/src/pp/s1184725/boppi/antlr/package-info.java b/src/pp/s1184725/boppi/antlr/package-info.java new file mode 100644 index 0000000..f75eb5f --- /dev/null +++ b/src/pp/s1184725/boppi/antlr/package-info.java @@ -0,0 +1,5 @@ +/** + * This package contains the Boppi language grammar and, after ANTLR + * generation, the lexer, parser and visitor classes. + */ +package pp.s1184725.boppi.antlr; diff --git a/src/pp/s1184725/boppi/exception/SymbolTableException.java b/src/pp/s1184725/boppi/exception/SymbolTableException.java index a96d2f6..9abd815 100644 --- a/src/pp/s1184725/boppi/exception/SymbolTableException.java +++ b/src/pp/s1184725/boppi/exception/SymbolTableException.java @@ -1,6 +1,6 @@ package pp.s1184725.boppi.exception; -import pp.s1184725.boppi.CachingSymbolTable; +import pp.s1184725.boppi.symboltable.CachingSymbolTable; /** * Exceptions that may be thrown by the {@link CachingSymbolTable}. diff --git a/src/pp/s1184725/boppi/exception/package-info.java b/src/pp/s1184725/boppi/exception/package-info.java new file mode 100644 index 0000000..e146496 --- /dev/null +++ b/src/pp/s1184725/boppi/exception/package-info.java @@ -0,0 +1,5 @@ +/** + * This package contains custom exceptions for + * {@link pp.s1184725.boppi.CachingSymbolTable}. + */ +package pp.s1184725.boppi.exception; diff --git a/src/pp/s1184725/boppi/package-info.java b/src/pp/s1184725/boppi/package-info.java new file mode 100644 index 0000000..abab314 --- /dev/null +++ b/src/pp/s1184725/boppi/package-info.java @@ -0,0 +1,5 @@ +/** + * This package contains the main Boppi classes: a checker, generator, + * toolchain and supporting classes. + */ +package pp.s1184725.boppi; diff --git a/src/pp/s1184725/boppi/CachingSymbolTable.java b/src/pp/s1184725/boppi/symboltable/CachingSymbolTable.java similarity index 98% rename from src/pp/s1184725/boppi/CachingSymbolTable.java rename to src/pp/s1184725/boppi/symboltable/CachingSymbolTable.java index 15e830f..1a93c0b 100644 --- a/src/pp/s1184725/boppi/CachingSymbolTable.java +++ b/src/pp/s1184725/boppi/symboltable/CachingSymbolTable.java @@ -1,8 +1,9 @@ -package pp.s1184725.boppi; +package pp.s1184725.boppi.symboltable; import java.util.*; import java.util.function.Supplier; +import pp.s1184725.boppi.Messages; import pp.s1184725.boppi.exception.*; import pp.s1184725.boppi.type.*; @@ -15,7 +16,7 @@ import pp.s1184725.boppi.type.*; * * @param * the typing class - * + * * @author Frank Wibbelink */ public class CachingSymbolTable { @@ -36,9 +37,9 @@ public class CachingSymbolTable { * Closes a lexical scope for a function declaration, removing all * declarations within this scope, restoring the local offset and decreasing * the lookup depth. - * + * * @return function scope details - * + * * @throws NoProgramException * if no scope is open */ @@ -51,7 +52,7 @@ public class CachingSymbolTable { * Closes a lexical scope, removing all declarations within this scope. Runs * in {@code O(k log k log n)} time, with {@code k} the number of * identifiers going out of scope, when the stream is parallelized. - * + * * @throws NoProgramException * if no scope is open */ @@ -69,7 +70,7 @@ public class CachingSymbolTable { /** * Returns the type of an identifier, if any. - * + * * @param id * the name of the identifier * @return the type of the identifier @@ -87,7 +88,7 @@ public class CachingSymbolTable { /** * Returns the current function scope, if any. - * + * * @return the current function scope * @throws NoProgramException * if no scope is open @@ -101,7 +102,7 @@ public class CachingSymbolTable { /** * Returns whether the given identifier is declared. - * + * * @param id * the name of the identifier * @return true if the identifier has a declared type @@ -136,7 +137,7 @@ public class CachingSymbolTable { * and returns the newly made variable instance that belongs to this * identifier. Throws an exception if the identifier is declared in this * scope already. - * + * * @param id * the name of the identifier * @param type @@ -166,7 +167,7 @@ public class CachingSymbolTable { * helper method to make sure calls to {@link #openFunctionScope()} and * {@link #closeFunctionScope()} are balanced and returns the function scope * details. {@code code} must take no arguments and must not return a value. - * + * * @param code * the code to execute within the scope * @return the function scope details @@ -187,7 +188,7 @@ public class CachingSymbolTable { * helper method to make sure calls to {@link #openScope()} and * {@link #closeScope()} are balanced. {@code code} must take no arguments * and must return a value. - * + * * @param * the type of the code's return value * @param code diff --git a/src/pp/s1184725/boppi/DebugCachingSymbolTable.java b/src/pp/s1184725/boppi/symboltable/DebugCachingSymbolTable.java similarity index 96% rename from src/pp/s1184725/boppi/DebugCachingSymbolTable.java rename to src/pp/s1184725/boppi/symboltable/DebugCachingSymbolTable.java index fc5cbad..6469082 100644 --- a/src/pp/s1184725/boppi/DebugCachingSymbolTable.java +++ b/src/pp/s1184725/boppi/symboltable/DebugCachingSymbolTable.java @@ -1,4 +1,4 @@ -package pp.s1184725.boppi; +package pp.s1184725.boppi.symboltable; import pp.s1184725.boppi.exception.*; import pp.s1184725.boppi.type.*; @@ -9,7 +9,7 @@ import pp.s1184725.boppi.type.*; * * @param * the typing class - * + * * @author Frank Wibbelink */ public class DebugCachingSymbolTable extends CachingSymbolTable { diff --git a/src/pp/s1184725/boppi/FunctionScope.java b/src/pp/s1184725/boppi/symboltable/FunctionScope.java similarity index 96% rename from src/pp/s1184725/boppi/FunctionScope.java rename to src/pp/s1184725/boppi/symboltable/FunctionScope.java index cdaa90e..ab12ce8 100644 --- a/src/pp/s1184725/boppi/FunctionScope.java +++ b/src/pp/s1184725/boppi/symboltable/FunctionScope.java @@ -1,4 +1,4 @@ -package pp.s1184725.boppi; +package pp.s1184725.boppi.symboltable; import java.util.*; import java.util.stream.Collectors; @@ -7,9 +7,9 @@ import pp.s1184725.boppi.type.Type; /** * Keeps track of local data for functions. - * + * * @author Frank Wibbelink - * + * * @param * the type system to use */ @@ -19,7 +19,7 @@ public class FunctionScope { /** * Creates a function scope with the given depth. - * + * * @param depth * the lexical depth of this function */ @@ -30,7 +30,7 @@ public class FunctionScope { /** * Creates a new variable in this function scope and returns it. - * + * * @param type * the type of variable to create * @return the variable created @@ -44,7 +44,7 @@ public class FunctionScope { /** * Returns the current lexical scope/function depth. - * + * * @return the function depth */ public int getFunctionDepth() { @@ -56,7 +56,7 @@ public class FunctionScope { * in the current function scope. Note that this is the size calculated up * and including the last variable declaration, so make sure to call it * right before leaving a function scope. - * + * * @return the number of bytes required to store all variables local to a * function */ @@ -66,7 +66,7 @@ public class FunctionScope { /** * Returns the variables local to this function scope. - * + * * @return a list of variables */ public List> getVars() { diff --git a/src/pp/s1184725/boppi/Variable.java b/src/pp/s1184725/boppi/symboltable/Variable.java similarity index 96% rename from src/pp/s1184725/boppi/Variable.java rename to src/pp/s1184725/boppi/symboltable/Variable.java index 783e2f8..f47763b 100644 --- a/src/pp/s1184725/boppi/Variable.java +++ b/src/pp/s1184725/boppi/symboltable/Variable.java @@ -1,4 +1,4 @@ -package pp.s1184725.boppi; +package pp.s1184725.boppi.symboltable; /** * This class tracks a variable's properties. It is agnostic of the type system @@ -6,7 +6,7 @@ package pp.s1184725.boppi; * * @param * the typing class - * + * * @author Frank Wibbelink */ public class Variable { @@ -18,7 +18,7 @@ public class Variable { /** * Creates a variable with the given type instance and memory offset. - * + * * @param type * the type of the variable * @param depth @@ -43,7 +43,7 @@ public class Variable { /** * Gets the lexical depth of this variable instance. - * + * * @return the depth */ public int getDepth() { @@ -52,7 +52,7 @@ public class Variable { /** * Gets the local data offset of this variable instance. - * + * * @return the offset */ public int getOffset() { @@ -61,7 +61,7 @@ public class Variable { /** * Gets the type of this variable instance. - * + * * @return the type */ public T getType() { @@ -70,7 +70,7 @@ public class Variable { /** * Returns whether this variable has been assigned. - * + * * @return whether this variable has been assigned */ public boolean isAssigned() { @@ -79,7 +79,7 @@ public class Variable { /** * Returns whether this variable is a constant. - * + * * @return whether this variable is a constant */ public boolean isConstant() { @@ -88,7 +88,7 @@ public class Variable { /** * Returns whether this variable has been assigned conditionally. - * + * * @return whether this variable has been assigned conditionally */ public boolean isPossiblyAssigned() { @@ -104,7 +104,7 @@ public class Variable { /** * Sets the const-ness of this variable. - * + * * @param constant * whether this variable is a constant */ diff --git a/src/pp/s1184725/boppi/symboltable/package-info.java b/src/pp/s1184725/boppi/symboltable/package-info.java new file mode 100644 index 0000000..b4865dc --- /dev/null +++ b/src/pp/s1184725/boppi/symboltable/package-info.java @@ -0,0 +1,4 @@ +/** + * This package contains a symbol table implementation. + */ +package pp.s1184725.boppi.symboltable; diff --git a/src/pp/s1184725/boppi/test/ArrayTest.java b/src/pp/s1184725/boppi/test/ArrayTest.java index 8fa4c41..97adf42 100644 --- a/src/pp/s1184725/boppi/test/ArrayTest.java +++ b/src/pp/s1184725/boppi/test/ArrayTest.java @@ -6,7 +6,7 @@ import org.junit.Test; /** * Test cases for fixed-size arrays. - * + * * @author Frank Wibbelink */ public class ArrayTest { @@ -127,17 +127,17 @@ public class ArrayTest { assertThat(BoppiTests.vm.getInterrupt(), is(0)); assertThat(BoppiTests.log, is(empty())); assertThat(BoppiTests.out, is(arrayContaining("1337", "13", "2", "15"))); - + BoppiTests.compileAndRunString("var int[][] m; m := [[1,3]]; m[0]; var int[] a; a := [8,9]; print(m[0][1])"); assertThat(BoppiTests.vm.getInterrupt(), is(0)); assertThat(BoppiTests.log, is(empty())); assertThat(BoppiTests.out, is(arrayContaining("3"))); - + BoppiTests.compileAndRunString("print([1,2]==[1,2],[1,2]<>[1,2],[1]==[1,2],[0]<>[2])"); assertThat(BoppiTests.vm.getInterrupt(), is(0)); assertThat(BoppiTests.log, is(empty())); assertThat(BoppiTests.out, is(arrayContaining("true", "false", "false", "true"))); - + BoppiTests.compileAndRunFile("programs/arrayFunctions.boppi", "Hello"); assertThat(BoppiTests.vm.getInterrupt(), is(0)); assertThat(BoppiTests.log, is(empty())); diff --git a/src/pp/s1184725/boppi/test/ExpressionTest.java b/src/pp/s1184725/boppi/test/ExpressionTest.java index 1f07515..1767240 100644 --- a/src/pp/s1184725/boppi/test/ExpressionTest.java +++ b/src/pp/s1184725/boppi/test/ExpressionTest.java @@ -8,11 +8,11 @@ import java.util.logging.Level; import org.apache.commons.lang3.StringUtils; import org.junit.Test; -import pp.s1184725.boppi.util.RegisterPool; +import pp.s1184725.boppi.RegisterPool; /** * Test cases for the basic expression language. - * + * * @author Frank Wibbelink */ public class ExpressionTest { @@ -63,6 +63,7 @@ public class ExpressionTest { public void wrongExpressionChecking() { BoppiTests.checkString("+true"); assertThat(BoppiTests.log, hasSize(1)); + BoppiTests.printOutput(); BoppiTests.checkString("5 || true"); assertThat(BoppiTests.log, hasSize(1)); @@ -85,6 +86,7 @@ public class ExpressionTest { BoppiTests.compileAndRunString("print(5*3)"); assertThat(BoppiTests.log, is(empty())); assertThat(BoppiTests.out, is(arrayContaining("15"))); + BoppiTests.printOutput(); BoppiTests.compileAndRunString("print('T', 'e', 's', 't', '!')"); assertThat(BoppiTests.log, is(empty())); diff --git a/src/pp/s1184725/boppi/test/package-info.java b/src/pp/s1184725/boppi/test/package-info.java new file mode 100644 index 0000000..3baa265 --- /dev/null +++ b/src/pp/s1184725/boppi/test/package-info.java @@ -0,0 +1,4 @@ +/** + * This package contains JUnit tests for Boppi features and memlib. + */ +package pp.s1184725.boppi.test; diff --git a/src/pp/s1184725/boppi/type/package-info.java b/src/pp/s1184725/boppi/type/package-info.java new file mode 100644 index 0000000..6d15a8d --- /dev/null +++ b/src/pp/s1184725/boppi/type/package-info.java @@ -0,0 +1,5 @@ +/** + * This package contains the types in Boppi. Type unification and checking is + * currently performed in {@link pp.s1184725.boppi.BoppiChecker}. + */ +package pp.s1184725.boppi.type; diff --git a/src/pp/s1184725/boppi/util/package-info.java b/src/pp/s1184725/boppi/util/package-info.java new file mode 100644 index 0000000..7dcc4c5 --- /dev/null +++ b/src/pp/s1184725/boppi/util/package-info.java @@ -0,0 +1,5 @@ +/** + * This package provides a command-line interface for compiling Boppi code + * and a rudimentary REPL. + */ +package pp.s1184725.boppi.util;