Adds env-get and env-set

This commit is contained in:
sloum 2023-12-23 15:50:49 -08:00
parent 18dc864340
commit 5799e071ed
4 changed files with 41 additions and 3 deletions

View File

@ -61,4 +61,6 @@ var kwDocstrings = map[string]string {
"filter!": "Docstring coming soon.",
"time": "Stack:\nRead :\nPush : INT\nNotes: Adds the current time, as a unix timestamp INT, to TOS.",
"char-conv": "Stack: INT/STRING\nRead :\nPush : STRING/INT\nNotes: If an INT is TOS converts the INT as a rune to a char STRING (single character based on the rune number), if a STRING is TOS converts the first character of the STRING to an INT as a rune value. Will error on empty STRING or negative INT.",
"get-env": "Stack: STRING\nRead :\nPush : STRING\nNotes: Takes a STRING representing an envirnment variable and returns the value of that environment variable. If the variable could not be found, an empty STRING is returned.",
"set-env": "Stack: STRING ANY\nRead :\nPush : STRING\nNotes: Takes a STRING representing an envirnment variable and then an ANY representing a value (which well be stringified) to set to the variable. If the variable could not be set an error will be raised.",
}

View File

@ -118,7 +118,7 @@ func isKeyword(s string) bool {
"file-exists?", "file-read", "docstring!", "input", "re-match?",
"re-find", "re-replace", "slice", "stackdepth", "net-get", "try",
"catch", "throw", "import", "rot", "each!", "filter!", "words", "time",
"char-conv":
"char-conv", "env-get", "env-set":
return true
default:
return false

View File

@ -134,6 +134,10 @@ func callKeyword(kw token, r *tokenReader, en *env) error {
return libTime(kw.line, kw.file)
case "char-conv":
return libToChar(kw.line, kw.file)
case "env-get":
return libEnvGet(kw.line, kw.file)
case "env-set":
return libEnvSet(kw.line, kw.file)
default:
return fmt.Errorf("Unknown keyword: `%s`", kw.line, kw.val.(string))
}
@ -1539,3 +1543,34 @@ func libToChar(line int, fp string) error {
}
return fmt.Errorf("Cannot convert %s to or from a char", kindToString(t.kind))
}
func libEnvGet(line int, fp string) error {
t, err := globalStack.Pop()
if err != nil {
return err
}
if t.kind != STRING {
return fmt.Errorf("Non-STRING value on TOS")
}
v := os.Getenv(t.val.(string))
return globalStack.Push(token{STRING, v, line, fp})
}
func libEnvSet(line int, fp string) error {
t, err := globalStack.Pop()
if err != nil {
return err
}
if t.kind != STRING {
return fmt.Errorf("Non-STRING value on TOS")
}
val, err := globalStack.Pop()
if err != nil {
return err
}
err = os.Setenv(t.val.(string), toString(val, false))
if err != nil {
return err
}
return nil
}

View File

@ -28,7 +28,7 @@ import (
)
const (
version float64 = 0.33
version float64 = 0.34
logo string = ` _
|, _ |. __ _
| (/,||_) (/,`
@ -47,7 +47,8 @@ var completions = []string{
"file-exists?", "file-read", "docstring!", "input", "re-match?",
"re-find", "re-replace", "slice", "stackdepth", "net-get", "try", "throw",
"catch", "import", "rot", "each!", "filter!", "INT", "STRING", "FLOAT",
"LIST", "BOOL", "TYPE", "words", "time", "char-conv", "end",
"LIST", "BOOL", "TYPE", "words", "time", "char-conv", "end", "env-get",
"env-set",
}
//go:embed lib/std.fe