slang-users mailing list

[2008 Date Index] [2008 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

[slang-users] Re: Support for assertions?


Hallo John,

"John E. Davis" <davis@xxxxxxxxxxxxx> wrote:
> On Sat, 6 Dec 2008 23:08:09 +0000 (UTC), Jörg Sommer <joerg@xxxxxxxxxxxx>
> wrote:
>> I can't solve it easily with a function, because a function gets the
>> value of “a == 4” and not the expression. Using eval in the assert
>> function might not work, because a might be a local variable in the
>> calling function.
>
> The following (convoluted) function might work.  You would use it as
> in the following example:

Howly moly. It's really impressive to see the power of S‐Lang.

Index: doc/tm/rtl/debug.tm
===================================================================
--- doc/tm/rtl/debug.tm	(Revision 282)
+++ doc/tm/rtl/debug.tm	(Arbeitskopie)
@@ -232,3 +232,43 @@
 \seealso{_slangtrace, _traceback}
 \done
 
+\function{_get_frame_info}
+\synopsis{Returns informations about a stack frame}
+\usage{Struct_Type _get_frame_info (Integer_Type depth)}
+\description
+  \ifun{_get_frame_info} returns a structure with informations about the
+  stack from of depth \svar{depth}. The structue contains the following
+  fields:
+  * file: The file that contains the code of the stack frame.
+  * line: The line in the file the stack frame is in.
+  * function: the name of the function containing the code of the stack
+    frame, might be NULL if the code isn't inside of a function
+  * locals: Array of String_Type containing the names of variables local
+    to the stack frame. It might be NULL if the stack frame doesn't
+    belong to a function.
+  * namespace: The namespace the code of this stack frame is in.
+\done
+
+\function{_get_frame_variable}
+\synopsis{Returns the value of a variable as seen from the stack frame}
+\usage{Any_Type _get_frame_variable (Integer_Type depth, String_Type name)}
+\description
+  This function returns value of the variable \svar{name} in the stack
+  frame at depth \svar{depth}. This might not only a local variable but
+  also variables from outer scopes, e.g. a variable private of the
+  namespace.
+
+  If no variable with this name is found an UndefinedNameError is
+  thrown and if the variable is not initialized an
+  VariableUninitializedError is thrown.
+\done
+
+\function{_use_frame_namespace}
+\synopsis{Selects the namespace of a stack frame}
+\usage{_use_frame_namespace (Integer_Type depth)}
+\description
+  This function sets the namespace to the one belonging to the stack
+  frame at depth \svar{depth}. It's approximately the same as
+
+  use_namespace(_get_frame_info(depth).namespace)
+\done
Index: slsh/lib/require.sl
===================================================================
--- slsh/lib/require.sl	(Revision 282)
+++ slsh/lib/require.sl	(Arbeitskopie)
@@ -28,6 +28,44 @@
    Features[f] = 1;
 }
 
+%!%+
+%\function{require}
+%\synopsis{Make sure a feature is present, and load it if not}
+%\usage{require (feature [,namespace [,file]])
+%#v+
+%   String_Type feature, namespace, file;
+%#v-
+%\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 the \exmp{namespace} argument is present
+%  and non-NULL, the specified namespace will be used.  The default is to use
+%  the current non-anonymous namespace. If called with three arguments, the 
+%  feature will be loaded from the file specified by the third argument 
+%  if it does not already exist in the namespace.  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.
+%\examples
+%#v+
+%    require ("histogram");
+%    require ("histogram", "foo");
+%    require ("histogram", "foo", "/home/bob/hist.sl");
+%    require ("histogram", , "/home/bob/hist.sl");
+%#v-
+%\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}
+%!%-
 define require ()
 {
    variable feat, file;

Bye, Jörg.
-- 
Objektivität ist die Wahnvorstellung, Beobachtungen könnten ohne
Beobachter gemacht werden – Heinz v. Foerster



[2008 date index] [2008 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]