2020-10-27 22:25:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-10-27 22:25:19 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-15 10:34:40 +00:00
|
|
|
#include <AK/Assertions.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-10-27 22:25:19 +00:00
|
|
|
#include <AK/GenericLexer.h>
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/Queue.h>
|
|
|
|
#include <AK/StringView.h>
|
2021-12-14 09:22:35 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2020-11-16 18:13:24 +00:00
|
|
|
#include <LibRegex/Regex.h>
|
2020-10-27 22:25:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static void print_help_and_exit()
|
|
|
|
{
|
|
|
|
outln(R"(
|
|
|
|
Usage: expr EXPRESSION
|
|
|
|
expr [--help]
|
|
|
|
|
|
|
|
Print the value of EXPRESSION to standard output.)");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2021-02-21 23:07:24 +00:00
|
|
|
template<typename Fmt, typename... Args>
|
|
|
|
[[noreturn]] void fail(Fmt&& fmt, Args&&... args)
|
2020-10-27 22:25:19 +00:00
|
|
|
{
|
2020-11-09 09:47:19 +00:00
|
|
|
warn("ERROR: \e[31m");
|
2022-07-11 19:53:29 +00:00
|
|
|
warnln(StringView { fmt, strlen(fmt) }, args...);
|
2020-11-09 09:47:19 +00:00
|
|
|
warn("\e[0m");
|
2021-07-08 16:04:15 +00:00
|
|
|
exit(2);
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Expression {
|
|
|
|
public:
|
|
|
|
enum Precedence {
|
|
|
|
Or,
|
|
|
|
And,
|
|
|
|
Comp,
|
|
|
|
ArithS,
|
|
|
|
ArithM,
|
|
|
|
StringO,
|
|
|
|
Paren,
|
|
|
|
};
|
|
|
|
static NonnullOwnPtr<Expression> parse(Queue<StringView>& args, Precedence prec = Or);
|
|
|
|
|
|
|
|
enum class Type {
|
|
|
|
Integer,
|
|
|
|
String,
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual bool truth() const = 0;
|
|
|
|
virtual int integer() const = 0;
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual DeprecatedString string() const = 0;
|
2020-10-27 22:25:19 +00:00
|
|
|
virtual Type type() const = 0;
|
2022-03-24 03:18:10 +00:00
|
|
|
virtual ~Expression() = default;
|
2020-10-27 22:25:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ValueExpression : public Expression {
|
|
|
|
public:
|
|
|
|
ValueExpression(int v)
|
|
|
|
: as_integer(v)
|
|
|
|
, m_type(Type::Integer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
ValueExpression(DeprecatedString&& v)
|
2020-10-27 22:25:19 +00:00
|
|
|
: as_string(move(v))
|
|
|
|
, m_type(Type::String)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-24 03:18:10 +00:00
|
|
|
virtual ~ValueExpression() {};
|
2020-10-27 22:25:19 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool truth() const override
|
|
|
|
{
|
|
|
|
if (m_type == Type::String)
|
|
|
|
return !as_string.is_empty();
|
|
|
|
return integer() != 0;
|
|
|
|
}
|
|
|
|
virtual int integer() const override
|
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
case Type::Integer:
|
|
|
|
return as_integer;
|
|
|
|
case Type::String:
|
|
|
|
if (auto converted = as_string.to_int(); converted.has_value())
|
|
|
|
return converted.value();
|
|
|
|
fail("Not an integer: '{}'", as_string);
|
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual DeprecatedString string() const override
|
2020-10-27 22:25:19 +00:00
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
case Type::Integer:
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString::formatted("{}", as_integer);
|
2020-10-27 22:25:19 +00:00
|
|
|
case Type::String:
|
|
|
|
return as_string;
|
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|
|
|
|
virtual Type type() const override { return m_type; }
|
|
|
|
|
|
|
|
union {
|
|
|
|
int as_integer;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString as_string;
|
2020-10-27 22:25:19 +00:00
|
|
|
};
|
|
|
|
Type m_type { Type::String };
|
|
|
|
};
|
|
|
|
|
|
|
|
class BooleanExpression : public Expression {
|
|
|
|
public:
|
|
|
|
enum class BooleanOperator {
|
|
|
|
And,
|
|
|
|
Or,
|
|
|
|
};
|
2021-11-10 23:55:02 +00:00
|
|
|
static BooleanOperator op_from(StringView sv)
|
2020-10-27 22:25:19 +00:00
|
|
|
{
|
|
|
|
if (sv == "&")
|
|
|
|
return BooleanOperator::And;
|
|
|
|
return BooleanOperator::Or;
|
|
|
|
}
|
|
|
|
BooleanExpression(BooleanOperator op, NonnullOwnPtr<Expression>&& left, NonnullOwnPtr<Expression>&& right)
|
|
|
|
: m_op(op)
|
|
|
|
, m_left(move(left))
|
|
|
|
, m_right(move(right))
|
|
|
|
{
|
|
|
|
if (m_op == BooleanOperator::Or)
|
|
|
|
m_left_truth = m_left->truth();
|
|
|
|
else
|
|
|
|
m_right_truth = m_right->truth();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool truth() const override
|
|
|
|
{
|
|
|
|
if (m_op == BooleanOperator::Or)
|
|
|
|
return m_left_truth ? true : m_right->truth();
|
|
|
|
return m_right_truth ? m_left->truth() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int integer() const override
|
|
|
|
{
|
|
|
|
switch (m_op) {
|
|
|
|
case BooleanOperator::And:
|
|
|
|
if (m_right_truth)
|
|
|
|
return m_left->integer();
|
|
|
|
return 0;
|
|
|
|
case BooleanOperator::Or:
|
|
|
|
if (m_left_truth)
|
|
|
|
return m_left->integer();
|
|
|
|
return m_right->integer();
|
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual DeprecatedString string() const override
|
2020-10-27 22:25:19 +00:00
|
|
|
{
|
|
|
|
switch (m_op) {
|
|
|
|
case BooleanOperator::And:
|
|
|
|
if (m_right_truth)
|
|
|
|
return m_left->string();
|
|
|
|
return "0";
|
|
|
|
case BooleanOperator::Or:
|
|
|
|
if (m_left_truth)
|
|
|
|
return m_left->string();
|
|
|
|
return m_right->string();
|
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|
|
|
|
virtual Type type() const override
|
|
|
|
{
|
|
|
|
switch (m_op) {
|
|
|
|
case BooleanOperator::And:
|
|
|
|
if (m_right_truth)
|
|
|
|
return m_left->type();
|
|
|
|
return m_right->type();
|
|
|
|
case BooleanOperator::Or:
|
|
|
|
if (m_left_truth)
|
|
|
|
return m_left->type();
|
|
|
|
return m_right->type();
|
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BooleanOperator m_op { BooleanOperator::And };
|
|
|
|
NonnullOwnPtr<Expression> m_left, m_right;
|
|
|
|
bool m_left_truth { false }, m_right_truth { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
class ComparisonExpression : public Expression {
|
|
|
|
public:
|
|
|
|
enum class ComparisonOperation {
|
|
|
|
Less,
|
|
|
|
LessEq,
|
|
|
|
Eq,
|
|
|
|
Neq,
|
|
|
|
GreaterEq,
|
|
|
|
Greater,
|
|
|
|
};
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
static ComparisonOperation op_from(StringView sv)
|
2020-10-27 22:25:19 +00:00
|
|
|
{
|
2021-12-14 09:23:07 +00:00
|
|
|
if (sv == "<"sv)
|
2020-10-27 22:25:19 +00:00
|
|
|
return ComparisonOperation::Less;
|
2021-12-14 09:23:07 +00:00
|
|
|
if (sv == "<="sv)
|
2020-10-27 22:25:19 +00:00
|
|
|
return ComparisonOperation::LessEq;
|
2021-12-14 09:23:07 +00:00
|
|
|
if (sv == "="sv)
|
2020-10-27 22:25:19 +00:00
|
|
|
return ComparisonOperation::Eq;
|
2021-12-14 09:23:07 +00:00
|
|
|
if (sv == "!="sv)
|
2020-10-27 22:25:19 +00:00
|
|
|
return ComparisonOperation::Neq;
|
2021-12-14 09:23:07 +00:00
|
|
|
if (sv == ">="sv)
|
2020-10-27 22:25:19 +00:00
|
|
|
return ComparisonOperation::GreaterEq;
|
|
|
|
return ComparisonOperation::Greater;
|
|
|
|
}
|
|
|
|
|
|
|
|
ComparisonExpression(ComparisonOperation op, NonnullOwnPtr<Expression>&& left, NonnullOwnPtr<Expression>&& right)
|
|
|
|
: m_op(op)
|
|
|
|
, m_left(move(left))
|
|
|
|
, m_right(move(right))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template<typename T>
|
2022-10-16 22:06:11 +00:00
|
|
|
bool compare(T const& left, T const& right) const
|
2020-10-27 22:25:19 +00:00
|
|
|
{
|
|
|
|
switch (m_op) {
|
|
|
|
case ComparisonOperation::Less:
|
|
|
|
return left < right;
|
|
|
|
case ComparisonOperation::LessEq:
|
|
|
|
return left == right || left < right;
|
|
|
|
case ComparisonOperation::Eq:
|
|
|
|
return left == right;
|
|
|
|
case ComparisonOperation::Neq:
|
|
|
|
return left != right;
|
|
|
|
case ComparisonOperation::GreaterEq:
|
|
|
|
return !(left < right);
|
|
|
|
case ComparisonOperation::Greater:
|
|
|
|
return left != right && !(left < right);
|
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool truth() const override
|
|
|
|
{
|
|
|
|
switch (m_left->type()) {
|
|
|
|
case Type::Integer:
|
|
|
|
return compare(m_left->integer(), m_right->integer());
|
|
|
|
case Type::String:
|
|
|
|
return compare(m_left->string(), m_right->string());
|
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|
|
|
|
virtual int integer() const override { return truth(); }
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual DeprecatedString string() const override { return truth() ? "1" : "0"; }
|
2020-10-27 22:25:19 +00:00
|
|
|
virtual Type type() const override { return Type::Integer; }
|
|
|
|
|
|
|
|
ComparisonOperation m_op { ComparisonOperation::Less };
|
|
|
|
NonnullOwnPtr<Expression> m_left, m_right;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ArithmeticExpression : public Expression {
|
|
|
|
public:
|
|
|
|
enum class ArithmeticOperation {
|
|
|
|
Sum,
|
|
|
|
Difference,
|
|
|
|
Product,
|
|
|
|
Quotient,
|
|
|
|
Remainder,
|
|
|
|
};
|
2021-11-10 23:55:02 +00:00
|
|
|
static ArithmeticOperation op_from(StringView sv)
|
2020-10-27 22:25:19 +00:00
|
|
|
{
|
2021-12-14 09:23:07 +00:00
|
|
|
if (sv == "+"sv)
|
2020-10-27 22:25:19 +00:00
|
|
|
return ArithmeticOperation::Sum;
|
2021-12-14 09:23:07 +00:00
|
|
|
if (sv == "-"sv)
|
2020-10-27 22:25:19 +00:00
|
|
|
return ArithmeticOperation::Difference;
|
2021-12-14 09:23:07 +00:00
|
|
|
if (sv == "*"sv)
|
2020-10-27 22:25:19 +00:00
|
|
|
return ArithmeticOperation::Product;
|
2021-12-14 09:23:07 +00:00
|
|
|
if (sv == "/"sv)
|
2020-10-27 22:25:19 +00:00
|
|
|
return ArithmeticOperation::Quotient;
|
|
|
|
return ArithmeticOperation::Remainder;
|
|
|
|
}
|
|
|
|
ArithmeticExpression(ArithmeticOperation op, NonnullOwnPtr<Expression>&& left, NonnullOwnPtr<Expression>&& right)
|
|
|
|
: m_op(op)
|
|
|
|
, m_left(move(left))
|
|
|
|
, m_right(move(right))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool truth() const override
|
|
|
|
{
|
|
|
|
switch (m_op) {
|
|
|
|
case ArithmeticOperation::Sum:
|
|
|
|
return m_left->truth() || m_right->truth();
|
|
|
|
default:
|
|
|
|
return integer() != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtual int integer() const override
|
|
|
|
{
|
|
|
|
auto right = m_right->integer();
|
|
|
|
if (right == 0) {
|
|
|
|
if (m_op == ArithmeticOperation::Product)
|
|
|
|
return 0;
|
|
|
|
if (m_op == ArithmeticOperation::Quotient || m_op == ArithmeticOperation::Remainder)
|
|
|
|
fail("Division by zero");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto left = m_left->integer();
|
|
|
|
switch (m_op) {
|
|
|
|
case ArithmeticOperation::Product:
|
|
|
|
return right * left;
|
|
|
|
case ArithmeticOperation::Sum:
|
|
|
|
return right + left;
|
|
|
|
case ArithmeticOperation::Difference:
|
|
|
|
return left - right;
|
|
|
|
case ArithmeticOperation::Quotient:
|
|
|
|
return left / right;
|
|
|
|
case ArithmeticOperation::Remainder:
|
|
|
|
return left % right;
|
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual DeprecatedString string() const override
|
2020-10-27 22:25:19 +00:00
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString::formatted("{}", integer());
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|
|
|
|
virtual Type type() const override
|
|
|
|
{
|
|
|
|
return Type::Integer;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArithmeticOperation m_op { ArithmeticOperation::Sum };
|
|
|
|
NonnullOwnPtr<Expression> m_left, m_right;
|
|
|
|
};
|
|
|
|
|
|
|
|
class StringExpression : public Expression {
|
|
|
|
public:
|
|
|
|
enum class StringOperation {
|
|
|
|
Substring,
|
|
|
|
Index,
|
|
|
|
Length,
|
|
|
|
Match,
|
|
|
|
};
|
|
|
|
|
2021-01-10 23:29:28 +00:00
|
|
|
StringExpression(StringOperation op, NonnullOwnPtr<Expression> string, OwnPtr<Expression> pos_or_chars = {}, OwnPtr<Expression> length = {})
|
2020-10-27 22:25:19 +00:00
|
|
|
: m_op(op)
|
|
|
|
, m_str(move(string))
|
|
|
|
, m_pos_or_chars(move(pos_or_chars))
|
|
|
|
, m_length(move(length))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-10 18:38:17 +00:00
|
|
|
virtual bool truth() const override
|
|
|
|
{
|
|
|
|
if (type() == Expression::Type::String)
|
|
|
|
return !string().is_empty();
|
|
|
|
return integer() != 0;
|
|
|
|
}
|
2020-10-27 22:25:19 +00:00
|
|
|
virtual int integer() const override
|
|
|
|
{
|
2020-11-16 18:13:24 +00:00
|
|
|
if (m_op == StringOperation::Substring || m_op == StringOperation::Match) {
|
2020-10-27 22:25:19 +00:00
|
|
|
auto substr = string();
|
|
|
|
if (auto integer = substr.to_int(); integer.has_value())
|
|
|
|
return integer.value();
|
|
|
|
else
|
|
|
|
fail("Not an integer: '{}'", substr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_op == StringOperation::Index) {
|
2021-05-24 09:50:46 +00:00
|
|
|
if (auto idx = m_str->string().find(m_pos_or_chars->string()); idx.has_value())
|
2020-10-27 22:25:19 +00:00
|
|
|
return idx.value() + 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_op == StringOperation::Length)
|
|
|
|
return m_str->string().length();
|
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|
2022-12-04 18:02:33 +00:00
|
|
|
static auto safe_substring(DeprecatedString const& str, int start, int length)
|
2020-10-27 22:25:19 +00:00
|
|
|
{
|
|
|
|
if (start < 1 || (size_t)start > str.length())
|
|
|
|
fail("Index out of range");
|
|
|
|
--start;
|
|
|
|
if (str.length() - start < (size_t)length)
|
|
|
|
fail("Index out of range");
|
|
|
|
return str.substring(start, length);
|
|
|
|
}
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual DeprecatedString string() const override
|
2020-10-27 22:25:19 +00:00
|
|
|
{
|
|
|
|
if (m_op == StringOperation::Substring)
|
|
|
|
return safe_substring(m_str->string(), m_pos_or_chars->integer(), m_length->integer());
|
|
|
|
|
2020-11-16 18:13:24 +00:00
|
|
|
if (m_op == StringOperation::Match) {
|
|
|
|
auto match = m_compiled_regex->match(m_str->string(), PosixFlags::Global);
|
|
|
|
if (m_compiled_regex->parser_result.capture_groups_count == 0) {
|
|
|
|
if (!match.success)
|
|
|
|
return "0";
|
|
|
|
|
|
|
|
size_t count = 0;
|
|
|
|
for (auto& m : match.matches)
|
|
|
|
count += m.view.length();
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString::number(count);
|
2020-11-16 18:13:24 +00:00
|
|
|
} else {
|
|
|
|
if (!match.success)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
StringBuilder result;
|
2021-07-10 18:38:17 +00:00
|
|
|
for (auto& e : match.capture_group_matches[0])
|
2021-07-18 00:37:01 +00:00
|
|
|
result.append(e.view.string_view());
|
2020-11-16 18:13:24 +00:00
|
|
|
|
2023-01-26 18:58:09 +00:00
|
|
|
return result.to_deprecated_string();
|
2020-11-16 18:13:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString::number(integer());
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|
|
|
|
virtual Type type() const override
|
|
|
|
{
|
|
|
|
if (m_op == StringOperation::Substring)
|
|
|
|
return Type::String;
|
2020-11-16 18:13:24 +00:00
|
|
|
if (m_op == StringOperation::Match) {
|
|
|
|
if (!m_pos_or_chars)
|
|
|
|
fail("'match' expects a string pattern");
|
|
|
|
|
|
|
|
ensure_regex();
|
|
|
|
if (m_compiled_regex->parser_result.capture_groups_count == 0)
|
|
|
|
return Type::Integer;
|
|
|
|
|
|
|
|
return Type::String;
|
|
|
|
}
|
2020-10-27 22:25:19 +00:00
|
|
|
return Type::Integer;
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:13:24 +00:00
|
|
|
void ensure_regex() const
|
|
|
|
{
|
2021-07-10 08:49:47 +00:00
|
|
|
if (!m_compiled_regex) {
|
|
|
|
m_compiled_regex = make<regex::Regex<PosixBasic>>(m_pos_or_chars->string());
|
|
|
|
if (m_compiled_regex->parser_result.error != regex::Error::NoError)
|
|
|
|
fail("Regex error: {}", regex::get_error_string(m_compiled_regex->parser_result.error));
|
|
|
|
}
|
2020-11-16 18:13:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 22:25:19 +00:00
|
|
|
StringOperation m_op { StringOperation::Substring };
|
|
|
|
NonnullOwnPtr<Expression> m_str;
|
|
|
|
OwnPtr<Expression> m_pos_or_chars, m_length;
|
2021-07-10 08:49:47 +00:00
|
|
|
mutable OwnPtr<regex::Regex<PosixBasic>> m_compiled_regex;
|
2020-10-27 22:25:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
NonnullOwnPtr<Expression> Expression::parse(Queue<StringView>& args, Precedence prec)
|
|
|
|
{
|
|
|
|
switch (prec) {
|
|
|
|
case Or: {
|
|
|
|
auto left = parse(args, And);
|
|
|
|
while (!args.is_empty() && args.head() == "|") {
|
|
|
|
args.dequeue();
|
|
|
|
auto right = parse(args, And);
|
|
|
|
left = make<BooleanExpression>(BooleanExpression::BooleanOperator::Or, move(left), move(right));
|
|
|
|
}
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
case And: {
|
|
|
|
auto left = parse(args, Comp);
|
2021-12-14 09:23:07 +00:00
|
|
|
while (!args.is_empty() && args.head() == "&"sv) {
|
2020-10-27 22:25:19 +00:00
|
|
|
args.dequeue();
|
|
|
|
auto right = parse(args, Comp);
|
|
|
|
left = make<BooleanExpression>(BooleanExpression::BooleanOperator::And, move(left), move(right));
|
|
|
|
}
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
case Comp: {
|
|
|
|
auto left = parse(args, ArithS);
|
2021-12-14 09:23:07 +00:00
|
|
|
while (!args.is_empty() && args.head().is_one_of("<"sv, "<="sv, "="sv, "!="sv, "=>"sv, ">"sv)) {
|
2020-10-27 22:25:19 +00:00
|
|
|
auto op = args.dequeue();
|
|
|
|
auto right = parse(args, ArithM);
|
|
|
|
left = make<ComparisonExpression>(ComparisonExpression::op_from(op), move(left), move(right));
|
|
|
|
}
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
case ArithS: {
|
|
|
|
auto left = parse(args, ArithM);
|
2021-12-14 09:23:07 +00:00
|
|
|
while (!args.is_empty() && args.head().is_one_of("+"sv, "-"sv)) {
|
2020-10-27 22:25:19 +00:00
|
|
|
auto op = args.dequeue();
|
|
|
|
auto right = parse(args, ArithM);
|
|
|
|
left = make<ArithmeticExpression>(ArithmeticExpression::op_from(op), move(left), move(right));
|
|
|
|
}
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
case ArithM: {
|
|
|
|
auto left = parse(args, StringO);
|
2021-12-14 09:23:07 +00:00
|
|
|
while (!args.is_empty() && args.head().is_one_of("*"sv, "/"sv, "%"sv)) {
|
2020-10-27 22:25:19 +00:00
|
|
|
auto op = args.dequeue();
|
|
|
|
auto right = parse(args, StringO);
|
|
|
|
left = make<ArithmeticExpression>(ArithmeticExpression::op_from(op), move(left), move(right));
|
|
|
|
}
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
case StringO: {
|
|
|
|
if (args.is_empty())
|
|
|
|
fail("Expected a term");
|
|
|
|
|
|
|
|
OwnPtr<Expression> left;
|
|
|
|
|
|
|
|
while (!args.is_empty()) {
|
|
|
|
auto& op = args.head();
|
2021-12-14 09:23:07 +00:00
|
|
|
if (op == "+"sv) {
|
2020-10-27 22:25:19 +00:00
|
|
|
args.dequeue();
|
|
|
|
left = make<ValueExpression>(args.dequeue());
|
2021-12-14 09:23:07 +00:00
|
|
|
} else if (op == "substr"sv) {
|
2020-10-27 22:25:19 +00:00
|
|
|
args.dequeue();
|
|
|
|
auto str = parse(args, Paren);
|
|
|
|
auto pos = parse(args, Paren);
|
|
|
|
auto len = parse(args, Paren);
|
|
|
|
left = make<StringExpression>(StringExpression::StringOperation::Substring, move(str), move(pos), move(len));
|
2021-12-14 09:23:07 +00:00
|
|
|
} else if (op == "index"sv) {
|
2020-10-27 22:25:19 +00:00
|
|
|
args.dequeue();
|
|
|
|
auto str = parse(args, Paren);
|
|
|
|
auto chars = parse(args, Paren);
|
|
|
|
left = make<StringExpression>(StringExpression::StringOperation::Index, move(str), move(chars));
|
2021-12-14 09:23:07 +00:00
|
|
|
} else if (op == "match"sv) {
|
2020-10-27 22:25:19 +00:00
|
|
|
args.dequeue();
|
|
|
|
auto str = parse(args, Paren);
|
|
|
|
auto pattern = parse(args, Paren);
|
|
|
|
left = make<StringExpression>(StringExpression::StringOperation::Match, move(str), move(pattern));
|
2021-12-14 09:23:07 +00:00
|
|
|
} else if (op == "length"sv) {
|
2020-10-27 22:25:19 +00:00
|
|
|
args.dequeue();
|
|
|
|
auto str = parse(args, Paren);
|
|
|
|
left = make<StringExpression>(StringExpression::StringOperation::Length, move(str));
|
|
|
|
} else if (!left) {
|
|
|
|
left = parse(args, Paren);
|
|
|
|
}
|
|
|
|
|
2021-12-14 09:23:07 +00:00
|
|
|
if (!args.is_empty() && args.head() == ":"sv) {
|
2020-10-27 22:25:19 +00:00
|
|
|
args.dequeue();
|
|
|
|
auto right = parse(args, Paren);
|
|
|
|
left = make<StringExpression>(StringExpression::StringOperation::Match, left.release_nonnull(), move(right));
|
|
|
|
} else {
|
|
|
|
return left.release_nonnull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return left.release_nonnull();
|
|
|
|
}
|
|
|
|
case Paren: {
|
|
|
|
if (args.is_empty())
|
|
|
|
fail("Expected a term");
|
|
|
|
|
2021-12-14 09:23:07 +00:00
|
|
|
if (args.head() == "("sv) {
|
2020-10-27 22:25:19 +00:00
|
|
|
args.dequeue();
|
|
|
|
auto expr = parse(args);
|
|
|
|
if (args.head() != ")")
|
|
|
|
fail("Expected a close paren");
|
|
|
|
args.dequeue();
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return make<ValueExpression>(args.dequeue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fail("Invalid expression");
|
|
|
|
}
|
|
|
|
|
2021-12-14 09:22:35 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-10-27 22:25:19 +00:00
|
|
|
{
|
2021-12-14 09:22:35 +00:00
|
|
|
TRY(Core::System::pledge("stdio"sv));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2020-10-27 22:25:19 +00:00
|
|
|
|
2021-12-14 09:22:35 +00:00
|
|
|
if ((arguments.strings.size() == 2 && "--help"sv == arguments.strings[1]) || arguments.strings.size() == 1)
|
2020-10-27 22:25:19 +00:00
|
|
|
print_help_and_exit();
|
|
|
|
|
|
|
|
Queue<StringView> args;
|
2021-12-14 09:22:35 +00:00
|
|
|
for (size_t i = 1; i < arguments.strings.size(); ++i)
|
|
|
|
args.enqueue(arguments.strings[i]);
|
2020-10-27 22:25:19 +00:00
|
|
|
|
|
|
|
auto expression = Expression::parse(args);
|
|
|
|
if (!args.is_empty())
|
|
|
|
fail("Extra tokens at the end of the expression");
|
|
|
|
|
|
|
|
switch (expression->type()) {
|
|
|
|
case Expression::Type::Integer:
|
|
|
|
outln("{}", expression->integer());
|
|
|
|
break;
|
|
|
|
case Expression::Type::String:
|
|
|
|
outln("{}", expression->string());
|
|
|
|
break;
|
|
|
|
}
|
2021-07-08 16:04:15 +00:00
|
|
|
return expression->truth() ? 0 : 1;
|
2020-10-27 22:25:19 +00:00
|
|
|
}
|