Related articles |
---|
A minor(?) problem I need help with arrow@trelleborg.mail.telia.com (The Arrow) (1999-05-16) |
Re: A minor(?) problem I need help with Maffu@Kindy.net (1999-05-22) |
From: | The Arrow <arrow@trelleborg.mail.telia.com> |
Newsgroups: | comp.compilers |
Date: | 16 May 1999 15:09:04 -0400 |
Organization: | Compilers Central |
Keywords: | parse, types, question |
I have a little problem with a compiler I am writing. The language it
compiles is completely typeless, and variables can be assigned
anything. The problem is that I have no way of telling wether a
variable is assigned to a class or an object (an instance of a class)
or a function, or checking for property calls or assignments at
compile time.
The following code snip shows illegal and legal stuff in my language:
--- begin snip ---
/* define two classes, properties in classes can not be used without
* the classes being instanciated
*/
class Class1 =
{
prop1 = "This is a string";
prop2 = { return "This is another string"; }
/* prop1 can get assigned any value, while prop2 can not get
* assigned at all
*/
}
class Class2 =
{
prop1 = 5;
prop2 = "foo";
}
/* define an instance of a class, this one with inheritance */
object Instance : Class2 =
{
prop1 = 4;
}
function fun1(a, b) = return a + b;
function fun2 =
{
var obj1, obj2, obj3;
var tmp, fun. cls;
obj1 = new Class1;
obj2 = new Class2;
obj3 = Instance;
tmp = Instance.prop1; /* tmp is assigned 4 */
Instance.prop1 = 7; /* Instance.prop1 is assigned 7 */
obj3.prop1 = 1; /* Instance.prop1 should be assigned 1 */
obj1.prop1 = "bar"; /* what class is obj1? */
tmp = obj2.prop2; /* what class is obj2? */
tmp = obj1.prop2; /* legal */
obj1.prop2 = tmp; /* illegal */
fun = fun1; /* how can I know fun is now a function? */
tmp = fun(1, 2); /* tmp should be assigned 3 (1 + 2) */
tmp = fun1(2, 3); /* tmp is assigned 5 (2 + 3) */
cls = Class1;
tmp = cls.prop1; /* illegal, not an instanciated object */
obj3 = new cls; /* creating a new instance */
tmp = obj3.prop1; /* this is ok, obj3 is an instance */
}
--- end snip ---
Is there any way of doing these checks at compile time, without
special cases in assignments to keep track of these things?
The compiler is recursive-descent, allthough I have been thinking
about using operator precedence for my expression parsing (but it
wouln't help much on solving this problem). All functions are stored
in a global table, as are all classes/objects. Variables are stored
in a table in the function it is defined in, and global variables is
stored in a global no-name-not-callable function. Properties are
really a kind functions, and are stored in tables local for the class.
The language itself is pretty generic (I think), but for now it is to
be used in a multi-player Internet text-game. Both to define the
world, and to handle game specific things.
All ideas or thoughts (good or bad) are more than wellcome! :)
PS.
I hope you excuse any spelling misstakes... ;)
/ Joachim
======================================================================
The Arrow Email: arrow@trelleborg.mail.telia.com
Joachim Pileborg WWW: [Under construction]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.