/
ExpressionParser.cpp
840 lines (720 loc) · 19.3 KB
/
ExpressionParser.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
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
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <memory>
#include <regex>
#include <string>
#include <vector>
#include "Common/MathUtil.h"
#include "Common/StringUtil.h"
#include "InputCommon/ControlReference/ExpressionParser.h"
namespace ciface::ExpressionParser
{
using namespace ciface::Core;
enum TokenType
{
TOK_DISCARD,
TOK_INVALID,
TOK_EOF,
TOK_LPAREN,
TOK_RPAREN,
TOK_AND,
TOK_OR,
TOK_UNARY,
TOK_ADD,
TOK_MUL,
TOK_DIV,
TOK_MOD,
TOK_ASSIGN,
TOK_LTHAN,
TOK_GTHAN,
TOK_CONTROL,
TOK_LITERAL,
TOK_VARIABLE,
};
inline std::string OpName(TokenType op)
{
switch (op)
{
case TOK_AND:
return "And";
case TOK_OR:
return "Or";
case TOK_UNARY:
return "Unary";
case TOK_ADD:
return "Add";
case TOK_MUL:
return "Mul";
case TOK_DIV:
return "Div";
case TOK_MOD:
return "Mod";
case TOK_ASSIGN:
return "Assign";
case TOK_LTHAN:
return "LThan";
case TOK_GTHAN:
return "GThan";
case TOK_VARIABLE:
return "Var";
default:
assert(false);
return "";
}
}
class Token
{
public:
TokenType type;
std::string data;
Token(TokenType type_) : type(type_) {}
Token(TokenType type_, std::string data_) : type(type_), data(std::move(data_)) {}
operator std::string() const
{
switch (type)
{
case TOK_DISCARD:
return "Discard";
case TOK_EOF:
return "EOF";
case TOK_LPAREN:
return "(";
case TOK_RPAREN:
return ")";
case TOK_AND:
return "&";
case TOK_OR:
return "|";
case TOK_UNARY:
return "!" + data;
case TOK_ADD:
return "+";
case TOK_MUL:
return "*";
case TOK_DIV:
return "/";
case TOK_MOD:
return "%";
case TOK_ASSIGN:
return "=";
case TOK_LTHAN:
return "<";
case TOK_GTHAN:
return ">";
case TOK_CONTROL:
return "Device(" + data + ")";
case TOK_LITERAL:
return '\'' + data + '\'';
case TOK_VARIABLE:
return '$' + data;
case TOK_INVALID:
break;
}
return "Invalid";
}
};
class Lexer
{
public:
std::string expr;
std::string::iterator it;
Lexer(const std::string& expr_) : expr(expr_) { it = expr.begin(); }
bool FetchDelimString(std::string& value, char delim)
{
value = "";
while (it != expr.end())
{
char c = *it;
++it;
if (c == delim)
return true;
value += c;
}
return false;
}
std::string FetchWordChars()
{
std::string word;
std::regex valid_name_char("[a-z0-9_]", std::regex_constants::icase);
while (it != expr.end() && std::regex_match(std::string(1, *it), valid_name_char))
{
word += *it;
++it;
}
return word;
}
Token GetUnaryFunction() { return Token(TOK_UNARY, FetchWordChars()); }
Token GetLiteral()
{
std::string value;
FetchDelimString(value, '\'');
return Token(TOK_LITERAL, value);
}
Token GetVariable() { return Token(TOK_VARIABLE, FetchWordChars()); }
Token GetFullyQualifiedControl()
{
std::string value;
FetchDelimString(value, '`');
return Token(TOK_CONTROL, value);
}
Token GetBarewordsControl(char c)
{
std::string name;
name += c;
while (it != expr.end())
{
c = *it;
if (!isalpha(c))
break;
name += c;
++it;
}
ControlQualifier qualifier;
qualifier.control_name = name;
return Token(TOK_CONTROL, qualifier);
}
Token NextToken()
{
if (it == expr.end())
return Token(TOK_EOF);
char c = *it++;
switch (c)
{
case ' ':
case '\t':
case '\n':
case '\r':
return Token(TOK_DISCARD);
case '(':
return Token(TOK_LPAREN);
case ')':
return Token(TOK_RPAREN);
case '&':
return Token(TOK_AND);
case '|':
return Token(TOK_OR);
case '!':
return GetUnaryFunction();
case '+':
return Token(TOK_ADD);
case '*':
return Token(TOK_MUL);
case '/':
return Token(TOK_DIV);
case '%':
return Token(TOK_MOD);
case '=':
return Token(TOK_ASSIGN);
case '<':
return Token(TOK_LTHAN);
case '>':
return Token(TOK_GTHAN);
case '\'':
return GetLiteral();
case '$':
return GetVariable();
case '`':
return GetFullyQualifiedControl();
default:
if (isalpha(c))
return GetBarewordsControl(c);
else
return Token(TOK_INVALID);
}
}
ParseStatus Tokenize(std::vector<Token>& tokens)
{
while (true)
{
Token tok = NextToken();
if (tok.type == TOK_DISCARD)
continue;
if (tok.type == TOK_INVALID)
{
tokens.clear();
return ParseStatus::SyntaxError;
}
tokens.push_back(tok);
if (tok.type == TOK_EOF)
break;
}
return ParseStatus::Successful;
}
};
class ControlExpression : public Expression
{
public:
// Keep a shared_ptr to the device so the control pointer doesn't become invalid
// TODO: This is causing devices to be destructed after backends are shutdown:
std::shared_ptr<Device> m_device;
explicit ControlExpression(ControlQualifier qualifier_) : qualifier(qualifier_) {}
ControlState GetValue() const override
{
if (!input)
return 0.0;
// Note: Inputs may return negative values in situations where opposing directions are
// activated. We clamp off the negative values here.
// FYI: Clamping values greater than 1.0 is purposely not done to support unbounded values in
// the future. (e.g. raw accelerometer/gyro data)
return std::max(0.0, input->GetState());
}
void SetValue(ControlState value) override
{
if (output)
output->SetState(value);
}
int CountNumControls() const override { return (input || output) ? 1 : 0; }
void UpdateReferences(ControlEnvironment& env) override
{
m_device = env.FindDevice(qualifier);
input = env.FindInput(qualifier);
output = env.FindOutput(qualifier);
}
operator std::string() const override { return "`" + static_cast<std::string>(qualifier) + "`"; }
private:
ControlQualifier qualifier;
Device::Input* input = nullptr;
Device::Output* output = nullptr;
};
class BinaryExpression : public Expression
{
public:
TokenType op;
std::unique_ptr<Expression> lhs;
std::unique_ptr<Expression> rhs;
BinaryExpression(TokenType op_, std::unique_ptr<Expression>&& lhs_,
std::unique_ptr<Expression>&& rhs_)
: op(op_), lhs(std::move(lhs_)), rhs(std::move(rhs_))
{
}
ControlState GetValue() const override
{
ControlState lhsValue = lhs->GetValue();
ControlState rhsValue = rhs->GetValue();
switch (op)
{
case TOK_AND:
return std::min(lhsValue, rhsValue);
case TOK_OR:
return std::max(lhsValue, rhsValue);
case TOK_ADD:
return lhsValue + rhsValue;
case TOK_MUL:
return lhsValue * rhsValue;
case TOK_DIV:
{
const ControlState result = lhsValue / rhsValue;
return std::isinf(result) ? 0.0 : result;
}
case TOK_MOD:
{
const ControlState result = std::fmod(lhsValue, rhsValue);
return std::isnan(result) ? 0.0 : result;
}
case TOK_ASSIGN:
{
lhs->SetValue(rhsValue);
// TODO: Should this instead GetValue(lhs) ?
return rhsValue;
}
case TOK_LTHAN:
return lhsValue < rhsValue;
case TOK_GTHAN:
return lhsValue > rhsValue;
default:
assert(false);
return 0;
}
}
void SetValue(ControlState value) override
{
// Don't do anything special with the op we have.
// Treat "A & B" the same as "A | B".
lhs->SetValue(value);
rhs->SetValue(value);
}
int CountNumControls() const override
{
return lhs->CountNumControls() + rhs->CountNumControls();
}
void UpdateReferences(ControlEnvironment& env) override
{
lhs->UpdateReferences(env);
rhs->UpdateReferences(env);
}
operator std::string() const override
{
return OpName(op) + "(" + (std::string)(*lhs) + ", " + (std::string)(*rhs) + ")";
}
};
class UnaryExpression : public Expression
{
public:
UnaryExpression(std::unique_ptr<Expression>&& inner_) : inner(std::move(inner_)) {}
int CountNumControls() const override { return inner->CountNumControls(); }
void UpdateReferences(ControlEnvironment& env) override { inner->UpdateReferences(env); }
operator std::string() const override
{
return "!" + GetFuncName() + "(" + (std::string)(*inner) + ")";
}
protected:
virtual std::string GetFuncName() const = 0;
std::unique_ptr<Expression> inner;
};
// TODO: Return an oscillating value to make it apparent something was spelled wrong?
class UnaryUnknownExpression : public UnaryExpression
{
public:
UnaryUnknownExpression(std::unique_ptr<Expression>&& inner_) : UnaryExpression(std::move(inner_))
{
}
ControlState GetValue() const override { return 0.0; }
void SetValue(ControlState value) override {}
std::string GetFuncName() const override { return "Unknown"; }
};
class UnaryToggleExpression : public UnaryExpression
{
public:
UnaryToggleExpression(std::unique_ptr<Expression>&& inner_) : UnaryExpression(std::move(inner_))
{
}
ControlState GetValue() const override
{
const ControlState inner_value = inner->GetValue();
if (inner_value < THRESHOLD)
{
m_released = true;
}
else if (m_released && inner_value > THRESHOLD)
{
m_released = false;
m_state ^= true;
}
return m_state;
}
void SetValue(ControlState value) override {}
std::string GetFuncName() const override { return "Toggle"; }
private:
static constexpr ControlState THRESHOLD = 0.5;
// eww:
mutable bool m_released{};
mutable bool m_state{};
};
class UnaryNotExpression : public UnaryExpression
{
public:
UnaryNotExpression(std::unique_ptr<Expression>&& inner_) : UnaryExpression(std::move(inner_)) {}
ControlState GetValue() const override { return 1.0 - inner->GetValue(); }
void SetValue(ControlState value) override { inner->SetValue(1.0 - value); }
std::string GetFuncName() const override { return ""; }
};
class UnarySinExpression : public UnaryExpression
{
public:
UnarySinExpression(std::unique_ptr<Expression>&& inner_) : UnaryExpression(std::move(inner_)) {}
ControlState GetValue() const override { return std::cos(inner->GetValue()); }
void SetValue(ControlState value) override {}
std::string GetFuncName() const override { return "Sin"; }
};
std::unique_ptr<UnaryExpression> MakeUnaryExpression(std::string name,
std::unique_ptr<Expression>&& inner_)
{
// Case insensitive matching.
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
if ("" == name)
return std::make_unique<UnaryNotExpression>(std::move(inner_));
else if ("toggle" == name)
return std::make_unique<UnaryToggleExpression>(std::move(inner_));
else if ("sin" == name)
return std::make_unique<UnarySinExpression>(std::move(inner_));
else
return std::make_unique<UnaryUnknownExpression>(std::move(inner_));
}
class LiteralExpression : public Expression
{
public:
void SetValue(ControlState value) override
{
// Do nothing.
}
int CountNumControls() const override { return 1; }
void UpdateReferences(ControlEnvironment&) override
{
// Nothing needed.
}
operator std::string() const override { return '\'' + GetName() + '\''; }
protected:
virtual std::string GetName() const = 0;
};
class LiteralReal : public LiteralExpression
{
public:
LiteralReal(ControlState value) : m_value(value) {}
ControlState GetValue() const override { return m_value; }
std::string GetName() const override { return ValueToString(m_value); }
private:
const ControlState m_value{};
};
// A +1.0 per second incrementing timer:
class LiteralTimer : public LiteralExpression
{
public:
ControlState GetValue() const override
{
const auto ms =
std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now().time_since_epoch());
// TODO: Will this roll over nicely?
return ms.count() / 1000.0;
}
std::string GetName() const override { return "Timer"; }
private:
using Clock = std::chrono::steady_clock;
};
std::unique_ptr<LiteralExpression> MakeLiteralExpression(std::string name)
{
// Case insensitive matching.
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
// Check for named literals:
if ("timer" == name)
{
return std::make_unique<LiteralTimer>();
}
else
{
// Assume it's a Real. If TryParse fails we'll just get a Zero.
ControlState val{};
TryParse(name, &val);
return std::make_unique<LiteralReal>(val);
}
}
class VariableExpression : public Expression
{
public:
VariableExpression(std::string name) : m_name(name) {}
ControlState GetValue() const override { return *m_value_ptr; }
void SetValue(ControlState value) override { *m_value_ptr = value; }
int CountNumControls() const override { return 1; }
void UpdateReferences(ControlEnvironment& env) override
{
m_value_ptr = env.GetVariablePtr(m_name);
}
operator std::string() const override { return '$' + m_name; }
protected:
const std::string m_name;
ControlState* m_value_ptr{};
};
// This class proxies all methods to its either left-hand child if it has bound controls, or its
// right-hand child. Its intended use is for supporting old-style barewords expressions.
class CoalesceExpression : public Expression
{
public:
CoalesceExpression(std::unique_ptr<Expression>&& lhs, std::unique_ptr<Expression>&& rhs)
: m_lhs(std::move(lhs)), m_rhs(std::move(rhs))
{
}
ControlState GetValue() const override { return GetActiveChild()->GetValue(); }
void SetValue(ControlState value) override { GetActiveChild()->SetValue(value); }
int CountNumControls() const override { return GetActiveChild()->CountNumControls(); }
operator std::string() const override
{
return "Coalesce(" + static_cast<std::string>(*m_lhs) + ", " +
static_cast<std::string>(*m_rhs) + ')';
}
void UpdateReferences(ControlEnvironment& env) override
{
m_lhs->UpdateReferences(env);
m_rhs->UpdateReferences(env);
}
private:
const std::unique_ptr<Expression>& GetActiveChild() const
{
return m_lhs->CountNumControls() > 0 ? m_lhs : m_rhs;
}
std::unique_ptr<Expression> m_lhs;
std::unique_ptr<Expression> m_rhs;
};
std::shared_ptr<Device> ControlEnvironment::FindDevice(ControlQualifier qualifier) const
{
if (qualifier.has_device)
return container.FindDevice(qualifier.device_qualifier);
else
return container.FindDevice(default_device);
}
Device::Input* ControlEnvironment::FindInput(ControlQualifier qualifier) const
{
const std::shared_ptr<Device> device = FindDevice(qualifier);
if (!device)
return nullptr;
return device->FindInput(qualifier.control_name);
}
Device::Output* ControlEnvironment::FindOutput(ControlQualifier qualifier) const
{
const std::shared_ptr<Device> device = FindDevice(qualifier);
if (!device)
return nullptr;
return device->FindOutput(qualifier.control_name);
}
ControlState* ControlEnvironment::GetVariablePtr(const std::string& name)
{
return &m_variables[name];
}
struct ParseResult
{
ParseResult(ParseStatus status_, std::unique_ptr<Expression>&& expr_ = {})
: status(status_), expr(std::move(expr_))
{
}
ParseStatus status;
std::unique_ptr<Expression> expr;
};
class Parser
{
public:
explicit Parser(std::vector<Token> tokens_) : tokens(tokens_) { m_it = tokens.begin(); }
ParseResult Parse() { return Toplevel(); }
private:
std::vector<Token> tokens;
std::vector<Token>::iterator m_it;
Token Chew() { return *m_it++; }
Token Peek() { return *m_it; }
bool Expects(TokenType type)
{
Token tok = Chew();
return tok.type == type;
}
ParseResult Atom()
{
Token tok = Chew();
switch (tok.type)
{
case TOK_CONTROL:
{
ControlQualifier cq;
cq.FromString(tok.data);
return {ParseStatus::Successful, std::make_unique<ControlExpression>(cq)};
}
case TOK_LITERAL:
{
return {ParseStatus::Successful, MakeLiteralExpression(tok.data)};
}
case TOK_VARIABLE:
{
return {ParseStatus::Successful, std::make_unique<VariableExpression>(tok.data)};
}
case TOK_LPAREN:
return Paren();
default:
return {ParseStatus::SyntaxError};
}
}
bool IsUnaryExpression(TokenType type)
{
switch (type)
{
case TOK_UNARY:
return true;
default:
return false;
}
}
ParseResult Unary()
{
if (IsUnaryExpression(Peek().type))
{
Token tok = Chew();
ParseResult result = Atom();
if (result.status == ParseStatus::SyntaxError)
return result;
return {ParseStatus::Successful, MakeUnaryExpression(tok.data, std::move(result.expr))};
}
return Atom();
}
bool IsBinaryToken(TokenType type)
{
switch (type)
{
case TOK_AND:
case TOK_OR:
case TOK_ADD:
case TOK_MUL:
case TOK_DIV:
case TOK_MOD:
case TOK_ASSIGN:
case TOK_LTHAN:
case TOK_GTHAN:
return true;
default:
return false;
}
}
ParseResult Binary()
{
ParseResult result = Unary();
if (result.status == ParseStatus::SyntaxError)
return result;
std::unique_ptr<Expression> expr = std::move(result.expr);
while (IsBinaryToken(Peek().type))
{
Token tok = Chew();
ParseResult unary_result = Unary();
if (unary_result.status == ParseStatus::SyntaxError)
{
return unary_result;
}
expr = std::make_unique<BinaryExpression>(tok.type, std::move(expr),
std::move(unary_result.expr));
}
return {ParseStatus::Successful, std::move(expr)};
}
ParseResult Paren()
{
// lparen already chewed
ParseResult result = Toplevel();
if (result.status != ParseStatus::Successful)
return result;
if (!Expects(TOK_RPAREN))
{
return {ParseStatus::SyntaxError};
}
return result;
}
ParseResult Toplevel() { return Binary(); }
};
static ParseResult ParseComplexExpression(const std::string& str)
{
Lexer l(str);
std::vector<Token> tokens;
ParseStatus tokenize_status = l.Tokenize(tokens);
if (tokenize_status != ParseStatus::Successful)
return {tokenize_status};
return Parser(std::move(tokens)).Parse();
}
static std::unique_ptr<Expression> ParseBarewordExpression(const std::string& str)
{
ControlQualifier qualifier;
qualifier.control_name = str;
qualifier.has_device = false;
return std::make_unique<ControlExpression>(qualifier);
}
std::pair<ParseStatus, std::unique_ptr<Expression>> ParseExpression(const std::string& str)
{
if (StripSpaces(str).empty())
return std::make_pair(ParseStatus::EmptyExpression, nullptr);
auto bareword_expr = ParseBarewordExpression(str);
ParseResult complex_result = ParseComplexExpression(str);
if (complex_result.status != ParseStatus::Successful)
{
return std::make_pair(complex_result.status, std::move(bareword_expr));
}
auto combined_expr = std::make_unique<CoalesceExpression>(std::move(bareword_expr),
std::move(complex_result.expr));
return std::make_pair(complex_result.status, std::move(combined_expr));
}
} // namespace ciface::ExpressionParser