diff options
Diffstat (limited to 'libslang/slsh/lib')
-rw-r--r-- | libslang/slsh/lib/arrayfuns.sl | 50 | ||||
-rw-r--r-- | libslang/slsh/lib/autoload.sl | 4 | ||||
-rw-r--r-- | libslang/slsh/lib/require.sl | 82 | ||||
-rw-r--r-- | libslang/slsh/lib/slsh.rc | 46 |
4 files changed, 182 insertions, 0 deletions
diff --git a/libslang/slsh/lib/arrayfuns.sl b/libslang/slsh/lib/arrayfuns.sl new file mode 100644 index 0000000..17d9a49 --- /dev/null +++ b/libslang/slsh/lib/arrayfuns.sl @@ -0,0 +1,50 @@ +%!%+ +%\function{reverse} +%\synopsis{Reverse the elements of a 1-d array} +%\usage{Array_Type reverse (Array_Type A)} +%\description +% The \slfun{reverse} function reverses the elements of a 1-d array and +% returns the result. +%\seealso{shift} +%!%- +public define reverse (a) +{ + variable i = length (a); + if (i <= 1) + return a; + + i--; + __tmp(a)[[i:0:-1]]; +} + + +%!%+ +%\function{shift} +%\synopsis{Shift the elements of a 1-d array} +%\usage{Array_Type shift (Array_Type A, Int_Type n)} +%\description +% The \slfun{shift} function shifts the elements of an array by a specified amount +% and returns the result. If \exmp{n} is positive, the ith element of the array +% will be shifted to the position \exmp{i-n} of the array. Elements for +% which \exmp{i-n} is less than 0 will be moved to the end of the array. +%\example +%#v+ +% A = [1,2,3,4,5,6,7,8,9]; +% B = shift (A, 3); % ==> B = [4,5,6,7,8,9,1,2,3]; +% C = shift (A, -1); % ==> C = [9,1,2,3,4,5,6,7,8]; +%#v- +%\notes +% It many ways \exmp{rotate} would be a better name for this function. +%\seealso{reverse, transpose} +%!%- +public define shift (x, n) +{ + variable len = length(x); + variable i = [0:len-1]; + + % allow n to be negative and large + n = len + n mod len; + return x[(i + n)mod len]; +} + +provide ("arrayfuns"); diff --git a/libslang/slsh/lib/autoload.sl b/libslang/slsh/lib/autoload.sl new file mode 100644 index 0000000..db5837a --- /dev/null +++ b/libslang/slsh/lib/autoload.sl @@ -0,0 +1,4 @@ +autoload ("require", "require"); +autoload ("provide", "require"); +autoload ("reverse", "arrayfuns"); +autoload ("shift", "arrayfuns"); diff --git a/libslang/slsh/lib/require.sl b/libslang/slsh/lib/require.sl new file mode 100644 index 0000000..eec1e0d --- /dev/null +++ b/libslang/slsh/lib/require.sl @@ -0,0 +1,82 @@ +% These functions were taken from the jed editor + +static variable Features = Assoc_Type [Int_Type,0]; + +%!%+ +%\function{_featurep} +%\synopsis{Test whether or not a feature is present} +%\usage{Int_Type _featurep (String_Type feature)} +%\description +% The \sfun{_featurep} function returns a non-zero value if the specified +% feature is present. Otherwise, it returns 0 to indicate that the feature +% has not been loaded. +%\seealso{require, provide} +%!%- +public define _featurep (f) +{ + Features[f]; +} + + +%!%+ +%\function{provide} +%\synopsis{Declare that a specified feature is available} +%\usage{provide (String_Type feature)} +%\description +% The \sfun{provide} function may be used to declare that a "feature" has +% been loaded. See the documentation for \sfun{require} for more information. +%\seealso{require, _featurep} +%!%- +public define provide (f) +{ + Features[f] = 1; +} + +%!%+ +%\function{require} +%\synopsis{Make sure a feature is present, and load it if not} +%\usage{require (String_Type feature [,String_Type file]} +%\description +% The \sfun{require} function ensures that a specified "feature" is present. +% If the feature is not present, the \sfun{require} function will attempt to +% load the feature from a file. If called with two arguments, the feature +% will be loaded from the file specified by the second argument. Otherwise, +% the feature will be loaded from a file given by the name of the feature, +% with ".sl" appended. +% +% If after loading the file, if the feature is not present, +% a warning message will be issued. +%\notes +% "feature" is an abstract quantity that is undefined here. +% +% A popular use of the \sfun{require} function is to ensure that a specified +% file has already been loaded. In this case, the feature is the +% filename itself. The advantage of using this mechanism over using +% \ifun{evalfile} is that if the file has already been loaded, \sfun{require} +% will not re-load it. For this to work, the file must indicate that it +% provides the feature via the \sfun{provide} function. +%\seealso{provide, _featurep, evalfile} +%!%- +public define require () +{ + variable f, file; + + if (_NARGS == 2) + { + (f, file) = (); + } + else + { + f = (); + file = f; + } + + if (_featurep (f)) + return; + + () = evalfile (file); + !if (_featurep (f)) + vmessage ("***Warning: feature %s not found in %s", f, file); +} + + diff --git a/libslang/slsh/lib/slsh.rc b/libslang/slsh/lib/slsh.rc new file mode 100644 index 0000000..10ce328 --- /dev/null +++ b/libslang/slsh/lib/slsh.rc @@ -0,0 +1,46 @@ +% -*- slang -*- + +% This file gets loaded whenever slsh runs. Its primary purpose is to define +% some functions that are useful in scripts, and to set up some local paths + +static define dir_exists (dir) +{ + variable s = stat_file (dir); + if (s == NULL) return 0; + return stat_is ("dir", s.st_mode); +} + +%!%+ +%\function{prepend_to_slang_load_path} +%\synopsis{Prepend a directory to the load-path} +%\usage{prepend_to_slang_load_path (String_Type dir)} +%\description +% This function adds a directory to the beginning of the interpreter's +% load-path. +%\seealso{append_to_slang_load_path, set_slang_load_path} +%!%- +public define prepend_to_slang_load_path (p) +{ + if (dir_exists (p)) + set_slang_load_path (strcat (p, ":", get_slang_load_path ())); +} + +%!%+ +%\function{append_to_slang_load_path} +%\synopsis{Append a directory to the load-path} +%\usage{append_to_slang_load_path (String_Type dir)} +%\description +% This function adds a directory to the end of the interpreter's +% load-path. +%\seealso{prepend_to_slang_load_path, set_slang_load_path} +%!%- +public define append_to_slang_load_path (p) +{ + if (dir_exists (p)) + set_slang_load_path (get_slang_load_path (), ":", p); +} + +() = evalfile ("autoload.sl"); + +% Add local additions here + |