def run(): primitives() metacommands() modes() scales() instruments() ## test macros sg.new_macro('P', 'play') sg.new_macro('R', 'rest') sg.new_macro('HAT', 'p rp p lp p') sg.new_macro('STAIRS3', 'p rp p rp p rp') sg.new_macro('HATCMD', 'begingroup p rp p lp p endgroup') return True
def define_macro(self, string): # = is syntactic sugar for >> begingroup ... endgroup splitter = '>>' if '=' in string: splitter = '=' vals = string.split(splitter, 1) signature = vals[0].strip() if len(vals) == 2: body = vals[1].strip() if len(body) == 0: ex = SilicaSyntaxError( 'Syntax error in macro definition: macro has no body.') return SilicaEvent('exception', exception=ex) vals = signature.split('(', 1) name = vals[0].strip().upper() if not self.valid_name(name): ex = SilicaNameError('The name %s is invalid in this context.' % name) return SilicaEvent('exception', exception=ex) args = None if len(vals) == 2: arglist = vals[1].strip() if len(arglist) > 0: if arglist[-1] != ')': ex = SilicaSyntaxError( 'Syntax error in macro definition: unbounded arglist.') return SilicaEvent('exception', exception=ex) else: arglist = arglist[:-1] arglist = arglist.split(',') args = [a.strip() for a in arglist] if splitter == '=': body = 'begingroup ' + body + ' endgroup' sg.new_macro(name, body, args) return SilicaEvent('macro_def', message='Macro %s defined.' % name)
def define_macro(self, string): # = is syntactic sugar for >> begingroup ... endgroup splitter = '>>' if '=' in string: splitter = '=' vals = string.split(splitter, 1) signature = vals[0].strip() if len(vals) == 2: body = vals[1].strip() if len(body) == 0: ex = SilicaSyntaxError('Syntax error in macro definition: macro has no body.') return SilicaEvent('exception', exception=ex) vals = signature.split('(',1) name = vals[0].strip().upper() if not self.valid_name(name): ex = SilicaNameError('The name %s is invalid in this context.' % name) return SilicaEvent('exception', exception=ex) args = None if len(vals) == 2: arglist = vals[1].strip() if len(arglist) > 0: if arglist[-1] != ')': ex = SilicaSyntaxError('Syntax error in macro definition: unbounded arglist.') return SilicaEvent('exception', exception=ex) else: arglist = arglist[:-1] arglist = arglist.split(',') args = [a.strip() for a in arglist] if splitter == '=': body = 'begingroup ' + body + ' endgroup' sg.new_macro(name, body, args) return SilicaEvent('macro_def', message='Macro %s defined.' % name)