User:Alice/Special Issue V: Difference between revisions
(Created page with "== Reader #1 == === Title and topic === === Research ===") |
No edit summary |
||
Line 2: | Line 2: | ||
=== Title and topic === | === Title and topic === | ||
=== Research === | === Research === | ||
== Code == | |||
<source lang=python> | |||
import linecache | |||
import textwrap | |||
import sys | |||
from sys import exit | |||
class LeavingProgram(Exception): | |||
pass | |||
def parse(program): | |||
cmds = program.split(',') | |||
splitted_cmds = [] | |||
for cmd in cmds: | |||
splitted = cmd.split() | |||
splitted_cmds.append(splitted) | |||
return splitted_cmds | |||
#return tokenize(program) | |||
def tokenize(s): | |||
return s.split() | |||
def repl(): | |||
while True: | |||
try: | |||
val = eval(parse(input('> '))) | |||
if val is not None: | |||
print(val) | |||
except LeavingProgram: | |||
break | |||
text = None | |||
line_number = 0 | |||
last_index = 0 | |||
def eval(cmds): | |||
global text | |||
global line_number | |||
global last_index | |||
for cmd in cmds: | |||
if cmd == []: | |||
line_number += 1 | |||
last_index = 0 | |||
elif cmd[0] == 'load': | |||
contents = open('output.txt').read() | |||
text = textwrap.wrap(contents, 40, break_long_words=True) | |||
print('\n'.join(text)) | |||
line_number = 0 | |||
last_index = 0 | |||
elif cmd[0] == 'show': | |||
print(text[line_number]) | |||
elif cmd[0] == 'under': | |||
current_line = text[line_number] | |||
char_number = int(cmd[1]) - 1 | |||
char_list = list(current_line) | |||
x=range(last_index, char_number + last_index + 1) | |||
for time in x: | |||
if time < len(char_list): | |||
char_list[time] = u'\u21e2' | |||
last_index += char_number + 1 | |||
joined = ''.join(char_list) | |||
text[line_number] = joined | |||
elif cmd[0] == 'over': | |||
last_index += int(cmd[1]) | |||
elif cmd[0] == 'pattern': | |||
pattern = text[0:line_number + 1] | |||
print('\n'.join(pattern)) | |||
elif cmd[0] == 'quit': | |||
print('Adios!') | |||
raise LeavingProgram() | |||
else: | |||
joined = ' '.join(cmd) | |||
print('Did not understand command {}'.format(joined)) | |||
if __name__ == '__main__': | |||
repl() | |||
<source> |
Revision as of 17:09, 19 March 2018
Reader #1
Title and topic
Research
Code
<source lang=python>
import linecache import textwrap import sys from sys import exit
class LeavingProgram(Exception):
pass
def parse(program):
cmds = program.split(',') splitted_cmds = [] for cmd in cmds: splitted = cmd.split() splitted_cmds.append(splitted) return splitted_cmds
#return tokenize(program)
def tokenize(s):
return s.split()
def repl():
while True: try: val = eval(parse(input('> '))) if val is not None: print(val) except LeavingProgram: break
text = None line_number = 0 last_index = 0
def eval(cmds):
global text global line_number global last_index
for cmd in cmds: if cmd == []: line_number += 1 last_index = 0
elif cmd[0] == 'load': contents = open('output.txt').read() text = textwrap.wrap(contents, 40, break_long_words=True) print('\n'.join(text)) line_number = 0 last_index = 0
elif cmd[0] == 'show': print(text[line_number])
elif cmd[0] == 'under': current_line = text[line_number] char_number = int(cmd[1]) - 1 char_list = list(current_line)
x=range(last_index, char_number + last_index + 1) for time in x: if time < len(char_list): char_list[time] = u'\u21e2'
last_index += char_number + 1
joined = .join(char_list) text[line_number] = joined
elif cmd[0] == 'over': last_index += int(cmd[1])
elif cmd[0] == 'pattern':
pattern = text[0:line_number + 1] print('\n'.join(pattern))
elif cmd[0] == 'quit': print('Adios!') raise LeavingProgram() else: joined = ' '.join(cmd) print('Did not understand command {}'.format(joined))
if __name__ == '__main__':
repl()
<source>