[pure-lang-svn] SF.net SVN: pure-lang: [321] pure/trunk
Status: Beta
Brought to you by:
agraef
From: <ag...@us...> - 2008-06-27 10:28:16
|
Revision: 321 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=321&view=rev Author: agraef Date: 2008-06-27 03:28:24 -0700 (Fri, 27 Jun 2008) Log Message: ----------- Add some more stuff to the public runtime API. Modified Paths: -------------- pure/trunk/ChangeLog pure/trunk/runtime.cc pure/trunk/runtime.h Modified: pure/trunk/ChangeLog =================================================================== --- pure/trunk/ChangeLog 2008-06-27 09:24:38 UTC (rev 320) +++ pure/trunk/ChangeLog 2008-06-27 10:28:24 UTC (rev 321) @@ -1,5 +1,8 @@ 2008-06-27 Albert Graef <Dr....@t-...> + * runtime.cc/h: Added pure_current_interp(), variable and constant + definitions, management of temporary definition levels. + * pure.cc, interpreter.cc, lexer.ll: Fix up completion support, second attempt (constructor symbols without any rules were still missing). Modified: pure/trunk/runtime.cc =================================================================== --- pure/trunk/runtime.cc 2008-06-27 09:24:38 UTC (rev 320) +++ pure/trunk/runtime.cc 2008-06-27 10:28:24 UTC (rev 321) @@ -801,6 +801,63 @@ pure_unref_internal(x); } +extern "C" +bool pure_let(int32_t sym, pure_expr *x) +{ + if (sym <= 0 || !x) return false; + try { + interpreter& interp = *interpreter::g_interp; + interp.defn(sym, x); + return true; + } catch (err &e) { + return false; + } +} + +extern "C" +bool pure_def(int32_t sym, pure_expr *x) +{ + if (sym <= 0 || !x) return false; + try { + interpreter& interp = *interpreter::g_interp; + interp.const_defn(sym, x); + return true; + } catch (err &e) { + return false; + } +} + +extern "C" +bool pure_clear(int32_t sym) +{ + if (sym > 0) { + interpreter& interp = *interpreter::g_interp; + interp.clear(); + return true; + } else + return false; +} + +extern "C" +uint8_t pure_save() +{ + interpreter& interp = *interpreter::g_interp; + if (interp.temp < 0xff) + return ++interp.temp; + else + return 0; +} + +extern "C" +uint8_t pure_restore() +{ + interpreter& interp = *interpreter::g_interp; + uint8_t level = interp.temp; + interp.clear(); + if (level > 0 && interp.temp > level-1) --interp.temp; + return interp.temp; +} + #ifndef HOST #define HOST "unknown" #endif @@ -939,6 +996,12 @@ interpreter::g_interp = (interpreter*)interp; } +extern "C" +pure_interp *pure_current_interp() +{ + return (pure_interp*)interpreter::g_interp; +} + /* END OF PUBLIC API. *******************************************************/ extern "C" Modified: pure/trunk/runtime.h =================================================================== --- pure/trunk/runtime.h 2008-06-27 09:24:38 UTC (rev 320) +++ pure/trunk/runtime.h 2008-06-27 10:28:24 UTC (rev 321) @@ -141,18 +141,14 @@ corresponding values. Parameter pointers may be NULL in which case they are not set. - Notes: + NOTES: pure_is_mpz takes a pointer to an uninitialized mpz_t and + initializes it with a copy of the Pure bigint. pure_is_symbol will return + true not only for (constant and unbound variable) symbols, but also for + arbitrary closures including local and anonymous functions. In the case of + an anonymous closure, the returned symbol will be 0. You can check whether + an expression actually represents a named or anonymous closure using the + funp and lambdap predicates from the library API (see below). */ - - pure_is_mpz takes a pointer to an uninitialized mpz_t and initializes it - with a copy of the Pure bigint. - - - pure_is_symbol will return true not only for (constant and unbound - variable) symbols, but also for arbitrary closures including local and - anonymous functions. In the case of an anonymous closure, the returned - symbol will be 0. You can check whether an expression actually represents - a named or anonymous closure using the funp and lambdap predicates from - the library API (see below). */ - bool pure_is_symbol(const pure_expr *x, int32_t *sym); bool pure_is_int(const pure_expr *x, int32_t *i); bool pure_is_mpz(const pure_expr *x, mpz_t *z); @@ -221,20 +217,54 @@ void pure_ref(pure_expr *x); void pure_unref(pure_expr *x); +/* Variable and constant definitions. These allow you to directly bind + variable and constant symbols to pure_expr* values, as the 'let' and 'def' + constructs do in the Pure language. The functions return true if + successful, false otherwise. */ + +bool pure_let(int32_t sym, pure_expr *x); +bool pure_def(int32_t sym, pure_expr *x); + +/* Purge the definition of a global (constant, variable or function) symbol. */ + +bool pure_clear(int32_t sym); + +/* Manage temporary definition levels (see the Pure manual for details). + pure_save starts a new level, pure_restore returns to the previous level, + removing all definitions of the current level. In either case the new level + is returned. A zero return value of pure_save indicates an error condition, + most likely because the maximum number of levels was exceeded. + + Note that the command line version of the interpreter starts at temporary + level 1, while the standalone interpreters created with the public API (see + below) start at level 0. Hence in the latter case you first need to invoke + pure_save before you can define temporaries. */ + +uint8_t pure_save(); +uint8_t pure_restore(); + /* The following routines provide standalone C/C++ applications with fully initialized interpreter instances which can be used together with the operations listed above. This is only needed for modules which are not to - be loaded by the command line version of the interpreter. + be loaded by the command line version of the interpreter. */ - The argc, argv parameters passed to pure_create_interp specify the command - line arguments of the interpreter instance. This includes any scripts that - are to be loaded on startup as well as any other options understood by the - command line version of the interpreter (options like -i and -q won't have - any effect, though, and the interpreter will always be in non-interactive - mode). The argv vector must be NULL-terminated, and argv[0] should be set - to the name of the hosting application (usually the main program of the - application). +/* The pure_interp type serves as a C proxy for Pure interpreters. Pointers + to these are used as C handles for the real Pure interpreter objects (which + are actually implemented by a C++ class). If your application needs more + elaborate control over interpreters as provided by this API, pure_interp* + can be cast to interpreter* (cf. interpreter.hh in the Pure sources). */ +typedef struct _pure_interp pure_interp; + +/* Manage interpreter instances. The argc, argv parameters passed to + pure_create_interp specify the command line arguments of the interpreter + instance. This includes any scripts that are to be loaded on startup as + well as any other options understood by the command line version of the + interpreter. (Options like -i and -q won't have any effect, though, and the + interpreter will always be in non-interactive mode.) The argv vector must + be NULL-terminated, and argv[0] should be set to the name of the hosting + application (usually the main program of the application). + An application may use multiple interpreter instances, but only a single instance can be active at any one time. By default, the first created instance will be active, but you can switch between different instances @@ -242,7 +272,10 @@ destroys an interpreter instance; if the destroyed instance is currently active, the active instance will be undefined afterwards, so you'll have to either create or switch to another instance before calling any other - operations. + operations. The pure_current_interp returns the currently active + instance. If the application is hosted by the command line interpreter, + this will return a handle to the command line interpreter if it is invoked + before switching to any other interpreter instance. Note that when using different interpreter instances in concert, it is *not* possible to pass pure_expr* values created with one interpreter @@ -250,11 +283,10 @@ the library API (see below) to first unparse the expression in the source interpreter and then reparse it in the target interpreter. */ -typedef struct _pure_interp pure_interp; // Pure interpreter handles (opaque). - pure_interp *pure_create_interp(int argc, char *argv[]); void pure_delete_interp(pure_interp *interp); void pure_switch_interp(pure_interp *interp); +pure_interp *pure_current_interp(); /* END OF PUBLIC API. *******************************************************/ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |