@@ -94,14 +94,19 @@ sys.stdout.close()`,
94
94
func Compile (str , filename , mode string , flags int , dont_inherit bool ) py.Object {
95
95
Ast , err := parser .ParseString (str , mode )
96
96
if err != nil {
97
- panic (err )
97
+ panic (err ) // FIXME error handling!
98
98
}
99
- fmt .Println (ast .Dump (Ast ))
99
+ return CompileAst (Ast , filename , flags , dont_inherit )
100
+ }
101
+
102
+ // As Compile but takes an Ast
103
+ func CompileAst (Ast ast.Ast , filename string , flags int , dont_inherit bool ) * py.Code {
104
+ //fmt.Println(ast.Dump(Ast))
100
105
code := & py.Code {
101
106
Filename : filename ,
102
- Firstlineno : 1 , // FIXME
103
- Name : "<module>" , // FIXME
104
- Flags : 64 , // FIXME
107
+ Firstlineno : 1 , // FIXME
108
+ Name : "<module>" , // FIXME
109
+ Flags : int32 ( flags | py . CO_NOFREE ), // FIXME
105
110
}
106
111
c := & compiler {
107
112
Code : code ,
@@ -115,6 +120,11 @@ func Compile(str, filename, mode string, flags int, dont_inherit bool) py.Object
115
120
c .Expr (node .Body )
116
121
case * ast.Suite :
117
122
c .Stmts (node .Body )
123
+ case ast.Expr :
124
+ // Make None the first constant so lambda can't have a docstring
125
+ c .Code .Name = "<lambda>"
126
+ c .Const (py .None ) // FIXME extra None for some reason in Consts
127
+ c .Expr (node )
118
128
default :
119
129
panic (py .ExceptionNewf (py .SyntaxError , "Unknown ModuleBase: %v" , Ast ))
120
130
}
@@ -380,7 +390,13 @@ func (c *compiler) Expr(expr ast.Expr) {
380
390
case * ast.Lambda :
381
391
// Args *Arguments
382
392
// Body Expr
383
- panic ("FIXME compile: Lambda not implemented" )
393
+ // newC := Compiler
394
+ code := CompileAst (node .Body , c .Code .Filename , int (c .Code .Flags )| py .CO_OPTIMIZED | py .CO_NEWLOCALS , false ) // FIXME pass on compile args
395
+ code .Argcount = int32 (len (node .Args .Args ))
396
+ c .OpArg (vm .LOAD_CONST , c .Const (code ))
397
+ c .OpArg (vm .LOAD_CONST , c .Const (py .String ("<lambda>" )))
398
+ // FIXME node.Args
399
+ c .OpArg (vm .MAKE_FUNCTION , 0 )
384
400
case * ast.IfExp :
385
401
// Test Expr
386
402
// Body Expr
0 commit comments