Add asm parser

This commit is contained in:
lash 2023-04-04 10:38:01 +01:00
parent 664eab98d9
commit d3fb782a8c
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 22 additions and 1 deletions

View File

@ -7,8 +7,11 @@ import (
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
"git.defalsify.org/festive/vm"
)
type Asm struct {
Instructions []*Instruction `@@*`
}
@ -112,7 +115,8 @@ func Parse(s string, w io.Writer) (int, error) {
rd := strings.NewReader(s)
ast, err := asmParser.Parse("file", rd)
for i, v := range ast.Instructions {
fmt.Printf("%v %v\n", i, v)
op := vm.OpcodeIndex[v.OpCode]
fmt.Printf("%v (%v) %v\n", i, op, v)
}
return 0, err
}

View File

@ -38,4 +38,21 @@ var (
MNEXT: "MNEXT",
MPREV: "MPREV",
}
OpcodeIndex = map[string]Opcode {
"NOOP": NOOP,
"CATCH": CATCH,
"CROAK": CROAK,
"LOAD": LOAD,
"RELOAD": RELOAD,
"MAP": MAP,
"MOVE": MOVE,
"HALT": HALT,
"INCMP": INCMP,
"MSIZE": MSIZE,
"MOUT": MOUT,
"MNEXT": MNEXT,
"MPREV": MPREV,
}
)