1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 10:15:55 +03:00

Merge pull request #331 from github/compile-python-function-decorators

Compile Python function decorators and finish milestone #2.
This commit is contained in:
Rob Rix 2019-10-11 14:51:34 -04:00 committed by GitHub
commit 30c447a5ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 1 deletions

View File

@ -18,6 +18,7 @@ import Core.Name as Name
import Data.Coerce
import Data.Foldable
import Data.Function
import Data.List.NonEmpty (NonEmpty (..))
import GHC.Records
import Source.Span (Span)
import Syntax.Stack (Stack)
@ -201,10 +202,32 @@ deriving instance Compile Py.CompoundStatement
instance Compile Py.ConcatenatedString
instance Compile Py.ConditionalExpression
instance Compile Py.ContinueStatement
instance Compile Py.DecoratedDefinition
instance Compile Py.DecoratedDefinition where
compile it@Py.DecoratedDefinition
{ definition
, extraChildren = [ Py.Decorator { extraChildren } ]
} cc next = do
let thenReassign item = do
_ :> lastbound <- asks unBindings
tocall <- compile extraChildren pure next
let callit go = (pure lastbound .= (tocall $$ pure lastbound)) >>> go
fmap callit (cc item)
locate it <$> compile definition thenReassign next
compile it _ _ = fail ("Can't figure out decorated definition " <> show it)
instance Compile Py.DeleteStatement
instance Compile Py.Dictionary
instance Compile Py.DictionaryComprehension
instance Compile Py.DottedName where
compile it@Py.DottedName
{ extraChildren = Py.Identifier { text } :| rest
} cc _next = do
let aggregate Py.Identifier { text = inner } x = x ... Name inner
composite = foldr aggregate (pure (Name text)) rest
locate it composite & cc
instance Compile Py.Ellipsis
instance Compile Py.ExecStatement

View File

@ -0,0 +1,7 @@
# CHECK-TREE: { const <- \a -> \b -> { identity <- \x -> x; identity a }; #record{ const: const }}
def const(a, b):
def identity(x):
return x
return identity(a)

View File

@ -0,0 +1,11 @@
# CHECK-JQ: .scope.zilch[0].b[0].span == { start: [8, 8], end: [ 8, 16 ] }
# CHECK-JQ: .scope.result[0].a[0].span == { start: [5, 8], end: [ 5, 16 ] }
def const(a, b):
def result():
return a
def zilch(b):
return b
return result()

View File

@ -0,0 +1,7 @@
# CHECK-TREE: { passthru <- \x -> x; decorated <- \x -> x; decorated = passthru(decorated); #record { passthru: passthru, decorated: decorated }}
def passthru(x):
return x
@passthru
def decorated(x):
return x