about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--users/wpcarro/scratch/simple-select/main.py13
-rw-r--r--users/wpcarro/scratch/simple-select/scanner.py27
2 files changed, 40 insertions, 0 deletions
diff --git a/users/wpcarro/scratch/simple-select/main.py b/users/wpcarro/scratch/simple-select/main.py
new file mode 100644
index 000000000000..6a86324ef73f
--- /dev/null
+++ b/users/wpcarro/scratch/simple-select/main.py
@@ -0,0 +1,13 @@
+from scanner import Scanner
+
+def tokenize(x):
+  s = Scanner(x)
+  return None
+
+def main():
+  while True:
+    x = input("> ")
+    print(tokenize(x))
+
+if __name__ == "__main__":
+  main()
diff --git a/users/wpcarro/scratch/simple-select/scanner.py b/users/wpcarro/scratch/simple-select/scanner.py
new file mode 100644
index 000000000000..96704ec1ab3c
--- /dev/null
+++ b/users/wpcarro/scratch/simple-select/scanner.py
@@ -0,0 +1,27 @@
+# According to Crafting Interpreters, the only two primitives that a
+# scanner/lexer needs are peek and advance; other functions (e.g. match) are
+# nice-to-haves.
+class Scanner(object):
+  def __init__(self, source):
+    self.i = 0
+    self.source = source
+
+  def exhausted(self):
+    return self.i >= len(self.source)
+
+  def peek(self, n=0):
+    return self.source[self.i + n] if self.i + n < len(self.source) else '\0'
+
+  def advance(self):
+    result = self.peek()
+    self.i += 1
+    return result
+
+  def match(self, x):
+    if self.exhausted():
+      return False
+    if self.peek() == x:
+      self.advance()
+      return True
+    else:
+      return False