Class: Theta::Parser
- Inherits:
-
Object
- Object
- Theta::Parser
- Defined in:
- lib/theta/parser.rb
Overview
parses strings into arrays of items understandable by the interpreter
Instance Method Summary collapse
-
#atom(token) ⇒ Object
returns appropriate numeric object if a number, otherwise returns a symbol.
-
#parse(string) ⇒ Object
returns the scheme interpretation of the string.
-
#read_from(tokens) ⇒ Object
reads an expression from an array of tokens (created by tokenize).
- #restructure(token_array, offset = 0) ⇒ Object
-
#to_string(expression) ⇒ Object
convert an expression back to a readable string.
-
#tokenize(string) ⇒ Object
converts a string into an array of tokens.
Instance Method Details
#atom(token) ⇒ Object
returns appropriate numeric object if a number, otherwise returns a symbol
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/theta/parser.rb', line 115 def atom(token) if token.start_with?("\"") return token.gsub("\"", "") end token.gsub!(/\n\t/, "") begin return Integer(token) rescue ArgumentError begin return Float(token) rescue ArgumentError return token.to_sym end end end |
#parse(string) ⇒ Object
returns the scheme interpretation of the string
6 7 8 |
# File 'lib/theta/parser.rb', line 6 def parse(string) return read_from(tokenize(string)) end |
#read_from(tokens) ⇒ Object
reads an expression from an array of tokens (created by tokenize)
71 72 73 74 75 76 77 78 |
# File 'lib/theta/parser.rb', line 71 def read_from(tokens) if tokens.length == 0 raise SyntaxError, "unexpected EOF while reading" end tokens.delete_if { |item| item.strip == "" || item == []} tmp, expression = restructure tokens return expression end |
#restructure(token_array, offset = 0) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/theta/parser.rb', line 80 def restructure(token_array, offset = 0) statement = [] while offset < token_array.length if token_array[offset] == "(" offset, tmp_array = restructure(token_array, offset + 1) statement << tmp_array elsif token_array[offset] == ")" break else statement << atom(token_array[offset]) end offset += 1 end return offset, statement end |
#to_string(expression) ⇒ Object
convert an expression back to a readable string
132 133 134 135 136 137 138 139 |
# File 'lib/theta/parser.rb', line 132 def to_string(expression) if expression.is_a? Array expression.map { |exp| to_string(exp) } return "(" + expression.join(" ") + ")" else return expression.to_s end end |
#tokenize(string) ⇒ Object
converts a string into an array of tokens
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 |
# File 'lib/theta/parser.rb', line 11 def tokenize(string) tokens = [] found_string = false temp = "" # loop through each character passed string.each_char do |char| # if we're currently in a string and we hit the quote, # it's the end of the string. # move the string into the token array if found_string if char == "\"" found_string = false temp << char if not temp.empty? tokens << temp end temp = "" # otherwise add the character to the temporary variable else temp << char end else # if we hit parentheses, add whatever's in temp as a token # then add the parentheses as a token if char == "(" || char == ")" if not temp.empty? tokens << temp temp = "" end tokens << char next # if we hit a quote, we're beginning a string # flip the found_string flag and begin adding to the temp variable elsif char == "\"" found_string = true temp << char # space signals the end of a token, push the temp variable on the token queue elsif char == " " if not temp.empty? tokens << temp end temp = "" # ignore tabs or newlines, unless there's something in temp # push that onto tokens elsif char == "\t" || char == "\n" if not temp.empty? tokens << temp end temp = "" # add the character to temp to build a token else temp << char end end end return tokens end |