def _gen_sign(cls, params, ret): new_params = [] for stype, name, desc in params: stype = TypeParser.get_python_stype(stype) new_params.append((stype, name, desc)) ret_stype, _, ret_desc = ret ret_stype = TypeParser.get_python_stype(ret_stype) return super()._gen_sign(new_params, (ret_stype, _, ret_desc))
def _gen_args_expr(cls, pname, params, ret): def new_array(lv, last_list_name, arr_name, top_lv_index=None): def apfmt(fmt, **kwargs): exprs.append(TWO_SPCS * lv + fmt.format(**kwargs)) if lv == depth: return apfmt('{arr_name} = {last_list_name}.getItem({iname}).{cast_func}();', arr_name=arr_name, last_list_name=last_list_name, iname=top_lv_index if lv == 0 else 'i{}'.format(lv - 1), cast_func=cls.LIB_CAST[stype]) list_name = lname_fmt.format(lv) apfmt('JSONList {list_name} = (JSONList) {last_list_name}.getItem({iname});', list_name=list_name, last_list_name=last_list_name, iname=top_lv_index if lv == 0 else 'i{}'.format(lv - 1)) apfmt('{arr_name} = new {str_type}[{list_name}.getEntryCount()]{sub_arr};', arr_name=arr_name, str_type=str_type, list_name=list_name, sub_arr='[]' * (depth - lv - 1)) apfmt('for (int i{lv} = 0; i{lv} < {list_name}.getEntryCount(); i{lv}++) {{', lv=lv, list_name=list_name) new_array(lv + 1, list_name, '{}[i{}]'.format(arr_name, lv)) apfmt('}}') exprs = [ 'String input = new Scanner(System.in).nextLine();', 'JSONList argumentList = (JSONList) JSONParser.getObjectFromString(input);' ] for i, (str_type, name, _) in enumerate(params): stype, depth = TypeParser.parse_type(str_type) exprs.append('{} {};'.format(str_type, name)) str_type = str_type[:len(str_type) - depth * 2] # get pure type lname_fmt = name + 'List{}' new_array(0, 'argumentList', name, i) return '\n'.join(exprs)
def _gen_method(cls, pname, params, ret): ret_type, _, _ = ret stype, depth = TypeParser.parse_type(ret_type) return cls.METHOD.format( ret_type=ret_type, name=pname, params=', '.join('{0} {1}'.format(*p) for p in params), ret_val=indent(cls.RET_EXPR.format('null' if depth > 0 else cls.DEFAULT_VAL[stype])), content=indent('// TODO;\n'))
def _gen_print_expr(cls, pname, params, ret): print_expr = '{}({})'.format(pname, ', '.join(n for (_, n, _) in params)) cast_fmt, cast_func = '{}({})', 'String.valueOf' ret_type, _, _ = ret stype, depth = TypeParser.parse_type(ret_type) if depth > 1: cast_func = 'Arrays.deepToString' elif depth == 1: cast_func = 'Arrays.toString' elif stype == 'String': cast_fmt = '{}{}' cast_func = '' elif stype == 'boolean': cast_fmt = '{1} ? "True" : "False"' print_expr = cast_fmt.format(cast_func, print_expr) return 'System.out.println({});\n'.format(print_expr)
def _gen_method(cls, pname, params, ret): return cls.METHOD.format( name=pname, params=', '.join(n for (_, n, _) in params), ret=repr(TypeParser.get_type(ret[0])()))