diff options
author | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2005-11-28 17:40:44 +0000 |
---|---|---|
committer | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2005-11-28 17:40:44 +0000 |
commit | 990c85f22f23b4a6745d2ed5c061e8767c058684 (patch) | |
tree | ec917971963cbc3030f1f86ca196a5189628bc97 /sources | |
parent | 83e70dd503cfd958cd6783df1f48ae8b98f31757 (diff) | |
download | scala-990c85f22f23b4a6745d2ed5c061e8767c058684.tar.gz scala-990c85f22f23b4a6745d2ed5c061e8767c058684.tar.bz2 scala-990c85f22f23b4a6745d2ed5c061e8767c058684.zip |
A simple version of Codification framework that...
A simple version of Codification framework that has the following
limitations:
- it does not provide types which are too difficult to get.
- it only covers simple code: no if, no matching, no class or object definitions etc.
- it is only applied to typed code functions.
** This is a temporary version for Fatemeh to work on **
Diffstat (limited to 'sources')
-rwxr-xr-x | sources/scala/reflect/Code.scala | 2 | ||||
-rw-r--r-- | sources/scala/reflect/Print.scala | 111 | ||||
-rwxr-xr-x | sources/scala/reflect/Type.scala | 11 | ||||
-rwxr-xr-x | sources/scala/tools/nsc/typechecker/Codification.scala | 38 |
4 files changed, 146 insertions, 16 deletions
diff --git a/sources/scala/reflect/Code.scala b/sources/scala/reflect/Code.scala index 203ff946c5..ad92e4f581 100755 --- a/sources/scala/reflect/Code.scala +++ b/sources/scala/reflect/Code.scala @@ -16,4 +16,4 @@ case class Literal(value: Any) extends Code; case class Apply(fun: Code, args: List[Code]) extends Code; case class TypeApply(fun: Code, args: List[Type]) extends Code; case class Function(params: List[Symbol], body: Code) extends Code; - +case class This(sym: Symbol) extends Code; diff --git a/sources/scala/reflect/Print.scala b/sources/scala/reflect/Print.scala new file mode 100644 index 0000000000..ea22f6e825 --- /dev/null +++ b/sources/scala/reflect/Print.scala @@ -0,0 +1,111 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2004, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ +package scala.reflect; + +object Print extends Function1[Any, String] { + + def apply (any: Any): String = { + if (any.isInstanceOf[TypedCode[Any]]) + apply(any.asInstanceOf[TypedCode[Any]]) + else if (any.isInstanceOf[Code]) + apply(any.asInstanceOf[Code]) + else if (any.isInstanceOf[Symbol]) + apply(any.asInstanceOf[Symbol]) + else if (any.isInstanceOf[Type]) + apply(any.asInstanceOf[Type]) + else "UnknownAny" + } + + def apply (typedCode: TypedCode[Any]): String = + Print(typedCode.code); + + def apply (code: Code): String = code match { + case reflect.Ident(sym) => + "Ident (" + Print(sym) + ")" + case reflect.Select(qual, sym) => + "Select (" + Print(qual) + " from " + Print(sym) + ")" + case reflect.Literal(value) => + "Literal (" + value + ")" + case reflect.Apply(fun, args) => + "Apply (" + Print(fun) + " on " + + ((args match { + case Nil => "nothing " + case _ :: _ => args.map(Print).mkString("", ", ", "") + }):String) + ")" + case reflect.TypeApply(fun, args) => + "TypeApply (" + Print(fun) + " on " + + ((args match { + case Nil => "nothing" + case _ :: _ => args.map(Print).mkString("", ", ", "") + }):String) + ")" + case reflect.Function(params, body) => + "Function (" + + ((params match { + case Nil => "nothing" + case _ :: _ => params.map(Print).mkString("", ", ", "") + }):String) + " is " + Print(body) + ")" + case reflect.This(sym) => + "This (" + Print(sym) + ")" + case _ => "UnknownCode" + } + + def apply (symbol: Symbol): String = symbol match { + case reflect.Class(name) => + "Class (" + name + ")" + case reflect.Method(name, datatype) => + "Method (" + name + " of " + Print(datatype) + ")" + case reflect.Field(name, datatype) => + "Field (" + name + " of " + Print(datatype) + ")" + case reflect.TypeField(name, datatype) => + "TypeField (" + name + " of " + Print(datatype) + ")" + case reflect.LocalValue(owner, name, datatype) => + "LocalValue (" + name + " owned by " + Print(owner) + + " of " + Print(datatype) + ")" + case reflect.LocalMethod(owner, name, datatype) => + "LocalMethod (" + name + " owned by " + Print(owner) + + " of " + Print(datatype) + ")" + case reflect.NoSymbol => "NoSymbol" + case reflect.RootSymbol => "RootSymbol" + case _ => "UnknownSymbol" + } + + def apply (datatype: Type): String = datatype match { + case reflect.NoPrefix => "NoPrefix" + case reflect.NoType => "NoType" + case reflect.NamedType(name) => + "NamedType (" + name + ")" + case reflect.PrefixedType(prefix, symbol) => + "PrefixedType (" + Print(symbol) + " in " + Print(prefix) + ")" + case reflect.SingleType(prefix, symbol) => + "SingleType (" + Print(symbol) + " in " + Print(prefix) + ")" + case reflect.ThisType(clazz) => + "ThisType (" + Print(clazz) + ")" + case reflect.AppliedType(datatype, args) => + "AppliedType (" + Print(datatype) + " on " + + ((args match { + case Nil => "nothing" + case _ :: _ => args.map(Print).mkString("", ", ", "") + }):String) + ")" + case reflect.TypeBounds(lo, hi) => + "TypeBounds (" + Print(lo) + " to " + Print(hi) + ")" + case reflect.MethodType(formals, resultType) => + "MethodType (" + + ((formals match { + case Nil => "nothing" + case _ :: _ => formals.map(Print).mkString("", ", ", "") + }):String) + " is " + Print(resultType) + ")" + case reflect.PolyType(typeParams, typeBounds, resultType) => + "PolyType (" + (typeParams zip typeBounds).map{ + case Pair(typeParam, Pair(lo, hi)) => + Print(lo) + " < " + Print(typeParam) + " < " + Print(hi) + }.mkString("", ", ", "") + " to " + Print(resultType) + ")" + case _ => "UnknownType" + } + +}
\ No newline at end of file diff --git a/sources/scala/reflect/Type.scala b/sources/scala/reflect/Type.scala index 76f1ffd227..204133958f 100755 --- a/sources/scala/reflect/Type.scala +++ b/sources/scala/reflect/Type.scala @@ -13,11 +13,22 @@ abstract class Type; case object NoPrefix extends Type; case object NoType extends Type; +/** fullname */ case class NamedType(fullname: String) extends Type; +/** pre # sym */ case class PrefixedType(pre: Type, sym: Symbol) extends Type; +/** pre.type # sym == pre.sym */ case class SingleType(pre: Type, sym: Symbol) extends Type; +/** clazz.this */ case class ThisType(clazz: Symbol) extends Type; +/** clazz.super[superClazz] */ +/** tpe[args1, ..., argsn] */ case class AppliedType(tpe: Type, args: List[Type]) extends Type; +/** [a <: lo >: hi] */ case class TypeBounds(lo: Type, hi: Type) extends Type; +/** (formals1 ... formalsn) restpe */ case class MethodType(formals: List[Type], restpe: Type) extends Type; +/** */ +case class PolyType(typeParams: List[Symbol], typeBounds: List[Pair[Type, Type]], resultType: Type) extends Type; +/** */ class ImplicitMethodType(formals: List[Type], restpe: Type) extends MethodType(formals, restpe); diff --git a/sources/scala/tools/nsc/typechecker/Codification.scala b/sources/scala/tools/nsc/typechecker/Codification.scala index aa387ea10b..169468b87b 100755 --- a/sources/scala/tools/nsc/typechecker/Codification.scala +++ b/sources/scala/tools/nsc/typechecker/Codification.scala @@ -43,6 +43,8 @@ import scala.tools.nsc.util.{ListBuffer, FreshNameCreator}; } reflect.Function(vparams map (.symbol) map env1, new Reifier(env1, currentOwner).reify(body)) + case This(_) => + reflect.This(reify(tree.symbol)) case _ => throw new TypeError("cannot reify tree: " + tree) } @@ -57,29 +59,29 @@ import scala.tools.nsc.util.{ListBuffer, FreshNameCreator}; case Some(rsym) => rsym case None => - if (sym.isRoot || sym.isRootPackage || sym.isEmptyPackageClass || sym.isEmptyPackage) - reflect.RootSymbol - else reify(sym.owner) match { + if (sym.isRoot || sym.isRootPackage || sym.isEmptyPackageClass || sym.isEmptyPackage) + reflect.RootSymbol + else reify(sym.owner) match { case reflect.NoSymbol => reflect.NoSymbol; case reflect.RootSymbol => - mkGlobalSymbol(sym.name.toString(), sym) - case reflect.Class(ownername) => - mkGlobalSymbol(ownername + "." + sym.name, sym) + mkGlobalSymbol(sym.name.toString(), sym) + case reflect.Class(ownername) => + mkGlobalSymbol(ownername + "." + sym.name, sym) case _ => reflect.NoSymbol } } - def reify(tp: Type): reflect.Type = tp match { + def reify(tp: Type): reflect.Type = null /*tp match { case NoPrefix => reflect.NoPrefix case NoType => reflect.NoType case TypeRef(pre, sym, args) => - val tp = if (sym.owner.isPackageClass) reflect.NamedType(sym.fullNameString); - else reflect.PrefixedType(reify(pre), reify(sym)); - if (args.isEmpty) tp else reflect.AppliedType(tp, args map reify) + val tp = if (sym.owner.isPackageClass) reflect.NamedType(sym.fullNameString); + else reflect.PrefixedType(reify(pre), reify(sym)); + if (args.isEmpty) tp else reflect.AppliedType(tp, args map reify) case SingleType(pre, sym) => reflect.SingleType(reify(pre), reify(sym)) case ThisType(clazz) => @@ -91,9 +93,14 @@ import scala.tools.nsc.util.{ListBuffer, FreshNameCreator}; val restp1 = reify(restp); if (tp.isInstanceOf[ImplicitMethodType]) new reflect.ImplicitMethodType(formals1, restp1) else reflect.MethodType(formals1, restp1) + //case PolyType(typeParams, ClassInfoType(parents, decls, symbol)) => reflect.PolyType(typeParams map reify, Nil, resultType) + //case PolyType(typeParams, SingleType(pre, sym)) => reflect.PolyType(typeParams map reify, Nil, resultType) + case PolyType(typeParams, MethodType(paramsList, resultType)) => + System.err.println("poly polyyyy"); + reflect.PolyType(Nil, Nil , reify(resultType)) //typeParams map mkTypeBounds case _ => throw new TypeError("cannot reify type: " + tp) - } + }*/ } type InjectEnvironment = ListMap[reflect.Symbol, Name]; @@ -112,6 +119,7 @@ import scala.tools.nsc.util.{ListBuffer, FreshNameCreator}; case reflect.Class(_) => "scala.reflect.Class" case reflect.Method(_, _) => "scala.reflect.Method" case reflect.Field(_, _) => "scala.reflect.Field" + case reflect.This(_) => "scala.reflect.This" case reflect.NamedType(_) => "scala.reflect.NamedType" case reflect.PrefixedType(_, _) => "scala.reflect.PrefixedType" case reflect.SingleType(_, _) => "scala.reflect.SingleType" @@ -120,6 +128,7 @@ import scala.tools.nsc.util.{ListBuffer, FreshNameCreator}; case reflect.TypeBounds(_, _) => "scala.reflect.TypeBounds" case reflect.MethodType(_, _) => if (value.isInstanceOf[reflect.ImplicitMethodType]) "scala.reflect.ImplicitMethodType" else "scala.reflect.MethodType" + case reflect.PolyType(_, _, _) => "scala.reflect.PolyType" case _ => "" } @@ -162,14 +171,15 @@ import scala.tools.nsc.util.{ListBuffer, FreshNameCreator}; case c: CaseClass => val name = objectName(c); if (name.length() != 0) gen.mkRef(definitions.getModule(name)) - else { + else { val name = className(c); if (name.length() == 0) throw new Error("don't know how to inject " + value); val injectedArgs = new ListBuffer[Tree]; for (val i <- Iterator.range(0, c.caseArity)) injectedArgs += inject(c.caseElement(i)); New(Ident(definitions.getClass(name)), List(injectedArgs.toList)) - } + } + case null => gen.mkRef(definitions.getModule("scala.reflect.NoType")) case _ => throw new Error("don't know how to inject " + value); } } @@ -195,5 +205,3 @@ import scala.tools.nsc.util.{ListBuffer, FreshNameCreator}; } } - - |