Commits

Steven! Ragnarök  committed dbec8b8

Some class refactoring.

  • Participants
  • Parent commits 01ac748

Comments (0)

Files changed (2)

 		end
 	end
 end
+
 ## Terminal Matcher ##
 def match(terminal, token)
 	if terminal == token[:type]

File top_downer.rb

 		@r_cache = Hash.new
 	end
 
-	def parse_string string
+	def parse string
 		@enumerator = tokenize(string).each
 		@tree = Hash.new
-		parse [@grammar[:Program]]
+		recognize [:Program]
 	end
 
 	private
 		raise ArgumentError.new "Unknown token type for #{token}"
 	end
 
+	## String Tokenizer ##
+	# Breaks up a string into an array of symbols.
 	def tokenize string
-		tokens = str.gsub(/[\n\t ]+/,' ').split(' ').map {|t| t.to_sym}
+		tokens = string.gsub(/[\n\t ]+/,' ').split(' ').map {|t| t.to_sym}
 		tokens.map do |token|
 			detect(token)
 		end
 	end
-	#
+
 	# Returns all valid token types of the first terminal in a construct.
 	def first construct
-		return [Grammar.has_key? construct && first(Grammar[construct]) ||
-			construct].flatten
+		#		return [Grammar.has_key? construct && first(Grammar[construct]) ||
+			# construct].flatten
 	end
 
 	## Recognizer ##
-	# Returns a symbol describing what is constructed by the token(bitch) 
+	# Returns a symbol describing what is constructed by the token 
 	# and lookahead. For a token :return and a lookahead :"4", it returns
 	# :ReturnStatement. For a token :lparen and a lookahead :ident it would
 	# return :Arglist.
-	def recognize bitch, lookahead
-		@cache[:"#{bitch},#{lookahead}"] ||=
-			@grammar.each do |key, val|
-			# Let's assume our key is :ReturnStatement and our val is [:return, :int_lit]			
-				if bitch == first(val[0])
-					return key
-				end
-			end
-	end
 
-	def parse recognizer
-		case recognizer
+	def recognize construct
+		case construct
 		# Array means nonliteral.
 		when Array
 			# check for meta-grammar
-			return recognizer.map {|r| parse @grammar[r] || r}
+			return construct.map {|r| parse @grammar[r] || r}
 		# Symbol means literal
 		when Symbol
 			tkn = @enumerator.next
 			puts tkn
-			if tkn[:type] == recognizer
+			if tkn[:type] == construct
 				# Base step of recursion
 				return tkn[:token]
 			else