Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@takes(AST)
@returns(Clazz)
def parse_enum(node):
_kind = node.getText()
cls = Clazz(kind=_kind)
cls.name = node.getChild(0).getChild(0).getText()
_nodes = node.getChildren()[1:] # exclude name
constants = util.implode_id(util.mk_v_node_w_children(_nodes)).split(',')
for c in constants:
# define representative field
fld = field.Field(clazz=cls, mods=C.PBST, typ=cls.name, name=c)
cls.add_fld(fld)
# initialize it in
f = exp.gen_E_id(cls.name)
init_e = exp.gen_E_new(exp.gen_E_call(f, []))
init_s = st.gen_S_assign(exp.gen_E_id(c), init_e)
cls.clinit_fld(fld, [init_s])
return cls
@staticmethod
def call_init(tname, params=[]):
args = []
try:
cls = class_lookup(tname)
inits = cls.mtd_by_name(tname) # AttributeError if cls is NoneType
for init in inits:
_args = method.sig_match(init.params, params)
# try to find best matched one
if len(args) <= len(_args): args = _args
except (AttributeError, IndexError): pass
f = exp.gen_E_id(tname)
args = map(exp.to_expression, args)
return exp.gen_E_new(exp.gen_E_call(f, args)), args
@takes(AST)
@returns(Clazz)
def parse_enum(node):
_kind = node.getText()
cls = Clazz(kind=_kind)
cls.name = node.getChild(0).getChild(0).getText()
_nodes = node.getChildren()[1:] # exclude name
constants = util.implode_id(util.mk_v_node_w_children(_nodes)).split(',')
for c in constants:
# define representative field
fld = field.Field(clazz=cls, mods=C.PBST, typ=cls.name, name=c)
cls.add_fld(fld)
# initialize it in
f = exp.gen_E_id(cls.name)
init_e = exp.gen_E_new(exp.gen_E_call(f, []))
init_s = st.gen_S_assign(exp.gen_E_id(c), init_e)
cls.clinit_fld(fld, [init_s])
return cls
@takes("Clazz", "Field", list_of("Statement"))
@returns(nothing)
def init_fld(self, fld, init_ss=[]):
# declare if not exists
inits = self.mtd_by_name(self._name)
if not inits: inits = [self.add_default_init()]
# make initializing statements
if not init_ss and not fld.is_final:
call = Clazz.call_init_if_instantiable(fld.typ)
if call: init_ss = [st.gen_S_assign(exp.gen_E_id(fld.name), call)]
# update the method body
if init_ss:
for init in inits: init.body.extend(init_ss[:])
cs = map(curried_s, rm_braces(__node.getChildren()[1:]))
catches.append( (e, cs) )
elif __kind == '}': break
idx = idx - 1
b = map(curried_s, rm_braces(_nodes[1:idx+1]))
s = gen_S_try(b, catches, fs)
# (S... typ var (= (E... )) ';')
elif _nodes[-2].getText() == '=':
var = _nodes[-3].getText()
# type can be a list of nodes, e.g., List < T >
ty_node = util.mk_v_node_w_children(_nodes[:-3])
ty = util.implode_id(ty_node)
mtd.locals[var] = ty
le = gen_E_id(var, ty)
re = curried_e(_nodes[-2].getChild(0))
s = gen_S_assign(le, re)
# (DECL typ var ';') # local variable declaration
elif node.getText() == C.T.DECL:
# type can be a list of nodes, e.g., Class < ? extends Activity >
ty_node = util.mk_v_node_w_children(_nodes[:-2])
ty = util.implode_id(ty_node)
var = _nodes[-2].getText()
mtd.locals[var] = ty
e_decl = gen_E_id(var, ty)
s = gen_S_e(e_decl)
else: raise Exception("unhandled statement", node.toStringTree())
return s
# type can be a list of nodes, e.g., List < T >
ty_node = util.mk_v_node_w_children(_nodes[:-3])
ty = util.implode_id(ty_node)
mtd.locals[var] = ty
le = gen_E_id(var, ty)
re = curried_e(_nodes[-2].getChild(0))
s = gen_S_assign(le, re)
# (DECL typ var ';') # local variable declaration
elif node.getText() == C.T.DECL:
# type can be a list of nodes, e.g., Class < ? extends Activity >
ty_node = util.mk_v_node_w_children(_nodes[:-2])
ty = util.implode_id(ty_node)
var = _nodes[-2].getText()
mtd.locals[var] = ty
e_decl = gen_E_id(var, ty)
s = gen_S_e(e_decl)
else: raise Exception("unhandled statement", node.toStringTree())
return s