Example #1
0
def eval_expr(alpaca, playfield, x, y, ast, verbose=False):
    """Given a playfield and a position within it, and a boolean expression,
    return what the expression evaluates to at that position.
    
    """
    if ast.type == 'BoolOp':
        lhs = eval_expr(alpaca, playfield, x, y, ast.lhs, verbose=verbose)
        rhs = eval_expr(alpaca, playfield, x, y, ast.rhs, verbose=verbose)
        op = ast.op
        if op == 'and':
            return lhs and rhs
        elif op == 'or':
            return lhs or rhs
        elif op == 'xor':
            return lhs != rhs
    elif ast.type == 'Not':
        return not eval_expr(
            alpaca, playfield, x, y, ast.expr, verbose=verbose)
    elif ast.type == 'Adjacency':
        rel = ast.rel
        nbhd = ast.nbhd
        if nbhd.type == 'NbhdRef':
            nbhd = find_nbhd_defn(alpaca, nbhd.id).children[0]
        assert nbhd.type == 'Neighbourhood'
        count = 0
        for (dx, dy) in set([node.value for node in nbhd.children]):
            pf_state_id = playfield.get(x + dx, y + dy)
            if eval_relation(alpaca,
                             playfield,
                             x,
                             y,
                             pf_state_id,
                             rel,
                             verbose=verbose):
                count += 1
        #print "(%d,%d) has %d neighbours that are %r" % (x, y, count, rel)
        return count >= int(ast.count)
    elif ast.type == 'Relational':
        state_id = eval_state_ref(playfield, x, y, ast.lhs)
        rel = ast.rhs
        return eval_relation(alpaca,
                             playfield,
                             x,
                             y,
                             state_id,
                             rel,
                             verbose=verbose)
    elif ast.type == 'BoolLit':
        if ast.value == 'true':
            return True
        elif ast.value == 'false':
            return False
        elif ast.value == 'guess':
            return bool(random.getrandbits(1))
        else:
            raise NotImplementedError(repr(ast))
    else:
        raise NotImplementedError(repr(ast))
Example #2
0
def eval_expr(alpaca, playfield, x, y, ast):
    """Given a playfield and a position within it, and a boolean expression,
    return what the expression evaluates to at that position.
    
    """
    if ast.type == "BoolOp":
        lhs = eval_expr(alpaca, playfield, x, y, ast.children[0])
        rhs = eval_expr(alpaca, playfield, x, y, ast.children[1])
        op = ast.value
        if op == "and":
            return lhs and rhs
        elif op == "or":
            return lhs or rhs
        elif op == "xor":
            return lhs != rhs
    elif ast.type == "Not":
        return not eval_expr(alpaca, playfield, x, y, ast.children[0])
    elif ast.type == "Adjacency":
        rel = ast.children[0]
        nb = ast.children[1]
        if nb.type == "NbhdRef":
            nb = find_nbhd_defn(alpaca, nb.value).children[0]
        assert nb.type == "Neighbourhood"
        nb = set([node.value for node in nb.children])
        count = 0
        for (dx, dy) in nb:
            pf_state_id = playfield.get(x + dx, y + dy)
            if eval_relation(alpaca, playfield, x, y, pf_state_id, rel):
                count += 1
        # print "(%d,%d) has %d neighbours that are %r" % (x, y, count, rel)
        return count >= int(ast.value)
    elif ast.type == "Relational":
        state_id = eval_state_ref(playfield, x, y, ast.children[0])
        rel = ast.children[1]
        return eval_relation(alpaca, playfield, x, y, state_id, rel)
    elif ast.type == "BoolLit":
        if ast.value == "true":
            return True
        elif ast.value == "false":
            return False
        elif ast.value == "guess":
            return bool(random.getrandbits(1))
        else:
            raise NotImplementedError(repr(ast))
    else:
        raise NotImplementedError(repr(ast))
Example #3
0
def eval_expr(alpaca, playfield, x, y, ast, verbose=False):
    """Given a playfield and a position within it, and a boolean expression,
    return what the expression evaluates to at that position.
    
    """
    if ast.type == 'BoolOp':
        lhs = eval_expr(alpaca, playfield, x, y, ast.lhs, verbose=verbose)
        rhs = eval_expr(alpaca, playfield, x, y, ast.rhs, verbose=verbose)
        op = ast.op
        if op == 'and':
            return lhs and rhs
        elif op == 'or':
            return lhs or rhs
        elif op == 'xor':
            return lhs != rhs
    elif ast.type == 'Not':
        return not eval_expr(alpaca, playfield, x, y, ast.expr, verbose=verbose)
    elif ast.type == 'Adjacency':
        rel = ast.rel
        nbhd = ast.nbhd
        if nbhd.type == 'NbhdRef':
            nbhd = find_nbhd_defn(alpaca, nbhd.id).children[0]
        assert nbhd.type == 'Neighbourhood'
        count = 0
        for (dx, dy) in set([node.value for node in nbhd.children]):
            pf_state_id = playfield.get(x + dx, y + dy)
            if eval_relation(alpaca, playfield, x, y, pf_state_id, rel, verbose=verbose):
                count += 1
        #print "(%d,%d) has %d neighbours that are %r" % (x, y, count, rel)
        return count >= int(ast.count)
    elif ast.type == 'Relational':
        state_id = eval_state_ref(playfield, x, y, ast.lhs)
        rel = ast.rhs
        return eval_relation(alpaca, playfield, x, y, state_id, rel, verbose=verbose)
    elif ast.type == 'BoolLit':
        if ast.value == 'true':
            return True
        elif ast.value == 'false':
            return False
        elif ast.value == 'guess':
            return bool(random.getrandbits(1))
        else:
            raise NotImplementedError(repr(ast))
    else:
        raise NotImplementedError(repr(ast))
Example #4
0
 def compile_expr(self, expr):
     if expr.type == 'BoolOp':
         self.file.write('(')
         self.compile_expr(expr.lhs)
         self.file.write({
             'or': '||',
             'and': '&&',
             'xor': '!==',
         }[expr.op])
         self.compile_expr(expr.rhs)
         self.file.write(')')
     elif expr.type == 'Not':
         self.file.write('!(')
         self.compile_expr(expr.expr)
         self.file.write(')')
     elif expr.type == 'BoolLit':
         if expr.value == 'guess':
             self.file.write('(Math.random()<.5)')
         else:
             self.file.write(expr.value)
     elif expr.type == 'Relational':
         self.compile_relation(expr.lhs, expr.rhs)
     elif expr.type == 'Adjacency':
         count = expr.count
         rel = expr.rel
         nbhd = expr.nbhd
         if nbhd.type == 'NbhdRef':
             nbhd = find_nbhd_defn(self.alpaca, nbhd.id).children[0]
         assert nbhd.type == 'Neighbourhood'
         if rel.type == 'ClassDecl':
             self.file.write("(in_nbhd_pred(pf, x, y, is_%s" % rel.id)
         else:
             self.file.write('(in_nbhd_eq(pf, x, y, ')
             self.compile_state_ref(rel)
         self.file.write(', [')
         self.file.write(','.join(
             ['[%d,%d]' % child.value for child in nbhd.children]
         ))
         self.file.write(']) >= %d)' % int(count))
     else:
         raise NotImplementedError(repr(expr))
Example #5
0
 def compile_expr(self, expr):
     if expr.type == 'BoolOp':
         self.file.write('(')
         self.compile_expr(expr.lhs)
         self.file.write({
             'or': '||',
             'and': '&&',
             'xor': '!==',
         }[expr.op])
         self.compile_expr(expr.rhs)
         self.file.write(')')
     elif expr.type == 'Not':
         self.file.write('!(')
         self.compile_expr(expr.expr)
         self.file.write(')')
     elif expr.type == 'BoolLit':
         if expr.value == 'guess':
             self.file.write('(Math.random()<.5)')
         else:
             self.file.write(expr.value)
     elif expr.type == 'Relational':
         self.compile_relation(expr.lhs, expr.rhs)
     elif expr.type == 'Adjacency':
         count = expr.count
         rel = expr.rel
         nbhd = expr.nbhd
         if nbhd.type == 'NbhdRef':
             nbhd = find_nbhd_defn(self.alpaca, nbhd.id).children[0]
         assert nbhd.type == 'Neighbourhood'
         if rel.type == 'ClassDecl':
             self.file.write("(in_nbhd_pred(pf, x, y, is_%s" % rel.id)
         else:
             self.file.write('(in_nbhd_eq(pf, x, y, ')
             self.compile_state_ref(rel)
         self.file.write(', [')
         self.file.write(','.join(
             ['[%d,%d]' % child.value for child in nbhd.children]))
         self.file.write(']) >= %d)' % int(count))
     else:
         raise NotImplementedError(repr(expr))