From 9415b27f41c745e8da55a8012478f0d1e2ca1daa Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Wed, 13 Apr 2022 13:18:24 -0400 Subject: [PATCH] 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. --- src/env.c | 12 +++++ src/examples/lib.pbl | 103 +++++++++++++++++++++++++++++++++----- src/examples/pebblisp.pbl | 76 ++++++++-------------------- 3 files changed, 124 insertions(+), 67 deletions(-) diff --git a/src/env.c b/src/env.c index f8d5f03..6cbf786 100644 --- a/src/env.c +++ b/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; diff --git a/src/examples/lib.pbl b/src/examples/lib.pbl index 070073a..3366354 100644 --- a/src/examples/lib.pbl +++ b/src/examples/lib.pbl @@ -1,26 +1,38 @@ #!/usr/bin/pl ; Print with newline -(def prnl (fn (_txt) ( - (prn (cat _txt (ch 10))) - ))) +(def esc (ch 27)) -(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)) ))) -; 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 diff --git a/src/examples/pebblisp.pbl b/src/examples/pebblisp.pbl index 8c6ce29..6b6f1a9 100644 --- a/src/examples/pebblisp.pbl +++ b/src/examples/pebblisp.pbl @@ -9,67 +9,33 @@ (def bold "") (def reset "") - -(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 () "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) @@ -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) )))