# QSP Type system Internally, there is only two types in the engine : String and Integer. This is represented with this C struct : ```C typedef struct { union { QSPString Str; int Num; } Val; QSP_TINYINT Type; } QSPVariant; ``` ## Variables and assignation The type of a variable is determined by the first character in the name: ```C #define QSP_STRCHAR QSP_FMT("$") // … #define QSP_VARBASETYPE(name) (*(name).Str == QSP_STRCHAR[0]) /* QSP_TYPE_STRING | QSP_TYPE_NUMBER */ ``` But during the assignation, a conversion is automatically done when the types does not match : ```C INLINE void qspSetVar(QSPString name, QSPVariant *val, QSP_CHAR op) { … if (QSP_VARBASETYPE(name) != QSP_BASETYPE(val->Type)) { if (!qspConvertVariantTo(val, QSP_VARBASETYPE(name))) { qspSetError(QSP_ERR_TYPEMISMATCH); return; } qspSetVarValueByReference(var, index, val); } ``` This can raise uncontrolled error with some functions like `INPUT` if you directly the result into a numeric variable. ## Conversion and comparaison between values Most of the functions provide automatic conversion between them: a string will be converted in integer if the other operand is an integer: - math operation That’s why `'1' + 1` will give the result `2` and not `'11'` - comparaison For example, the source code for the comparaison is the following one: ```C case qspOpEq: QSP_NUM(tos) = QSP_TOBOOL(qspAutoConvertCompare(args, args + 1) == 0); break; ``` When string cannot be converted into a number, the number will be converted in string, and the comparaison will compare thoses two strings. In the ASCII table, a text will always be greater than a number (`'a' > 9999` will return `-1` and `'$' > 0` will return `0`) ## Explicit conversion ### From string to integer `isnum(…)` will tell you if a value is an integer or not: ``` isnum('123') -> -1 isnum('abc') -> 0 ``` Using `val(…)` you can convert a string into a integer: ``` val('123') -> 123 val('abc') -> 0 ! No error here ``` ### From integer to string `str(…)` will convert a number in string. ## QSP Syntax Parser > [!NOTE] > This parts describe how QSP Syntax Parser deal with the type system. This is > not a part of the language reference. ### No implicit conversion *Explicit is better than implicit.* Implicit conversion is a bad thing. It can raise errors during the game if you don’t care, and can hide other error in the code: This code is valid, but does not work as expected (because of a `$` is missing in the if clause, `$myvar` and `myvar` are two differents variables): ``` $myvar = 'VALUE' … if myvar = 'VALUE': … ``` In order to spot this kind of errors, a warning will be raised here. ### The boolean type I’ve added a boolean type which does not exists in the original syntax. In QSP, the value `-1` stand for `True` and `0` for `False.` Because *Readability counts.* , the application will raise a warning if a String or an integer is directly given in an if clause. A comparaison operator is required.