Skip to content

Commit 12eb5fc

Browse files
committed
Complete rewrite, update docs again, add for loop
The new rewrite is very close to a full release of the project. Documentation needs to be updated, syntax highlighting is incomplete, but the core functionality of the transpiling process is done.
1 parent d8af9b9 commit 12eb5fc

30 files changed

+524
-10116
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
__pycache__/
1+
__pycache__/
2+
temp.out

Examples/attributes.owpy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ Rule "Attributes"
77
Conditions
88
Position Of
99
Event Player
10-
.X.Y == True
10+
.X < 10
11+
Event Player.moving == True
12+
Event Player.jumping == True

Examples/for.owpy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Rule "For Loop"
2+
Event
3+
On Global
4+
Actions
5+
angles = [15, 30, 45, 60, 75, 90]
6+
angles2 = range(15, 91, 15)
7+
for ang in angles:
8+
Msg(Event Player, ang)

Examples/func_args.owpy

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
All
2020
All
2121
Conditions
22-
Array Contains
23-
Players In Radius
24-
Center: origin
25-
Radius: radius
26-
Team: All
27-
LOS: Surfaces
28-
Event Player
22+
Event Player in Players In Radius
23+
Center: origin
24+
Radius: radius
25+
Team: All
26+
LOS: Surfaces
2927
== True
3028
Actions
3129
Teleport

Examples/time.owpy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
Rule "Time Shorthands"
22
Actions
33
Wait(16ms)
4-
Wait 3.5s
54
Wait /* It takes a parameter, so indent if you please! */
65
0.35min

Examples/variables.owpy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ Rule "Variables"
33
On Global
44

55
Actions
6-
pVar h@Event Player = 1
6+
pvar h@Event Player = 1

OWScript.py

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,38 @@
11
import argparse
2-
import os
3-
import re
42
import sys
5-
from antlr4 import *
6-
from src.OWScriptLexer import OWScriptLexer
7-
from src.OWScriptParser import OWScriptParser
8-
from src.ASTBuilder import ASTBuilder
9-
from src.Transpiler import Transpiler
3+
from OWScript.Lexer import Lexer
4+
from OWScript.Parser import Parser
5+
from OWScript.Transpiler import Transpiler
6+
7+
class DEBUG:
8+
"""Bit flags used for debug code."""
9+
TOKENS = 1
10+
TREE = 2
11+
12+
def transpile(text, minify=False, save=None, debug=0):
13+
"""Transpiles an OWScript code into Overwatch Workshop rules."""
14+
lexer = Lexer(text=text)
15+
tokens = lexer.lex()
16+
if debug & DEBUG.TOKENS:
17+
lexer.print_tokens()
18+
parser = Parser(tokens=tokens)
19+
tree = parser.script()
20+
if debug & DEBUG.TREE:
21+
print(tree.string())
22+
transpiler = Transpiler(tree=tree)
23+
code = transpiler.run()
24+
print(code)
25+
1026

11-
class UppercaseStream(FileStream):
12-
def _loadString(self):
13-
self._index = 0
14-
self.data = [ord(c.upper()) for c in self.strdata]
15-
self._size = len(self.data)
16-
17-
def process(code, minify=False, save=None):
18-
input_stream = UppercaseStream(code, 'utf-8')
19-
lexer = OWScriptLexer(input_stream)
20-
stream = CommonTokenStream(lexer)
21-
parser = OWScriptParser(stream)
22-
ast = ASTBuilder().run(parser.script())
23-
# print(ast)
24-
output = Transpiler().run(ast)
25-
if minify:
26-
output = re.sub(r'[\s\n]*', '', output)
27-
if not save:
28-
sys.stdout.write(output)
29-
else:
30-
with open(save, 'w') as f:
31-
f.write(output)
32-
3327
if __name__ == '__main__':
34-
sys.path.append(os.path.join(os.getcwd(), 'src'))
3528
parser = argparse.ArgumentParser(description='Generate Overwatch Workshop code from OWScript')
3629
parser.add_argument('input', nargs='*', type=str, help='Standard input to process')
3730
parser.add_argument('-m', '--min', action='store_true', help='Minifies the output by removing whitespace')
3831
parser.add_argument('-s', '--save', help='Save the output to a file instead of printing it')
32+
parser.add_argument('-d', '--debug', type=int, help='Debugging tool used for development')
3933
args = parser.parse_args()
40-
if not args.input:
41-
process(sys.stdin)
42-
for file in args.input:
43-
process(file, minify=args.min, save=args.save)
34+
file_input = args.input[0] if args.input else sys.stdin
35+
with open(file_input) as f:
36+
text = f.read()
37+
transpile(text, minify=args.min, save=args.save, debug=args.debug)
38+

OWScript/AST.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
class AST:
2+
children = []
23
def __init__(self):
34
self.children = []
45

56
@property
67
def format_children(self):
78
return ', '.join(map(repr, self.children))
89

10+
def string(self, indent=0):
11+
string = ''
12+
if not self.__class__ == Block:
13+
string += ' ' * indent + '{}'.format(self.__class__.__name__) + '\n'
14+
else:
15+
indent -= 3
16+
for child in self.children:
17+
string += child.string(indent=indent + 3)
18+
return string
19+
920
def __repr__(self):
1021
if not self.children:
1122
return self.__class__.__name__
@@ -69,15 +80,18 @@ class Const(Terminal):
6980
class String(Terminal):
7081
pass
7182

83+
class Time(Terminal):
84+
pass
85+
7286
class Numeral(Terminal):
7387
pass
7488

7589
class OWID(Data):
7690
pass
7791

7892
class Array(AST):
79-
def __init__(self):
80-
self.elements = []
93+
def __init__(self, elements=None):
94+
self.elements = elements or []
8195

8296
def append(self, elem):
8397
self.elements.append(elem)
@@ -120,6 +134,32 @@ def __init__(self, cond, true_block, false_block=None):
120134
def __repr__(self):
121135
return 'if {}: {} | else: {}'.format(self.cond, self.true_block, self.false_block)
122136

137+
class While:
138+
def __init__(self, cond, body):
139+
self.cond = cond
140+
self.body = body
141+
142+
def __repr__(self):
143+
return 'while {}: {}'.format(self.cond, self.body)
144+
145+
class For:
146+
def __init__(self, pointer, iterable, body):
147+
self.pointer = pointer
148+
self.iterable = iterable
149+
self.body = body
150+
151+
def __repr__(self):
152+
return 'for {} in {}: {}'.format(self.pointer, self.iterable, self.body)
153+
154+
class Function(AST):
155+
def __init__(self, name, params):
156+
super().__init__()
157+
self.name = name
158+
self.params = params
159+
160+
def __repr__(self):
161+
return '%{}({}): {}'.format(self.name, ', '.join(self.params), self.format_children)
162+
123163
class Attribute(Trailer):
124164
def __init__(self, name, parent):
125165
super().__init__(parent)

0 commit comments

Comments
 (0)