umx_asm

UMX virtual machine assembly compiler
git clone git://bsandro.tech/umx_asm
Log | Files | Refs | README | LICENSE

commit b118ea33b223c36cde9beffd2fbfa89ad04a5b77
parent 2ce447b3fc20c541aff0e02cac186460a57b45f7
Author: bsandro <email@bsandro.tech>
Date:   Wed,  6 Jul 2022 02:55:34 +0300

Small refactoring and one test addition:
- moved input string parsing into the Instruction
- added parsing validation test

Diffstat:
Masm/instruction.go | 5++++-
Masm/instruction_test.go | 18++++++++++++++++++
Mmain.go | 4+---
3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/asm/instruction.go b/asm/instruction.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "log" + "regexp" "strconv" "strings" ) @@ -14,7 +15,9 @@ type Instruction struct { Value uint32 } -func NewInstruction(input []string) *Instruction { +func NewInstruction(in string) *Instruction { + parsingRe := regexp.MustCompile(`[a-zA-Z0-9]+`) + input := parsingRe.FindAllString(in, -1) cnt := len(input) if cnt == 0 { log.Fatal("invalid input - array cannot be empty") diff --git a/asm/instruction_test.go b/asm/instruction_test.go @@ -19,3 +19,21 @@ func TestInstructionStringify(t *testing.T) { } } } + +func TestInstructionParse(t *testing.T) { + input := []struct { + in string + out string + }{ + {"cmov 2 3 4", "CMOV 2 3 4 "}, + {"orth 1 1020", "ORTH 1 1020"}, + {"outp 6", "OUTP 6 "}, + } + + for _, tt := range input { + ins := NewInstruction(tt.in) + if ins.String() != tt.out { + t.Fatalf(`test mismatch: "%s" instead of "%s"`, ins.String(), tt.out) + } + } +} diff --git a/main.go b/main.go @@ -7,7 +7,6 @@ import ( "log" "os" "path/filepath" - "regexp" "strings" "umx_asm/asm" ) @@ -17,7 +16,6 @@ const allowedExt = ".ums" const compileExt = ".um" func main() { - re := regexp.MustCompile(`[a-zA-Z0-9]+`) if len(os.Args) < 2 { log.Fatal("no assembly file supplied") } @@ -38,7 +36,7 @@ func main() { str, _, _ := strings.Cut(scanner.Text(), commentSym) str = strings.TrimSpace(str) if len(str) > 0 { - ins := asm.NewInstruction(re.FindAllString(str, -1)) + ins := asm.NewInstruction(str) buf := make([]byte, 4) binary.BigEndian.PutUint32(buf, ins.Pack()) bytecode = append(bytecode, buf...)