-
Notifications
You must be signed in to change notification settings - Fork 0
/
shibe.py
132 lines (117 loc) · 3.34 KB
/
shibe.py
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
import re
virtual = {}
def declaration(scanner, token):
varname = scanner.match.group(2)
value = scanner.match.group(3)
virtual[varname] = value
return "DECLARE", varname, value
def equality(scanner, token):
return "EQUALITY", scanner.match.group(1), scanner.match.group(2), scanner.match.group(3)
def simpleMath(op, val1, val2):
real1 = 0
real2 = 0
type1 = type(val1)
type2 = type(val2)
if type1 in [int,float]:
real1 = val1
if type2 in [int,float]:
real2 = val2
if type1 == str:
if val1 in virtual: # it's a variable
real1 = virtual[val1]
elif '.' in val1: #its' a decimal
real1 = float(val1)
elif '.' not in val1: #its an int.
real1 = int(val1)
if type2 == str:
if val2 in virtual:
real2 = virtual[val2]
elif '.' in val1:
real2 = float(val2)
elif '.' not in val1:
real2 = int(val2)
if op == '+':
return float(real1) + float(real2)
elif op == '-':
print type(real1), type(real2)
print real1,real2
return float(real1) - float(real2)
elif op == '/':
if float(real2) == 0:
return 0.0
else:
return float(real1) / float(real2)
elif op == "*":
return float(real1) * float(real2)
def addition(scanner, token):
result_varname = scanner.match.group(1)
operand_1 = scanner.match.group(2)
operand_2 = scanner.match.group(3)
virtual[result_varname] = simpleMath('+', operand_1, operand_2)
return "EQADD", result_varname, operand_1, operand_2
def subtraction(scanner,token):
result_varname = scanner.match.group(1)
operand_1 = scanner.match.group(2)
operand_2 = scanner.match.group(3)
virtual[result_varname] = simpleMath('-', operand_1, operand_2)
return "EQSUB", result_varname, operand_1, operand_2
def multiplication(scanner,token):
result_varname = scanner.match.group(1)
operand_1 = scanner.match.group(2)
operand_2 = scanner.match.group(3)
virtual[result_varname] = simpleMath('*', operand_1, operand_2)
return "EQMUL", result_varname, operand_1, operand_2
def division(scanner,token):
result_varname = scanner.match.group(1)
operand_1 = scanner.match.group(2)
operand_2 = scanner.match.group(3)
virtual[result_varname] = simpleMath('/', operand_1, operand_2)
return "EQDIV", result_varname, operand_1, operand_2
def increment(scanner, token):
return "INCREMENT", scanner.match.group(1)
def digit(scanner, token):
return "DIGIT", token
def end_stmnt(scanner, token):
return "END"
def start_stmnt(scanner, token):
return "BEGIN"
def printer(scanner,token):
variable_name = scanner.match.group(1)
return "PRINT", variable_name
scanner = re.Scanner([
(r"so\s+((\w+))\s+much\s(.+)", declaration),
(r"very\s+(\w+)", increment),
(r"nice\s+(\w+)\s+very\s+(.+)\s+very\s+(.+)", addition),
(r"bad\s+(\w+)\s+very\s+(.+)\s+very\s+(.+)", subtraction),
(r"much\s+(\w+)\s+very\s+(.+)\s+very\s+(.+)", multiplication),
(r"few\s+(\w+)\s+very\s+(.+)\s+very\s+(.+)", division),
(r"amaze\s+(.+)", printer),
(r"wow", end_stmnt),
(r"many codes", start_stmnt),
(r"\s+", None),
])
sample_program = """
many codes
so x much 50
so y much 6
so a much 3.14
so b much -42
very x
nice z very x very y
nice p very 60 very 9
bad f very x very y
bad g very 60 very 9
much f very x very y
much g very 60 very 9
much t very a very b
few f very x very y
few g very 60 very 9
amaze z
amaze y
amaze x
wow
"""
tokens, remainder = scanner.scan(sample_program)
for token in tokens:
print token
print virtual