def blockify(category, text, flag, command, defaults): if command in IGNORE_COMMANDS: return shape = SHAPE_FLAGS[flag] if text in ('stop script', 'stop all', 'forever', 'forever if %b'): shape = 'cap' defaults = OVERRIDE_DEFAULTS.get(command, defaults) parts = [] for part in filter(None, INSERT_RE.split(text)): if INSERT_RE.match(part): default = defaults.pop(0) if defaults else None if isinstance(default, Symbol): default = default.value kind = INSERT_KINDS.get(part) part = kurt.Insert(INSERT_SHAPES[part], kind, default=default) parts.append(part) match = MATCH_COMMANDS.get(command) # c & e blocks if command == "doIfElse": parts += [kurt.Insert("stack"), "else", kurt.Insert("stack")] elif flag == "c": parts += [kurt.Insert("stack")] pbt = kurt.PluginBlockType(category, shape, command, parts, match=match) # fix insert kinds if command == 'getAttribute:of:': pbt.inserts[1].kind = 'spriteOrStage' elif command == 'touching:': pbt.inserts[0].kind = 'touching' elif command == 'showBackground:': pbt.inserts[0].kind = 'backdrop' # fix unevaluated inserts if pbt.text in ("wait until %s", "repeat until %s%s", "forever if %s%s"): pbt.inserts[0].unevaluated = True return pbt
def blockify(blockspec): if len(blockspec) > 1: (spec, flag, category_id, command) = blockspec[:4] defaults = blockspec[4:] shape = SHAPE_FLAGS[flag] category = CATEGORY_IDS[category_id] parts = list(parse_spec(spec, defaults)) if "c" in flag: parts += [kurt.Insert("stack")] elif "e" in flag: parts += [kurt.Insert("stack"), "else", kurt.Insert("stack")] pbt = kurt.PluginBlockType(category, shape, command, parts) if pbt.text in ("wait until %s", "repeat until %s%s", "forever if %s%s"): pbt.inserts[0].unevaluated = True return pbt else: return None
#override the version in kurt.scratch14.blocks squeak_blockspecs = """'control' ('when green flag clicked' #S #EventHatMorph) ('when %k key pressed' #K #KeyEventHatMorph) ('when %m clicked' #M #MouseClickEventHatMorph) #- ('wait %n secs' #t #wait:elapsed:from: 1) #- ('forever' #c #doForever) ('repeat %n' #c #doRepeat 10) #- ('broadcast %e' #- #broadcast:) ('broadcast %e and wait' #s #doBroadcastAndWait) ('when I receive %e' #E #EventHatMorph) #- ('forever if %b' #c #doForeverIf) ('if %b' #c #doIf) ('if %b' #c #doIfElse) ('wait until %b' #s #doWaitUntil) ('repeat until %b' #c #doUntil) #- ('stop script' #s #doReturn) ('stop all' #- #stopAll) 'operators' ('%n + %n' #r #+ #- #-) ('%n - %n' #r #- #- #-) ('%n * %n' #r #* #- #-) ('%n / %n' #r #/ #- #-) #- ('pick random %n to %n' #r #randomFrom:to: 1 10) #- ('%s < %s' #b #< '' '') ('%s = %s' #b #= '' '') ('%s > %s' #b #> '' '') #- ('%b and %b' #b #&) ('%b or %b' #b #|) ('not %b' #b #not) #- ('join %s %s' #r #concatenate:with: 'hello ' 'world') ('letter %n of %s' #r #letter:of: 1 'world') ('length of %s' #r #stringLength: 'world') #- ('%n mod %n' #r #\\\\ #- #-) ('round %n' #r #rounded #-) #- ('%f of %n' #r #computeFunction:of: 'sqrt' 10) 'sound' ('say %s and play $S' #- #say:withSound:elapsed:from:) ('play sound %S' #- #playSound:) ('play sound %S until done' #s #doPlaySoundAndWait) ('stop all sounds' #- #stopAllSounds) #- ('play drum %D for %n beats' #t #drum:duration:elapsed:from: 48 0.2) ('rest for %n beats' #t #rest:elapsed:from: 0.2) #- ('play note %N for %n beats' #t #noteOn:duration:elapsed:from: 60 0.5) ('set instrument to %I' #- #midiInstrument: 1) #- ('change volume by %n' #- #changeVolumeBy: -10) ('set volume to %n%' #- #setVolumeTo: 100) ('volume' #r #volume) #- ('change tempo by %n' #- #changeTempoBy: 20) ('set tempo to %n bpm' #- #setTempoTo: 60) ('tempo' #r #tempo) 'motor' ('motor on for %n secs' #t #motorOnFor:elapsed:from: 1) ('motor on' #- #allMotorsOn) ('motor off' #- #allMotorsOff) ('motor power %n' #- #startMotorPower: 100) ('motor direction %W' #- #setMotorDirection: 'this way') 'variables' ('show variable %v' #- #showVariable:) ('hide variable %v' #- #hideVariable:) 'list' ('add %s to %L' #- #append:toList: 'thing') #- ('delete %y of %L' #- #deleteLine:ofList: 1) ('insert %s at %i of %L' #- #insert:at:ofList: 'thing' 1) ('replace item %i of %L with %s' #- #setLine:ofList:to: 1 'list' 'thing') #- ('item %i of %L' #r #getLine:ofList: 1) ('length of %L' #r #lineCountOfList:) ('%L contains %s' #b #list:contains: 'list' 'thing')""" squeak_sprite_blockspecs = """'motion' ('move %n steps' #- #forward:) ('glide %n steps' #t #forward:elapsed:from: 50) ('%n glide %n steps' #t #speed:forward:elapsed:from: 50) ('turn cw %n degrees' #- #turnRight: 90) ('turn ccw %n degrees' #- #turnLeft: 90) #- ('point in direction %d' #- #heading: 90) ('point towards %m' #- #pointTowards:) #- ('go to x:%n y:%n' #- #gotoX:y: 0 0) ('go to %m' #- #gotoSpriteOrMouse:) ('glide %n secs to x:%n y:%n' #t #glideSecs:toX:y:elapsed:from: 1 50 50) ('glide to %m' #- #glidetoSpriteOrMouse:elapsed:from:) ('glide %n to %m' #- #glide:toSpriteOrMouse:elapsed:from:) #- ('change x by %n' #- #changeXposBy: 10) ('set x to %n' #- #xpos: 0) ('change y by %n' #- #changeYposBy: 10) ('set y to %n' #- #ypos: 0) #- ('if on edge, bounce' #- #bounceOffEdge) #- ('x position' #r #xpos) ('y position' #r #ypos) ('direction' #r #heading) 'pen' ('clear' #- #clearPenTrails) #- ('pen down' #- #putPenDown) ('pen up' #- #putPenUp) #- ('set pen color to %c' #- #penColor:) ('change pen color by %n' #- #changePenHueBy:) ('set pen color to %n' #- #setPenHueTo: 0) #- ('change pen shade by %n' #- #changePenShadeBy:) ('set pen shade to %n' #- #setPenShadeTo: 50) #- ('change pen size by %n' #- #changePenSizeBy: 1) ('set pen size to %n' #- #penSize: 1) #- ('stamp' #- #stampCostume) 'looks' ('switch to costume %l' #- #lookLike:) ('next costume' #- #nextCostume) ('costume #' #r #costumeIndex) #- ('say %s for %n secs' #t #say:duration:elapsed:from: 'Hello!' 2) ('say %s' #- #say: 'Hello!') ('think %s for %n secs' #t #think:duration:elapsed:from: 'Hmm...' 2) ('think %s' #- #think: 'Hmm...') #- ('change %g effect by %n' #- #changeGraphicEffect:by: 'color' 25) ('set %g effect to %n' #- #setGraphicEffect:to: 'color' 0) ('clear graphic effects' #- #filterReset) #- ('change size by %n' #- #changeSizeBy:) ('set size to %n%' #- #setSizeTo: 100) ('size' #r #scale) #- ('show' #- #show) ('hide' #- #hide) #- ('go to front' #- #comeToFront) ('go back %n layers' #- #goBackByLayers: 1) 'sensing' ('touching %m?' #b #touching:) ('touching color %C?' #b #touchingColor:) ('color %C is touching %C?' #b #color:sees:) #- ('ask %s and wait' #s #doAsk 'What''s your name?') ('answer' #r #answer) #- ('mouse x' #r #mouseX) ('mouse y' #r #mouseY) ('mouse down?' #b #mousePressed) #- ('key %k pressed?' #b #keyPressed: 'up arrow') #- ('distance to %m' #r #distanceTo:) #- ('reset timer' #- #timerReset) ('timer' #r #timer) #- ('%a of %m' #r #getAttribute:of:) #- ('loudness' #r #soundLevel) ('loud?' #b #isLoud) #~ ('%H sensor value' #r #sensor: 'slider') ('sensor %h?' #b #sensorPressed: 'button pressed')""" # noqa block_list = ( list(make_blocks(squeak_blockspecs)) + list(make_blocks(kurt.scratch14.blockspecs_src.squeak_stage_blockspecs)) + list(make_blocks(squeak_sprite_blockspecs)) + list(make_blocks(kurt.scratch14.blockspecs_src.squeak_obsolete_blockspecs)) ) #taken directly from kurt.scratch14 block_list += [ # variable reporters kurt.PluginBlockType('variables', 'reporter', 'readVariable', [kurt.Insert('inline', 'var', default='var')]), kurt.PluginBlockType('variables', 'reporter', 'contentsOfList:', [kurt.Insert('inline', 'list', default='list')]), # Blocks with different meaning depending on arguments are special-cased # inside load_block/save_block. kurt.PluginBlockType('control', 'hat', 'whenGreenFlag', ['when green flag clicked']), kurt.PluginBlockType('control', 'hat', 'whenIReceive', ['when I receive ', kurt.Insert('readonly-menu', 'broadcast')]), # changeVariable is special-cased (and isn't in blockspecs) kurt.PluginBlockType('variables', 'stack', 'changeVar:by:', ['change ', kurt.Insert('readonly-menu', 'var'), ' by ', kurt.Insert('number')]),