43 EXPRSYM_Colon, |
43 EXPRSYM_Colon, |
44 }; |
44 }; |
45 |
45 |
46 class Expression final |
46 class Expression final |
47 { |
47 { |
48 public: |
48 public: |
49 using SymbolList = List<ExpressionSymbol*>; |
49 using SymbolList = List<ExpressionSymbol*>; |
50 |
50 |
51 Expression (BotscriptParser* parser, Lexer* lx, DataType reqtype); |
51 Expression (BotscriptParser* parser, Lexer* lx, DataType reqtype); |
52 ~Expression(); |
52 ~Expression(); |
53 ExpressionValue* getResult(); |
53 ExpressionValue* getResult(); |
54 |
54 |
55 private: |
55 private: |
56 BotscriptParser* m_parser; |
56 BotscriptParser* m_parser; |
57 Lexer* m_lexer; |
57 Lexer* m_lexer; |
58 SymbolList m_symbols; |
58 SymbolList m_symbols; |
59 DataType m_type; |
59 DataType m_type; |
60 String m_badTokenText; |
60 String m_badTokenText; |
61 |
61 |
62 ExpressionValue* evaluate(); // Process the expression and yield a result |
62 ExpressionValue* evaluate(); // Process the expression and yield a result |
63 ExpressionSymbol* parseSymbol(); |
63 ExpressionSymbol* parseSymbol(); |
64 String getTokenString(); |
64 String getTokenString(); |
65 void adjustOperators(); |
65 void adjustOperators(); |
66 void verify(); // Ensure the expr is valid |
66 void verify(); // Ensure the expr is valid |
67 void tryVerifyValue (bool* verified, SymbolList::Iterator it); |
67 void tryVerifyValue (bool* verified, SymbolList::Iterator it); |
68 ExpressionValue* evaluateOperator (const ExpressionOperator* op, |
68 ExpressionValue* evaluateOperator (const ExpressionOperator* op, |
69 const List<ExpressionValue*>& values); |
69 const List<ExpressionValue*>& values); |
70 SymbolList::Iterator findPrioritizedOperator(); |
70 SymbolList::Iterator findPrioritizedOperator(); |
71 }; |
71 }; |
72 |
72 |
73 // ============================================================================= |
73 // ============================================================================= |
74 // |
74 // |
75 class ExpressionSymbol |
75 class ExpressionSymbol |
76 { |
76 { |
77 public: |
77 PROPERTY (private, ExpressionSymbolType, type, setType, STOCK_WRITE) |
78 ExpressionSymbol (ExpressionSymbolType type) : |
|
79 m_type (type) {} |
|
80 |
78 |
81 PROPERTY (private, ExpressionSymbolType, type, setType, STOCK_WRITE) |
79 public: |
|
80 ExpressionSymbol (ExpressionSymbolType type) : |
|
81 m_type (type) {} |
82 }; |
82 }; |
83 |
83 |
84 // ============================================================================= |
84 // ============================================================================= |
85 // |
85 // |
86 class ExpressionOperator final : public ExpressionSymbol |
86 class ExpressionOperator final : public ExpressionSymbol |
87 { |
87 { |
88 PROPERTY (public, ExpressionOperatorType, id, setID, STOCK_WRITE) |
88 PROPERTY (public, ExpressionOperatorType, id, setID, STOCK_WRITE) |
89 |
89 |
90 public: |
90 public: |
91 ExpressionOperator (ExpressionOperatorType id); |
91 ExpressionOperator (ExpressionOperatorType id); |
92 }; |
92 }; |
93 |
93 |
94 // ============================================================================= |
94 // ============================================================================= |
95 // |
95 // |
96 class ExpressionValue final : public ExpressionSymbol |
96 class ExpressionValue final : public ExpressionSymbol |
97 { |
97 { |
98 PROPERTY (public, int, value, setValue, STOCK_WRITE) |
98 PROPERTY (public, int, value, setValue, STOCK_WRITE) |
99 PROPERTY (public, DataBuffer*, buffer, setBuffer, STOCK_WRITE) |
99 PROPERTY (public, DataBuffer*, buffer, setBuffer, STOCK_WRITE) |
100 PROPERTY (public, DataType, valueType, setValueType, STOCK_WRITE) |
100 PROPERTY (public, DataType, valueType, setValueType, STOCK_WRITE) |
101 |
101 |
102 public: |
102 public: |
103 ExpressionValue (DataType valuetype); |
103 ExpressionValue (DataType valuetype); |
104 ~ExpressionValue(); |
104 ~ExpressionValue(); |
105 |
105 |
106 void convertToBuffer(); |
106 void convertToBuffer(); |
107 |
107 |
108 inline ExpressionValue* clone() const |
108 inline ExpressionValue* clone() const |
109 { |
109 { |
110 return new ExpressionValue (*this); |
110 return new ExpressionValue (*this); |
111 } |
111 } |
112 |
112 |
113 inline bool isConstexpr() const |
113 inline bool isConstexpr() const |
114 { |
114 { |
115 return buffer() == null; |
115 return buffer() == null; |
116 } |
116 } |
117 }; |
117 }; |
118 |
118 |
119 // ============================================================================= |
119 // ============================================================================= |
120 // |
120 // |
121 // This class represents a ":" in the expression. It serves as the colon for the |
121 // This class represents a ":" in the expression. It serves as the colon for the |
122 // ternary ?: operator. It's not an operand nor is an operator, nor can we just |
122 // ternary ?: operator. It's not an operand nor is an operator, nor can we just |
123 // skip it so it is its own thing here. |
123 // skip it so it is its own thing here. |
124 // |
124 // |
125 class ExpressionColon final : public ExpressionSymbol |
125 class ExpressionColon final : public ExpressionSymbol |
126 { |
126 { |
127 public: |
127 public: |
128 ExpressionColon() : |
128 ExpressionColon() : |
129 ExpressionSymbol (EXPRSYM_Colon) {} |
129 ExpressionSymbol (EXPRSYM_Colon) {} |
130 }; |
130 }; |
131 |
131 |
132 #endif // BOTC_EXPRESSION_H |
132 #endif // BOTC_EXPRESSION_H |