summaryrefslogtreecommitdiffstats
path: root/lib/config/expression.hpp
blob: 644548d286eb83b8352a3093a119a285c219667d (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#ifndef EXPRESSION_H
#define EXPRESSION_H

#include "config/i2-config.hpp"
#include "base/debuginfo.hpp"
#include "base/array.hpp"
#include "base/dictionary.hpp"
#include "base/function.hpp"
#include "base/exception.hpp"
#include "base/scriptframe.hpp"
#include "base/shared-object.hpp"
#include "base/convert.hpp"
#include <map>

namespace icinga
{

struct DebugHint
{
public:
	DebugHint(Dictionary::Ptr hints = nullptr)
		: m_Hints(std::move(hints))
	{ }

	DebugHint(Dictionary::Ptr&& hints)
		: m_Hints(std::move(hints))
	{ }

	void AddMessage(const String& message, const DebugInfo& di)
	{
		GetMessages()->Add(new Array({ message, di.Path, di.FirstLine, di.FirstColumn, di.LastLine, di.LastColumn }));
	}

	DebugHint GetChild(const String& name)
	{
		const Dictionary::Ptr& children = GetChildren();

		Value vchild;
		Dictionary::Ptr child;

		if (!children->Get(name, &vchild)) {
			child = new Dictionary();
			children->Set(name, child);
		} else
			child = vchild;

		return DebugHint(child);
	}

	Dictionary::Ptr ToDictionary() const
	{
		return m_Hints;
	}

private:
	Dictionary::Ptr m_Hints;
	Array::Ptr m_Messages;
	Dictionary::Ptr m_Children;

	const Array::Ptr& GetMessages()
	{
		if (m_Messages)
			return m_Messages;

		if (!m_Hints)
			m_Hints = new Dictionary();

		Value vmessages;

		if (!m_Hints->Get("messages", &vmessages)) {
			m_Messages = new Array();
			m_Hints->Set("messages", m_Messages);
		} else
			m_Messages = vmessages;

		return m_Messages;
	}

	const Dictionary::Ptr& GetChildren()
	{
		if (m_Children)
			return m_Children;

		if (!m_Hints)
			m_Hints = new Dictionary();

		Value vchildren;

		if (!m_Hints->Get("properties", &vchildren)) {
			m_Children = new Dictionary();
			m_Hints->Set("properties", m_Children);
		} else
			m_Children = vchildren;

		return m_Children;
	}
};

enum CombinedSetOp
{
	OpSetLiteral,
	OpSetAdd,
	OpSetSubtract,
	OpSetMultiply,
	OpSetDivide,
	OpSetModulo,
	OpSetXor,
	OpSetBinaryAnd,
	OpSetBinaryOr
};

enum ScopeSpecifier
{
	ScopeLocal,
	ScopeThis,
	ScopeGlobal
};

typedef std::map<String, String> DefinitionMap;

/**
 * @ingroup config
 */
enum ExpressionResultCode
{
	ResultOK,
	ResultReturn,
	ResultContinue,
	ResultBreak
};

/**
 * @ingroup config
 */
struct ExpressionResult
{
public:
	template<typename T>
	ExpressionResult(T value, ExpressionResultCode code = ResultOK)
		: m_Value(std::move(value)), m_Code(code)
	{ }

	operator const Value&() const
	{
		return m_Value;
	}

	const Value& GetValue() const
	{
		return m_Value;
	}

	ExpressionResultCode GetCode() const
	{
		return m_Code;
	}

private:
	Value m_Value;
	ExpressionResultCode m_Code;
};

#define CHECK_RESULT(res)			\
	do {					\
		if (res.GetCode() != ResultOK)	\
			return res;		\
	} while (0);

#define CHECK_RESULT_LOOP(res)			\
	if (res.GetCode() == ResultReturn)	\
		return res;			\
	if (res.GetCode() == ResultContinue)	\
		continue;			\
	if (res.GetCode() == ResultBreak)	\
		break;				\

/**
 * @ingroup config
 */
class Expression : public SharedObject
{
public:
	DECLARE_PTR_TYPEDEFS(Expression);

	Expression() = default;
	Expression(const Expression&) = delete;
	virtual ~Expression();

	Expression& operator=(const Expression&) = delete;

	ExpressionResult Evaluate(ScriptFrame& frame, DebugHint *dhint = nullptr) const;
	virtual bool GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint = nullptr) const;
	virtual const DebugInfo& GetDebugInfo() const;

	virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const = 0;

	static boost::signals2::signal<void (ScriptFrame& frame, ScriptError *ex, const DebugInfo& di)> OnBreakpoint;

	static void ScriptBreakpoint(ScriptFrame& frame, ScriptError *ex, const DebugInfo& di);
};

std::unique_ptr<Expression> MakeIndexer(ScopeSpecifier scopeSpec, const String& index);

class OwnedExpression final : public Expression
{
public:
	OwnedExpression(Expression::Ptr expression)
		: m_Expression(std::move(expression))
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override
	{
		return m_Expression->DoEvaluate(frame, dhint);
	}

	const DebugInfo& GetDebugInfo() const override
	{
		return m_Expression->GetDebugInfo();
	}

private:
	Expression::Ptr m_Expression;
};

class LiteralExpression final : public Expression
{
public:
	LiteralExpression(Value value = Value());

	const Value& GetValue() const
	{
		return m_Value;
	}

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	Value m_Value;
};

inline LiteralExpression *MakeLiteralRaw(const Value& literal = Value())
{
	return new LiteralExpression(literal);
}

inline std::unique_ptr<LiteralExpression> MakeLiteral(const Value& literal = Value())
{
	return std::unique_ptr<LiteralExpression>(MakeLiteralRaw(literal));
}

class DebuggableExpression : public Expression
{
public:
	DebuggableExpression(DebugInfo debugInfo = DebugInfo())
		: m_DebugInfo(std::move(debugInfo))
	{ }

protected:
	const DebugInfo& GetDebugInfo() const final;

	DebugInfo m_DebugInfo;
};

class UnaryExpression : public DebuggableExpression
{
public:
	UnaryExpression(std::unique_ptr<Expression> operand, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_Operand(std::move(operand))
	{ }

protected:
	std::unique_ptr<Expression> m_Operand;
};

class BinaryExpression : public DebuggableExpression
{
public:
	BinaryExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_Operand1(std::move(operand1)), m_Operand2(std::move(operand2))
	{ }

	inline const std::unique_ptr<Expression>& GetOperand1() const noexcept
	{
		return m_Operand1;
	}

	inline const std::unique_ptr<Expression>& GetOperand2() const noexcept
	{
		return m_Operand2;
	}

protected:
	std::unique_ptr<Expression> m_Operand1;
	std::unique_ptr<Expression> m_Operand2;
};

class VariableExpression final : public DebuggableExpression
{
public:
	VariableExpression(String variable, std::vector<Expression::Ptr> imports, const DebugInfo& debugInfo = DebugInfo());

	inline const String& GetVariable() const
	{
		return m_Variable;
	}

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
	bool GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const override;

private:
	String m_Variable;
	std::vector<Expression::Ptr> m_Imports;

	friend void BindToScope(std::unique_ptr<Expression>& expr, ScopeSpecifier scopeSpec);
};

class DerefExpression final : public UnaryExpression
{
public:
	DerefExpression(std::unique_ptr<Expression> operand, const DebugInfo& debugInfo = DebugInfo())
		: UnaryExpression(std::move(operand), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
	bool GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const override;
};

class RefExpression final : public UnaryExpression
{
public:
	RefExpression(std::unique_ptr<Expression> operand, const DebugInfo& debugInfo = DebugInfo())
		: UnaryExpression(std::move(operand), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class NegateExpression final : public UnaryExpression
{
public:
	NegateExpression(std::unique_ptr<Expression> operand, const DebugInfo& debugInfo = DebugInfo())
		: UnaryExpression(std::move(operand), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class LogicalNegateExpression final : public UnaryExpression
{
public:
	LogicalNegateExpression(std::unique_ptr<Expression> operand, const DebugInfo& debugInfo = DebugInfo())
		: UnaryExpression(std::move(operand), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class AddExpression final : public BinaryExpression
{
public:
	AddExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class SubtractExpression final : public BinaryExpression
{
public:
	SubtractExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class MultiplyExpression final : public BinaryExpression
{
public:
	MultiplyExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class DivideExpression final : public BinaryExpression
{
public:
	DivideExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class ModuloExpression final : public BinaryExpression
{
public:
	ModuloExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class XorExpression final : public BinaryExpression
{
public:
	XorExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class BinaryAndExpression final : public BinaryExpression
{
public:
	BinaryAndExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class BinaryOrExpression final : public BinaryExpression
{
public:
	BinaryOrExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class ShiftLeftExpression final : public BinaryExpression
{
public:
	ShiftLeftExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class ShiftRightExpression final : public BinaryExpression
{
public:
	ShiftRightExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class EqualExpression final : public BinaryExpression
{
public:
	EqualExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class NotEqualExpression final : public BinaryExpression
{
public:
	NotEqualExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class LessThanExpression final : public BinaryExpression
{
public:
	LessThanExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class GreaterThanExpression final : public BinaryExpression
{
public:
	GreaterThanExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class LessThanOrEqualExpression final : public BinaryExpression
{
public:
	LessThanOrEqualExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class GreaterThanOrEqualExpression final : public BinaryExpression
{
public:
	GreaterThanOrEqualExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class InExpression final : public BinaryExpression
{
public:
	InExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class NotInExpression final : public BinaryExpression
{
public:
	NotInExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class LogicalAndExpression final : public BinaryExpression
{
public:
	LogicalAndExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class LogicalOrExpression final : public BinaryExpression
{
public:
	LogicalOrExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class FunctionCallExpression final : public DebuggableExpression
{
public:
	FunctionCallExpression(std::unique_ptr<Expression> fname, std::vector<std::unique_ptr<Expression> >&& args, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_FName(std::move(fname)), m_Args(std::move(args))
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

public:
	std::unique_ptr<Expression> m_FName;
	std::vector<std::unique_ptr<Expression> > m_Args;
};

class ArrayExpression final : public DebuggableExpression
{
public:
	ArrayExpression(std::vector<std::unique_ptr<Expression > >&& expressions, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_Expressions(std::move(expressions))
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	std::vector<std::unique_ptr<Expression> > m_Expressions;
};

class DictExpression final : public DebuggableExpression
{
public:
	DictExpression(std::vector<std::unique_ptr<Expression> >&& expressions = {}, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_Expressions(std::move(expressions))
	{ }

	void MakeInline();

	inline const std::vector<std::unique_ptr<Expression>>& GetExpressions() const noexcept
	{
		return m_Expressions;
	}

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	std::vector<std::unique_ptr<Expression> > m_Expressions;
	bool m_Inline{false};

	friend void BindToScope(std::unique_ptr<Expression>& expr, ScopeSpecifier scopeSpec);
};

class SetConstExpression final : public UnaryExpression
{
public:
	SetConstExpression(const String& name, std::unique_ptr<Expression> operand, const DebugInfo& debugInfo = DebugInfo())
		: UnaryExpression(std::move(operand), debugInfo), m_Name(name)
	{ }

protected:
	String m_Name;

	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class SetExpression final : public BinaryExpression
{
public:
	SetExpression(std::unique_ptr<Expression> operand1, CombinedSetOp op, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo), m_Op(op)
	{ }

	void SetOverrideFrozen();

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	CombinedSetOp m_Op;
	bool m_OverrideFrozen{false};

	friend void BindToScope(std::unique_ptr<Expression>& expr, ScopeSpecifier scopeSpec);
};

class ConditionalExpression final : public DebuggableExpression
{
public:
	ConditionalExpression(std::unique_ptr<Expression> condition, std::unique_ptr<Expression> true_branch, std::unique_ptr<Expression> false_branch, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_Condition(std::move(condition)), m_TrueBranch(std::move(true_branch)), m_FalseBranch(std::move(false_branch))
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	std::unique_ptr<Expression> m_Condition;
	std::unique_ptr<Expression> m_TrueBranch;
	std::unique_ptr<Expression> m_FalseBranch;
};

class WhileExpression final : public DebuggableExpression
{
public:
	WhileExpression(std::unique_ptr<Expression> condition, std::unique_ptr<Expression> loop_body, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_Condition(std::move(condition)), m_LoopBody(std::move(loop_body))
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	std::unique_ptr<Expression> m_Condition;
	std::unique_ptr<Expression> m_LoopBody;
};


class ReturnExpression final : public UnaryExpression
{
public:
	ReturnExpression(std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
		: UnaryExpression(std::move(expression), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class BreakExpression final : public DebuggableExpression
{
public:
	BreakExpression(const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class ContinueExpression final : public DebuggableExpression
{
public:
	ContinueExpression(const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class GetScopeExpression final : public Expression
{
public:
	GetScopeExpression(ScopeSpecifier scopeSpec)
		: m_ScopeSpec(scopeSpec)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	ScopeSpecifier m_ScopeSpec;
};

class IndexerExpression final : public BinaryExpression
{
public:
	IndexerExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
		: BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
	{ }

	void SetOverrideFrozen();

protected:
	bool m_OverrideFrozen{false};

	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
	bool GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const override;

	friend void BindToScope(std::unique_ptr<Expression>& expr, ScopeSpecifier scopeSpec);
};

void BindToScope(std::unique_ptr<Expression>& expr, ScopeSpecifier scopeSpec);

class ThrowExpression final : public DebuggableExpression
{
public:
	ThrowExpression(std::unique_ptr<Expression> message, bool incompleteExpr, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_Message(std::move(message)), m_IncompleteExpr(incompleteExpr)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	std::unique_ptr<Expression> m_Message;
	bool m_IncompleteExpr;
};

class ImportExpression final : public DebuggableExpression
{
public:
	ImportExpression(std::unique_ptr<Expression> name, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_Name(std::move(name))
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	std::unique_ptr<Expression> m_Name;
};

class ImportDefaultTemplatesExpression final : public DebuggableExpression
{
public:
	ImportDefaultTemplatesExpression(const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class FunctionExpression final : public DebuggableExpression
{
public:
	FunctionExpression(String name, std::vector<String> args,
		std::map<String, std::unique_ptr<Expression> >&& closedVars, std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_Name(std::move(name)), m_Args(std::move(args)), m_ClosedVars(std::move(closedVars)), m_Expression(expression.release())
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	String m_Name;
	std::vector<String> m_Args;
	std::map<String, std::unique_ptr<Expression> > m_ClosedVars;
	Expression::Ptr m_Expression;
};

class ApplyExpression final : public DebuggableExpression
{
public:
	ApplyExpression(String type, String target, std::unique_ptr<Expression> name,
		std::unique_ptr<Expression> filter, String package, String fkvar, String fvvar,
		std::unique_ptr<Expression> fterm, std::map<String, std::unique_ptr<Expression> >&& closedVars, bool ignoreOnError,
		std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_Type(std::move(type)), m_Target(std::move(target)),
			m_Name(std::move(name)), m_Filter(filter.release()), m_Package(std::move(package)), m_FKVar(std::move(fkvar)), m_FVVar(std::move(fvvar)),
			m_FTerm(fterm.release()), m_IgnoreOnError(ignoreOnError), m_ClosedVars(std::move(closedVars)),
			m_Expression(expression.release())
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	String m_Type;
	String m_Target;
	std::unique_ptr<Expression> m_Name;
	Expression::Ptr m_Filter;
	String m_Package;
	String m_FKVar;
	String m_FVVar;
	Expression::Ptr m_FTerm;
	bool m_IgnoreOnError;
	std::map<String, std::unique_ptr<Expression> > m_ClosedVars;
	Expression::Ptr m_Expression;
};

class NamespaceExpression final : public DebuggableExpression
{
public:
	NamespaceExpression(std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_Expression(expression.release())
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	Expression::Ptr m_Expression;
};

class ObjectExpression final : public DebuggableExpression
{
public:
	ObjectExpression(bool abstract, std::unique_ptr<Expression> type, std::unique_ptr<Expression> name, std::unique_ptr<Expression> filter,
		String zone, String package, std::map<String, std::unique_ptr<Expression> >&& closedVars,
		bool defaultTmpl, bool ignoreOnError, std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_Abstract(abstract), m_Type(std::move(type)),
		m_Name(std::move(name)), m_Filter(filter.release()), m_Zone(std::move(zone)), m_Package(std::move(package)), m_DefaultTmpl(defaultTmpl),
		m_IgnoreOnError(ignoreOnError), m_ClosedVars(std::move(closedVars)), m_Expression(expression.release())
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	bool m_Abstract;
	std::unique_ptr<Expression> m_Type;
	std::unique_ptr<Expression> m_Name;
	Expression::Ptr m_Filter;
	String m_Zone;
	String m_Package;
	bool m_DefaultTmpl;
	bool m_IgnoreOnError;
	std::map<String, std::unique_ptr<Expression> > m_ClosedVars;
	Expression::Ptr m_Expression;
};

class ForExpression final : public DebuggableExpression
{
public:
	ForExpression(String fkvar, String fvvar, std::unique_ptr<Expression> value, std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_FKVar(std::move(fkvar)), m_FVVar(std::move(fvvar)), m_Value(std::move(value)), m_Expression(std::move(expression))
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	String m_FKVar;
	String m_FVVar;
	std::unique_ptr<Expression> m_Value;
	std::unique_ptr<Expression> m_Expression;
};

class LibraryExpression final : public UnaryExpression
{
public:
	LibraryExpression(std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
		: UnaryExpression(std::move(expression), debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

enum IncludeType
{
	IncludeRegular,
	IncludeRecursive,
	IncludeZones
};

class IncludeExpression final : public DebuggableExpression
{
public:
	IncludeExpression(String relativeBase, std::unique_ptr<Expression> path, std::unique_ptr<Expression> pattern, std::unique_ptr<Expression> name,
		IncludeType type, bool searchIncludes, String zone, String package, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_RelativeBase(std::move(relativeBase)), m_Path(std::move(path)), m_Pattern(std::move(pattern)),
		m_Name(std::move(name)), m_Type(type), m_SearchIncludes(searchIncludes), m_Zone(std::move(zone)), m_Package(std::move(package))
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	String m_RelativeBase;
	std::unique_ptr<Expression> m_Path;
	std::unique_ptr<Expression> m_Pattern;
	std::unique_ptr<Expression> m_Name;
	IncludeType m_Type;
	bool m_SearchIncludes;
	String m_Zone;
	String m_Package;
};

class BreakpointExpression final : public DebuggableExpression
{
public:
	BreakpointExpression(const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo)
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
};

class TryExceptExpression final : public DebuggableExpression
{
public:
	TryExceptExpression(std::unique_ptr<Expression> tryBody, std::unique_ptr<Expression> exceptBody, const DebugInfo& debugInfo = DebugInfo())
		: DebuggableExpression(debugInfo), m_TryBody(std::move(tryBody)), m_ExceptBody(std::move(exceptBody))
	{ }

protected:
	ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;

private:
	std::unique_ptr<Expression> m_TryBody;
	std::unique_ptr<Expression> m_ExceptBody;
};

}

#endif /* EXPRESSION_H */