hy/hy/core/bootstrap.py

147 lines
3.9 KiB
Python
Raw Normal View History

2013-03-18 10:27:14 -04:00
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
2013-03-07 22:52:47 -05:00
#
# 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 macro
2013-03-08 18:18:43 -05:00
from hy.models.expression import HyExpression
2013-03-18 16:11:29 -04:00
from hy.models.integer import HyInteger
2013-03-08 18:18:43 -05:00
from hy.models.symbol import HySymbol
2013-03-13 20:41:53 -04:00
from hy.models.list import HyList
2013-03-07 22:52:47 -05:00
2013-03-08 18:18:43 -05:00
@macro("defn")
2013-04-01 16:36:56 -05:00
@macro("defun")
2013-03-08 18:18:43 -05:00
def defn_macro(tree):
return HyExpression([HySymbol("def"),
tree[1], HyExpression([HySymbol("fn")] + tree[2:])])
2013-03-09 00:17:02 -05:00
@macro("cond")
def cond_macro(tree):
tree.pop(0) # cond flag
it = iter(tree)
test, branch = next(it)
2013-03-09 00:17:02 -05:00
root = HyExpression([HySymbol("if"), test, branch])
ret = root
for (test, branch) in it:
2013-03-09 00:17:02 -05:00
n = HyExpression([HySymbol("if"), test, branch])
ret.append(n)
ret = n
return root
2013-03-12 22:04:51 -04:00
2013-03-13 20:41:53 -04:00
@macro("for")
def for_macro(tree):
tree.pop(0)
ret = None
# for [x iter y iter] ...
# ->
# foreach x iter
# foreach y iter
# ...
it = iter(tree.pop(0))
blocks = list(zip(it, it)) # List for Python 3.x degenerating.
key, val = blocks.pop(0)
ret = HyExpression([HySymbol("foreach"),
HyList([key, val])])
root = ret
ret.replace(tree)
for key, val in blocks:
# x, [1, 2, 3, 4]
nret = HyExpression([HySymbol("foreach"),
HyList([key, val])])
nret.replace(key)
ret.append(nret)
ret = nret
[ret.append(x) for x in tree] # we really need ~@
return root
@macro("_>")
2013-03-12 22:04:51 -04:00
def threading_macro(tree):
2013-03-13 09:03:50 -04:00
tree.pop(0)
2013-03-12 22:04:51 -04:00
ret = tree.pop(0)
for node in tree:
2013-03-13 09:03:50 -04:00
if not isinstance(node, HyExpression):
nnode = HyExpression([node])
nnode.replace(node)
node = nnode
node.insert(1, ret)
2013-03-12 22:04:51 -04:00
ret = node
2013-03-13 09:03:50 -04:00
return ret
2013-03-18 16:11:29 -04:00
2013-04-07 15:05:30 -04:00
@macro("_>>")
def threading_tail_macro(tree):
tree.pop(0)
ret = tree.pop(0)
for node in tree:
if not isinstance(node, HyExpression):
nnode = HyExpression([node])
nnode.replace(node)
node = nnode
node.append(ret)
ret = node
return ret
2013-03-18 16:11:29 -04:00
@macro("car")
@macro("first")
def first_macro(tree):
tree.pop(0) # "first"
ret = tree.pop(0) # the list
# assert tree is empty
return HyExpression([HySymbol('get'),
ret,
HyInteger(0)])
2013-03-18 19:49:36 -04:00
@macro("cdr")
@macro("rest")
def rest_macro(tree):
tree.pop(0) # "first"
ret = tree.pop(0) # the list
# assert tree is empty
return HyExpression([HySymbol('slice'),
ret,
HyInteger(1)])
2013-04-05 21:53:44 -04:00
@macro("let")
def let_macro(tree):
tree.pop(0) # "let"
ret = tree.pop(0) # vars
# tree is now the body
expr = HyExpression([HySymbol("fn"), HyList([])])
for var in ret:
expr.append(HyExpression([HySymbol("setf"), var[0], var[1]]))
for stmt in tree:
expr.append(stmt)
return HyExpression([expr])