| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (c) 2001, 2006 Michael J. Roberts |
| 5 |
* |
| 6 |
* This file is part of TADS 3. |
| 7 |
* |
| 8 |
* This module defines classes related to the DynamicFunc intrinsic |
| 9 |
* class. You should include this source file in your build if you're |
| 10 |
* using the DynamicFunc class. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <tads.h> |
| 14 |
#include <dynfunc.h> |
| 15 |
|
| 16 |
|
| 17 |
/* ------------------------------------------------------------------------ */ |
| 18 |
/* |
| 19 |
* Compiler: This object provides a simplified interface to the dynamic |
| 20 |
* compiler. The methods here can be used instead of manually creating |
| 21 |
* DynamicFunc instances. |
| 22 |
* |
| 23 |
* The main advantage of using this object to compile code is that it |
| 24 |
* automatically provides access to the global symbol table that was used |
| 25 |
* to compile the current program, for use in dynamic code. Without the |
| 26 |
* global symbol table, dynamic code won't have access to object names, |
| 27 |
* property names, function names, and so on. That doesn't stop you from |
| 28 |
* compiling code that only depends upon its own function parameters and |
| 29 |
* local variables, but for most purposes the global symbols are useful |
| 30 |
* to have around. |
| 31 |
* |
| 32 |
* Note that including this object in a project will automatically save |
| 33 |
* the global symbol table in the compiled .t3 file. This increases the |
| 34 |
* size of the .t3 file, as well as memory usage during execution. If |
| 35 |
* you're concerned about minimizing the .t3 file size or the run-time |
| 36 |
* memory footprint, *and* you don't need global symbols for dynamic code |
| 37 |
* (or you don't use the dynamic compiler at all), you can save some |
| 38 |
* space by omitting this whole module from the build. |
| 39 |
*/ |
| 40 |
Compiler: PreinitObject |
| 41 |
/* |
| 42 |
* Compile an expression or function. 'str' is a string giving the |
| 43 |
* code to compile. This can be a simple value expression, such as |
| 44 |
* 'Me.location' or 'new BigNumber(12345).sqrt()'. Or, it can be a |
| 45 |
* complete unnamed function definition, using this syntax: |
| 46 |
* |
| 47 |
*. 'function(x, y, z) { ...body of function... }' |
| 48 |
* |
| 49 |
* The body of the function can contain any executable code that you |
| 50 |
* could write in a regular function in static code: if, while, |
| 51 |
* switch, return, etc. |
| 52 |
* |
| 53 |
* The return value is a DynamicFunc containing the compiled |
| 54 |
* expression or function. You call it by using the return value as |
| 55 |
* though it were a function: |
| 56 |
* |
| 57 |
*. local f = Compiler.compile('Me.location'); |
| 58 |
*. local loc = f(); |
| 59 |
* |
| 60 |
* If the source string was just an expression, it acts like a |
| 61 |
* function that takes zero arguments, and returns the computed value |
| 62 |
* of the expression. The expression is evaluated anew each time you |
| 63 |
* invoke it, so you'll get the "live" value of an expression that |
| 64 |
* refers to object properties or other external data. In the |
| 65 |
* example above, we'd get the current value of Me.location every |
| 66 |
* time we call f(). |
| 67 |
* |
| 68 |
* The source string is actually compiled immediately when you call |
| 69 |
* this function. This means it's checked for errors, such as syntax |
| 70 |
* errors and unknown symbol names. If the code contains any errors, |
| 71 |
* this method throws a CompilerException describing the problem. |
| 72 |
*/ |
| 73 |
compile(str, locals?) |
| 74 |
{ |
| 75 |
/* compile the string, using our saved global symbol table */ |
| 76 |
return new DynamicFunc(str, symtab_, locals, macros_); |
| 77 |
} |
| 78 |
|
| 79 |
/* |
| 80 |
* Compile a dynamic function string, and add it to the global symbol |
| 81 |
* table as a function with the given name. This effectively creates |
| 82 |
* a new named function that you can call from other dynamic code |
| 83 |
* objects. |
| 84 |
*/ |
| 85 |
defineFunc(name, str, locals?) |
| 86 |
{ |
| 87 |
/* |
| 88 |
* compile the string, and add it to our symbol table under the |
| 89 |
* given function name |
| 90 |
*/ |
| 91 |
symtab_[name] = new DynamicFunc(str, symtab_, locals, macros_); |
| 92 |
} |
| 93 |
|
| 94 |
/* |
| 95 |
* Evaluate an expression. 'str' is a string giving code to compile. |
| 96 |
* In most cases, this is simply a simple value expression, although |
| 97 |
* it's also acceptable to use the 'function()' syntax to create a |
| 98 |
* function that takes no arguments. |
| 99 |
* |
| 100 |
* This method compiles the source string and immediately calls the |
| 101 |
* resulting compiled code. The return value is the value returned |
| 102 |
* from the compiled code itself. This method thus provides a quick |
| 103 |
* way to evaluate an expression. |
| 104 |
* |
| 105 |
* If the string contains any syntax errors or other compilation |
| 106 |
* errors, the method throws a CompilerException. In addition, it's |
| 107 |
* possible for the compiled code to throw exceptions of its own; |
| 108 |
* this method doesn't catch those, leaving it up to the caller to |
| 109 |
* handle them. |
| 110 |
* |
| 111 |
* If you expect to evaluate the same expression repeatedly, you're |
| 112 |
* better off using compile() to get the compiled representation of |
| 113 |
* the expression, and then call that compiled code each time the |
| 114 |
* value is needed. That's more efficient than using eval() each |
| 115 |
* time, since eval() to recompile the expression on every call, |
| 116 |
* which is a fairly complex process. |
| 117 |
*/ |
| 118 |
eval(str, locals?) |
| 119 |
{ |
| 120 |
/* |
| 121 |
* compile the string, call the resulting function, and return |
| 122 |
* the result from the function |
| 123 |
*/ |
| 124 |
return (new DynamicFunc(str, symtab_, locals, macros_))(); |
| 125 |
} |
| 126 |
|
| 127 |
/* |
| 128 |
* During preinit, save a reference to the program's global symbol |
| 129 |
* table in a property of self. The VM always makes the global |
| 130 |
* symbols available during preinit, but by default it discards the |
| 131 |
* table after that because most programs don't need it. That means |
| 132 |
* that the symbols aren't available by default during normal |
| 133 |
* execution. However, saving a reference here prevents the garbage |
| 134 |
* collector from discarding the table when preinit finishes, which |
| 135 |
* forces it to be saved in the final .t3 file and thus makes it |
| 136 |
* available permanently. |
| 137 |
*/ |
| 138 |
execute() |
| 139 |
{ |
| 140 |
/* save the global symbol table */ |
| 141 |
symtab_ = t3GetGlobalSymbols(T3GlobalSymbols); |
| 142 |
macros_ = t3GetGlobalSymbols(T3PreprocMacros); |
| 143 |
} |
| 144 |
|
| 145 |
/* a saved reference to the global symbol table */ |
| 146 |
symtab_ = nil |
| 147 |
|
| 148 |
/* a saved referenced to the preprocessor macro table */ |
| 149 |
macros_ = nil |
| 150 |
; |
| 151 |
|
| 152 |
|
| 153 |
/* ------------------------------------------------------------------------ */ |
| 154 |
/* |
| 155 |
* Compiler Exception. 'new DynamicFunc()' throws an exception of this |
| 156 |
* class if an error occurs compiling the source code of the new object. |
| 157 |
*/ |
| 158 |
class CompilerException: Exception |
| 159 |
construct(msg) { errmsg_ = msg; } |
| 160 |
displayException() |
| 161 |
{ |
| 162 |
if (errmsg_ != nil) |
| 163 |
"<<errmsg_>>"; |
| 164 |
else |
| 165 |
"Source code compilation error"; |
| 166 |
} |
| 167 |
|
| 168 |
/* the error message from the compiler */ |
| 169 |
errmsg_ = nil |
| 170 |
; |
| 171 |
|
| 172 |
/* export the compiler exception for use by the intrinsic class */ |
| 173 |
export CompilerException 'DynamicFunc.CompilerException'; |
| 174 |
|