From 7a925b5ee498b11afe706ffb109ab65bbd3dcab1 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Tue, 5 Mar 2013 20:28:09 -0500 Subject: [PATCH] futzing with ideas --- hy/compiler.py | 2 +- hy/lex/machine.py | 8 ++---- hy/lex/states.py | 56 ++++++++++++++++++++++++++++++++------ hy/models/expression.py | 3 +- hy/models/list.py | 28 +++++++++++++++++++ setup.py | 4 ++- tests/native_tests/math.hy | 2 +- 7 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 hy/models/list.py diff --git a/hy/compiler.py b/hy/compiler.py index 08e95dd..f212e8b 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -62,7 +62,7 @@ class HyASTCompiler(object): ret = [] tree.reverse() - if self.returnable: + if self.returnable and len(tree) > 0: el = tree.pop() if not isinstance(el, ast.stmt): ret.append(ast.Return(value=el, diff --git a/hy/lex/machine.py b/hy/lex/machine.py index 1123a8a..119ca97 100644 --- a/hy/lex/machine.py +++ b/hy/lex/machine.py @@ -65,7 +65,7 @@ class Machine(object): """ Accept and annotate the result. """ - if state and state.result: + if state and not state.result is None: result = state.result result.start_line, result.end_line = (self.start_line, self.line) @@ -87,14 +87,12 @@ class Machine(object): if self.submachine: self.submachine.process([char]) if type(self.submachine.state) == Idle: - if len(self.submachine.nodes) > 1: + if len(self.submachine.nodes) != 1: raise LexException("Funky Submachine stuff") nodes = self.submachine.nodes self.submachine = None - - if len(nodes) > 0: - self.state.nodes.append(nodes[0]) + self.state.nodes.append(nodes[0]) continue new = self.state.process(char) diff --git a/hy/lex/states.py b/hy/lex/states.py index c6cb3ba..87effe4 100644 --- a/hy/lex/states.py +++ b/hy/lex/states.py @@ -22,6 +22,7 @@ from hy.models.expression import HyExpression from hy.models.integer import HyInteger from hy.models.symbol import HySymbol from hy.models.string import HyString +from hy.models.list import HyList from hy.errors import HyError @@ -95,14 +96,7 @@ class State(object): pass # ABC -class Expression(State): - """ - Expression state. This will handle stuff like: - - (...... (....)) - ^^^^^^ -- expression - ^^^^^^^^^^^^^^^ -- expression - """ +class List(State): def enter(self): self.buf = "" @@ -118,12 +112,52 @@ class Expression(State): self.nodes.append(ret) self.buf = "" + def exit(self): + self.commit() + self.result = HyList(self.nodes) + + def process(self, char): + if char == "(": + self.machine.sub(Expression) + return + + if char == "[": + self.machine.sub(List) + return + + if char == "\"": + self.machine.sub(String) + return + + if char == "]": + return Idle + + if char in WHITESPACE: + self.commit() + return + + if self.buf == "": + self._start_line = self.machine.line + self._start_column = self.machine.column + + self.buf += char + + +class Expression(List): + """ + Expression state. This will handle stuff like: + + (...... (....)) + ^^^^^^ -- expression + ^^^^^^^^^^^^^^^ -- expression + """ + def exit(self): self.commit() if self.nodes != []: self.result = HyExpression(self.nodes) else: - self.result = None + self.result = HyList([]) def process(self, char): """ @@ -137,6 +171,10 @@ class Expression(State): self.machine.sub(Expression) return + if char == "[": + self.machine.sub(List) + return + if char == "\"": self.machine.sub(String) return diff --git a/hy/models/expression.py b/hy/models/expression.py index 74c6164..47722b4 100644 --- a/hy/models/expression.py +++ b/hy/models/expression.py @@ -19,9 +19,10 @@ # DEALINGS IN THE SOFTWARE. from hy.models import HyObject +from hy.models.list import HyList -class HyExpression(HyObject, list): +class HyExpression(HyList): """ Hy S-Expression. Basically just a list. """ diff --git a/hy/models/list.py b/hy/models/list.py new file mode 100644 index 0000000..c63e606 --- /dev/null +++ b/hy/models/list.py @@ -0,0 +1,28 @@ +# Copyright (c) 2012 Paul Tagliamonte +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. + +from hy.models import HyObject + + +class HyList(HyObject, list): + """ + Hy List. Basically just a list. + """ + pass diff --git a/setup.py b/setup.py index 9ff6b5d..4118c28 100755 --- a/setup.py +++ b/setup.py @@ -24,7 +24,9 @@ from hy import __appname__, __version__ from setuptools import setup -long_description = open('README.md', 'r').read() +long_description = """Hy is a Python <--> Lisp layer. It helps +make things work nicer, and lets Python and the Hy lisp variant play +nice together. """ setup( diff --git a/tests/native_tests/math.hy b/tests/native_tests/math.hy index b22d52d..7a56553 100644 --- a/tests/native_tests/math.hy +++ b/tests/native_tests/math.hy @@ -1,5 +1,5 @@ ; copyright .. -(def test_basic_math (fn () +(def test_basic_math (fn [] (assert (+ 2 2) 4)))