Start supporting numbers and strings

This commit is contained in:
2023-02-25 16:45:54 +00:00
parent cbae1c4629
commit 636101f1fa
15 changed files with 379 additions and 37 deletions

42
src/string.go Normal file
View File

@@ -0,0 +1,42 @@
package main
import (
"fmt"
"strconv"
"strings"
)
var stringCompile = makeRegex("(( *)\"((\\\\([a-z\\\"'`]))|[^\\\"])*\"( *))|(( *)'((\\\\([a-z\\'\"`]))|[^\\'])*'( *))")
func isString(code UNPARSEcode) bool {
return stringCompile.MatchString(code.code)
}
func unquoted(
str string,
) (string, error) {
str = strings.Trim(str, " ")
if str[0] == '\'' {
str = strings.Replace(str, "\\\"", "\"", -1)
str = strings.Replace(str, "\"", "\\\"", -1)
}
str = str[1 : len(str)-1]
str = strings.Replace(str, "\\'", "'", -1)
str = "\"" + str + "\""
return strconv.Unquote(str)
}
// returns translateString, success, error
func parseString(code UNPARSEcode) (translateString, bool, string) {
trim := strings.Trim(code.code, " ")
unquoted, err := unquoted(trim)
if err != nil {
return translateString{}, false, "Syntax Error: invalid string on line " + fmt.Sprint(code.line) + ": " + code.code
}
return translateString{
str: unquoted,
line: code.line,
}, true, ""
}