Skip to content

Commit

Permalink
Added proper error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcla1 committed Aug 1, 2014
1 parent 6dae375 commit b66901b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
35 changes: 22 additions & 13 deletions baby/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import (
"regexp"
"strconv"
"strings"
"errors"
)

var lineRegex = regexp.MustCompile("[0-9]* *(NUM|JMP|JRP|LDN|STO|SUB|CMP|STP)( *)?(-?[0-9]+)?")

var (
ErrNonInstruction = errors.New("trying to execute non-instruction code")
ErrUnknownOpcode = errors.New("unknown opcode")
)

type Baby struct {
CurrentInstruction uint32
Accumulator uint32
Expand All @@ -18,10 +24,8 @@ type Baby struct {
Memory [32]uint32
}

func (b *Baby) Run() {
func (b *Baby) Run() error {
for {
// fmt.Printf("CI : %d\nACC: %d\n-----\n", int32(b.CurrentInstruction), int32(b.Accumulator))
// fmt.Scanln()
b.CurrentInstruction++

instr := b.Memory[int32(b.CurrentInstruction)%32]
Expand All @@ -43,21 +47,22 @@ func (b *Baby) Run() {
b.CurrentInstruction++
}
case 0x0000E000: // STP
return
return nil
default:
fmt.Println("malicious position:", b.CurrentInstruction)
panic("trying to execute non-instruction code!")
return ErrNonInstruction
}
}
}

func instrToOpCode(instr string) uint32 {
func instrToOpCode(instr string) (uint32, error) {
matches := lineRegex.FindStringSubmatch(instr)[1:]

if len(matches) < 1 {
panic("unknown opcode!")
return 0, ErrUnknownOpcode
}

// We ignore the possible error,
// to keep the code simpler.
opCode, _ := strconv.Atoi(matches[2])

switch matches[0] {
Expand All @@ -76,11 +81,12 @@ func instrToOpCode(instr string) uint32 {
case "STP":
opCode |= 0x0000E000
}
// fmt.Printf("%032b\n", uint32(opCode))
return uint32(opCode)

return uint32(opCode), nil
}

func MemoryFromString(prog string) [32]uint32 {
func MemoryFromString(prog string) ([32]uint32, error) {
var err error
lines := strings.Split(prog, "\n")

var memory [32]uint32
Expand All @@ -93,10 +99,13 @@ func MemoryFromString(prog string) [32]uint32 {
}

if !strings.HasPrefix(line, ";") {
memory[memoryIndex] = instrToOpCode(line)
memory[memoryIndex], err = instrToOpCode(line)
if err != nil {
return memory, err
}
memoryIndex++
}
}

return memory
return memory, nil
}
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package main

import (
"github.com/jcla1/gobaby"
"github.com/jcla1/gobaby/baby"
"io/ioutil"
"fmt"
)

func main() {
fc, err := ioutil.ReadFile("primegen.asm")
fc, err := ioutil.ReadFile("examples/primegen.asm")
if err != nil {
panic(err)
}

mem := baby.MemoryFromString(string(fc))
mem, err := baby.MemoryFromString(string(fc))
if err != nil {
panic(err)
}

b := baby.Baby{0, 0, mem}

Expand All @@ -22,6 +25,4 @@ func main() {
b.Accumulator = 0
b.CurrentInstruction = 0
}

fmt.Println("done!")
}

0 comments on commit b66901b

Please sign in to comment.