www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - explore current scope, or other hack

reply spir <denis.spir gmail.com> writes:
Hello,

Is there a way to explore the current scope, meaning the set of currently d=
efined symbols?
(Equivalent of python's vars(), locals(), globals().)

I have 2 use cases for this:

1. name objects automatically
I need some objects to know their name (as field on themselves). the only s=
olution I can else imagine is for the user write:
	x =3D ...;
	x.name =3D "x";
I hope you agree this is more than stupid ;-) Exploring the scope would all=
ow providing a tool func that does this automatically, once all objects are=
 known. Is there an alternative I overlook?

2. some tool like format()
Say I wish like to define an alternative approach to variable strings, like
	s =3D VarString("Hello, 'userName'!");
This requires getting variable values at runtime, I guess, meaning explore =
the scope. How do format and writefln work (pointer welcome)? Is there an a=
lternative?


Denis
-- -- -- -- -- -- --
vit esse estrany =E2=98=A3

spir.wikidot.com
Nov 15 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
spir:

 1. name objects automatically
 I need some objects to know their name (as field on themselves). the only
solution I can else imagine is for the user write:
 	x = ...;
 	x.name = "x";
What if you have two or more references to the same object? Regarding your generic question, I'd like DMD to have more static introspection, for for now ... Bye, bearophile
Nov 15 2010
parent reply spir <denis.spir gmail.com> writes:
On Mon, 15 Nov 2010 12:44:24 -0500
bearophile <bearophileHUGS lycos.com> wrote:

 spir:
=20
 1. name objects automatically
 I need some objects to know their name (as field on themselves). the on=
ly solution I can else imagine is for the user write:
 	x =3D ...;
 	x.name =3D "x";
=20 What if you have two or more references to the same object?
I'm not sure what you mean. The said objects are patterns (instances of a P= attern class hierachy). And indeed, they are multiply referenced (that's pr= ecisely the point of writing a grammar ;-). Their names are used everywhere: * stand-alone pattern output * super-pattern output (subpatterns are replaced by their name, else output= explodes) eg operand: symbol|number * inside nodes they generate (eg allow discrimination between choices) * error output ("expected operand at position...") * result output for testing
 Regarding your generic question, I'd like DMD to have more static introsp=
ection, for for now ... Dynamic introspection would do the job for me, I guess. In python, would be= like (untested): def namePatterns (scope =3D var()): for (name,obj) in scope: if isinstance(obj, Pattern): obj.name =3D name Don't know how static would help better. Any idea how else to write somethink analog to format()? denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 15 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
spir <denis.spir gmail.com> wrote:

 On Mon, 15 Nov 2010 12:44:24 -0500
 bearophile <bearophileHUGS lycos.com> wrote:

 spir:

 1. name objects automatically
 I need some objects to know their name (as field on themselves). the  
only solution I can else imagine is for the user write:
 	x = ...;
 	x.name = "x";
What if you have two or more references to the same object?
I'm not sure what you mean. The said objects are patterns (instances of a Pattern class hierachy). And indeed, they are multiply referenced (that's precisely the point of writing a grammar ;-).
The two (or more) references could not be referred to by the same name. Sure, they would point to the same object, but how could the object know whether its functions were called from x or from y? While you may use the same name everywhere for the same object, the x in function foo is a different one from that in function bar, not to mention across modules. If I may come with a solution, it would be something like this: mixin template New!( T, string name ) { mixin( "auto " ~ name ~ " = new T(\"" ~ name ~ "\")"); } -- Simen
Nov 15 2010
parent spir <denis.spir gmail.com> writes:
On Mon, 15 Nov 2010 21:35:19 +0100
"Simen kjaeraas" <simen.kjaras gmail.com> wrote:

 spir <denis.spir gmail.com> wrote:
=20
 On Mon, 15 Nov 2010 12:44:24 -0500
 bearophile <bearophileHUGS lycos.com> wrote:

 spir:

 1. name objects automatically
 I need some objects to know their name (as field on themselves). the=
=20
 only solution I can else imagine is for the user write:
 	x =3D ...;
 	x.name =3D "x";
What if you have two or more references to the same object?
I'm not sure what you mean. The said objects are patterns (instances of=
=20
 a Pattern class hierachy). And indeed, they are multiply referenced =20
 (that's precisely the point of writing a grammar ;-).
=20 The two (or more) references could not be referred to by the same name. Sure, they would point to the same object, but how could the object know whether its functions were called from x or from y? While you may use the same name everywhere for the same object, the x in function foo is a different one from that in function bar, not to mention across modules.
Oh, yes! I see the point, now, thank you. But in the present case (and a fe= w others where I had to name objects), only the "birth" name is relevant. H= ere is an example from written using the library I'm writing, I'm sure it's= self-explaining. I overloaded opCall in the top Pattern class so that it n= ames the object: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D test code =3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D auto digits =3D new String(new Klass("0-9")); digits.name =3D "digits"; auto DOT =3D new Literal("."); DOT.name =3D "DOT"; auto integer =3D new Compose(digits,DOT,digits); integer.name =3D "integer"; // output writeln(digits.view()); writeln(integer.view()); writeln-); integer.test("123.45"); writeln-); integer.test("123."); =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D output =3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D digits=3D[0-9]+ integer=3D(digits DOT digits) --------------------------------- pattern : integer=3D(digits DOT digits) text : "123.45" outcome : "(123 . 45)" --------------------------------- Test Error: see outcome below. --------------------------------- pattern : integer=3D(digits DOT digits) text : "123." outcome : "*failure*" --------------------------------- ********************************* End Of Text Error: end of text reached while matching pattern digits=3D[0-9]+ at index 4 *********************************
 If I may come with a solution, it would be something like this:
=20
 mixin template New!( T, string name ) {
 	mixin( "auto " ~ name ~ " =3D new T(\"" ~ name ~ "\")");
 }
Thank you again, I'll explore this path. Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 16 2010