Commit 0869f9d4 authored by Robert Bradshaw's avatar Robert Bradshaw

more flexable declare

parent 80d9a6ab
...@@ -2549,6 +2549,22 @@ class ExprStatNode(StatNode): ...@@ -2549,6 +2549,22 @@ class ExprStatNode(StatNode):
child_attrs = ["expr"] child_attrs = ["expr"]
def analyse_declarations(self, env):
import ExprNodes
if isinstance(self.expr, ExprNodes.GeneralCallNode):
func = self.expr.function.as_cython_attribute()
if func == u'declare':
args, kwds = self.expr.explicit_args_kwds()
if len(args):
error(self.expr.pos, "Variable names must be specified.")
for var, type_node in kwds.key_value_pairs:
type = type_node.analyse_as_type(env)
if type is None:
error(type_node.pos, "Unknown type")
else:
env.declare_var(var.value, type, var.pos, is_cdef = True)
self.__class__ = PassStatNode
def analyse_expressions(self, env): def analyse_expressions(self, env):
self.expr.analyse_expressions(env) self.expr.analyse_expressions(env)
self.expr.release_temp(env) self.expr.release_temp(env)
...@@ -2609,8 +2625,7 @@ class SingleAssignmentNode(AssignmentNode): ...@@ -2609,8 +2625,7 @@ class SingleAssignmentNode(AssignmentNode):
args, kwds = self.rhs.explicit_args_kwds() args, kwds = self.rhs.explicit_args_kwds()
if func_name in ['declare', 'typedef']: if func_name in ['declare', 'typedef']:
self.declaration_only = True if len(args) > 2 or kwds is not None:
if len(args) != 1 or kwds is not None:
error(rhs.pos, "Can only declare one type at a time.") error(rhs.pos, "Can only declare one type at a time.")
return return
type = args[0].analyse_as_type(env) type = args[0].analyse_as_type(env)
...@@ -2628,7 +2643,13 @@ class SingleAssignmentNode(AssignmentNode): ...@@ -2628,7 +2643,13 @@ class SingleAssignmentNode(AssignmentNode):
return return
for var, pos in vars: for var, pos in vars:
env.declare_var(var, type, pos, is_cdef = True) env.declare_var(var, type, pos, is_cdef = True)
if len(args) == 2:
# we have a value
self.rhs = args[1]
else:
self.declaration_only = True
else: else:
self.declaration_only = True
if not isinstance(lhs, ExprNodes.NameNode): if not isinstance(lhs, ExprNodes.NameNode):
error(lhs.pos, "Invalid declaration.") error(lhs.pos, "Invalid declaration.")
env.declare_typedef(lhs.name, type, self.pos, 'private') env.declare_typedef(lhs.name, type, self.pos, 'private')
......
...@@ -719,12 +719,12 @@ class TransformBuiltinMethods(EnvTransform): ...@@ -719,12 +719,12 @@ class TransformBuiltinMethods(EnvTransform):
if attribute == u'compiled': if attribute == u'compiled':
node = BoolNode(node.pos, value=True) node = BoolNode(node.pos, value=True)
else: else:
error(node.pos, u"'%s' not a valid cython attribute" % attribute) error(node.pos, u"'%s' not a valid cython attribute or is being used incorrectly" % attribute)
return node return node
def visit_SimpleCallNode(self, node): def visit_SimpleCallNode(self, node):
# locals # locals builtin
if isinstance(node.function, ExprNodes.NameNode): if isinstance(node.function, ExprNodes.NameNode):
if node.function.name == 'locals': if node.function.name == 'locals':
pos = node.pos pos = node.pos
......
...@@ -21,11 +21,14 @@ def sizeof(arg): ...@@ -21,11 +21,14 @@ def sizeof(arg):
def address(arg): def address(arg):
return pointer(type(arg))([arg]) return pointer(type(arg))([arg])
def declare(type): def declare(type=None, value=None, **kwds):
if callable(type): if type and callable(type):
return type() if value:
return type(value)
else:
return type()
else: else:
return None return value
# Emulated types # Emulated types
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment