-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
executable file
·130 lines (100 loc) · 3.17 KB
/
main.rb
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env ruby
###################################################
# This is the cockpit of the 03-K64 Compiler.
# Crafted for Alan's 'Design Compilers' class,
# lovingly coded in Ruby magic, and narrated by
# her captain.
#
#
# Author: Kevin Pietrow
# Version: The shinier one
#
# Note: Instructions for running the best compiler
# in the Verse can be found in the README
#
# Get all of our requireds
require './Lexer/lexer.rb'
require './Lexer/tokenizer.rb'
require './Parser/parser.rb'
require './SymbolTable/symbol_table.rb'
require './SemanticAnalysis/semantic_analysis.rb'
require './SyntaxTrees/cst.rb'
require './SyntaxTrees/ast.rb'
require './SyntaxTrees/node.rb'
require './SyntaxTrees/cst_converter.rb'
require './CodeGen/code_gen.rb'
require './CodeGen/code_gen_helpers.rb'
# Error for a blank file of input
class BlankFileError < StandardError
def initialize
puts "ERROR: There don't seem to be any information in that here file. We're just gonna exit the program for ye."
exit
end
end
##
# Settin' up global regex variables now
#
$digit = /[0-9]/
$character = /[a-z]/
$space = /\s/
$eof = /\$/
# note, this here includes whitespace, so be careful about where it's used
$operator = /\W/
##
# This is the list of tokens that we will be using in this here system
#
# token_list = ["T_ASSIGNMENT", "T_LBRACE", "T_RBRACE", "T_LPAREN",
# "T_RPAREN", "T_QUOTE", "T_BOOLOP", "T_PLUS",
# "T_EOFSIGN", "T_IF", "T_WHILE", "T_BOOLEAN", "T_STRING",
# "T_ID", "T_DIGIT", "T_PRINT", "T_TYPE"]
#
###################################################
# The main function of the entire operation
#
#
def main
# He's going to be runnin' this here operation
# Retrieving that shiny input file
input_file = File.new(ARGV[0])
# Sortin' out them good lines from the bad
input_file = IO.readlines(input_file)
if input_file.length == 0
raise BlankFileError
end
puts "==================================================================="
puts "This is the captain speaking. Welcome to the 03-K64 Compiler, and now if you'll excuse me for a minute it's time to burn atmo and get this ship in the air."
# Lexer it!
puts "\nBeginnin' the Lexing process now...\n"
token_stream = lexer(input_file)
puts "\nLexing completed successfully, all tokens have been smuggled into the system\n\nToken Stream (in order):\n"
# print out the received tokens
for i in token_stream
print i.type
if i.type != "T_EOFSIGN"
print ", "
else
puts "\n\n"
end
end
# Parse it!
puts "\nNow we're gonna begin the parsin'..."
parser(token_stream)
puts "\n\nParsing successful. We've got ourselves a nice parse stream and symbol table now.\n\n"
$cst.printout
puts "\n\n\n"
puts "Now we're doin' some calculations and conversions, trying to change that CST to a nice AST...\n\n"
convert_cst
puts "Printing out that AST now"
$ast.printout
puts "\n\n"
puts "We're gonna begin the semantic analysis now.\n\n"
semantic_analysis
puts "\n\n"
$symbol_table.printout
puts "\n\n"
$symbol_table.analysis($symbol_table.root)
puts "\n\n"
puts "And now we're on to Code Generation. Here we might experience some turbulence, and possibly explode. So strap in!\n\n"
code_gen
end
main