Is it possible to use something like this in C? Multi-pass preprocessing that is.
I'm trying to write a simple HDL (Hardware Description Language) in C, since Lex is too bothersome.Code:#define proc0 name; \ /* define proc0 */ ( #define flag0 \ /* define flag0 */ int name(void){ ) #if defined(flag0) /* check for flag0 */ #define proc1 name; \ /* define proc1 */ ( int name(void){ ) #else #error proc0 must be used before proc1 #endif
I don't understand what you're trying to do. Can you specify it a bit?
Is it something like this you want to do; If the function func1 has been used, then func2 can be used as well. But if func1 hasn't been used, then func2 cannot be used as well? It could be done like this:
I kinda feel like I'm on the wrong track about my understanding of what you want to do...Code:void func1() { #define FUNC1_USED // ... } #ifdef FUNC1_USED void func2() { // ... } #else #error func1() has to be used, before func2() #endif
The purpose is to allow users to write something like this:
and have it translated to this:Code:-- (this is a comment) -- define process process NAME begin -- beginning of normal C code OUT = A and B; end
so even though your example would work, it would require the user to write in C, instead of the simplified HDL, and it would allow the user to more easily write malicious code.Code:// (this is a comment) // define process void NAME(void) { // beginning of normal C code OUT = A and B; }
Only within a certain region can you use normal C, and even then restricted. I might be asking for a bit too much from the C preprocessor though ^~^, just experimening.
The trick here is that the code is VERY strict, and has to be written in a certain way.
1. interface -- the physical pinout, connectors, switches, etc.
2. device -- logical interface, ports, registers, immediate memory, etc.
3. circuit -- a smaller unit that handles a certain operation
4. process -- the process of the device
Last edited by kenna; 08-13-2007 at 10:07 AM.
It's possible to "convert" C++ into "another language," using preprocessors. I've done this my self once, just for fun. I converted a lot C++ into the danish language (my native language) and it worked quite fine, though it was different.
There's some limit though. It's, for example, not possible to change the look or representation of comments.
My programs could look like this, for example:
It's in danish, yes, but I think you get the idea. Translated into english, it would be something like:Code:#include <iostream> #include "dansk.hpp" // Funktionen "FUNKTIONUDENPARS" tom som returtype til funktionen FUNKTIONUDENPARS uden_parameterliste indhold til funktionen er kald funktionen printf med vaerdien "Hello, World!\n" og_ikke_mere slut paa funktionen // Funktionen "FUNKTIONMEDPARS" tom som returtype til funktionen FUNKTIONMEDPARS med parameterlisten med tallet X og tallet Y indhold til funktionen er kald funktionen printf med vaerdierne "%d, %d\n" og X og Y og_ikke_mere slut paa funktionen // Funktionen "main" heltal som returtype til funktionen main uden_parameterliste indhold til funktionen er kald funktionen FUNKTIONUDENPARS uden_vaerdier kald funktionen FUNKTIONMEDPARS med vaerdierne 10 og 20 og_ikke_mere kald funktionen system med vaerdien "pause" og_ikke_mere returner 0; slut paa funktionen
And in normal C++:Code:#include <iostream> #include "dansk.hpp" // The function "FUNCTIONWITHOUTPARMS" void as returntype for the function FUNCTIONWITHOUTPARMS without_parameterlist content of the function is call the function printf with the values "Hello, World!\n" and_nothing_else end of the function // The function "FUNCTIONWITHPARMS" void as returntype for the function FUNCTIONWITHPARMS with the parameterlist including the number X and the number Y content of the function is call the function printf with the values "%d, %d\n" and X and Y and_nothing_else end of the function // The function "main" number as returntype for the function main without_parameterlist content of the function is call the function FUNCTIONWITHOUTPARMS without_values call the function FUNCTIONWITHPARMS with the values 10 and 20 and_nothing_else call the function system with the value "pause" and_nothing_else return 0; end of the function
It's kinda special, but works and is ready to compile, using any decent compiler. If you want to see the file "dansk.hpp," though it's danish, just tell me.Code:#include <iostream> #include "dansk.hpp" void FUNKTIONUDENPARS() { printf("Hello, World!\n"); } void FUNKTIONMEDPARS(int X, int Y) { printf("%d, %d\n", X, Y); } int main() { FUNKTIONUDENPARS(); FUNKTIONMEDPARS(10, 20); system("pause"); return 0; }
Lol, speaking swedish (finland-swedish) I did manage to understand most of it, although I find danish more difficult than norwegian ^_^ but awesome work anyway!
Sure, I can't find many "advanced" preprocessing tutorials on the internet;
Nice, are you from Sweden or Finland?
Anyways, here you get the full source from dansk.hpp:
As you see, there's no advanced preprocessing, but only simple directives. Furthermore, you can see the words under "Normale ord" ("Normal words"), they are just defined, without a value. It's so the code is easier and "better" to write, so the look like danish.Code:////////////////////////////////////////////////////////////////////////////////////////// // // Sprog: Dansk // Projekt: Programmering på dansk // Programmør: N. 'v0id' O. // E-mail: mail@v0id.dk // Hjemmeside: www.v0id.dk // Dato: 25 April, 2007 // Version: 1.1, beta // Beskrivelse: // Inkludér denne fil, og du vil kunne programmere // som du snakker, bogstaveligt talt. // Det er meget frit hvordan ens program skal se ud, // og man skal selvfølgelig også have lidt evner indenfor // dansk, og kunne stave nogenlunde. // Dette er kun en beta-udgave, så feed-back er velkommen! ////////////////////////////////////////////////////////////////////////////////////////// // Operatorene #define array [] #define arrayet array #define parameterlisten ( #define uden_parameterliste parameterlisten #define vaerdierne parameterlisten #define vaerdien parameterlisten #define og , #define samt og #define indhold ){ #define og_ikke_mere ); #define slut } #define uden_vaerdier (); ////////////////////////////////////////////////////////////////////////////////////////// // Normale ord #define med #define er #define returtype #define til #define funktionen #define paa #define indeholder #define der #define som #define kald ////////////////////////////////////////////////////////////////////////////////////////// // C++'s nøgleord #define assembly_kode asm #define lokal auto #define lokalt lokal #define automatisk lokal #define boolesk bool #define bryd break #define tilfaelde case #define sag tilfaelde #define grib catch #define tegn char #define bogstav tegn #define bogstaver tegn #define klasse class #define konstant const #define konstante konstant #define konstant_kast const_cast #define fortsaet continue #define standard default #define slet delete #define goer do #define dobbelt double #define dobbel dobbelt #define dynamisk_kast dynamic_cast #define ellers else #define enumerator enum #define enumeration enumerator #define udtryksfuld explicit #define udtryksfuldt udtryksfuld #define eksport export #define eksporter eksport #define ekstern extern #define eksternt ekstern #define falsk false #define ukorrekt falsk #define kommatal float #define for for #define ven friend #define gaa_til goto #define hvis if #define direkte inline #define heltal int #define tal heltal #define tallet heltal #define lang long #define langt lang #define muterbar mutable #define navnerum namespace #define ny new #define nyt ny #define operator operator #define privat private #define private privat #define beskyttet protect #define beskyt beskyttet #define offentligt public #define offentlig offentligt #define ekstra_speed register #define genfortolket_kast reinterpret_cast #define genfortolk_kast genfortolket_kast #define returner return #define kort short #define signeret signed #define stoerrelsen_af sizeof #define statisk static #define struktur struct #define skift switch #define skabelon template #define denne this #define dette denne #define kast throw #define sandt true #define korrekt sandt #define rigtigt sandt #define proev try #define definer_type typedef #define type_identifikation typeid #define forsamling union #define usigneret unsigned #define brug using #define bruger brug #define virtuel virtual #define virtuelt virtuel #define tom void #define ingenting tom #define ikke_aendringsbar volatile #define bredt_tegn wchar_t #define brede_tegn bredt_tegn #define bredt_bogstav bredt_tegn #define brede_bogstaver bredt_tegn #define imens while #define mens imens
EDIT: Sorry for the bad indentation in the code, but I think my IDE screwed it up. It's still readable, though.
your IDE probably uses tabs instead of spaces, that's usually what destroyes the indentation for me X_X
I'm from Finland, near the city of Vasa ^_^
Yeah, it's because my IDE is using tabs instead of spaces.
Oh, nice, that's funny. I was actually born in Finland, but at an age of one, my family moved to Denmark. I was born in Turku, and lived in the small town Kustavi. All of my dads family is in Finland, and I was actually visiting them two weeks ago. I'm visiting them every second year, and stays for two weeks. It's really great (except for the mosquitoes. lol.) I understand some finnish, but I'm unable to speak it, though.
Hyväa Joulua! (The only thing I can spell. lol.)
Awesome ^_^! Nice to see a fellow finn(although only for one year)/scandinavian here. I find it truly interesting how we share the same language (north germanic) and the differences we have.
My main mother tongue is finland-swedish (but I also speak fluent finnish), so I'd always want to travel to norway, denmark, and iceland sometime, to experience their language first hand.
Hyvää Joulua, although a bit earlyROFL
Btw, on a completely different subject, do you have any suggestions as to how I should implement an emulator, to keep as much speed as possible? I am going for a realistic approach, so there will be memory, processor, bus, and system controller.
Are there any common traps I should look out for?
Icelandic is completely different from danish, norwegian and finnish, so I think it would be hard for you to understand. I don't understand a single word of it neither. I've been planning to learn finnish before next summer, where some of my family comes to Denmark. It would be nice if I for the first time in my life actually could talk easily with them.
About your question; I don't really understand what you want to do. Can you make an complete of how you want it to look like, and what it afterwards should do?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks