def _generate_declaration(self, var: Variable): """ :return: Create declaration part E.g. array[1..n] -> std::vector<int> array = std::vector<int>(n-1+1); """ if var.dim_num() == 0: dims = [] elif var.dim_num() == 1: dims = [var.first_index.get_length()] elif var.dim_num() == 2: dims = [var.first_index.get_length(), var.second_index.get_length()] else: raise NotImplementedError if len(dims) == 0: ctor = '' elif len(dims) == 1: ctor = '({})'.format(dims[0]) else: ctor = '({})'.format(dims[-1]) ctype = self._convert_type(var.type) for dim in dims[-2::-1]: ctype = 'std::vector<{}>'.format(ctype) ctor = '({}, {}{})'.format(dim, ctype, ctor) line = "{decl_type} {name}{constructor};".format( name=var.name, decl_type=self._get_declaration_type(var), constructor=ctor ) return line
def _generate_declaration_and_allocation(self, var: Variable): """ :return: Create declaration part E.g. array[1..n] -> std::vector<int> array = std::vector<int>(n-1+1); """ if var.dim_num() == 0: if var.type == Type.int: return self.info.declare_int.format(name=var.name) elif var.type == Type.float: return self.info.declare_float.format(name=var.name) elif var.type == Type.str: return self.info.declare_string.format(name=var.name) else: raise NotImplementedError elif var.dim_num() == 1: return self.info.declare_and_allocate_seq.format( name=var.name, type=self._convert_type(var.type), length=self._get_length(var.first_index), default=self._default_val(var.type)) elif var.dim_num() == 2: return self.info.declare_and_allocate_2d_seq.format( name=var.name, type=self._convert_type(var.type), length_i=self._get_length(var.first_index), length_j=self._get_length(var.second_index), default=self._default_val(var.type))
def _generate_declaration(self, var: Variable): """ :return: Create declaration part E.g. array[1..n] -> array = [int()] * (n-1+1) # type: List[int] """ if var.dim_num() == 0: dims = [] elif var.dim_num() == 1: dims = [var.first_index.get_length()] elif var.dim_num() == 2: dims = [var.first_index.get_length(), var.second_index.get_length()] else: raise NotImplementedError ctype = self._convert_type(var.type) if len(dims) == 0: ctor = "{}()".format(ctype) elif len(dims) == 1: ctor = "[{ctype}()] * ({dim})".format(ctype=ctype, dim=dims[0]) else: ctor = "[{ctype}()] * ({dim})".format(ctype=ctype, dim=dims[0]) for dim in dims[-2::-1]: ctor = "[{ctor} for _ in range({dim})]".format( ctor=ctor, dim=dim) line = "{name} = {constructor} # type: {decl_type} ".format( name=var.name, decl_type=self._get_declaration_type(var), constructor=ctor ) return line
def _get_declaration_type(self, var: Variable): ctype = self._convert_type(var.type) for _ in range(var.dim_num()): ctype = "List[{}]".format(ctype) if var.dim_num(): ctype = '"{}"'.format(ctype) return ctype
def _generate_declaration(self, var: Variable): """ :return: Create declaration part E.g. array[1..n] -> std::vector<int> array = std::vector<int>(n-1+1); """ if var.dim_num() == 0: dims = [] elif var.dim_num() == 1: dims = [var.first_index.get_length()] elif var.dim_num() == 2: dims = [ var.first_index.get_length(), var.second_index.get_length() ] else: raise NotImplementedError if len(dims) == 0: ctor = '' elif len(dims) == 1: ctor = '({})'.format(dims[0]) else: ctor = '({})'.format(dims[-1]) ctype = self._convert_type(var.type) for dim in dims[-2::-1]: ctype = 'std::vector<{}>'.format(ctype) ctor = '({}, {}{})'.format(dim, ctype, ctor) line = "{decl_type} {name}{constructor};".format( name=var.name, decl_type=self._get_declaration_type(var), constructor=ctor) return line
def _get_var_name(var: Variable): name = var.name if var.dim_num() >= 1: name += "[i]" if var.dim_num() >= 2: name += "[j]" return name
def _generate_declaration(self, var: Variable): """ :return: Create declaration part E.g. array[1..n] -> array = [int()] * (n-1+1) # type: List[int] """ if var.dim_num() == 0: dims = [] elif var.dim_num() == 1: dims = [var.first_index.get_length()] elif var.dim_num() == 2: dims = [ var.first_index.get_length(), var.second_index.get_length() ] else: raise NotImplementedError ctype = self._convert_type(var.type) if len(dims) == 0: ctor = "{}()".format(ctype) elif len(dims) == 1: ctor = "[{ctype}()] * ({dim})".format(ctype=ctype, dim=dims[0]) else: ctor = "[{ctype}()] * ({dim})".format(ctype=ctype, dim=dims[0]) for dim in dims[-2::-1]: ctor = "[{ctor} for _ in range({dim})]".format(ctor=ctor, dim=dim) line = "{name} = {constructor} # type: {decl_type} ".format( name=var.name, decl_type=self._get_declaration_type(var), constructor=ctor) return line
def _generate_declaration(self, var: Variable): """ :return: Create declaration part E.g. array[1..n] -> std::vector<int> array = std::vector<int>(n-1+1); """ if var.dim_num() == 0: dims = [] elif var.dim_num() == 1: dims = [var.first_index.get_length()] elif var.dim_num() == 2: dims = [ var.first_index.get_length(), var.second_index.get_length() ] else: raise NotImplementedError t = self._convert_type(var.type) if len(dims) == 0: ret = "" else: d = [] for dim in dims: d.append(str(dim)) if len(dims) == 1: jtype = "Vector" elif len(dims) == 2: jtype = "Matrix" else: jtype = "Array" ret = "{name} = similar({jtype}{{{type}}}, {dims})".format( type=t, name=var.name, jtype=jtype, dims=", ".join(d)) return ret
def _generate_declaration(self, var: Variable): """ :return: Create declaration part e.g. array[1..n] -> int[] array = new int[](n-1+1); """ if var.dim_num() == 0: dims = [] elif var.dim_num() == 1: dims = [var.first_index.get_length()] elif var.dim_num() == 2: dims = [var.first_index.get_length(), var.second_index.get_length()] else: raise NotImplementedError decl_type = self._get_declaration_type(var) line = "{decl_type} {name}".format( name=var.name, decl_type=decl_type) if len(dims) > 0: ctor_args = map(lambda d: "cast(size_t) ({})".format(d), dims) line += ' = new {}({})'.format(decl_type, ", ".join(ctor_args)) line += ";" return line
def _generate_declaration(self, var: Variable): if var.dim_num() == 0: constructor = "" elif var.dim_num() == 1: if var.type == Type.str: constructor = " = vec![String::new(); ({size}) as usize]".format( type=self._convert_type(var.type), size=var.first_index.get_length()) else: constructor = " = vec![0{type}; ({size}) as usize]".format( type=self._convert_type(var.type), size=var.first_index.get_length()) elif var.dim_num() == 2: if var.type == Type.str: constructor = " = vec![vec![String::new(); ({col_size}) as usize]; ({row_size}) as usize]".format( type=self._convert_type(var.type), row_size=var.first_index.get_length(), col_size=var.second_index.get_length()) else: constructor = " = vec![vec![0{type}; ({col_size}) as usize]; ({row_size}) as usize]".format( type=self._convert_type(var.type), row_size=var.first_index.get_length(), col_size=var.second_index.get_length()) else: raise NotImplementedError line = "let mut {name}: {decl_type}{constructor};".format( name=var.name, decl_type=self._get_declaration_type(var), constructor=constructor) return line
def _generate_declaration(self, var: Variable): """ :return: Create declaration part E.g. array[1..n] -> std::vector<int> array = std::vector<int>(n-1+1); """ if var.dim_num() == 0: dims = [] elif var.dim_num() == 1: dims = [var.first_index.get_length()] elif var.dim_num() == 2: dims = [ var.first_index.get_length(), var.second_index.get_length() ] else: raise NotImplementedError ret = "{decl_type} {name}".format( decl_type=self._get_declaration_type(var), name=var.name) if len(dims) > 0: t = self._convert_type(var.type) d = [] for dim in dims: d.append(str(dim)) ret += " = new {type}[{dims}]".format(type=t, dims=",".join(d)) ret += ";" return ret
def _get_declaration_type(self, var: Variable): type = self._convert_type(var.type) if var.dim_num() == 0: return type elif var.dim_num() == 1: return "Vector{{{}}}".format(type) elif var.dim_num() == 2: return "Matrix{{{}}}".format(type) else: return "Array{{{t}, {nd}}}".format(t=type, nd=var.dim_num())
def _get_declaration_type(self, var: Variable): if var.dim_num() == 0: template = "{type}" elif var.dim_num() == 1: template = "Vec<{type}>" elif var.dim_num() == 2: template = "Vec<Vec<{type}>>" else: raise NotImplementedError return template.format(type=self._convert_type(var.type))
def _get_var_name(self, var: Variable): name = var.name if var.dim_num() == 0: return name elif var.dim_num() == 1: return self.info.access_1d.format(name=name) elif var.dim_num() == 2: return self.info.access_2d.format(name=name) else: raise NotImplementedError
def _generate_declaration(self, var: Variable): """ :return: Create declaration part as string[] E.g. array[1..n] -> ["array := make([]int, n)"] array[1..n][1..m] -> [ "array := make([][]int, n)", "for i := 0; i < n; i++ {", " array[i] = make([]int, m)", "}", ] """ if var.dim_num() == 0: dims = [] elif var.dim_num() == 1: dims = [var.first_index.get_length()] elif var.dim_num() == 2: dims = [ var.first_index.get_length(), var.second_index.get_length() ] else: raise NotImplementedError lines = [] if len(dims) == 0: lines.append("var {name} {decl_type}".format( name=var.name, decl_type=self._get_declaration_type(var))) else: lines.append("{name} := make({decl_type}, {dim})".format( name=var.name, decl_type=self._get_declaration_type(var), dim=dims[0])) indexes = "" loop_vars = list("ijk") ctype = self._convert_type(var.type) for i, dim in enumerate(dims[1::1]): loop_var = loop_vars[i] lines.append(_make_loop_header(loop_var, dims[i])) indexes += "[{}]".format(loop_var) brackets = "[]" * (len(dims) - i - 1) lines.append( "{indent}{name}{indexes} = make({brackets}{ctype}, {dim})". format(indent=self._indent(i + 1), name=var.name, indexes=indexes, brackets=brackets, ctype=ctype, dim=dim)) for i in reversed(range(len(dims) - 1)): lines.append("{indent}}}".format(indent=self._indent(i))) return lines
def _generate_declaration(self, var: Variable): """ :return: Create declaration part E.g. array[1..n] -> std::vector<int> array = std::vector<int>(n-1+1); """ if var.dim_num() == 0: dims = [] elif var.dim_num() == 1: dims = [var.first_index.get_length()] elif var.dim_num() == 2: dims = [var.first_index.get_length(), var.second_index.get_length()] else: raise NotImplementedError e = self._default_val(var.type) for dim in dims[::-1]: e = "newSeqWith({}, {})".format(dim, e) return "var {name} = {expression}".format(name=var.name, expression=e)
def _get_argument(self, var: Variable): if var.dim_num() == 0: if var.type == Type.float: return self.info.arg_float.format(name=var.name) elif var.type == Type.int: return self.info.arg_int.format(name=var.name) elif var.type == Type.str: return self.info.arg_string.format(name=var.name) else: raise NotImplementedError elif var.dim_num() == 1: return self.info.arg_seq.format(name=var.name, type=self._convert_type(var.type)) elif var.dim_num() == 2: return self.info.arg_2d_seq.format(name=var.name, type=self._convert_type( var.type))
def _generate_declaration(self, var: Variable): if var.dim_num() == 0: constructor = "" elif var.dim_num() == 1: constructor = " = new {type}[(int)({size})]".format( type=self._convert_type(var.type), size=var.first_index.get_length()) elif var.dim_num() == 2: constructor = " = new {type}[int({row_size})][int({col_size})]".format( type=self._convert_type(var.type), row_size=var.first_index.get_length(), col_size=var.second_index.get_length()) else: raise NotImplementedError line = "{decl_type} {name}{constructor};".format( name=var.name, decl_type=self._get_declaration_type(var), constructor=constructor) return line
def _generate_declaration(self, var: Variable): if var.dim_num() == 0: constructor = "" elif var.dim_num() == 1: constructor = " = new {type}[(int)({size})]".format( type=self._convert_type(var.type), size=var.first_index.get_length() ) elif var.dim_num() == 2: constructor = " = new {type}[(int)({row_size})][(int)({col_size})]".format( type=self._convert_type(var.type), row_size=var.first_index.get_length(), col_size=var.second_index.get_length() ) else: raise NotImplementedError line = "{decl_type} {name}{constructor};".format( name=var.name, decl_type=self._get_declaration_type(var), constructor=constructor ) return line
def _generate_declaration(self, var: Variable): if var.dim_num() == 0: constructor = "" elif var.dim_num() == 1: if var.type == Type.str: constructor = " = vec![String::new(); ({size}) as usize]".format( type=self._convert_type(var.type), size=var.first_index.get_length() ) else: constructor = " = vec![0{type}; ({size}) as usize]".format( type=self._convert_type(var.type), size=var.first_index.get_length() ) elif var.dim_num() == 2: if var.type == Type.str: constructor = " = vec![vec![String::new(); ({col_size}) as usize]; ({row_size}) as usize]".format( type=self._convert_type(var.type), row_size=var.first_index.get_length(), col_size=var.second_index.get_length() ) else: constructor = " = vec![vec![0{type}; ({col_size}) as usize]; ({row_size}) as usize]".format( type=self._convert_type(var.type), row_size=var.first_index.get_length(), col_size=var.second_index.get_length() ) else: raise NotImplementedError line = "let mut {name}: {decl_type}{constructor};".format( name=var.name, decl_type=self._get_declaration_type(var), constructor=constructor ) return line
def create_typed_format(cls, simple_format: Format[SimpleVariable], var_to_type: Dict[str, Type]): var_to_info = {} for var in simple_format.all_vars(): assert var.name not in var_to_info var_to_info[var.name] = Variable(var.name, var.first_index, var.second_index, var_to_type[var.name]) fmt = Format() # type: Format[Variable] for pattern in simple_format.sequence: fmt.push_back(pattern.with_replaced_vars(var_to_info)) return FormatPredictionResult(fmt)
def _get_declaration_type(cls, var: Variable): ctype = cls._get_var_basetype(var) for _ in range(var.dim_num()): ctype += "[]" return ctype
def _get_declaration_type(self, var: Variable): ctype = self._convert_type(var.type) for _ in range(var.dim_num()): ctype = 'std::vector<{}>'.format(ctype) return ctype
def _get_declaration_type(self, var: Variable): ctype = self._convert_type(var.type) if var.dim_num() == 0: return ctype else: return "{}[{}]".format(ctype, "," * (var.dim_num() - 1))
def _get_declaration_type(self, var: Variable): ctype = self._convert_type(var.type) for _ in range(var.dim_num()): ctype = '[]{}'.format(ctype) return ctype