Creating a psuedo-language using #defines
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.
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()
begin
end
That should be legal.
To answer your question:
#define begin {
#define end }
void f()
begin
end
That should be legal.
I think C+ is fine. I tried it, but I used typedefs. It turned out okay, but was useless.
Scott Simontis
Big Joke: C#
Scott Simontis
Big Joke: C#
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:
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]
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:
//script.h#define TOK( sym ) TOK_##sym, enum {#include "tokens.h" TOK_LastEntry };#undef TOK//script.cpp#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]
quote:Original post by Village Specialton
I think C+ is fine. I tried it, but I used typedefs. It turned out okay, but was useless.
WTF is "C+" and what do typedefs have to do with anything?
anonymous.c
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)
banks.c
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]
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)
banks.c
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]
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 fileBeginStateMachine OnMsg( MSG_DestroyMachine ) m_Owner->MarkLastMachineForRemoval(); OnMsg( MSG_CheckOccupancy ) if (m_Owner->NextTileOccupied()) SetState( STATE_WAITING ); ///////////////////////////////////////////////////////////////// State( STATE_INIT ) OnEnter m_timer = 0; SetState( STATE_FINDINGPATH ); OnUpdate m_timer++; OnExit 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 )
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement