How to use increase_indent method in molecule

Best Python code snippet using molecule_python

CompilationEngine.py

Source:CompilationEngine.py Github

copy

Full Screen

...35 def compile_class(self):36 # create symbol table for class37 self.symbol_table = SymbolTable()38 self.add_opening_tag('class')39 self.increase_indent()40 self.write_next_token() # 'class'41 self.class_name = self.write_next_token() # className42 self.write_next_token() # {43 while self.tokenizer.has_more_tokens():44 if self.tokenizer.look_ahead()[1] in set(['static', 'field']):45 self.compile_class_var_dec()46 elif self.tokenizer.look_ahead()[1] in set(['function', 'constructor', 'method']):47 self.compile_subroutine()48 elif self.tokenizer.look_ahead()[1] == '}':49 self.write_next_token() # }50 # print self.symbol_table.class_symbols51 self.decrease_indent()52 self.add_closing_tag('class')53 # ('static' | 'field' ) type varName (',' varName)* ';'54 def compile_class_var_dec(self):55 tokens = []56 self.add_opening_tag('classVarDec')57 self.increase_indent()58 while self.tokenizer.look_ahead()[1] != ';':59 tokens.append(self.write_next_token())60 self.symbol_table.add_class_var(tokens)61 self.write_next_token()62 self.decrease_indent()63 self.add_closing_tag('classVarDec')64 # ('constructor' | 'function' | 'method') ('void' | type) subroutineName '(' parameterList ')' subroutineBody65 def compile_subroutine(self):66 self.symbol_table.reset_subroutine()67 # add class to sub_symbols table68 self.add_opening_tag('subroutineDec')69 self.increase_indent()70 f_type = self.write_next_token() # constructor|function|method71 # if f_type in {'method', 'constructor'}:72 if f_type == 'method':73 self.symbol_table.add_sub_var('arg', self.class_name, 'this')74 self.write_next_token() # 'void'|type75 f_name = self.write_next_token() # subroutineName76 self.compile_param_list()77 self.write_next_token() # )78 # tokens = { varDec*79 self.write_next_token() # {80 num_locals = 081 while self.tokenizer.look_ahead()[1] == 'var':82 # figure out number of vars83 num_locals += self.compile_var_dec()84 self.VMwriter.write_function('{}.{}'.format(self.class_name, f_name), num_locals)85 if f_type == 'method':86 # push this87 self.VMwriter.write_push('arg', 0)88 # store memory address of this obj in this89 self.VMwriter.write_pop('pointer', 0)90 elif f_type == 'constructor':91 # push fields onto stack, allocate that much memory92 num_fields = self.symbol_table.num_field_vars()93 self.VMwriter.write_push('constant', num_fields)94 self.VMwriter.write_call('Memory.alloc', 1)95 self.VMwriter.write_pop('pointer', 0)96 self.compile_subroutine_body()97 self.decrease_indent()98 self.add_closing_tag('subroutineDec')99 # statements '}'100 def compile_subroutine_body(self):101 self.add_opening_tag('subroutineBody')102 self.increase_indent()103 while self.tokenizer.look_ahead()[1] != '}':104 self.compile_statements()105 self.write_next_token() # }106 self.decrease_indent()107 self.add_closing_tag('subroutineBody')108 # ( (type varName) (',' type varName)*)?109 def compile_param_list(self):110 self.add_opening_tag('parameterList')111 self.increase_indent()112 while self.tokenizer.look_ahead()[1] != ')':113 token = self.tokenizer.look_ahead()114 # ( OR ,115 if token[0] == 'symbol':116 token_type = None117 elif token[0] == 'keyword':118 token_type = token[1]119 else:120 if token_type:121 token_name = token[1]122 self.symbol_table.add_sub_var('arg', token_type, token_name)123 else:124 token_type = token[1]125 self.write_next_token()126 self.decrease_indent()127 self.add_closing_tag('parameterList')128 # 'var' type varName (',' varName)* ';'129 def compile_var_dec(self):130 self.add_opening_tag('varDec')131 self.increase_indent()132 self.write_next_token() # var133 token_type = self.write_next_token() # type134 num_vars = 0135 while self.tokenizer.look_ahead()[1] != ';':136 token = self.tokenizer.look_ahead()137 if token[0] == 'identifier':138 num_vars += 1139 token_name = token[1]140 self.symbol_table.add_sub_var('var', token_type, token_name)141 self.write_next_token()142 self.write_next_token() # ;143 self.decrease_indent()144 self.add_closing_tag('varDec')145 return num_vars146 # statement*147 # letStatement | ifStatement | whileStatement | doStatement | returnStatement148 def compile_statements(self):149 self.add_opening_tag('statements')150 self.increase_indent()151 while self.tokenizer.look_ahead()[1] != '}':152 if self.tokenizer.look_ahead()[1] == 'do':153 self.compile_do()154 elif self.tokenizer.look_ahead()[1] == 'let':155 self.compile_let()156 elif self.tokenizer.look_ahead()[1] == 'while':157 self.compile_while()158 elif self.tokenizer.look_ahead()[1] == 'if':159 self.compile_if()160 elif self.tokenizer.look_ahead()[1] == 'return':161 self.compile_return()162 self.decrease_indent()163 self.add_closing_tag('statements')164 # subroutineCall: subroutineName '(' expressionList ')' | ( className | varName) '.' subroutineName '(' expressionList ')'165 def compile_subroutine_call(self):166 call_name = self.write_next_token() # subroutineName|className|varName167 # lookup in symbol table call_name168 symbol = self.symbol_table.sub_symbols.get(call_name, None)169 if not symbol:170 symbol = self.symbol_table.class_symbols.get(call_name, None)171 # entering expressionlist172 # subroutine case 2: show(x, y, z) (method)173 if self.tokenizer.look_ahead()[1] == '(':174 self.write_next_token() # (175 # first push class as "this"176 self.VMwriter.write_push('pointer', 0)177 num_exp = self.compile_expression_list() + 1178 self.write_next_token() # )179 self.VMwriter.write_call('{}.{}'.format(self.class_name, call_name), num_exp)180 # subroutineCall case 1:181 elif self.tokenizer.look_ahead()[1] == '.':182 self.write_next_token() # .183 method_name = self.write_next_token() # subroutineName184 self.write_next_token() # (185 # game.run()186 if symbol:187 self.VMwriter.write_push(symbol['kind'], symbol['index'])188 call_name = symbol['type']189 # Math.multiply(x, y)190 num_exp = self.compile_expression_list()191 if symbol:192 # this means we're operating on a method193 num_exp += 1194 # writes the VM command that calls the function with number of args195 self.VMwriter.write_call('{}.{}'.format(call_name, method_name), num_exp)196 self.write_next_token() # )197 # 'do' subroutineCall ';'198 def compile_do(self):199 self.add_opening_tag('doStatement')200 self.increase_indent()201 self.write_next_token() # do202 self.compile_subroutine_call() # subroutine call203 # throw away top stack item204 self.VMwriter.write_pop('temp', 0)205 self.write_next_token() # ;206 self.decrease_indent()207 self.add_closing_tag('doStatement')208 # 'let' varName ('[' expression ']')? '=' expression ';'209 def compile_let(self):210 self.add_opening_tag('letStatement')211 self.increase_indent()212 self.write_next_token() # let213 token = self.write_next_token() # varName214 symbol = self.symbol_table.sub_symbols.get(token, None)215 if not symbol:216 symbol = self.symbol_table.class_symbols.get(token, None)217 # [ expression ]218 did_index = False219 if self.tokenizer.look_ahead()[1] == '[':220 # only push if we're indexing array221 self.VMwriter.write_push(symbol['kind'], symbol['index'])222 did_index = True223 self.write_next_token() # [224 self.compile_expression() # expression225 self.write_next_token() # ]226 # add token and index (from compiled expression)227 self.VMwriter.write_arithmetic('+')228 self.write_next_token() # =229 # write expression and place on stack230 self.compile_expression()231 if did_index:232 self.VMwriter.write_pop('temp', 0)233 self.VMwriter.write_pop('pointer', 1)234 self.VMwriter.write_push('temp', 0)235 self.VMwriter.write_pop('that', 0)236 else:237 # pop symboltable[symbol]238 self.VMwriter.write_pop(symbol['kind'], symbol['index'])239 self.write_next_token() # ;240 self.decrease_indent()241 self.add_closing_tag('letStatement')242 # 'while' '(' expression ')' '{' statements '}'243 def compile_while(self):244 self.label_index += 1245 label = self.label_index246 self.add_opening_tag('whileStatement')247 self.increase_indent()248 self.write_next_token() # while249 self.VMwriter.write_label('WHILE_COND{}'.format(label))250 self.write_next_token() # (251 self.compile_expression()252 self.write_next_token() # )253 self.VMwriter.write_arithmetic('~', unary=True)254 self.VMwriter.write_if('END_WHILE{}'.format(label))255 self.write_next_token() # {256 while self.tokenizer.look_ahead()[1] != '}':257 self.compile_statements()258 self.write_next_token() # }259 self.VMwriter.write_goto('WHILE_COND{}'.format(label))260 self.VMwriter.write_label('END_WHILE{}'.format(label))261 self.decrease_indent()262 self.add_closing_tag('whileStatement')263 # 'return' expression? ';'264 def compile_return(self):265 self.add_opening_tag('returnStatement')266 self.increase_indent()267 self.write_next_token() # return268 # returns nothing269 if self.tokenizer.look_ahead()[1] == ';':270 self.VMwriter.write_push('constant', 0)271 while self.tokenizer.look_ahead()[1] != ';':272 self.compile_expression()273 # write return274 self.VMwriter.write_return()275 self.write_next_token() # ;276 self.decrease_indent()277 self.add_closing_tag('returnStatement')278 # 'if' '(' expression ')' '{' statements '}' ( 'else' '{' statements '}' )?279 # if (expression) {s1} else {s2}280 # if (expression) {s1} ==> not expression; if-goto l1; s1; l1281 def compile_if(self):282 self.label_index += 1283 label = self.label_index284 self.add_opening_tag('ifStatement')285 self.increase_indent()286 self.write_next_token() # if287 self.write_next_token() # (288 self.compile_expression()289 self.write_next_token() # )290 self.VMwriter.write_if('IF_TRUE{}'.format(label))291 self.VMwriter.write_goto('IF_FALSE{}'.format(label))292 self.VMwriter.write_label('IF_TRUE{}'.format(label))293 self.write_next_token() # {294 self.compile_statements() # s1295 self.write_next_token() # }296 # [else]297 # goto end298 self.VMwriter.write_goto('IF_END{}'.format(label))299 self.VMwriter.write_label('IF_FALSE{}'.format(label))300 if self.tokenizer.look_ahead()[1] == 'else': # l1301 self.write_next_token() # else302 # write label for else code executions303 self.write_next_token() # {304 self.compile_statements() # s2305 self.write_next_token() # }306 self.VMwriter.write_label('IF_END{}'.format(label))307 self.decrease_indent()308 self.add_closing_tag('ifStatement')309 # term (op term)*310 def compile_expression(self):311 self.add_opening_tag('expression')312 self.increase_indent()313 self.compile_term() # term314 while self.tokenizer.look_ahead()[1] in ops:315 self.tokenizer.look_ahead()[1]316 operation = self.write_next_token() # op317 self.compile_term()318 self.VMwriter.write_arithmetic(operation)319 self.decrease_indent()320 self.add_closing_tag('expression')321 # integerConstant | stringConstant | keywordConstant | varName |322 # varName '[' expression ']' | subroutineCall | '(' expression ')' | unaryOp term323 def compile_term(self):324 self.add_opening_tag('term')325 self.increase_indent()326 next_token = self.tokenizer.look_ahead()327 if next_token[1] in unary_ops:328 operation = self.write_next_token() # unaryOp329 self.compile_term() # term330 self.VMwriter.write_arithmetic(operation, unary=True)331 elif next_token[1] == '(':332 self.write_next_token() # (333 self.compile_expression() # expression334 self.write_next_token() # )335 else: # some sort of identifier is present first336 # subroutineCall337 if self.tokenizer.tokens[1][1] in {'.', '('}:338 self.compile_subroutine_call()339 # varname|intconstant|stringconstant|keywordconstant340 else:341 token = self.write_next_token() # write identifer342 token_type = self.tokenizer.get_token_type()343 symbol = self.symbol_table.sub_symbols.get(token, None)344 if not symbol:345 symbol = self.symbol_table.class_symbols.get(token, None)346 # varName ([expression])?347 if symbol and token != 'this':348 # evaluating a term, so needs to be on stack349 self.VMwriter.write_push(symbol['kind'], symbol['index'])350 # [ expression ]351 if self.tokenizer.look_ahead()[1] == '[':352 self.write_next_token() # [353 self.compile_expression() # expression354 self.write_next_token() # ]355 # add token and index (from compiled expression)356 self.VMwriter.write_arithmetic('+')357 # pop pointer 1 (stores base address of that)358 self.VMwriter.write_pop('pointer', 1)359 # put dereferenced value of THAT on the stack360 self.VMwriter.write_push('that', 0)361 # intConstant, stringConstant, keywordConstant362 elif token_type == 'integerConstant':363 self.VMwriter.write_push('constant', token)364 elif token_type == 'stringConstant':365 self.VMwriter.write_push('constant', len(token))366 self.VMwriter.write_call('String.new', 1)367 for char in token:368 self.VMwriter.write_push('constant', ord(char))369 self.VMwriter.write_call('String.appendChar', 2)370 elif token_type == 'keyword':371 if token in {'null', 'false'}:372 self.VMwriter.write_push('constant', 0)373 elif token == 'true':374 self.VMwriter.write_push('constant', 0)375 self.VMwriter.write_arithmetic('~', unary=True)376 elif token == 'this':377 self.VMwriter.write_push('pointer', 0)378 else:379 raise Exception('Unkown keyword!')380 else:381 raise Exception('Unkown term!')382 self.decrease_indent()383 self.add_closing_tag('term')384 # (expression (',' expression)* )?385 def compile_expression_list(self):386 self.add_opening_tag('expressionList')387 self.increase_indent()388 num_exp = 0389 while self.tokenizer.look_ahead()[1] not in set([')', ']']):390 if self.tokenizer.look_ahead()[1] == ',':391 self.write_next_token() # ,392 else:393 self.compile_expression()394 num_exp += 1395 self.decrease_indent()396 self.add_closing_tag('expressionList')397 return num_exp398 def increase_indent(self):399 self.indent += 1400 def decrease_indent(self):401 self.indent -= 1402 def write_token(self, token):403 token_str = token + "\n"404 self.contents.append(token_str)405 def add_opening_tag(self, tagname):406 tag_str = "\t" * self.indent + "<{}>\n".format(tagname)407 self.contents.append(tag_str)408 def add_closing_tag(self, tagname):409 tag_str = "\t" * self.indent + "</{}>\n".format(tagname)...

Full Screen

Full Screen

pretty_print.py

Source:pretty_print.py Github

copy

Full Screen

1#encoding: utf-82from .syntax import *3indent = 04def increase_indent():5 global indent6 indent += 47def decrease_indent():8 global indent9 indent -= 410def write(arg):11 print(" "*indent + str(arg))12def compound_statement_print(node):13 write(":STATEMENTS")14 increase_indent()15 for st in node.statements:16 pretty_print(st)17 decrease_indent()18def expr_print(node):19 write(":EXPR")20 increase_indent()21 pretty_print(node.left)22 write(node.oper)23 pretty_print(node.right)24 decrease_indent()25def postfix_print(node):26 write(":POSTFIX")27 increase_indent()28 write(node.name)29 write(node.oper)30 decrease_indent()31def integer_print(node):32 write(f":INTEGER({node.value})")33def float_print(node):34 write(f":FLOAT({node.value})")35def ident_print(node):36 write(f":IDENT({node.name})")37def string_print(node):38 write(f":STRING({node.value})")39def bool_print(node):40 write(f":BOOL({node.value})")41def var_def_print(node):42 name = node.name43 var_type = node.type44 expr = node.expr45 46 write(":VAR_DEF")47 increase_indent()48 write(name)49 write(var_type)50 pretty_print(expr)51 decrease_indent()52def assignment_print(node):53 name = node.name54 expr = node.expr55 56 write(":ASSIGNMENT")57 increase_indent()58 write(name)59 pretty_print(expr)60 decrease_indent()61def infinit_loop_print(node):62 write(":INFINIT_LOOP")63 increase_indent()64 pretty_print(node.statements)65 decrease_indent()66def count_loop_print(node):67 write(":COUNT_LOOP")68 increase_indent()69 pretty_print(node.n)70 pretty_print(node.statements)71 decrease_indent()72def break_print(node):73 write(":BREAK")74def continue_print(node):75 write(":CONTINUE")76def branch_print(node):77 write(":BRANCH")78 increase_indent()79 pretty_print(node.if_clause)80 if node.elif_clauses is not None:81 pretty_print(node.elif_clauses)82 if node.else_clause is not None:83 pretty_print(node.else_clause)84 decrease_indent()85def if_print(node):86 write(":IF")87 increase_indent()88 pretty_print(node.condition)89 pretty_print(node.statements)90 decrease_indent()91def elif_print(node):92 write(":ELIF")93 increase_indent()94 pretty_print(node.condition)95 pretty_print(node.statements)96 decrease_indent()97def elif_clauses_print(node):98 for clause in node.elif_clauses:99 pretty_print(clause)100def else_print(node):101 write(":ELSE")102 increase_indent()103 pretty_print(node.statements)104 decrease_indent()105def switch_condition_print(node):106 write(":SWITCH_CONDITION")107 increase_indent()108 pretty_print(node.case_clauses)109 if node.else_clause is not None:110 pretty_print(node.else_clause)111 if node.finally_clause is not None:112 pretty_print(node.finally_clause)113 decrease_indent()114def switch_value_print(node):115 write(":SWITCH_VALUE")116 increase_indent()117 pretty_print(node.case_clauses)118 if node.else_clause is not None:119 pretty_print(node.else_clause)120 if node.finally_clause is not None:121 pretty_print(node.finally_clause)122 decrease_indent()123def match_condition_clauses_print(node):124 write(":MATCH_CONDITION_CLAUSES")125 increase_indent()126 for match in node.match_condition_clauses:127 pretty_print(match)128 decrease_indent()129def match_condition_print(node):130 write(":MATCH_CONDITION")131 increase_indent()132 pretty_print(node.condition)133 pretty_print(node.statements)134 decrease_indent()135def match_value_clauses_print(node):136 write(":MATCH_VALUE_CLAUSES")137 increase_indent()138 for match in node.match_value_clauses:139 pretty_print(match)140 decrease_indent()141def match_value_print(node):142 write(":MATCH_VALUE")143 increase_indent()144 pretty_print(node.exprs)145 pretty_print(node.statements)146 decrease_indent()147def finally_print(node):148 write(":FINALLY")149 increase_indent()150 pretty_print(node.statements)151 decrease_indent()152def return_print(node):153 write(":RETURN")154 increase_indent()155 156 if node.exprs is not None:157 pretty_print(node.exprs)158 decrease_indent()159def exprs_print(node):160 write(":EXPRS")161 increase_indent()162 for expr in node.exprs:163 pretty_print(expr)164 decrease_indent()165def import_print(node):166 write(":IMPORT")167 increase_indent()168 for name in node.names:169 write(name)170 decrease_indent()171def func_def_print(node):172 write(":FUNC_DEF")173 increase_indent()174 write(node.name)175 write(", ".join(map(lambda x: x[0] + ": " + x[1],node.arg_names)))176 write(", ".join(node.return_types))177 pretty_print(node.statements)178 decrease_indent()179def call_func_print(node):180 write(":CALL_FUNC")181 increase_indent()182 write(node.name)183 if node.args is not None:184 pretty_print(node.args)185 decrease_indent()186def call_lib_func_print(node):187 write(":CALL_LIB_FUNC")188 increase_indent()189 write(node.name)190 write(node.lib)191 if node.args is not None:192 pretty_print(node.args)193 decrease_indent()194def args_print(node):195 write(":ARGS")196 increase_indent()197 for arg in node.args:198 pretty_print(arg)199 decrease_indent()200syntax_f = {201 COMPOUND_STATEMENT: compound_statement_print,202 EXPR: expr_print,203 POSTFIX: postfix_print,204 INTEGER: integer_print,205 FLOAT: float_print,206 IDENT: ident_print,207 STRING: string_print,208 BOOL: bool_print,209 VAR_DEF: var_def_print,210 ASSIGNMENT: assignment_print,...

Full Screen

Full Screen

yaml.py

Source:yaml.py Github

copy

Full Screen

...9def map_constructor(loader, node):10 loader.flatten_mapping(node)11 return OrderedDict(loader.construct_pairs(node))12# This monkeypatch ensures we get nice indentation for dashed lists.13def increase_indent(self, flow=False, indentless=False):14 return super(pyyaml.dumper.Dumper, self).increase_indent(flow=flow, indentless=False)15if pyyaml.safe_dump is pyyaml.dump:16 # PyYAML v4.117 SafeDumper = pyyaml.dumper.Dumper18 DangerDumper = pyyaml.dumper.DangerDumper19else:20 SafeDumper = pyyaml.dumper.SafeDumper21 DangerDumper = pyyaml.dumper.Dumper22SafeDumper.increase_indent = increase_indent23DangerDumper.increase_indent = increase_indent24pyyaml.add_representer(dict, map_representer, Dumper=SafeDumper)25pyyaml.add_representer(OrderedDict, map_representer, Dumper=SafeDumper)26pyyaml.add_representer(dict, map_representer, Dumper=DangerDumper)27pyyaml.add_representer(OrderedDict, map_representer, Dumper=DangerDumper)28del map_constructor, map_representer...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run molecule automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful