Mailing List Archive

use function return value (array) in expression
Hello Developers,

I'm currently using nasl 2.2.10. I can use the result of a function call
in an expression for further evaluation if the result is of number or
string type. But if the result is an array I can not use it directly,
but have to assign the a variable. The following testcode makes this clear.

function get_num()
{
return 3;
}

if(get_num() == 3)
{
display("this works\n");
}

function get_str()
{
return "octect";
}

if(get_str() == "octect")
{
display("string works\n");
}

function get_array()
{
local_var a;
a["one"]=1;
a["two"]="zwei";
a["three"]="trois";
return a;
}

local_var res; res=get_array();
if(res["three"] == "trois")
{
display("this works too\n");
}

# but this does not :(
if(get_array()["three"] == "trois")
{
display("does not work\n");
}


Now my questions are:
1) Did I use the wrong syntax?
2) Does this work with nasl3?
3) Can it be fixed in nasl2?

--
---> Dirk Jagdmann ^ doj / cubic
----> http://cubic.org/~doj
-----> http://llg.cubic.org
_______________________________________________
Plugins-writers mailing list
Plugins-writers@list.nessus.org
http://mail.nessus.org/mailman/listinfo/plugins-writers
Re: use function return value (array) in expression [ In reply to ]
On Wed, 25 Jul 2007 23:29:40 +0200
Dirk Jagdmann <doj@cubic.org> wrote:

> But if the result is an array I can not use it directly,
> but have to assign the a variable.

Right. So...?

> if(get_array()["three"] == "trois")
> {
> display("does not work\n");
> }

You cannot dereference the return from the function like this. The
syntax is not supported. You have to put the return value into a
variable.
I cannot imagine a situation where such a syntax would be useful or
necessary.

_______________________________________________
Plugins-writers mailing list
Plugins-writers@list.nessus.org
http://mail.nessus.org/mailman/listinfo/plugins-writers
Re: use function return value (array) in expression [ In reply to ]
>> if(get_array()["three"] == "trois")
>> {
>> display("does not work\n");
>> }
>
> You cannot dereference the return from the function like this. The
> syntax is not supported. You have to put the return value into a
> variable.
> I cannot imagine a situation where such a syntax would be useful or
> necessary.

I don't imagine, I have that situation already. I'm currently writing a
compiler with nasl as the destination language. And my compiler likes to
generate complex (boolean expressions) like:

if((f1() == 1 && f2() == 3) || (f3() == "str" && f4() < 5) { ... }

Those functions are generated by the compiler and perform a test. Now it
would be convinient if a function can return an array/hash (as it
currently can) and then test one of the array items directly in an
expression. Generating the following code would make my (and most other
upcoming) compilers just more complex:

local_var v9; v9=f9();
local_var v8; v8=f8();
if(v9[3] == 3 || v8["test"] == "test") { ... }

And while we're at it the following features would be an enhancement too:

- array/hash comparison
- direct initialization of a declared variable
local_var a=3;
global_var g="nil";


--
---> Dirk Jagdmann ^ doj / cubic
----> http://cubic.org/~doj
-----> http://llg.cubic.org
_______________________________________________
Plugins-writers mailing list
Plugins-writers@list.nessus.org
http://mail.nessus.org/mailman/listinfo/plugins-writers