1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# 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.
|