diff --git a/script.sh b/script.sh deleted file mode 100755 index e593d4d..0000000 --- a/script.sh +++ /dev/null @@ -1,697 +0,0 @@ -#!/bin/sh - -COMMAND="scala -cp classes zweic.AnalyzerTest" - -SEPARATOR="echo ==========================================================================================================" - -echo " -This wicked cool script will test your Zwei compiler for conformity to expected behavior. -" - -file="tests/4/Factorial.zwei" - -$SEPARATOR -echo " -Testing file $file. No errors should be returned. -" - -$COMMAND $file - -$SEPARATOR - -#################################################################################### - -$SEPARATOR -echo "Class typing" -$SEPARATOR - -file="tests/4/01.zwei" - -echo "Testing file $file : - Error in file : undeclared superclass \"b\" (line 1) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/02.zwei" - -echo "Testing file $file : - Error in file : class \"a\" already declared (line 3) -" - -$COMMAND $file - -$SEPARATOR - -#################################################################################### - -$SEPARATOR -echo "Supertypes" -$SEPARATOR - -file="tests/4/03.zwei" - -echo "Testing file $file : - Error in file : illegal call of method \"equal\" : arguments have no common supertype (line 13) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/04.zwei" - -echo "Testing file $file : - No error in file : arguments have common supertype -" - -$COMMAND $file - -$SEPARATOR - -#################################################################################### - -$SEPARATOR -echo "Class typing, courtesy M. Ganguin & J. Ruffin" -$SEPARATOR - -file="tests/4/class00-1.zwei" - -echo "Testing file $file : - Error in file : undeclared superclass \"unknown\" (line 1) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/class00-2.zwei" - -echo "Testing file $file : - Error in file : class \"ClassSymbolTest\" already declared (line 3) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/class00-3.zwei" - -echo "Testing file $file : - Error in file : type \"RANDOM\" is unknown (class hasn't been declared) (line 3) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/class00-4.zwei" - -echo "Testing file $file : - Error in file : undeclared identifier \"NONE\" (line 3) -" - -$COMMAND $file - -$SEPARATOR - -#################################################################################### - -$SEPARATOR -echo "Field typing, courtesy M. Ganguin & J. Ruffin" -$SEPARATOR - -file="tests/4/field00-1.zwei" - -echo "Testing file $file : - Error in file : type \"UNKNOWN\" is unknown (line 1) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/field00-2.zwei" - -echo "Testing file $file : - Recursive typing in classes (no error should be returned) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/field00-3.zwei" - -echo "Testing file $file : - Error in file : field \"a\" already declared (line 3) -" - -$COMMAND $file - -$SEPARATOR - - -#################################################################################### - -$SEPARATOR -echo "Method typing, courtesy M. Ganguin & J. Ruffin" -$SEPARATOR - -file="tests/4/method00-1.zwei" - -echo "Testing file $file : - Error in file : return type \"UNKNOWN\" is unknown (line 4) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/method00-2.zwei" - -echo "Testing file $file : - Error in file : argument type \"UNKNOWN\" is unknown (line 2) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/method00-3.zwei" - -echo "Testing file $file : - Error in file : Overriding method \"overrideTest\" in class \"MethodTest\" has bad return type. (line 14) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/method00-4.zwei" - -echo "Testing file $file : - Error in file : Overriding method \"overrideTest\" in class \"MethodTest\" has bad argument type. (line 11) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/method00-5.zwei" - -echo "Testing file $file : - Error in file : Method \"returnExpressionTypeTest\" does not return correct type. (line 8) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/method00-6.zwei" - -echo "Testing file $file : - Should return no errors : returning container class -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/method00-7.zwei" - -echo "Testing file $file : - Should return no errors : overriding and polymorphism -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/method00-8.zwei" - -echo "Testing file $file : - Should return no errors : testing variable scope -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/method00-9.zwei" - -echo "Testing file $file : - Should return no errors : checking method return types -" - -$COMMAND $file - -$SEPARATOR - -#################################################################################### - -$SEPARATOR -echo "Program typing, courtesy M. Ganguin & J. Ruffin" -$SEPARATOR - -file="tests/4/program00-1.zwei" - -echo "Testing file $file : - Error in file : type \"RANDOM\" is unknown (line 1) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/program00-2.zwei" - -echo "Testing file $file : - Error in file : undeclared identifier \"k\" (line 3) -" - -$COMMAND $file - -$SEPARATOR - -#################################################################################### - -$SEPARATOR -echo "Statement typing, courtesy M. Ganguin & J. Ruffin" -$SEPARATOR - -file="tests/4/statement-while.zwei" - -echo "Testing file $file : - Errors in file : * type mismatch (expected : Int, found : null) in condition (line 6) - * type mismatch (expected : Int, found : null) in statement (line 7) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/statement-var.zwei" - -echo "Testing file $file : - Errors in file : * type \"nonexistent\" is unknown (line 8) - * undeclared class \"nonexistent\" (line 8) - * type mismatch (expected : Int, found : null) for return type (line 10) - * type mismatch (expected : Int, found : null) for return type (line 11) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/statement-set.zwei" - -echo "Testing file $file : - Errors in file : * undeclared identifier \"s\" (line 10) - * type mismatch (expected : Int, found : foo) (line 11) - * type mismatch (expected : bar, found : foo) (line 12) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/statement-do.zwei" - -echo "Testing file $file : - Errors in file : * type mismatch (expected : Int, found : null) (line 5) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/statement-printInt.zwei" - -echo "Testing file $file : - Errors in file : * type mismatch (expected : Int, found : foo) (line 7) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/statement-printChar.zwei" - -echo "Testing file $file : - Errors in file : * type mismatch (expected : Int, found : foo) (line 7) -" - -$COMMAND $file - -$SEPARATOR - -#################################################################################### - -$SEPARATOR -echo "Term typing, courtesy M. Ganguin & J. Ruffin" -$SEPARATOR - -file="tests/4/term-ident.zwei" - -echo "Testing file $file : - Error in file : undeclared identifier \"y\" (line 6) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/term-select.zwei" - -echo "Testing file $file : - Errors in file : * undeclared identifier \"k\" (line 16) - * undeclared field \"x\" in class \"foo\" (line 18) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/term-call.zwei" - -echo "Testing file $file : - Errors in file : * undeclared identifier \"k\" (line 20) - * undeclared method \"gneuh\" in class \"foo\" (line 21) - * illegal call of method \"works\" (wrong number of arguments) (line 22) - * illegal call of method \"works\" (argument types do not match) (line 23) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/term-new.zwei" - -echo "Testing file $file : - Errors in file : * undeclared class \"unknown\" (line 17) - * illegal use of class constructor (wrong number of arguments) (line 18) - * illegal use of class constructor (argument types do not match) (line 19) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/term-unop.zwei" - -echo "Testing file $file : - Errors in file : * undeclared identifier \"y\" (line 6) - * type mismatch (unop can only be applied to Int) (line 7) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/term-binop.zwei" - -echo "Testing file $file : - Errors in file : * undeclared identifier \"k\" (line 7) - * undeclared identifier \"l\" (line 8) - * type mismatch (binop can only be applied to two Ints) (line 9) - * type mismatch (binop can only be applied to two Ints) (line 10) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/term-objComp.zwei" - -echo "Testing file $file : - Errors in file : * two type mismatches (neither opr�rand is an Int) (line 18) - * undeclared identifier \"k\" (line 19) - * undeclared identifier \"l\" (line 20) - * type mismatch (expected : foo, found : bar) (line 21) - * type mismatch (expected : bar, found : foo) (line 22) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/term-if.zwei" - -echo "Testing file $file : - Errors in file : * undeclared identifier \"k\" (line 21) - * condition in \"if\" is not an Int (line 22) - * unknown type \"gorh\" (line 23) - * unknown type \"gorh\" (line 24) - * return value types in \"if ... else\" bock do not have a common supertype (lines 24, 25, 26) - * type mismatch in \"if\" block (expected : bof, found : bar) (line 27) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/term-block.zwei" - -echo "Testing file $file : - Errors in file : * type mismatch (expected return type : Int, found : null) (line 24) - * undeclared identifier \"y\" (outside of block scope) (line 29) -" - -$COMMAND $file - -$SEPARATOR - -#################################################################################### - -$SEPARATOR -echo "Various test files, courtesy R. Tagliani" -$SEPARATOR - -file="tests/4/bionInt.zwei" - -echo "Testing file $file : - No errors in file -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/blockvar.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/classes.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/classes0.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/expr_if.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/expr_readChar.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/expr_readInt.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/functions.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/new.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/null.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/printChar.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/printInt.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/select.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/unop.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/unopa.zwei" - -echo "Testing file $file : - No errors in file -" - -file="tests/4/blockvar_2xerror0.zwei" - -echo "Testing file $file : - Errors in file : * undeclared identifier \"y\" (line 3) - * undeclared identifier \"z\" (line 4) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/blockvar_error0.zwei" - -echo "Testing file $file : - Errors in file : * undeclared identifier \"z\" (line 3) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/blockvar_error1.zwei" - -echo "Testing file $file : - Errors in file : * variable \"x\" already declared (line 3) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/classes_error0.zwei" - -echo "Testing file $file : - Errors in file : * class \"C1\" already declared (line 2) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/classes_error1.zwei" - -echo "Testing file $file : - Errors in file : * undeclared superclass \"C1\" (line 2) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/classes_error2.zwei" - -echo "Testing file $file : - Errors in file : * field \"a\" already declared (line 3) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/functions_error0.zwei" - -echo "Testing file $file : - Errors in file : * method \"f1\" does not return correct type (line 2) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/functions_error1.zwei" - -echo "Testing file $file : - Errors in file : * method \"f2\" does not return correct type (line 3) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/new_error0.zwei" - -echo "Testing file $file : - Errors in file : * illegal use of class constructor : argument types do not match (line 6) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/new_error0.zwei" - -echo "Testing file $file : - Errors in file : * illegal use of class constructor : wrong number of parameters (line 6) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/printChar_error0.zwei" - -echo "Testing file $file : - Errors in file : * program starts with block (line 0) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/unopa_error1.zwei" - -echo "Testing file $file : - Errors in file : * undeclared identifier \"b\" (line 3) -" - -$COMMAND $file - -$SEPARATOR - -file="tests/4/paramredef.zwei" - -echo "Testing file $file : - Errors in file : * parameter defined twice -" - -$COMMAND $file - -$SEPARATOR - diff --git a/tests/5/arith.zwei b/tests/5/arith.zwei index a8270eb..b075d97 100644 --- a/tests/5/arith.zwei +++ b/tests/5/arith.zwei @@ -1 +1,5 @@ --(3*5)+2-(-1)/(5%2) \ No newline at end of file +//#isut +{ +//#-12 +printInt (-(3*5)+2-(-1)/(5%2)); +} \ No newline at end of file diff --git a/tests/5/cond_not.zwei b/tests/5/cond_not.zwei new file mode 100644 index 0000000..33d8850 --- /dev/null +++ b/tests/5/cond_not.zwei @@ -0,0 +1,15 @@ +//#isunittest + +{ +//#1 +printInt(1); + +//#0\n +printInt(!1); +printChar(10); + +//#01 +printInt(0); +printInt(!0); + +} \ No newline at end of file diff --git a/tests/5/empty.zwei b/tests/5/empty.zwei index 573541a..6e98df8 100644 --- a/tests/5/empty.zwei +++ b/tests/5/empty.zwei @@ -1 +1,2 @@ +//#isut 0 diff --git a/tests/5/if.zwei b/tests/5/if.zwei index 4e19c24..caca64d 100644 --- a/tests/5/if.zwei +++ b/tests/5/if.zwei @@ -1 +1,10 @@ -if ( 1 + (2/9) ) 3 % 5 else (4 -7) * 5 +//#isut +{ +printInt ( +if ( 1 + (2/9) ) + //#3 + 3 % 5 +else + (4 -7) * 5 +); +} \ No newline at end of file diff --git a/tests/5/int.zwei b/tests/5/int.zwei new file mode 100644 index 0000000..ea57153 --- /dev/null +++ b/tests/5/int.zwei @@ -0,0 +1,6 @@ +//#isut +//#65536 +{ +Int a = 65536; +printInt(a); +} diff --git a/tests/5/start.zwei b/tests/5/start.zwei index c95789e..ed1ace8 100644 --- a/tests/5/start.zwei +++ b/tests/5/start.zwei @@ -1 +1,3 @@ -readChar \ No newline at end of file +{ +readChar; +} \ No newline at end of file diff --git a/unittest.py b/unittest.py new file mode 100644 index 0000000..a990f08 --- /dev/null +++ b/unittest.py @@ -0,0 +1,127 @@ +#usage: python check.py --help +COMPILER = "scala -cp classes zweic.GeneratorTest" +RISC = "java -cp risc/lib/risc.jar risc.emulator.Main" +OUTDIR = "asm" + +import sys, os, re +from optparse import OptionParser + +rx_isut = re.compile(r'//[^#]*#' \ + 'is(?:ut|unittest)', re.I) + +rx_tl = re.compile(r'//[^#]*#' \ + r'(?!is(?:ut|unittest))' \ + r'(.*)', re.I) + +class Checker: + def __init__(self, spath, options): + self.isUT = False + + self.fname = os.path.splitext(os.path.basename(spath))[0] + self.spath = spath + self.options = options + + self.compile() + + solution = self.parseUnitTest(self.spath) + + if self.isUT: + self.check(solution) + + print + + def compile(self): + print '> compiling %s' % self.fname, + + # call compiler via internal shell and redirect output to file + pipes = os.popen3(r'%s %s > %s' % + (self.options.compiler, self.spath, + os.path.join(self.options.outdir, self.fname+'.asm'))) + + errs = pipes[2].read() + print '.' + if errs: + print >>sys.stderr, errs + sys.exit(1) + + + def check(self, solution): + # call risc emu via internal shell and pipe output + pipes = os.popen3(r'%s asm/%s.asm' % (self.options.risc, self.fname)) + errs = pipes[2].read() + if errs: + print >>sys.stderr, errs + sys.exit(2) + answer = pipes[1].read() + + if solution == answer: + print "> checked: ok" + else: + self.error(solution, answer) + + + def parseUnitTest(self, path): + ret = [] + for line in open(path): + if rx_isut.search(line): + #this is a file containing meta information + self.isUT = True + + match = rx_tl.search(line) + if self.isUT and match: + #replace newlines + f = match.group(1).replace(r'\n','\n') + ret.append(f) + + return "".join(ret) + + + def error(self, expected, found): + # error message for broken files + expected = expected.split('\n') + found = found.split('\n') + + print >>sys.stderr, "* '%s' broken: " % self.fname + print >>sys.stderr, "%-50s %s" % ("expected", "found") + for e, f in xzip(expected, found): + print >>sys.stderr, "%-50s %s" % (e, f) + + +def xzip(l1, l2): + for x in range(max(len(l1), len(l2))): + e = '' + f = '' + if len(l1) > x: + e = l1[x] + if len(l2) > x: + f = l2[x] + yield e, f + + +if __name__ == '__main__': + optpar = OptionParser() + optpar.add_option( + "-c", "--compiler", dest="compiler", + help="Command to execute the compiler, default: '%s'" % COMPILER, + default=COMPILER, metavar="COMPILER") + + optpar.add_option( + "-a", "--risc", dest="risc", + help="Command to execute the risc emulator, default: '%s'" % RISC, + default=RISC, metavar="RISC") + + optpar.add_option( + "-o", "--outdir", dest="outdir", + help="Path where to store compiled sources, default: '%s'" % OUTDIR, + default=OUTDIR, metavar="OUTDIR") + + (options, args) = optpar.parse_args() + + try: + os.makedirs(options.outdir) + except Exception, (errno, strerror): + if not errno == 17: + print >>sys.stderr, strerror + for source in args: + Checker(source, options) +