-
Notifications
You must be signed in to change notification settings - Fork 2
/
lazy.py
132 lines (115 loc) · 4.63 KB
/
lazy.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 sys
import re
import random
import time
class ProcrastiCodeInterpreter:
def __init__(self):
self.variables = {}
self.functions = {}
self.output = []
def run(self, code):
lines = code.split('\n')
i = 0
while i < len(lines):
line = lines[i].strip()
if line.startswith('maybe'):
self.declare_variable(line)
elif line.startswith('complain'):
self.complain(line)
elif line.startswith('eventually'):
i = self.execute_loop(lines, i)
elif line.startswith('whenever'):
i = self.execute_conditional(lines, i)
elif line.startswith('someday'):
i = self.define_function(lines, i)
elif line.startswith('procrastinate'):
self.procrastinate()
i += 1
def declare_variable(self, line):
parts = line.split()
if len(parts) >= 4 and parts[2] == '=':
var_name = parts[1]
value = ' '.join(parts[3:])
self.variables[var_name] = eval(value, {}, self.variables)
def complain(self, line):
message = re.match(r'complain\s*\((.*)\)', line).group(1)
output_message = f"Ugh, fine. {eval(message, {}, self.variables)}"
print(output_message)
self.output.append(output_message)
def execute_loop(self, lines, start):
condition = re.match(r'eventually\s*\((.*)\)', lines[start]).group(1)
body_start = start + 1
body_end = body_start
while body_end < len(lines) and not lines[body_end].strip().startswith('done'):
body_end += 1
while eval(condition, {}, self.variables):
self.run('\n'.join(lines[body_start:body_end]))
return body_end
def execute_conditional(self, lines, start):
condition = re.match(r'whenever\s*\((.*)\)', lines[start]).group(1)
body_start = start + 1
body_end = body_start
while body_end < len(lines) and not lines[body_end].strip().startswith('done'):
body_end += 1
if eval(condition, {}, self.variables):
self.run('\n'.join(lines[body_start:body_end]))
return body_end
def define_function(self, lines, start):
func_def = re.match(r'someday\s*(\w+)\s*\((.*)\)', lines[start])
func_name = func_def.group(1)
params = [p.strip() for p in func_def.group(2).split(',')]
body_start = start + 1
body_end = body_start
while body_end < len(lines) and not lines[body_end].strip().startswith('done'):
body_end += 1
self.functions[func_name] = (params, '\n'.join(lines[body_start:body_end]))
return body_end
def procrastinate(self):
excuses = [
"I'll do it in 5 minutes...",
"But first, let me check my emails.",
"I need a coffee break to think about this.",
"I should reorganize my desk before starting.",
"Let me just quickly check social media first."
]
procrastination_message = f"Procrastinating: {random.choice(excuses)}"
print(procrastination_message)
self.output.append(procrastination_message)
time.sleep(2)
def save_output(self, filename):
with open(f"{filename}.lazy", "w") as file:
file.write('\n'.join(self.output))
print(f"Output saved to {filename}.lazy")
def main():
if len(sys.argv) != 3 or sys.argv[1] != "run":
print("Usage: lazy run <filename>.lazy")
sys.exit(1)
filename = sys.argv[2]
if not filename.endswith('.lazy'):
print("Error: The file must have a .lazy extension.")
sys.exit(1)
try:
with open(filename, 'r') as file:
code = file.read()
except FileNotFoundError:
print(f"Error: File {filename} not found.")
sys.exit(1)
interpreter = ProcrastiCodeInterpreter()
interpreter.run(code)
interpreter.save_output(filename.split('.')[0]) # Save the output
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: lazy run <filename>.lazy")
sys.exit(1)
filename = sys.argv[1]
if not filename.lower().endswith('.lazy'):
print("Please provide a .lazy file.")
sys.exit(1)
try:
with open(filename, 'r') as file:
code = file.read()
except FileNotFoundError:
print(f"File '{filename}' not found.")
sys.exit(1)
interpreter = ProcrastiCodeInterpreter()
interpreter.run(code)