vishapoberon / compiler

vishap oberon compiler
http://oberon.vishap.am
GNU General Public License v3.0
186 stars 25 forks source link

variable args, variadic functions #20

Closed z505 closed 8 years ago

z505 commented 8 years ago

Hi this is not as much of an issue, more of a question or comment. I could not figure out how to make a comment on the Vishap blog or home page. I can't log in to wordpress because there is no "register" feature, to create an account to comment with, that I could find.

You know how C and GoLang have variadic functions (functions with variable arguments) or in the case of freepascal they call them VARARGS. ... With your Vishap compiler tool have you ever come across C functions that have multiple arguments, variable amount, not a fixed number of arguments? Does your compiler have an ability to make variable number of arguments? Or does your compiler have the ability to link to C libraries that use a variable amount of arguments?

http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html

Similar to standard pascal's WriteLn which allows multiple types:

writeln("string", 6543, float)

WriteLn in freepascal is like a variadic function in GoLang.

Or there is things like Printf and such.

What happens if there is a C dll that has variable amount of args... , can you link to that DLL and use the function in oberon?

This is an incompatibility that exists between C and Oberon, as Wirth is against such "features" in programming languages, even though his WriteLn in standard pascal was basically the same thing (but compiler magic).

For example in Oberon you have to go: console.WriteString("some string"); console.WriteInt(4634);

Instead of console.Write("some string", 4634);

Whereas standard pascal and C allowed you to do that, as does GoLang.

So when a C library has variable amounts of arguments how would oberon deal with it?

norayr commented 8 years ago

hey, thank you for the comment. yes, I believe github is more convenient for discussion than wordpress comment system.

as you said, Oberon has no vararg functions. however, it's quite possible to link to vararg C functions, and I believe, I have done it several times.

the only thing you need, is to create a different Oberon function for each vararg C variant you want to call. For example, imagine there is a C function int add_em_up (int count,...)

then we would create several Oberon wrappers:

``IMPORT SYSTEM; (* this is necessary for modules that link to C code *) PROCEDURE -AddEm(a, b: INTEGER): INTEGER "add_em_up(a, b)";

PROCEDURE -AddEm1(a, b, c: INTEGER): INTEGER "add_em_up(a, b, c)";

PROCEDURE -AddEm2(a, b, c, d: INTEGER): INTEGER "add_em_up(a, b, c, d)";``

Is that enough for you, or do you need varargs in Oberon language?

z505 commented 8 years ago

Hi, how about the calling conventions... STDCALL vs CDECL vs PASCAL call? How does oberon link up to different declarations, STDCALL and such.

I would ideally enjoy an oberon language with VARARG ability, similar to GoLang, and also a STRING type like in Eberon. Rather than always using ARRAY OF CHAR, a string similar to GoLang and Eberon (extended oberon). https://github.com/vladfolts/oberonjs/wiki/Eberon-Strings

However that is a separate comment :-)

Do you ever find the need for VarArgs like pascal's old convenient WriteLn that was more generic and not so fixed?

I understand Wirth's objection to features as they can cause abuse, however in GoLang these have proven extremely useful.

norayr commented 8 years ago

Hey, hey. (:

How does oberon link up to different declarations, STDCALL and such. I believe the same way.

You've probably noticed thath voc generates C code. And that PROCEDURE -Something syntax is a very simple and universal way to define wrappers.

Once I've managed (as I remember) to duscuss that with Gutknecht and Wirth. We decided to not introduce pragmas for the different calling conventions for the port of O-7 compiler I was writing.

I also agree, that it's better to make it as a compile time flag (that would imply that all function wrappers in a given module use that calling convention), or use same PROCEDURE -Something syntax.

As you may notice in the source files, like Unix.Mod, this syntax is not necessarily used in order to define wrappers, but also let's say to include some header.

also a STRING type like in Eberon

On strings, they can be implemented in libraries. I was thinking about the strings a lot, and came to the conclusion that it complicates the language and leads to ambiguities. I believe that it's better to keep strings out of the language.

I personally use unicode (utf-8) strings with voc a lot, by having only one byte character type.

I dislike the syntax which uses '+' for string concatenation, because, though it's fast to write, but then, which is the '-' operator for strings? What's '/' then? And if we would like to write faster, there are many scripting languages that created by keeping that in mind.

So, in short, code procedures (when name is prefixed with '-' sign) are used to create wrappers, and calling convention does not matter, I believe.

Anyway, Oberon has to gather a community, and different compilers have to compete for features, users. So I am glad there is an interest, independently from my personal opinion. I can hope that voc can turn out to be useful as is now, but we are trying to move it forward to some directions, and a lot of work have been already done.