Permalink
Browse files

reads in println, making sure priority of the regexes are correct

  • Loading branch information...
Kevin Ngo
Kevin Ngo committed Feb 5, 2012
1 parent eadabf1 commit c6108942d86e7157ff4d1a9d0e0e7c1bca0e9e00
Showing with 30 additions and 13 deletions.
  1. +30 −13 parser.rb
View
@@ -16,35 +16,52 @@
# tokenize stream of characters with regexes in if/next fashion
until s.eos?
- # trim whitespace
+ # nom whitespace, turn input to character stream
begin
l = s.scan(/\s/)
if l == '\n'
line += 1
end
end while l
- # boolean
- l = s.scan(/true/)
- l = s.scan(/false/) unless l
+ # parentheses
+ l = s.scan(/[()]/)
if l
- token = BooleanToken.new(:boolean, l)
+ token = Token.new(:openparen) if l == '('
+ token = Token.new(:closeparen) if l == ')'
st.try_set(l, token)
tokens << token
next
end
- # parenthesis
- l = s.scan(/[()]/)
+ # ==========================================
+ # STATEMENTS (print, if, while, let, assign)
+ # ==========================================
+
+ # print statement
+ l = s.scan(/println/)
if l
- token = Token.new(:openparen) if l == '('
- token = Token.new(:closeparen) if l == ')'
+ token = Token.new(:print)
+ st.try_set(l, token)
+ tokens << token
+ next
+ end
+
+ # ==========================================
+ # CONSTANTS (boolean, integer, real, string)
+ # ==========================================
+
+ # booleans
+ l = s.scan(/true/)
+ l = s.scan(/false/) unless l
+ if l
+ token = BooleanToken.new(:boolean, l)
st.try_set(l, token)
tokens << token
next
end
- # real number / float
+ # reals
l = s.scan(/\d+\.(\d+)?/)
if l
token = RealToken.new(:real, l.to_f)
@@ -53,7 +70,7 @@
next
end
- # integer
+ # integers
l = s.scan(/\d+/)
if l
token = IntegerToken.new(:int, l.to_i)
@@ -62,7 +79,7 @@
next
end
- # string literal
+ # strings (quoted)
l = s.scan(/"(.*)"/)
l = s.scan(/'(.*)'/) unless l
if l
@@ -72,7 +89,7 @@
next
end
- # bare word
+ # strings (bare)
l = s.scan(/[^\s)]+/)
if l
token = StringToken.new(:string, l)

0 comments on commit c610894

Please sign in to comment.