MIPS parser using C# Pidgin, failing to parse a sequence of things

Viewed 416

I am trying to write a simple parser combinator to parse MIPS code using the Pidgin library but my SeparatedAndOptionallyTerminated parser always finds a single thing to the parser and no error is thrown. I am not sure what is going on.

I appreciate any help or hint.

var skipCommaP = Char(',').IgnoreResult();

var registerP = Char('$')
    .Then(Enums.GetValues<Register>().Select(x => x.Name())
        .Select(x => String(new string(x.Skip(1).ToArray())))
        .Aggregate((a, b) => a.Or(b))
        .Map(v => $"${v}".ToRegister()));

var loadImmediateP = Map(
    (a, b, c) => (Instruction) new LoadImmediate(b, c),
    String(LoadImmediate.Name).Between(SkipWhitespaces),
    registerP.Before(skipCommaP.Optional()).Between(SkipWhitespaces),
    Num.Between(SkipWhitespaces)
);

var moveP = Map(
    (a, b, c) => (Instruction) new Move(b, c),
    String(Move.Name).Between(SkipWhitespaces),
    registerP.Before(skipCommaP.Optional()).Between(SkipWhitespaces),
    registerP.Between(SkipWhitespaces)
);

var instructionsP = loadImmediateP.Or(moveP);

ProgramP = instructionsP.SeparatedAndOptionallyTerminated(EndOfLine);

Then using it:

const string code = "move $t0, $t0 \n" + 
                    "li $t0 555";

var result = new MipsParser().ProgramP.Parse(code);

// result.Value has single thing in it always.
Console.WriteLine(result);

My repo

0 Answers
Related