summaryrefslogtreecommitdiffstats
path: root/src/CBot/CBotProgram.cpp
blob: 1944f9202a61d8ce796d2cb5d2991e578fb951eb (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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
// * This file is part of the COLOBOT source code
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see  http://www.gnu.org/licenses/.

//////////////////////////////////////////////////////////////////////
// database management of CBoT program

#include "CBot.h"
#include <stdio.h>

CBotProgram::CBotProgram()
{
    m_Prog      = NULL;
    m_pRun      = NULL;
    m_pClass    = NULL;
    m_pStack    = NULL;
    m_pInstance = NULL;

    m_ErrorCode = 0;
    m_Ident     = 0;
    m_bDebugDD  = 0;
}

CBotProgram::CBotProgram(CBotVar* pInstance)
{
    m_Prog      = NULL;
    m_pRun      = NULL;
    m_pClass    = NULL;
    m_pStack    = NULL;
    m_pInstance = pInstance;

    m_ErrorCode = 0;
    m_Ident     = 0;
    m_bDebugDD  = 0;
}


CBotProgram::~CBotProgram()
{
//  delete  m_pClass;
    m_pClass->Purge();
    m_pClass    = NULL;

    CBotClass::FreeLock(this);

    delete  m_Prog;
#if STACKMEM
    m_pStack->Delete();
#else
    delete  m_pStack;
#endif
}


bool CBotProgram::Compile( const char* program, CBotStringArray& ListFonctions, void* pUser )
{
    int         error = 0;
    Stop();

//  delete      m_pClass;
    m_pClass->Purge();      // purge the old definitions of classes
                            // but without destroying the object
    m_pClass    = NULL;
    delete      m_Prog;     m_Prog= NULL;

    ListFonctions.SetSize(0);
    m_ErrorCode = 0;

    if (m_pInstance != NULL && m_pInstance->m_pUserPtr != NULL)
        pUser = m_pInstance->m_pUserPtr;

    // transforms the program in Tokens
    CBotToken*  pBaseToken = CBotToken::CompileTokens(program, error);
    if ( pBaseToken == NULL ) return false;


    CBotCStack* pStack      = new CBotCStack(NULL);
    CBotToken*  p  = pBaseToken->GetNext();                 // skips the first token (separator)

    pStack->SetBotCall(this);                               // defined used routines
    CBotCall::SetPUser(pUser);

    // first made a quick pass just to take the headers of routines and classes
    while ( pStack->IsOk() && p != NULL && p->GetType() != 0)
    {
        if ( IsOfType(p, ID_SEP) ) continue;                // semicolons lurking

        if ( p->GetType() == ID_CLASS ||
            ( p->GetType() == ID_PUBLIC && p->GetNext()->GetType() == ID_CLASS ))
        {
            CBotClass*  nxt = CBotClass::Compile1(p, pStack);
            if (m_pClass == NULL ) m_pClass = nxt;
            else m_pClass->AddNext(nxt);
        }
        else
        {
            CBotFunction*   next = CBotFunction::Compile1(p, pStack, NULL);
            if (m_Prog == NULL ) m_Prog = next;
            else m_Prog->AddNext(next);
        }
    }
    if ( !pStack->IsOk() )
    {
        m_ErrorCode = pStack->GetError(m_ErrorStart, m_ErrorEnd);
        delete m_Prog;
        m_Prog = NULL;
        delete pBaseToken;
        return false;
    }

//  CBotFunction*   temp = NULL;
    CBotFunction*   next = m_Prog;      // rewind the list

    p  = pBaseToken->GetNext();                             // returns to the beginning

    while ( pStack->IsOk() && p != NULL && p->GetType() != 0 )
    {
        if ( IsOfType(p, ID_SEP) ) continue;                // semicolons lurking

        if ( p->GetType() == ID_CLASS ||
            ( p->GetType() == ID_PUBLIC && p->GetNext()->GetType() == ID_CLASS ))
        {
            m_bCompileClass = true;
            CBotClass::Compile(p, pStack);                  // completes the definition of the class
        }
        else
        {
            m_bCompileClass = false;
            CBotFunction::Compile(p, pStack, next);
            if (next->IsExtern()) ListFonctions.Add(next->GetName()/* + next->GetParams()*/);
            next->m_pProg = this;                           // keeps pointers to the module
            next = next->Next();
        }
    }

//  delete m_Prog;          // the list of first pass
//  m_Prog = temp;          // list of the second pass

    if ( !pStack->IsOk() )
    {
        m_ErrorCode = pStack->GetError(m_ErrorStart, m_ErrorEnd);
        delete m_Prog;
        m_Prog = NULL;
    }

    delete pBaseToken;
    delete pStack;

    return (m_Prog != NULL);
}


bool CBotProgram::Start(const char* name)
{
#if STACKMEM
    m_pStack->Delete();
#else
    delete m_pStack;
#endif
    m_pStack = NULL;

    m_pRun = m_Prog;
    while (m_pRun != NULL)
    {
        if ( m_pRun->GetName() == name ) break;
        m_pRun = m_pRun->m_next;
    }

    if ( m_pRun == NULL )
    {
        m_ErrorCode = TX_NORUN;
        return false;
    }

#if STACKMEM
    m_pStack = CBotStack::FirstStack();
#else
    m_pStack = new CBotStack(NULL);                 // creates an execution stack
#endif

    m_pStack->SetBotCall(this);                     // bases for routines

    return true;                                    // we are ready for Run ()
}

bool CBotProgram::GetPosition(const char* name, int& start, int& stop, CBotGet modestart, CBotGet modestop)
{
    CBotFunction* p = m_Prog;
    while (p != NULL)
    {
        if ( p->GetName() == name ) break;
        p = p->m_next;
    }

    if ( p == NULL ) return false;

    p->GetPosition(start, stop, modestart, modestop);
    return true;
}

bool CBotProgram::Run(void* pUser, int timer)
{
    bool    ok;

    if (m_pStack == NULL || m_pRun == NULL) goto error;

    m_ErrorCode = 0;
    if (m_pInstance != NULL && m_pInstance->m_pUserPtr != NULL)
        pUser = m_pInstance->m_pUserPtr;

    m_pStack->Reset(pUser);                         // empty the possible previous error, and resets the timer
    if ( timer >= 0 ) m_pStack->SetTimer(timer);

    m_pStack->SetBotCall(this);                     // bases for routines

#if STACKRUN
    // resumes execution on the top of the stack
    ok = m_pStack->Execute();
    if ( ok )
    {
#ifdef  _DEBUG
        CBotVar*    ppVar[3];
        ppVar[0] = CBotVar::Create("aa", CBotTypInt);
        ppVar[1] = CBotVar::Create("bb", CBotTypInt);
        ppVar[2] = NULL;
        ok = m_pRun->Execute(ppVar, m_pStack, m_pInstance);
#else
        // returns to normal execution
        ok = m_pRun->Execute(NULL, m_pStack, m_pInstance);
#endif
    }
#else
    ok = m_pRun->Execute(NULL, m_pStack, m_pInstance);
#endif

    // completed on a mistake?
    if (!ok && !m_pStack->IsOk())
    {
        m_ErrorCode = m_pStack->GetError(m_ErrorStart, m_ErrorEnd);
#if STACKMEM
        m_pStack->Delete();
#else
        delete m_pStack;
#endif
        m_pStack = NULL;
        return true;                                // execution is finished!
    }

    if ( ok ) m_pRun = NULL;                        // more function in execution
    return ok;

error:
    m_ErrorCode = TX_NORUN;
    return true;
}

void CBotProgram::Stop()
{
#if STACKMEM
    m_pStack->Delete();
#else
    delete m_pStack;
#endif
    m_pStack = NULL;
    m_pRun = NULL;
}



bool CBotProgram::GetRunPos(const char* &FunctionName, int &start, int &end)
{
    FunctionName = NULL;
    start = end = 0;
    if (m_pStack == NULL) return false;

    m_pStack->GetRunPos(FunctionName, start, end);
    return true;
}

CBotVar* CBotProgram::GetStackVars(const char* &FunctionName, int level)
{
    FunctionName = NULL;
    if (m_pStack == NULL) return NULL;

    return m_pStack->GetStackVars(FunctionName, level);
}

void CBotProgram::SetTimer(int n)
{
    CBotStack::SetTimer( n );
}

int CBotProgram::GetError()
{
    return m_ErrorCode;
}

void CBotProgram::SetIdent(long n)
{
    m_Ident = n;
}

long CBotProgram::GetIdent()
{
    return m_Ident;
}

bool CBotProgram::GetError(int& code, int& start, int& end)
{
    code  = m_ErrorCode;
    start = m_ErrorStart;
    end   = m_ErrorEnd;
    return code > 0;
}

bool CBotProgram::GetError(int& code, int& start, int& end, CBotProgram* &pProg)
{
    code    = m_ErrorCode;
    start   = m_ErrorStart;
    end     = m_ErrorEnd;
    pProg   = this;
    return code > 0;
}

CBotString CBotProgram::GetErrorText(int code)
{
    CBotString TextError;

    TextError.LoadString( code );
    if (TextError.IsEmpty())
    {
        char    buf[100];
        sprintf(buf, "Exception numéro %d.", code);
        TextError = buf;
    }
    return TextError;
}


CBotFunction* CBotProgram::GetFunctions()
{
    return  m_Prog;
}

bool CBotProgram::AddFunction(const char* name,
                              bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser),
                              CBotTypResult rCompile (CBotVar* &pVar, void* pUser))
{
    // stores pointers to the two functions
    return CBotCall::AddFunction(name, rExec, rCompile);
}


bool WriteWord(FILE* pf, unsigned short w)
{
    size_t  lg;

    lg = fwrite(&w, sizeof( unsigned short ), 1, pf );

    return (lg == 1);
}

bool ReadWord(FILE* pf, unsigned short& w)
{
    size_t  lg;

    lg = fread(&w, sizeof( unsigned short ), 1, pf );

    return (lg == 1);
}

bool WriteFloat(FILE* pf, float w)
{
    size_t  lg;

    lg = fwrite(&w, sizeof( float ), 1, pf );

    return (lg == 1);
}

bool ReadFloat(FILE* pf, float& w)
{
    size_t  lg;

    lg = fread(&w, sizeof( float ), 1, pf );

    return (lg == 1);
}

bool WriteLong(FILE* pf, long w)
{
    size_t  lg;

    lg = fwrite(&w, sizeof( long ), 1, pf );

    return (lg == 1);
}

bool ReadLong(FILE* pf, long& w)
{
    size_t  lg;

    lg = fread(&w, sizeof( long ), 1, pf );

    return (lg == 1);
}

bool WriteString(FILE* pf, CBotString s)
{
    size_t  lg1, lg2;

    lg1 = s.GetLength();
    if (!WriteWord(pf, lg1)) return false;

    lg2 = fwrite(s, 1, lg1, pf );
    return (lg1 == lg2);
}

bool ReadString(FILE* pf, CBotString& s)
{
    unsigned short  w;
    char    buf[1000];
    size_t  lg1, lg2;

    if (!ReadWord(pf, w)) return false;
    lg1 = w;
    lg2 = fread(buf, 1, lg1, pf );
    buf[lg2] = 0;

    s = buf;
    return (lg1 == lg2);
}

bool WriteType(FILE* pf, CBotTypResult type)
{
    int typ = type.GetType();
    if ( typ == CBotTypIntrinsic ) typ = CBotTypClass;
    if ( !WriteWord(pf, typ) ) return false;
    if ( typ == CBotTypClass )
    {
        CBotClass* p = type.GetClass();
        if ( !WriteString(pf, p->GetName()) ) return false;
    }
    if ( type.Eq( CBotTypArrayBody ) ||
         type.Eq( CBotTypArrayPointer ) )
    {
        if ( !WriteWord(pf, type.GetLimite()) ) return false;
        if ( !WriteType(pf, type.GetTypElem()) ) return false;
    }
    return true;
}

bool ReadType(FILE* pf, CBotTypResult& type)
{
    unsigned short  w, ww;
    if ( !ReadWord(pf, w) ) return false;
    type.SetType(w);

    if ( type.Eq( CBotTypIntrinsic ) )
    {
        type = CBotTypResult( w, "point" );
    }

    if ( type.Eq( CBotTypClass ) )
    {
        CBotString  s;
        if ( !ReadString(pf, s) ) return false;
        type = CBotTypResult( w, s );
    }

    if ( type.Eq( CBotTypArrayPointer ) ||
         type.Eq( CBotTypArrayBody ) )
    {
        CBotTypResult   r;
        if ( !ReadWord(pf, ww) ) return false;
        if ( !ReadType(pf, r) ) return false;
        type = CBotTypResult( w, r );
        type.SetLimite(static_cast<short>(ww));
    }
    return true;
}


bool CBotProgram::DefineNum(const char* name, long val)
{
    return CBotToken::DefineNum(name, val);
}


bool CBotProgram::SaveState(FILE* pf)
{
    if (!WriteWord( pf, CBOTVERSION)) return false;


    if ( m_pStack != NULL )
    {
        if (!WriteWord( pf, 1)) return false;
        if (!WriteString( pf, m_pRun->GetName() )) return false;
        if (!m_pStack->SaveState(pf)) return false;
    }
    else
    {
        if (!WriteWord( pf, 0)) return false;
    }
    return true;
}


bool CBotProgram::RestoreState(FILE* pf)
{
    unsigned short  w;
    CBotString      s;

    Stop();

    if (!ReadWord( pf, w )) return false;
    if ( w != CBOTVERSION ) return false;

    if (!ReadWord( pf, w )) return false;
    if ( w == 0 ) return true;

    if (!ReadString( pf, s )) return false;
    Start(s);       // point de reprise

#if STACKMEM
    m_pStack->Delete();
#else
    delete m_pStack;
#endif
    m_pStack = NULL;

    // retrieves the stack from the memory
    // uses a NULL pointer (m_pStack) but it's ok like that
    if (!m_pStack->RestoreState(pf, m_pStack)) return false;
    m_pStack->SetBotCall(this);                     // bases for routines

    // restored some states in the stack according to the structure
    m_pRun->RestoreState(NULL, m_pStack, m_pInstance);
    return true;
}

int CBotProgram::GetVersion()
{
    return  CBOTVERSION;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////

CBotCall* CBotCall::m_ListCalls = NULL;

CBotCall::CBotCall(const char* name,
                   bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser),
                   CBotTypResult rCompile (CBotVar* &pVar, void* pUser))
{
    m_name       = name;
    m_rExec      = rExec;
    m_rComp      = rCompile;
    m_next       = NULL;
    m_nFuncIdent = CBotVar::NextUniqNum();
}

CBotCall::~CBotCall()
{
    if (m_next) delete m_next;
    m_next = NULL;
}

void CBotCall::Free()
{
    delete CBotCall::m_ListCalls;
}

bool CBotCall::AddFunction(const char* name,
                           bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser),
                           CBotTypResult rCompile (CBotVar* &pVar, void* pUser))
{
    CBotCall*   p = m_ListCalls;
    CBotCall*   pp = NULL;

    if ( p != NULL ) while ( p->m_next != NULL )
    {
        if ( p->GetName() == name )
        {
            // frees redefined function
            if ( pp ) pp->m_next = p->m_next;
            else      m_ListCalls = p->m_next;
            pp = p;
            p = p->m_next;
            pp->m_next = NULL;  // not to destroy the following list
            delete pp;
            continue;
        }
        pp = p;             // previous pointer
        p = p->m_next;
    }

    pp = new CBotCall(name, rExec, rCompile);

    if (p) p->m_next = pp;
    else m_ListCalls = pp;

    return true;
}


// transforms the array of pointers to variables
// in a chained list of variables
CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal=false)
{
    int     i = 0;
    CBotVar*    pVar = NULL;

    while( true )
    {
//        ppVars[i];
        if ( ppVars[i] == NULL ) break;

        CBotVar*    pp = CBotVar::Create(ppVars[i]);
        if (bSetVal) pp->Copy(ppVars[i]);
        else
            if ( ppVars[i]->GetType() == CBotTypPointer )
                pp->SetClass( ppVars[i]->GetClass());
// copy the pointer according to indirections
        if (pVar == NULL) pVar = pp;
        else pVar->AddNext(pp);
        i++;
    }
    return pVar;
}

// is acceptable by a call procedure name
// and given parameters

CBotTypResult CBotCall::CompileCall(CBotToken* &p, CBotVar** ppVar, CBotCStack* pStack, long& nIdent)
{
    nIdent = 0;
    CBotCall*   pt = m_ListCalls;
    CBotString  name = p->GetString();

    while ( pt != NULL )
    {
        if ( pt->m_name == name )
        {
            CBotVar*    pVar = MakeListVars(ppVar);
            CBotVar*    pVar2 = pVar;
            CBotTypResult r = pt->m_rComp(pVar2, m_pUser);
            int ret = r.GetType();

            // if a class is returned, it is actually a pointer
            if ( ret == CBotTypClass ) r.SetType( ret = CBotTypPointer );

            if ( ret > 20 )
            {
                if (pVar2) pStack->SetError(ret, p /*pVar2->GetToken()*/ );
            }
            delete pVar;
            nIdent = pt->m_nFuncIdent;
            return r;
        }
        pt = pt->m_next;
    }
    return -1;
}

void* CBotCall::m_pUser = NULL;

void CBotCall::SetPUser(void* pUser)
{
    m_pUser = pUser;
}

bool CBotCall::CheckCall(const char* name)
{
    CBotCall* p = m_ListCalls;

    while ( p != NULL )
    {
        if ( name == p->GetName() ) return true;
        p = p->m_next;
    }
    return false;
}



CBotString CBotCall::GetName()
{
    return  m_name;
}

CBotCall* CBotCall::Next()
{
    return  m_next;
}


int CBotCall::DoCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack, CBotTypResult& rettype)
{
    CBotCall*   pt = m_ListCalls;

    if ( nIdent ) while ( pt != NULL )
    {
        if ( pt->m_nFuncIdent == nIdent )
        {
            goto fund;
        }
        pt = pt->m_next;
    }

    pt = m_ListCalls;

    if ( token != NULL )
    {
        CBotString name = token->GetString();
        while ( pt != NULL )
        {
            if ( pt->m_name == name )
            {
                nIdent = pt->m_nFuncIdent;
                goto fund;
            }
            pt = pt->m_next;
        }
    }

    return -1;

fund:
#if !STACKRUN
    // lists the parameters depending on the contents of the stack (pStackVar)

    CBotVar*    pVar = MakeListVars(ppVar, true);
    CBotVar*    pVarToDelete = pVar;

    // creates a variable to the result
    CBotVar*    pResult = rettype.Eq(0) ? NULL : CBotVar::Create("", rettype);

    CBotVar*    pRes = pResult;
    int         Exception = 0;
    int res = pt->m_rExec(pVar, pResult, Exception, pStack->GetPUser());

    if ( pResult != pRes ) delete pRes; // different result if made
    delete pVarToDelete;

    if (res == false)
    {
        if (Exception!=0)
        {
            pStack->SetError(Exception, token);
        }
        delete pResult;
        return false;
    }
    pStack->SetVar(pResult);

    if ( rettype.GetType() > 0 && pResult == NULL )
    {
        pStack->SetError(TX_NORETVAL, token);
    }
    nIdent = pt->m_nFuncIdent;
    return true;

#else

    CBotStack*  pile = pStack->AddStackEOX(pt);
    if ( pile == EOX ) return true;

    // lists the parameters depending on the contents of the stack (pStackVar)

    CBotVar*    pVar = MakeListVars(ppVar, true);
//    CBotVar*    pVarToDelete = pVar;

    // creates a variable to the result
    CBotVar*    pResult = rettype.Eq(0) ? NULL : CBotVar::Create("", rettype);

    pile->SetVar( pVar );

    CBotStack*  pile2 = pile->AddStack();
    pile2->SetVar( pResult );

    pile->SetError(0, token);           // for the position on error + away
    return pt->Run( pStack );

#endif

}

#if STACKRUN

bool CBotCall::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack)
{
    CBotCall*   pt = m_ListCalls;

    {
        CBotString name = token->GetString();
        while ( pt != NULL )
        {
            if ( pt->m_name == name )
            {
                nIdent = pt->m_nFuncIdent;

                CBotStack*  pile = pStack->RestoreStackEOX(pt);
                if ( pile == NULL ) return true;

 //               CBotStack*  pile2 = pile->RestoreStack();
                pile->RestoreStack();
                return true;
            }
            pt = pt->m_next;
        }
    }

    return false;
}

bool CBotCall::Run(CBotStack* pStack)
{
    CBotStack*  pile = pStack->AddStackEOX(this);
    if ( pile == EOX ) return true;
    CBotVar*    pVar = pile->GetVar();

    CBotStack*  pile2 = pile->AddStack();
    CBotVar*    pResult = pile2->GetVar();
    CBotVar*    pRes = pResult;

    int         Exception = 0;
    int res = m_rExec(pVar, pResult, Exception, pStack->GetPUser());

    if (res == false)
    {
        if (Exception!=0)
        {
            pStack->SetError(Exception);
        }
        if ( pResult != pRes ) delete pResult;  // different result if made
        return false;
    }

    if ( pResult != NULL ) pStack->SetCopyVar( pResult );
    if ( pResult != pRes ) delete pResult;  // different result if made

    return true;
}

#endif

///////////////////////////////////////////////////////////////////////////////////////

CBotCallMethode::CBotCallMethode(const char* name,
                   bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception),
                   CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar))
{
    m_name       = name;
    m_rExec      = rExec;
    m_rComp      = rCompile;
    m_next       = NULL;
    m_nFuncIdent = CBotVar::NextUniqNum();
}

CBotCallMethode::~CBotCallMethode()
{
    delete m_next;
    m_next = NULL;
}

// is acceptable by a call procedure name
// and given parameters

CBotTypResult CBotCallMethode::CompileCall(const char* name, CBotVar* pThis,
                                           CBotVar** ppVar, CBotCStack* pStack,
                                           long& nIdent)
{
    CBotCallMethode*    pt = this;
    nIdent = 0;

    while ( pt != NULL )
    {
        if ( pt->m_name == name )
        {
            CBotVar*    pVar = MakeListVars(ppVar, true);
            CBotVar*    pVar2 = pVar;
            CBotTypResult r = pt->m_rComp(pThis, pVar2);
            int ret = r.GetType();
            if ( ret > 20 )
            {
                if (pVar2) pStack->SetError(ret, pVar2->GetToken());
            }
            delete pVar;
            nIdent = pt->m_nFuncIdent;
            return r;
        }
        pt = pt->m_next;
    }
    return CBotTypResult(-1);
}


CBotString CBotCallMethode::GetName()
{
    return  m_name;
}

CBotCallMethode* CBotCallMethode::Next()
{
    return  m_next;
}

void CBotCallMethode::AddNext(CBotCallMethode* pt)
{
    CBotCallMethode* p = this;
    while ( p->m_next != NULL ) p = p->m_next;

    p->m_next = pt;
}


int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pToken)
{
    CBotCallMethode*    pt = this;

    // search by the identifier

    if ( nIdent ) while ( pt != NULL )
    {
        if ( pt->m_nFuncIdent == nIdent )
        {
            // lists the parameters depending on the contents of the stack (pStackVar)

            CBotVar*    pVar = MakeListVars(ppVars, true);
            CBotVar*    pVarToDelete = pVar;

            // then calls the routine external to the module

            int         Exception = 0;
            int res = pt->m_rExec(pThis, pVar, pResult, Exception);
            pStack->SetVar(pResult);

            if (res == false)
            {
                if (Exception!=0)
                {
//                  pStack->SetError(Exception, pVar->GetToken());
                    pStack->SetError(Exception, pToken);
                }
                delete pVarToDelete;
                return false;
            }
            delete pVarToDelete;
            return true;
        }
        pt = pt->m_next;
    }

    // search by name

    while ( pt != NULL )
    {
        if ( pt->m_name == name )
        {
            // lists the parameters depending on the contents of the stack (pStackVar)

            CBotVar*    pVar = MakeListVars(ppVars, true);
            CBotVar*    pVarToDelete = pVar;

            int         Exception = 0;
            int res = pt->m_rExec(pThis, pVar, pResult, Exception);
            pStack->SetVar(pResult);

            if (res == false)
            {
                if (Exception!=0)
                {
//                  pStack->SetError(Exception, pVar->GetToken());
                    pStack->SetError(Exception, pToken);
                }
                delete pVarToDelete;
                return false;
            }
            delete pVarToDelete;
            nIdent = pt->m_nFuncIdent;
            return true;
        }
        pt = pt->m_next;
    }

    return -1;
}

bool rSizeOf( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
{
    if ( pVar == NULL ) return TX_LOWPARAM;

    int i = 0;
    pVar = pVar->GetItemList();

    while ( pVar != NULL )
    {
        i++;
        pVar = pVar->GetNext();
    }

    pResult->SetValInt(i);
    return true;
}

CBotTypResult cSizeOf( CBotVar* &pVar, void* pUser )
{
    if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
    if ( pVar->GetType() != CBotTypArrayPointer )
                        return CBotTypResult( TX_BADPARAM );
    return CBotTypResult( CBotTypInt );
}


CBotString CBotProgram::m_DebugVarStr = "";

bool rCBotDebug( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
{
    pResult->SetValString( CBotProgram::m_DebugVarStr );

    return true;
}

CBotTypResult cCBotDebug( CBotVar* &pVar, void* pUser )
{
    // no parameter
    if ( pVar != NULL ) return CBotTypResult( TX_OVERPARAM );

    // function returns a result "string"
    return CBotTypResult( CBotTypString );
}


#include "StringFunctions.cpp"

void CBotProgram::Init()
{
    CBotToken::DefineNum( "CBotErrOpenPar", 5000) ;     // missing the opening parenthesis
    CBotToken::DefineNum( "CBotErrClosePar", 5001) ;    // missing the closing parenthesis
    CBotToken::DefineNum( "CBotErrNotBoolean", 5002) ;  // expression must be a boolean
    CBotToken::DefineNum( "CBotErrUndefVar", 5003) ;    // undeclared variable
    CBotToken::DefineNum( "CBotErrBadLeft", 5004) ;     // impossible assignment (5 = ...)
    CBotToken::DefineNum( "CBotErrNoTerminator", 5005) ;// semicolon expected
    CBotToken::DefineNum( "CBotErrCaseOut", 5006) ;     // case outside a switch
    CBotToken::DefineNum( "CBotErrCloseBlock", 5008) ;  // missing " } "
    CBotToken::DefineNum( "CBotErrElseWhitoutIf", 5009) ;// else without matching if
    CBotToken::DefineNum( "CBotErrOpenBlock", 5010) ;   // missing " { "
    CBotToken::DefineNum( "CBotErrBadType1", 5011) ;    // wrong type for the assignment
    CBotToken::DefineNum( "CBotErrRedefVar", 5012) ;    // redefinition of the variable
    CBotToken::DefineNum( "CBotErrBadType2", 5013) ;    // two operands are incompatible
    CBotToken::DefineNum( "CBotErrUndefCall", 5014) ;   // routine unknown
    CBotToken::DefineNum( "CBotErrNoDoubleDots", 5015) ;// " : " expected
    CBotToken::DefineNum( "CBotErrBreakOutside", 5017) ;// break outside of a loop
    CBotToken::DefineNum( "CBotErrUndefLabel", 5019) ;  // unknown label
    CBotToken::DefineNum( "CBotErrLabel", 5018) ;       // label can not get here
    CBotToken::DefineNum( "CBotErrNoCase", 5020) ;      // missing " case "
    CBotToken::DefineNum( "CBotErrBadNum", 5021) ;      // expected number
    CBotToken::DefineNum( "CBotErrVoid", 5022) ;        // " void " not possble here
    CBotToken::DefineNum( "CBotErrNoType", 5023) ;      // type declaration expected
    CBotToken::DefineNum( "CBotErrNoVar", 5024) ;       // variable name expected
    CBotToken::DefineNum( "CBotErrNoFunc", 5025) ;      // expected function name
    CBotToken::DefineNum( "CBotErrOverParam", 5026) ;   // too many parameters
    CBotToken::DefineNum( "CBotErrRedefFunc", 5027) ;   // this function already exists
    CBotToken::DefineNum( "CBotErrLowParam", 5028) ;    // not enough parameters
    CBotToken::DefineNum( "CBotErrBadParam", 5029) ;    // mauvais types de paramètres
    CBotToken::DefineNum( "CBotErrNbParam", 5030) ;     // wrong number of parameters
    CBotToken::DefineNum( "CBotErrUndefItem", 5031) ;   // element does not exist in the class
    CBotToken::DefineNum( "CBotErrUndefClass", 5032) ;  // variable is not a class
    CBotToken::DefineNum( "CBotErrNoConstruct", 5033) ; // no appropriate constructor
    CBotToken::DefineNum( "CBotErrRedefClass", 5034) ;  // Class already exists
    CBotToken::DefineNum( "CBotErrCloseIndex", 5035) ;  // " ] " expected
    CBotToken::DefineNum( "CBotErrReserved", 5036) ;    // reserved word (for a DefineNum)

// Here are the list of errors that can be returned by the module
// for the execution

    CBotToken::DefineNum( "CBotErrZeroDiv", 6000) ;     // division by zero
    CBotToken::DefineNum( "CBotErrNotInit", 6001) ;     // uninitialized variable
    CBotToken::DefineNum( "CBotErrBadThrow", 6002) ;    // throw a negative value
    CBotToken::DefineNum( "CBotErrNoRetVal", 6003) ;    // function did not return results
    CBotToken::DefineNum( "CBotErrNoRun", 6004) ;       // active Run () without a function
    CBotToken::DefineNum( "CBotErrUndefFunc", 6005) ;   // Calling a function that no longer exists

    CBotProgram::AddFunction("sizeof", rSizeOf, cSizeOf );

    InitStringFunctions();

    // just a function for various debug
    CBotProgram::AddFunction("CBOTDEBUGDD", rCBotDebug, cCBotDebug);
    //TODO implement this deletion
    // DeleteFile("CbotDebug.txt");

}

void CBotProgram::Free()
{
    CBotToken::Free() ;
    CBotCall ::Free() ;
    CBotClass::Free() ;
}