Better handling of exceptions for extern usage
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-07-27 19:45:15 +02:00
parent 586dd994e2
commit 2b248101d5
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 73 additions and 7 deletions

51
src/EvaluateResult.hpp Normal file
View File

@ -0,0 +1,51 @@
#ifndef PORYGONLANG_EVALUATERESULT_HPP
#define PORYGONLANG_EVALUATERESULT_HPP
#include <string>
#include <utility>
#include <bits/stdc++.h>
#include "Evaluator/EvalValues/EvalValue.hpp"
namespace Porygon{
struct EvaluateResult{
const Evaluation::EvalValue* _value;
const u_int8_t _result;
char * _errorMessage;
const size_t _errorSize;
public:
explicit EvaluateResult(const Evaluation::EvalValue* value)
: _result(0), _value(value), _errorMessage(nullptr), _errorSize(0)
{
}
~EvaluateResult(){
delete _value;
delete _errorMessage;
}
explicit EvaluateResult(const std::string& error)
: _result(1), _value(nullptr), _errorSize(error.size())
{
_errorMessage = new char[error.size()]();
std::strcpy(_errorMessage, error.c_str());
}
EvaluateResult& operator= (EvaluateResult b) = delete;
EvaluateResult(const EvaluateResult& b) = delete;
[[nodiscard]] const Evaluation::EvalValue* GetValue() const{
return _value;
}
[[nodiscard]] u_int8_t GetResult() const{
return _result;
}
[[nodiscard]] const char * GetError() const{
return _errorMessage;
}
};
}
#endif //PORYGONLANG_EVALUATERESULT_HPP

View File

@ -7,6 +7,7 @@
#include "Parser/Lexer.hpp"
#include "Parser/Parser.hpp"
#include "Binder/Binder.hpp"
#include "EvaluateResult.hpp"
Porygon::Script* Porygon::Script::Create(const u16string& script) {
return new Script(script);
@ -103,15 +104,19 @@ Porygon::Script::Script(shared_ptr<BoundScriptStatement> boundScript,
_evaluator = new Evaluator(_scriptVariables);
}
extern "C" {
Porygon::Script* CreateScript(char16_t * s){
return Porygon::Script::Create(s);
}
const EvalValue* EvaluateScript(Porygon::Script* script){
auto result = script -> Evaluate();
return result.Clone();
Porygon::EvaluateResult* EvaluateScript(Porygon::Script* script){
try{
auto result = script -> Evaluate();
return new Porygon::EvaluateResult(result.Take());
}
catch (const EvaluationException& e){
return new Porygon::EvaluateResult(e.what());
}
}
bool HasVariable(Porygon::Script* script, const char16_t* key){
@ -126,8 +131,18 @@ extern "C" {
return script->HasFunction(key);
}
const EvalValue* CallFunction(Porygon::Script* script, const char16_t* key, EvalValue* parameters[], int parameterCount){
std::vector<EvalValue*> v(parameters, parameters + parameterCount);
return script->CallFunction(key, v);
Porygon::EvaluateResult* CallFunction(Porygon::Script* script, const char16_t* key, EvalValue* parameters[], int parameterCount){
try{
std::vector<EvalValue*> v(parameters, parameters + parameterCount);
auto result = script->CallFunction(key, v);
return new Porygon::EvaluateResult(result);
}
catch (const EvaluationException& e){
return new Porygon::EvaluateResult(e.what());
}
}
const char * GetResultError(Porygon::EvaluateResult * result){
return result->GetError();
}
}