-
Notifications
You must be signed in to change notification settings - Fork 1
/
operators.go
761 lines (700 loc) · 19.9 KB
/
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
package operators
import (
"fmt"
"math"
"math/bits"
"strconv"
"strings"
"github.com/dece2183/hexowl/builtin"
"github.com/dece2183/hexowl/user"
"github.com/dece2183/hexowl/utils"
)
type operatorType int
// Operator types
const (
OP_NONE operatorType = iota
OP_DECLFUNC operatorType = iota
OP_ASSIGN operatorType = iota
OP_LOCALASSIGN operatorType = iota
OP_DECREMENT operatorType = iota
OP_INCREMENT operatorType = iota
OP_ASSIGNMUL operatorType = iota
OP_ASSIGNDIV operatorType = iota
OP_ASSIGNBITAND operatorType = iota
OP_ASSIGNBITOR operatorType = iota
OP_LOGICNOT operatorType = iota
OP_LOGICOR operatorType = iota
OP_LOGICAND operatorType = iota
OP_EQUALITY operatorType = iota
OP_NOTEQ operatorType = iota
OP_MORE operatorType = iota
OP_LESS operatorType = iota
OP_MOREEQ operatorType = iota
OP_LESSEQ operatorType = iota
OP_MINUS operatorType = iota
OP_PLUS operatorType = iota
OP_MULTIPLY operatorType = iota
OP_DIVIDE operatorType = iota
OP_MODULO operatorType = iota
OP_POWER operatorType = iota
OP_LEFTSHIFT operatorType = iota
OP_RIGHTSHIFT operatorType = iota
OP_BITOR operatorType = iota
OP_BITAND operatorType = iota
OP_BITXOR operatorType = iota
OP_BITCLEAR operatorType = iota
OP_BITINVERSE operatorType = iota
OP_POPCNT operatorType = iota
OP_ENUMERATE operatorType = iota
OP_SEQUENCE operatorType = iota
OP_LOCALVAR operatorType = iota
OP_USERVAR operatorType = iota
OP_CONSTANT operatorType = iota
OP_USERFUNC operatorType = iota
OP_BUILTINFUNC operatorType = iota
)
type Operator struct {
Type operatorType
OperandA *Operator
OperandB *Operator
Result interface{}
}
var (
operatorsPriorityList = [...]string{
"->", ";", "=", ":=", "-=", "+=", "*=", "/=", "&=", "|=", ",", "||", "&&", "==", "!=", "!", ">", "<", ">=", "<=", "+", "-", "*", "**", "/", "%", "<<", ">>", "|", "&", "^", "&^", "&~", "~", "#",
}
)
func getLocalVariable(localVars map[string]interface{}, literal string) (val interface{}, found bool) {
if localVars == nil {
return nil, false
}
val, found = localVars[literal]
return
}
func execUserFunc(f user.Func, args []interface{}) (result interface{}, err error) {
var lasterr error
for vi, variant := range f.Variants {
argsLen := len(args)
argNames := variant.ArgNames()
argNamesLen := len(argNames)
if argNamesLen != argsLen {
if argNamesLen == 0 {
if argsLen > 1 || args[0] != nil {
lasterr = fmt.Errorf("expected 0 args but got %d (#%d)", argsLen, vi)
continue
}
} else if argsLen < argNamesLen || argNames[argNamesLen-1] != "@" {
lasterr = fmt.Errorf("expected %d args but got %d (#%d)", argNamesLen, argsLen, vi)
continue
}
}
argMap := make(map[string]interface{})
for pos, name := range argNames {
if name == "@" {
argMap[name] = args[pos:]
break
} else {
argMap[name] = args[pos]
}
}
argOperators, err := Generate(variant.Args, argMap)
if err != nil {
lasterr = fmt.Errorf("%s (#%d)", err, vi)
continue
}
result, err := Calculate(argOperators, argMap)
if err != nil {
lasterr = fmt.Errorf("%s (#%d)", err, vi)
continue
}
argsCompatible := true
switch r := result.(type) {
case []interface{}:
for _, val := range r {
switch v := val.(type) {
case bool:
if !v {
argsCompatible = false
break
}
}
if !argsCompatible {
break
}
}
default:
switch val := r.(type) {
case bool:
if !val {
argsCompatible = false
}
}
}
if !argsCompatible {
lasterr = fmt.Errorf("args not compatible (#%d)", vi)
continue
}
bodyOperators, err := Generate(variant.Body, argMap)
if err != nil {
result = nil
return result, err
}
return Calculate(bodyOperators, argMap)
}
result = nil
err = lasterr
return
}
func GetType(op string) operatorType {
switch op {
case ";":
return OP_SEQUENCE
case "->":
return OP_DECLFUNC
case "=":
return OP_ASSIGN
case ":=":
return OP_LOCALASSIGN
case "-=":
return OP_DECREMENT
case "+=":
return OP_INCREMENT
case "*=":
return OP_ASSIGNMUL
case "/=":
return OP_ASSIGNDIV
case "&=":
return OP_ASSIGNBITAND
case "|=":
return OP_ASSIGNBITOR
case ",":
return OP_ENUMERATE
case "!":
return OP_LOGICNOT
case "||":
return OP_LOGICOR
case "&&":
return OP_LOGICAND
case "==":
return OP_EQUALITY
case "!=":
return OP_NOTEQ
case ">":
return OP_MORE
case "<":
return OP_LESS
case ">=":
return OP_MOREEQ
case "<=":
return OP_LESSEQ
case "-":
return OP_MINUS
case "+":
return OP_PLUS
case "*":
return OP_MULTIPLY
case "**":
return OP_POWER
case "/":
return OP_DIVIDE
case "%":
return OP_MODULO
case "<<":
return OP_LEFTSHIFT
case ">>":
return OP_RIGHTSHIFT
case "|":
return OP_BITOR
case "&":
return OP_BITAND
case "^":
return OP_BITXOR
case "&^", "&~":
return OP_BITCLEAR
case "~":
return OP_BITINVERSE
case "#":
return OP_POPCNT
default:
return -1
}
}
func Generate(words []utils.Word, localVars map[string]interface{}) (*Operator, error) {
var err error
newOp := &Operator{}
if len(words) == 0 {
newOp.Result = uint64(0)
return newOp, nil
} else if len(words) == 1 {
w := words[0]
switch w.Type {
case utils.W_UNIT:
// Try to find variable
_, found := getLocalVariable(localVars, w.Literal)
if found {
newOp.Type = OP_LOCALVAR
newOp.Result = w.Literal
} else if user.HasVariable(w.Literal) {
newOp.Type = OP_USERVAR
newOp.Result = w.Literal
} else if builtin.HasConstant(w.Literal) {
newOp.Type = OP_CONSTANT
newOp.Result = w.Literal
} else if user.HasFunction(w.Literal) {
newOp.Type = OP_USERFUNC
newOp.Result = w.Literal
} else if builtin.HasFunction(w.Literal) {
newOp.Type = OP_BUILTINFUNC
newOp.Result = w.Literal
} else {
return nil, fmt.Errorf("there is no variable named '%s'", w.Literal)
}
case utils.W_FUNC:
// Try to find function
v, found := getLocalVariable(localVars, w.Literal)
if found || user.HasVariable(w.Literal) {
if !found {
v, _ = user.GetVariable(w.Literal)
}
switch fname := v.(type) {
case string:
if user.HasFunction(fname) {
newOp.Type = OP_USERFUNC
newOp.Result = fname
return newOp, nil
} else if builtin.HasFunction(fname) {
newOp.Type = OP_BUILTINFUNC
newOp.Result = fname
return newOp, nil
}
}
}
if user.HasFunction(w.Literal) {
newOp.Type = OP_USERFUNC
newOp.Result = w.Literal
break
}
if builtin.HasFunction(w.Literal) {
newOp.Type = OP_BUILTINFUNC
newOp.Result = w.Literal
break
}
return nil, fmt.Errorf("there is no function named '%s'", w.Literal)
case utils.W_NUM_DEC, utils.W_NUM_HEX, utils.W_NUM_BIN, utils.W_NUM_SCI:
switch w.Type {
case utils.W_NUM_SCI:
num := strings.Split(w.Literal, "e")
var mantisse, order float64
mantisse, err = strconv.ParseFloat(num[0], 64)
if err != nil {
return nil, fmt.Errorf("unable to parse mantisse part of literal '%s'", w.Literal)
}
order, err = strconv.ParseFloat(num[1], 64)
if err != nil {
return nil, fmt.Errorf("unable to parse order part of literal '%s'", w.Literal)
}
newOp.Result = mantisse * math.Pow(10, order)
case utils.W_NUM_DEC:
newOp.Result, err = strconv.ParseFloat(w.Literal, 64)
if err != nil {
return nil, fmt.Errorf("unable to parse literal '%s' as number", w.Literal)
}
case utils.W_NUM_HEX:
newOp.Result, err = strconv.ParseUint(w.Literal, 16, 64)
if err != nil {
return nil, fmt.Errorf("unable to parse literal '%s' as hex number", w.Literal)
}
case utils.W_NUM_BIN:
newOp.Result, err = strconv.ParseUint(w.Literal, 2, 64)
if err != nil {
return nil, fmt.Errorf("unable to parse literal '%s' as bin number", w.Literal)
}
}
case utils.W_STR:
newOp.Result = w.Literal
}
return newOp, nil
}
minPriority := len(operatorsPriorityList)
minPriorityIndex := 0
var minPriorityWord *utils.Word
bracketsCount := 0
if words[0].Type == utils.W_CTL && words[len(words)-1].Type == utils.W_CTL {
if words[0].Literal != "(" {
return nil, fmt.Errorf("missing opening bracket")
}
if words[len(words)-1].Literal != ")" {
return nil, fmt.Errorf("missing closing bracket")
}
for i := 1; i < len(words)-1; i++ {
if words[i].Type != utils.W_CTL {
continue
}
if words[i].Literal == "(" {
bracketsCount++
} else {
bracketsCount--
if bracketsCount < 0 {
break
}
}
}
if bracketsCount >= 0 {
words = words[1 : len(words)-1]
}
}
bracketsCount = 0
for i := 0; i < len(words); i++ {
w := words[i]
if w.Type == utils.W_CTL {
if w.Literal == "(" {
bracketsCount++
} else {
bracketsCount--
}
continue
} else if w.Type == utils.W_UNIT {
// Function call detect
if i+1 < len(words)-1 && words[i+1].Type == utils.W_CTL && words[i+1].Literal == "(" {
words[i].Type = utils.W_FUNC
}
continue
}
if w.Type != utils.W_OP || bracketsCount > 0 {
continue
}
prio := -1
if GetType(w.Literal) == OP_MINUS && (i == 0 || words[i-1].Type == utils.W_OP) {
// If it is a single minus operator give it the max prioriy
prio = len(operatorsPriorityList)
} else {
for pr, lit := range operatorsPriorityList {
if lit == w.Literal {
prio = pr
break
}
}
}
if prio < 0 {
return nil, fmt.Errorf("unknown operator '%s'", w.Literal)
}
if prio <= minPriority {
minPriority = prio
minPriorityIndex = i
minPriorityWord = &words[i]
}
}
if minPriorityWord == nil {
if words[0].Type == utils.W_FUNC {
if len(words) < 3 {
return nil, fmt.Errorf("missing function '%s' arguments", words[0].Literal)
}
newOp.OperandA, err = Generate(words[:1], localVars)
if err != nil {
return nil, err
}
newOp.Type = newOp.OperandA.Type
newOp.OperandA.Type = OP_NONE
if len(words) > 3 {
newOp.OperandB, err = Generate(words[2:len(words)-1], localVars)
if err != nil {
return nil, err
}
} else {
newOp.OperandB = &Operator{}
}
} else {
return nil, fmt.Errorf("operators not found")
}
} else {
newOp.Type = GetType(minPriorityWord.Literal)
if newOp.Type < 0 {
return nil, fmt.Errorf("unknown operator '%s'", minPriorityWord.Literal)
}
if newOp.Type == OP_DECLFUNC {
// Function declaration operator
if minPriorityIndex < 3 {
return nil, fmt.Errorf("missing a function declaration on left side of operator '%s'", minPriorityWord.Literal)
} else if minPriorityIndex >= len(words)-1 {
return nil, fmt.Errorf("missing a function bidy on right side of operator '%s'", minPriorityWord.Literal)
}
// Find brackets to determine arguments
bracketsCount = 0
lastBracketIndex := -1
if words[1].Literal != "(" {
return nil, fmt.Errorf("wrong function declaration syntax, missing '('")
} else {
for i := 1; i < minPriorityIndex; i++ {
if words[i].Type == utils.W_CTL {
if words[i].Literal == "(" {
bracketsCount++
} else {
lastBracketIndex = i
bracketsCount--
}
} else {
continue
}
}
if bracketsCount > 0 || lastBracketIndex < 0 {
return nil, fmt.Errorf("wrong function declaration syntax, missing ')'")
}
}
newOp.OperandA = &Operator{
Result: words[:lastBracketIndex+1],
}
newOp.OperandB = &Operator{
Result: words[minPriorityIndex+1:],
}
return newOp, nil
} else if newOp.Type == OP_BITINVERSE || newOp.Type == OP_POPCNT || newOp.Type == OP_LOGICNOT {
// One side operators
newOp.OperandA = &Operator{}
} else if newOp.Type >= OP_ASSIGN && newOp.Type <= OP_ASSIGNDIV {
// Assign operators
if minPriorityIndex < 1 {
return nil, fmt.Errorf("missing a variable on left side of operator '%s'", minPriorityWord.Literal)
}
lit := words[minPriorityIndex-1].Literal
_, foundLocal := getLocalVariable(localVars, lit)
if foundLocal {
newOp.OperandA = &Operator{
Type: OP_LOCALVAR,
Result: lit,
}
} else if user.HasVariable(lit) {
newOp.OperandA = &Operator{
Type: OP_USERVAR,
Result: lit,
}
} else {
newOp.OperandA = &Operator{
Result: lit,
}
}
if newOp.OperandA.Type == OP_NONE {
if newOp.Type > OP_LOCALASSIGN {
return nil, fmt.Errorf("there is no user variable named '%s'", lit)
} else {
localVars[lit] = nil
}
}
} else {
newOp.OperandA, err = Generate(words[:minPriorityIndex], localVars)
if err != nil {
return nil, err
}
}
newOp.OperandB, err = Generate(words[minPriorityIndex+1:], localVars)
if err != nil {
return nil, err
}
}
return newOp, nil
}
func Calculate(op *Operator, localVars map[string]interface{}) (interface{}, error) {
var err error
if op == nil {
return nil, nil
}
if op.OperandA == nil && op.OperandB == nil {
switch op.Type {
case OP_NONE:
return op.Result, nil
case OP_LOCALVAR:
op.OperandA = &Operator{
Result: op.Result.(string),
}
op.Result, _ = getLocalVariable(localVars, op.Result.(string))
case OP_USERVAR:
op.OperandA = &Operator{
Result: op.Result.(string),
}
op.Result, _ = user.GetVariable(op.Result.(string))
case OP_CONSTANT:
op.OperandA = &Operator{
Result: op.Result.(string),
}
op.Result, _ = builtin.GetConstant(op.Result.(string))
case OP_USERFUNC:
op.OperandA = &Operator{
Result: true,
}
op.Result = op.Result.(string)
case OP_BUILTINFUNC:
op.OperandA = &Operator{
Result: true,
}
op.Result = op.Result.(string)
default:
return nil, fmt.Errorf("missing operands")
}
return op.Result, nil
} else {
if op.OperandA != nil {
op.OperandA.Result, err = Calculate(op.OperandA, localVars)
if err != nil {
return nil, err
}
}
if op.OperandB != nil {
op.OperandB.Result, err = Calculate(op.OperandB, localVars)
if err != nil {
return nil, err
}
}
}
switch op.Type {
case OP_SEQUENCE:
if op.OperandB != nil {
op.Result = op.OperandB.Result
} else {
return nil, nil
}
case OP_DECLFUNC:
leftSideWords := op.OperandA.Result.([]utils.Word)
rightSideWords := op.OperandB.Result.([]utils.Word)
funcName := leftSideWords[0].Literal
newFunc := user.FuncVariant{
Args: leftSideWords[2 : len(leftSideWords)-1],
Body: rightSideWords,
}
user.SetFunctionVariant(funcName, newFunc)
case OP_ASSIGN, OP_LOCALASSIGN, OP_DECREMENT, OP_INCREMENT, OP_ASSIGNMUL, OP_ASSIGNDIV, OP_ASSIGNBITAND, OP_ASSIGNBITOR:
switch op.Type {
case OP_ASSIGN:
op.Result = op.OperandB.Result
case OP_LOCALASSIGN:
op.Result = op.OperandB.Result
localVars[op.OperandA.Result.(string)] = op.Result
return op.Result, nil
case OP_DECREMENT:
op.Result = utils.ToNumber[float64](op.OperandA.Result) - utils.ToNumber[float64](op.OperandB.Result)
case OP_INCREMENT:
op.Result = utils.ToNumber[float64](op.OperandA.Result) + utils.ToNumber[float64](op.OperandB.Result)
case OP_ASSIGNMUL:
op.Result = utils.ToNumber[float64](op.OperandA.Result) * utils.ToNumber[float64](op.OperandB.Result)
case OP_ASSIGNDIV:
opB := utils.ToNumber[float64](op.OperandB.Result)
if opB == 0 {
op.Result = math.Inf(int(utils.ToNumber[float64](op.OperandA.Result)))
} else {
op.Result = utils.ToNumber[float64](op.OperandA.Result) / opB
}
case OP_ASSIGNBITAND:
op.Result = utils.ToNumber[uint64](op.OperandA.Result) & utils.ToNumber[uint64](op.OperandB.Result)
case OP_ASSIGNBITOR:
op.Result = utils.ToNumber[uint64](op.OperandA.Result) | utils.ToNumber[uint64](op.OperandB.Result)
}
switch op.OperandA.Type {
case OP_NONE:
user.SetVariable(op.OperandA.Result.(string), op.Result)
case OP_USERVAR:
user.SetVariable(op.OperandA.OperandA.Result.(string), op.Result)
case OP_LOCALVAR:
localVars[op.OperandA.OperandA.Result.(string)] = op.Result
default:
return nil, fmt.Errorf("try to assign non user variable")
}
case OP_LOGICNOT:
op.Result = !utils.ToBool(op.OperandB.Result)
case OP_LOGICOR:
op.Result = utils.ToBool(op.OperandA.Result) || utils.ToBool(op.OperandB.Result)
case OP_LOGICAND:
op.Result = utils.ToBool(op.OperandA.Result) && utils.ToBool(op.OperandB.Result)
case OP_EQUALITY:
op.Result = utils.ToNumber[float64](op.OperandA.Result) == utils.ToNumber[float64](op.OperandB.Result)
case OP_NOTEQ:
op.Result = utils.ToNumber[float64](op.OperandA.Result) != utils.ToNumber[float64](op.OperandB.Result)
case OP_MORE:
op.Result = utils.ToNumber[float64](op.OperandA.Result) > utils.ToNumber[float64](op.OperandB.Result)
case OP_LESS:
op.Result = utils.ToNumber[float64](op.OperandA.Result) < utils.ToNumber[float64](op.OperandB.Result)
case OP_MOREEQ:
op.Result = utils.ToNumber[float64](op.OperandA.Result) >= utils.ToNumber[float64](op.OperandB.Result)
case OP_LESSEQ:
op.Result = utils.ToNumber[float64](op.OperandA.Result) <= utils.ToNumber[float64](op.OperandB.Result)
case OP_MINUS:
op.Result = utils.ToNumber[float64](op.OperandA.Result) - utils.ToNumber[float64](op.OperandB.Result)
case OP_PLUS:
op.Result = utils.ToNumber[float64](op.OperandA.Result) + utils.ToNumber[float64](op.OperandB.Result)
case OP_MULTIPLY:
op.Result = utils.ToNumber[float64](op.OperandA.Result) * utils.ToNumber[float64](op.OperandB.Result)
case OP_DIVIDE:
opB := utils.ToNumber[float64](op.OperandB.Result)
if opB == 0 {
op.Result = math.Inf(int(utils.ToNumber[float64](op.OperandA.Result)))
} else {
op.Result = utils.ToNumber[float64](op.OperandA.Result) / opB
}
case OP_MODULO:
opB := utils.ToNumber[int64](op.OperandB.Result)
if opB == 0 {
op.Result = math.Inf(1)
} else {
op.Result = utils.ToNumber[int64](op.OperandA.Result) % opB
}
case OP_POWER:
op.Result = math.Pow(utils.ToNumber[float64](op.OperandA.Result), utils.ToNumber[float64](op.OperandB.Result))
case OP_LEFTSHIFT:
op.Result = utils.ToNumber[uint64](op.OperandA.Result) << utils.ToNumber[uint64](op.OperandB.Result)
case OP_RIGHTSHIFT:
op.Result = utils.ToNumber[uint64](op.OperandA.Result) >> utils.ToNumber[uint64](op.OperandB.Result)
case OP_BITOR:
op.Result = utils.ToNumber[uint64](op.OperandA.Result) | utils.ToNumber[uint64](op.OperandB.Result)
case OP_BITAND:
op.Result = utils.ToNumber[uint64](op.OperandA.Result) & utils.ToNumber[uint64](op.OperandB.Result)
case OP_BITXOR:
op.Result = utils.ToNumber[uint64](op.OperandA.Result) ^ utils.ToNumber[uint64](op.OperandB.Result)
case OP_BITCLEAR:
op.Result = utils.ToNumber[uint64](op.OperandA.Result) &^ utils.ToNumber[uint64](op.OperandB.Result)
case OP_POPCNT:
op.Result = uint64(bits.OnesCount64(utils.ToNumber[uint64](op.OperandB.Result)))
case OP_BITINVERSE:
op.Result = 0xFFFFFFFFFFFFFFFF ^ utils.ToNumber[uint64](op.OperandB.Result)
case OP_ENUMERATE:
switch op.OperandA.Result.(type) {
case []interface{}:
break
case nil:
op.OperandA.Result = make([]interface{}, 0)
default:
op.OperandA.Result = []interface{}{op.OperandA.Result}
}
switch op.OperandB.Result.(type) {
case []interface{}, nil:
break
default:
op.OperandB.Result = []interface{}{op.OperandB.Result}
}
if op.OperandB.Result == nil {
op.Result = op.OperandA.Result
} else {
op.Result = append(op.OperandA.Result.([]interface{}), op.OperandB.Result.([]interface{})...)
}
case OP_BUILTINFUNC:
f, _ := builtin.GetFunction(op.OperandA.Result.(string))
switch op.OperandB.Result.(type) {
case []interface{}:
op.Result, err = f.Exec(op.OperandB.Result.([]interface{})...)
default:
op.Result, err = f.Exec(op.OperandB.Result)
}
case OP_USERFUNC:
var args []interface{}
fname := op.OperandA.Result.(string)
f, _ := user.GetFunction(fname)
switch op.OperandB.Result.(type) {
case []interface{}:
args = op.OperandB.Result.([]interface{})
default:
args = []interface{}{op.OperandB.Result}
}
op.Result, err = execUserFunc(f, args)
if err != nil {
return nil, fmt.Errorf("unable to find proper '%s' function variation for argsuments: %v; (%s)", fname, args, err)
}
}
// fmt.Println(op.Result)
return op.Result, err
}