Nmap Development mailing list archives

[SoC] Proposal for NSE standard library functional facilities


From: "Duilio Protti" <dprotti () fceia unr edu ar>
Date: Sat, 24 Mar 2007 13:26:47 -0300 (ART)

I'm not sure what are the functional programming facilities desired for
NSE but I think one of them should be something similar to list
comprehension as in Haskell or Python. Just to remind you, for L =
[16,4,28,7]:

[2*x | x <- L, x < 10]      (Haskell)
[2*x for x in L if x < 10]  (Python)

the above lists will be [8,14] in the respective languages.
In general, we will have (supposing we allow only one predicate, to
simplify the example) something like:

  [M | x in L, P]     (idea 1)

where L is a list (it could be another comprehension list), P and M are
respectively a predicate and a mapping over the elements of L and there is
a  binding of x in P and M.

The Lua interpreter embedded in NSE could be extended with a little of
syntactic sugar in order to handle this kind of expressions. (idea 1)
could be translated into a call to a function lc() like the following

function lc (L,P,M)
  function list_iter (t)
    local i = 0
    local n = table.getn(t)
    return function ()
              i = i + 1
              if i <= n then return t[i] end
           end
  end
  t = {}; i = 0
  for x in list_iter(L) do
    if P(x) then
      i = i + 1
      t[i] = M(x)
    end
  end
  return t
end

To obtain the list mentioned in the first example, we should translate the
Lua expression

  [2*x | x in {16,4,28,7}, x < 10]

to a call equivalent to

lc({16,4,28,7},
    function (x) return x < 10 end,
    function (x) return 2 * x end)

I have a general picture of how to implement this, but first I would like
to know if this kind of feature is what the project is looking for.

Some final remarks about the proposal:
 - the syntax of the language will be extended with two new tokens: '|'
and 'in', and an "overload" for '[]' (I have an idea of how to handle
this in lparser.c:primaryexp())
 - the syntax [M | x in L, P] is just a suggestion, for sure there will be
another nice ways of doing it.
 - it's not difficult to implement the range() and xrange() python-like
list generators. Even better, we can handle objects which its own range()
defined, so i.e. an IPAddress object will known how to generate an IP
range.

Finally, about the question of "why don't doing it just in Lua (i.e. using
iterator's power?" my answer is: because it have to be *simple*. The
mechanism must be nice for a network expert, without the needing of
lua-specific-guru-skills. This is a case where a little of syntactic sugar
helps a lot.

What do think about the idea?

Cheers,
Duilio Protti.

http://www.fceia.unr.edu.ar/~dprotti/



_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://SecLists.Org


Current thread: