How do scripting languages ​​set / change / read variables?

Assuming that the interpreter for the language (can be anything from PHP to Ruby) is written in C. How are the variables (or more complex data structures not only contacting the name and value) that are defined by the script that is currently being executed, stored and read ?

I, with my fairly poor knowledge of C, will end up being able to do this only with an array.

// Variable type definition would go here
var* variables;

The type varwill contain two lines nameand value.

Good. Thus, a script defines, for example, 30 variables. Now, if you need to read one of the variables, the function getVar(or something similar) should go through all 30 variables and compare them namewith the name of the requested variable. Imagine that with a loop that asks

  • Am I really wrong? If so, how are (modern?) Scripting languages ​​handling variables? How are they stored and read?

  • In languages ​​where variables are clearly defined by the syntax (PHP:) $myVar, the interpreter can replace all variables with numerical values ​​during the parsing process. (Am I right with this?) Is this the case?

+5
source share
3 answers

Hashtables, , ... .

, , , .

:

  • , -.

, , hashtable .

, - (, ), . :

enum stype = { INT, STRING, FLOAT, BOOL };
struct scalar {
    enum stype type;
    generic_blob_t *heap_blob;
};

.

: "" C " . - ; .

+3

.

 struct Var
 {
     char *name;
     int type;
     union value; ....
  };

( ),

+2

, C . , C , :

[ SO: ]

+2
source

All Articles