def getSetup(self, v, printVar): templateVars = { 'v': v, 'varType': self.expand('IntType', {'id': 'loopStartType'}), 'm': self.expand('MaxToken'), 'printVar': printVar } # switch on the three structures struct = self.getChoice('whileDecPrint-setup') if struct == 'printVal': return gu.format( ''' {varType} {v} = {m}; {printVar} ''', templateVars) if struct == 'printStart': return gu.format( ''' {varType} {v} = {m}; println(START); ''', templateVars) if struct == 'prePrintStart': return gu.format( ''' println(START); {varType} {v} = {m}; ''', templateVars) raise Exception('unknown stuct ' + struct)
def renderAddsSame(self): tempVars = { 'd': self.expand('Diameter', {'id': 'diam'}), 'v': 'oval', 'x': self.params['x'], 'y': self.expand('Y') } preLine = gu.format('GOval {v} = new GOval({d}, {d});', tempVars) addLine = gu.format('add({v}, {x}, {y});', tempVars) self.getState('loop-pre-vars').append(preLine) return addLine
def renderStandard(self): x = self.params['x'] tempVars = { 'd': self.expand('Diameter', {'id': 'diam'}), 'v': 'oval', 'x': x, 'y': self.expand('Y') } preLine = gu.format('GOval {v} = new GOval({d}, {d});', tempVars) addLine = gu.format('add({v}, {x}, {y});', tempVars) self.getState('loop-start-lines').append(preLine) return addLine
def renderCode(self): choice = self.getChoice(self.getKey()) self.incrementState('extra_command_count') if choice == 'turn': count = self.getState('turn_count') template = ''' {Turn}\n ''' templateVars = { 'Turn': self.expand( 'Turn', {'id': 'turn_{}'.format(count)}, ), } elif choice == 'move': count = self.getState('move_count') template = ''' {Move}\n ''' templateVars = { 'Move': self.expand( 'Move', {'id': 'move_{}'.format(count)}, ), } else: raise Exception('how did you get here?') return gu.format(template, templateVars)
def renderCode(self): # keep track of variables declared before the loop self.addState('forloop-pre-vars', []) # this has to be precalculated so it can be used in other steps v = self.expand('LoopIndex') self.addState('forloop-i', v) templateVars = { 'v' : v, 'comp': self.getChoice('countUp-compOpp'), 'endVal' : self.expand('ForUpEndVal'), 'inc' : self.expand('Increment', {'varName':v}), 'varType' : self.expand('IntType'), # this non terminal defines the strategy of using the loop 'forCountUpBody' : self.expand('ForCountUpBody'), 'predeclardVars': self.renderPredeclaredVars() } template = """ {predeclardVars} for({varType} {v} = 0; {v} {comp} {endVal}; {inc}) {{ {forCountUpBody} }} """ return gu.format(template, templateVars)
def renderCode(self): choice = self.getChoice(self.getKey()) self.incrementState('turn_count') turn_amount_count = self.getState('turn_amount_count') if choice == 'left': template = ''' TurnLeft({TurnAmount}) \n ''' templateVars = { 'TurnAmount': self.expand( 'TurnAmount', {'id': 'turn_amount_{}'.format(turn_amount_count)}), } elif choice == 'right': template = ''' TurnRight({TurnAmount}) \n ''' templateVars = { 'TurnAmount': self.expand('TurnAmount', {'id': 'turn_amount_{}'.format(turn_amount_count)}) } else: raise Exception('how did you get here?') return gu.format(template, templateVars)
def renderCode(self): # many random decisions can appear multiple times in a # trajectory. We thus need to keep track of counts. self.addState('for_loop_params_count', 0) self.addState('single_shape_count', 0) self.addState('random_count', 0) self.addState('repeat_num_count', 0) self.addState('start_value_count', 0) self.addState('end_value_count', 0) self.addState('increment_count', 0) self.addState('pixel_start_value_count', 0) self.addState('pixel_end_value_count', 0) self.addState('pixel_increment_count', 0) self.addState('body_count', 0) self.addState('single_body_count', 0) self.addState('move_count', 0) self.addState('turn_count', 0) self.addState('move_amount_count', 0) self.addState('turn_amount_count', 0) self.addState('extra_loop_count', 0) self.addState('extra_command_count', 0) templateVars = { 'MultipleShapes': self.expand('MultipleShapes'), } template = ''' Program {MultipleShapes} ''' return gu.format(template, templateVars)
def renderCode(self): choice = self.getChoice(self.getKey()) self.incrementState('move_count') move_amount_count = self.getState('move_amount_count') if choice == 'backward': template = ''' MoveBackwards({MoveAmount}) \n ''' templateVars = { 'MoveAmount': self.expand( 'MoveAmount', {'id': 'move_amount_{}'.format(move_amount_count)} ), } elif choice == 'forward': template = ''' Move({MoveAmount}) \n ''' templateVars = { 'MoveAmount': self.expand( 'MoveAmount', {'id': 'move_amount_{}'.format(move_amount_count)} ), } else: raise Exception('how did you get here?') return gu.format(template, templateVars)
def renderCode(self): _id = int(self.getKey().split('_')[-1]) choice = self.getChoice(self.getKey()) self.incrementState('repeated_body_count') if choice == 'repeat': single_body_count = self.getState('single_body_count') template = ''' {SingleBody} {RepeatedBody} \n ''' templateVars = { 'SingleBody': self.expand( 'SingleBody', {'id': 'single_body_{}'.format(single_body_count)}), 'RepeatedBody': self.expand('RepeatedBody', {'id': 'repeated_body_{}'.format(_id + 1)}), } elif choice == 'none': template = "" templateVars = {} else: raise Exception('how did you get here?') return gu.format(template, templateVars)
def renderCode(self): v = self.params['varName'] c = self.getChoice('increment-style') templateVars = {'v': v} if c == '++': template = '{v}++' if c == '+=': template = '{v} += 1' if c == 'full': template = '{v} = {v} + 1' return gu.format(template, templateVars)
def innerTemp(self): tempVars = {'temp': 'x', 'calc': self.expand('ForUpInlineCalc')} template = ''' int {temp} = {calc}; println({temp}); ''' return gu.format(template, tempVars)
def renderCode(self): parts = { 'TurnDir': self.expand('TurnDir'), 'TurnAmt': self.expand('TurnAmt') } self.addState('firstTurn', False) code = 'Turn{TurnDir}({TurnAmt})' return gu.format(code, parts)
def renderCode(self): v = self.params['varName'] c = self.getChoice('decrement-style') templateVars = {'v': v} if c == '--': template = '{v}--' if c == '-=': template = '{v} -= 1' if c == 'full': template = '{v} = {v} - 1' return gu.format(template, templateVars)
def renderCode(self): ch = self.getChoice('zeroX') if ch == '0': return ch params = { 'd': self.expand('Diameter', {'id': 'diam'}), 'w': self.expand('Width') } return gu.format('({w} - 10 * {d}) / 2', params)
def renderCode(self): parts = { 'MoveDir': self.expand('MoveDir'), 'MoveAmt': self.expand('MoveAmt') } self.addState('firstMove', False) code = 'Move{MoveDir}({MoveAmt})' return gu.format(code, parts)
def renderCode(self): templateVars = { 'method': self.getChoice('liftoff-printMethod'), 'base': self.getChoice('liftoff-stringBase'), 'excite': self.getChoice('liftoff-stringExcitement') } template = '{method}("{base}{excite}");' return gu.format(template, templateVars)
def renderCode(self): ch = self.getChoice('zeroY') if ch == '0': return ch params = { 'd': self.expand('Diameter', {'id': 'diam'}), 'h': self.expand('Height') } return gu.format('({h} - {d}) / 2', params)
def renderCode(self): paramStr = self.expand('PrintVarParam', self.params) c = self.getChoice('println-function') templateVars = {'p': paramStr} if c == 'print': template = 'print({p});' if c == 'println': template = 'println({p});' if c == 'none': template = '' ret = gu.format(template, templateVars) return ret
def renderFRMT2(self): parts = self.getParts() code = '''{ForHeader}{{ Repeat({Iter}) {{ {Move} }} }} {Turn}''' return gu.format(code, parts)
def outerTemp(self): # add a preloop var definition self.getState('forloop-pre-vars').append('int x = 0;') tempVars = {'temp': 'temp', 'calc': self.expand('ForUpInlineCalc')} template = ''' {temp} = {calc}; println({temp}) ''' return gu.format(template, tempVars)
def renderTM(self): code = ''' {Turn} {Move} ''' parts = { 'Move': self.expand('MainMove'), 'Turn': self.expand('MainTurn') } return gu.format(code, parts)
def innerTemp(self): tempVars = { 'temp':'x', 'calc': self.expand('ForUpInlineCalc') } line = gu.format('int {temp} = {calc};', tempVars) self.getState('loop-start-lines').append(line) ret = self.expand('DrawCircleXY', {'x':'x'}) return ret
def renderMM(self): code = ''' {Move1} {Move2} ''' parts = { 'Move1': self.expand('MainMove'), 'Move2': self.expand('MainMove') } return gu.format(code, parts)
def renderCode(self): if self.getChoice('constant-starterCode'): return 'private static final int START = 10;' hasConst = self.hasChoice('usesConstant') and self.getChoice( 'usesConstant') if hasConst: name = self.getChoice('constName') return gu.format('private static final int {n} = 10;', {'n': name}) return ''
def makeMethod(self, method): # method is a tuple (name, body) name, body = method templateVars = {'name': name, 'body': body} template = ''' private void {name}() {{ {body} }} ''' return gu.format(template, templateVars)
def renderCode(self): choice = self.getChoice(self.getDecisionName()) choiceParts = choice.split(',') params = { 'Start':choiceParts[0], 'End':choiceParts[1], 'Delta':choiceParts[2] } code = 'For({Start}, {End}, {Delta})' self.incrementCount() return gu.format(code, params)
def renderCode(self): parts = { 'Body':self.expand('DrawShapeX'), 'ForHeader':self.expand('ForHeader') } code = '''{ForHeader}{{ {Body} }}''' return gu.format(code, parts)
def outerTemp(self): # add a preloop var definition tempVars = { 'temp':'x', 'calc': self.expand('ForUpInlineCalc') } line = gu.format('{temp} = {calc};', tempVars) self.getState('loop-start-lines').append(line) self.getState('loop-pre-vars').append('int x = {ZeroX};') return self.expand('DrawCircleXY', {'x':'x'})
def expandRepeat(self, x): code = ''' Repeat({x}){{ {body} }} ''' params = { 'x':x, 'body':'{Repeat'+ self.toString(x) + '}' } v = gu.format(code, params) return v
def renderCode(self): var = self.params['var'] prompts = { 'a': f'{var}=', 'b': f'{var}:', 'c': 'The answer is', 'd': 'The hypotenuse is ' } templateVars = { 'base': prompts[self.getChoice('outputPrompt')], } template = '{base}' return gu.format(template, templateVars)