adding in decorators
This commit is contained in:
parent
3dba5f7aff
commit
b1c3a758f9
@ -169,6 +169,15 @@ class HyASTCompiler(object):
|
|||||||
slice=ast.Index(value=sli),
|
slice=ast.Index(value=sli),
|
||||||
ctx=ast.Load())
|
ctx=ast.Load())
|
||||||
|
|
||||||
|
@builds("decorate_with")
|
||||||
|
def compile_decorate_expression(self, expr):
|
||||||
|
expr.pop(0) # decorate-with
|
||||||
|
fn = self.compile(expr.pop(-1))
|
||||||
|
if type(fn) != ast.FunctionDef:
|
||||||
|
raise TypeError("Decorated a non-function")
|
||||||
|
fn.decorator_list = [self.compile(x) for x in expr]
|
||||||
|
return fn
|
||||||
|
|
||||||
@builds("=")
|
@builds("=")
|
||||||
@builds("!=")
|
@builds("!=")
|
||||||
@builds("<")
|
@builds("<")
|
||||||
@ -180,16 +189,11 @@ class HyASTCompiler(object):
|
|||||||
@builds("is_not")
|
@builds("is_not")
|
||||||
@builds("not_in")
|
@builds("not_in")
|
||||||
def compile_compare_op_expression(self, expression):
|
def compile_compare_op_expression(self, expression):
|
||||||
ops = {"=": ast.Eq,
|
ops = {"=": ast.Eq, "!=": ast.NotEq,
|
||||||
"!=": ast.NotEq,
|
"<": ast.Lt, "<=": ast.LtE,
|
||||||
"<": ast.Lt,
|
">": ast.Gt, ">=": ast.GtE,
|
||||||
"<=": ast.LtE,
|
"is": ast.Is, "is_not": ast.IsNot,
|
||||||
">": ast.Gt,
|
"in": ast.In, "not_in": ast.NotIn}
|
||||||
">=": ast.GtE,
|
|
||||||
"is": ast.Is,
|
|
||||||
"is_not": ast.IsNot,
|
|
||||||
"in": ast.In,
|
|
||||||
"not_in": ast.NotIn}
|
|
||||||
|
|
||||||
inv = expression.pop(0)
|
inv = expression.pop(0)
|
||||||
op = ops[inv]
|
op = ops[inv]
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
import hy
|
import hy
|
||||||
from .native_tests.math import *
|
from .native_tests.math import *
|
||||||
from .native_tests.language import *
|
from .native_tests.language import *
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
(import sys)
|
(import sys)
|
||||||
(import-from os.path exists isdir isfile)
|
(import-from os.path exists isdir isfile)
|
||||||
|
|
||||||
@ -8,6 +9,7 @@
|
|||||||
"NATIVE: test sys.argv"
|
"NATIVE: test sys.argv"
|
||||||
(assert (isinstance sys.argv list)))
|
(assert (isinstance sys.argv list)))
|
||||||
|
|
||||||
|
|
||||||
(defn test-lists []
|
(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]))))
|
||||||
@ -88,3 +90,16 @@
|
|||||||
(assert (is (exists ".") true))
|
(assert (is (exists ".") true))
|
||||||
(assert (is (isdir ".") true))
|
(assert (is (isdir ".") true))
|
||||||
(assert (is (isfile ".") false)))
|
(assert (is (isfile ".") false)))
|
||||||
|
|
||||||
|
|
||||||
|
(defn foodec [func]
|
||||||
|
(lambda [] (+ 1 1)))
|
||||||
|
|
||||||
|
|
||||||
|
(decorate-with foodec
|
||||||
|
(defn tfunction []
|
||||||
|
(* 2 2)))
|
||||||
|
|
||||||
|
|
||||||
|
(defn test-decorators []
|
||||||
|
(assert (= (tfunction) 2)))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user