-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorth.torth
71 lines (58 loc) · 2 KB
/
torth.torth
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
// torth.torth - Self hosted implementation of the Torth compiler
include "std"
include "argparser"
include "compiler/asm"
include "compiler/compile"
include "compiler/lex"
include "compiler/program"
include "compiler/typecheck"
include "compiler/utils"
function main :
// Parse command line arguments
parse_command_line_arguments
take args in
// Get the code file name from command line
"code_file" args Argparser.get_argument
take code_file in
// Get a list of included files for the program
args "Getting included files" verbose_output
code_file get_included_files
take included_files in
// Parse diffent code components
args f"Parsing code from {code_file}" verbose_output
included_files get_tokens_from_files
take tokens in
// Parse Constants
tokens get_constants
take constants in
// Parse Functions
constants tokens get_functions
take functions in
// Parse classes
constants functions tokens parse_classes
// Get the intermediate representation (Program) of each Function
constants functions get_sub_programs
take sub_programs in
// Type check the Program
args "Type checking the program" verbose_output
functions sub_programs type_check_application
// Generate assembly code from the intermediate representation
args "Generating Assembly code" verbose_output
args code_file get_output_file_name
take out_file in
sub_programs f"{out_file}.asm" generate_assembly_file
// Compile the assembly code file to a Linux x86_64 executable
args "Compiling Assembly code" verbose_output
out_file compile_executable
// Remove files generated during compilation
// if -s command line flag is not present
if "-s" args Argparser.flag_present not do
args "Removing files generated during compilation" verbose_output
out_file remove_compilation_files
endif
// Run the program if -r command line flag is present
if "-r" args Argparser.flag_present do
args "Running the program" verbose_output
out_file execute_program& exec_forked
endif
end