def current_area(self): cs = self.callstack + [self.ar] for ar in seq_reversed(cs): if ar.program.tree.source_filename == self.toplevel_filename: return self.area_near(ar) # if everything else fails return self.area_near(self.ar)
def _pos_end(self, subtrees): """Returns the position where the last nonempty element of `subtrees` occurs. """ for subtree in utils.seq_reversed(subtrees): if subtree is None or isinstance(subtree, str): continue return subtree.pos_end return self.starting_position
def render(self, board, draw_head=True): w, h = board.size out = utils.StringIO() def row_titles(border): out.write('<tr>') out.write('<td class="lx ' + border + '_left"></td>') for x in range(w): out.write('<td class="lh">%u</td>' % (x,)) out.write('<td class="lx ' + border + '_right"></td>') out.write('</tr>\n') out.write('<table class="gbs_board">\n') row_titles('top') for y in utils.seq_reversed(range(h)): out.write(' <tr>\n') out.write(' <td class="lv">%u</td>\n' % (y,)) for x in range(w): def cell_for(coli): cant = board.cells[y][x].num_stones(coli) if cant == 0: return '<td><div class="O"></div></td>' col = gbs_builtins.Color(coli).name() return '<td><div class="gbs_stone %s"><span>%i</span></div></td>' % (col[0], cant) if board.head == (y, x) and draw_head: out.write(' <td class="gc gh">\n') else: out.write(' <td class="gc">\n') out.write(' <table>\n') out.write(' <tr>%s%s</tr>\n' % (cell_for(1), cell_for(0))) out.write(' <tr>%s%s</tr>\n' % (cell_for(2), cell_for(3))) out.write(' </table>\n') out.write(' </td>\n') out.write(' <td class="lv">%u</td>\n' % (y,)) out.write(' </tr>\n') row_titles('bottom') out.write('</table>\n') res = out.getvalue() out.close() return res
def _cell_contents(self, cell): w, h = self.Cell_w, self.Cell_h out = [[' ' for i in range(w)] for j in range(h)] for col in range(gbs_builtins.NUM_COLORS): count = cell.num_stones(col) if count == 0: continue if col < 2: y = 0 else: y = self.Cell_h - 1 if col % 2 == 0: x = self.Cell_w - 2 else: x = self.Max_num_len out[y][x] = gbs_builtins.Color(col).name()[0] scount = str(count) if len(scount) > self.Max_num_len: scount = prefix_notation(count) for c in utils.seq_reversed(scount): x -= 1 out[y][x] = c return out
def parse(self, token_stream): "Parse a token stream." stack = ['<start>'] previous_token = next(token_stream) # BOF token = next(token_stream) while stack != []: top = stack[-1] if is_nonterminal(top): productions = self._parse_table.get((top, token.type), None) if productions is not None: #if len(productions) != 1: # self._warn_conflict( # top, # token.type, # position.ProgramAreaNear(token)) # in case of conflict, choose the lexically least production production = utils.dict_min_value( productions, key=lambda p: p.rule) stack.pop() res_nonterm = None for nonterm in utils.seq_reversed(production.rule): if nonterm != '': stack.append(nonterm) res_nonterm = nonterm yield 'PRODUCE', res_nonterm, production else: self.parse_error(top, previous_token, token) else: if top == token.type: yield 'CONSUME', top, token previous_token = token token = next(token_stream) stack.pop() else: self.parse_error(top, previous_token, token)
def _make_list_expression(expr_list): """Convert a list of expressions [e1, ..., eN] into an expression representing a Gobstones list, of the form: cons(e1, ... cons(eN, nil) ... ) """ pos_b = expr_list.pos_begin pos_e = expr_list.pos_end lst = expr_list.children ret = ASTNode(['funcCall', bnf_parser.Token('lowerid', '[]', pos_b, pos_e), ASTNode([], pos_b, pos_e) ], pos_b, pos_e) for exp in utils.seq_reversed(lst): pos_b = exp.pos_begin pos_e = exp.pos_end exp_list = ASTNode(['funcCall', bnf_parser.Token('lowerid', '[x]', pos_b, pos_e), ASTNode([exp], pos_b, pos_e) ], pos_b, pos_e) ret = ASTNode(['listop', bnf_parser.Token('lowerid', '++', pos_b, pos_e), exp_list, ret], pos_b, pos_e) return ret
def unpack(self, s): n = 0 s = utils.seq_reversed(s) for c in s: n = (n << 8) | ord(c) return n
def compile_assign_var_tuple1(self, tree, code): "Compile a tuple assignment: (v1, ..., vN) := f(...)" self.compile_expression(tree.children[2], code) varnames = [var.value for var in tree.children[1].children] for var in utils.seq_reversed(varnames): code.push(('popTo', var), near=tree)