Breaking things to start again.
This commit is contained in:
parent
dbd9f0bbf8
commit
67b803b99a
17
bin/hy2py
17
bin/hy2py
@ -1,13 +1,20 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
from hy.compiler import hy_compile
|
from hy.importer import (import_file_to_ast, import_file_to_module,
|
||||||
from hy.lex import tokenize
|
import_file_to_hst)
|
||||||
import codegen
|
import codegen
|
||||||
import sys
|
import sys
|
||||||
import ast
|
import ast
|
||||||
|
|
||||||
tokens = tokenize(open(sys.argv[1], 'r').read())
|
|
||||||
print tokens
|
hst = import_file_to_hst(sys.argv[1])
|
||||||
_ast = hy_compile(tokens)
|
print hst
|
||||||
|
print ""
|
||||||
|
print ""
|
||||||
|
_ast = import_file_to_ast(sys.argv[1])
|
||||||
|
print ""
|
||||||
|
print ""
|
||||||
print ast.dump(_ast)
|
print ast.dump(_ast)
|
||||||
|
print ""
|
||||||
|
print ""
|
||||||
print codegen.to_source(_ast)
|
print codegen.to_source(_ast)
|
||||||
|
@ -204,6 +204,8 @@ class HyASTCompiler(object):
|
|||||||
|
|
||||||
@builds("fn")
|
@builds("fn")
|
||||||
def compile_fn_expression(self, expression):
|
def compile_fn_expression(self, expression):
|
||||||
|
print expression, type(expression), expression.start_line
|
||||||
|
|
||||||
expression.pop(0) # fn
|
expression.pop(0) # fn
|
||||||
|
|
||||||
ret_status = self.returnable
|
ret_status = self.returnable
|
||||||
|
@ -20,8 +20,13 @@
|
|||||||
|
|
||||||
|
|
||||||
from hy.macros import macro
|
from hy.macros import macro
|
||||||
|
from hy.models.expression import HyExpression
|
||||||
|
from hy.models.symbol import HySymbol
|
||||||
|
|
||||||
|
|
||||||
@macro("noop")
|
@macro("defn")
|
||||||
def noop_macro(tree):
|
def defn_macro(tree):
|
||||||
return tree
|
# (defn foo [x] ...)
|
||||||
|
# (def foo (fn [x] ...))
|
||||||
|
return HyExpression([HySymbol("def"),
|
||||||
|
tree[1], HyExpression([HySymbol("fn")] + tree[2:])])
|
||||||
|
@ -1,15 +1,31 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
from hy.lex import tokenize
|
from hy.lex import tokenize
|
||||||
|
from hy.macros import process
|
||||||
from hy.compiler import hy_compile
|
from hy.compiler import hy_compile
|
||||||
|
|
||||||
|
import hy.core.bootstrap # language bits.
|
||||||
|
|
||||||
|
|
||||||
import imp
|
import imp
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def import_file_to_hst(fpath):
|
||||||
|
tree = tokenize(open(fpath, 'r').read())
|
||||||
|
tree = process(tree)
|
||||||
|
return tree
|
||||||
|
|
||||||
|
|
||||||
|
def import_file_to_ast(fpath):
|
||||||
|
tree = import_file_to_hst(fpath)
|
||||||
|
ast = hy_compile(tree)
|
||||||
|
return ast
|
||||||
|
|
||||||
|
|
||||||
def import_file_to_module(name, fpath):
|
def import_file_to_module(name, fpath):
|
||||||
ast = hy_compile(tokenize(open(fpath, 'r').read()))
|
ast = import_file_to_ast(fpath)
|
||||||
mod = imp.new_module(name)
|
mod = imp.new_module(name)
|
||||||
mod.__file__ = fpath
|
mod.__file__ = fpath
|
||||||
eval(compile(ast, fpath, "exec"), mod.__dict__)
|
eval(compile(ast, fpath, "exec"), mod.__dict__)
|
||||||
|
17
hy/macros.py
17
hy/macros.py
@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
from hy.models.expression import HyExpression
|
from hy.models.expression import HyExpression
|
||||||
from hy.models.list import HyList
|
from hy.models.list import HyList
|
||||||
|
from hy.models import HyObject
|
||||||
|
|
||||||
_hy_macros = {}
|
_hy_macros = {}
|
||||||
|
|
||||||
@ -31,20 +32,6 @@ def macro(name):
|
|||||||
return _
|
return _
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def process(tree):
|
def process(tree):
|
||||||
if isinstance(tree, HyExpression):
|
|
||||||
it = iter(tree)
|
|
||||||
fn = next(it)
|
|
||||||
|
|
||||||
body = [process(x) for x in it]
|
|
||||||
|
|
||||||
if fn in _hy_macros:
|
|
||||||
m = _hy_macros[fn]
|
|
||||||
return m(HyExpression([fn] + body))
|
|
||||||
|
|
||||||
return [fn] + body
|
|
||||||
|
|
||||||
if isinstance(tree, HyList):
|
|
||||||
return HyList([process(x) for x in tree])
|
|
||||||
|
|
||||||
return tree
|
return tree
|
||||||
|
@ -25,3 +25,12 @@ class HyObject(object):
|
|||||||
Hy lexing Objects at once.
|
Hy lexing Objects at once.
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def replace(self, other):
|
||||||
|
if isinstance(other, HyObject):
|
||||||
|
self.start_line = other.start_line
|
||||||
|
self.end_line = other.end_line
|
||||||
|
self.start_column = other.start_column
|
||||||
|
self.end_column = other.end_column
|
||||||
|
else:
|
||||||
|
raise TypeError("Can't replace a non Hy object with a Hy object")
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
(def test_lists (fn []
|
(defn test_lists []
|
||||||
"NATIVE: test lists work right"
|
"NATIVE: test lists work right"
|
||||||
(assert (= [1 2 3 4] (+ [1 2] [3 4])))))
|
(assert (= [1 2 3 4] (+ [1 2] [3 4]))))
|
||||||
|
|
||||||
|
|
||||||
(def test_for_loop (fn []
|
(defn test_for_loop []
|
||||||
"NATIVE: test for loops?"
|
"NATIVE: test for loops?"
|
||||||
(def count 0)
|
(def count 0)
|
||||||
(for [x [1 2 3 4 5]]
|
(for [x [1 2 3 4 5]]
|
||||||
(def count (+ count x)))
|
(def count (+ count x)))
|
||||||
(assert (= count 15))))
|
(assert (= count 15)))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user