Advertisement

char support (both type and constant)

Started by December 07, 2005 05:16 AM
9 comments, last by WitchLord 18 years, 11 months ago
Hi, Maybe I'm missing something, but afaik there's no character support in the language. I think it could be a great feature to add. For example, to insert a character in a string at a given index, you would write something like :

myString[8] = ' ';
...
char value = myString[8];
switch( value )
{
  case ' ':
  ...
}
instead of :

myString[8] = 32;
...
int value = myString[8];
switch( value )
{
  case 32:
  ...
}
It would allow us to tear our ascii table :) Am I missing something ? Best Regards, Lbas
Lbas
EDIT: SORRY, completely misunderstood the question. Saw it on the front page and assumend it was C++.

[Edited by - CTar on December 7, 2005 6:28:14 AM]
Advertisement
I meant in the AngelCode scripting language.
Didn't I post it in the right thread ?
Lbas
Lbas
The char type is already represented by int8 (or uint8 if you prefer unsigned char).

The character literal though is not yet supported by AngelScript. It is supported by Deyja's preprocessor though, if you need it.

I may implement support for character literals in the near future. It shouldn't be too hard to do. If you feel like diving into the source code for the library you could do it yourself, and then send it to me as a contribution. :)

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Ok, I'll consider it.
Btw, you're right to say that chars are represented by (u)int8 because it's SBS only, but having a char type could prepare us to future MBS support in AS :)

Best Regards,
Lbas
Lbas
Good point. I'll keep that in mind :)

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
The preprocessor handles them fine. And, if you want a char type, you can always register it (Though you won't get implicit conversions then) or you can use the preprocessor to #define it (You wouldn't actually need to #define char int8 in each file - you can set it up so it's defined in every file preprocessed.)
My little contribution to 1.10 users ...

In as_tokenizer.cpp in bool asCTokenizer::IsConstant() line 257 :

	// String constant between double-quotes	if( source[0] == '"' || source[0] == '\'' )	{		bool evenSlashes = true;		int n, l;		for( l = 0, n = 1; n < sourceLength; n++ )		{			if( source[n] == '\n' ) break;			if(source[0] == source[n] && evenSlashes )			{				if (source[0] == '"') {					tokenType = ttStringConstant;					tokenLength = n+1;					return true;				}				else {					// Char type					if (l == 1) {						tokenType = ttIntConstant;						tokenLength = n+1;						return true;					}					return false;				}			}			if( source[n] == '\\' ) evenSlashes = !evenSlashes; else evenSlashes = true;			l += (evenSlashes ? 1 : 0);		}		tokenType = ttNonTerminatedStringConstant;		tokenLength = n-1;		return true;	}


and in as_compiler.cpp in void asCCompiler::CompileExpressionValue(asCScriptNode *node, asCByteCode *bc, asCTypeInfo *type) line 3159

			// TODO: Check for overflow			asDWORD val;			if (value[0] != '\'') {				val = asStringScanUInt(value, 10, 0);			}			else {				int n, l;				val = 0x00;				for( l = 0, n = 1; n < vnode->tokenLength; n++ )				{					if(value[0] == value[n])					{						break;					}					if( value[n] == '\\' ) {						n++;						switch (value[n]) {						// \a Bell (alert) 						case 'a' : val = '\a'; break;						// \b Backspace 						case 'b' : val = '\b'; break;						// \f Formfeed 						case 'f' : val = '\f'; break;						// \n New line 						case 'n' : val = '\n'; break;						// \r Carriage return 						case 'r' : val = '\r'; break;						// \t Horizontal tab 						case 't' : val = '\t'; break;						// \v Vertical tab 						case 'v' : val = '\v'; break;						default : val = value[n];						}					}					else {						val = value[n];					}				}			}			bc->InstrDWORD(BC_SET4, val);


This will allow to get 'x' in scripts. Those chars will then be treaten in AS as const int.

AbrKen.

EDIT : Change return true to true/false in IsConstant.

[Edited by - abrken on December 8, 2005 2:14:11 AM]
You need to support hex constansts as well.
Quote: Original post by Deyja
You need to support hex constansts as well.


Here it is !

in IsConstant :

	// String constant between double-quotes	if( source[0] == '"' || source[0] == '\'' )	{		bool evenSlashes = true;		int n, l, hexaValue;		for(hexaValue = 0, l = 0, n = 1; n < sourceLength; n++ )		{			if( source[n] == '\n' ) break;			if(source[0] == source[n] && evenSlashes )			{				if (source[0] == '"') {					tokenType = ttStringConstant;					tokenLength = n+1;					return true;				}				else {					// Char type					if (l == 1) {						tokenType = ttIntConstant;						tokenLength = n+1;						return true;					}					return false;				}			}						// '\xhhh'			if(!evenSlashes && (source[n] == 'x' || source[n] == 'X')) {				hexaValue = n;			}			if( source[n] == '\\' ) evenSlashes = !evenSlashes; else evenSlashes = true;			if (hexaValue && hexaValue != n) {				if( !(source[n] >= '0' && source[n] <= '9') &&					!(source[n] >= 'a' && source[n] <= 'f') &&					!(source[n] >= 'A' && source[n] <= 'F') )					hexaValue = 0; // Invalid hexa value				else					l = 0;			}			l += (evenSlashes ? 1 : 0);		}		tokenType = ttNonTerminatedStringConstant;		tokenLength = n-1;		return true;	}


and in CompileExpressionValue :

		if( vnode->tokenType == ttIntConstant )		{			GETSTRING(value, &script->code[vnode->tokenPos], vnode->tokenLength);			// TODO: Check for overflow			asDWORD val;			if (value[0] != '\'') {				val = asStringScanUInt(value, 10, 0);			}			else {				int n;				val = 0x00;				for(n = 1; n < vnode->tokenLength; n++ )				{					if(value[0] == value[n])					{						break;					}					if( value[n] == '\\' ) {						n++;						switch (value[n]) {						// \a Bell (alert) 						case 'a' : val = '\a'; break;						// \b Backspace 						case 'b' : val = '\b'; break;						// \f Formfeed 						case 'f' : val = '\f'; break;						// \n New line 						case 'n' : val = '\n'; break;						// \r Carriage return 						case 'r' : val = '\r'; break;						// \t Horizontal tab 						case 't' : val = '\t'; break;						// \v Vertical tab 						case 'v' : val = '\v'; break;						// \xhhh						case 'x' :						case 'X' : n++;											 val = asStringScanUInt(value+n, 16, 0);											 n = vnode->tokenLength;											 break;						default : val = value[n];						}					}					else {						val = value[n];					}				}			}			bc->InstrDWORD(BC_SET4, val);			type->Set(asCDataType(ttUInt, true, false));			type->isConstant = true;			type->dwordValue = val;		}


Enjoy !

AbrKen

This topic is closed to new replies.

Advertisement