Re: How to implement dynamic typing?

Paul Biggar <paul.biggar@gmail.com>
Tue, 6 Apr 2010 18:32:46 +0100

          From comp.compilers

Related articles
How to implement dynamic typing? davidbleier@hotmail.com (David Belier) (2010-04-02)
Re: How to implement dynamic typing? gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-04-06)
Re: How to implement dynamic typing? sleepdev@gmail.com (andy johnson) (2010-04-06)
Re: How to implement dynamic typing? rpw3@rpw3.org (2010-04-06)
Re: How to implement dynamic typing? bartc@freeuk.com (bartc) (2010-04-06)
Re: How to implement dynamic typing? paul.biggar@gmail.com (Paul Biggar) (2010-04-06)
Re: How to implement dynamic typing? iant@google.com (Ian Lance Taylor) (2010-04-06)
Re: How to implement dynamic typing? barry.j.kelly@gmail.com (Barry Kelly) (2010-04-07)
Re: How to implement dynamic typing? torbenm@diku.dk (2010-04-07)
Re: How to implement dynamic typing? ctalk@ctalklang.org (Ctalk Project) (2010-04-07)
Re: How to implement dynamic typing? gneuner2@comcast.net (George Neuner) (2010-04-07)
Re: How to implement dynamic typing? cr88192@hotmail.com (BGB / cr88192) (2010-04-10)
[19 later articles]
| List of all articles for this month |
From: Paul Biggar <paul.biggar@gmail.com>
Newsgroups: comp.compilers
Date: Tue, 6 Apr 2010 18:32:46 +0100
Organization: Compilers Central
References: 10-04-009
Keywords: types
Posted-Date: 07 Apr 2010 01:53:48 EDT

Hi David,


On Fri, Apr 2, 2010 at 4:38 PM, David Belier <davidbleier@hotmail.com> wrote:
> Recently I finished chapter 7 of the dragon book (2nd edition) but I
> still don't understand how to implement dynamic typing. I don't get
> how you are supposed to store the type information with each variable


The important part of dynamic typing is that it is not the variable
which holds the type, but the value itself. The variable just points
to the value. Since it can point to many values over its lifetime, and
each value can have a different type, you have dynamic typing.


In PHP, a variable is implemented as a pointer[1] to a zval:


struct zval
{
        int type;
        union value { int _int, int _bool, double real, ..... }
        ....
};


``type'' is an enum, and possible values are IS_STRING, IS_INT, IS_REAL, etc.


Other scripting language implementations are similar, especially Lua,
and to a lesser degree Perl, Python and Ruby. In Python, each variable
points to a run-time value, each value is an object, and each object
has a pointer to a class[2].


There are other implementation possible. I don't know much about
"tagged pointers", but its a keyword to google for you. SELF (and now
V8, Google's JS engine) use "maps" (hidden classes in V8 terminology).


Type checks typically only occur when you want to run different code
depending on the type. PHP implements this--I shit you not--with
switch statements. This leads to horrible code and inefficiencies, so
avoid this. Instead, have a look at the SELF implementation with PICs
[3]. Again, V8 does this too.


If I may be permitted to cite myself, I gave an overview of this stuff
in sections 2.1 and 5.5.1 of my thesis at
http://www.paulbiggar.com/research/#wip-phd-dissertation.




Paul


[1] Actually, variables in scripting languages are often not really
variables. Instead, symbol tables are dynamic, and can be considered
to be tables. So variables may be dynamically created and destroyed,
rather than having a fixed scope or lifetime,


[2] In Python and Ruby, objects can change class dynamically, but this
is not required for a dynamically typed language. PHP can't do this,
for example.


[3] Urs HC6lzle, Craig Chambers, and David Ungar. Optimizing dynamically
typed
object-oriented languages with polymorphic inline caches. In ECOOP
b91: Proceedings of the European Conference on Object-Oriented Programming,
pages 21b38. Springer-Verlag, 1991.




On Fri, Apr 2, 2010 at 4:38 PM, David Belier <davidbleier@hotmail.com> wrote:
> Recently I finished chapter 7 of the dragon book (2nd edition) but I
> still don't understand how to implement dynamic typing. I don't get
> how you are supposed to store the type information with each variable
> and check it every time using an acceptable amount of resources and
> time. Can someone please name papers or websites to read?


--
Paul Biggar
paul.biggar@gmail.com



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.