def is_prop_fetch(src): if type(src) is not tuple: return False if len(src) > 2 and src[1] == lisp.Symbol('->') and (lisp.getop( src[-1]) == ':' or not isinstance(src[-1], tuple)): return True return False
def is_prop_fetch(src): if type(src) is not tuple: return False if len(src) > 2 and src[1] == lisp.Symbol('->') and ( lisp.getop(src[-1]) == ':' or not isinstance(src[-1], tuple)): return True return False
def prim_try(compiler, source): curr_stat = 'try' # Step 1: Parse the list try_block = [] except_blocks = [] finally_block = [] try_var = pycode.name('try') for item in source[1:]: if lisp.getop(item) == 'except': if not type(item[1]) is tuple and len(item[1]) == 2 and ( type(item[1][0]) is lisp.Symbol and type(item[1][1]) is lisp.Symbol): raise SyntaxError, item curr_stat = 'except' (except_blocks.append((item[1][0].name, item[1][1].name, item[2:]))) elif lisp.getop(item) == 'finally': finally_block.extend(item[1:]) else: if curr_stat != 'try': raise SyntaxError, source try_block.append(item) if not except_blocks and not finally_block: raise SyntaxError, source # Try part output_tpl = 'try:\n %s = $#\n' % (try_var,) output_codes = [compiler.compile_block(try_block)] # Except part for type_sym, bind_sym, code in except_blocks: output_tpl += 'except %s, %s:\n %s = $#\n' % ( type_sym, bind_sym, try_var) output_codes.append(compiler.compile_block(code)) if finally_block: output_tpl += 'finally:\n $#\n' output_codes.append(compiler.compile_block(finally_block)) output_tpl += try_var return pycode.create(output_tpl, *output_codes)
def prim_class(compiler, source): class_name = source[1].name if len(source) > 2 and lisp.getop(source[2]) == ':': extends = [x.name for x in source[2][1:]] block = source[3:] else: extends = ['object'] block = source[2:] tpl = 'class %s(%s):\n' % (class_name, ','.join(extends)) if block: tpl += ' $#\n' + class_name return pycode.create(tpl, compiler.compile_block(block)) else: tpl += ' pass\n' + class_name return pycode.create(tpl)
def _parse_arglist(arglist): args = [] kwargs = [] kwargs_source = [] idx = 0 while idx < len(arglist): curr = arglist[idx] if type(curr) is lisp.Symbol and curr.name in ('.', '..'): args.append(curr.name.replace('.', '*') + arglist[idx + 1].name) idx += 1 elif type(curr) is lisp.Symbol: args.append(curr.name) elif lisp.getop(curr) == ':': if type(curr[1]) is not lisp.Symbol: raise SyntaxError, arglist kwargs.append(curr[1].name) kwargs_source.append(arglist[idx + 1]) idx += 1 else: raise SyntaxError, arglist idx += 1 return args, kwargs, kwargs_source
def compile_pattern_element(self, element): proc_var = lisp.env_curr() # '_' is a blank space holder that do nothing if element == _S('_'): return pycode.create(proc_var, shortcut_nop = True) # Name binding bind_to, may_raise = Compiler.get_binding(element) if bind_to is not None: return pycode.create( '%s=%s\n%s' % (bind_to, proc_var, proc_var), shortcut_bind = [bind_to], raise_random = may_raise) opname = lisp.getop(element) # Force equal testing if opname == '\'': return self.compile_equal(element[1]) # Type checking if opname == ':': # NOTE: infact, type checking expression could throw exception return self.compile_test((_S('isinstance'), _S('_'), element[1])) if opname == '?': return self.compile_test(*element[1:]).add_meta(raise_random = True) # Expanded matchings if opname in self.EXTS: return self.EXTS[opname](self, element) # Extractor if type(element) is tuple and len(element) > 0: return self.compile_call( (_S('#'),) + element[1:], (element[0], _S('_'))).add_meta(raise_random = True) # Now all come to the structual matching return self.compile_pattern_struct(element)
def compile_pattern_element(self, element): proc_var = lisp.env_curr() # '_' is a blank space holder that do nothing if element == _S('_'): return pycode.create(proc_var, shortcut_nop=True) # Name binding bind_to, may_raise = Compiler.get_binding(element) if bind_to is not None: return pycode.create('%s=%s\n%s' % (bind_to, proc_var, proc_var), shortcut_bind=[bind_to], raise_random=may_raise) opname = lisp.getop(element) # Force equal testing if opname == '\'': return self.compile_equal(element[1]) # Type checking if opname == ':': # NOTE: infact, type checking expression could throw exception return self.compile_test((_S('isinstance'), _S('_'), element[1])) if opname == '?': return self.compile_test(*element[1:]).add_meta(raise_random=True) # Expanded matchings if opname in self.EXTS: return self.EXTS[opname](self, element) # Extractor if type(element) is tuple and len(element) > 0: return self.compile_call( (_S('#'), ) + element[1:], (element[0], _S('_'))).add_meta(raise_random=True) # Now all come to the structual matching return self.compile_pattern_struct(element)
def prim_def(compiler, source): fn_name = lisp.getop(source[1]) if fn_name is not None: return compile_fn(compiler, fn_name, source[1][1:], source[2:]) else: return compiler.compile((lisp.Symbol('='), source[1], source[2:]))
def is_proc(node): return lisp.getop(node) in ('#', '=>')