Skip to content

Commit

Permalink
Rename compile to tokenise.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Feb 21, 2024
1 parent ec3754a commit 030e984
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/bfg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func main() {
outputBuf := bufio.NewWriter(os.Stdout)
defer outputBuf.Flush()

program, err := parser.Compile(sourceBuf)
program, err := parser.Tokenise(sourceBuf)
if err != nil {
fmt.Println("error compiling program: err:", err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions parser/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func TestExecuteSmall(t *testing.T) {
}
}

func TestCompileExecuteSmall(t *testing.T) {
program, _ := Compile(bufio.NewReader(strings.NewReader(
func TestTokeniseExecuteSmall(t *testing.T) {
program, _ := Tokenise(bufio.NewReader(strings.NewReader(
`+> >+> >+> >+> >
>++++++++
[
Expand Down
10 changes: 5 additions & 5 deletions parser/compile.go → parser/tokenise.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"io"
)

// Compile sourcecode into a program
func Compile(input io.ByteReader) (program []Instruction, err error) {
// Tokenise sourcecode into an array of operators
func Tokenise(input io.ByteReader) (program []Instruction, err error) {
var pc, jmpPc int = 0, 0
jmpStack := make([]int, 0)
program = append(program, Instruction{opNoop, 0})
Expand All @@ -16,7 +16,7 @@ func Compile(input io.ByteReader) (program []Instruction, err error) {
if err == io.EOF {
break
} else if err != nil {
return nil, errors.New("compilation read error")
return nil, errors.New("tokenisation read error")
}
program = append(program, NewInstruction(chr))
switch program[pc].operator {
Expand All @@ -39,7 +39,7 @@ func Compile(input io.ByteReader) (program []Instruction, err error) {
jmpStack = append(jmpStack, pc)
case opJmpNz:
if len(jmpStack) == 0 {
return nil, errors.New("compilation error: unbalanced braces")
return nil, errors.New("tokenisation error: unbalanced braces")
}
jmpPc = jmpStack[len(jmpStack)-1]
jmpStack = jmpStack[:len(jmpStack)-1]
Expand All @@ -66,7 +66,7 @@ func Compile(input io.ByteReader) (program []Instruction, err error) {
pc++
}
if len(jmpStack) != 0 {
return nil, errors.New("compilation error: unexpected EOF")
return nil, errors.New("tokenisation error: unexpected EOF")
}
return
}
12 changes: 6 additions & 6 deletions parser/compile_test.go → parser/tokenise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
)

func TestCompile(t *testing.T) {
func TestTokenise(t *testing.T) {
table := []struct {
name string
code string
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestCompile(t *testing.T) {

for _, v := range table {
t.Run(v.name, func(t *testing.T) {
got, err := Compile(bufio.NewReader(strings.NewReader(v.code)))
got, err := Tokenise(bufio.NewReader(strings.NewReader(v.code)))
want := v.program

if !reflect.DeepEqual(got, want) {
Expand All @@ -107,7 +107,7 @@ func TestCompile(t *testing.T) {
}
}

func TestCompileError(t *testing.T) {
func TestTokeniseError(t *testing.T) {
table := []struct {
name string
code string
Expand All @@ -116,18 +116,18 @@ func TestCompileError(t *testing.T) {
{
"too_many_open",
"[[[",
errors.New("compilation error: unexpected EOF"),
errors.New("tokenisation error: unexpected EOF"),
},
{
"too_many_close",
"]]]",
errors.New("compilation error: unbalanced braces"),
errors.New("tokenisation error: unbalanced braces"),
},
}

for _, v := range table {
t.Run(v.name, func(t *testing.T) {
_, got := Compile(bufio.NewReader(strings.NewReader(v.code)))
_, got := Tokenise(bufio.NewReader(strings.NewReader(v.code)))
want := v.err

if !reflect.DeepEqual(got, want) {
Expand Down

0 comments on commit 030e984

Please sign in to comment.