Note that there are some explanatory texts on larger screens.

plurals
  1. POData structure for storing variables in an interpreted language
    primarykey
    data
    text
    <p>I am designing my own experimental scripting language for the purpose of embedding it in my bigger application.</p> <p>Almost everything I wanted to do was programmed smoothly, but the "simple" act of storing variables in memory appeared to be the hardest part here. I don't know how to store them to allow all type checking, global variables and special flags on them. First look at a sample code:</p> <pre><code>a = 1 b = 2 someFunction() print(a) --&gt; This should read the global variable and print `1` a = 3 --&gt; Now `a` should become a local variable of this function and the global `a` remain unchanged x = 4 --&gt; `x` should always be local of this function end </code></pre> <p>I call the "locality" of variables their <code>level</code>s so variables in nested blocks have a higher level. In the above code, <code>a</code> and <code>b</code> are level 1 variables. Local variables of someFunction will have level 2. The first line of the function should read the global variable <code>a</code> (level 1) but the second line should create a variable again called <code>a</code> but with level 2 that shadows the global <code>a</code> from that point onwards. The third line should create the variable <code>x</code> with level 2. How to store and keep track of all these in memory?</p> <p><strong>What I tried so far:</strong></p> <p>Method 1: Storing maps of <code>variable=&gt;value</code> in array of levels:</p> <pre><code>variables { level=1 //global variables { a =&gt; 1, b =&gt; 2 }, level=2 //function variables { a =&gt; 3, x =&gt; 4 } } </code></pre> <p>But that will make variable look-up really slow since one has to search all the levels for a given variable.</p> <p>Method 2: Storing the (variable, level) pairs as keys of a map:</p> <pre><code>variables { (a, 1) =&gt; 1, //global (b, 1) =&gt; 2, //global (a, 2) =&gt; 3, //function (x, 2) =&gt; 3 //function } </code></pre> <p>This has the same problem as before since we have to try the pair (variable, level) with all possible levels for a given variable.</p> <p>What method should I use for optimal memory usage and fastest access time?</p> <p><strong>Additional notes:</strong></p> <p>I know about how variables are managed on stack and heap on other "real" languages, but I find it tricky to do this on an interpreted language. "This mustn't be how Lua and Python do that," I always think. Correct me if I'm wrong. I'm trying to store the variable in maps and internal C++ structures.</p> <p>And finally, this is how I represent a variable. Do you think it's big and there can be more memory-efficient representations? (I've also tried to put the "Level" as a member here but it had the same problem as the other too.)</p> <pre><code>struct Member { uchar type; //0=num, 1=str, 2=function, 3=array, etc uchar flags; //0x80 = read-only, 0x40 = write-only, etc union { long double value_num; char* value_str; int value_func; //etc }; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload