forked from Sidoine/Ovale
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulationCraft.lua
More file actions
5475 lines (5239 loc) · 189 KB
/
SimulationCraft.lua
File metadata and controls
5475 lines (5239 loc) · 189 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
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[--------------------------------------------------------------------
Copyright (C) 2014, 2015 Johnny C. Lam.
See the file LICENSE.txt for copying permission.
--]]--------------------------------------------------------------------
local OVALE, Ovale = ...
local OvaleSimulationCraft = Ovale:NewModule("OvaleSimulationCraft")
Ovale.OvaleSimulationCraft = OvaleSimulationCraft
--<private-static-properties>
local AceConfig = LibStub("AceConfig-3.0")
local AceConfigDialog = LibStub("AceConfigDialog-3.0")
local L = Ovale.L
local OvaleDebug = Ovale.OvaleDebug
local OvaleOptions = Ovale.OvaleOptions
local OvalePool = Ovale.OvalePool
-- Forward declarations for module dependencies.
local OvaleAST = nil
local OvaleCompile = nil
local OvaleData = nil
local OvaleHonorAmongThieves = nil
local OvaleLexer = nil
local OvalePower = nil
local OvaleScripts = nil
local format = string.format
local gmatch = string.gmatch
local gsub = string.gsub
local ipairs = ipairs
local next = next
local pairs = pairs
local rawset = rawset
local strfind = string.find
local strlen = string.len
local strlower = string.lower
local strmatch = string.match
local strsub = string.sub
local strupper = string.upper
local tconcat = table.concat
local tinsert = table.insert
local tonumber = tonumber
local tostring = tostring
local tremove = table.remove
local tsort = table.sort
local type = type
local wipe = wipe
local yield = coroutine.yield
local RAID_CLASS_COLORS = RAID_CLASS_COLORS
-- Keywords for SimulationCraft action lists.
local KEYWORD = {}
local MODIFIER_KEYWORD = {
["ammo_type"] = true,
["attack_speed"] = true,
["chain"] = true,
["choose"] = true,
["cooldown"] = true,
["cooldown_stddev"] = true,
["cycle_targets"] = true,
["damage"] = true,
["early_chain_if"] = true,
["extra_amount"] = true,
["five_stacks"] = true,
["for_next"] = true,
["if"] = true,
["interrupt"] = true,
["interrupt_if"] = true,
["interrupt_immediate"] = true,
["lethal"] = true,
["line_cd"] = true,
["max_cycle_targets"] = true,
["max_energy"] = true,
["min_frenzy"] = true,
["moving"] = true,
["name"] = true,
["nonlethal"] = true,
["op"] = true,
["range"] = true,
["sec"] = true,
["slot"] = true,
["sync"] = true,
["sync_weapons"] = true,
["target"] = true,
["target_if"] = true,
["target_if_first"] = true, -- "target_if_<type>" is a fake modifier.
["target_if_max"] = true,
["target_if_min"] = true,
["travel_speed"] = true,
["type"] = true,
["value"] = true,
["wait"] = true,
["wait_on_ready"] = true,
["weapon"] = true,
}
local LITTERAL_MODIFIER = {
["name"] = true
}
local FUNCTION_KEYWORD = {
["ceil"] = true,
["floor"] = true,
}
local SPECIAL_ACTION = {
["apply_poison"] = true,
["auto_attack"] = true,
["call_action_list"] = true,
["cancel_buff"] = true,
["cancel_metamorphosis"] = true,
["exotic_munitions"] = true,
["flask"] = true,
["food"] = true,
["health_stone"] = true,
["pool_resource"] = true,
["potion"] = true,
["run_action_list"] = true,
["snapshot_stats"] = true,
["stance"] = true,
["start_moving"] = true,
["stealth"] = true,
["stop_moving"] = true,
["swap_action_list"] = true,
["use_item"] = true,
["variable"] = true,
["wait"] = true,
}
local RUNE_OPERAND = {
["rune"] = "rune"
}
do
-- All expression keywords are keywords.
for keyword, value in pairs(MODIFIER_KEYWORD) do
KEYWORD[keyword] = value
end
-- All function keywords are keywords.
for keyword, value in pairs(FUNCTION_KEYWORD) do
KEYWORD[keyword] = value
end
-- All special actions are keywords.
for keyword, value in pairs(SPECIAL_ACTION) do
KEYWORD[keyword] = value
end
end
-- Table of pattern/tokenizer pairs for SimulationCraft action lists.
local MATCHES = nil
-- Unary and binary operators with precedence.
local UNARY_OPERATOR = {
["!"] = { "logical", 15 },
["-"] = { "arithmetic", 50 },
["@"] = { "arithmetic", 50 },
}
local BINARY_OPERATOR = {
-- logical
["|"] = { "logical", 5, "associative" },
["^"] = { "logical", 8, "associative" },
["&"] = { "logical", 10, "associative" },
-- comparison
["!="] = { "compare", 20 },
["<"] = { "compare", 20 },
["<="] = { "compare", 20 },
["="] = { "compare", 20 },
[">"] = { "compare", 20 },
[">="] = { "compare", 20 },
["~"] = { "compare", 20 },
["!~"] = { "compare", 20 },
-- addition, subtraction
["+"] = { "arithmetic", 30, "associative" },
["-"] = { "arithmetic", 30 },
-- multiplication, division, modulus
["%"] = { "arithmetic", 40 },
["*"] = { "arithmetic", 40, "associative" },
}
-- INDENT[k] is a string of k concatenated tabs.
local INDENT = {}
do
INDENT[0] = ""
local metatable = {
__index = function(tbl, key)
key = tonumber(key)
if key > 0 then
local s = tbl[key - 1] .. "\t"
rawset(tbl, key, s)
return s
end
return INDENT[0]
end,
}
setmetatable(INDENT, metatable)
end
local EMIT_DISAMBIGUATION = {}
local EMIT_EXTRA_PARAMETERS = {}
local OPERAND_TOKEN_PATTERN = "[^.]+"
local POTION_STAT = {
["draenic_agility"] = "agility",
["draenic_armor"] = "armor",
["draenic_intellect"] = "intellect",
["draenic_strength"] = "strength",
["jade_serpent"] = "intellect",
["mogu_power"] = "strength",
["mountains"] = "armor",
["tolvir"] = "agility",
["virmens_bite"] = "agility",
}
-- Mark() and Sweep() static variables.
-- functionDefined[name] = true if the function is declared with AddFunction(), or false otherwise.
local self_functionDefined = {}
-- functionUsed[name] = true if the function is used within the script, or false otherwise.
local self_functionUsed = {}
local self_outputPool = OvalePool("OvaleSimulationCraft_outputPool")
local self_childrenPool = OvalePool("OvaleSimulationCraft_childrenPool")
local self_pool = OvalePool("OvaleSimulationCraft_pool")
do
self_pool.Clean = function(self, node)
if node.child then
self_childrenPool:Release(node.child)
node.child = nil
end
end
end
-- Save the most recent profile entered into the SimulationCraft input window.
local self_lastSimC = nil
-- Save the most recent script translated from the profile in the SimulationCraft input window.
local self_lastScript = nil
do
-- Add a slash command "/ovale simc" to access the GUI for this module.
local actions = {
simc = {
name = "SimulationCraft",
type = "execute",
func = function()
local appName = OvaleSimulationCraft:GetName()
AceConfigDialog:SetDefaultSize(appName, 700, 550)
AceConfigDialog:Open(appName)
end,
},
}
-- Inject into OvaleOptions.
for k, v in pairs(actions) do
OvaleOptions.options.args.actions.args[k] = v
end
OvaleOptions:RegisterOptions(OvaleSimulationCraft)
end
-- XXX Temporary hard-coding of tags and tag priorities.
local OVALE_TAGS = { "main", "shortcd", "cd" }
local OVALE_TAG_PRIORITY = {}
do
for i, tag in pairs(OVALE_TAGS) do
OVALE_TAG_PRIORITY[tag] = i * 10
end
end
do
local defaultDB = {
overrideCode = "",
}
-- Insert defaults into OvaleOptions.
for k, v in pairs(defaultDB) do
OvaleOptions.defaultDB.profile[k] = v
end
OvaleOptions:RegisterOptions(OvaleSimulationCraft)
end
--</private-static-properties>
--<private-static-methods>
-- Implementation of PHP-like print_r() taken from http://lua-users.org/wiki/TableSerialization.
-- This is used to print out a table, but has been modified to print out an AST.
local function print_r(node, indent, done, output)
done = done or {}
output = output or {}
indent = indent or ''
for key, value in pairs(node) do
if type(value) == "table" then
if done[value] then
tinsert(output, indent .. "[" .. tostring(key) .. "] => (self_reference)")
else
-- Shortcut conditional allocation
done[value] = true
tinsert(output, indent .. "[" .. tostring(key) .. "] => {")
print_r(value, indent .. " ", done, output)
tinsert(output, indent .. "}")
end
else
tinsert(output, indent .. "[" .. tostring(key) .. "] => " .. tostring(value))
end
end
return output
end
-- Get a new node from the pool and save it in the nodes array.
local function NewNode(nodeList, hasChild)
local node = self_pool:Get()
if nodeList then
local nodeId = #nodeList + 1
node.nodeId = nodeId
nodeList[nodeId] = node
end
if hasChild then
node.child = self_childrenPool:Get()
end
return node
end
--[[---------------------------------------------
Lexer functions (for use with OvaleLexer)
--]]---------------------------------------------
local function TokenizeName(token)
if KEYWORD[token] then
return yield("keyword", token)
else
return yield("name", token)
end
end
local function TokenizeNumber(token, options)
if options and options.number then
token = tonumber(token)
end
return yield("number", token)
end
local function Tokenize(token)
return yield(token, token)
end
local function NoToken()
return yield(nil)
end
do
MATCHES = {
{ "^%d+%.?%d*", TokenizeNumber },
{ "^[%a_][%w_]*[.:]?[%w_.]*", TokenizeName },
{ "^!=", Tokenize },
{ "^<=", Tokenize },
{ "^>=", Tokenize },
{ "^!~", Tokenize },
{ "^.", Tokenize },
{ "^$", NoToken },
}
end
local function GetTokenIterator(s)
local exclude = { space = false, comments = false }
return OvaleLexer.scan(s, MATCHES, exclude)
end
--[[------------------------
"Unparser" functions
--]]------------------------
-- Return the precedence of an operator in the given node.
-- Returns nil if the node is not an expression node.
local function GetPrecedence(node)
local precedence = node.precedence
if not precedence then
local operator = node.operator
if operator then
if node.expressionType == "unary" and UNARY_OPERATOR[operator] then
precedence = UNARY_OPERATOR[operator][2]
elseif node.expressionType == "binary" and BINARY_OPERATOR[operator] then
precedence = BINARY_OPERATOR[operator][2]
end
end
end
return precedence
end
local UNPARSE_VISITOR = nil
local function Unparse(node)
local visitor = UNPARSE_VISITOR[node.type]
if not visitor then
OvaleSimulationCraft:Error("Unable to unparse node of type '%s'.", node.type)
else
return visitor(node)
end
end
local function UnparseAction(node)
local output = self_outputPool:Get()
output[#output + 1] = node.name
for modifier, expressionNode in pairs(node.child) do
output[#output + 1] = modifier .. "=" .. Unparse(expressionNode)
end
local s = tconcat(output, ",")
self_outputPool:Release(output)
return s
end
local function UnparseActionList(node)
local output = self_outputPool:Get()
local listName
if node.name == "_default" then
listName = "action"
else
listName = "action." .. node.name
end
output[#output + 1] = ""
for i, actionNode in pairs(node.child) do
local operator = (i == 1) and "=" or "+=/"
output[#output + 1] = listName .. operator .. Unparse(actionNode)
end
local s = tconcat(output, "\n")
self_outputPool:Release(output)
return s
end
local function UnparseExpression(node)
local expression
local precedence = GetPrecedence(node)
if node.expressionType == "unary" then
local rhsExpression
local rhsNode = node.child[1]
local rhsPrecedence = GetPrecedence(rhsNode)
if rhsPrecedence and precedence >= rhsPrecedence then
rhsExpression = "(" .. Unparse(rhsNode) .. ")"
else
rhsExpression = Unparse(rhsNode)
end
expression = node.operator .. rhsExpression
elseif node.expressionType == "binary" then
local lhsExpression, rhsExpression
local lhsNode = node.child[1]
local lhsPrecedence = GetPrecedence(lhsNode)
if lhsPrecedence and lhsPrecedence < precedence then
lhsExpression = "(" .. Unparse(lhsNode) .. ")"
else
lhsExpression = Unparse(lhsNode)
end
local rhsNode = node.child[2]
local rhsPrecedence = GetPrecedence(rhsNode)
if rhsPrecedence and precedence > rhsPrecedence then
rhsExpression = "(" .. Unparse(rhsNode) .. ")"
elseif rhsPrecedence and precedence == rhsPrecedence then
if BINARY_OPERATOR[node.operator][3] == "associative" and node.operator == rhsNode.operator then
rhsExpression = Unparse(rhsNode)
else
rhsExpression = "(" .. Unparse(rhsNode) .. ")"
end
else
rhsExpression = Unparse(rhsNode)
end
expression = lhsExpression .. node.operator .. rhsExpression
end
return expression
end
local function UnparseFunction(node)
return node.name .. "(" .. Unparse(node.child[1]) .. ")"
end
local function UnparseNumber(node)
return tostring(node.value)
end
local function UnparseOperand(node)
return node.name
end
do
UNPARSE_VISITOR = {
["action"] = UnparseAction,
["action_list"] = UnparseActionList,
["arithmetic"] = UnparseExpression,
["compare"] = UnparseExpression,
["function"] = UnparseFunction,
["logical"] = UnparseExpression,
["number"] = UnparseNumber,
["operand"] = UnparseOperand,
}
end
--[[--------------------
Parser functions
--]]--------------------
-- Prints the error message and the next 20 tokens from tokenStream.
local function SyntaxError(tokenStream, ...)
OvaleSimulationCraft:Print(...)
local context = { "Next tokens:" }
for i = 1, 20 do
local tokenType, token = tokenStream:Peek(i)
if tokenType then
context[#context + 1] = token
else
context[#context + 1] = "<EOS>"
break
end
end
OvaleSimulationCraft:Print(tconcat(context, " "))
end
-- Forward declarations of parser functions needed to implement a recursive descent parser.
local ParseAction = nil
local ParseActionList = nil
local ParseExpression = nil
local ParseFunction = nil
local ParseModifier = nil
local ParseNumber = nil
local ParseOperand = nil
local ParseParentheses = nil
local ParseSimpleExpression = nil
local function TicksRemainTranslationHelper(p1, p2, p3, p4)
if p4 then
return p1 .. p2 .. "<" .. tostring(tonumber(p4) + 1)
else
return p1 .. "<" .. tostring(tonumber(p3) + 1)
end
end
ParseAction = function(action, nodeList, annotation)
local ok = true
local stream = action
do
-- Fix "|" being silently replaced by "||" in WoW strings entered via an edit box.
stream = gsub(stream, "||", "|")
end
do
-- Fix bugs in SimulationCraft action lists.
-- ",," into ","
stream = gsub(stream, ",,", ",")
-- "&&" into "&"
stream = gsub(stream, "%&%&", "&")
-- "target.target." into "target."
stream = gsub(stream, "target%.target%.", "target.")
end
do
-- Changes to SimulationCraft action lists for easier translation into Ovale timespan concept.
-- "active_dot.dotName=0" into "!(active_dot.dotName>0)"
stream = gsub(stream, "(active_dot%.[%w_]+)=0", "!(%1>0)")
-- "cooldown_remains=0" into "!(cooldown_remains>0)"
stream = gsub(stream, "([^_%.])(cooldown_remains)=0", "%1!(%2>0)")
stream = gsub(stream, "([a-z_%.]+%.cooldown_remains)=0", "!(%1>0)")
-- "remains=0" into "!(remains>0)"
stream = gsub(stream, "([^_%.])(remains)=0", "%1!(%2>0)")
stream = gsub(stream, "([a-z_%.]+%.remains)=0", "!(%1>0)")
-- "ticks_remain=1" into "ticks_remain<2"
-- "ticks_remain<=N" into "ticks_remain<N+1"
stream = gsub(stream, "([^_%.])(ticks_remain)(<?=)([0-9]+)", TicksRemainTranslationHelper)
stream = gsub(stream, "([a-z_%.]+%.ticks_remain)(<?=)([0-9]+)", TicksRemainTranslationHelper)
end
do
-- Convert "@" absolute value unary operator for easier translation into Ovale timespan concept.
-- "@expr1<N" into "(expr1<N&expr1>-N)"
stream = gsub(stream, "%@([a-z_%.]+)<(=?)([0-9]+)", "(%1<%2%3&%1>%2-%3)")
-- "@expr1>N" into "(expr1>N|expr1<-N)"
stream = gsub(stream, "%@([a-z_%.]+)>(=?)([0-9]+)", "(%1>%2%3|%1<%2-%3)")
end
do
-- Convert "!foo.cooldown.up" into "foo.cooldown.down" to avoid emitting "not not ...".
stream = gsub(stream, "!([a-z_%.]+)%.cooldown%.up", "%1.cooldown.down")
end
do
--[[
Mage APLs have a custom "target_if=max:..." modifier to the "choose_target"
action which does not adhere to the language standard.
--]]
stream = gsub(stream, ",target_if=first:", ",target_if_first=")
stream = gsub(stream, ",target_if=max:", ",target_if_max=")
stream = gsub(stream, ",target_if=min:", ",target_if_min=")
end
do
--"sim.target" is the "priority target" property of the simulator, change into "sim_target".
stream = gsub(stream, "sim.target", "sim_target")
end
local tokenStream = OvaleLexer("SimulationCraft", GetTokenIterator(stream))
-- Consume the action.
local name
do
local tokenType, token = tokenStream:Consume()
if (tokenType == "keyword" and SPECIAL_ACTION[token]) or tokenType == "name" then
name = token
else
SyntaxError(tokenStream, "Syntax error: unexpected token '%s' when parsing action line '%s'; name or special action expected.", token, action)
ok = false
end
end
local child = self_childrenPool:Get()
if ok then
local tokenType, token = tokenStream:Peek()
while ok and tokenType do
if tokenType == "," then
-- Consume the ',' token.
tokenStream:Consume()
local modifier, expressionNode
ok, modifier, expressionNode = ParseModifier(tokenStream, nodeList, annotation)
if ok then
child[modifier] = expressionNode
tokenType, token = tokenStream:Peek()
end
else
SyntaxError(tokenStream, "Syntax error: unexpected token '%s' when parsing action line '%s'; ',' expected.", token, action)
ok = false
end
end
end
local node
if ok then
node = NewNode(nodeList)
node.type = "action"
node.action = action
node.name = name
node.child = child
-- Save first action for "sync=action" references.
annotation.sync = annotation.sync or {}
annotation.sync[name] = annotation.sync[name] or node
else
self_childrenPool:Release(child)
end
return ok, node
end
ParseActionList = function(name, actionList, nodeList, annotation)
local ok = true
local child = self_childrenPool:Get()
for action in gmatch(actionList, "[^/]+") do
local actionNode
ok, actionNode = ParseAction(action, nodeList, annotation)
if ok then
child[#child + 1] = actionNode
else
break
end
end
local node
if ok then
node = NewNode(nodeList)
node.type = "action_list"
node.name = name
node.child = child
else
self_childrenPool:Release(child)
end
return ok, node
end
--[[
Operator-precedence parser for logical and arithmetic expressions.
Implementation taken from Wikipedia:
http://en.wikipedia.org/wiki/Operator-precedence_parser
--]]
ParseExpression = function(tokenStream, nodeList, annotation, minPrecedence)
minPrecedence = minPrecedence or 0
local ok = true
local node
-- Check for unary operator expressions first as they decorate the underlying expression.
do
local tokenType, token = tokenStream:Peek()
if tokenType then
local opInfo = UNARY_OPERATOR[token]
if opInfo then
local opType, precedence = opInfo[1], opInfo[2]
local asType = (opType == "logical") and "boolean" or "value"
tokenStream:Consume()
local operator = token
local rhsNode
ok, rhsNode = ParseExpression(tokenStream, nodeList, annotation, precedence)
if ok then
if operator == "-" and rhsNode.type == "number" then
-- Elide the unary negation operator into the number.
rhsNode.value = -1 * rhsNode.value
node = rhsNode
else
node = NewNode(nodeList, true)
node.type = opType
node.expressionType = "unary"
node.operator = operator
node.precedence = precedence
node.child[1] = rhsNode
rhsNode.asType = asType
end
end
else
ok, node = ParseSimpleExpression(tokenStream, nodeList, annotation)
if ok and node then
node.asType = "boolean"
end
end
end
end
-- Peek at the next token to see if it is a binary operator.
while ok do
local keepScanning = false
local tokenType, token = tokenStream:Peek()
if tokenType then
local opInfo = BINARY_OPERATOR[token]
if opInfo then
local opType, precedence = opInfo[1], opInfo[2]
local asType = (opType == "logical") and "boolean" or "value"
if precedence and precedence > minPrecedence then
keepScanning = true
tokenStream:Consume()
local operator = token
local lhsNode = node
local rhsNode
ok, rhsNode = ParseExpression(tokenStream, nodeList, annotation, precedence)
if ok then
node = NewNode(nodeList, true)
node.type = opType
node.expressionType = "binary"
node.operator = operator
node.precedence = precedence
node.child[1] = lhsNode
node.child[2] = rhsNode
lhsNode.asType = asType
rhsNode.asType = asType
-- Left-rotate tree to preserve precedence.
while node.type == rhsNode.type and node.operator == rhsNode.operator and BINARY_OPERATOR[node.operator][3] == "associative" and rhsNode.expressionType == "binary" do
node.child[2] = rhsNode.child[1]
rhsNode.child[1] = node
node = rhsNode
rhsNode = node.child[2]
end
end
end
end
end
if not keepScanning then
break
end
end
return ok, node
end
ParseFunction = function(tokenStream, nodeList, annotation)
local ok = true
local name
-- Consume the name.
do
local tokenType, token = tokenStream:Consume()
if tokenType == "keyword" and FUNCTION_KEYWORD[token] then
name = token
else
SyntaxError(tokenStream, "Syntax error: unexpected token '%s' when parsing FUNCTION; name expected.", token)
ok = false
end
end
-- Consume the left parenthesis.
if ok then
local tokenType, token = tokenStream:Consume()
if tokenType ~= "(" then
SyntaxError(tokenStream, "Syntax error: unexpected token '%s' when parsing FUNCTION; '(' expected.", token)
ok = false
end
end
-- Consume the function argument.
local argumentNode
if ok then
ok, argumentNode = ParseExpression(tokenStream, nodeList, annotation)
end
-- Consume the right parenthesis.
if ok then
local tokenType, token = tokenStream:Consume()
if tokenType ~= ")" then
SyntaxError(tokenStream, "Syntax error: unexpected token '%s' when parsing FUNCTION; ')' expected.", token)
ok = false
end
end
-- Create the AST node.
local node
if ok then
node = NewNode(nodeList, true)
node.type = "function"
node.name = name
node.child[1] = argumentNode
end
return ok, node
end
ParseIdentifier = function(tokenStream, nodeList, annotation)
local tokenType, token = tokenStream:Consume()
local node = NewNode(nodeList)
node.type = "operand"
node.name = token
annotation.operand = annotation.operand or {}
annotation.operand[#annotation.operand + 1] = node
return true, node
end
ParseModifier = function(tokenStream, nodeList, annotation)
local ok = true
local name
do
local tokenType, token = tokenStream:Consume()
if tokenType == "keyword" and MODIFIER_KEYWORD[token] then
name = token
else
SyntaxError(tokenStream, "Syntax error: unexpected token '%s' when parsing action line; expression keyword expected.", token)
ok = false
end
end
if ok then
-- Consume the '=' token.
local tokenType, token = tokenStream:Consume()
if tokenType ~= "=" then
SyntaxError(tokenStream, "Syntax error: unexpected token '%s' when parsing action line; '=' expected.", token)
ok = false
end
end
local expressionNode
if ok then
if LITTERAL_MODIFIER[name] then
ok, expressionNode = ParseIdentifier(tokenStream, nodeList, annotation)
else
ok, expressionNode = ParseExpression(tokenStream, nodeList, annotation)
if ok and expressionNode and name == "sec" then
expressionNode.asType = "value"
end
end
end
return ok, name, expressionNode
end
ParseNumber = function(tokenStream, nodeList, annotation)
local ok = true
local value
-- Consume the number.
do
local tokenType, token = tokenStream:Consume()
if tokenType == "number" then
value = tonumber(token)
else
SyntaxError(tokenStream, "Syntax error: unexpected token '%s' when parsing NUMBER; number expected.", token)
ok = false
end
end
-- Create the AST node.
local node
if ok then
node = NewNode(nodeList)
node.type = "number"
node.value = value
end
return ok, node
end
ParseOperand = function(tokenStream, nodeList, annotation)
local ok = true
local name
-- Consume the operand.
do
local tokenType, token = tokenStream:Consume()
if tokenType == "name" then
name = token
elseif tokenType == "keyword" and token == "target" then
-- Allow a bare "target" to be used as an operand.
name = token
else
SyntaxError(tokenStream, "Syntax error: unexpected token '%s' when parsing OPERAND; operand expected.", token)
ok = false
end
end
-- Create the AST node.
local node
if ok then
node = NewNode(nodeList)
node.type = "operand"
node.name = name
node.rune = RUNE_OPERAND[name]
if node.rune then
local firstCharacter = strsub(name, 1, 1)
node.includeDeath = (firstCharacter == "B" or firstCharacter == "F" or firstCharacter == "U")
end
annotation.operand = annotation.operand or {}
annotation.operand[#annotation.operand + 1] = node
end
return ok, node
end
ParseParentheses = function(tokenStream, nodeList, annotation)
local ok = true
local leftToken, rightToken
-- Consume the left parenthesis.
do
local tokenType, token = tokenStream:Consume()
if tokenType == "(" then
leftToken, rightToken = "(", ")"
elseif tokenType == "{" then
leftToken, rightToken = "{", "}"
else
SyntaxError(tokenStream, "Syntax error: unexpected token '%s' when parsing PARENTHESES; '(' or '{' expected.", token)
ok = false
end
end
-- Consume the inner expression.
local node
if ok then
ok, node = ParseExpression(tokenStream, nodeList, annotation)
end
-- Consume the right parenthesis.
if ok then
local tokenType, token = tokenStream:Consume()
if tokenType ~= rightToken then
SyntaxError(tokenStream, "Syntax error: unexpected token '%s' when parsing PARENTHESES; '%s' expected.", token, rightToken)
ok = false
end
end
-- Create the AST node.
if ok then
node.left = leftToken
node.right = rightToken
end
return ok, node
end
ParseSimpleExpression = function(tokenStream, nodeList, annotation)
local ok = true
local node
local tokenType, token = tokenStream:Peek()
if tokenType == "number" then
ok, node = ParseNumber(tokenStream, nodeList, annotation)
elseif tokenType == "keyword" then
if FUNCTION_KEYWORD[token] then
ok, node = ParseFunction(tokenStream, nodeList, annotation)
elseif token == "target" then
ok, node = ParseOperand(tokenStream, nodeList, annotation)
end
elseif tokenType == "name" then
ok, node = ParseOperand(tokenStream, nodeList, annotation)
elseif tokenType == "(" then
ok, node = ParseParentheses(tokenStream, nodeList, annotation)
else
SyntaxError(tokenStream, "Syntax error: unexpected token '%s' when parsing SIMPLE EXPRESSION", token)
tokenStream:Consume()
ok = false
end
return ok, node
end
--[[-----------------------------
Code generation functions
--]]-----------------------------
local CamelCase = nil
do
local function CamelCaseHelper(first, rest)
return strupper(first) .. strlower(rest)
end
CamelCase = function(s)
local tc = gsub(s, "(%a)(%w*)", CamelCaseHelper)
return gsub(tc, "[%s_]", "")
end
end
local function CamelSpecialization(annotation)
local output = self_outputPool:Get()
local profileName, class, specialization = annotation.name, annotation.class, annotation.specialization
if specialization then
output[#output + 1] = specialization
end
if strmatch(profileName, "_1[hH]_") then
if class == "DEATHKNIGHT" and specialization == "frost" then
output[#output + 1] = "dual wield"
elseif class == "WARRIOR" and specialization == "fury" then
output[#output + 1] = "single minded fury"
end
elseif strmatch(profileName, "_2[hH]_") then
if class == "DEATHKNIGHT" and specialization == "frost" then
output[#output + 1] = "two hander"
elseif class == "WARRIOR" and specialization == "fury" then
output[#output + 1] = "titans grip"
end
elseif strmatch(profileName, "_[gG]ladiator_") then
output[#output + 1] = "gladiator"
end
local outputString = CamelCase(tconcat(output, " "))
self_outputPool:Release(output)
return outputString
end
local function OvaleFunctionName(name, annotation)
local functionName = CamelCase(name .. " actions")
if annotation.specialization then
functionName = CamelSpecialization(annotation) .. functionName
end
return functionName
end
local function AddSymbol(annotation, symbol)
local symbolTable = annotation.symbolTable or {}