2015-12-27 15:51:57 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
2021-09-11 13:52:34 +00:00
|
|
|
* Copyright (C) 2001-2021, Daniel Roux, EPSITEC SA & TerranovaTeam
|
2015-12-27 15:51:57 +00:00
|
|
|
* http://epsitec.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
|
|
|
|
*/
|
|
|
|
|
2015-11-21 15:31:22 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "common/restext.h"
|
2015-11-22 15:42:51 +00:00
|
|
|
|
|
|
|
#include "CBot/CBot.h"
|
2015-11-21 15:31:22 +00:00
|
|
|
|
2015-12-26 13:37:36 +00:00
|
|
|
using namespace CBot;
|
|
|
|
|
2018-04-20 09:20:56 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2015-11-21 15:31:22 +00:00
|
|
|
CBotTypResult cMessage(CBotVar* &var, void* user)
|
|
|
|
{
|
|
|
|
if ( var == nullptr ) return CBotTypResult(CBotErrLowParam);
|
|
|
|
if ( var->GetType() != CBotTypString &&
|
|
|
|
var->GetType() > CBotTypDouble ) return CBotTypResult(CBotErrBadNum);
|
|
|
|
var = var->GetNext();
|
|
|
|
|
|
|
|
if ( var == nullptr ) return CBotTypResult(CBotTypFloat);
|
|
|
|
return CBotTypResult(CBotErrOverParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rMessage(CBotVar* var, CBotVar* result, int& exception, void* user)
|
|
|
|
{
|
2015-12-20 15:19:10 +00:00
|
|
|
std::string message = var->GetValString();
|
2015-11-21 15:31:22 +00:00
|
|
|
|
|
|
|
std::cout << message << std::endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-20 09:20:56 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-11-21 15:31:22 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
// Read program code from stdin
|
|
|
|
std::string code = "";
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(std::cin, line))
|
|
|
|
{
|
|
|
|
code += line;
|
|
|
|
code += "\n";
|
|
|
|
}
|
|
|
|
|
2015-12-26 18:57:12 +00:00
|
|
|
// Initialize the CBot engine, add standard library functions
|
|
|
|
CBotProgram::Init();
|
|
|
|
CBotProgram::AddFunction("message", rMessage, cMessage);
|
|
|
|
|
2015-11-21 15:31:22 +00:00
|
|
|
// Error message strings are stored on Colobot side (meh!) so let's initialize that
|
|
|
|
InitializeRestext();
|
|
|
|
|
|
|
|
|
|
|
|
// Compile the program
|
2015-12-20 15:19:10 +00:00
|
|
|
std::vector<std::string> externFunctions;
|
2015-11-21 15:31:22 +00:00
|
|
|
std::unique_ptr<CBotProgram> program{new CBotProgram(nullptr)};
|
|
|
|
if (!program->Compile(code.c_str(), externFunctions, nullptr))
|
|
|
|
{
|
2015-12-21 20:35:20 +00:00
|
|
|
CBotError error;
|
|
|
|
int cursor1, cursor2;
|
2015-11-21 15:31:22 +00:00
|
|
|
program->GetError(error, cursor1, cursor2);
|
|
|
|
std::string errorStr;
|
|
|
|
GetResource(RES_CBOT, error, errorStr);
|
|
|
|
std::cerr << "COMPILE ERROR: " << errorStr << " (code: " << error << ") @ " << cursor1 << " - " << cursor2 << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute all compiled functions marked as "extern"
|
2015-12-20 13:49:30 +00:00
|
|
|
if (externFunctions.empty())
|
2015-11-21 15:31:22 +00:00
|
|
|
{
|
|
|
|
std::cerr << "NO EXTERN FUNCTIONS FOUND";
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
bool runErrors = false;
|
2015-12-20 15:19:10 +00:00
|
|
|
for (const std::string& func : externFunctions)
|
2015-11-21 15:31:22 +00:00
|
|
|
{
|
|
|
|
if (!program->Start(func))
|
|
|
|
{
|
|
|
|
std::cerr << "FAILED TO START: " << func << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "Running program: " << func << std::endl;
|
|
|
|
|
|
|
|
while (!program->Run(nullptr)); // Run the program
|
|
|
|
|
2015-12-21 20:35:20 +00:00
|
|
|
CBotError error;
|
|
|
|
int cursor1, cursor2;
|
2015-11-21 15:31:22 +00:00
|
|
|
program->GetError(error, cursor1, cursor2);
|
|
|
|
if (error != 0)
|
|
|
|
{
|
|
|
|
std::string errorStr;
|
|
|
|
GetResource(RES_CBOT, error, errorStr);
|
|
|
|
std::cerr << "RUNTIME ERROR: " << errorStr << " (code: " << error << ") @ " << cursor1 << " - " << cursor2 << std::endl;
|
|
|
|
runErrors = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "Program finished." << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return runErrors ? 3 : 0;
|
2015-11-22 15:42:51 +00:00
|
|
|
}
|