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 os import system, remove | ||||
| from error import traceback | ||||
| from argparse import ArgumentParser | ||||
|  | ||||
|  | ||||
| def main(): | ||||
| 	in_path = "test2.grnd" | ||||
| 	out_path = "out" | ||||
| 	arch = "x86_64" | ||||
| 	arg_parser = ArgumentParser(prog="GroundPY", description="A compiler for the Ground Programming Language.") | ||||
| 	arg_parser.add_argument("input", help="ground source file path") | ||||
| 	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() | ||||
| 	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() | ||||
| 	file.close() | ||||
|  | ||||
| @@ -23,15 +33,14 @@ def main(): | ||||
|  | ||||
| 	if arch == "x86_64": | ||||
| 		generator = x86_64.X86_64Generator(ast, code, out_path) | ||||
| 	else: | ||||
| 		traceback(code, "fatal error", f"unkown architecture \"{arch}\"") | ||||
|  | ||||
| 	generator.init() | ||||
|  | ||||
| 	system(f"nasm -felf64 {out_path}.asm") | ||||
| 	system(f"ld -m elf_{arch} -o {out_path} {out_path}.o") | ||||
| 	remove(out_path + ".o") | ||||
| 	#remove(out_path + ".asm") | ||||
| 	if not args.save_asm: | ||||
| 		system(f"nasm -felf64 {out_path}.asm") | ||||
| 		system(f"ld -m elf_{arch} -o {out_path} {out_path}.o") | ||||
| 		remove(out_path + ".o") | ||||
| 		remove(out_path + ".asm") | ||||
|  | ||||
| 	compile_time = time()-start | ||||
| 	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 | ||||
| 	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" | ||||
| stdout "Hello, World!\n" | ||||
| end 0 | ||||
		Reference in New Issue
	
	Block a user
	 SpookyDervish
					SpookyDervish