src/expression.cpp

Sun, 20 Jul 2014 17:25:36 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 20 Jul 2014 17:25:36 +0300
changeset 134
eca2fc0acaa2
parent 132
4d45b1383246
child 135
8b9132fea327
permissions
-rw-r--r--

- massive refactoring

119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 #include "expression.h"
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 #include "dataBuffer.h"
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 #include "lexer.h"
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 struct OperatorInfo
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
7 Token token;
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 int priority;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 int numoperands;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 DataHeader header;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 };
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 static const OperatorInfo g_Operators[] =
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
15 {Token::ExclamationMark, 0, 1, DataHeader::NegateLogical, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
16 {Token::Minus, 0, 1, DataHeader::UnaryMinus, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
17 {Token::Multiply, 10, 2, DataHeader::Multiply, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
18 {Token::Divide, 10, 2, DataHeader::Divide, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
19 {Token::Modulus, 10, 2, DataHeader::Modulus, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
20 {Token::Plus, 20, 2, DataHeader::Add, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
21 {Token::Minus, 20, 2, DataHeader::Subtract, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
22 {Token::LeftShift, 30, 2, DataHeader::LeftShift, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
23 {Token::RightShift, 30, 2, DataHeader::RightShift, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
24 {Token::Lesser, 40, 2, DataHeader::LessThan, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
25 {Token::Greater, 40, 2, DataHeader::GreaterThan, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
26 {Token::AtLeast, 40, 2, DataHeader::AtLeast, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
27 {Token::AtMost, 40, 2, DataHeader::AtMost, },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
28 {Token::Equals, 50, 2, DataHeader::Equals },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
29 {Token::NotEquals, 50, 2, DataHeader::NotEquals },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
30 {Token::Amperstand, 60, 2, DataHeader::AndBitwise },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
31 {Token::Caret, 70, 2, DataHeader::EorBitwise },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
32 {Token::Bar, 80, 2, DataHeader::OrBitwise },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
33 {Token::DoubleAmperstand, 90, 2, DataHeader::AndLogical },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
34 {Token::DoubleBar, 100, 2, DataHeader::OrLogical },
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
35 {Token::QuestionMark, 110, 3, DataHeader::NumDataHeaders },
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 };
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
38 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 Expression::Expression (BotscriptParser* parser, Lexer* lx, DataType reqtype) :
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 m_parser (parser),
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 m_lexer (lx),
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 m_type (reqtype)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 ExpressionSymbol* sym;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 while ((sym = parseSymbol()) != null)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 m_symbols << sym;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 // If we were unable to get any expression symbols, something's wonky with
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 // the script. Report an error. mBadTokenText is set to the token that
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 // ParseSymbol ends at when it returns false.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 if (m_symbols.isEmpty())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 error ("unknown identifier '%1'", m_badTokenText);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 adjustOperators();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 verify();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 evaluate();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
61 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 Expression::~Expression()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 for (ExpressionSymbol* sym : m_symbols)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 delete sym;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
69 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 //
132
4d45b1383246 - corrected more instances of "OPER_erand" and stuff like that
Teemu Piippo <crimsondusk64@gmail.com>
parents: 131
diff changeset
71 // Try to parse an expression symbol (i.e. an operator or operand or a colon)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 // from the lexer.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 ExpressionSymbol* Expression::parseSymbol()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 int pos = m_lexer->position();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 ExpressionValue* op = null;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
79 if (m_lexer->next (Token::Colon))
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 return new ExpressionColon;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81
131
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
82 // Check for operator
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 for (const OperatorInfo& op : g_Operators)
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
84 {
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 if (m_lexer->next (op.token))
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 return new ExpressionOperator ((ExpressionOperatorType) (&op - &g_Operators[0]));
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
87 }
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 // Check sub-expression
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
90 if (m_lexer->next (Token::ParenStart))
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 Expression expr (m_parser, m_lexer, m_type);
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
93 m_lexer->mustGetNext (Token::ParenEnd);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 return expr.getResult()->clone();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 op = new ExpressionValue (m_type);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 // Check function
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 if (CommandInfo* comm = findCommandByName (m_lexer->peekNextString()))
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 m_lexer->skip();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
104 if (m_type != TYPE_Unknown and comm->returnvalue != m_type)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 error ("%1 returns an incompatible data type", comm->name);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 op->setBuffer (m_parser->parseCommand (comm));
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 return op;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 // Check for variables
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
112 if (m_lexer->next (Token::DollarSign))
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
114 m_lexer->mustGetNext (Token::Symbol);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 Variable* var = m_parser->findVariable (getTokenString());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 if (var == null)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 error ("unknown variable %1", getTokenString());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 if (var->type != m_type)
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
121 {
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 error ("expression requires %1, variable $%2 is of type %3",
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 dataTypeName (m_type), var->name, dataTypeName (var->type));
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
124 }
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 if (var->isarray)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
128 m_lexer->mustGetNext (Token::BracketStart);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 Expression expr (m_parser, m_lexer, TYPE_Int);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 expr.getResult()->convertToBuffer();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 DataBuffer* buf = expr.getResult()->buffer()->clone();
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
132 buf->writeDWord (DataHeader::PushGlobalArray);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 buf->writeDWord (var->index);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 op->setBuffer (buf);
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
135 m_lexer->mustGetNext (Token::BracketEnd);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 elif (var->writelevel == WRITE_Constexpr)
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
138 {
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 op->setValue (var->value);
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
140 }
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 else
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 DataBuffer* buf = new DataBuffer (8);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
145 if (var->isGlobal())
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
146 buf->writeDWord (DataHeader::PushGlobalVar);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 else
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
148 buf->writeDWord (DataHeader::PushLocalVar);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 buf->writeDWord (var->index);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 op->setBuffer (buf);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 return op;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 // Check for literal
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 switch (m_type)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 case TYPE_Void:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 case TYPE_Unknown:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
163 error ("unknown identifier `%1` (expected keyword, function or variable)",
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
164 getTokenString());
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 case TYPE_Bool:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
170 if (m_lexer->next (Token::True) or m_lexer->next (Token::False))
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
172 Token tt = m_lexer->tokenType();
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
173 op->setValue (tt == Token::True ? 1 : 0);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 return op;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 case TYPE_Int:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
180 if (m_lexer->next (Token::Number))
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 op->setValue (getTokenString().toLong());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 return op;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 case TYPE_String:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
189 if (m_lexer->next (Token::String))
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 op->setValue (getStringTableIndex (getTokenString()));
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 return op;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197 m_badTokenText = m_lexer->token()->text;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 m_lexer->setPosition (pos);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 delete op;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 return null;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
203 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 //
131
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
205 // The symbol parsing process only does token-based checking for operators.
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
206 // Thus ALL minus operators are actually unary minuses simply because both
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
207 // have Token::Minus as their token and the unary minus is prior to the binary minus
131
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
208 // in the operator table. Now that we have all symbols present, we can
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 // correct cases like this.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 void Expression::adjustOperators()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 for (auto it = m_symbols.begin() + 1; it != m_symbols.end(); ++it)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 if ((*it)->type() != EXPRSYM_Operator)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216 continue;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 ExpressionOperator* op = static_cast<ExpressionOperator*> (*it);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
220 // Unary minus with a value as the previous symbol cannot really be
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221 // unary; replace with binary minus.
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
222 if (op->id() == OPER_UnaryMinus and (*(it - 1))->type() == EXPRSYM_Value)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
223 op->setID (OPER_Subtraction);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
225 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
227 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229 // Verifies a single value. Helper function for Expression::verify.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231 void Expression::tryVerifyValue (bool* verified, SymbolList::Iterator it)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 {
131
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
233 // If it's an unary operator we skip to its value. The actual operator will
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
234 // be verified separately.
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
235 if ((*it)->type() == EXPRSYM_Operator and
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 g_Operators[static_cast<ExpressionOperator*> (*it)->id()].numoperands == 1)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238 ++it;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 int i = it - m_symbols.begin();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243 // Ensure it's an actual value
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 if ((*it)->type() != EXPRSYM_Value)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 error ("malformed expression (symbol #%1 is not a value)", i);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
246
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 verified[i] = true;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
250 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
252 // Ensures the expression is valid and well-formed and not OMGWTFBBQ. Throws an
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
253 // error if this is not the case.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
254 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
255 void Expression::verify()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
256 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
257 if (m_symbols.size() == 1)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
258 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
259 if (m_symbols[0]->type() != EXPRSYM_Value)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
260 error ("bad expression");
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
261
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
262 return;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
263 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
264
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
265 if (m_type == TYPE_String)
132
4d45b1383246 - corrected more instances of "OPER_erand" and stuff like that
Teemu Piippo <crimsondusk64@gmail.com>
parents: 131
diff changeset
266 error ("Cannot perform operations on strings");
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
267
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
268 bool* verified = new bool[m_symbols.size()];
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
269 memset (verified, 0, m_symbols.size() * sizeof (decltype (*verified)));
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
270 const auto last = m_symbols.end() - 1;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
271 const auto first = m_symbols.begin();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
272
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
273 for (auto it = m_symbols.begin(); it != m_symbols.end(); ++it)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
274 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
275 int i = (it - first);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
276
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
277 if ((*it)->type() != EXPRSYM_Operator)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
278 continue;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
279
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
280 ExpressionOperator* op = static_cast<ExpressionOperator*> (*it);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
281 int numoperands = g_Operators[op->id()].numoperands;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
282
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
283 switch (numoperands)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
284 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
285 case 1:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
286 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
287 // Ensure that:
131
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
288 // - unary operator is not the last symbol
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
289 // - unary operator is succeeded by a value symbol
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
290 // - neither symbol overlaps with something already verified
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
291 tryVerifyValue (verified, it + 1);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
292
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
293 if (it == last or verified[i] == true)
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
294 error ("ill-formed expression");
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
295
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
296 verified[i] = true;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
297 break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
298 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
299
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
300 case 2:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
301 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
302 // Ensure that:
131
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
303 // - binary operator is not the first or last symbol
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
304 // - is preceded and succeeded by values
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
305 // - none of the three tokens are already verified
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
306 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
307 // Basically similar logic as above.
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
308 if (it == first or it == last or verified[i] == true)
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
309 error ("ill-formed expression");
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
310
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
311 tryVerifyValue (verified, it + 1);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
312 tryVerifyValue (verified, it - 1);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
313 verified[i] = true;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
314 break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
315 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
316
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
317 case 3:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
318 {
131
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
319 // Ternary operator case. This goes a bit nuts.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
320 // This time we have the following:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
321 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
322 // (VALUE) ? (VALUE) : (VALUE)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
323 // ^
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
324 // --------/ we are here
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
325 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
326 // Check that the:
131
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
327 // - questionmark operator is not misplaced (first or last)
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
328 // - the value behind the operator (-1) is valid
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
329 // - the value after the operator (+1) is valid
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
330 // - the value after the colon (+3) is valid
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
331 // - none of the five tokens are verified
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
332 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
333 tryVerifyValue (verified, it - 1);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
334 tryVerifyValue (verified, it + 1);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
335 tryVerifyValue (verified, it + 3);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
336
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
337 if (it == first
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
338 or it >= m_symbols.end() - 3
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
339 or verified[i] == true
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
340 or verified[i + 2] == true
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
341 or (*(it + 2))->type() != EXPRSYM_Colon)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
342 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
343 error ("ill-formed expression");
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
344 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
345
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
346 verified[i] = true;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
347 verified[i + 2] = true;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
348 break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
349 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
350
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
351 default:
132
4d45b1383246 - corrected more instances of "OPER_erand" and stuff like that
Teemu Piippo <crimsondusk64@gmail.com>
parents: 131
diff changeset
352 error ("WTF operator with %1 operands", numoperands);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
353 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
354 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
355
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
356 for (int i = 0; i < m_symbols.size(); ++i)
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
357 {
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
358 if (verified[i] == false)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
359 error ("malformed expression: expr symbol #%1 is was left unverified", i);
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
360 }
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
361
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
362 delete verified;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
363 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
364
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
365
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
366 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
367 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
368 // Which operator to evaluate?
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
369 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
370 Expression::SymbolList::Iterator Expression::findPrioritizedOperator()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
371 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
372 SymbolList::Iterator best = m_symbols.end();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
373 int bestpriority = __INT_MAX__;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
374
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
375 for (SymbolList::Iterator it = m_symbols.begin(); it != m_symbols.end(); ++it)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
376 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
377 if ((*it)->type() != EXPRSYM_Operator)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
378 continue;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
379
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
380 ExpressionOperator* op = static_cast<ExpressionOperator*> (*it);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
381 const OperatorInfo* info = &g_Operators[op->id()];
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
382
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
383 if (info->priority < bestpriority)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
384 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
385 best = it;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
386 bestpriority = info->priority;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
387 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
388 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
389
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
390 return best;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
391 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
392
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
393 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
394 //
131
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
395 // Process the given operator and values into a new value.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
396 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
397 ExpressionValue* Expression::evaluateOperator (const ExpressionOperator* op,
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
398 const List<ExpressionValue*>& values)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
399 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
400 const OperatorInfo* info = &g_Operators[op->id()];
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
401 bool isconstexpr = true;
125
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
402 ASSERT_EQ (values.size(), info->numoperands)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
403
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
404 // See whether the values are constexpr
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
405 for (ExpressionValue* val : values)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
406 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
407 if (not val->isConstexpr())
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
408 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
409 isconstexpr = false;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
410 break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
411 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
412 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
413
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
414 // If not all of the values are constexpr, none of them shall be.
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
415 if (not isconstexpr)
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
416 {
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
417 for (ExpressionValue* val : values)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
418 val->convertToBuffer();
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
419 }
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
420
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
421 ExpressionValue* newval = new ExpressionValue (m_type);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
422
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
423 if (isconstexpr == false)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
424 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
425 // This is not a constant expression so we'll have to use databuffers
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
426 // to convey the expression to bytecode. Actual value cannot be evaluated
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
427 // until Zandronum processes it at run-time.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
428 newval->setBuffer (new DataBuffer);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
429
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
430 if (op->id() == OPER_Ternary)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
431 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
432 // There isn't a dataheader for ternary operator. Instead, we use DataHeader::IfNotGoto
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
433 // to create an "if-block" inside an expression. Behold, big block of writing madness!
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
434 DataBuffer* buf = newval->buffer();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
435 DataBuffer* b0 = values[0]->buffer();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
436 DataBuffer* b1 = values[1]->buffer();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
437 DataBuffer* b2 = values[2]->buffer();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
438 ByteMark* mark1 = buf->addMark (""); // start of "else" case
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
439 ByteMark* mark2 = buf->addMark (""); // end of expression
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
440 buf->mergeAndDestroy (b0);
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
441 buf->writeDWord (DataHeader::IfNotGoto); // if the first operand (condition)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
442 buf->addReference (mark1); // didn't eval true, jump into mark1
132
4d45b1383246 - corrected more instances of "OPER_erand" and stuff like that
Teemu Piippo <crimsondusk64@gmail.com>
parents: 131
diff changeset
443 buf->mergeAndDestroy (b1); // otherwise, perform second operand (true case)
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
444 buf->writeDWord (DataHeader::Goto); // afterwards, jump to the end, which is
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
445 buf->addReference (mark2); // marked by mark2.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
446 buf->adjustMark (mark1); // move mark1 at the end of the true case
132
4d45b1383246 - corrected more instances of "OPER_erand" and stuff like that
Teemu Piippo <crimsondusk64@gmail.com>
parents: 131
diff changeset
447 buf->mergeAndDestroy (b2); // perform third operand (false case)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
448 buf->adjustMark (mark2); // move the ending mark2 here
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
449
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
450 for (int i = 0; i < 3; ++i)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
451 values[i]->setBuffer (null);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
452 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
453 else
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
454 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
455 ASSERT_NE (info->header, DataHeader::NumDataHeaders);
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
456
131
8ae8038b0168 - OPER_erator -> operator. bleh.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
457 // Generic case: write all arguments and apply the operator's
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
458 // data header.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
459 for (ExpressionValue* val : values)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
460 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
461 newval->buffer()->mergeAndDestroy (val->buffer());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
462
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
463 // Null the pointer out so that the value's destructor will not
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
464 // attempt to double-free it.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
465 val->setBuffer (null);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
466 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
467
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
468 newval->buffer()->writeDWord (info->header);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
469 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
470 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
471 else
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
472 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
473 // We have a constant expression. We know all the values involved and
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
474 // can thus compute the result of this expression on compile-time.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
475 List<int> nums;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
476 int a;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
477
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
478 for (ExpressionValue* val : values)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
479 nums << val->value();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
480
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
481 switch (op->id())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
482 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
483 case OPER_Addition: a = nums[0] + nums[1]; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
484 case OPER_Subtraction: a = nums[0] - nums[1]; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
485 case OPER_Multiplication: a = nums[0] * nums[1]; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
486 case OPER_UnaryMinus: a = -nums[0]; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
487 case OPER_NegateLogical: a = !nums[0]; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
488 case OPER_LeftShift: a = nums[0] << nums[1]; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
489 case OPER_RightShift: a = nums[0] >> nums[1]; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
490 case OPER_CompareLesser: a = (nums[0] < nums[1]) ? 1 : 0; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
491 case OPER_CompareGreater: a = (nums[0] > nums[1]) ? 1 : 0; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
492 case OPER_CompareAtLeast: a = (nums[0] <= nums[1]) ? 1 : 0; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
493 case OPER_CompareAtMost: a = (nums[0] >= nums[1]) ? 1 : 0; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
494 case OPER_CompareEquals: a = (nums[0] == nums[1]) ? 1 : 0; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
495 case OPER_CompareNotEquals: a = (nums[0] != nums[1]) ? 1 : 0; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
496 case OPER_BitwiseAnd: a = nums[0] & nums[1]; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
497 case OPER_BitwiseOr: a = nums[0] | nums[1]; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
498 case OPER_BitwiseXOr: a = nums[0] ^ nums[1]; break;
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
499 case OPER_LogicalAnd: a = (nums[0] and nums[1]) ? 1 : 0; break;
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
500 case OPER_LogicalOr: a = (nums[0] or nums[1]) ? 1 : 0; break;
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
501 case OPER_Ternary: a = (nums[0] != 0) ? nums[1] : nums[2]; break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
502
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
503 case OPER_Division:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
504 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
505 if (nums[1] == 0)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
506 error ("division by zero in constant expression");
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
507
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
508 a = nums[0] / nums[1];
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
509 break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
510 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
511
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
512 case OPER_Modulus:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
513 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
514 if (nums[1] == 0)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
515 error ("modulus by zero in constant expression");
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
516
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
517 a = nums[0] % nums[1];
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
518 break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
519 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
520 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
521
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
522 newval->setValue (a);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
523 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
524
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
525 // The new value has been generated. We don't need the old stuff anymore.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
526 for (ExpressionValue* val : values)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
527 delete val;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
528
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
529 delete op;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
530 return newval;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
531 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
532
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
533 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
534 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
535 ExpressionValue* Expression::evaluate()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
536 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
537 SymbolList::Iterator it;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
538
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
539 while ((it = findPrioritizedOperator()) != m_symbols.end())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
540 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
541 int i = it - m_symbols.begin();
132
4d45b1383246 - corrected more instances of "OPER_erand" and stuff like that
Teemu Piippo <crimsondusk64@gmail.com>
parents: 131
diff changeset
542 List<SymbolList::Iterator> operands;
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
543 ExpressionOperator* op = static_cast<ExpressionOperator*> (*it);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
544 const OperatorInfo* info = &g_Operators[op->id()];
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
545 int lower, upper; // Boundaries of area to replace
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
546
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
547 switch (info->numoperands)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
548 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
549 case 1:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
550 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
551 lower = i;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
552 upper = i + 1;
132
4d45b1383246 - corrected more instances of "OPER_erand" and stuff like that
Teemu Piippo <crimsondusk64@gmail.com>
parents: 131
diff changeset
553 operands << it + 1;
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
554 break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
555 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
556
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
557 case 2:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
558 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
559 lower = i - 1;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
560 upper = i + 1;
132
4d45b1383246 - corrected more instances of "OPER_erand" and stuff like that
Teemu Piippo <crimsondusk64@gmail.com>
parents: 131
diff changeset
561 operands << it - 1
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
562 << it + 1;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
563 break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
564 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
565
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
566 case 3:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
567 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
568 lower = i - 1;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
569 upper = i + 3;
132
4d45b1383246 - corrected more instances of "OPER_erand" and stuff like that
Teemu Piippo <crimsondusk64@gmail.com>
parents: 131
diff changeset
570 operands << it - 1
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
571 << it + 1
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
572 << it + 3;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
573 break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
574 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
575
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
576 default:
125
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
577 error ("WTF bad expression with %1 operands", info->numoperands);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
578 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
579
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
580 List<ExpressionValue*> values;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
581
132
4d45b1383246 - corrected more instances of "OPER_erand" and stuff like that
Teemu Piippo <crimsondusk64@gmail.com>
parents: 131
diff changeset
582 for (auto it : operands)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
583 values << static_cast<ExpressionValue*> (*it);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
584
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
585 // Note: @op and all of @values are invalid after this call.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
586 ExpressionValue* newvalue = evaluateOperator (op, values);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
587
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
588 for (int i = upper; i >= lower; --i)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
589 m_symbols.removeAt (i);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
590
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
591 m_symbols.insert (lower, newvalue);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
592 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
593
125
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
594 ASSERT_EQ (m_symbols.size(), 1)
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
595 ASSERT_EQ (m_symbols.first()->type(), EXPRSYM_Value)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
596 ExpressionValue* val = static_cast<ExpressionValue*> (m_symbols.first());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
597 return val;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
598 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
599
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
600 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
601 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
602 ExpressionValue* Expression::getResult()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
603 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
604 return static_cast<ExpressionValue*> (m_symbols.first());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
605 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
606
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
607 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
608 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
609 String Expression::getTokenString()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
610 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
611 return m_lexer->token()->text;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
612 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
613
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
614 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
615 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
616 ExpressionOperator::ExpressionOperator (ExpressionOperatorType id) :
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
617 ExpressionSymbol (EXPRSYM_Operator),
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
618 m_id (id) {}
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
619
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
620 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
621 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
622 ExpressionValue::ExpressionValue (DataType valuetype) :
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
623 ExpressionSymbol (EXPRSYM_Value),
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
624 m_buffer (null),
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
625 m_valueType (valuetype) {}
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
626
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
627 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
628 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
629 ExpressionValue::~ExpressionValue()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
630 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
631 delete m_buffer;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
632 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
633
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
634 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
635 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
636 void ExpressionValue::convertToBuffer()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
637 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
638 if (isConstexpr() == false)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
639 return;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
640
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
641 setBuffer (new DataBuffer);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
642
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
643 switch (m_valueType)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
644 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
645 case TYPE_Bool:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
646 case TYPE_Int:
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
647 buffer()->writeDWord (DataHeader::PushNumber);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
648 buffer()->writeDWord (abs (value()));
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
649
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
650 if (value() < 0)
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
651 buffer()->writeDWord (DataHeader::UnaryMinus);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
652 break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
653
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
654 case TYPE_String:
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
655 buffer()->writeDWord (DataHeader::PushStringIndex);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
656 buffer()->writeDWord (value());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
657 break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
658
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
659 case TYPE_Void:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
660 case TYPE_Unknown:
125
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
661 error ("WTF: tried to convert bad expression value type %1 to buffer", m_valueType);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
662 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
663 }

mercurial