From 5bb89a86a7bc9bd17d699d654bc40181d166febe Mon Sep 17 00:00:00 2001 From: William Bell Date: Sun, 25 Jun 2023 18:24:08 +0100 Subject: [PATCH] add path --- src/built-ins.go | 1 + src/file.go | 7 +++++ src/path.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++ src/term-class.go | 4 +-- 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/path.go diff --git a/src/built-ins.go b/src/built-ins.go index bfc019b..0bd8ab1 100644 --- a/src/built-ins.go +++ b/src/built-ins.go @@ -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) } diff --git a/src/file.go b/src/file.go index 1d97f2e..95ce4d6 100644 --- a/src/file.go +++ b/src/file.go @@ -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{} } diff --git a/src/path.go b/src/path.go new file mode 100644 index 0000000..63eff70 --- /dev/null +++ b/src/path.go @@ -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, + } + }}, + }) diff --git a/src/term-class.go b/src/term-class.go index 05ede44..0978ef2 100644 --- a/src/term-class.go +++ b/src/term-class.go @@ -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)) }