Add struct-handling to (?)
Move several functions from pebblisp.pbl to lib.pbl. pebblisp.pbl aliases replace the start of a command, rather than exact match.
This commit is contained in:
parent
d9d2bca8c8
commit
9415b27f41
12
src/env.c
12
src/env.c
|
@ -413,6 +413,18 @@ Object help(Object* params, int length, struct Environment* env)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < dictionary.structCount; i++) {
|
||||||
|
if (strcmp(dictionary.structDefs[i].name, symbol) == 0) {
|
||||||
|
char structDef[128] = {'{', ' ', '\0'};
|
||||||
|
for(int field = 0; field < dictionary.structDefs[i].fieldCount; field++) {
|
||||||
|
strcat(structDef, dictionary.structDefs[i].names[field]);
|
||||||
|
strcat(structDef, " ");
|
||||||
|
}
|
||||||
|
strcat(structDef, "}");
|
||||||
|
return nullTerminated(structDef);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct StrippedObject* object = fetch(symbol, env);
|
struct StrippedObject* object = fetch(symbol, env);
|
||||||
if (object) {
|
if (object) {
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
|
|
|
@ -1,26 +1,38 @@
|
||||||
#!/usr/bin/pl
|
#!/usr/bin/pl
|
||||||
; Print with newline
|
; Print with newline
|
||||||
(def prnl (fn (_txt) (
|
(def esc (ch 27))
|
||||||
(prn (cat _txt (ch 10)))
|
|
||||||
)))
|
|
||||||
|
|
||||||
(def loadfile (fn (file-name) (
|
(def string (fn (a) (cat "" a)))
|
||||||
|
|
||||||
|
(def nl (ch 10))
|
||||||
|
|
||||||
|
(def prnl (fn (_txt)
|
||||||
|
"Print with an appended newline." (
|
||||||
|
(prn (cat _txt nl))
|
||||||
|
)))
|
||||||
|
|
||||||
|
(def loadfile (fn (file-name)
|
||||||
|
"Read and immediately evaluate the file with the given path." (
|
||||||
(eval (rf file-name))
|
(eval (rf file-name))
|
||||||
)))
|
)))
|
||||||
|
|
||||||
; Exponentiate a^b
|
|
||||||
(def exp (fn (a b)
|
(def exp (fn (a b)
|
||||||
|
"Exponentiate a^b"
|
||||||
(if (= b 0)
|
(if (= b 0)
|
||||||
1
|
1
|
||||||
(* a (exp a (- b 1)))
|
(* a (exp a (- b 1)))
|
||||||
)
|
)
|
||||||
))
|
))
|
||||||
|
|
||||||
; Square a
|
(def sq (fn (a)
|
||||||
(def sq (fn (a) (* a a)))
|
"Square the given number"
|
||||||
|
(* a a)
|
||||||
|
))
|
||||||
|
|
||||||
; Cube a
|
(def cube (fn (a)
|
||||||
(def cube (fn (a) (exp a 3)))
|
"Cube the given number"
|
||||||
|
(exp a 3)
|
||||||
|
))
|
||||||
|
|
||||||
(def fib (fn (a)
|
(def fib (fn (a)
|
||||||
(if (< a 2)
|
(if (< a 2)
|
||||||
|
@ -28,11 +40,76 @@
|
||||||
(+ (fib (- a 1)) (fib (- a 2)))
|
(+ (fib (- a 1)) (fib (- a 2)))
|
||||||
)))
|
)))
|
||||||
|
|
||||||
; Return the larger of the two
|
(def max (fn (a b)
|
||||||
(def max (fn (a b) (if (> a b) a b)))
|
"Return the larger of the given values"
|
||||||
|
(if (> a b) a b)
|
||||||
|
))
|
||||||
|
|
||||||
; Return the smaller of the two
|
(def min (fn (a b)
|
||||||
(def min (fn (a b) (if (< a b) a b)))
|
"Return the smaller of the given values"
|
||||||
|
(if (< a b) a b)
|
||||||
|
))
|
||||||
|
|
||||||
|
(def for-each (fn (action list)
|
||||||
|
"Apply the given action to each element in list." (
|
||||||
|
(map f list)
|
||||||
|
""
|
||||||
|
)))
|
||||||
|
|
||||||
|
(struct Output (stdout stderr))
|
||||||
|
|
||||||
|
(def run (fn (command)
|
||||||
|
"Run the given shell command and return an Output struct" (
|
||||||
|
(def stdout "/tmp/pebblisp.stdout")
|
||||||
|
(def stderr "/tmp/pebblisp.stderr")
|
||||||
|
(def piped (cat command " > " stdout " 2> " stderr))
|
||||||
|
(sys piped)
|
||||||
|
(Output (rf stdout) (rf stderr))
|
||||||
|
)))
|
||||||
|
|
||||||
|
(def stdout (fn (command)
|
||||||
|
"Run the given shell command and return the stdout as a string" (
|
||||||
|
(def output (run command))
|
||||||
|
output.stdout
|
||||||
|
)))
|
||||||
|
|
||||||
|
(def stderr (fn (command)
|
||||||
|
"Run the given shell command and return the stderr as a string" (
|
||||||
|
(def output (run command))
|
||||||
|
output.stderr
|
||||||
|
)))
|
||||||
|
|
||||||
|
(def first (fn (list)
|
||||||
|
"Get the first element of the given list"
|
||||||
|
(at 0 list)
|
||||||
|
))
|
||||||
|
|
||||||
|
(def second (fn (list)
|
||||||
|
"Get the second element of the given list"
|
||||||
|
(at 1 list)
|
||||||
|
))
|
||||||
|
|
||||||
|
(def third (fn (list)
|
||||||
|
"Get the third element of the given list"
|
||||||
|
(at 2 list)
|
||||||
|
))
|
||||||
|
|
||||||
|
(def loadfile (fn (file-name)
|
||||||
|
"Read and evaluate the file with the given name." (
|
||||||
|
(eval (rf file-name))
|
||||||
|
)))
|
||||||
|
|
||||||
|
(def startsWith (fn (text pattern) (
|
||||||
|
(matches text (cat pattern "*"))
|
||||||
|
)))
|
||||||
|
|
||||||
|
(def contains (fn (text pattern) (
|
||||||
|
(matches text (cat "*" pattern "*"))
|
||||||
|
)))
|
||||||
|
|
||||||
|
(def endsWith (fn (text pattern) (
|
||||||
|
(matches text (cat "*" pattern))
|
||||||
|
)))
|
||||||
|
|
||||||
; Switch expression
|
; Switch expression
|
||||||
; Doesn't yet work with lambdas
|
; Doesn't yet work with lambdas
|
||||||
|
|
|
@ -9,67 +9,33 @@
|
||||||
|
|
||||||
(def bold "[1m")
|
(def bold "[1m")
|
||||||
(def reset "[0m")
|
(def reset "[0m")
|
||||||
|
|
||||||
(def esc (ch 27))
|
|
||||||
|
|
||||||
(def nl (ch 10))
|
|
||||||
|
|
||||||
(def fore (fn (action list)
|
|
||||||
"Apply the given action to each element in list." (
|
|
||||||
(map f list)
|
|
||||||
""
|
|
||||||
)))
|
|
||||||
|
|
||||||
(struct Output (stdout stderr))
|
|
||||||
(def run (fn (command)
|
|
||||||
"Run the given shell command and return an Output struct" (
|
|
||||||
(def stdout "/tmp/pebblisp.stdout")
|
|
||||||
(def stderr "/tmp/pebblisp.stderr")
|
|
||||||
(def piped (cat command " > " stdout " 2> " stderr))
|
|
||||||
(sys piped)
|
|
||||||
(Output (rf stdout) (rf stderr))
|
|
||||||
)))
|
|
||||||
|
|
||||||
(def stdout (fn (command)
|
|
||||||
"Run the given shell command and return the stdout as a string" (
|
|
||||||
(def output (run command))
|
|
||||||
output.stdout
|
|
||||||
)))
|
|
||||||
|
|
||||||
(def stderr (fn (command)
|
|
||||||
"Run the given shell command and return the stderr as a string" (
|
|
||||||
(def output (run command))
|
|
||||||
output.stderr
|
|
||||||
)))
|
|
||||||
|
|
||||||
(def first (fn (list) (at 0 list)))
|
|
||||||
|
|
||||||
(def ~ (env "HOME"))
|
(def ~ (env "HOME"))
|
||||||
|
|
||||||
(def config (cat ~ "/.pebblisp.pbl"))
|
(def config (cat ~ "/.pebblisp.pbl"))
|
||||||
|
|
||||||
(def loadfile (fn (file-name)
|
|
||||||
"Read and evaluate the file with the given name." (
|
|
||||||
(eval (rf file-name))
|
|
||||||
)))
|
|
||||||
|
|
||||||
(def reloadConfig (fn () (loadfile config)))
|
(def reloadConfig (fn () (loadfile config)))
|
||||||
|
|
||||||
(def string (fn (a) (cat "" a)))
|
(def editConfig (fn () (
|
||||||
|
(sys (cat (env "EDITOR") " " config))
|
||||||
|
(reloadConfig)
|
||||||
|
)))
|
||||||
|
|
||||||
(struct Alias (name value))
|
(struct Alias (name value))
|
||||||
(def aliases (
|
(def aliases (
|
||||||
(Alias "ls" "ls --color")
|
(Alias "ls" "ls --color")
|
||||||
|
(Alias "vimc" "vim ~/.vimrc")
|
||||||
(Alias "alias" "(alias)")
|
(Alias "alias" "(alias)")
|
||||||
(Alias "pbl" (cat (env "EDITOR") " ~/.pebblisp.pbl"))
|
(Alias "pbl" "(editConfig)")
|
||||||
(Alias "tags" "ctags --exclude=node_modules -f newtags -R . && mv newtags tags")
|
(Alias "tags" "ctags --exclude=node_modules -f newtags -R . && mv newtags tags")
|
||||||
(Alias "r" "(reloadConfig)")
|
(Alias "r" "(reloadConfig)")
|
||||||
|
(Alias "cd ~" (cat "cd " ~))
|
||||||
|
(Alias "sudo" "echo -e '\e[1;31m' && sudo")
|
||||||
))
|
))
|
||||||
|
|
||||||
(def alias (fn ()
|
(def alias (fn ()
|
||||||
"Get a string that lists all current aliases." (
|
"Get a string that lists all current aliases." (
|
||||||
(reduce (map (fn (a) (cat nl a.name " -> " a.value)) aliases) cat "")
|
(reduce (map (fn (a) (cat nl a.name " -> " a.value)) aliases) cat "")
|
||||||
)))
|
)))
|
||||||
|
|
||||||
(def commRunning F)
|
(def commRunning F)
|
||||||
|
|
||||||
|
@ -85,12 +51,15 @@
|
||||||
)))
|
)))
|
||||||
|
|
||||||
(def preprocess (fn (text) (
|
(def preprocess (fn (text) (
|
||||||
(def matches (fil (fn (a) (= text a.name)) aliases))
|
(def alias-matches (fil (fn (a) (startsWith text a.name)) aliases))
|
||||||
(def match (first matches))
|
(def match (first alias-matches))
|
||||||
(if (iserr match) text match.value)
|
(if (iserr match) text
|
||||||
|
(cat match.value (substr (slen match.name) 999 text))
|
||||||
|
)
|
||||||
)))
|
)))
|
||||||
|
|
||||||
(def hour (fn (ti) (
|
(def hour (fn (ti)
|
||||||
|
"Get the hour 1-12 of the given Time struct." (
|
||||||
(def h (% ti.hour 12))
|
(def h (% ti.hour 12))
|
||||||
(if (= 0 h) 12 h)
|
(if (= 0 h) 12 h)
|
||||||
)))
|
)))
|
||||||
|
@ -101,19 +70,18 @@
|
||||||
|
|
||||||
(def cleanDir (fn ()
|
(def cleanDir (fn ()
|
||||||
"Get a string of the current directory" (
|
"Get a string of the current directory" (
|
||||||
(def di (cwd))
|
(def directory (cwd))
|
||||||
(if (matches di (cat ~ "*"))
|
(if (matches directory (cat ~ "*"))
|
||||||
(cat "~" (substr (slen ~) 999 di))
|
(cat "~" (substr (slen ~) 999 directory))
|
||||||
di
|
directory
|
||||||
)
|
)
|
||||||
)))
|
)))
|
||||||
|
|
||||||
(def prompt (fn (a) (
|
(def prompt (fn (a) (
|
||||||
(def ti (time))
|
|
||||||
(def d (cleanDir))
|
(def d (cleanDir))
|
||||||
|
|
||||||
(cat nl
|
(cat nl
|
||||||
esc "]0; " d (ch 7)
|
esc "]0; " d (ch 7)
|
||||||
bold red "[sage] " blue (clock ti) " " reset cyan d nl
|
bold red "[sage] " blue (clock (time)) " " reset cyan d nl
|
||||||
bold green "pebblisp ~> " reset)
|
bold green "pebblisp ~> " reset)
|
||||||
)))
|
)))
|
||||||
|
|
Loading…
Reference in New Issue