added some command line args
This commit is contained in:
29
main.py
29
main.py
@@ -5,15 +5,25 @@ from time import time
|
|||||||
from generators import x86_64
|
from generators import x86_64
|
||||||
from os import system, remove
|
from os import system, remove
|
||||||
from error import traceback
|
from error import traceback
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
in_path = "test2.grnd"
|
arg_parser = ArgumentParser(prog="GroundPY", description="A compiler for the Ground Programming Language.")
|
||||||
out_path = "out"
|
arg_parser.add_argument("input", help="ground source file path")
|
||||||
arch = "x86_64"
|
arg_parser.add_argument("--output", "-o", default="out", help="output file name")
|
||||||
|
arg_parser.add_argument("--arch", "-a", default="x86_64", choices=["x86_64"], help="the architecture to compile for")
|
||||||
|
arg_parser.add_argument("-S", "--save-asm", help="only generate an asm file, don't actually assemble it", action="store_true")
|
||||||
|
args = arg_parser.parse_args()
|
||||||
|
in_path = args.input
|
||||||
|
out_path = args.output
|
||||||
|
arch = args.arch
|
||||||
|
|
||||||
start = time()
|
start = time()
|
||||||
file = open(in_path, "r")
|
try:
|
||||||
|
file = open(in_path, "r")
|
||||||
|
except FileNotFoundError:
|
||||||
|
traceback("", "fatal", f"no such file or directory \"{in_path}\"")
|
||||||
code = file.read()
|
code = file.read()
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
@@ -23,15 +33,14 @@ def main():
|
|||||||
|
|
||||||
if arch == "x86_64":
|
if arch == "x86_64":
|
||||||
generator = x86_64.X86_64Generator(ast, code, out_path)
|
generator = x86_64.X86_64Generator(ast, code, out_path)
|
||||||
else:
|
|
||||||
traceback(code, "fatal error", f"unkown architecture \"{arch}\"")
|
|
||||||
|
|
||||||
generator.init()
|
generator.init()
|
||||||
|
|
||||||
system(f"nasm -felf64 {out_path}.asm")
|
if not args.save_asm:
|
||||||
system(f"ld -m elf_{arch} -o {out_path} {out_path}.o")
|
system(f"nasm -felf64 {out_path}.asm")
|
||||||
remove(out_path + ".o")
|
system(f"ld -m elf_{arch} -o {out_path} {out_path}.o")
|
||||||
#remove(out_path + ".asm")
|
remove(out_path + ".o")
|
||||||
|
remove(out_path + ".asm")
|
||||||
|
|
||||||
compile_time = time()-start
|
compile_time = time()-start
|
||||||
print(f"Compiled in {round(compile_time, 3)} seconds.")
|
print(f"Compiled in {round(compile_time, 3)} seconds.")
|
||||||
|
47
out.asm
47
out.asm
@@ -1,47 +0,0 @@
|
|||||||
; ~~~ Auto generated by the GroundPY compiler for Linux x86_64 targets. ~~~
|
|
||||||
|
|
||||||
section .data
|
|
||||||
LC0: db "yep!", 10, 0
|
|
||||||
LC1: equ $ - LC0
|
|
||||||
LC2: db "nope...", 10, 0
|
|
||||||
LC3: equ $ - LC2
|
|
||||||
section .text
|
|
||||||
global _start
|
|
||||||
_start:
|
|
||||||
mov rdi, 1
|
|
||||||
mov rsi, 3
|
|
||||||
call greater
|
|
||||||
test eax, eax
|
|
||||||
jnz .yes
|
|
||||||
jmp .no
|
|
||||||
.yes:
|
|
||||||
mov rsi, LC0
|
|
||||||
mov rdx, LC1
|
|
||||||
push rdi
|
|
||||||
mov rax, 1
|
|
||||||
mov rdi, 1
|
|
||||||
syscall
|
|
||||||
pop rdi
|
|
||||||
mov rdi, 0
|
|
||||||
mov rax, 60
|
|
||||||
syscall
|
|
||||||
.no:
|
|
||||||
mov rsi, LC2
|
|
||||||
mov rdx, LC3
|
|
||||||
push rdi
|
|
||||||
mov rax, 1
|
|
||||||
mov rdi, 1
|
|
||||||
syscall
|
|
||||||
pop rdi
|
|
||||||
mov rdi, 0
|
|
||||||
mov rax, 60
|
|
||||||
syscall
|
|
||||||
greater:
|
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
mov rax, rdi
|
|
||||||
cmp rax, rsi
|
|
||||||
setg al
|
|
||||||
movzx eax, al
|
|
||||||
pop rbp
|
|
||||||
ret
|
|
17
test2.grnd
17
test2.grnd
@@ -1,17 +1,2 @@
|
|||||||
fun -bool !greater -int &x -int &y
|
stdout "Hello, World!\n"
|
||||||
greater $x $y &x
|
|
||||||
return $x
|
|
||||||
endfun
|
|
||||||
|
|
||||||
pusharg 1
|
|
||||||
pusharg 3
|
|
||||||
call !greater &bigger
|
|
||||||
|
|
||||||
if $bigger %yes
|
|
||||||
jump %no
|
|
||||||
@yes
|
|
||||||
stdout "yep!\n"
|
|
||||||
end 0
|
|
||||||
@no
|
|
||||||
stdout "nope...\n"
|
|
||||||
end 0
|
end 0
|
Reference in New Issue
Block a user