Skip to content

Commit 4826ccc

Browse files
committed
compile: basic lambda: working
1 parent d0ea27b commit 4826ccc

File tree

4 files changed

+196
-134
lines changed

4 files changed

+196
-134
lines changed

compile/compile.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,19 @@ sys.stdout.close()`,
9494
func Compile(str, filename, mode string, flags int, dont_inherit bool) py.Object {
9595
Ast, err := parser.ParseString(str, mode)
9696
if err != nil {
97-
panic(err)
97+
panic(err) // FIXME error handling!
9898
}
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))
100105
code := &py.Code{
101106
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
105110
}
106111
c := &compiler{
107112
Code: code,
@@ -115,6 +120,11 @@ func Compile(str, filename, mode string, flags int, dont_inherit bool) py.Object
115120
c.Expr(node.Body)
116121
case *ast.Suite:
117122
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)
118128
default:
119129
panic(py.ExceptionNewf(py.SyntaxError, "Unknown ModuleBase: %v", Ast))
120130
}
@@ -380,7 +390,13 @@ func (c *compiler) Expr(expr ast.Expr) {
380390
case *ast.Lambda:
381391
// Args *Arguments
382392
// 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)
384400
case *ast.IfExp:
385401
// Test Expr
386402
// Body Expr

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy