Assuming the interpreter for the language (Can be anything from PHP to Ruby) is written in C. How are variables (or more complex data structures not only containg name and value), which are defined by the script that is currently being executed, stored and read out?
I, with my rather poor knowledge of C, would end up with the conclusion that this can only be done with an array.
// Variable type definition would go here
var* variables;
The var
type would contain two strings name
and value
.
Okay. So a script defines e.g.: 30 variables. Now, if one of the variables has to be read out, the function getVar
(or something similar) would have to walk through all 30 variables and compare their name
s with the name of the requested variable. Imagine that with a loop that requests
Am I getting it totally wrong? If yes, how do (modern?) scripting languages handle variables? How are they stored and read out?
In languages where variables are clearly defined by the syntax (PHP: $myVar
), the interpreter could replace all variables by numerical values during the parsing process. (Am I right with that?)
Is this the case?
Hashtables, scope linked lists, references ... there is alot to it.
What you are asking is half-abstract, and the implementation is variable.
Depending on the implementation:
In a basic scripting language, variable names when encoutered would be put into a scope structure such as a linked-list containing a hashtable implementation for looking up identifiers in that scope.
When a variable is referenced, the runtime code looks it up in the hashtable and obtains some value (a memory address of a struct for example) which relates to that value. Structs can be used to implement scalar variables:
enum stype = { INT, STRING, FLOAT, BOOL };
struct scalar {
enum stype type;
generic_blob_t *heap_blob;
};
Or some variation of my poor example source.
Some good books are "Modern Compiler Implementation in C" and the Dragon Book. Reading up on this topic is a good idea; I would recommend it to any programmer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With