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);
|
||||
if (object) {
|
||||
size_t len = 0;
|
||||
|
|
|
@ -1,26 +1,38 @@
|
|||
#!/usr/bin/pl
|
||||
; Print with newline
|
||||
(def prnl (fn (_txt) (
|
||||
(prn (cat _txt (ch 10)))
|
||||
(def esc (ch 27))
|
||||
|
||||
(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) (
|
||||
(def loadfile (fn (file-name)
|
||||
"Read and immediately evaluate the file with the given path." (
|
||||
(eval (rf file-name))
|
||||
)))
|
||||
|
||||
; Exponentiate a^b
|
||||
(def exp (fn (a b)
|
||||
"Exponentiate a^b"
|
||||
(if (= b 0)
|
||||
1
|
||||
(* a (exp a (- b 1)))
|
||||
)
|
||||
))
|
||||
|
||||
; Square a
|
||||
(def sq (fn (a) (* a a)))
|
||||
(def sq (fn (a)
|
||||
"Square the given number"
|
||||
(* a a)
|
||||
))
|
||||
|
||||
; Cube a
|
||||
(def cube (fn (a) (exp a 3)))
|
||||
(def cube (fn (a)
|
||||
"Cube the given number"
|
||||
(exp a 3)
|
||||
))
|
||||
|
||||
(def fib (fn (a)
|
||||
(if (< a 2)
|
||||
|
@ -28,11 +40,76 @@
|
|||
(+ (fib (- a 1)) (fib (- a 2)))
|
||||
)))
|
||||
|
||||
; Return the larger of the two
|
||||
(def max (fn (a b) (if (> a b) a b)))
|
||||
(def max (fn (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) (if (< a b) a b)))
|
||||
(def min (fn (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
|
||||
; Doesn't yet work with lambdas
|
||||
|
|
|
@ -9,61 +9,27 @@
|
|||
|
||||
(def bold "[1m")
|
||||
(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 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 string (fn (a) (cat "" a)))
|
||||
(def editConfig (fn () (
|
||||
(sys (cat (env "EDITOR") " " config))
|
||||
(reloadConfig)
|
||||
)))
|
||||
|
||||
(struct Alias (name value))
|
||||
(def aliases (
|
||||
(Alias "ls" "ls --color")
|
||||
(Alias "vimc" "vim ~/.vimrc")
|
||||
(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 "r" "(reloadConfig)")
|
||||
(Alias "cd ~" (cat "cd " ~))
|
||||
(Alias "sudo" "echo -e '\e[1;31m' && sudo")
|
||||
))
|
||||
|
||||
(def alias (fn ()
|
||||
|
@ -85,12 +51,15 @@
|
|||
)))
|
||||
|
||||
(def preprocess (fn (text) (
|
||||
(def matches (fil (fn (a) (= text a.name)) aliases))
|
||||
(def match (first matches))
|
||||
(if (iserr match) text match.value)
|
||||
(def alias-matches (fil (fn (a) (startsWith text a.name)) aliases))
|
||||
(def match (first alias-matches))
|
||||
(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))
|
||||
(if (= 0 h) 12 h)
|
||||
)))
|
||||
|
@ -101,19 +70,18 @@
|
|||
|
||||
(def cleanDir (fn ()
|
||||
"Get a string of the current directory" (
|
||||
(def di (cwd))
|
||||
(if (matches di (cat ~ "*"))
|
||||
(cat "~" (substr (slen ~) 999 di))
|
||||
di
|
||||
(def directory (cwd))
|
||||
(if (matches directory (cat ~ "*"))
|
||||
(cat "~" (substr (slen ~) 999 directory))
|
||||
directory
|
||||
)
|
||||
)))
|
||||
|
||||
(def prompt (fn (a) (
|
||||
(def ti (time))
|
||||
(def d (cleanDir))
|
||||
|
||||
(cat nl
|
||||
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)
|
||||
)))
|
||||
|
|
Loading…
Reference in New Issue