Permalink
Browse files

Added multi-line string support for the interactive REPL only.

Hope to add this to the REPL loader and the interpreter.
  • Loading branch information...
1 parent 4703fc0 commit ab4eb6e1ef0b7b61d14c46c1843782472160656f @built committed May 31, 2012
Showing with 33 additions and 9 deletions.
  1. +16 −8 bigwheel.py
  2. +9 −0 interpreter/tools.py
  3. +8 −1 tests/macro_tests.py
View
@@ -2,7 +2,7 @@
from category import Category
import fileinput
import sys
-from interpreter.tools import parse, evaluate, expand_arrows
+from interpreter.tools import parse, evaluate, expand_arrows, has_dangling_quote
from common import *
import os
@@ -44,10 +44,23 @@ def load_prelude():
load_prelude()
+def next_line_of_input():
+ line = raw_input(">> ") + "\n"
+ if not has_dangling_quote(line): return line
+ # Otherwise keep accumulating the line until we get balanced quotes.
+ accumulating = True
+ while(accumulating):
+ nextline = raw_input() + "\n"
+ line += nextline
+ accumulating = not has_dangling_quote(nextline) # stop
+
+ return line
+
while not exiting:
- line = raw_input(">> ").strip()
- command = line.lower() # The line may be a REPL command.
+ line = next_line_of_input()
+
+ command = line.lower().strip() # The line may be a REPL command.
if command in exit_commands:
exiting = True
@@ -82,11 +95,6 @@ def load_prelude():
relations_before = len(ROOT.terms)
evaluate( parse( expand_arrows(line), ROOT), ROOT )
relations_after = len(ROOT.terms)
- # print "%i relations created. Current total: %i" % ( (relations_after - relations_before), relations_after)
- # print "%i patterns now exist." % len(ROOT.comprehend("pattern"))
-
- # CategoryInspector(ROOT).dump_to_file("bigwheel.dot")
- # CategoryInspector(ROOT).dump_to_file_especial("bigwheel.dot")
View
@@ -334,3 +334,12 @@ def expand_arrows(line):
(front, back) = [x.strip() for x in line.split('->')][:2]
return "transition (pattern %s) (action %s)" % (front, back)
+
+def even(number):
+ return (number % 2) == 0
+
+def has_dangling_quote(line):
+ """
+ Returns true if the line contains an odd number of quote characters.
+ """
+ return even( len( line.split('"') ) )
View
@@ -1,12 +1,19 @@
import unittest
-from interpreter.tools import expand_arrows
+from interpreter.tools import expand_arrows, has_dangling_quote
class TestCategoryOperation(unittest.TestCase):
def test_transition_expansion(self):
test_line = "foo -> bar"
self.assertEqual("transition (pattern foo) (action bar)", expand_arrows(test_line))
+ def test_quote_imbalance(self):
+ """
+ For instance, if there is a line with three quote characters, it probably needs to
+ be considered multi-line. Or there is a typo.
+ """
+ test_line = '"foo" -> "bar '
+ self.assertTrue( has_dangling_quote(test_line))
if __name__ == '__main__':
unittest.main()

0 comments on commit ab4eb6e

Please sign in to comment.