Golang странная работа кода
я решил сделать мини яп и почему-то в кавычках когда я вызываю \" не видит закрывающую скобочку в конце.
Вот код:
package main
import (
"fmt"
"os"
"io"
"strings"
)
func main(){
if len(os.Args) < 2 {
fmt.Println("PointLang error: you not enter file name.")
os.Exit(1)
}
file, err := os.Open(os.Args[1])
if err != nil{
fmt.Println("PointLang error:", err)
os.Exit(1)
}
defer file.Close()
data := make([]byte, 64)
n, err := file.Read(data)
if err == io.EOF{ // если конец файла
os.Exit(1) // выходим из цикла
}
str := string(data[:n])
currentString := strings.Split(str, "\n")
for i := 0; i < len(currentString); i++ {
if strings.HasPrefix(currentString[i], "println") {
arg := currentString[i][7:]
if strings.HasPrefix(arg, "(") || strings.HasPrefix(arg, " ("){
if strings.HasPrefix(arg, "("){
arg = arg[1:]
} else {
arg = arg[2:]
}
symb := strings.Split(arg, "")
output := ""
textedR := false
endStr := 0
for j := 0; j < len(symb); j++ {
if symb[j] == ")" && textedR == false {
endStr = 1
break
} else if symb[j] == "\"" {
if textedR {
textedR = false
} else {
textedR = true
}
} else if symb[j] == "\\" && textedR == true {
j += 1
if symb[j] == "\"" {
output += symb[j]
}
} else if textedR {
output += symb[j]
}
}
if endStr == 0 {
fmt.Println("PointLang error on line ", i+1 ,": you forgot \")\" to end the function")
os.Exit(1)
}
fmt.Println(output)
} else {
fmt.Println("PointLang error on line ", i+1 ,": you forgot \"(\" to start the function")
os.Exit(1)
}
}
}
}
в коде скрипта самого языка
println("hello world")
println("test\"")