From aab804e8ffb8c1c7dd4f0de8a6ec72d9717f4697 Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 5 Apr 2023 08:38:07 +0100 Subject: [PATCH] Add assembler binary --- go/asm/asm.go | 7 +++++-- go/dev/asm/asm.go | 28 ++++++++++++++++++++++++++++ go/testdata/foo.fst | 7 +++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 go/dev/asm/asm.go create mode 100644 go/testdata/foo.fst diff --git a/go/asm/asm.go b/go/asm/asm.go index b13d6a7..6f5f6d4 100644 --- a/go/asm/asm.go +++ b/go/asm/asm.go @@ -104,7 +104,7 @@ var ( asmLexer = lexer.MustSimple([]lexer.SimpleRule{ {"Comment", `(?:#)[^\n]*\n?`}, {"Ident", `^[A-Z]+`}, - {"Sym", `[a-zA-Z]+`}, + {"Sym", `[a-zA-Z0-9_]+`}, {"Size", `[0-9]+`}, {"Whitespace", `[ \t\n\r]+`}, {"Quote", `["']`}, @@ -315,8 +315,11 @@ func parseNoarg(op vm.Opcode, arg Arg, w io.Writer) (int, error) { func Parse(s string, w io.Writer) (int, error) { rd := strings.NewReader(s) ast, err := asmParser.Parse("file", rd) - var rn int + if err != nil { + return 0, err + } + var rn int for _, v := range ast.Instructions { op := vm.OpcodeIndex[v.OpCode] n, err := parseSized(op, v.OpArg, w) diff --git a/go/dev/asm/asm.go b/go/dev/asm/asm.go new file mode 100644 index 0000000..79f2bcb --- /dev/null +++ b/go/dev/asm/asm.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "os" + + "git.defalsify.org/festive/asm" +) + +func main() { + if (len(os.Args) < 2) { + os.Exit(1) + } + fp := os.Args[1] + v, err := ioutil.ReadFile(fp) + if err != nil { + fmt.Fprintf(os.Stderr, "read error: %v", err) + os.Exit(1) + } + n, err := asm.Parse(string(v), os.Stdout) + if err != nil { + fmt.Fprintf(os.Stderr, "parse error: %v", err) + os.Exit(1) + } + log.Printf("parsed total %v bytes", n) +} diff --git a/go/testdata/foo.fst b/go/testdata/foo.fst new file mode 100644 index 0000000..f587b32 --- /dev/null +++ b/go/testdata/foo.fst @@ -0,0 +1,7 @@ +MOUT 0 "to foo" +MOUT 1 "go bar" +LOAD inky 20 +HALT +INCMP 0 _ +INCMP 1 baz +CATCH _catch 1 1