Local variable declarations / Intellisense

Started by
11 comments, last by WitchLord 13 years, 1 month ago
Hi,
I’m trying to build an intellisense solution for my editor and so I need to build up a table of declared classes and variables etc.
Using the normal GetxxxCount()/GetxxxID methods I can build up most of these values with the exception of local variables.

How can find the local variables declared in a class method or global function?

Any help is very appreciated and finally: great scripting engine – thanks!

[Edited by - Carrot on September 22, 2010 1:48:48 PM]
<a href="http://www.purplenose.com>purplenose.com
Advertisement
Okay then, any idea how (or if it's even possible) to get the parse tree?

<a href="http://www.purplenose.com>purplenose.com
Hmm. I haven't designed the interface to give access to the local variables in functions for intellisense, though they are available for debugging while running a script.

You should be able to get the information you want from the asIScriptContext after calling Prepare with the function id. Though it is a bit awkward I guess.

void PrintLocalVariables(int funcId, asIScriptEngine *engine){  asIScriptContext *ctx = engine->CreateContext();  ctx->Prepare(funcId);  int c = ctx->GetVarCount();  for( int n = 0; n < c; n++ )    printf("%s", ctx->GetVarName(n);  ctx->Release();}


The parse tree isn't exposed, and will most likely never be. Though if you wish to parse the script yourself, you can take a look at the CScriptBuilder which does superficial parsing to find declarations of functions, variables, etc.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Hi.
I appreciate the reply, thanks.
I had tried your suggestion already, but messed around again and managed to get local variables from global functions working okay, but I still have problems with classes.
The following code excerpt is what I use to iterate through classes:


/* Find classes declarations */for(int i=0; i < module->GetObjectTypeCount(); i++){	asIObjectType* obj = module->GetObjectTypeByIndex(i);	cout << "Class: " << obj->GetName() << endl;	/* Loop for each method...	 */	for(int b=0; b<obj->GetMethodCount(); b++)	{		asIScriptFunction* method = obj->GetMethodDescriptorByIndex(b);		cout << "\t" << method->GetDeclaration() << endl;		/* Loop for each local variable...		 */		r = context->Prepare(method->GetId());		for( int n = 0; n < context->GetVarCount(); n++ )		{			cout << "Local: " << context->GetVarName(n) << endl;		}		context->Unprepare();	}}


The problem is that GetVarCount always returns zero for a method.
Am I doing something wrong here?
<a href="http://www.purplenose.com>purplenose.com
This is because the function id is the virtual function id, only when the execution starts will the real function be known.

I'll have to think about what can be changed to let you get to the information you need. It will probably be to expose the real function id in the asIObjectType interface.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I've made a couple improvements to the interface to expose the variables in the funcions, and also to allow the retrieval of the real function for class methods.

You will now be able to enumerate the variables without the context:

void PrintLocalVariables(int funcId, asIScriptEngine *engine){  asIScriptFunction *func = engine->GetFunctionDescriptor(funcId;  int c = func->GetVarCount();  for( int n = 0; n < c; n++ )  {    const char *name; int typeId;    func->GetVar(n, &name, &typeId);    printf("%s", name);  }}


Also, to get the real function from a class declaration you should pass the second parameter as false to the GetMethod function.

asIScriptFunction *virtualFunc = objType->GetMethodDescriptorByIndex(0 /*, true */);asIScriptFunction *realFunc = objType->GetMethodDescriptorByIndex(0, false);


These changes are available in the SVN, revision 702.

Let me know if you give it a try and if you find any problems with it.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

That's great news, thanks. I'll try it out as soon as I get a chance.
Ciaran.
<a href="http://www.purplenose.com>purplenose.com
That worked perfectly with no context needed! Thanks.
But am I correct in thinking that I'll still need to create the context to find the line-number information?

(I'll need this for calculating function/class definitions for calculating variable scope)
<a href="http://www.purplenose.com>purplenose.com
Currently the line numbers are only known for bytecode instructions, this can be used to set break points or pin-point where exceptions occur.

I do not store the line number where functions or variables are declared. I would have to change the compiler to store this information as well if this is what you want.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

hi Andreas,

I'm sure it would be helpful to others writing IDEs if this information was available, but if it's too big a job to change in the compiler then
alternatively the information could be calculated from by looking for matching braces for example.
It seems to be calculating what could be already available though...

Of course I don't expect you to change it just on my request, but if you think others would find it helpful then great! :)

Thanks,
Ciaran
<a href="http://www.purplenose.com>purplenose.com

This topic is closed to new replies.

Advertisement