Commits

Steven! Ragnarök committed 01ac748

I was tackling it wrong. May have to hard code.

Comments (0)

Files changed (1)

 ## lparen and rparen.
 
 # Define all my token types using a standard Ruby hash.
-TokenTypes = { :start => /^program$/, :set => /^set$/,
-	:math_op => /^[-\*+]$/, :return => /^return$/, :define => /^define$/,
-	:lparen => /^\($/, :rparen => /^\)$/, :ident => /^[a-zA-Z][a-zA-Z0-9_]*$/,
-	:int_lit => /^[0-9]+$/,	:eof => /\$/
+TokenTypes = {
+	:start => /^program$/, :set => /^set$/,	:math_op => /^[-\*+]$/,
+	:return => /^return$/, :define => /^define$/,	:lparen => /^\($/,
+	:rparen => /^\)$/, :ident => /^[a-zA-Z][a-zA-Z0-9_]*$/,
+	:int_lit => /^-?[0-9]+$/,	:eof => /\$/
 }
 
 ## ::meta-grammar:: * => 0..n, + => 1..n, or => any ONE of the following. ##
 raise ArgumentError.new "too many arguments" if ARGV.length > 1
 raise ArgumentError.new "no argument given" if ARGV.empty?
 
+def match_maker construct
+	matcher = Array.new
+	rhs = Grammar[construct] || construct
+	case rhs
+	when Array
+		rhs.each do |c|
+
+		end
+	end
+end
+## Terminal Matcher ##
+def match(terminal, token)
+	if terminal == token[:type]
+		token[:token]
+	else
+		raise ArgumentError.new "[SYNTAX ERROR] Expected:#{terminal} Got:#{token}"
+	end
+end
+
 ## A Token Type Detector ##
 def detect(token)
 	TokenTypes.each do |type, matcher|
 			return { :token => token, :type => type }
 		end
 	end
-	raise ArgumentError.new "Unknown token type for #{token}"
+	raise ArgumentError.new "[SCAN ERROR] Unknown token type for #{token}"
 end
 
 # Tokenize and scan. 
 @tree = Hash.new
 
 ## 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}"] ||=
+def recognize token, lookahead
+	@cache[:"#{token},#{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])
+		# Let's assume our key is :ReturnStatement and our val is [:return]
+			if token == first(val[0])
 
 				return key
 			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
 end
 
+## ::Example Trees returned by `parse`:: ##
+# for the following program:
+# 	set x 2
+# 	return x
+# The parse tree returned should be:
+#		{ :Program => [:set => [:x, :"2"], :return => [:x]] }
+#	With an output as follows.
+#		>Program
+#		>	set
+#		>		x
+#		>		2
+#		>	return
+#		>		x
+##
+
+# ::function:: parse, the recursive call which builds the parse tree.
+# ::args:: recognizer: the construct at the root of the tree.
 def parse recognizer
 	puts "Parsing: #{recognizer}"
 	case recognizer
-	# Array means nonliteral.
+	## Array means nonliteral.
 	when Array
-		# check for meta-grammar
-		if recognizer.first == :*
+		# Divide & Conquer step of recursion
 		return recognizer.map {|r| parse Grammar[r] || r}
-	# Symbol means literal
+	## Symbol means literal
 	when Symbol
 		tkn = @enumerator.next
 		puts tkn