commit dfaf29bf00e52b69b8ed9e164822d55d4c91fbb0
parent cdd0f97d046e60946e0688294831493aebdc0f50
Author: lash <dev@holbrook.no>
Date: Mon, 26 Aug 2024 17:47:49 +0100
Add preprocessor step for the asm
Diffstat:
M | dev/asm/main.go | | | 77 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 77 insertions(+), 0 deletions(-)
diff --git a/dev/asm/main.go b/dev/asm/main.go
@@ -5,10 +5,78 @@ import (
"io/ioutil"
"log"
"os"
+ "strconv"
+ "strings"
+
+ "github.com/alecthomas/participle/v2"
+ "github.com/alecthomas/participle/v2/lexer"
"git.defalsify.org/vise.git/asm"
)
+
+type arg struct {
+ One *string `@Sym`
+ Two *string `((@Sym | @NumFirst) Whitespace?)?`
+ Three *string `((@Sym | @NumFirst) Whitespace?)?`
+ //Desc *string `(Quote ((@Sym | @Size) @Whitespace?)+ Quote Whitespace?)?`
+}
+
+type instruction struct {
+ OpCode string `@Ident`
+ OpArg arg `(Whitespace @@)?`
+ Comment string `Comment? EOL`
+}
+
+type asmAsm struct {
+ Instructions []*instruction `@@*`
+}
+
+func preProcess(b []byte) ([]byte, error) {
+ asmLexer := lexer.MustSimple([]lexer.SimpleRule{
+ {"Comment", `(?:#)[^\n]*`},
+ {"Ident", `^[A-Z]+`},
+ {"NumFirst", `[0-9][a-zA-Z0-9]*`},
+ {"Sym", `[a-zA-Z_\*\.\^\<\>][a-zA-Z0-9_]*`},
+ {"Whitespace", `[ \t]+`},
+ {"EOL", `[\n\r]+`},
+ {"Quote", `["']`},
+ })
+ asmParser := participle.MustBuild[asmAsm](
+ participle.Lexer(asmLexer),
+ participle.Elide("Comment", "Whitespace"),
+ )
+ ast, err := asmParser.ParseString("preprocessor", string(b))
+ if err != nil {
+ return nil, err
+ }
+
+ b = []byte{}
+ for _, v := range ast.Instructions {
+ s := []string{v.OpCode, *v.OpArg.One}
+ if v.OpCode == "CATCH" {
+ _, err := strconv.Atoi(*v.OpArg.Two)
+ if err != nil {
+ s = append(s, "42")
+ } else {
+ s = append(s, *v.OpArg.Two)
+ }
+ s = append(s, *v.OpArg.Three)
+ } else {
+ for _, r := range []*string{v.OpArg.Two, v.OpArg.Three} {
+ if r == nil {
+ break
+ }
+ s = append(s, *r)
+ }
+ }
+ b = append(b, []byte(strings.Join(s, " "))...)
+ b = append(b, 0x0a)
+ }
+
+ return b, nil
+}
+
func main() {
if (len(os.Args) < 2) {
os.Exit(1)
@@ -19,6 +87,15 @@ func main() {
fmt.Fprintf(os.Stderr, "read error: %v", err)
os.Exit(1)
}
+ log.Printf("start preprocessor")
+
+ v, err = preProcess(v)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "preprocess error: %v", err)
+ os.Exit(1)
+ }
+ log.Printf("preprocessor done")
+
n, err := asm.Parse(string(v), os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "parse error: %v", err)