stdlib: Implement read() and readlines() functions.
[goals.git] / stdlib / prelude.gl
index fdb23e9..5d072ea 100644 (file)
@@ -21,7 +21,7 @@
 # tactics.
 
 # The only tactic that ‘make’ has.
-tactic *file (filename) = {
+tactic *file (filename) = @{
     # Rebuild if the target file doesn't exist at all.
     test -f %filename || exit 99
 
@@ -33,28 +33,105 @@ tactic *file (filename) = {
 
 # This is a simpler tactic than the above since it will
 # rebuild if the file is missing, but not if it is older.
-tactic *exists (filename) = {
+tactic *exists (filename) = @{
     test -f %filename || exit 99
 }
 
 #----------------------------------------------------------------------
+# Basic functions.
+
+# Wrap list of strings in a call or tactic.
+pure function wrap (wrapper, xs) = @{
+    echo '['
+    for x in %xs; do
+        echo %wrapper "( "
+        quoted_string "$x"
+        echo " ),"
+    done
+    echo ']'
+}
+
+#----------------------------------------------------------------------
 # Text functions.
 
+# Head of a list.
+pure function head (xs) returning string = @{
+    for f in %xs; do
+        echo "$f"
+        exit 0
+    done
+}
 
+# Join two lists.
+pure function join (xs, ys) returning strings = @{
+    for f in %xs %ys; do echo "$f"; done
+}
+
+# Sort + uniq a list.
+pure function sort (xs) returning strings = @{
+    for f in %xs; do echo "$f"; done | sort -u
+}
+
+# Substitute.
+pure function subst (from, to, text) returning string = @{
+    # We need to replace any / characters in ‘to’ with escaped ones.
+    to="$( echo -n %to | sed 's,/,\\/,g' )"
+    echo %text | sed -E s/%from/$to/g
+}
+
+# Tail of a list.
+pure function tail (xs) returning strings = @{
+    drop=1
+    for f in %xs; do
+        if [ -z "$drop" ]; then echo "$f"; fi
+        drop=
+    done
+}
 
 #----------------------------------------------------------------------
 # File functions.
 
+# Base name.
+pure function basename (name) returning string = @{
+    basename %name
+}
+
+# Directory name.
+pure function dirname (name) returning string = @{
+    dirname %name
+}
+
+# File extension.
+pure function extension (name) returning string = @{
+    name=%name
+    echo "${name##*.}"
+}
+
+# Read a file.
+function read (filename) returning string = @{
+    cat %filename
+}
+
+# Read a file as a list of lines.
+function readlines (filename) returning strings = @{
+    cat %filename
+}
+
+# Real path.
+function realpath (filename) returning string = @{
+    realpath -- %filename
+}
+
 # Expand a wildcard into a list of filenames.
-function wildcard (wc) = {
+#
+# This function is probably not "pure" since it depends on the
+# current working directory and also files may be created in
+# the directory while goals is running which would affect the
+# result.
+function wildcard (wc) returning strings = @{
     shopt -s nullglob
     # Note that the substitution is quoted by goals, so to expand
     # it we must assign it to a variable and then use it unquoted.
     wc=%wc
-    echo '['
-    for f in $wc; do
-        quoted_string "$f"
-        echo ','
-    done
-    echo ']'
+    for f in $wc; do echo "$f"; done
 }