脚本语言如何设置/修改/读出变量?
假设该语言的解释器(可以是从 PHP 到 Ruby 的任何语言)是用 C 编写的。 由当前正在执行的脚本定义的变量(或更复杂的数据结构,不仅包含名称和值)如何存储和读出?
我对 C 的了解相当少,最终得出的结论是这只能用数组来完成。
// Variable type definition would go here
var* variables;
var
类型将包含两个字符串name
和value
。
好的。因此,一个脚本定义了例如:30 个变量。现在,如果必须读出其中一个变量,则函数 getVar
(或类似的函数)必须遍历所有 30 个变量,并将它们的名称
与请求的变量的名称。想象一下,如果有一个请求
的循环,
- 我完全错了吗?如果是,(现代?)脚本语言如何处理变量?它们是如何存储和读出的?
在通过语法明确定义变量的语言中(PHP:
$myVar
),解释器可以在解析过程中用数值替换所有变量。 (我这样说对吗?) 是这样吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
哈希表、作用域链表、引用...有很多内容。
您所要求的是半抽象的,并且实现是可变的。
根据实现:
在基本脚本语言中,遇到的变量名称将被放入作用域结构中,例如包含哈希表的链接列表 strong> 用于在该范围内查找标识符的实现。
当引用变量时,运行时代码会在哈希表中查找它并获取与该值相关的某个值(例如结构的内存地址)。结构可用于实现标量变量:
或者我的糟糕示例源的一些变体。
一些好书是“现代编译器在 C 中的实现”和 < a href="http://dragonbook.stanford.edu/" rel="nofollow noreferrer">龙书。阅读这个主题是个好主意;我会把它推荐给任何程序员。
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:
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.
他们几乎肯定使用更复杂的数据结构。
然后将它们存储在树中(散列或二进制),以便可以通过名称检索它们
They almost certainly use a more sophisticated data structure.
and then store them in a tree (hash or binary) so they can be retrieved by name
通常,脚本语言实现将使用相当复杂的 C 数据结构来表示脚本语言中的变量。对于 C 扩展定义明确的语言,文档很容易获得:
[SO 编辑:请随时向上述列表添加更多引用]
Typically, scripting language implementations will use fairly complex C data structures to represent variables in the scripting language. For languages where C extensions are well-defined, the documentation is readily available:
[SO editors: feel free to add more references to the above list]