def as_expression(self): e = self.temp_name('') e_singular = self.singular_name() e_index = self.index() if self.field == 'keys': field_type = self.args[0].pseudo_type[1] first = local(e_singular, field_type) second = local('_', self.args[0].pseudo_type[2]) else: field_type = self.args[0].pseudo_type[2] first = local('_', self.args[0].pseudo_type[1]) second = local(e_singular, field_type) e = local(e, field_type) e_singular = local(e_singular, field_type) return [assignment(e, Node('_go_make_slice', slice_type=['List', field_type], length=call('len', [self.args[0]], 'Int'), pseudo_type=['List', field_type])), assignment(e_index, to_node(0)), Node('for_statement', sequences=Node('for_sequence_with_items', sequence=self.args[0]), iterators=Node('for_iterator_with_items', key=first, value=second), block=[ assignment( Node('index', sequence=e, index=e_index, pseudo_type=field_type), e_singular), Node('aug_assignment', op='+', target=e_index, value=to_node(1))]), e], None def as_assignment(self, target): e = self.as_expression()[0] e[3] = assignment(target, e) return e
def normalize(f, from_, to): if to.type == 'int': if to.value < 0: if from_.type != 'int': return Node('binary_op', op='-', left=Node('binary_op', op='-', left=attr(f, 'Length', 'Int'), right=to, pseudo_type='Int'), right=from_, pseudo_type='Int') else: return Node('binary_op', op='-', left=attr(f, 'Length', 'Int'), right=to_node(-to.value + from_.value), pseudo_type='Int') else: if from_.type != 'int': return Node('binary_op', op='-', left=to, right=from_, pseudo_type='Int') else: return to_node(to.value - from_.value) else: if from_.type == 'int' and from_.value == 0: return to else: return Node('binary_op', op='-', left=to, right=from_, pseudo_type='Int')
def expand_slice(receiver, from_, to, pseudo_type=None): return method_call( method_call( receiver, 'Take', [Node('binary_op', op='-', left=attr(receiver, 'Length', 'Int'), right=to_node(-to.value), pseudo_type='Int') if to.type == 'int' and to.value < 0 else to], pseudo_type=pseudo_type), 'Drop', [Node('binary_op', op='-', left=attr(receiver, 'Length', 'Int'), right=to_node(-from_.value), pseudo_type='Int') if from_.type == 'int' and from_.value < 0 else from_], pseudo_type=pseudo_type)
def as_expression(self, target=None): t = self.temp_name(target or self.default) element = '_%sElement' % extract_name(self.args[0], t) j = '_%sIndex' % extract_name(self.args[0], t) element = local(element, self.args[0].pseudo_type[1]) j = local(j, 'Int') if target is None: target = local(t, 'Int') return [ assignment(target, to_node(-1)), Node('for_statement', sequences=Node('for_sequence_with_index', sequence=self.args[0]), iterators=Node('for_iterator_with_index', index=j, iterator=element), block=[ Node('if_statement', test=Node('comparison', op='==', pseudo_type='Boolean', left=element, right=self.args[1]), block=[assignment(target, j), Node('break_')], otherwise=None) ]), target ], None
def as_expression(self): return [], Node( '_py_with', call=call('open', [self.args[0], to_node('w')], 'File'), context='_f', block=[method_call(local('_f', 'File'), 'write', [self.args[1]], 'Void')], pseudo_type='Void')
def as_expression(self): return [Node( '_py_with', call=call('open', [self.args[0], to_node('r')], 'File'), context='_f', block=[method_call(local('_f', 'File'), 'read', [], 'String')], pseudo_type='Void')], None
def as_expression(self, target=None): t = self.temp_name(target or self.default) element = '_%sElement' % extract_name(self.args[0], t) j = '_%sIndex' % extract_name(self.args[0], t) element = local(element, self.args[0].pseudo_type[1]) j = local(j, 'Int') if target is None: target = local(t, 'Int') return [assignment(target, to_node(-1)), Node('for_statement', sequences=Node('for_sequence_with_index', sequence=self.args[0]), iterators=Node('for_iterator_with_index', index=j, iterator=element), block=[ Node('if_statement', test=Node('comparison', op='==', pseudo_type='Boolean', left=element, right=self.args[1]), block=[assignment(target, j), Node('break_')], otherwise=None)]), target], None
def expand_slice(receiver, from_=None, to=None, pseudo_type=None): if from_: if pseudo_type: #to if from_.type == 'int' and from_.value == 0: return Node('_go_slice_to', sequence=receiver, to=to, pseudo_type=pseudo_type) else: if from_.type == 'int' and from_.value < 0: from_ = Node('binary_op', op='-', left=call('len', [receiver], 'Int'), right=to_node(-from_.value), pseudo_type='Int') if to.type == 'int' and to.value < 0: to = Node('binary_op', op='-', left=call('len', [receiver], 'Int'), right=to_node(-to.value), pseudo_type='Int') return Node('_go_slice', sequence=receiver, from_=from_, to=to, pseudo_type=pseudo_type) else: pseudo_type = to return Node('_go_slice_from', sequence=receiver, from_=from_, pseudo_type=pseudo_type) elif to: if to.type == 'int' and to.value < 0: to = Node('binary_op', op='-', left=call('len', [receiver], 'Int'), right=to_node(-to.value), pseudo_type='Int') return Node('_go_slice_to', sequence=receiver, to=to, pseudo_type=pseudo_type) else: return None
def as_expression(self): e = self.temp_name('') e_singular = self.singular_name() e_index = self.index() if self.field == 'keys': field_type = self.args[0].pseudo_type[1] first = local(e_singular, field_type) second = local('_', self.args[0].pseudo_type[2]) else: field_type = self.args[0].pseudo_type[2] first = local('_', self.args[0].pseudo_type[1]) second = local(e_singular, field_type) e = local(e, field_type) e_singular = local(e_singular, field_type) return [ assignment( e, Node('_go_make_slice', slice_type=['List', field_type], length=call('len', [self.args[0]], 'Int'), pseudo_type=['List', field_type])), assignment(e_index, to_node(0)), Node('for_statement', sequences=Node('for_sequence_with_items', sequence=self.args[0]), iterators=Node('for_iterator_with_items', key=first, value=second), block=[ assignment( Node('index', sequence=e, index=e_index, pseudo_type=field_type), e_singular), Node('aug_assignment', op='+', target=e_index, value=to_node(1)) ]), e ], None def as_assignment(self, target): e = self.as_expression()[0] e[3] = assignment(target, e) return e
def as_expression(self): return [ Node( '_py_with', call=call('open', [self.args[0], to_node('r')], 'File'), context='_f', block=[method_call(local('_f', 'File'), 'read', [], 'String')], pseudo_type='Void') ], None
def as_expression(self): return [], Node('_py_with', call=call('open', [self.args[0], to_node('w')], 'File'), context='_f', block=[ method_call(local('_f', 'File'), 'write', [self.args[1]], 'Void') ], pseudo_type='Void')
def expand_slice(receiver, from_=None, to=None, pseudo_type=None): if from_: if to: return Node('_rb_slice', sequence=receiver, from_=from_, to=to, pseudo_type=pseudo_type) else: return Node('_rb_slice_from', sequence=receiver, from_=from_, pseudo_type=pseudo_type) elif to: return Node('_rb_slice', sequence=receiver, from_=to_node(0), to=to, pseudo_type=pseudo_type) else: return None
def as_expression(self): return [ Node('_go_multi_assignment', targets=[local('reader', 'Reader'), local('err', 'Error')], values=[call(attr(local('bufio', 'GoLibrary'), 'NewReader', ['Function', 'IO', 'Reader']), [attr(local('os', 'GoLibrary'), 'Stdin', 'IO')], pseudo_type='Reader')]), call( attr(local('reader', 'Reader'), 'ReadString', ['Function', 'String', 'String']), [to_node('\\n')], pseudo_type='String')], None
def as_expression(self): # pseudo_type=self.args[0].pseudo_type begin = method_call(self.args[0], 'begin', [], 'CppIterator') end = method_call(self.args[0], 'end', [], 'CppIterator') if self.name == 'slice_to': from_, to = to_node(0), self.args[1] else: from_, to = self.args[1], self.args[2] if from_.type == 'int' and from_.value == 0: start = begin else: start = Node('binary_op', op='+', left=begin, right=from_, pseudo_type='CppIterator') if self.name == 'slice_from': finish = end elif to.type == 'int' and to.value < 0: finish = Node('binary_op', op='-', left=end, right=to_node(-to.value)) else: finish = Node('binary_op', op='+', left=begin, right=to, pseudo_type='CppIterator') return [ Node('_cpp_declaration', name='_sliced', args=[start, finish], decl_type=self.args[0].pseudo_type, pseudo_type='Void')], None
def as_expression(self): return [ Node('_go_multi_assignment', targets=[local('reader', 'Reader'), local('err', 'Error')], values=[ call(attr(local('bufio', 'GoLibrary'), 'NewReader', ['Function', 'IO', 'Reader']), [attr(local('os', 'GoLibrary'), 'Stdin', 'IO')], pseudo_type='Reader') ]), call(attr(local('reader', 'Reader'), 'ReadString', ['Function', 'String', 'String']), [to_node('\\n')], pseudo_type='String') ], None
def as_expression(self): return [Node('_cpp_declaration', name='ifs', args=[to_node('f.py')], decl_type='ifstream', pseudo_type='Void'), Node('_cpp_declaration', name=self.temp_name(None), args=[Node('_cpp_group', value=Node('_cpp_anon_declaration', args=[local('ifs', 'ifstream')], decl_type='istreambuf_iterator<char>', pseudo_type='Void')), Node('_cpp_group', value=Node('_cpp_anon_declaration', args=[], decl_type='istreambuf_iterator<char>', pseudo_type='Void'))], decl_type='String', pseudo_type='Void')], None
def expand_slice(receiver, from_=None, to=None, pseudo_type=None): if from_: if pseudo_type: #to return Node('_rb_slice', sequence=receiver, from_=from_, to=to, pseudo_type=pseudo_type) else: pseudo_type = to return Node('_rb_slice_from', sequence=receiver, from_=from_, pseudo_type=pseudo_type) elif to: return Node('_rb_slice', sequence=receiver, from_=to_node(0), to=to, pseudo_type=pseudo_type) else: return None
def pad(f, count, fill, pseudo_type): return method_call( method_call( f, 'PadLeft', [Node('binary_op', op='+', left=Node('binary_op', op='/', left=Node('binary_op', op='-', left=count, right=attr(f, 'Length', 'Int'), pseudo_type='Int'), right=to_node(2), pseudo_type='Int'), right=attr(f, 'Length', 'Int'), pseudo_type='Int'), fill], pseudo_type='String'), 'PadRight', [count, fill], 'String')
def empty(f, _): return Node('comparison', op='==', left=attr(f, 'length', pseudo_type='Int'), right=to_node(0), pseudo_type='Boolean')
class RubyTranslator(ApiTranslator): ''' Ruby api translator The DSL is explained in the ApiTranslator docstring ''' methods = { 'List': { '@equivalent': 'Array', 'push': '#push', 'pop': '#pop', 'length': '#length', 'insert': '#insert', 'remove_at': '#delete_at', 'remove': '#delete', 'slice': expand_slice, 'slice_from': expand_slice, 'slice_to': lambda receiver, to, pseudo_type: expand_slice( receiver, to_node(0), to, pseudo_type), 'join': '#join', 'map': to_method_rb_block('map'), 'filter': to_method_rb_block('select'), 'reduce': to_method_rb_block('reduce'), 'any?': to_method_rb_block('any?'), 'all?': to_method_rb_block('all?'), 'sort': '#sort!', 'present?': lambda receiver, _: Node('unary_op', op='not', value=method_call(receiver, 'empty?', [], 'Boolean'), pseudo_type='Boolean'), 'empty?': '#empty?', 'find': '#index', 'contains?': '#include?' }, 'Dictionary': { '@equivalent': 'Hash', 'length': '#length', 'keys': '#keys', 'values': '#values', 'contains?': '#include?', 'present?': lambda receiver, _: Node('unary_op', op='not', value=method_call(receiver, 'empty?', [], 'Boolean'), pseudo_type='Boolean'), 'empty?': '#empty?' }, 'Set': { '@equivalent': 'Set', 'length': '#length', 'contains?': '#include?', 'union': to_op('|'), 'intersection': '#intersection', 'present?': lambda receiver, _: Node('unary_op', op='not', value=method_call(receiver, 'empty?', [], 'Boolean'), pseudo_type='Boolean'), 'empty?': '#empty?' }, 'Tuple': { '@equivalent': 'Array', 'length': '#length' }, 'Array': { '@equivalent': 'Array', 'length': '#length' }, 'String': { '@equivalent': 'String', 'substr': expand_slice, 'substr_from': expand_slice, 'substr_to': lambda receiver, to, _: expand_slice(receiver, None, to, 'String'), 'length': '#length', 'concat': to_op('+'), 'find': '#index', 'find_from': '#index', 'count': '#count', 'partition': '#partition', 'split': '#split', 'trim': '#trim', 'reversed': '#reverse', 'center': '#center', 'present?': lambda receiver, _: Node('unary_op', op='not', value=method_call(receiver, 'empty?', [], 'Boolean'), pseudo_type='Boolean'), 'empty?': '#empty?', 'contains?': '#include?', 'to_int': '#to_i', 'pad_left': '#ljust', 'pad_right': '#rjust' }, 'Regexp': { '@equivalent': 'Regexp', 'match': '%{0}#scan(%{self})' }, 'Int': { 'to_float': '#to_f' }, 'Float': { 'to_int': '#to_i' }, 'RegexpMatch': { '@equivalent': 'Regexp_', 'group': lambda receiver, index, _: Node('index', sequence=Node('index', sequence=receiver, index=index, pseudo_type= ['List', 'String']), index=to_node(0), pseudo_type='String'), 'has_match': lambda receiver, _: Node('unary_op', op='not', value=method_call(receiver, 'empty?', [], 'Boolean'), pseudo_type='Boolean') } } functions = { 'global': { 'wat': lambda _: Node('block', block=[]), 'exit': lambda status, _: call('exit', [status]) }, 'io': { 'display': display, 'read': 'gets', 'read_file': 'File.read', 'write_file': 'File.write' }, 'math': { 'ln': 'Math.log', 'log': 'Math.log', 'tan': 'Math.tan', 'sin': 'Math.sin', 'cos': 'Math.cos', 'pow': lambda left, right, pseudo_type: Node('binary_op', op='**', left=left, right=right, pseudo_type=pseudo_type) }, 'regexp': { 'compile': lambda value, _: Node( '_rb_regex_interpolation', value=value, pseudo_type='Regexp'), 'escape': 'Regexp.escape' }, 'system': { 'args': lambda _: typename('ARGV', ['List', 'String']), 'arg_count': lambda _: Node('binary_op', op='+', left=method_call(local('ARGV', ['List', 'String']), 'length', [], 'Int'), right=to_node(1), pseudo_type='Int'), 'index': lambda value, _: Node('index', sequence=local('ARGV', ['List', 'String']), index=to_node(value.value - 1) if value.type == 'int' else Node('binary_op', op='-', left=value, right=to_node(1), pseudo_type='Int'), pseudo_type='String') # in ruby args counting starts from 0, not 1 } } dependencies = {'http': {'@all': 'Requests'}, 'Set': {'@all': 'set'}}
class GolangTranslator(ApiTranslator): ''' Go api translator The DSL is explained in the ApiTranslator docstring ''' methods = { 'List': { '@equivalent': 'slice', 'push': lambda receiver, element, _: assignment( receiver, call('append', [receiver, element], receiver.pseudo_type)), 'pop': lambda receiver, _: assignment( receiver, Node('_go_slice_to', sequence=receiver, to=Node('binary_op', op='-', left=call('len', [receiver], 'Int'), right=to_node(1), pseudo_type='Int'), pseudo_type=receiver.pseudo_type)), 'length': 'len', 'insert': expand_insert, 'slice': expand_slice, 'slice_from': expand_slice, 'slice_to': lambda receiver, to, pseudo_type: expand_slice( receiver, None, to, pseudo_type), 'join': 'strings.Join(%{self}, %{0})', 'map': expand_map, 'filter': expand_filter, 'find': Find, 'reduce': expand_reduce, 'contains?': ListContains, 'present?': present, 'empty?': empty }, 'Dictionary': { '@equivalent': 'map', 'length': 'len', 'contains?': Contains, 'keys': DictKeys, 'values': DictValues, 'present?': present, 'empty?': empty }, 'String': { '@equivalent': 'str', 'substr': expand_slice, 'substr_from': expand_slice, 'length': 'len', 'substr_to': lambda receiver, to, _: expand_slice( receiver, None, to, pseudo_type='String'), 'find': 'strings.Index(%{self}, %{0})', 'count': 'strings.Count(%{self}, %{0})', 'split': 'strings.Split(%{self}, %{0})', 'concat': to_op('+'), 'contains?': 'strings.Contains(%{self}, %{0})', 'present?': present, 'empty?': empty, 'find_from': lambda f, value, index, _: Node( 'binary_op', op='+', pseudo_type='Int', left=index, right=Node('static_call', receiver=local('strings', 'Library'), message='Index', args=[ Node('_go_slice_from', sequence=f, from_=index, pseudo_type='String'), value ], pseudo_type='Int')), 'to_int': Int }, 'Regexp': { '@equivalent': 'Regexp', 'match': lambda receiver, word, _: method_call( receiver, 'FindAllSubmatch', [ Node('_go_bytes', value=word, pseudo_type='Bytes'), to_node(-1) ], pseudo_type='RegexpMatch') }, 'RegexpMatch': { '@equivalent': 'R', 'group': lambda receiver, index, _: Node('index', sequence=receiver, index=Node('binary_op', op='+', left=index, right=to_node(1), pseudo_type='Int'), pseudo_type='String'), 'has_match': lambda receiver, _: Node('comparison', op='!=', left=receiver, right=Node('null', pseudo_type='None'), pseudo_type='Boolean') }, 'Set': { '@equivalent': 'map[bool]struct{}', 'length': 'len', 'contains?': Contains, 'present?': present, 'empty?': empty }, 'Tuple': { '@equivalent': 'L', 'length': lambda receiver, _: to_node(len(receiver.pseudo_type) - 1) }, 'Array': { '@equivalent': 'int[]', 'length': lambda receiver, _: to_node(receiver.pseudo_type[2]) } } functions = { 'regexp': { 'compile': 'regexp.MustCompile', 'escape': 'regexp.QuoteMeta' }, 'io': { 'display': 'fmt.Println', 'read': Read, 'read_file': ReadFile, 'write_file': 'ioutil.WriteFile' }, 'math': { 'ln': 'math.Log', 'log': 'math.Log', 'tan': 'math.Tan', 'sin': 'math.Sin', 'cos': 'math.Cos', 'pow': 'math.Pow' }, 'system': { 'args': 'os.Args!', 'arg_count': lambda _: call('len', [ attr(local('os', 'Library'), 'Args', ['List', 'String']) ], 'Int'), 'index': lambda value, _: Node('index', sequence=attr(local('os', 'Library'), 'Args', ['List', 'String']), index=value, pseudo_type='String') } } dependencies = { 'regexp': { '@all': 'regexp' }, 'io': { 'display': 'fmt', 'read': ['bufio', 'os'], 'read_file': ['io/ioutil'], 'write_file': ['io/ioutil'] }, 'math': { '@all': 'math' }, 'List': { 'join': 'strings' }, 'String': { 'find': 'strings', 'count': 'strings', 'split': 'strings', 'contains?': 'strings', 'find_from': 'strings', 'to_int': 'strconv' }, 'system': { '@all': 'os' } } errors = {}
def present(s, _): return Node('binary_op', op='>', left=call('len', [s], 'Int'), right=to_node(0), pseudo_type='Boolean')
if name[-1] == '_': name = name[:-1] # int etc examples = globals().get(name.title().replace('_', '')) if examples: test_name = 'test_%s' % name namespace[test_name] = generate_test(name, examples, exp) if 'gen' not in namespace: namespace['gen'] = TestHelpers.gen if 'gen_with_imports' not in namespace: namespace['gen_with_imports'] = TestHelpers.gen_with_imports namespace['maxDiff'] = None return super().__new__(cls, name, bases, namespace) Module = [Node('module', constants=[], code=[])] Int = [to_node(42)] Float = [to_node(42.420)] String = [to_node('la')] Boolean = [Node('boolean', value='true', pseudo_type='Boolean')] Null = [Node('null')] Dictionary = [Node('dictionary', pairs=[ Node('pair', key=to_node('la'), value=to_node(0))], pseudo_type=['Dictionary', 'String', 'Int'])] List = [Node('list', elements=[to_node('la')], pseudo_type=['List', 'String'])] Local = [local('egg')] Typename = [typename('Egg')] InstanceVariable = [Node('instance_variable', name='egg')] Attr = [Node('attr', object=local('e'), attr='egg')] Assignment = [ Node('assignment', target=local('egg', pseudo_type='Int'), value=local('ham', pseudo_type='Int')), Node('assignment', target=Node('instance_variable', name='egg', pseudo_type='Int'), value=local('ham', pseudo_type='Int')),
class CSharpTranslator(ApiTranslator): ''' CSharp api translator The DSL is explained in the ApiTranslator docstring ''' methods = { 'List': { '@equivalent': 'List', 'push': '#Insert', 'pop': lambda l, _: method_call(l, 'RemoveAt', [ Node('binary_op', op='-', left=attr(l, 'Length', 'Int'), right=to_node(1), pseudo_type='Int') ], pseudo_type='Void'), 'insert': '#Insert', 'remove_at': '#RemoveAt', 'remove': '#Remove', 'length': '.Length!', 'slice': expand_slice, 'slice_from': lambda l, from_, pseudo_type: method_call( l, 'Drop', [ Node('binary_op', op='-', left=attr(l, 'Length', 'Int'), right=to_node(-from_.value), pseudo_type='Int') if from_.type == 'int' and from_.value < 0 else from_ ], pseudo_type=pseudo_type), 'slice_to': lambda l, to, pseudo_type: method_call( l, 'Take', [ Node('binary_op', op='-', left=attr(l, 'Length', 'Int'), right=to_node(-to.value), pseudo_type='Int') if to.type == 'int' and to.value < 0 else to ], pseudo_type=pseudo_type), 'slice_': '#Slice', 'join': lambda l, delimiter, _: method_call(delimiter, 'Join', [receiver], 'String'), 'map': linq('Select'), 'filter': linq('Where'), 'reduce': linq('Aggregate', False, swap=True), 'all?': linq('All'), 'any?': linq('Any'), 'concat': '#AddRange', 'present?': '#Any', 'empty?': empty, 'find': '#IndexOf', 'contains?': '#Contains', 'sort': '#Sort' }, 'Dictionary': { '@equivalent': 'Dictionary', 'length': '.Length!', 'keys': '#Keys', 'values': '#Values', 'contains?': '#Contains', 'present?': '#Any', 'empty?': empty }, 'String': { '@equivalent': 'String', 'length': '.Length!', 'substr': lambda f, from_, to, pseudo_type: method_call( f, 'Substring', [from_, normalize(f, from_, to)], pseudo_type), 'substr_from': '#Substring', 'substr_to': lambda f, to, pseudo_type: method_call(f, 'Substring', [ to_node(0), normalize(f, to_node(0), to) ], pseudo_type), 'find_from': '#IndexOf', 'find': '#IndexOf', 'count': lambda f, element, pseudo_type: method_call( f, 'Count', [ Node('anonymous_function', params=[local('sub', 'String')], block=[ Node('comparison', op='==', left=local('sub', 'String'), right=local('s', 'String'), pseudo_type='Boolean') ], return_type='Boolean', pseudo_type=['Function', 'String', 'Boolean']) ], pseudo_type='Int'), 'concat': to_op('+'), 'split': split, 'trim': '#Trim', 'center': pad, 'present?': lambda f, _: Node('comparison', op='!=', left=attr(f, 'Length', 'Int'), right=to_node(0), pseudo_type='Boolean'), 'empty?': lambda f, _: Node('comparison', op='==', left=attr(f, 'Length', 'Int'), right=to_node(0), pseudo_type='Boolean'), 'contains?': '#Contains', 'to_int': 'Int32.Parse(%{self})', 'pad_left': '#PadLeft', 'pad_right': '#PadRight' }, 'Set': { '@equivalent': 'HashSet', 'length': '.Length!', 'contains?': '#Contains', 'union': '#Union', 'intersection': '#Intersection', 'present?': '#Any', 'empty?': empty }, 'Regexp': { '@equivalent': 'Regexp', 'match': '#match' }, 'RegexpMatch': { '@equivalent': 'RegexpMatch', 'group': lambda receiver, index, _: Node( 'index', sequence=attr( Node('index', sequence=attr(receiver, 'Groups', ['List', 'CSharpRegexGroup']), index=to_node(1 + index.value) if index.type == 'int' else Node('binary_op', op='+', left=to_node(1), right=index, pseudo_type='Int'), pseudo_type='CSharpRegexGroup'), 'Captures', ['List', 'RegexpMatch']), index=to_node(0), pseudo_type='RegexpMatch'), 'has_match': '.Success!' }, 'Array': { '@equivalent': 'Any[]', 'length': lambda receiver, _: to_node(receiver.pseudo_type[2]) }, 'Tuple': { '@equivalent': 'Tuple', 'length': lambda _, pseudo_type: to_node(len(pseudo_type) - 1) } } functions = { 'io': { 'display': Display, 'read': 'Console.ReadLine', 'read_file': 'File.ReadAllText', 'write_file': 'File.WriteAllText' }, 'math': { 'ln': 'Math.Log', 'log': 'Math.Log', 'tan': 'Math.Tan', 'sin': 'Math.Sin', 'cos': 'Math.Cos', 'pow': 'Math.Pow' }, 'http': {}, 'system': { 'args': lambda _: local('args', ['List', 'String']), 'index': lambda index, _: Node('index', sequence=local('args', ['List', 'String']), index=to_node(index.value - 1) if index.type == 'int' else Node('binary_op', op='-', left=index, right=to_node(1), pseudo_type='Int'), pseudo_type='String'), 'arg_count': lambda _: Node('binary_op', op='+', left=attr(local('args', ['List', 'String']), 'Length', 'Int'), right=to_node(1), pseudo_type='Int') }, 'regexp': { 'compile': lambda value, _: Node('new_instance', class_name='Regex', args=[value], pseudo_type='Regexp'), 'escape': 'Regex.Escape' } } dependencies = { 'List': { '@all': 'System.Collections.Generic', 'map': 'System.Linq', 'filter': 'System.Linq', 'any?': 'System.Linq', 'all?': 'System.Linq', 'reduce': 'System.Linq' }, 'String': { '@all': 'System.Text' }, 'Dictionary': { '@all': 'System.Collections.Generic', }, 'io': { 'read_file': 'System.IO', 'write_file': 'System.IO' }, 'regexp': { '@all': 'System.Text.RegularExpressions' } }
name = name[:-1] # int etc examples = globals().get(name.title().replace('_', '')) if examples: test_name = 'test_%s' % name namespace[test_name] = generate_test(name, examples, exp) if 'gen' not in namespace: namespace['gen'] = TestHelpers.gen if 'gen_with_imports' not in namespace: namespace['gen_with_imports'] = TestHelpers.gen_with_imports namespace['maxDiff'] = None return super().__new__(cls, name, bases, namespace) Module = [Node('module', constants=[], code=[])] Int = [to_node(42)] Float = [to_node(42.420)] String = [to_node('la')] Boolean = [Node('boolean', value='true', pseudo_type='Boolean')] Null = [Node('null')] Dictionary = [ Node('dictionary', pairs=[Node('pair', key=to_node('la'), value=to_node(0))], pseudo_type=['Dictionary', 'String', 'Int']) ] List = [Node('list', elements=[to_node('la')], pseudo_type=['List', 'String'])] Local = [local('egg')] Typename = [typename('Egg')] InstanceVariable = [Node('instance_variable', name='egg')] Attr = [Node('attr', object=local('e'), attr='egg')] Assignment = [
class JSTranslator(ApiTranslator): ''' JS api translator The DSL is explained in the ApiTranslator docstring ''' methods = { 'List': { '@equivalent': 'Array', 'push': '#push', 'pop': '#pop', 'length': '.length!', 'insert': '#splice(%{self}, 0, %{0})', 'remove_at': lambda receiver, index, _: method_call( receiver, 'splice', [ index, to_node(index.value + 1) if index.type == 'int' else Node('binary_op', op='+', left=index, right=to_node(1), pseudo_type='Int') ], pseudo_type='Void'), 'remove': '_.pull(%{self}, %{0})', 'slice': '#slice', 'slice_from': '#slice', 'slice_to': '#slice(0, %{0})', 'map': '_.map(%{self}, %{0})', 'filter': '_.filter(%{self}, %{0})', 'reduce': '_.reduce(%{self}, %{0}, %{1})', 'any?': '_.any(%{self}, %{0})', 'all?': '_.all(%{self}, %{0})', 'sort': '#sort', 'empty?': empty, 'present?': present, 'find': '#indexOf', 'contains?': '_.contains(%{self}, %{0})' }, 'Dictionary': { '@equivalent': 'Object', 'length': object_len, 'keys': 'Object.keys(%{self})', 'values': 'Object.values(%{self})', 'contains?': '#hasOwnProperty', 'present?': present, 'empty?': empty }, 'String': { '@equivalent': 'String', 'substr': '#slice', 'substr_from': '#slice', 'length': '.length!', 'substr_to': '#slice(0, %{0})', 'find': '#search', 'find_from': lambda f, value, index, _: Node( 'binary_op', op='+', pseudo_type='Int', left=index, right=method_call(method_call(f, 'slice', [index], 'String'), 'search', [value], 'Int')), 'count': lambda f, count, _: attr( method_call(LODASH, 'where', [count], ['List', 'String']), 'length', 'Int'), 'concat': to_op('+'), 'partition': lambda f, delimiter, _: Node( 'index', sequence=method_call(LODASH, 'partition', [delimiter], 'String' ), index=to_node(1), pseudo_type=['Tuple', 'String', 'String', 'String']), 'split': '#split', 'trim': '#trim', 'reversed': lambda f, _: method_call( method_call( method_call(f, 'split', [to_node('')], ['List', 'String']), 'reverse', [], ['List', 'String']), 'join', [to_node('')], 'String'), 'center': '_.pad(%{self}, %{0}, %{1})', 'present?': lambda f, _: f, 'empty?': lambda f, _: Node( 'unary_op', op='not', value=f, pseudo_type='Boolean'), 'contains?': '_.contains(%{self}, %{0})', 'to_int': 'parseInt', 'pad_left': '_.padLeft(%{self}, %{0}, %{1})', 'pad_right': '_.padRight(%{self}, %{0}, %{1})' }, 'Tuple': { '@equivalent': 'Array', 'length': '.length!' }, 'Array': { '@equivalent': 'Array', 'length': '.length!' }, 'Set': { '@equivalent': 'Object', 'length': object_len, 'contains?': '_.contains(%{self}, %{0})', 'intersection': '_.intersection(%{self}, %{0})', 'union': '_.union(%{self}, %{0})', 'present?': present, 'empty?': empty }, 'RegexpMatch': { '@equivalent': 'Array', 'group': lambda receiver, index, _: Node('index', sequence=receiver, index=to_node(1 + index.value) if index.type == 'int' else Node( 'binary_op', op='+', left=to_node(1), right=index, pseudo_type='Int'), pseudo_type='String'), 'has_match': lambda receiver, _: receiver }, 'Regexp': { '@equivalent': 'Regexp', 'match': '#exec' } } functions = { 'global': { 'wat': lambda _: Node('block', block=[]), 'exit': lambda status, _: call('exit', [status]) }, 'system': { 'args': 'process.argv!', 'arg_count': lambda _: Node('binary_op', op='-', left=attr( attr(local('process', 'Library'), 'argv', ['List', 'String']), 'length', 'Int'), right=to_node(1), pseudo_type='Int'), 'index': lambda value, _: Node('index', sequence=attr(local('process', 'Library'), 'argv', ['List', 'String']), index=to_node(value.value + 1) if value.type == 'int' else Node('binary_op', op='+', left=value, right=value, pseudo_type='Int'), pseudo_type='String') }, 'io': { 'display': 'console.log', 'read_file': "fs.readFileSync(%{0}, 'utf8')", 'write_file': "fs.writeFileSync(%{0}, %{1}, 'utf8')", }, 'math': { 'ln': 'Math.log', 'log': 'Math.log', 'tan': 'Math.tan', 'sin': 'Math.sin', 'cos': 'Math.cos', 'pow': 'Math.pow' }, 'regexp': { 'compile': lambda value, _: Node('new_instance', class_name='RegExp', args=[value], pseudo_type='Regexp'), 'escape': '_.escapeRegExp' } } js_dependencies = {'_': 'lodash'} dependencies = { 'io': { 'read_file': 'fs', 'write_file': 'fs' }, 'regexp': { 'escape': 'lodash' } }
def present(f, _): return Node('comparison', op='>', left=attr(f, 'length', pseudo_type='Int'), right=to_node(0), pseudo_type='Boolean')