Jump to content

multi-pass preprocessing

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
kenna

kenna

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Is it possible to use something like this in C? Multi-pass preprocessing that is.

#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'm trying to write a simple HDL (Hardware Description Language) in C, since Lex is too bothersome.

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
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:
void func1()
{
    #define FUNC1_USED
    // ...
}

#ifdef FUNC1_USED
void func2()
{
    // ...
}
#else
    #error func1() has to be used, before func2()
#endif
I kinda feel like I'm on the wrong track about my understanding of what you want to do...

#3
kenna

kenna

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
The purpose is to allow users to write something like this:

-- (this is a comment)

-- define process
process NAME begin
    -- beginning of normal C code
    OUT = A and B;
end

and have it translated to this:

// (this is a comment)

// define process
void NAME(void) {
    // beginning of normal C code
    OUT = A and B;
}

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.

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

#4
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
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:
#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
It's in danish, yes, but I think you get the idea. Translated into english, it would be something like:
#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
And in normal C++:
#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;
}
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.

#5
kenna

kenna

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
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 ^^;

#6
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
Nice, are you from Sweden or Finland?
Anyways, here you get the full source from dansk.hpp:
//////////////////////////////////////////////////////////////////////////////////////////
//
//		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
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.

EDIT: Sorry for the bad indentation in the code, but I think my IDE screwed it up. It's still readable, though.

#7
kenna

kenna

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
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 ^_^

#8
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
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.)

#9
kenna

kenna

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
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 early ^^ ROFL

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?

#10
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
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?

#11
kenna

kenna

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Icelandic has not evolved as much as the other languages, so it's much closer to norse than our languages. I think I have some really good bookmarks somewhere that I could give you. They might make it easier to understand that supid, silly, complex system of finnish. It's one of my mother tongues so I don't need to worry about it, I know it by heart, but whenever I have to THINK about it, I go all crazy ^~^

--------------------------------

The problem is that I'm not very good with neither threads nor emulation.

malloc(n); // memory, global array

void t_sysc(){} // system controller thread

void t_cpu_sisd0(){} // cpu sisd0 thread
void t_cpu_sisd1(){} // cpu sisd1 thread
void t_cpu_simd(){} // cpu simd thread
void t_cpu_misd(){} // cpu misd thread

void t_foobar(){} // device thread, e.g. gpu, network, apu, math, etc.

The centre of the design is the system controller that handles communication between the various devices (interrupts). It multiplexes input interrupts and sends them to the devices. The system controller also handles power management and other system related features, i.e. not restricted to only one device.

The first problem here is communication, no problem in linux, but windows doesn't allow the use of signals, so I'm left with polling, or something equally horrible.

The data bus (direct transfer of data between devices) is probably going to be sockets or memory sharing.

I guess that the inter-thread communication is the biggest problem. But mainly I was just wondering about things I should avoid.

It's an architecture of my own btw, since I'm having so much difficulty learning the x86 architecture with it's modes and other mindblowing stuff O_O ^_^ Still way I'll also learn alot, and I'm also working on a space simulator far different from the usual crap, where I'm going to use emulation quite a lot, for all the spaceship computers ^^

Hmm...hope that made some sense...

#12
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
It would be great with those bookmarks.
If you want to, you can send me an e-mail, or add my MSN, if you're using MSN Messenger or send me a private message. You can find all the information needed, in my profile. That would be better, instead of having us two, flooding the forum with our chit-chat. :-)

- - -

I don't think I'm able to helping you on this one. This is simply to hard for me, sorry. I don't have much experience with x86, yet. Anyways, what you're doing looks interesting, and if you finds an answer, let me know.