Typo fix for token code and some code formatting.
[cslatevm.git] / src / syntax / quote.slate
blob211e9273b175b6c67f3ebcafdf5b8b983c371b2c
1 nodes define: #Unquote &parents: {nodes Node}.
3 "Unquoted expressions must be treated specially since they are not simply
4 unboxed."
6 node@(nodes Node traits) unquoted
7 [node].
9 val@(Root traits) unquoted
10 [nodes Literal for: val].
12 node@(nodes Node traits) instantiateWith: unquoteValues
13 "Transforms the Syntax tree by replacing Unquote nodes in the default traversal
14 order with the provided unquoteValues. This method is only intended to be called
15 implicitly by the quote macro."
16 [| index |
17   unquoteValues infect: [| :node | node unquoted].
18   index: -1.
19   node deepCopy
20     transformBy:
21       [| :node |
22         (node isSameAs: nodes Unquote)
23           ifTrue:
24             [index: index + 1.
25              unquoteValues at: index]
26           ifFalse: [node]]
29 _@(nodes Node traits) isQuotedBy: _
30 "Controls the default quotation semantics of Syntax Nodes.
31 By default, all Syntax Nodes are quoted."
32 [True].
34 macro@(nodes Macro traits) isQuotedBy: label
35 "Defers the quotation decision to the message sending:isQuotedBy:
36 so that specific macros may choose whether or not they are captured
37 by quotation."
38 [macro sending: macro selector isQuotedBy: label].
40 node@(nodes Node traits) quote: label
41 "This is the main quotation method, taking a label object to pair quote/unquote
42 actions as needed. "
43 [| unquotes literal |
44   unquotes: #{} writer.
45   literal:
46     (nodes Literal for:
47       (node deepCopy
48         transformBy:
49           [| :node |
50            (node isQuotedBy: label)
51               ifTrue: [node]
52               ifFalse:
53                 [unquotes nextPut: node.
54                  nodes Unquote]])).
55   unquotes position isPositive
56     ifTrue:
57       [nodes KeywordMessage sending: #instantiateWith:
58         to: {literal. unquotes contents as: nodes Array}]
59     ifFalse: [literal]
62 paren@(nodes Parenthesis traits) quote: label
64   paren statements size = 1
65     ifTrue: [paren statements first quote: label]
66     ifFalse: [resend]
69 node@(nodes Node traits) quote
70 "Quote the Syntax Node, providing direct syntax representation to the source
71 code. This goes through labelled-quote: using Nil as a label."
72 [node quote: #[nodes Literal for: Nil]].
74 "The following are sending:isQuotedBy: methods for allowing specific
75 macros to work even within the body of a quotation, effectively
76 serving as unquotes which may also transform supplied expressions. 
77 They just have to specialize on the selector and may optionally
78 work only in the context of specific quotation labels." 
79 _@(nodes Macro traits) sending: _@(Symbol traits) isQuotedBy: _
80 [True].
82 _@(nodes Macro traits) sending: _@#unquote isQuotedBy: label
83 "`unquote only works with the default label."
84 [label ~= #[nodes Literal for: Nil]].
86 node@(nodes Node traits) unquote
87 "Unquoting simply substitutes the expression unchanged."
88 [node].
90 macro@(nodes Macro traits) sending: _@#unquote: isQuotedBy: label
91 [macro arguments second ~= label].
93 node@(nodes Node traits) unquote: _
94 "Labeled unquoting simply substitutes the expression unchanged."
95 [node].
97 _@(nodes Macro traits) sending: _@#literal isQuotedBy: label
98 "`literal only works with the default label."
99 [label ~= #[nodes Literal for: Nil]].
101 value@(nodes Node traits) literal
102 "An unquote macro that wraps the supplied value expression with a Literal."
103 [`(nodes Literal for: value `unquote)].
105 _@(nodes Macro traits) sending: _@#load isQuotedBy: label
106 "`load only works with the default label."
107 [label ~= #[nodes Literal for: Nil]].
109 var@(nodes Node traits) load
110 "An unquote macro that wraps the supplied Variable expression with a LoadVariable."
111 [`(nodes LoadVariable from: var `unquote)].
113 _@(nodes Macro traits) sending: _@#store: isQuotedBy: label
114 "`store: only works with the default label."
115 [label ~= #[nodes Literal for: Nil]].
117 var@(nodes Node traits) store: value
118 "An unquote macro that wraps the supplied Variable expression and value expression with a StoreVariable."
119 [`(nodes StoreVariable of: value `unquote into: var `unquote)].
121 _@(nodes Macro traits) sending: _@#sendTo: isQuotedBy: label
122 "`sendTo: only works with the default label."
123 [label ~= #[nodes Literal for: Nil]].
125 selector@(nodes Node traits) sendTo: arguments
126 "An unquote macro that generates a Message sending the Symbol expression as a message selector to the arguments Array expression."
127 [`(nodes Message sending: selector `unquote to: arguments `unquote)].
129 _@(nodes Macro traits) sending: _@#array isQuotedBy: label
130 "`array only works with the default label."
131 [label ~= #[nodes Literal for: Nil]].
133 vals@(nodes Node traits) array
134 "An unquote macro that stuffs the supplied collection into an Array."
135 [`(vals `unquote as: nodes Array)].
137 _@(nodes Macro traits) sending: _@#block isQuotedBy: label
138 "`block only works with the default label."
139 [label ~= #[nodes Literal for: Nil]].
141 expr@(nodes Node traits) block
142 "An unquote macro that wraps the supplied expression into a Block."
143 [`(nodes Block newFor: expr `unquote)].