File: ActionExpression.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (66 lines) | stat: -rw-r--r-- 1,521 bytes parent folder | download | duplicates (2)
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

#include "ActionExpression.h"

#include "ExpressionParser.h"
#include "TypeDefinition.h"

#include "actions/expression/nodes/AbstractExpression.h"

namespace actions {
namespace expression {
namespace {

SCP_string getTypeName(ValueType type)
{
	const auto& typeDef = TypeDefinition::forValueType(type);
	return typeDef.getName();
}

} // namespace

ActionExpression ActionExpression::parseFromTable(ValueType expectedReturnType, const ParseContext& context)
{
	SCP_string expressionText;

	stuff_string(expressionText, F_NAME);

	ExpressionParser parser(expressionText);

	auto expression = parser.parse(context);

	if (!expression) {
		error_display(0, "Failed to parse action expression:\n%s", parser.getErrorText().c_str());
		return ActionExpression();
	}

	auto type = expression->getExpressionType();

	// Check if the type this evaluates to matches what we expect
	if (!checkTypeWithImplicitConversion(type, expectedReturnType)) {
		error_display(0,
			"Expression evaluates to type <%s> but <%s> was expected!",
			getTypeName(type).c_str(),
			getTypeName(expectedReturnType).c_str());
		return ActionExpression();
	}

	// Everything is valid
	return ActionExpression(expression);
}

Value ActionExpression::execute(const ProgramVariables& variables) const
{
	// Do not crash on invalid expressions
	if (!isValid()) {
		return Value();
	}

	return m_expression->execute(variables);
}
bool ActionExpression::isValid() const
{
	return m_expression != nullptr;
}

} // namespace expression
} // namespace actions