Skip to content

Commit

Permalink
computing precisely a set of IObservable[T] to generate
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkolotaev committed Feb 27, 2014
1 parent cefc083 commit 681f8e4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#((() => .Boolean),(() => .Float),.IObservable[.Float]) => .IObservable[.Float]
class IFunctionIObservablefloatIFunctionboolIFunctionfloatIObservablefloat(object):
_types = [meta.function((IFunctionbool,IFunctionfloat,IObservablefloat,),IObservablefloat)]
_types.append(IFunctionIObservablefloatIObservableboolIFunctionfloatIObservablefloat)
_types.append(IFunctionIObservablefloatIFunctionboolIObservablefloatIObservablefloat)
_types.append(IFunctionIObservablefloatIObservableboolIFunctionfloatIObservablefloat)
_types.append(IFunctionIObservablefloatIObservableboolIObservablefloatIObservablefloat)
pass

Expand Down
2 changes: 1 addition & 1 deletion marketsim/gen/_out/_ifunction/_ifunctionu.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class IFunctionU(object):
_types = [meta.function((),U)]
_types.append(IFunctionobject)
_types.append(IFunctionT)
_types.append(IFunctionR)
_types.append(IFunctionT)
pass


Expand Down
4 changes: 2 additions & 2 deletions marketsim/gen/_out/_iobservable/_iobservableint.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from marketsim.gen._out._ievent import IEvent
from marketsim.gen._out._ifunction._ifunctionint import IFunctionint
from marketsim.gen._out._iobservable._iobservablefloat import IObservablefloat
from marketsim.gen._out._iobservable._iobservableobject import IObservableobject
from marketsim.gen._out._iobservable._iobservablefloat import IObservablefloat
class IObservableint(IEvent, IFunctionint):
_types = []
_types.append(IObservablefloat)
_types.append(IObservableobject)
_types.append(IObservablefloat)
pass
7 changes: 0 additions & 7 deletions marketsim/gen/_out/_iobservable/_iobservablestr.py

This file was deleted.

110 changes: 59 additions & 51 deletions marketsim/gen/src/main/scala/generator/python/gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ package object gen
processFunctions(p, new File(dst_dir), new File(idx_dir))
processTypes(p, new File(dst_dir), new File(idx_dir))

//Typed.topLevel.getUsedTypes foreach { t => println(base.withImports(t.asCode)) }
processUsedTypes(new File(dst_dir))
}

def printWriter(dst_dir : File, filename : String) =
Expand All @@ -42,11 +42,43 @@ package object gen
new File(dst_dir, filename.toLowerCase), true))),
true)

def processUsedTypes()
def processUsedTypes(out_dir : File)
{
var processedTypes = Set.empty[TypesBound.Base]

def impl(typesToProcess : Set[TypesBound.Base]) {
def impl(typesToProcess : Set[TypesBound.Base])
{
if (typesToProcess.nonEmpty)
{
Typed.topLevel.clearUsedTypes()

typesToProcess foreach {
case f : TypesBound.Interface if f.decl.name == "IObservable" =>
val rt = f.genericArgs(0)
val name = Printer.mangle(f.asCode.toString)
val methods = getMethods(f)
val casts = Code.from(
TypesBound.directCasts(f.genericArgs(0)).toList map {
t => "_types.append(" ||| Typed.topLevel.observableOf(t).asCode ||| ")"
},
predef.crlf)

for (ty_out <- managed(printWriter(new File(out_dir, "_iobservable"), s"_$name.py")))
{
val s =
s"class $name("||| Typed.topLevel.IEvent.asCode |||", "|||
TypesBound.Function(Nil, rt).asCode |||"):" |>
("_types = []" | casts | methods)

ty_out.println(base.withImports(s).toString)
}
case _ =>

}
processedTypes = processedTypes ++ typesToProcess

impl(Typed.topLevel.getUsedTypes -- processedTypes)
}

}

Expand Down Expand Up @@ -132,6 +164,30 @@ package object gen
}

}
def getMethods(t : TypesBound.Base) =
{
val methods = Typed.topLevel getMethods t

if (methods.isEmpty)
toLazy("pass")
else {
(methods map { case (method_name, fs) =>
val args = fs.head.parameters.tail
val target = fs.head.name ||| ImportFrom(fs.head.name, Printer.moduleName(fs.head))
if (args.isEmpty) {
base.Prop(method_name, "return " ||| target ||| "(self)")
} else {
val in_args = Code.from(args map { _.name ||| " = None" }, ",")
val pass_args = Code.from(args map { "," ||| _.name }, "")
base.Def(method_name,
in_args,
"return " ||| target |||
"(self" ||| pass_args ||| ")")
}
} reduce { _ | _ }) | "pass"
}
}

def processTypes(p : Typed.Package, dir : File, idx_dir : File)
{
ensure_dir(dir)
Expand All @@ -145,30 +201,6 @@ package object gen
def importsWithout(code: => predef.Code, exclude : Importable => Boolean) : Code =
new WithoutImports((code.imports.toSet[Importable] filterNot exclude map { _.repr + crlf } mkString "") + code)

def getMethods(t : TypesBound.Base) =
{
val methods = Typed.topLevel getMethods t

if (methods.isEmpty)
toLazy("pass")
else {
(methods map { case (method_name, fs) =>
val args = fs.head.parameters.tail
val target = fs.head.name ||| ImportFrom(fs.head.name, Printer.moduleName(fs.head))
if (args.isEmpty) {
base.Prop(method_name, "return " ||| target ||| "(self)")
} else {
val in_args = Code.from(args map { _.name ||| " = None" }, ",")
val pass_args = Code.from(args map { "," ||| _.name }, "")
base.Def(method_name,
in_args,
"return " ||| target |||
"(self" ||| pass_args ||| ")")
}
} reduce { _ | _ }) | "pass"
}
}

for (idx_out <- managed(printWriter(idx_dir, "__init__.py")))
{
if (p.types.nonEmpty)
Expand Down Expand Up @@ -213,30 +245,6 @@ package object gen
} else {
val base_dir = new File(s"$dir/_$name")

if (interface.name == "IObservable") {

val fs = interface.getInstances

fs foreach { f =>

val rt = f.genericArgs(0)
val name = Printer.mangle(f.asCode.toString)
val methods = getMethods(f)
val casts = Code.from(
(fs filter { y => y != f && (f canCastTo y) } map { "_types.append(" ||| _.asCode ||| ")" }).toList,
predef.crlf)

for (ty_out <- managed(printWriter(base_dir, s"_$name.py")))
{
val s =
s"class $name("||| Typed.topLevel.IEvent.asCode |||", "|||
TypesBound.Function(Nil, rt).asCode |||"):" |>
("_types = []" | casts | methods)

ty_out.println(base.withImports(s).toString)
}
}
}
if (interface.name == "Observable") {

val fs = interface.getInstances
Expand Down

0 comments on commit 681f8e4

Please sign in to comment.