Permalink
Browse files

fixed some tests

  • Loading branch information...
1 parent 4469d6b commit 7dde1944510ddaa7f6cf2ac54b2343900c176471 @juliend2 committed Apr 9, 2011
Showing with 221 additions and 214 deletions.
  1. +6 −6 grammar.y
  2. +36 −33 lexer.rb
  3. +148 −144 parser.rb
  4. +1 −0 phlower.rb
  5. +2 −2 runtime.rb
  6. +28 −29 tests.rb
View
@@ -146,19 +146,19 @@ rule
# if and if-else block
If:
- IF Expression Block { result = IfNode.new(val[1], val[2]) }
- | IF Expression Block NEWLINE
- ELSE Block { result = IfNode.new(val[1], val[2], val[5]) }
+ IF Expression Block
+ ELSE Block { result = IfNode.new(val[1], val[2], val[4]) }
+ | IF Expression Block { result = IfNode.new(val[1], val[2]) }
;
# A block of indented code. You see here that all the hard work was done
# by the lexer.
Block:
- INDENT Expressions DEDENT { result = val[1] }
+ # INDENT Expressions DEDENT { result = val[1] }
# If you don't like indentation you could replace the previous rule with
# the following one do seperate blocks w/ "{" ... "}".
# (You'll need remove the indentation magic section in the lexer too)
- # "{" Expressions "}" { replace = val[1] }
+ "{" Expressions "}" { result = val[1] }
;
end
@@ -175,4 +175,4 @@ end
def next_token
@tokens.shift
- end
+ end
View
@@ -12,11 +12,11 @@ def tokenize(code)
tokens = []
# Current indent level is the number of spaces in the last indent.
- current_indent = 0
+ #current_indent = 0
# We keep track of the indentation levels we are in so
# that when we dedent, we can check if we're on the
# correct level.
- indent_stack = []
+ # indent_stack = []
# This is how to implement a very simple scanner.
# Scan one caracter at the time until you find something to parse.
@@ -63,38 +63,41 @@ def tokenize(code)
#
# This elsif takes care of the first case.
# The number of spaces will determine the indent level.
- elsif indent = chunk[/\A\:\n( +)/m, 1]
- # When we create a new block we expect the indent level
- # to go up.
- if indent.size <= current_indent
- raise "Bad indent level, got #{indent.size} indents, " +
- "expected > #{current_indent}"
- end
- # Adjust the current indentation level.
- current_indent = indent.size
- indent_stack.push(current_indent)
- tokens << [:INDENT, indent.size]
- i += indent.size + 2
+ # elsif indent = chunk[/\A\:\n( +)/m, 1]
+ # # When we create a new block we expect the indent level
+ # # to go up.
+ # if indent.size <= current_indent
+ # raise "Bad indent level, got #{indent.size} indents, " +
+ # "expected > #{current_indent}"
+ # end
+ # # Adjust the current indentation level.
+ # current_indent = indent.size
+ # indent_stack.push(current_indent)
+ # tokens << [:INDENT, indent.size]
+ # i += indent.size + 2
# This one takes care of cases 2 and 3.
# We stay in the same block if the indent level is the
# same as current_indent, or close a block, if it is lower.
- elsif indent = chunk[/\A\n( *)/m, 1]
- if indent.size < current_indent
- indent_stack.pop
- current_indent = indent_stack.first || 0
- tokens << [:DEDENT, indent.size]
- tokens << [:NEWLINE, "\n"]
- elsif indent.size == current_indent
- # Nothing to do, we're still in the same block
- tokens << [:NEWLINE, "\n"]
- else # indent.size > current_indent
- # Cannot increase indent level without using ":", so
- # this is an error.
- raise "Missing ':'"
- end
- i += indent.size + 1
+ # elsif indent = chunk[/\A\n( *)/m, 1]
+ # if indent.size < current_indent
+ # indent_stack.pop
+ # current_indent = indent_stack.first || 0
+ # tokens << [:DEDENT, indent.size]
+ # tokens << [:NEWLINE, "\n"]
+ # elsif indent.size == current_indent
+ # # Nothing to do, we're still in the same block
+ # tokens << [:NEWLINE, "\n"]
+ # else # indent.size > current_indent
+ # # Cannot increase indent level without using ":", so
+ # # this is an error.
+ # raise "Missing ':'"
+ # end
+ # i += indent.size + 1
+ elsif indent = chunk[/\A\n( *)/m, 1]
+ tokens << [:NEWLINE, "\n"]
+ i += 1
# Ignore whitespace
elsif chunk.match(/\A /)
i += 1
@@ -111,9 +114,9 @@ def tokenize(code)
end
# Close all open blocks
- while indent = indent_stack.pop
- tokens << [:DEDENT, indent_stack.first || 0]
- end
+ # while indent = indent_stack.pop
+ # tokens << [:DEDENT, indent_stack.first || 0]
+ # end
tokens
end
@@ -149,4 +152,4 @@ def tokenize(code)
# [:NEWLINE, "\n"], [:ELSE, "else"],
# [:INDENT, 2], [:IDENTIFIER, "weird"], ["(", "("], [")", ")"],
# [:DEDENT, 0],
-# [:NEWLINE, "\n"]]
+# [:NEWLINE, "\n"]]
Oops, something went wrong.

0 comments on commit 7dde194

Please sign in to comment.