Merge branch 'master' of github.com:paultag/hy
This commit is contained in:
commit
93625be7eb
1
AUTHORS
1
AUTHORS
@ -1,6 +1,7 @@
|
|||||||
Code contributors:
|
Code contributors:
|
||||||
|
|
||||||
- Paul R. Tagliamonte <tag@pault.ag>
|
- Paul R. Tagliamonte <tag@pault.ag>
|
||||||
|
- Gergely Nagy <algernon@madhouse-project.org>
|
||||||
|
|
||||||
I'd also like to thank the following people:
|
I'd also like to thank the following people:
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import hy.lang.importer
|
import hy.lang.importer # NOQA
|
||||||
from app import app
|
from app import app
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -10,9 +10,6 @@ from hy.lang.list import HYList
|
|||||||
from hy.lang.bool import HYBool
|
from hy.lang.bool import HYBool
|
||||||
from hy.lang.map import HYMap
|
from hy.lang.map import HYMap
|
||||||
|
|
||||||
from hy.lang.builtins import builtins
|
|
||||||
from hy.lang.natives import natives
|
|
||||||
|
|
||||||
|
|
||||||
def _ast_print(node, children, obj):
|
def _ast_print(node, children, obj):
|
||||||
""" Handle `print' statements """
|
""" Handle `print' statements """
|
||||||
@ -30,7 +27,7 @@ def _ast_binop(node, children, obj):
|
|||||||
# XXX: Add these folks in
|
# XXX: Add these folks in
|
||||||
|
|
||||||
inv = node.get_invocation()
|
inv = node.get_invocation()
|
||||||
ops = { "+": ast.Add, "/": ast.Div, "*": ast.Mult, "-": ast.Sub }
|
ops = {"+": ast.Add, "/": ast.Div, "*": ast.Mult, "-": ast.Sub}
|
||||||
op = ops[inv['function']]
|
op = ops[inv['function']]
|
||||||
left = children.pop(0)
|
left = children.pop(0)
|
||||||
calc = None
|
calc = None
|
||||||
@ -164,6 +161,10 @@ class AST27Converter(object):
|
|||||||
"for": self._ast_for,
|
"for": self._ast_for,
|
||||||
"kwapply": self._ast_kwapply,
|
"kwapply": self._ast_kwapply,
|
||||||
}
|
}
|
||||||
|
self.special_types = {
|
||||||
|
HYMap: self._ast_fn_index,
|
||||||
|
HYList: self._ast_fn_index,
|
||||||
|
}
|
||||||
self.in_fn = False
|
self.in_fn = False
|
||||||
|
|
||||||
def _ast_set_index(self, node):
|
def _ast_set_index(self, node):
|
||||||
@ -194,6 +195,13 @@ class AST27Converter(object):
|
|||||||
ast.Index(value=self.render(tar), ctx=ast.Load()),
|
ast.Index(value=self.render(tar), ctx=ast.Load()),
|
||||||
ast.Load())
|
ast.Load())
|
||||||
|
|
||||||
|
def _ast_fn_index(self, node):
|
||||||
|
i = node.get_invocation()
|
||||||
|
cmd = ["index"]
|
||||||
|
cmd.append(i['function'])
|
||||||
|
cmd.extend(i['args'])
|
||||||
|
return self.render_expression(HYExpression(cmd))
|
||||||
|
|
||||||
def _ast_dot(self, node):
|
def _ast_dot(self, node):
|
||||||
inv = node.get_invocation()
|
inv = node.get_invocation()
|
||||||
args = inv['args']
|
args = inv['args']
|
||||||
@ -268,7 +276,6 @@ class AST27Converter(object):
|
|||||||
orelse=orel,
|
orelse=orel,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _ast_for(self, node):
|
def _ast_for(self, node):
|
||||||
i = node.get_invocation()
|
i = node.get_invocation()
|
||||||
args = i['args']
|
args = i['args']
|
||||||
@ -388,6 +395,9 @@ class AST27Converter(object):
|
|||||||
|
|
||||||
inv = node.get_invocation()
|
inv = node.get_invocation()
|
||||||
|
|
||||||
|
if type(inv['function']) in self.special_types:
|
||||||
|
return self.special_types[type(inv['function'])](node)
|
||||||
|
|
||||||
if inv['function'] in self.native_cases:
|
if inv['function'] in self.native_cases:
|
||||||
return self.native_cases[inv['function']](node)
|
return self.native_cases[inv['function']](node)
|
||||||
|
|
||||||
@ -402,12 +412,12 @@ class AST27Converter(object):
|
|||||||
if inv['function'] in special_cases:
|
if inv['function'] in special_cases:
|
||||||
return special_cases[inv['function']](node, c, self)
|
return special_cases[inv['function']](node, c, self)
|
||||||
|
|
||||||
ret = value=ast.Call(
|
ret = ast.Call(
|
||||||
func=self.render_symbol(inv['function']),
|
func=self.render_symbol(inv['function']),
|
||||||
args=c,
|
args=c,
|
||||||
keywords=[],
|
keywords=[],
|
||||||
starargs=None,
|
starargs=None,
|
||||||
kwargs=None
|
kwargs=None
|
||||||
)
|
)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@ -422,7 +432,6 @@ class AST27Converter(object):
|
|||||||
node.lineno = tree.line
|
node.lineno = tree.line
|
||||||
node.col_offset = tree.column
|
node.col_offset = tree.column
|
||||||
|
|
||||||
|
|
||||||
if isinstance(ret, list):
|
if isinstance(ret, list):
|
||||||
for r in ret:
|
for r in ret:
|
||||||
_correct_tree(r, tree)
|
_correct_tree(r, tree)
|
||||||
|
@ -6,4 +6,4 @@ class HYBool(HYObject):
|
|||||||
self._val = val
|
self._val = val
|
||||||
|
|
||||||
def eval(self, lns, *args, **kwargs):
|
def eval(self, lns, *args, **kwargs):
|
||||||
return self._val == True
|
return self._val is True
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#
|
#
|
||||||
import sys
|
import sys
|
||||||
from hy.lang.internals import HYNamespaceCOW
|
|
||||||
from hy.lang.string import HYString
|
from hy.lang.string import HYString
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ from hy.compiler.ast27 import forge_module
|
|||||||
|
|
||||||
from hy.lex.tokenize import tokenize
|
from hy.lex.tokenize import tokenize
|
||||||
import sys
|
import sys
|
||||||
import imp
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from hy.lang.bool import HYBool
|
|
||||||
from hy.lex.tokenize import tokenize as _hy_tok
|
from hy.lex.tokenize import tokenize as _hy_tok
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@ -24,6 +23,7 @@ def _foreach(*args):
|
|||||||
for arg in a:
|
for arg in a:
|
||||||
args[1](arg)
|
args[1](arg)
|
||||||
|
|
||||||
|
|
||||||
def _get(*args):
|
def _get(*args):
|
||||||
m = args[0]
|
m = args[0]
|
||||||
k = args[1]
|
k = args[1]
|
||||||
@ -85,7 +85,6 @@ def _ne(*args):
|
|||||||
|
|
||||||
|
|
||||||
def _gt(*args):
|
def _gt(*args):
|
||||||
arg = args[0]
|
|
||||||
for i in range(1, len(args)):
|
for i in range(1, len(args)):
|
||||||
if not (args[i - 1] > args[i]):
|
if not (args[i - 1] > args[i]):
|
||||||
return False
|
return False
|
||||||
@ -93,7 +92,6 @@ def _gt(*args):
|
|||||||
|
|
||||||
|
|
||||||
def _ge(*args):
|
def _ge(*args):
|
||||||
arg = args[0]
|
|
||||||
for i in range(1, len(args)):
|
for i in range(1, len(args)):
|
||||||
if not (args[i - 1] >= args[i]):
|
if not (args[i - 1] >= args[i]):
|
||||||
return False
|
return False
|
||||||
@ -101,7 +99,6 @@ def _ge(*args):
|
|||||||
|
|
||||||
|
|
||||||
def _le(*args):
|
def _le(*args):
|
||||||
arg = args[0]
|
|
||||||
for i in range(1, len(args)):
|
for i in range(1, len(args)):
|
||||||
if not (args[i - 1] <= args[i]):
|
if not (args[i - 1] <= args[i]):
|
||||||
return False
|
return False
|
||||||
@ -109,7 +106,6 @@ def _le(*args):
|
|||||||
|
|
||||||
|
|
||||||
def _lt(*args):
|
def _lt(*args):
|
||||||
arg = args[0]
|
|
||||||
for i in range(1, len(args)):
|
for i in range(1, len(args)):
|
||||||
if not (args[i - 1] < args[i]):
|
if not (args[i - 1] < args[i]):
|
||||||
return False
|
return False
|
||||||
|
@ -74,17 +74,22 @@ class Comment(State):
|
|||||||
|
|
||||||
class Idle(State):
|
class Idle(State):
|
||||||
def p(self, x):
|
def p(self, x):
|
||||||
if x == "#": return HashExpression
|
if x == "#":
|
||||||
if x == ";": return Comment
|
return HashExpression
|
||||||
if x == "(": return Expression
|
if x == ";":
|
||||||
if x in WHITESPACE: return
|
return Comment
|
||||||
|
if x == "(":
|
||||||
|
return Expression
|
||||||
|
if x in WHITESPACE:
|
||||||
|
return
|
||||||
|
|
||||||
raise LexException("Unknown char: %s" % (x))
|
raise LexException("Unknown char: %s" % (x))
|
||||||
|
|
||||||
|
|
||||||
class HashExpression(State):
|
class HashExpression(State):
|
||||||
def p(self, x):
|
def p(self, x):
|
||||||
if x == "!": return Comment
|
if x == "!":
|
||||||
|
return Comment
|
||||||
|
|
||||||
raise LexException("Unknwon Hash modifier - %s" % (x))
|
raise LexException("Unknwon Hash modifier - %s" % (x))
|
||||||
|
|
||||||
@ -106,13 +111,26 @@ class Expression(State):
|
|||||||
self.bulk = ""
|
self.bulk = ""
|
||||||
|
|
||||||
def p(self, x):
|
def p(self, x):
|
||||||
if x == ")": return Idle
|
if x == ")":
|
||||||
if x in WHITESPACE: self.commit(); return
|
return Idle
|
||||||
if x == "\"": self.sub(String); return
|
if x in WHITESPACE:
|
||||||
if x == "(": self.sub(Expression); return
|
self.commit()
|
||||||
if x == "[": self.sub(List); return
|
return
|
||||||
if x == "{": self.sub(Map); return
|
if x == "\"":
|
||||||
if x == ";": self.sub(Comment); return
|
self.sub(String)
|
||||||
|
return
|
||||||
|
if x == "(":
|
||||||
|
self.sub(Expression)
|
||||||
|
return
|
||||||
|
if x == "[":
|
||||||
|
self.sub(List)
|
||||||
|
return
|
||||||
|
if x == "{":
|
||||||
|
self.sub(Map)
|
||||||
|
return
|
||||||
|
if x == ";":
|
||||||
|
self.sub(Comment)
|
||||||
|
return
|
||||||
self.bulk += x
|
self.bulk += x
|
||||||
|
|
||||||
|
|
||||||
@ -132,13 +150,26 @@ class List(State):
|
|||||||
self.bulk = ""
|
self.bulk = ""
|
||||||
|
|
||||||
def p(self, x):
|
def p(self, x):
|
||||||
if x == "]": return Idle
|
if x == "]":
|
||||||
if x in WHITESPACE: self.commit(); return
|
return Idle
|
||||||
if x == "\"": self.sub(String); return
|
if x in WHITESPACE:
|
||||||
if x == "[": self.sub(List); return
|
self.commit()
|
||||||
if x == "(": self.sub(Expression); return
|
return
|
||||||
if x == "{": self.sub(Map); return
|
if x == "\"":
|
||||||
if x == ";": self.sub(Comment); return
|
self.sub(String)
|
||||||
|
return
|
||||||
|
if x == "[":
|
||||||
|
self.sub(List)
|
||||||
|
return
|
||||||
|
if x == "(":
|
||||||
|
self.sub(Expression)
|
||||||
|
return
|
||||||
|
if x == "{":
|
||||||
|
self.sub(Map)
|
||||||
|
return
|
||||||
|
if x == ";":
|
||||||
|
self.sub(Comment)
|
||||||
|
return
|
||||||
self.bulk += x
|
self.bulk += x
|
||||||
|
|
||||||
|
|
||||||
@ -167,18 +198,31 @@ class Map(State):
|
|||||||
self.bulk = ""
|
self.bulk = ""
|
||||||
|
|
||||||
def p(self, x):
|
def p(self, x):
|
||||||
if x == "}": return Idle
|
if x == "}":
|
||||||
if x in WHITESPACE: self.commit(); return
|
return Idle
|
||||||
if x == "\"": self.sub(String); return
|
if x in WHITESPACE:
|
||||||
if x == "[": self.sub(List); return
|
self.commit()
|
||||||
if x == "{": self.sub(Map); return
|
return
|
||||||
if x == "(": self.sub(Expression); return
|
if x == "\"":
|
||||||
if x == ";": self.sub(Comment); return
|
self.sub(String)
|
||||||
|
return
|
||||||
|
if x == "[":
|
||||||
|
self.sub(List)
|
||||||
|
return
|
||||||
|
if x == "{":
|
||||||
|
self.sub(Map)
|
||||||
|
return
|
||||||
|
if x == "(":
|
||||||
|
self.sub(Expression)
|
||||||
|
return
|
||||||
|
if x == ";":
|
||||||
|
self.sub(Comment)
|
||||||
|
return
|
||||||
self.bulk += x
|
self.bulk += x
|
||||||
|
|
||||||
|
|
||||||
class String(State):
|
class String(State):
|
||||||
magic = { "n": "\n", "t": "\t", "\\": "\\", "\"": "\"" }
|
magic = {"n": "\n", "t": "\t", "\\": "\\", "\"": "\""}
|
||||||
|
|
||||||
def enter(self):
|
def enter(self):
|
||||||
self.buf = ""
|
self.buf = ""
|
||||||
|
67
hy/utils.py
67
hy/utils.py
@ -1,67 +0,0 @@
|
|||||||
import os
|
|
||||||
import imp
|
|
||||||
import marshall
|
|
||||||
import struct
|
|
||||||
import time
|
|
||||||
|
|
||||||
|
|
||||||
MAGIC = imp.get_magic()
|
|
||||||
|
|
||||||
def _write_long(fp, int_):
|
|
||||||
"""Internal; write a 32-bit int to a file in little-endian order."""
|
|
||||||
fp.write(chr( int_ & 0xff))
|
|
||||||
fp.write(chr((int_ >> 8) & 0xff))
|
|
||||||
fp.write(chr((int_ >> 16) & 0xff))
|
|
||||||
fp.write(chr((int_ >> 24) & 0xff))
|
|
||||||
|
|
||||||
|
|
||||||
def get_mtime(fp):
|
|
||||||
'''Get the last modified date from the 4-byte timestamp in the pyc file.
|
|
||||||
'''
|
|
||||||
with open(filename, 'rb') as f:
|
|
||||||
f.seed(4)
|
|
||||||
moddate = f.read(4)
|
|
||||||
modtime = time.asctime(time.localtime(struct.unpack('L', moddate)[0]))
|
|
||||||
return modtime
|
|
||||||
|
|
||||||
|
|
||||||
def write_pyc(filename, codeobject, cfile=None, dfile='source'):
|
|
||||||
"""Byte-compile one Python source file to Python bytecode.
|
|
||||||
|
|
||||||
Arguments:
|
|
||||||
filename: filename associated with the bytecode (i.e., foo.py)
|
|
||||||
|
|
||||||
cfile: target filename; defaults to source with 'c' or 'o' appended
|
|
||||||
('c' normally, 'o' in optimizing mode, giving .pyc or .pyo)
|
|
||||||
dfile: purported filename; defaults to source (this is the filename
|
|
||||||
that will show up in error messages)
|
|
||||||
See http://hg.python.org/cpython/file/2.7/Lib/py_compile.py
|
|
||||||
"""
|
|
||||||
# 'U' opens the file in universal line-ending mode.
|
|
||||||
with open(filename, 'U') as f:
|
|
||||||
try:
|
|
||||||
timestamp = long(os.fstat(f.fileno()).st_mtime)
|
|
||||||
except AttributeError:
|
|
||||||
timestamp = long(os.stat(filename).st_mtime)
|
|
||||||
codestring = f.read()
|
|
||||||
|
|
||||||
# Add on the .pyc (or .pyo) filename extension.
|
|
||||||
if cfile is None:
|
|
||||||
cfile = filename + (__debug__ and 'c' or 'o')
|
|
||||||
|
|
||||||
# Write out the compiled code.
|
|
||||||
with open(cfile, 'wb') as f:
|
|
||||||
|
|
||||||
# Write a placeholder for the magic number.
|
|
||||||
f.write('\0\0\0\0')
|
|
||||||
|
|
||||||
# Write the timestamp.
|
|
||||||
_write_long(f, timestamp)
|
|
||||||
|
|
||||||
# Dump the bytecode.
|
|
||||||
marshal.dump(codeobject, f)
|
|
||||||
|
|
||||||
# Write the magic number of the placeholder.
|
|
||||||
f.flush()
|
|
||||||
f.seek(0, 0)
|
|
||||||
f.write(MAGIC)
|
|
5
tests/lang/colls.hy
Normal file
5
tests/lang/colls.hy
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
(defn test-map-index []
|
||||||
|
({"foo" "bar"} "foo"))
|
||||||
|
|
||||||
|
(defn test-list-index []
|
||||||
|
(["first" "second"] 1))
|
7
tests/lang/test_types_as_fn.py
Normal file
7
tests/lang/test_types_as_fn.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import tests.lang.colls
|
||||||
|
|
||||||
|
def test_map_index():
|
||||||
|
assert tests.lang.colls.test_map_index() == "bar"
|
||||||
|
|
||||||
|
def test_list_index():
|
||||||
|
assert tests.lang.colls.test_list_index() == "second"
|
Loading…
x
Reference in New Issue
Block a user