aboutsummaryrefslogtreecommitdiff
path: root/documentation/typing.md
blob: 57b792dd1169dede4bdac391d691e2739f382488 (plain)
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
# 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 num

`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
```