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 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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]} {token[i+3][1]} {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. #print (texto)
  38. def simpleexpre(exp,var,texto):
  39. '''
  40. ADD dest,src1,src2 dest = src1 + src2
  41. SUB dest,src1,src2 dest = src1 - src2
  42. ADDI dest,src1,I dest = src1 + I
  43. MUL dest,src1,src2 dest = src1 × src2
  44. DIV[U] dest,src1,src2 dest = src1/src2
  45. '''
  46. t=1
  47. exp=exp[0].split()
  48. values=[]
  49. operador=['+','-','*','/']
  50. for i in range(len(exp)):
  51. if exp[i] in operador:
  52. a = values.pop()
  53. b = values.pop()
  54. if i == len(exp)-1:
  55. texto.append(f'{var} := {b} {a} {exp[i]}')
  56. else:
  57. texto.append(f'T{t} := {b} {a} {exp[i]}')
  58. values.append(f'T{t}')
  59. t+=1
  60. else:
  61. values.append(exp[i])
  62. if len(values) > 0:
  63. texto.append(f'{var} := {values[0]}')
  64. def expressao(token, i):
  65. l=i+1
  66. var=[]
  67. while not('fim_linha' in token[l][0]):
  68. var.append(token[l][1])
  69. l+=1
  70. return ' '.join(var)
  71. def variaveis(token,i):
  72. l=i+1
  73. var=[]
  74. while not('fim_linha' in token[l][0]):
  75. if not('virgula' in token[l][0]):
  76. var.append('INTEIRO '+token[l][1])
  77. l+=1
  78. return var