Bora vê
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Geracaodecodigo.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. '''
  2. intermediario(token, argumentos, posicao inicial que deseja converter para assembler, posição final, valor inicial das Labels dos loops, texto final)
  3. '''
  4. def intemerdiario(token,args, inicio, fim, loop,condicional, texto):
  5. L=loop
  6. C=condicional
  7. i=inicio
  8. while i < fim :
  9. if 'inteiro' in token[i][0]:
  10. texto.extend(variaveis(token,i))
  11. elif 'leia' in token[i][0]:
  12. texto.append('LEIA '+token[i+2][1])
  13. elif 'escreva' in token[i][0]:
  14. texto.append('ESCREVA '+ token[i+2][1])
  15. elif 'recebe' in token[i][0]:
  16. exp=[]
  17. from infixtoposfix import infixToPostfix
  18. exp.append(infixToPostfix(expressao(token, i)))
  19. simpleexpre(exp,token[i-1][1],texto)
  20. elif 'enquanto' in token[i][0]:
  21. texto.append(f'_L{L}: if {token[i+2][1]}'+ (' >' if '<' in token[i+3][1] else ' <')+f' {token[i+4][1]} goto _L{L+1} ')
  22. i=intemerdiario(token, args, i+6, fim, L+1, C, texto)
  23. texto.append(f'_L{L+1}:')
  24. L=L+i
  25. C=L
  26. elif 'se' == token[i][0]:
  27. texto.append(f'_C{C}: if {token[i+2][1]}' + (' >' if '<' in token[i+3][1] else ' <')+f' {token[i+4][1]} goto _C{C+1}')
  28. i=intemerdiario(token, args, i+6, fim, L, C+1, texto)
  29. texto.append(f'_C{L+1}:')
  30. C = C + i
  31. L = C
  32. elif '}' in token[i][1]:
  33. return i
  34. i=i+1
  35. with open('intermediario.s','w') as f:
  36. f.write('\n'.join(texto))
  37. def simpleexpre(exp,var,texto):
  38. '''
  39. ADD dest,src1,src2 dest = src1 + src2
  40. SUB dest,src1,src2 dest = src1 - src2
  41. ADDI dest,src1,I dest = src1 + I
  42. MUL dest,src1,src2 dest = src1 × src2
  43. DIV[U] dest,src1,src2 dest = src1/src2
  44. '''
  45. t=1
  46. exp=exp[0].split()
  47. values=[]
  48. operador=['+','-','*','/']
  49. for i in range(len(exp)):
  50. if exp[i] in operador:
  51. a = values.pop()
  52. b = values.pop()
  53. if i == len(exp)-1:
  54. texto.append(f'{var} := {b} {a} {exp[i]}')
  55. else:
  56. texto.append(f'T{t} := {b} {a} {exp[i]}')
  57. values.append(f'T{t}')
  58. t+=1
  59. else:
  60. values.append(exp[i])
  61. if len(values) > 0:
  62. texto.append(f'{var} := {values[0]}')
  63. def expressao(token, i):
  64. l=i+1
  65. var=[]
  66. while not('fim_linha' in token[l][0]):
  67. var.append(token[l][1])
  68. l+=1
  69. return ' '.join(var)
  70. def variaveis(token,i):
  71. l=i+1
  72. var=[]
  73. while not('fim_linha' in token[l][0]):
  74. if not('virgula' in token[l][0]):
  75. var.append('INTEIRO '+token[l][1])
  76. l+=1
  77. return var
  78. def gerarcodigo(token,args):
  79. texto=[]
  80. '''
  81. intermediario(token, args, posicao inicial que deseja converter para assembler, posição final, valor inicial das Labels dos loops, valor inicial das Labels dos condicionais)
  82. '''
  83. intemerdiario(token, args, 0, len(token), 0, 0, texto)
  84. gerar()
  85. def gerar():
  86. texto=[]
  87. with open('intermediario.s','r') as f:
  88. texto=f.readlines()
  89. i=0
  90. final=[]
  91. db=[]
  92. msg=-1
  93. final.append('format ELF64 executable 3')
  94. final.append('segment readable executable')
  95. final.append('entry main')
  96. final.append('main:')
  97. while i < len(texto):
  98. if 'ESCREVA' in texto[i]:
  99. if '"' in texto[i]:
  100. msg += 1
  101. size = texto[i].strip('ESCREVA')
  102. size = size.strip('\n').replace('"','')
  103. final.append(f'lea rdi, [msg{msg}]')
  104. final.append(f'mov rax, {len(size)}')
  105. final.append(f'mov rdx, rax')
  106. final.append(f'mov rsi, rdi')
  107. final.append(f'mov rdi, 1')
  108. final.append(f'mov rax, 1')
  109. final.append(f'syscall')
  110. db.extend([f'msg{msg} db \'{size.strip()}\', 10,0'])
  111. else:
  112. print ()
  113. i+=1
  114. final.append('xor rdi,rdi')
  115. final.append('mov rax, 60')
  116. final.append('syscall')
  117. if(msg>-1):
  118. final.append('segment readable writable')
  119. final.extend(db)
  120. with open('final.fasm','w') as f:
  121. f.write('\n'.join(final))