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:
commit
30c447a5ec
@ -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
|
||||
|
||||
|
7
semantic-python/test/fixtures/2-06-nested-function-definition.py
vendored
Normal file
7
semantic-python/test/fixtures/2-06-nested-function-definition.py
vendored
Normal 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)
|
11
semantic-python/test/fixtures/2-07-closure-over-scope.py
vendored
Normal file
11
semantic-python/test/fixtures/2-07-closure-over-scope.py
vendored
Normal 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()
|
7
semantic-python/test/fixtures/2-08-function-decorator.py
vendored
Normal file
7
semantic-python/test/fixtures/2-08-function-decorator.py
vendored
Normal 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
|
Loading…
Reference in New Issue
Block a user