Skip to content

Instantly share code, notes, and snippets.

@Ronald-TR
Last active March 21, 2018 18:17
Show Gist options
  • Save Ronald-TR/e1332e8c72e039332d3d753af925519b to your computer and use it in GitHub Desktop.
Save Ronald-TR/e1332e8c72e039332d3d753af925519b to your computer and use it in GitHub Desktop.
import re
code = 'se 1234 > 4321: mostre "verdadeiro"'
def mostre(code):
code = code.replace(' ', '')
r_print = re.compile('^mostre"[a-z A-Z]*"$')
rprint = r_print.match(code)
if rprint == None:
return False
p = re.compile('"([^"]*)"').findall(rprint.group())[0]
print(p)
return True
def se(code):
r_if = re.compile('^se [a-z 0-9]* [><=!] [a-z 0-9]*\:')
rif = r_if.match(code)
if rif == None:
return None, None
b_expression = code[2:].split(':')[0].replace(' ', '')
return eval(b_expression), ''.join(code.split(':')[1:]).replace(' ', '')
print('transpiler example with regex, write \'exit\' to quit\n')
while True:
scode = input('transpiler>')
if scode == 'exit': exit(1)
res, exp = se(scode)
if res != None:
if res == True:
if not mostre(exp):
result = eval(exp)
if result != None: print(result)
elif not mostre(scode):
try:
result = eval(scode)
if result != None: print(result)
except Exception as e:
exec(scode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment