fix infinity in json stringify

This commit is contained in:
2023-03-15 16:48:47 +00:00
parent ebc3df801b
commit b53f2b4a0a
15 changed files with 471 additions and 461 deletions

View File

@@ -2,12 +2,14 @@ package main
import (
"bytes"
"fmt"
"io"
"os"
)
var ArFile = ArMap{
"read": builtinFunc{"read", ArRead},
"read": builtinFunc{"read", ArRead},
"write": builtinFunc{"write", ArWrite},
}
func readtext(file *os.File) (string, error) {
@@ -16,15 +18,15 @@ func readtext(file *os.File) (string, error) {
if err != nil {
return "", err
}
return string(buf.Bytes()), nil
return buf.String(), nil
}
func ArRead(args ...any) (any, ArErr) {
if len(args) == 0 {
return ArMap{}, ArErr{TYPE: "Runtime Error", message: "open takes 1 argument", EXISTS: true}
if len(args) != 1 {
return ArMap{}, ArErr{TYPE: "Runtime Error", message: "read takes 1 argument, got " + fmt.Sprint(len(args)), EXISTS: true}
}
if typeof(args[0]) != "string" {
return ArMap{}, ArErr{TYPE: "Runtime Error", message: "open takes a string not a '" + typeof(args[0]) + "'", EXISTS: true}
return ArMap{}, ArErr{TYPE: "Runtime Error", message: "read takes a string not type '" + typeof(args[0]) + "'", EXISTS: true}
}
filename := args[0].(string)
file, err := os.Open(filename)
@@ -44,11 +46,45 @@ func ArRead(args ...any) (any, ArErr) {
if err != nil {
return ArMap{}, ArErr{TYPE: "Runtime Error", message: err.Error(), EXISTS: true}
}
return parse(text), ArErr{}
}},
"line": builtinFunc{"line", func(...any) (any, ArErr) {
return "", ArErr{}
return jsonparse(text), ArErr{}
}},
}, ArErr{}
}
func ArWrite(args ...any) (any, ArErr) {
if len(args) != 1 {
return ArMap{}, ArErr{TYPE: "Runtime Error", message: "write takes 1 argument, got " + fmt.Sprint(len(args)), EXISTS: true}
}
if typeof(args[0]) != "string" {
return ArMap{}, ArErr{TYPE: "Runtime Error", message: "write takes a string not type '" + typeof(args[0]) + "'", EXISTS: true}
}
filename := args[0].(string)
file, err := os.Create(filename)
if err != nil {
return ArMap{}, ArErr{TYPE: "Runtime Error", message: err.Error(), EXISTS: true}
}
return ArMap{
"text": builtinFunc{"text", func(args ...any) (any, ArErr) {
if len(args) != 1 {
return ArMap{}, ArErr{TYPE: "Runtime Error", message: "text takes 1 argument, got " + fmt.Sprint(len(args)), EXISTS: true}
}
if typeof(args[0]) != "string" {
return ArMap{}, ArErr{TYPE: "Runtime Error", message: "text takes a string not type '" + typeof(args[0]) + "'", EXISTS: true}
}
file.Write([]byte(args[0].(string)))
return nil, ArErr{}
}},
"json": builtinFunc{"json", func(args ...any) (any, ArErr) {
if len(args) != 1 {
return ArMap{}, ArErr{TYPE: "Runtime Error", message: "json takes 1 argument, got " + fmt.Sprint(len(args)), EXISTS: true}
}
jsonstr, err := jsonstringify(args[0], 0)
if err != nil {
return ArMap{}, ArErr{TYPE: "Runtime Error", message: err.Error(), EXISTS: true}
}
file.Write([]byte(jsonstr))
return nil, ArErr{}
}},
}, ArErr{}
}