1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 23:42:31 +03:00

Compile Python lambdas.

Lambdas are so hilariously crippled in Python that they're
significantly easier to compile than functions. Indeed, the resulting
core looks much like a function definition, without a `rec` binding.
This commit is contained in:
Patrick Thomson 2019-12-13 12:14:04 -05:00
parent 4ff4e5d776
commit e137f4c68b
2 changed files with 16 additions and 1 deletions

View File

@ -311,7 +311,18 @@ instance Compile Py.ImportFromStatement
instance Compile Py.ImportStatement
instance Compile Py.Integer
instance Compile Py.Lambda
instance Compile Py.Lambda where
compile it@Py.Lambda
{ body
, parameters
} cc next = do
let unparams (Py.LambdaParameters _ ps) = toList ps
unparam (Py.Parameter (Prj (Py.Identifier _pann pname))) = Just . named' . Name $ pname
unparam _ = Nothing
body' <- compile body cc next
let params = maybe [] unparams parameters
pure . locate it. . lams (catMaybes (fmap unparam params)) $ body'
instance Compile Py.List
instance Compile Py.ListComprehension
instance Compile Py.ListSplat

View File

@ -0,0 +1,4 @@
# CHECK-TREE: { const <- \x -> \y -> x; y <- const #true #true; z <- const #false #false; #record { const: const, y : y, z: z, }}
const = lambda x, y: x
y = const(True, True)
z = const(False, False)