From d777b45166c715b75a7f9dcfb2eaf5a24f5e79ba Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Tue, 9 Nov 2021 14:48:06 +0100 Subject: [PATCH] constructor type fix --- src/main/scala/hb/dhbw/Parser.scala | 2 +- src/main/scala/hb/dhbw/TYPE.scala | 3 ++- src/test/scala/IntegrationTest.scala | 6 ++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/scala/hb/dhbw/Parser.scala b/src/main/scala/hb/dhbw/Parser.scala index dc8fc01..cf6b382 100644 --- a/src/main/scala/hb/dhbw/Parser.scala +++ b/src/main/scala/hb/dhbw/Parser.scala @@ -24,7 +24,7 @@ object Parser { def methodCall[_: P]: P[MethodCall] =P( ident ~ paramList ).map(ite => MethodCall(null, ite._1, ite._2) ) def paramList[_: P] : P[List[Expr]]= P("(".! ~ (expr ~ (",".! ~ expr).rep.map(_.toList.map{_._2})).? ~ ")".! ).map(ite => ite._2.map(params => params._1 :: params._2).getOrElse(List.empty)) def variable[_: P]: P[Expr] = P(ident).map(LocalVar) - def expr[_: P]: P[Expr] = constructor | P( variable ~ (prefixMethodCall | fieldVar).rep.map(_.toList) ) + def expr[_: P]: P[Expr] = P( (variable | constructor)~ (prefixMethodCall | fieldVar).rep.map(_.toList) ) .map(ite => ite._2.foldLeft(ite._1) { (e1 : Expr, e2 : Expr) => e2 match{ case MethodCall(_, name, e3) => MethodCall(e1, name, e3) diff --git a/src/main/scala/hb/dhbw/TYPE.scala b/src/main/scala/hb/dhbw/TYPE.scala index ac4b042..2f389a9 100644 --- a/src/main/scala/hb/dhbw/TYPE.scala +++ b/src/main/scala/hb/dhbw/TYPE.scala @@ -78,7 +78,8 @@ object TYPE { val genericReplace = new GenericTypeReplaceMonad(this) val es = params.map(ex => TYPEExpr(ex, localVars, ast)) val cl = findClasses(className, ast) - val retCons = es.flatMap(_._2.map(genericReplace.replaceGenerics(_))) ++ + val paramCons = cl.fields.map(_._1).zip(es.map(_._1)).map(pair => genericReplace.replaceGenerics(LessDot(pair._2, pair._1))) + val retCons = paramCons ++ es.flatMap(_._2.map(genericReplace.replaceGenerics(_))) ++ cl.genericParams.map(gp => LessDot(genericReplace.replaceGenerics(gp._1), genericReplace.replaceGenerics(gp._2))) (RefType(className, cl.genericParams.map(_._1).map(genericReplace.replaceGenerics(_))), retCons) } diff --git a/src/test/scala/IntegrationTest.scala b/src/test/scala/IntegrationTest.scala index 949bc15..c8fb1fd 100644 --- a/src/test/scala/IntegrationTest.scala +++ b/src/test/scala/IntegrationTest.scala @@ -52,4 +52,10 @@ class IntegrationTest extends FunSuite { val result = FJTypeinference.typeinference(input ) println(result.map(Main.prettyPrint(_))) } + + test("fieldVar access"){ + val input ="class List extends Object{\nA f;\nget(){ return this.f; }\n}\n\nclass Test2 extends Object{\nget(){ return new List(this).get();}\n}" + val result = FJTypeinference.typeinference(input ) + println(result.map(Main.prettyPrint(_))) + } }