Created
August 13, 2010 23:49
-
-
Save ldunn/523736 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'ast' | |
| class Parser | |
| attr_accessor :stack | |
| attr_accessor :env | |
| attr_accessor :evaling | |
| def initialize(stack, words) | |
| @words = words | |
| @dec_valid = ('0'..'9').to_a + ['.'] + ['-'] | |
| @hex_valid = ('0'..'9').to_a + ('a'..'z').to_a | |
| @stack = stack | |
| @evaling = true | |
| @in_string = false | |
| end | |
| def parse(token) | |
| new = [] | |
| if token =~ /\".*\"/ | |
| new.push token[1..-2] | |
| elsif token =~/^\".*/ | |
| p "start of string" | |
| new.push token | |
| @evaling = false | |
| @in_string = true | |
| elsif token =~ /^.*\"/ | |
| p "end of string" | |
| #Gobble up the string | |
| str = [token] | |
| temp = @stack.pop | |
| while (temp != "\"" and temp !~ /\".*/ and temp != nil) | |
| str << [temp] | |
| temp = @stack.pop | |
| end | |
| str << [temp] | |
| p str | |
| new.push ((str.reverse) * " ")[1..-2] | |
| @evaling = true | |
| @in_string = false | |
| p @in_string | |
| elsif @in_string | |
| new.push token.to_s | |
| elsif token == "[" | |
| new.push "[" | |
| @evaling = false | |
| elsif token == "]" | |
| #Gobble up the quotation | |
| quot = Quotation.new([]) | |
| temp = @stack.pop | |
| while temp != "[" | |
| quot.contents.push temp | |
| temp = @stack.pop | |
| end | |
| quot.contents.reverse! | |
| new.push quot | |
| @evaling = true | |
| elsif @words[token] | |
| new.push @words[token] | |
| elsif token.split('').all? {|c| @dec_valid.member? c } | |
| new.push token.to_f | |
| elsif token.split('0x')[1] | |
| new.push token.hex.to_f if token.split('0x')[1].downcase.split('').all? {|c| @hex_valid.member? c} | |
| elsif token.split('0b')[1] | |
| new.push token.to_i(2).to_f if token.split('0b')[1].downcase.split('').all? {|c| ['0','1'].member? c} | |
| else | |
| puts "ERROR: Unrecognised token" | |
| end | |
| @stack.push new | |
| @stack.flatten! | |
| return new | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment