-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: pull request #2 from
raklaptudirm/vm
`core`: implement working virtual machine
- Loading branch information
Showing
8 changed files
with
240 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright © 2021 Rak Laptudirm <raklaptudirm@gmail.com> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"laptudirm.com/x/brainfuck/pkg/lexer" | ||
"laptudirm.com/x/brainfuck/pkg/parser" | ||
"laptudirm.com/x/brainfuck/pkg/token" | ||
"laptudirm.com/x/brainfuck/pkg/vm" | ||
) | ||
|
||
const Memory = 30000 | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Fprintln(os.Stderr, "usage: brainfuck [filename]") | ||
os.Exit(1) | ||
} | ||
|
||
filename := os.Args[1] | ||
file, err := os.ReadFile(filename) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
} | ||
|
||
var l lexer.Lexer | ||
l.Init(string(file), handleErr) | ||
|
||
var p parser.Parser | ||
p.Init(&l, handleErr) | ||
|
||
program := p.ParseProgram() | ||
|
||
if l.ErrorCount > 0 || p.ErrorCount > 0 { | ||
os.Exit(1) | ||
} | ||
|
||
m := vm.New(Memory) | ||
err = m.Execute(program) | ||
if err != nil { | ||
printErr(err.Error()) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func handleErr(p token.Position, e string) { | ||
printErr("%s: %v\n", p, e) | ||
} | ||
|
||
func printErr(format string, a ...interface{}) error { | ||
_, err := fmt.Fprintf(os.Stderr, format, a...) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/raklaptudirm/brainfuck | ||
module laptudirm.com/x/brainfuck | ||
|
||
go 1.16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright © 2021 Rak Laptudirm <raklaptudirm@gmail.com> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package vm | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"laptudirm.com/x/brainfuck/pkg/ast" | ||
"laptudirm.com/x/brainfuck/pkg/token" | ||
) | ||
|
||
type Machine struct { | ||
Length int | ||
Memory []byte | ||
Pointer int | ||
} | ||
|
||
func New(l int) *Machine { | ||
return &Machine{ | ||
Length: l, | ||
Memory: make([]byte, l), | ||
} | ||
} | ||
|
||
var ErrAst = errors.New("invalid ast structure") | ||
|
||
func (m *Machine) Execute(n ast.Node) error { | ||
switch v := n.(type) { | ||
case *ast.Program: | ||
return m.executeOperations(v.Operations) | ||
case *ast.Comment: | ||
return nil // ignore comments | ||
case *ast.Loop: | ||
if m.Memory[m.Pointer] == 0 { | ||
return nil | ||
} | ||
|
||
for { | ||
err := m.executeOperations(v.Operations) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if m.Memory[m.Pointer] == 0 { | ||
return nil | ||
} | ||
} | ||
case *ast.Operator: | ||
return m.executeOperator(v) | ||
default: | ||
return ErrAst | ||
} | ||
} | ||
|
||
func (m *Machine) executeOperations(operations []ast.Operation) error { | ||
for _, operation := range operations { | ||
err := m.Execute(operation) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *Machine) executeOperator(o *ast.Operator) error { | ||
switch o.Token { | ||
case token.INC_VAL: | ||
m.Memory[m.Pointer]++ | ||
case token.DEC_VAL: | ||
m.Memory[m.Pointer]-- | ||
case token.INC_PTR: | ||
m.Pointer++ | ||
case token.DEC_PTR: | ||
m.Pointer-- | ||
case token.INPUT: | ||
_, err := fmt.Scanf("%c", &m.Memory[m.Pointer]) | ||
return err | ||
case token.PRINT: | ||
fmt.Print(string(m.Memory[m.Pointer])) | ||
default: | ||
return fmt.Errorf("invalid operator %s", o.Token) | ||
} | ||
|
||
return nil | ||
} |