/
gen.scala
148 lines (123 loc) · 4.61 KB
/
gen.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package generator.python
import java.io._
import resource._
import scala.Some
package object gen
{
// TODO: memoization
def generationUnit(f : Typed.Function) =
{
if (f.tryGetAttribute("python") == Some("no"))
None
else
f.annotations collect {
case Typed.Annotation(g : PythonGenerator, args) => g.generatePython(args)_
} match {
case Nil =>
Some(base.python.generatePython(Nil)(f))
case g :: Nil =>
Some(g(f))
case _ => throw new Exception("Multiple python generators are given for function " + f)
}
}
def apply(p : Typed.Package, dst_dir : String, idx_dir : String)
{
apply(p, new File(dst_dir), new File(idx_dir))
}
def apply(p : Typed.Package, dir : File, idx_dir : File)
{
ensure_dir(dir)
ensure_dir(idx_dir)
p.packages.values foreach { sub => apply(sub, new File(dir, sub.name), new File(idx_dir, sub.name) ) }
def printWriter(dst_dir : File, filename : String) =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(
new File(dst_dir, filename), true))),
true)
if (config.verbose)
println("\t" + p.qualifiedName)
for (idx_out <- managed(printWriter(idx_dir, "__init__.py")))
{
p.functions.values foreach { f =>
try {
generationUnit(f) map { g =>
//println(f.parent.qualifiedName, f.name)
for (out <- managed(printWriter(dir, s"_${f.name}.py"))) {
out.println(g.code)
}
idx_out.println(base.withImports(Printer.importsOf(f)))
}
} catch {
case e : Exception =>
if (config.catch_errors) {
println(s"An exception '${e.getMessage}' caught when generating python code for $f")
println(e.getMessage)
}
}
}
if (p.types.nonEmpty)
{
for (out <- managed(printWriter(dir, "_types.py")))
{
p.types.values foreach {
case interface : Typed.InterfaceDecl =>
if (interface.generics.isEmpty) {
val name = interface.name
val bases = interface.bases map { "," + _.toString } mkString ""
out.println(s"#class $name(object$bases): pass")
}
case alias : Typed.AliasDecl =>
if (alias.generics.isEmpty)
out.println("#" + alias.name + " = " + alias.target.toString)
}
}
}
p.functionAliases.values foreach { f =>
generationUnit(f.target) map { g =>
idx_out.println(base.withImports(Printer.importsOf(f.target).as(f.name)))
}
}
p.packages.values foreach { pkg =>
idx_out.println(s"import " + pkg.name)
}
for (out <- managed(printWriter(dir, "__init__.py"))) {}
}
}
def ensure_dir(dir: File) {
if (!dir.exists()) {
dir.mkdirs()
if (!dir.exists())
throw new Exception("cannot create directory " + dir.getName)
}
}
trait GenerationUnit
{
def name : String
def code : predef.Code
}
trait PythonGenerator extends Typed.AnnotationHandler
{
def generatePython(/** arguments of the annotation */ args : List[String])
(/** function to process */ f : Typed.Function) : GenerationUnit
}
object Annotations {
import Typed.Annotations._
override def toString = registry.toString()
// TODO: non-intrusive registration
register(random)
register(mathops)
register(curried)
register(curried.after_typing)
register(base.python)
register(observable)
register(function)
register(intrinsic)
register(intrinsic_function)
register(intrinsic_observable)
register(order_factory)
register(order_factory_curried)
register(order_factory_on_proto)
}
}