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?