commit 199674cc3db52a5628cbe2ea07f005c2229d059a
parent 1a838bbe2d1ddf584bc3f5ebee46fa30ff2eedc0
Author: bsandro <email@bsandro.tech>
Date: Mon, 4 Jul 2022 02:02:29 +0300
no error propagation - log.Fatal() on failures
Diffstat:
2 files changed, 11 insertions(+), 17 deletions(-)
diff --git a/asm/instruction.go b/asm/instruction.go
@@ -2,8 +2,8 @@ package asm
import (
"bytes"
- "errors"
"fmt"
+ "log"
"strconv"
"strings"
)
@@ -14,10 +14,10 @@ type Instruction struct {
Value uint32
}
-func NewInstruction(input []string) (*Instruction, error) {
+func NewInstruction(input []string) *Instruction {
cnt := len(input)
if cnt == 0 {
- return nil, errors.New("invalid input - array cannot be empty")
+ log.Fatal("invalid input - array cannot be empty")
}
opcode := NewOpcode(strings.ToUpper(input[0]))
instr := Instruction{Opcode: opcode}
@@ -26,7 +26,7 @@ func NewInstruction(input []string) (*Instruction, error) {
switch opcode {
case CMOV, ARRI, ARRA, ADD, MUL, DIV, NOTA:
if cnt != 4 {
- return nil, errors.New(errmsg)
+ log.Fatal(errmsg)
}
instr.RegA = NewRegister(input[1])
instr.RegB = NewRegister(input[2])
@@ -34,28 +34,28 @@ func NewInstruction(input []string) (*Instruction, error) {
case HALT: // no operands
case ALLO, LOAD:
if cnt != 3 {
- return nil, errors.New(errmsg)
+ log.Fatal(errmsg)
}
instr.RegB = NewRegister(input[1])
instr.RegC = NewRegister(input[2])
case ABAN, OUTP, INP:
if cnt != 2 {
- return nil, errors.New(errmsg)
+ log.Fatal(errmsg)
}
instr.RegC = NewRegister(input[1])
case ORTH:
if cnt != 3 {
- return nil, errors.New(errmsg)
+ log.Fatal(errmsg)
}
val, err := strconv.ParseUint(input[2], 10, 32)
if err != nil {
- return nil, err
+ log.Fatal(err)
}
instr.RegA = NewRegister(input[1])
instr.Value = uint32(val)
}
- return &instr, nil
+ return &instr
}
func (i *Instruction) String() string {
diff --git a/main.go b/main.go
@@ -11,8 +11,6 @@ import (
func main() {
re := regexp.MustCompile(`[a-zA-Z0-9]+`)
- fmt.Println("umx_asm")
-
file, err := os.Open("sample.ums")
if err != nil {
fmt.Println(err)
@@ -24,12 +22,8 @@ func main() {
for scanner.Scan() {
str, _, _ := strings.Cut(scanner.Text(), ";")
if len(str) > 0 {
- ins, err := asm.NewInstruction(re.FindAllString(str, -1))
- if err == nil {
- fmt.Printf("%s\n", ins.String())
- } else {
- fmt.Printf("error: %q\n", err)
- }
+ ins := asm.NewInstruction(re.FindAllString(str, -1))
+ fmt.Println(ins.String())
}
}
if err := scanner.Err(); err != nil {