-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathsyntax.factor
More file actions
69 lines (52 loc) · 2.12 KB
/
Copy pathsyntax.factor
File metadata and controls
69 lines (52 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
USING: accessors combinators effects effects.parser kernel lexer
namespaces parser python python.objects sequences
sequences.generalizations words ;
IN: python.syntax
<PRIVATE
SYMBOL: current-context
: with-each-definition ( quot -- )
scan-token dup ";" = [ 2drop ] [
scan-effect rot [ call( tok eff -- ) ] keep with-each-definition
] if ; inline recursive
: scan-definitions ( quot -- )
scan-token current-context set "=>" expect with-each-definition ; inline
: gather-args-quot ( in-effect -- quot )
dup ?last "**" = [
but-last length '[ [ _ narray array>py-tuple ] dip ]
] [
length '[ _ narray array>py-tuple f ]
] if ;
: unpack-value-quot ( out-effect -- quot )
length {
{ 0 [ [ drop ] ] }
{ 1 [ [ ] ] }
[ '[ py-tuple>array _ firstn ] ]
} case ;
: make-function-quot ( obj-quot effect -- quot )
[ in>> gather-args-quot ] [ out>> unpack-value-quot ] bi
swapd '[ @ @ -rot call-object-full @ ] ;
: make-factor-words ( module name prefix? -- call-word obj-word )
[ [ ":" glue ] [ ":$" glue ] 2bi ] [ nip dup "$" prepend ] if
[ create-word-in ] bi@ ;
:: add-function ( name effect module prefix? -- )
module name prefix? make-factor-words :> ( call-word obj-word )
obj-word module name '[ _ _ py-import-from ] ( -- o ) define-inline
call-word obj-word def>> effect make-function-quot effect define-inline ;
: make-method-quot ( name effect -- quot )
[ in>> rest gather-args-quot ] [ out>> unpack-value-quot ] bi swapd
'[ @ rot _ getattr -rot call-object-full @ ] ;
: method-callable ( name effect -- )
[ dup create-word-in swap ] dip [ make-method-quot ] keep define-inline ;
: method-object ( name -- )
[ "$" prepend create-word-in ] [ '[ _ getattr ] ] bi
{ "obj" } { "obj'" } <effect> define-inline ;
: add-method ( name effect -- )
dupd method-callable method-object ;
PRIVATE>
SYNTAX: PY-FROM: [
current-context get f add-function
] scan-definitions ; inline
SYNTAX: PY-QUALIFIED-FROM: [
current-context get t add-function
] scan-definitions ; inline
SYNTAX: PY-METHODS: [ add-method ] scan-definitions ; inline