Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mapping = {
- 0: 'POINTER_LEFT',
- 1: 'POINTER_RIGHT',
- 2: 'VALUE_INCREASE',
- 3: 'VALUE_DECREASE',
- 4: 'OPEN_BRACKET',
- 5: 'CLOSE_BRACKET',
- 6: 'OUTPUT_CHARACTER',
- 7: 'INPUT_CHARACTER',
- 8: 'OUTPUT_INTEGER'
- }
- def lex(content):
- glyphs = [char for char in content]
- lines = [len([char for char in line]) for line in ''.join(glyphs).split('\n')]
- return {i: mapping[line] for i, line in enumerate(lines)}
- brain_mapping = {
- 'POINTER_LEFT': '<',
- 'POINTER_RIGHT': '>',
- 'VALUE_INCREASE': '+',
- 'VALUE_DECREASE': '-',
- 'OPEN_BRACKET': '[',
- 'CLOSE_BRACKET': ']',
- 'OUTPUT_INTEGER': '!',
- 'INPUT_CHARACTER': ',',
- 'OUTPUT_CHARACTER': '.'
- }
- def match(target):
- for key in brain_mapping:
- if brain_mapping[key] == target:
- return key
- def match_spaces(target):
- for key in mapping:
- if mapping[key] == target:
- return key
- def to_brain(lines):
- final = ''
- for line in lines:
- final += brain_mapping[lines[line]]
- return final
- def from_brain(code):
- final = []
- for char in code:
- final.append(match_spaces(match(char)))
- final = [item for item in final if item != None]
- finalstr = ''
- for kc in final:
- finalstr += ' '*kc
- finalstr += '\n'
- return finalstr
- def sprint(astro):
- lines = astro.split('\n')
- for line in lines:
- print(f'|{line}|')
- def read(file_name):
- with open(file_name, 'r') as file:
- return file.read()
- def lex_all(file_name):
- cont = read(file_name)
- lexed = lex(cont)
- return to_brain(lexed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement