This commit is contained in:
2023-06-25 18:24:08 +01:00
parent 6ff2d7c69f
commit 5bb89a86a7
4 changed files with 86 additions and 2 deletions

View File

@@ -335,5 +335,6 @@ func makeGlobal() ArObject {
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot get max of type '" + typeof(a[0]) + "'", EXISTS: true}
}}
vars["path"] = ArPath
return Map(vars)
}

View File

@@ -116,6 +116,13 @@ func ArRead(args ...any) (any, ArErr) {
}
return newNumber().SetInt64(info.Size()), ArErr{}
}},
"ModTime": builtinFunc{"ModTime", func(...any) (any, ArErr) {
info, err := file.Stat()
if err != nil {
return ArObject{}, ArErr{TYPE: "Runtime Error", message: err.Error(), EXISTS: true}
}
return ArTimeClass(info.ModTime()), ArErr{}
}},
}), ArErr{}
}

76
src/path.go Normal file
View File

@@ -0,0 +1,76 @@
package main
import (
"fmt"
"os"
"path"
)
var ArPath = Map(
anymap{
"ReadDir": builtinFunc{
"ReadDir",
func(args ...any) (any, ArErr) {
if len(args) != 1 {
return nil, ArErr{
TYPE: "runtime",
message: "ReadDir takes exactly 1 argument, got " + fmt.Sprint(len(args)),
EXISTS: true,
}
}
args[0] = ArValidToAny(args[0])
if typeof(args[0]) != "string" {
return nil, ArErr{
TYPE: "runtime",
message: "ReadDir argument must be a string, got " + typeof(args[0]),
EXISTS: true,
}
}
files, err := os.ReadDir(args[0].(string))
if err != nil {
return nil, ArErr{
TYPE: "runtime",
message: err.Error(),
EXISTS: true,
}
}
var ret []any
for _, file := range files {
ret = append(ret, file.Name())
}
return ret, ArErr{}
}},
"join": builtinFunc{
"join",
func(args ...any) (any, ArErr) {
if len(args) != 1 {
return nil, ArErr{
TYPE: "runtime",
message: "join takes exactly 1 argument, got " + fmt.Sprint(len(args)),
EXISTS: true,
}
}
args[0] = ArValidToAny(args[0])
switch arr := args[0].(type) {
case []any:
var Path []string
for _, x := range arr {
x = ArValidToAny(x)
if typeof(x) != "string" {
return nil, ArErr{
TYPE: "runtime",
message: "join argument must be an array of strings, got " + typeof(x),
EXISTS: true,
}
}
Path = append(Path, x.(string))
}
return path.Join(Path...), ArErr{}
}
return nil, ArErr{
TYPE: "runtime",
message: "join argument must be an array, got " + typeof(args[0]),
EXISTS: true,
}
}},
})

View File

@@ -80,7 +80,7 @@ var ArTerm = Map(anymap{
}},
}),
"error": builtinFunc{"error", func(args ...any) (any, ArErr) {
output := []any{"error: "}
output := []any{}
for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], false, true, 3, 0, false, 0))
}
@@ -89,7 +89,7 @@ var ArTerm = Map(anymap{
},
},
"warn": builtinFunc{"error", func(args ...any) (any, ArErr) {
output := []any{"warning: "}
output := []any{}
for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], false, true, 3, 0, false, 0))
}