-
Notifications
You must be signed in to change notification settings - Fork 0
/
erlango_02e_operators.go
318 lines (239 loc) · 10.6 KB
/
erlango_02e_operators.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
Copyright (c) 2023, Balazs Nyiro
All rights reserved.
This source code (all file in this repo) is licensed
under the Apache-2 style license found in the
LICENSE file in the root directory of this source tree.
http://erlang.org/documentation/doc-6.0/doc/reference_manual/data_types.html
*/
package old2
import (
"fmt"
"strings"
)
func expression_detect_operators(tokensOrExpressionsOld TokensOrExpressions, wantedExpressionDetectionTypesCommaSeparated string) TokensOrExpressions {
if ! (strings.Contains(wantedExpressionDetectionTypesCommaSeparated, "operators") ||
strings.Contains(wantedExpressionDetectionTypesCommaSeparated, "detectAllExpressions")) {
return tokensOrExpressionsOld
}
/*
https://www.erlang.org/doc/reference_manual/expressions.html
operators are not expressions, because they don't have their own value,
but from another perspective they are expressions,
because +1 can be interpreted as plus(1), and this function has a value.
So every operator can be used as a function, and from that perspective they are expressions
# 3 chars long:
=:= Exactly equal to
=/= Exactly not equal to
# 2 chars long:
== Equal to
/= Not equal to
=< Less than or equal to
>= Greater than or equal to
# 2 chars, list operators:
--
++
|| comprehension, [{E,L} || E <- L=[1,2,3]].
<- comprehension
# 2 chars, map operator:
=> // map val declaration
:= // map update existing value
# 2 chars, fun expression:
->
# 2 chars, bitstring generator:
<= BitstringPattern <= BitStringExpr
######### 1 chars long ###########
: modul:func
# The record expressions Expr#Name.Field and #Name.Field ??? honestly I have never seen # operator
= The = character is used to denote two similar but distinct operators: the match operator and the compound pattern operator. Which one is meant is determined by context.
< Less than
> Greater than
# arithmetic operators, 1 chars long
+ Unary + Number
- Unary - Number
+ number
- Number
* Number
/ Floating point division Number
///////////////////////////////////////////////////////////////////////////////////////////
// from interpreter perspective, these are detected as ATOMS - so they have to be converted
// TODO: convert these operators, too
bnot Unary bitwise NOT Integer
div Integer division Integer
rem Integer remainder of X/Y Integer
band Bitwise AND Integer
bor Bitwise OR Integer
bxor Arithmetic bitwise XOR Integer
bsl Arithmetic bitshift left Integer
bsr Bitshift right Integer
TODO: convert these, too:
not Unary Logical NOT
and Logical AND
or Logical OR
xor Logical XOR
*/
// locally used functions only, for num detection
isHashmark := func (tokenOrExpression TokenOrExpression) bool {
return tokenOrExpression.token.stringRepresentation() == "#"
}
isStar := func (tokenOrExpression TokenOrExpression) bool {
return tokenOrExpression.token.stringRepresentation() == "*"
}
isPipe := func (tokenOrExpression TokenOrExpression) bool {
fmt.Println("isPipe: ", tokenOrExpression.token.stringRepresentation())
return tokenOrExpression.token.stringRepresentation() == "|"
}
isPlus := func (tokenOrExpression TokenOrExpression) bool {
fmt.Println("isPlus: ", tokenOrExpression.token.stringRepresentation())
return tokenOrExpression.token.stringRepresentation() == "+"
}
isMinus := func (tokenOrExpression TokenOrExpression) bool {
fmt.Println("isMinus: ", tokenOrExpression.token.stringRepresentation())
return tokenOrExpression.token.stringRepresentation() == "-"
}
isLess := func (tokenOrExpression TokenOrExpression) bool {
fmt.Println("isLess: ", tokenOrExpression.token.stringRepresentation())
return tokenOrExpression.token.stringRepresentation() == "<"
}
isGreater := func (tokenOrExpression TokenOrExpression) bool {
fmt.Println("isGreater: ", tokenOrExpression.token.stringRepresentation())
return tokenOrExpression.token.stringRepresentation() == ">"
}
isEqual := func (tokenOrExpression TokenOrExpression) bool {
fmt.Println("isEqual: ", tokenOrExpression.token.stringRepresentation())
return tokenOrExpression.token.stringRepresentation() == "="
}
isColon := func (tokenOrExpression TokenOrExpression) bool {
fmt.Println("isColon: ", tokenOrExpression.token.stringRepresentation())
return tokenOrExpression.token.stringRepresentation() == ":"
}
isSlash := func (tokenOrExpression TokenOrExpression) bool {
fmt.Println("isSlash: ", tokenOrExpression.token.stringRepresentation())
return tokenOrExpression.token.stringRepresentation() == "/"
}
tokensOrExpressionsNew := TokensOrExpressions{}
lenTokenOrExpressions := len(tokensOrExpressionsOld)
operatorTokenElems := TokensOrExpressions{ }
if lenTokenOrExpressions > 0 { // so if there is something to check
idTokenOrExpr := -1
for {
// because id is manipulated inside the for loop, we can move forward more with 1,
// if it is necessary, with more than 1 token processing
idTokenOrExpr++
if idTokenOrExpr >= lenTokenOrExpressions { break }
tokenOrExpressionActual := getTokenOrExpression_fromLot(idTokenOrExpr, tokensOrExpressionsOld)
tokenOrExpressionNext1 := getTokenOrExpression_fromLot(idTokenOrExpr+1, tokensOrExpressionsOld)
tokenOrExpressionNext2 := getTokenOrExpression_fromLot(idTokenOrExpr+2, tokensOrExpressionsOld)
isOperator := false
// there is a few 3 chars long operators only. This was the original detection pattern,
// one detection case for each case
/////////// 3 chars long operator //////////////////////////////////////////////////////////
if ! isOperator { // =:=
if isEqual(tokenOrExpressionActual) {
if isColon(tokenOrExpressionNext1){
if isEqual(tokenOrExpressionNext2) {
isOperator = true
operatorTokenElems = TokensOrExpressions{
TokenOrExpression{token: tokenOrExpressionActual.token},
TokenOrExpression{token: tokenOrExpressionNext1.token},
TokenOrExpression{token: tokenOrExpressionNext2.token},
}
idTokenOrExpr += 2 // 2 next token were used, they have to be skipped in next turns
}
}
}
if ! isOperator{
fmt.Println("NOT =:= operator")
}
}
/////////// 3 chars long operator //////////////////////////////////////////////////////////
if ! isOperator { // =/=
if isEqual(tokenOrExpressionActual) {
if isSlash(tokenOrExpressionNext1){
if isEqual(tokenOrExpressionNext2) {
isOperator = true
operatorTokenElems = TokensOrExpressions{
TokenOrExpression{token: tokenOrExpressionActual.token},
TokenOrExpression{token: tokenOrExpressionNext1.token},
TokenOrExpression{token: tokenOrExpressionNext2.token},
}
idTokenOrExpr += 2 // 2 next token were used, they have to be skipped in next turns
}
}
}
if ! isOperator{
fmt.Println("NOT =/= operator")
}
}
/////////// 2 chars long operators //////////////////////////////////////////////////////////
if ! isOperator {
if (isEqual(tokenOrExpressionActual) && isEqual(tokenOrExpressionNext1) ) || // ==
(isEqual(tokenOrExpressionActual) && isSlash(tokenOrExpressionNext1) ) || // =/
(isEqual(tokenOrExpressionActual) && isLess(tokenOrExpressionNext1) ) || // =<
(isGreater(tokenOrExpressionActual) && isEqual(tokenOrExpressionNext1) ) || // >=
(isPlus(tokenOrExpressionActual) && isPlus(tokenOrExpressionNext1) ) || // ++
(isMinus(tokenOrExpressionActual) && isMinus(tokenOrExpressionNext1) ) || // --
(isPipe(tokenOrExpressionActual) && isPipe(tokenOrExpressionNext1) ) || // ||
(isLess(tokenOrExpressionActual) && isMinus(tokenOrExpressionNext1) ) || // <-
(isEqual(tokenOrExpressionActual) && isGreater(tokenOrExpressionNext1) ) || // =>
(isColon(tokenOrExpressionActual) && isEqual(tokenOrExpressionNext1) ) || // :=
(isMinus(tokenOrExpressionActual) && isGreater(tokenOrExpressionNext1) ) || // ->
(isLess(tokenOrExpressionActual) && isEqual(tokenOrExpressionNext1) ) { // <=
isOperator = true
operatorTokenElems = TokensOrExpressions{
TokenOrExpression{token: tokenOrExpressionActual.token},
TokenOrExpression{token: tokenOrExpressionNext1.token},
}
idTokenOrExpr += 1 // next token is used, it has to be skipped in next turns
}
if ! isOperator{
fmt.Println("NOT 2 chars long operator")
}
}
/////////// 1 char long operators //////////////////////////////////////////////////////////
if ! isOperator {
if isColon(tokenOrExpressionActual) || // :
isHashmark(tokenOrExpressionActual) || // #
isEqual(tokenOrExpressionActual) || // =
isLess(tokenOrExpressionActual) || // <
isGreater(tokenOrExpressionActual) || // >
isPlus(tokenOrExpressionActual) || // +
isMinus(tokenOrExpressionActual) || // -
isStar(tokenOrExpressionActual) || // *
isSlash(tokenOrExpressionActual) { // /
isOperator = true
operatorTokenElems = TokensOrExpressions{
TokenOrExpression{token: tokenOrExpressionActual.token},
}
}
if ! isOperator{
fmt.Println("NOT 1 char long operator")
}
}
/////////////////////////////////////////////////////////////////////
if isOperator {
/* an operator is not a real expression, because a + sign doesn't have a value in itself.
the operator and the operands, together, that is an expression.
but operators can be translated to functions: 1 + 2 == add(1, 2)
so from this perspective, ADD function can be evaluated, and it can have its own value,
if it is calculated.
Here I will mark the tokens as expressions, because an expression here means:
we know what can we do with those tokens, and value can be calculated.
and later when the expressions are interpreted, if it is not a simple value,
(not a function or an integer, for example) then the operator is executed, because that is a form of
function, too.
*/
tokenOrExpressionActual.elemType = tokenOrExpression_thisIsAnExpression
tokenOrExpressionActual.expression = ErlExpression {
ExpressionType: expression_operator,
TokensOrExpressions: operatorTokenElems,
}
// put back tokenOrExpression with modified elemType and expression
tokensOrExpressionsNew = append(tokensOrExpressionsNew, tokenOrExpressionActual)
} else { // not an operator - put back the tokenOrExpression without any extra change/modification
tokensOrExpressionsNew = append(tokensOrExpressionsNew, tokenOrExpressionActual)
}
} // FOR
} // len > 0
return tokensOrExpressionsNew
}