init
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
bin
|
||||
*.bin
|
||||
*.bfc
|
||||
23
README.md
Normal file
23
README.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# Brainfuck **!DRAFT!** 🚧
|
||||
|
||||
Интерпретатор для brainfuck, насписаный на golang с линтером и дополнительными функциями
|
||||
|
||||
> [!WARNING]
|
||||
> This is a WIP project, anything can be changed and/or broken without any notification beforehand
|
||||
|
||||
Новые функции:
|
||||
* Проект
|
||||
* сборка проекта из нескольких файлов
|
||||
* промежуточная компиляция в байт-код с дампом статических данных
|
||||
* сохранение/загрузка дампа ленты
|
||||
* кастомный размер ленты памяти
|
||||
* режим работы с UTF-8
|
||||
* Синтаксис
|
||||
* линтер кода
|
||||
* поддержка функций
|
||||
* поддержка коментариев
|
||||
* Другое
|
||||
* создание/чтение/изменение/удаление файлов
|
||||
* поддержка http[s] запросов
|
||||
* выполение команд в терминале
|
||||
* выполнение произвольного brainfuck кода
|
||||
28
brainfuck.yml
Normal file
28
brainfuck.yml
Normal file
@@ -0,0 +1,28 @@
|
||||
brainfuck:
|
||||
unicode: true
|
||||
memory_cells: 30000
|
||||
load_dump: 'data_in.bin'
|
||||
save_dump: 'data_out.bin'
|
||||
sorces:
|
||||
- 'example/hw.bf'
|
||||
compiled: 'bin/hw.bfc'
|
||||
lint:
|
||||
- brackets
|
||||
level: 'error'
|
||||
expected: '=1'
|
||||
- operators
|
||||
level: 'warning'
|
||||
expected: '=1'
|
||||
- loop_depth
|
||||
level: 'warning'
|
||||
expected: '<=100'
|
||||
- loop_depth
|
||||
level: 'error'
|
||||
expected: '<=255'
|
||||
- pointer_balance
|
||||
level: 'error'
|
||||
expected: '<0'
|
||||
- pointer_balance
|
||||
level: 'warning'
|
||||
expected: '>30000'
|
||||
|
||||
1
example/hw.bf
Normal file
1
example/hw.bf
Normal file
@@ -0,0 +1 @@
|
||||
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.,
|
||||
69
src/main.go
Normal file
69
src/main.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
const VersionCode = 1
|
||||
|
||||
func main() {
|
||||
cells := flag.Int("m", 30000, "cell count")
|
||||
unicode := flag.Bool("u", false, "use unicode")
|
||||
compile := flag.Bool("c", false, "compile to .bfc")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
args := flag.Args()
|
||||
var file string
|
||||
if len(args) > 0 {
|
||||
file = args[0]
|
||||
} else {
|
||||
fmt.Println("Ошибка: файл не указан.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Cells: %d\nUTF: %v\nCompile: %v\n", cells, unicode, compile)
|
||||
|
||||
firstArray := []byte{0x01, 0x02, 0x03, 0x04, 0x05}
|
||||
secondArray := []byte{0xAA, 0xBB, 0xCC}
|
||||
|
||||
save(file, firstArray, secondArray)
|
||||
load(file)
|
||||
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println(validateBrackets("[][][]"))
|
||||
fmt.Println(validateBrackets("[[[]]]"))
|
||||
fmt.Println(validateBrackets("[[[][]]]"))
|
||||
fmt.Println(validateBrackets("["))
|
||||
fmt.Println(validateBrackets("]"))
|
||||
fmt.Println(validateBrackets("]["))
|
||||
fmt.Println(validateBrackets("[]"))
|
||||
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println(validateOperators("+-<>-+.,[]"))
|
||||
fmt.Println(validateOperators("+-<>-+.,()"))
|
||||
fmt.Println(validateOperators("hello"))
|
||||
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println(validateLoopDepth("+++---"))
|
||||
fmt.Println(validateLoopDepth("+++[->+<]"))
|
||||
fmt.Println(validateLoopDepth("[[["))
|
||||
fmt.Println(validateLoopDepth("[]"))
|
||||
fmt.Println(validateLoopDepth("[[][]]"))
|
||||
fmt.Println(validateLoopDepth("[[[]][]]"))
|
||||
fmt.Println(validateLoopDepth("[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]"))
|
||||
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println(validatePointerBalance("+++---"))
|
||||
fmt.Println(validatePointerBalance("---+++"))
|
||||
fmt.Println(validatePointerBalance("++++++"))
|
||||
fmt.Println(validatePointerBalance("++++[]"))
|
||||
fmt.Println(validatePointerBalance("----[]"))
|
||||
fmt.Println(validatePointerBalance("+-+-[]"))
|
||||
}
|
||||
63
src/serialization.go
Normal file
63
src/serialization.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/binary"
|
||||
"os"
|
||||
)
|
||||
|
||||
func save(filepath string, data1 []byte, data2 []byte) error {
|
||||
file, err := os.Create(filepath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
err = binary.Write(file, binary.BigEndian, uint32(len(data1)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = file.Write(data1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = file.Write(data2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func load(filepath string) ([]byte, []byte, error) {
|
||||
file, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var data1ArraySize uint32
|
||||
err = binary.Read(file, binary.BigEndian, &data1ArraySize)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
data1Array := make([]byte, data1ArraySize)
|
||||
_, err = file.Read(data1Array)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
data2Array, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
data2Array = data2Array[4+data1ArraySize:]
|
||||
fmt.Printf("Размер первого массива: %d\n", data1ArraySize)
|
||||
fmt.Printf("Первый массив: %v\n", data1Array)
|
||||
fmt.Printf("Второй массив: %v\n", data2Array)
|
||||
return data1Array, data2Array, nil
|
||||
}
|
||||
71
src/validation.go
Normal file
71
src/validation.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
)
|
||||
|
||||
func validateBrackets(src string) int {
|
||||
count := 0
|
||||
for _, c := range src {
|
||||
if c == '[' {
|
||||
count++
|
||||
} else if c == ']' {
|
||||
count--
|
||||
if count < 0 {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
||||
if count == 0 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func validateOperators(src string) int {
|
||||
for i, c := range src {
|
||||
switch c {
|
||||
case '+', '-', '<', '>', '[', ']', ',', '.': break;
|
||||
default: return i;
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func validateLoopDepth(src string) int {
|
||||
if validateBrackets(src) == 0 {
|
||||
return -1
|
||||
}
|
||||
max_depth := 0
|
||||
current_depth := 0
|
||||
for _, c := range src {
|
||||
if c == '[' {
|
||||
current_depth++
|
||||
} else if c == ']' {
|
||||
current_depth--
|
||||
}
|
||||
max_depth = max(max_depth, current_depth)
|
||||
}
|
||||
return max_depth
|
||||
}
|
||||
|
||||
func validatePointerBalance(src string) int {
|
||||
max_index := 0
|
||||
pointer_index := 0
|
||||
for _, c := range src {
|
||||
if c == '[' || c == ']' {
|
||||
break
|
||||
} else if c == '+' {
|
||||
pointer_index++
|
||||
max_index = max(max_index, pointer_index)
|
||||
} else if c == '-' {
|
||||
pointer_index--
|
||||
return -1
|
||||
}
|
||||
}
|
||||
return max_index
|
||||
}
|
||||
|
||||
func validateSourceLength(src string) int {
|
||||
return len(src)
|
||||
}
|
||||
Reference in New Issue
Block a user