diff --git a/script.sh b/script.sh new file mode 100755 index 0000000..e3fd1df --- /dev/null +++ b/script.sh @@ -0,0 +1,687 @@ +#!/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 + diff --git a/sources/zweic/Analyzer.scala b/sources/zweic/Analyzer.scala index a4dc9c8..9e6d8ac 100755 --- a/sources/zweic/Analyzer.scala +++ b/sources/zweic/Analyzer.scala @@ -48,6 +48,10 @@ case Some(c) => Report.error(tree.pos, "Class already defined: " + name); case None => + classScope.get(extend.get.name) match { + case None => Report.error(tree.pos, "Superclass not defined: " + extend.get.name) + case _ => () + } val cs = ClassSymbol(tree.pos, name.name, classScope.get(extend.get.name)); classScope = classScope + name.name -> cs; //classScope(name.name) = cs; @@ -64,7 +68,7 @@ case Some(v) => Report.error(tree.pos, "Class variable already defined: " + name); case None => - name.sym= FieldSymbol(tree.pos, name.name, analyzeType(typ)); + name.sym = FieldSymbol(tree.pos, name.name, analyzeType(typ)); ownerClass.enterField(name.sym.asInstanceOf[FieldSymbol]); } case MethodDef(name, args, rtype, expr) => @@ -72,7 +76,10 @@ myVarScope = myVarScope + "this" -> VarSymbol(ownerClass.pos, "this", IClassType(ownerClass)); - for ( val x <- ownerClass.allFields ) myVarScope = myVarScope + x.name -> x.asInstanceOf[VarSymbol]; + for ( val x <- ownerClass.allFields ) { + Console.println(x); + myVarScope = myVarScope + x.name -> x.asInstanceOf[VarSymbol]; + } val paramtypes = for ( val f <- args ) yield { //TODO: check that there isn't already a class field with the same name @@ -277,7 +284,7 @@ * to the found type. */ private def checkSametype(pos: Int, found: Type, expected: Type): Unit = - if (!found.isSametype(expected)) + if (!found.isSametype(expected)) error(pos, "Type mismatch", found, expected); /** @@ -299,6 +306,6 @@ */ private def error(pos: Int, header: String, found: Type, expected: Type) = Report.error(pos, header + "\nexpected type: " + expected + "\n" + - "actual type : " + found); + "found type : " + found); } diff --git a/sources/zweic/Type.scala b/sources/zweic/Type.scala index 3633a29..a6884d8 100755 --- a/sources/zweic/Type.scala +++ b/sources/zweic/Type.scala @@ -15,7 +15,7 @@ } override def toString(): String = this match { - case IClassType(c) => "ClassType"; + case IClassType(c) => "ClassType: " + c.name; case IIntType => "IntType" case INullType => "NullType" case IBadType => ""