preliminary support of variadic MELT functions.

1 view
Skip to first unread message

Basile Starynkevitch

unread,
Oct 4, 2011, 10:48:48 AM10/4/11
to gcc-...@googlegroups.com
Hello All,

I just committed (into revision 179493 of the MELT branch) some preliminary
support for variadic MELT functions, that is MELT functions accepting a
variable number of arguments (there should be at least one argument, and it
is a value, since every MELT function has a value as its first argument, if
any).

[We don't have variadic primitives, and I don't think they could be useful.
We just have variadic functions.]

Variadic functions in MELT should deal not only with variable number of
arguments, but also variable kind of argument. We want to be able to pass a
value, a long, a c-string, another value, a long stuff to a variadic
function. So if FOO is bound to some variadic function, we want to be able to call
-when N is a :long stuff, T is a :tree stuff, V, W are :values
(FOO V N)
(FOO V T N W)
(FOO V "aa" T N W)
(FOO V)
if FOO is variadic after its first formal argument.

Variadic formals are ending with :rest. Think of :rest playing the same role
as ... in C. So the formal arguments list
(f x :rest)

should be understood as a formal arguments for at least two arguments (and
possibly more variadic arguments), the first, bound to f being a value, the
second, bould to x being also a value, and the rest being variadic.

Probably, the formals of our FOO from above examples are something like
(U :REST)
that is, FOO accept a first formal value argument U and some more variadic ones.

We need a way to parse the variadic parts of the formals, that is to get the
various variadic actual arguments into formal ones. So, to get our variadic
arguments, we need to be inside a (FOREVER ...) loop.

I addad the (AGAIN <loop-label>) syntax (similar to C's continue) to start
again the (FOREVER <loop-label> ...) enclosing loop. It is useful for
variadic argument parsing, but it is also useful independently.

To parse variadic arguments, I added a new language construct, (VARIADIC
....) which is often called inside a loop (much like va_arg in C is often
inside loops). It is a bit like a va_start ... va_end block in C.


a (VARIADIC ...) expression don't give any result, and It has a sequence of
variadic-cases, the last one could be :ELSE like in COND. But most
variadic-cases start with a formal argument list, which should fit into the
current variadic arguments. (It cannot be a match or a pattern, because
variadic arguments can be any things, either values or stuff).
So a variadic-case like
((:LONG L :VALUE V)
(do-something-with L V))

Means: if the current variadic arguments are a long and a value, consume
them and bind them to L and to V, and then call do-something-with on L and
V.


Here is my first variadic test case, in file gcc/testsuite/melt/tvariadic.melt

;; file tvariadic.melt
(defun varifun (f x :rest)
(let ( (:long sum 0) )
(forever
parseargsloop
(variadic ( (:value nv)
(setq sum (+i sum (get_int nv)))
(again parseargsloop))
( (:long n)
(setq sum (+i sum n))
(again parseargsloop))
( ()
(exit parseargsloop))
( :else
(assert_msg "INVALID argument")
(return))
))
(debuglong "varifun sum" sum)
(f x sum))
)

(varifun
(lambda (y :long s)
(let ( (t (tuple y (make_integerbox discr_integer s)))
)
(debug_msg t "varifun gave t")
t))
'a
'1 () 2 3)

;; eof tvariadic.melt


Do you understand that code?

Comments are welcome.

Cheers.

--
Basile STARYNKEVITCH http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mines, sont seulement les miennes} ***

Reply all
Reply to author
Forward
0 new messages