summaryrefslogtreecommitdiffstats
path: root/test/cbot/CBot_console/CBotConsole.cpp
blob: 1b0b185e2ab53daf7e5bc4227572e0ec2b4dfdb6 (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
/*
 * This file is part of the Colobot: Gold Edition source code
 * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
 * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
 *
 * 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://gnu.org/licenses
 */
/*
 * CBotConsole.cpp
 *
 *  Created on: 08-08-2012
 *      Author: michal
 */

#include "CBotConsole.h"
#include "CClass.h"
#include <ctime>
#include <iostream>

CBotConsole::CBotConsole() {
	// TODO Auto-generated constructor stub
	m_pProg = NULL;
	m_threadinfo.m_bRun	  = false;
	m_code  = 0;

}

CBotConsole::~CBotConsole() {
	// TODO Auto-generated destructor stub
}

uint ThreadProc(ThreadInfo *info)
{
	time_t	t0,t1;
	time(&t0);

	int		Cpt = 0;

	info->m_pProg->Start("LaCommande");
	while ( !info->m_bStop && !info->m_pProg->Run() )
	{
#if	0
		const char* FunctionName;
		const char* FN;
		int start, end;

		info->m_pProg->GetRunPos(FunctionName, start, end);

		if ( FunctionName != NULL )
		{
			info->m_pEditx->SetSel(start, end);

			char	buffer[200];
			sprintf( buffer, "step %s, %d, %d",FunctionName, start, end);
			AfxMessageBox( buffer );

			int level = 0;
			do
			{
				CBotVar* t = info->m_pProg->GetStackVars(FN, level--);
				if ( FN != FunctionName ) break;
				if ( t != NULL )
				{
					CString	s ;
					while ( t != NULL )
					{
						if (s.IsEmpty()) s+= "Stack -> ";
						else			 s+= " , ";
						s += t->GetValString();
						t = t->GetNext();
					}
					AfxMessageBox(s);
				}
			} while (TRUE);
		}
#endif
		Cpt++;
		if ( Cpt%50 == 0 ) std::cout << ".";
	}

	if ( info->m_bStop )
	{
		std::cout << "\nInterrupt\n";
	}
	else if (info->m_pProg->GetError() == 0)
	{
		time(&t1);
		double prog_time = difftime(t0,t1);

		char	buffer[200];
		sprintf( buffer, "\nExecution terminated in %f seconds.\nInterrupted %d time(s).\n",
				 prog_time, Cpt);

		std::cout << buffer;
	}

//	info->m_pWndMessage->SendMessage(WM_ENDPROG, 0, 0) ;
	return 0 ;
}

long CBotConsole::EndProg()
{
	m_threadinfo.m_bRun	  = false;

	if (m_pProg->GetError(m_code, m_start, m_end))
	{
		CBotString	TextError;
		TextError = CBotProgram::GetErrorText(m_code);
		std::cout << TextError;
		return 1;
	}
	delete m_pProg;
	m_pProg = NULL;

	return 0 ;
}


void CBotConsole::OnOK()
{
	m_code  = 0;

	std::string	Commande;
	std::cin >> Commande;

	std::string s = "void LaCommande() { " + Commande + " ;}";
	m_pProg = new CBotProgram();
	CBotStringArray liste;
	m_pProg->Compile(s.c_str(), liste);

	int	err, start, end;
	if ( m_pProg->GetError(err, start, end) )
	{
		CBotString	TextError;
		TextError = CBotProgram::GetErrorText(err);
		std::cout << TextError;
		return;
	}

	std::cout << "\n" + Commande + " ->\n";

//	m_Edit2.SetWindowText("");
//	m_Edit1.SetFocus();
//	m_Edit2.EnableWindow(FALSE);
//	m_cOK.EnableWindow(FALSE);

	// lance un processus paralèle pour l'exécution
//	m_threadinfo.m_pWndMessage = this ;

//	m_threadinfo.m_pEdit1 = &m_Edit1;
//	m_threadinfo.m_pEditx = m_pEditx;
	m_threadinfo.m_pProg  = m_pProg;
	m_threadinfo.m_bStop  = false;
	m_threadinfo.m_bRun	  = true;

	ThreadProc(&m_threadinfo);

//  here program starts
//	AfxBeginThread((AFX_THREADPROC)ThreadProc, &m_threadinfo) ;
}

void CBotConsole::OnCancel()
{
	m_threadinfo.m_bStop = true ;
}

bool CBotConsole::OnInitDialog()
{
//	CDialog::OnInitDialog();

	std::cout << "Following functions are availible:\n";
	for ( int i = 0; i < m_pListe->GetSize(); i++ )
	{
		CBotString x = (*m_pListe)[i] + CBotString("\n");
		std::cout << x;
	}
	std::cout << "Enter a command:\n";


	return true;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}