forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expression.cpp
90 lines (80 loc) · 2.3 KB
/
Expression.cpp
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
/*
* Copyright (c) 2021, Jan de Visser <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibSQL/AST/AST.h>
#include <LibSQL/Database.h>
namespace SQL::AST {
Value Expression::evaluate(ExecutionContext&) const
{
return Value::null();
}
Value NumericLiteral::evaluate(ExecutionContext&) const
{
Value ret(SQLType::Float);
ret = value();
return ret;
}
Value StringLiteral::evaluate(ExecutionContext&) const
{
Value ret(SQLType::Text);
ret = value();
return ret;
}
Value NullLiteral::evaluate(ExecutionContext&) const
{
return Value::null();
}
Value NestedExpression::evaluate(ExecutionContext& context) const
{
return expression()->evaluate(context);
}
Value ChainedExpression::evaluate(ExecutionContext& context) const
{
Value ret(SQLType::Tuple);
Vector<Value> values;
for (auto& expression : expressions()) {
values.append(expression.evaluate(context));
}
ret = values;
return ret;
}
Value UnaryOperatorExpression::evaluate(ExecutionContext& context) const
{
Value expression_value = NestedExpression::evaluate(context);
switch (type()) {
case UnaryOperator::Plus:
if (expression_value.type() == SQLType::Integer || expression_value.type() == SQLType::Float)
return expression_value;
// TODO: Error handling.
VERIFY_NOT_REACHED();
case UnaryOperator::Minus:
if (expression_value.type() == SQLType::Integer) {
expression_value = -int(expression_value);
return expression_value;
}
if (expression_value.type() == SQLType::Float) {
expression_value = -double(expression_value);
return expression_value;
}
// TODO: Error handling.
VERIFY_NOT_REACHED();
case UnaryOperator::Not:
if (expression_value.type() == SQLType::Boolean) {
expression_value = !bool(expression_value);
return expression_value;
}
// TODO: Error handling.
VERIFY_NOT_REACHED();
case UnaryOperator::BitwiseNot:
if (expression_value.type() == SQLType::Integer) {
expression_value = ~u32(expression_value);
return expression_value;
}
// TODO: Error handling.
VERIFY_NOT_REACHED();
}
VERIFY_NOT_REACHED();
}
}