-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.py
113 lines (86 loc) · 2.66 KB
/
compiler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import sys
import os
path = sys.argv[1]
if not path.endswith(".limpil"):
print("Você não selecionou um arquivo .limpil!")
sys.exit(0)
file_lines = list()
with open(path, "r") as file:
file_lines = [line.strip() for line in file.readlines()]
# ===== PARSING =====
print("[CMD] Transformando o código...")
program = list()
for line in file_lines:
splitted_command = line.split(" ")
opcode = splitted_command[0]
if opcode == "": # Linha em branco
continue
if opcode.startswith("--"): # Comentário
continue
program.append(opcode)
# Comandos
if opcode == "ADICIONE.INTEIRO":
number = int(splitted_command[1])
program.append(number)
if opcode == "ADICIONE.DECIMAL":
number = float(splitted_command[1])
program.append(number)
if opcode == "IMPRIMA":
string = " ".join((splitted_command[1:]))[1:-1]
program.append(string)
# ===== COMPILING =====
print("[CMD] Transformando em Assembly...")
# Removendo os 07 últimos caracteres (.limpil)
assembly_path = path[:-7] + ".asm"
asm_program = open(assembly_path, "w")
asm_program.write("""; -- Headers --
bits 64 ; Modo de 64 bits
default rel ; Usando endereços relativos
""")
asm_program.write("""; -- Variáveis --
section .bss
""")
asm_program.write("""; -- Constantes --
section .data
""")
asm_program.write("""; -- Lógica --
section .text
global main
extern ExitProcess
extern printf
extern scanf
main:
\t; Criando espaço para variáveis locais.
\tPUSH rbp
\tMOV rbp, rsp
\tSUB rsp, 32
""")
pointer = 0
while pointer < len(program):
opcode = program[pointer]
pointer += 1
if opcode == "ADICIONE.INTEIRO":
number = int(program[pointer])
pointer += 1
asm_program.write(f"\tPUSH {number}\n")
if opcode == "ADICIONE.DECIMAL":
number = float(program[pointer])
pointer += 1
asm_program.write(f"\tPUSH {number}\n")
if opcode == "IMPRIMA":
string_index = program[pointer]
pointer += 1
asm_program.write("\t;PRINT não implementado.\n")
if opcode == "LER.DECIMAL":
string_index = program[pointer]
pointer += 1
asm_program.write("\t;LEITURA não implementado.\n")
if opcode == "PARE":
asm_program.write("\tJMP exit_label\n")
asm_program.write("exit_label:\n")
asm_program.write("\txor rax, rax ; Zerando RAX\n")
asm_program.write("\tCALL ExitProcess\n")
asm_program.close()
# ===== ASSEMBLING =====
print("[CMD] Compilando Assembly...")
os.system(f"nasm -f elf64 {assembly_path}")