forked from evansneath/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.py
executable file
·92 lines (67 loc) · 2.49 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
#!/usr/bin/env python3
"""Compiler module
Acts as the command line interface to the compiler components. When given a
source file, the compilation process will be executed.
Author: Evan Sneath
License: Open Software License v3.0
Functions:
parse_arguments: Parses incoming command line arguments.
run_compiler: Executes the complete compilation process.
"""
# Import standard libraries
import argparse
import subprocess
import sys
# Import custom compiler libraries
from lib.parser import Parser
def parse_arguments():
"""Parse Arguments
Parses all command line arguments for the compiler program.
Returns:
An object containing all expected command line arguments.
"""
# Parse the command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--debug',
help='print comments in generated code',
action='store_true')
parser.add_argument('source',
help='source file to compile')
parser.add_argument('-o', '--out',
help='target path for the compiled code',
action='store',
default='a.out')
args = parser.parse_args()
return args
def run_compiler(source, target, debug=False):
"""Run Compiler
Executes the compilation process given a source file path.
Arguments:
source: The source file to compile.
target: The destination binary executable file.
debug: If True, verbose parsing details are shown. (Default: False)
Returns:
True on success, False otherwise.
"""
# Define a temporary location for the intermediate C code
TMP_CODE_FILE = './ir.c'
# Create a Parser object to parse the inputted source file
parser = Parser(debug)
# Parse the source file to the temporary code file
if not parser.parse(source, TMP_CODE_FILE):
print('Error while parsing "%s"' % source)
return False
# Set up gcc compilation command
gcc_cmd = ['gcc', '-m32', '-o', target, TMP_CODE_FILE]
# Compile the temporary file with gcc. Output to the target location
if subprocess.call(gcc_cmd) != 0:
print('Error while compiling "%s"' % target)
return False
return True
if __name__ == '__main__':
# Parse compiler arguments
args = parse_arguments()
# Run compilation process
result = run_compiler(args.source, args.out, debug=args.debug)
# Terminate program
sys.exit(not result)