diff --git a/cmd/bfg/main.go b/cmd/bfg/main.go index f304c35..f275782 100644 --- a/cmd/bfg/main.go +++ b/cmd/bfg/main.go @@ -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) diff --git a/parser/execute_test.go b/parser/execute_test.go index a1103f6..7c2d820 100644 --- a/parser/execute_test.go +++ b/parser/execute_test.go @@ -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( `+> >+> >+> >+> > >++++++++ [ diff --git a/parser/compile.go b/parser/tokenise.go similarity index 83% rename from parser/compile.go rename to parser/tokenise.go index 896b7ee..57981db 100644 --- a/parser/compile.go +++ b/parser/tokenise.go @@ -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}) @@ -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 { @@ -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] @@ -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 } diff --git a/parser/compile_test.go b/parser/tokenise_test.go similarity index 88% rename from parser/compile_test.go rename to parser/tokenise_test.go index 0344680..a56a3ab 100644 --- a/parser/compile_test.go +++ b/parser/tokenise_test.go @@ -8,7 +8,7 @@ import ( "testing" ) -func TestCompile(t *testing.T) { +func TestTokenise(t *testing.T) { table := []struct { name string code string @@ -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) { @@ -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 @@ -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) {