diff --git a/.vimrc b/.vimrc new file mode 100644 index 0000000..3e811b4 --- /dev/null +++ b/.vimrc @@ -0,0 +1,4 @@ +" Force indentation styles for this directory +autocmd FileType python set shiftwidth=4 +autocmd FileType python set tabstop=4 +autocmd FileType python set softtabstop=4 diff --git a/chapter2.py b/chapter1and2.py similarity index 98% rename from chapter2.py rename to chapter1and2.py index ec3b4b9..4e21d9e 100644 --- a/chapter2.py +++ b/chapter1and2.py @@ -1,4 +1,4 @@ -# Chapter 1&2 - Lexer and Parser +# Chapter 1 & 2 - Lexer and Parser from collections import namedtuple from enum import Enum @@ -156,7 +156,7 @@ class ParseError(Exception): pass class Parser(object): """Parser for the Kaleidoscope language. - + After the parser is created, invoke parse_toplevel multiple times to parse Kaleidoscope source into an AST. """ @@ -457,5 +457,7 @@ class TestParser(unittest.TestCase): if __name__ == '__main__': + # We just have the lexer and parser here, no code generation yet. This is + # just a simple way to parse Kaleidoscope expressions and dump the AST. p = Parser() print(p.parse_toplevel('def bina(a b) a + b').dump()) diff --git a/chapter3and4.py b/chapter3and4.py index 825c943..c5f61dc 100644 --- a/chapter3and4.py +++ b/chapter3and4.py @@ -173,7 +173,7 @@ class ParseError(Exception): pass class Parser(object): """Parser for the Kaleidoscope language. - + After the parser is created, invoke parse_toplevel multiple times to parse Kaleidoscope source into an AST. """ @@ -553,6 +553,9 @@ class TestEvaluator(unittest.TestCase): if __name__ == '__main__': + # Example of how to define a couple of functions and then evaluate + # expressions involving them. A single KaleidoscopeEvaluator object retains + # its state across multiple calls to 'evaluate'. kalei = KaleidoscopeEvaluator() print(kalei.evaluate('def adder(a b) a + b')) print(kalei.evaluate('def foo(x) (1+2+x)*(x+(1+2))'))