This topic is now archived and is closed to further replies.

Creating a psuedo-language using #defines

This topic is 5241 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve seen a bunch of demos where the programmers have kind of written their own language for their game using #defines. But I don''t understand certain aspects of this. Like how they completely get rid of braces. I understand you can do #define Blah(a) if(a){ , but I don''t understand how the ending brace gets accounted for.

Share this post

Link to post
Share on other sites
I think the real question you should be asking yourself is why people do this sort of crap.

To answer your question:
#define begin {
#define end }

void f()

That should be legal.

Share this post

Link to post
Share on other sites
There are some instances where it's nice. I wouldn't venture a whole 'language' out of macros, but you can do some creative and useful things with them. Say you're defining a language parser, and want similar keywords and enumerated values. You can write a separate token file that contains nothing but definitions like:

TOK( if )
TOK( else )

Your parser can use these defs by #defining TOK one way, and the interpreter can use them by #defining TOK another way:

#define TOK( sym ) TOK_##sym,
enum {
#include "tokens.h"
TOK_LastEntry };
#undef TOK

#define TOK( sym ) hr = m_Parser.AddKeyword( string( #sym ), TOK_##sym ); if( FAILED( hr ) ) return hr;
#include "tokens.h"
#undef TOK

One of the game gems books has a few good examples of this. I use it a lot. It actually makes things more readable, because the token defs are all in one place. I'm sure some people would disagree.

I like pie.

[edited by - RenderTarget on August 6, 2003 9:58:38 PM]

Share this post

Link to post
Share on other sites
Guest Anonymous Poster

A good example of an x86 emulator using #defines to sort of come up with the authors own language used in main().

PS: Those IOCCC guys are freaks! My god the code is beyond description. Not so much the link above but of the others. One guy wrote a program that used 1 C keyword...'char' and that was it.

here's a link to a nice little flight sim (1536 bytes of code)


Sorry for the cross post but the #defining a custon language reminded me of the obfuscated C contest.

[edit: enabled hyperlink]

[edited by - Magmai Kai Holmlor on August 6, 2003 12:20:54 AM]

Share this post

Link to post
Share on other sites
I wasn''t talking about re-writing the entire, language, just creating a psuedo-one for a few tasks. I would like to ust it so save typing. Instead of if(blah.k()==q || g.k()==q)), I could have ifDef(q, q). Here''s an example I was trying to go from:

.cpp file


OnMsg( MSG_DestroyMachine )

OnMsg( MSG_CheckOccupancy )
if (m_Owner->NextTileOccupied())

m_timer = 0;


m_timer = 0;

.h file

#define BeginStateMachine if( state < 0 ) { char statename[64] = "STATE_Global"; if(0) {
#define EndStateMachine return( true ); } } else { assert( 0 && "Invalid State" ); return( false ); } return( false );
#define State(a) return( true ); } } else if( a == state ) { char statename[64] = #a; if(0) {
#define OnMsg(a) return( true ); } else if( EVENT_Message == event && msg && a == msg->GetMsgName() ) { g_debuglog.LogStateMachineEvent( m_Owner->GetID(), msg, statename, #a, true );
#define OnEvent(a) return( true ); } else if( a == event ) { g_debuglog.LogStateMachineEvent( m_Owner->GetID(), msg, statename, #a, true );
#define OnUpdate OnEvent( EVENT_Update )
#define OnEnter OnEvent( EVENT_Enter )
#define OnExit OnEvent( EVENT_Exit )

Share this post

Link to post
Share on other sites