Qore Programming Language 1.19.2
Loading...
Searching...
No Matches
QoreParseListNode.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 QoreParseListNode.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30*/
31
32#ifndef _QORE_QOREPARSELISTNODE_H
33
34#define _QORE_QOREPARSELISTNODE_H
35
36#include <qore/Qore.h>
37
38#include <deque>
39#include <string>
40
41DLLLOCAL QoreValue copy_value_and_resolve_lvar_refs(const QoreValue& n, ExceptionSink* xsink);
42
43class QoreParseListNode : public ParseNode {
44public:
45 typedef std::deque<QoreValue> nvec_t;
46
47 DLLLOCAL QoreParseListNode(const QoreProgramLocation* loc) : ParseNode(loc, NT_PARSE_LIST, true) {
48 }
49
50 DLLLOCAL QoreParseListNode(const QoreParseListNode& old, ExceptionSink* xsink) : ParseNode(old),
51 vtype(old.vtype), typeInfo(old.typeInfo), finalized(old.finalized), vlist(old.vlist) {
52 for (unsigned i = 0; i < old.values.size(); ++i) {
53 add(copy_value_and_resolve_lvar_refs(old.values[i], xsink), old.lvec[i]);
54 if (*xsink)
55 return;
56 }
57 }
58
59 DLLLOCAL ~QoreParseListNode() {
60 for (auto& i : values)
61 i.discard(nullptr);
62 }
63
64 DLLLOCAL void add(QoreValue v, const QoreProgramLocation* loc) {
65 values.push_back(v);
66 lvec.push_back(loc);
67
68 if (!size()) {
69 if (v.hasNode() && v.hasEffect()) {
70 set_effect(true);
71 }
72 } else if (has_effect() && !v.hasEffect()) {
73 set_effect(false);
74 }
75 }
76
77 DLLLOCAL QoreValue shift() {
78 if (!values.size()) {
79 return QoreValue();
80 }
81 assert(vtypes.empty());
82 QoreValue rv = values[0];
83 values.pop_front();
84 lvec.pop_front();
85 return rv;
86 }
87
88 DLLLOCAL QoreValue pop() {
89 if (!values.size()) {
90 return QoreValue();
91 }
92 assert(vtypes.empty());
93 QoreValue rv = values.back();
94 values.pop_back();
95 lvec.pop_back();
96 return rv;
97 }
98
99 DLLLOCAL QoreValue get(size_t i) {
100 assert(i < values.size());
101 return values[i];
102 }
103
104 DLLLOCAL const QoreValue get(size_t i) const {
105 assert(i < values.size());
106 return values[i];
107 }
108
109 DLLLOCAL QoreValue& getReference(size_t i) {
110 assert(i < values.size());
111 return values[i];
112 }
113
114 DLLLOCAL const QoreProgramLocation* getLocation(size_t i) const {
115 assert(i < lvec.size());
116 return lvec[i];
117 }
118
119 DLLLOCAL QoreValue swap(size_t i, QoreValue v) {
120 QoreValue rv = values[i];
121 values[i] = v;
122 return rv;
123 }
124
125 DLLLOCAL size_t size() const {
126 return values.size();
127 }
128
129 DLLLOCAL bool empty() const {
130 return values.empty();
131 }
132
133 DLLLOCAL void setFinalized() {
134 if (!finalized)
135 finalized = true;
136 }
137
138 DLLLOCAL bool isFinalized() const {
139 return finalized;
140 }
141
142 DLLLOCAL bool isVariableList() const {
143 return vlist;
144 }
145
146 DLLLOCAL void setVariableList() {
147 assert(!vlist);
148 vlist = true;
149 }
150
151 DLLLOCAL void finalizeBlock(int start_line, int end_line);
152
153 DLLLOCAL nvec_t& getValues() {
154 return values;
155 }
156
157 DLLLOCAL const nvec_t& getValues() const {
158 return values;
159 }
160
161 DLLLOCAL const type_vec_t& getValueTypes() const {
162 return vtypes;
163 }
164
165 DLLLOCAL QoreParseListNode* listRefSelf() const {
166 ref();
167 return const_cast<QoreParseListNode*>(this);
168 }
169
170 DLLLOCAL int initArgs(QoreParseContext& parse_context, type_vec_t& arg_types, QoreListNode*& args);
171
172 DLLLOCAL virtual int getAsString(QoreString& str, int foff, ExceptionSink* xsink) const;
173
174 DLLLOCAL virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const;
175
176 DLLLOCAL virtual const char* getTypeName() const {
177 return "list";
178 }
179
180 DLLLOCAL QoreParseListNode* copyBlank() const {
181 QoreParseListNode* n = new QoreParseListNode(loc);
182 n->vtype = vtype;
183
184 n->finalized = finalized;
185 n->vlist = vlist;
186 return n;
187 }
188
189 DLLLOCAL int parseInitIntern(bool& needs_eval, QoreParseContext& parse_context);
190
191protected:
192 typedef std::deque<const QoreProgramLocation*> lvec_t;
193 nvec_t values;
194 type_vec_t vtypes;
195 lvec_t lvec;
196 // common value type, if any
197 const QoreTypeInfo* vtype = nullptr;
198 // node type info (derivative of list)
199 const QoreTypeInfo* typeInfo;
200 // flag for a list expression in curly brackets for the list version of the map operator
201 bool finalized = false;
202 // is this a variable list declaration?
203 bool vlist = false;
204
205 DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
206 return typeInfo;
207 }
208
209 DLLLOCAL virtual int parseInitImpl(QoreValue& val, QoreParseContext& parse_context);
210
211 DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
212};
213
214#endif
DLLLOCAL bool needs_eval() const
returns true if the object needs evaluation to return a value, false if not
Definition: AbstractQoreNode.h:145
DLLEXPORT void ref() const
increments the reference count
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLEXPORT bool hasEffect() const
return true if the value needs evaluation and has a side effect
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
std::vector< const QoreTypeInfo * > type_vec_t
vector of type information for parameter lists
Definition: common.h:251
const qore_type_t NT_PARSE_LIST
type value for QoreParseListNode
Definition: node_types.h:84
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:276
DLLEXPORT bool hasNode() const
returns true if the object contains a non-null AbstractQoreNode pointer (ie type == QV_Node && v....