Esempio n. 1
0
def prim_env(compiler, src):
    env_name = pycode.name('env')
    env_code = compiler.compile(src[1]).asname(env_name)
    lisp.env_push(env_name)
    body_code = compiler.compile_block((lisp.Symbol('_'),) + src[2:])
    lisp.env_pop()
    return env_code + body_code
Esempio n. 2
0
def prim_env(compiler, src):
    env_name = pycode.name('env')
    env_code = compiler.compile(src[1]).asname(env_name)
    lisp.env_push(env_name)
    body_code = compiler.compile_block((lisp.Symbol('_'), ) + src[2:])
    lisp.env_pop()
    return env_code + body_code
Esempio n. 3
0
    def compile_section(self, proc_var, section, is_last):
        pattern_part, body_part = section
        proc_stack = context.curr().setdefault('PROC_STACK', [])
        lisp.env_push(proc_var)

        if pattern_part:
            proc_stack.append('#')
            pattern_code = self.compile_pattern(pattern_part)

            if pattern_code.meta.get('raise_random', False) and (
                (not is_last) or
                (len(proc_stack) < 2 or proc_stack[-2] != '#')):
                # When compiled pattern code may raise random exception
                # And it's not the last one of matching chain or
                # The caller is call directely in a LISP environment (not in pattern matching)
                # It's the pattern matching code's responsibility to catch all exception
                # and turn it to _ME
                wrap_tpl = 'try:\n  $#\nexcept:\n  raise _ME(%s)\n%s' % (
                    proc_var, proc_var)
                pattern_code = pycode.create(wrap_tpl, pattern_code)
            proc_stack.pop()
        else:
            pattern_code = pycode.create(proc_var, shortcut_nop=True)

        if body_part is None:
            return pattern_code

        proc_stack.append('=>')
        body_code = pycode.create(proc_var + ' = $#\n' + proc_var,
                                  self.lisp_compiler.compile_block(body_part))
        proc_stack.pop()
        lisp.env_pop()

        if not is_last:
            body_tpl = 'try:\n  $#\nexcept _ME, e:\n  raise _UME(%s)\n%s' % (
                proc_var, proc_var)
            body_code = pycode.create(body_tpl, body_code)
        return pattern_code + body_code
Esempio n. 4
0
    def compile_section(self, proc_var, section, is_last):
	pattern_part, body_part = section
	proc_stack = context.curr().setdefault('PROC_STACK', [])
	lisp.env_push(proc_var)
    
	if pattern_part:
	    proc_stack.append('#')
	    pattern_code = self.compile_pattern(pattern_part)
            
	    if pattern_code.meta.get('raise_random', False) and (
                (not is_last) or (len(proc_stack) < 2 or proc_stack[-2] != '#')):
                # When compiled pattern code may raise random exception
                # And it's not the last one of matching chain or
                # The caller is call directely in a LISP environment (not in pattern matching)
                # It's the pattern matching code's responsibility to catch all exception
                # and turn it to _ME
		wrap_tpl = 'try:\n  $#\nexcept:\n  raise _ME(%s)\n%s' % (proc_var, proc_var)
		pattern_code = pycode.create(wrap_tpl, pattern_code)
	    proc_stack.pop()
	else:
	    pattern_code = pycode.create(proc_var, shortcut_nop = True)

	if body_part is None:
	    return pattern_code

	proc_stack.append('=>')
	body_code = pycode.create(
		proc_var + ' = $#\n' + proc_var,
		self.lisp_compiler.compile_block(body_part))
	proc_stack.pop()
	lisp.env_pop()

	if not is_last:
	   body_tpl = 'try:\n  $#\nexcept _ME, e:\n  raise _UME(%s)\n%s' % (
		   proc_var, proc_var)
	   body_code = pycode.create(body_tpl, body_code)
	return pattern_code + body_code