Jank @olasd's hack, clean up core.

The core shall from now on be only for the core language bits. Macro
 bits shall live in hy.macros and in hy.compiler. This cleans up
 garbage.
This commit is contained in:
Paul Tagliamonte 2013-07-06 14:00:11 -04:00
parent 8d96f68bd6
commit b78be9a594
6 changed files with 37 additions and 70 deletions

View File

@ -35,13 +35,12 @@ from hy.importer import ast_compile, import_buffer_to_module
from hy.lex.states import Idle, LexException
from hy.lex.machine import Machine
from hy.compiler import hy_compile
from hy.core import process
from hy.completer import completion
from hy.macros import macro, require, process
from hy.models.expression import HyExpression
from hy.models.string import HyString
from hy.models.symbol import HySymbol
from hy.macros import macro, require
_machine = Machine(Idle, 1, 0)

View File

@ -35,11 +35,9 @@ from hy.models.float import HyFloat
from hy.models.list import HyList
from hy.models.dict import HyDict
import hy.importer
from hy.macros import require
from hy.macros import require, process
from hy.util import str_type
from hy.core import process
import hy.importer
import traceback
import importlib
@ -394,7 +392,6 @@ class HyASTCompiler(object):
def compile_atom(self, atom_type, atom):
if atom_type in _compile_table:
atom = process(atom, self.module_name)
ret = _compile_table[atom_type](self, atom)
if not isinstance(ret, Result):
ret = Result() + ret
@ -402,6 +399,7 @@ class HyASTCompiler(object):
def compile(self, tree):
try:
tree = process(tree, self.module_name)
_type = type(tree)
ret = self.compile_atom(_type, tree)
if ret:
@ -633,10 +631,10 @@ class HyASTCompiler(object):
def compile_eval(self, expr):
expr.pop(0)
ret = self.compile(HyExpression([
HySymbol("hy_eval")] + expr + [
HyExpression([HySymbol("locals")])] + [
HyString(self.module_name)]).replace(expr))
ret = self.compile(HyExpression(
[HySymbol("hy_eval")] + expr + [HyExpression([HySymbol("locals")])]
+ [HyString(self.module_name)]).replace(expr)
)
ret.add_imports("hy.importer", ["hy_eval"])

View File

@ -1,43 +1,3 @@
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
#
# 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.macros import process as mprocess
MACROS = [
"hy.core.bootstrap",
]
STDLIB = [
"hy.core.language"
]
def process(tree, module_name):
load_macros()
old = None
while old != tree:
old = tree
tree = mprocess(tree, module_name)
return tree
def load_macros():
for module in MACROS:
__import__(module)

View File

@ -30,6 +30,11 @@ from hy.util import str_type
from collections import defaultdict
MACROS = [
"hy.core.bootstrap",
]
_hy_macros = defaultdict(dict)
@ -69,6 +74,20 @@ _wrappers = {
def process(tree, module_name):
load_macros()
old = None
while old != tree:
old = tree
tree = macroexpand(tree, module_name)
return tree
def load_macros():
for module in MACROS:
__import__(module)
def macroexpand(tree, module_name):
if isinstance(tree, HyExpression):
if tree == []:
return tree
@ -76,9 +95,7 @@ def process(tree, module_name):
fn = tree[0]
if fn in ("quote", "quasiquote"):
return tree
ntree = HyExpression(
[fn] + [process(x, module_name) for x in tree[1:]]
)
ntree = HyExpression(tree[:])
ntree.replace(tree)
if isinstance(fn, HyString):
@ -90,16 +107,5 @@ def process(tree, module_name):
obj.replace(tree)
return obj
ntree.replace(tree)
return ntree
if isinstance(tree, HyList):
obj = tree.__class__([process(x, module_name) for x in tree]) # NOQA
# flake8 thinks we're redefining from 52.
obj.replace(tree)
return obj
if isinstance(tree, list):
return [process(x, module_name) for x in tree]
return tree

View File

@ -53,14 +53,14 @@ class HyASTCompilerTest(unittest.TestCase):
h.end_line = 1
h.start_column = 1
h.end_column = 1
return h
return h.replace(h)
def setUp(self):
self.c = compiler.HyASTCompiler('test')
def test_fn_compiler_empty_function(self):
ret = self.c.compile_function_def(
self._make_expression("fn", HyList()))
self._make_expression(HySymbol("fn"), HyList()))
self.assertEqual(ret.imports, {})
self.assertEqual(len(ret.stmts), 1)

View File

@ -4,6 +4,8 @@ from hy.lex import tokenize
from hy.models.string import HyString
from hy.models.list import HyList
from hy.models.symbol import HySymbol
from hy.models.expression import HyExpression
@macro("test")
@ -20,13 +22,15 @@ def test_preprocessor_simple():
def test_preprocessor_expression():
""" Test inner macro expansion """
""" Test that macro expansion doesn't recurse"""
obj = process(tokenize('(test (test "one" "two"))')[0], __name__)
assert type(obj) == HyList
assert type(obj[0]) == HyList
assert type(obj[0]) == HyExpression
assert obj[0] == HyList([HyString("one"), HyString("two")])
assert obj[0] == HyExpression([HySymbol("test"),
HyString("one"),
HyString("two")])
obj = HyList([HyString("one"), HyString("two")])
obj = tokenize('(shill ["one" "two"])')[0][1]