-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang.py
More file actions
115 lines (102 loc) · 2.85 KB
/
lang.py
File metadata and controls
115 lines (102 loc) · 2.85 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
from lark import Lark
from dataclasses import dataclass
@dataclass
class room:
id: int
outline: list
directions: dict
asciiChar = "@"
def makeHouse(w,h):
top = []
bottom = []
side = []
twoDHouse = []
for x in range(w):
top.append(asciiChar)
bottom.append(asciiChar)
for y in range(w):
if(y == 0): side.append(asciiChar)
elif(y == w-1): side.append(asciiChar)
else: side.append(" ")
for u in range(h):
if(u == 0): twoDHouse.append(top)
elif(u == h-1): twoDHouse.append(bottom)
else: twoDHouse.append(side)
return twoDHouse
def printHouse(house):
for i in house:
print(i)
my_grammar = """
?start: room_list
room_list: room+ end
room: "(" id size door+ ")"
id: WORD
size: NUMBER "by" NUMBER
door: "[" direction "]"
direction: WORD "wall ->" WORD
end: "."
%import common.WORD
%import common.NUMBER
%import common.WS
%ignore WS
"""
def eval_door(door):
door_pointer = {
"north": None,
"south": None,
"east": None,
"west": None,
}
for i in door:
door_pointer[str(i.children[0].children[0])] = str(i.children[0].children[1])
return (door_pointer)
def run_tree(t, env):
if t.data == 'room_list':
for child in t.children:
run_tree(child, env)
elif t.data == 'room':
holder = []
for child in t.children:
holder.append(child)
for i in range(len(holder)):
if(i == 0):
id = holder[i].children[0]
if(i == 1):
width = holder[i].children[0]
height = holder[i].children[1]
if(i == 2):
doors = []
doors.append(holder[i])
if(i >= 3):
doors.append(holder[i])
doors = eval_door(doors)
r = room(str(id), makeHouse(int(width), int(height)), doors)
env['map'][str(id)] = r
elif t.data == 'end':
curr_location = env['map']['A']
newRoom = True
while(1):
if (newRoom == True):
printHouse(curr_location.outline)
print("Your map", curr_location.directions)
userInput = input("Where do you want to move: ").lower()
if (userInput == "exit"):
break
elif (curr_location.directions[userInput] != None):
curr_location = env['map'][curr_location.directions[userInput]]
newRoom = True
else:
print("No Door in that direction")
newRoom = False
else:
raise SyntaxError("unknown tree")
parser = Lark(my_grammar)
program = """
(A 10 by 15 [north wall -> B]) (B 5 by 10 [south wall -> A] [east wall -> C]) (C 5 by 5 [west wall -> B]).
"""
env = {
'map': {}
}
parse_tree = parser.parse(program)
#print(parse_tree.pretty())
print(run_tree(parse_tree, env))