Last active
March 21, 2018 18:17
-
-
Save Ronald-TR/e1332e8c72e039332d3d753af925519b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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