Store whatever the current command state is (e.g. character and number) and then keeping modifying that until you reach whatever is considered a new command, at which point the previous command is executed. E.g. 

0 => int cmdc;
float cmdnum;

for(...)
{
    if(letter == 'a')
    {
        // dispatch previous command
        if(cmdc)
            doCmd(cmdc, cmdnum);
        // store new command
        letter => cmdc;
        0 => cmdnum;
    }
    // further down ...
    // if is number
    else if(letter >= 48 && letter <= 57)
    {
        cmdnum*10 + (letter-48) => cmdnum;
    }
}

doCmd plays whatever the command is and needs to use the appropriate default if cmdnum is 0. Also this doesn't deal with octave/length or anything like that but I imagine thats straightforward to add on. Probably most of the letter if-statements could be folded into a single if. You may need to keep additional state variables for the command if there are other properties besides pitch class and duration, like octave (and intensity?). 

spencer



On Wed, Sep 7, 2016 at 5:52 PM, Colton Hill <colton-hill2014@hotmail.com> wrote:

that's the issue. I would slowly go through the string and parse down every command, if I knew how to grab numbers and things between one command and the next. For example, c8.c8


On 9/7/2016 7:44 PM, Spencer Salazar wrote:
I dont think you can use regex to match a whole expression like that but you can use it to pick off bits and pieces. Just make a regex that matches all of the possible commands + options and run that through, advancing the string position each time. Eg

"o4l8cdefg" => string code;
string matches[0];
"((o)([0-9]+)|([abcdefg])(1|2|4|8|16)?|(l)([0-9]+))" => string pattern;

for(0 => int i; i < code.length(); )
{
    if(RegEx.match(pattern, code.substring(i), matches))
    {
        if(matches[2] == "o")
            <<< "octave:", matches[3] >>>;
        else if(matches[4] == "a")
            <<< "a" >>>;
        // etc.
        
        matches[0].length() +=> i;
    }
    else
    {
        <<< "invalid code" >>>;
        break;
    }
}

I dont know anything about MML so I dont know what the final regex would look like. 

Another option is to just go through the string iteratively and look at each character one at a time. If whitespace is undesirable StringTokenizer might be hurting more than helping here. 

for(0 => int i; i < code.length(); )
{
    int c = code.charAt(i);
    if(c == 'o')
    {
         // look for number
    }
    else if(c == 'a' || c == 'b' || ... )
    {
         // see if number follows
    }
}

spencer



On Tue, Sep 6, 2016 at 6:32 PM, Colton Hill <colton-hill2014@hotmail.com> wrote:
I also want to make sure I have a customizable play function that will
handle my playing. Here's the code I've been using for my current mml
parser in chuck, actually does take a code string and a play function,
and operates well. Only issue is including doesn't work... So have a test.


On 9/6/2016 3:33 AM, Hans Åberg wrote:
>> On 5 Sep 2016, at 23:16, Colton Hill <colton-hill2014@hotmail.com> wrote:
>>
>> I know regular expressions syntax, but I really don't know how I would
>> manage to make an mml parser that actually works. Turn o4l8cdefg into
>> octave 4, length 8, and c d e f g notes with an 8th note length since no
>> length is specified. Then there's c4., which is c4^c8... Just bla…
> There are free MML parsers in C out there. Linking to ChucK, which is written in C++, might be a way.
>
>
> _______________________________________________
> chuck-users mailing list
> chuck-users@lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users


_______________________________________________
chuck-users mailing list
chuck-users@lists.cs.princeton.edu
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users




--
Spencer Salazar
Doctoral Candidate
Center for Computer Research in Music and Acoustics
Stanford University




_______________________________________________
chuck-users mailing list
chuck-users@lists.cs.princeton.edu
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users


_______________________________________________
chuck-users mailing list
chuck-users@lists.cs.princeton.edu
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users




--
Spencer Salazar
Doctoral Candidate
Center for Computer Research in Music and Acoustics
Stanford University

+1 831.277.4654