Breaking things to start again.

This commit is contained in:
Paul Tagliamonte 2013-03-08 18:18:43 -05:00
parent dbd9f0bbf8
commit 67b803b99a
7 changed files with 54 additions and 28 deletions

View File

@ -1,13 +1,20 @@
#!/usr/bin/env python
from hy.compiler import hy_compile
from hy.lex import tokenize
from hy.importer import (import_file_to_ast, import_file_to_module,
import_file_to_hst)
import codegen
import sys
import ast
tokens = tokenize(open(sys.argv[1], 'r').read())
print tokens
_ast = hy_compile(tokens)
hst = import_file_to_hst(sys.argv[1])
print hst
print ""
print ""
_ast = import_file_to_ast(sys.argv[1])
print ""
print ""
print ast.dump(_ast)
print ""
print ""
print codegen.to_source(_ast)

View File

@ -204,6 +204,8 @@ class HyASTCompiler(object):
@builds("fn")
def compile_fn_expression(self, expression):
print expression, type(expression), expression.start_line
expression.pop(0) # fn
ret_status = self.returnable

View File

@ -20,8 +20,13 @@
from hy.macros import macro
from hy.models.expression import HyExpression
from hy.models.symbol import HySymbol
@macro("noop")
def noop_macro(tree):
return tree
@macro("defn")
def defn_macro(tree):
# (defn foo [x] ...)
# (def foo (fn [x] ...))
return HyExpression([HySymbol("def"),
tree[1], HyExpression([HySymbol("fn")] + tree[2:])])

View File

@ -1,15 +1,31 @@
#
from hy.lex import tokenize
from hy.macros import process
from hy.compiler import hy_compile
import hy.core.bootstrap # language bits.
import imp
import sys
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):
ast = hy_compile(tokenize(open(fpath, 'r').read()))
ast = import_file_to_ast(fpath)
mod = imp.new_module(name)
mod.__file__ = fpath
eval(compile(ast, fpath, "exec"), mod.__dict__)

View File

@ -20,6 +20,7 @@
from hy.models.expression import HyExpression
from hy.models.list import HyList
from hy.models import HyObject
_hy_macros = {}
@ -31,20 +32,6 @@ def macro(name):
return _
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

View File

@ -25,3 +25,12 @@ class HyObject(object):
Hy lexing Objects at once.
"""
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")

View File

@ -1,14 +1,14 @@
;
(def test_lists (fn []
(defn test_lists []
"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?"
(def count 0)
(for [x [1 2 3 4 5]]
(def count (+ count x)))
(assert (= count 15))))
(assert (= count 15)))