diff --git a/LICENSE b/LICENSE index c6f2b345..7857d8d8 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ BSD 3-Clause License -Copyright (c) 2017, 2022 Alliance for Sustainable Energy, LLC +Copyright (c) 2017, 2022 Alliance for Energy Innovation, LLC All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/doc/html/absyn_8h_source.html b/doc/html/absyn_8h_source.html index b563ee88..9ccba6f4 100644 --- a/doc/html/absyn_8h_source.html +++ b/doc/html/absyn_8h_source.html @@ -89,7 +89,7 @@
absyn.h
-
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Sustainable Energy, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_absyn_h
26 #define __lk_absyn_h
27 
28 #include <vector>
29 
30 #include <unordered_map>
31 using std::unordered_map;
32 
33 #ifdef _MSC_VER
34 #pragma warning(disable: 4290) // ignore warning: 'C++ exception specification ignored except to indicate a function is not __declspec(nothrow)'
35 #endif
36 
37 #if defined(LK_USE_WXWIDGETS)
38 #include <wx/string.h>
39 #include <wx/hashmap.h>
40 #include <wx/intl.h>
41 
42 #if wxCHECK_VERSION(2,9,0)
43 typedef wxUniChar lk_char;
44 #define LK_UNICODE 1
45 #else
46 typedef wxChar lk_char;
47 #endif
48 
49 typedef wxString lk_string;
50 typedef wxStringHash lk_string_hash;
51 typedef wxStringEqual lk_string_equal;
52 
53 #else
54 #include <string>
55 
56 typedef std::string::value_type lk_char;
57 typedef std::string lk_string;
58 
59 typedef std::hash<std::string> lk_string_hash;
60 typedef std::equal_to<std::string> lk_string_equal;
61 
62 #endif
63 
64 #define lk_tr(s) lk::get_translation(s)
65 
66 namespace lk
67 {
68  lk_string get_translation(const lk_string &);
69  void set_translation_function(lk_string(*f)(const lk_string&));
70 
71  lk_char lower_char(lk_char c);
72  lk_char upper_char(lk_char c);
73  bool convert_integer(const lk_string &str, int *x);
74  bool convert_double(const lk_string &str, double *x);
75  std::string to_utf8(const lk_string &str);
76  lk_string from_utf8(const std::string &str);
77  lk_string from_utf8(const char *str);
78  lk_string to_string(lk_char c);
79 
80  extern int _node_alloc;
81 
82  class attr_t
83  {
84  public:
85  virtual ~attr_t() { };
86  };
87 
96  class srcpos_t
97  {
98  public:
99  static const srcpos_t npos;
100 
101  srcpos_t() { line = 0; stmt = 0; stmt_end = 0; }
102  srcpos_t(const lk_string &f, int l, int s, int e = 0) : file(f), line(l), stmt(s), stmt_end(e) { }
103  lk_string file;
104  int line, stmt, stmt_end;
105  };
106 
107  bool operator==(const srcpos_t &, const srcpos_t &);
108 
118  class node_t
119  {
120  private:
121  srcpos_t m_srcpos;
122  public:
123  attr_t *attr;
124  node_t(srcpos_t pos) : m_srcpos(pos), attr(0) { _node_alloc++; }
125  virtual ~node_t() { _node_alloc--; if (attr) delete attr; }
126  inline int line() { return m_srcpos.line; }
127  inline lk_string file() { return m_srcpos.file; }
128  inline srcpos_t srcpos() { return m_srcpos; }
129  };
130 
131  class list_t : public node_t
132  {
133  public:
134  std::vector<node_t*> items;
135  list_t(srcpos_t pos) : node_t(pos) { }
136  virtual ~list_t() { for (size_t i = 0; i < items.size(); i++) delete items[i]; }
137  };
138 
139  class iter_t : public node_t
140  {
141  public:
142  node_t *init, *test, *adv, *block;
143  iter_t(srcpos_t pos, node_t *i, node_t *t, node_t *a, node_t *b) : node_t(pos), init(i), test(t), adv(a), block(b) { }
144  virtual ~iter_t() { if (init) delete init; if (test) delete test; if (adv) delete adv; if (block) delete block; }
145  };
146 
147  class cond_t : public node_t
148  {
149  public:
150  node_t *test, *on_true, *on_false;
151  bool ternary;
152  cond_t(srcpos_t pos, node_t *t, node_t *ot, node_t *of, bool ter) : node_t(pos), test(t), on_true(ot), on_false(of), ternary(ter) { }
153  virtual ~cond_t() { if (test) delete test; if (on_true) delete on_true; if (on_false) delete on_false; }
154  };
155 
156  class expr_t : public node_t
157  {
158  public:
159  enum {
160  INVALID,
161 
162  PLUS,
163  MINUS,
164  MULT,
165  DIV,
166  INCR,
167  DECR,
168  DEFINE,
169  ASSIGN,
170  LOGIOR,
171  LOGIAND,
172  NOT,
173  EQ,
174  NE,
175  LT,
176  LE,
177  GT,
178  GE,
179  EXP,
180  NEG,
181  INDEX,
182  HASH,
183  CALL,
184  THISCALL,
185  SIZEOF,
186  KEYSOF,
187  TYPEOF,
188  INITVEC,
189  INITHASH,
190  SWITCH,
191  PLUSEQ,
192  MINUSEQ,
193  MULTEQ,
194  DIVEQ,
195  MINUSAT,
196  WHEREAT
197  };
198  int oper;
199  node_t *left, *right;
200  const char *operstr();
201  expr_t(srcpos_t pos, int op, node_t *l, node_t *r) : node_t(pos), oper(op), left(l), right(r) { }
202  virtual ~expr_t() { if (left) delete left; if (right) delete right; }
203  };
204 
205  class iden_t : public node_t
206  {
207  public:
208  lk_string name;
209  bool constval;
210  bool globalval;
211  bool special;
212  iden_t(srcpos_t pos, const lk_string &n, bool cons, bool glob, bool speci) : node_t(pos), name(n), constval(cons), globalval(glob), special(speci) { }
213  virtual ~iden_t() { }
214  };
215 
216  class ctlstmt_t : public node_t
217  {
218  public:
219  enum {
220  INVALID,
221 
222  RETURN,
223  EXIT,
224  BREAK,
225  CONTINUE
226  };
227 
228  const char *ctlstr();
229 
230  int ictl;
231  node_t *rexpr;
232  ctlstmt_t(srcpos_t pos, int ctl, node_t *ex = 0) : node_t(pos), ictl(ctl), rexpr(ex) { }
233  virtual ~ctlstmt_t() { if (rexpr) delete rexpr; }
234  };
235 
236  class constant_t : public node_t
237  {
238  public:
239  double value;
240  constant_t(srcpos_t pos, double v) : node_t(pos), value(v) { }
241  virtual ~constant_t() { }
242  };
243 
244  class literal_t : public node_t
245  {
246  public:
247  lk_string value;
248  literal_t(srcpos_t pos, const lk_string &s) : node_t(pos), value(s) { }
249  virtual ~literal_t() { }
250  };
251 
252  class null_t : public node_t
253  {
254  public:
255  null_t(srcpos_t pos) : node_t(pos) { }
256  virtual ~null_t() { }
257  };
258 
259  void pretty_print(lk_string &str, node_t *root, int level);
260 };
261 
262 #endif
Forms nodes of recursive descent tree.
Definition: absyn.h:118
+
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Energy Innovation, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_absyn_h
26 #define __lk_absyn_h
27 
28 #include <vector>
29 
30 #include <unordered_map>
31 using std::unordered_map;
32 
33 #ifdef _MSC_VER
34 #pragma warning(disable: 4290) // ignore warning: 'C++ exception specification ignored except to indicate a function is not __declspec(nothrow)'
35 #endif
36 
37 #if defined(LK_USE_WXWIDGETS)
38 #include <wx/string.h>
39 #include <wx/hashmap.h>
40 #include <wx/intl.h>
41 
42 #if wxCHECK_VERSION(2,9,0)
43 typedef wxUniChar lk_char;
44 #define LK_UNICODE 1
45 #else
46 typedef wxChar lk_char;
47 #endif
48 
49 typedef wxString lk_string;
50 typedef wxStringHash lk_string_hash;
51 typedef wxStringEqual lk_string_equal;
52 
53 #else
54 #include <string>
55 
56 typedef std::string::value_type lk_char;
57 typedef std::string lk_string;
58 
59 typedef std::hash<std::string> lk_string_hash;
60 typedef std::equal_to<std::string> lk_string_equal;
61 
62 #endif
63 
64 #define lk_tr(s) lk::get_translation(s)
65 
66 namespace lk
67 {
68  lk_string get_translation(const lk_string &);
69  void set_translation_function(lk_string(*f)(const lk_string&));
70 
71  lk_char lower_char(lk_char c);
72  lk_char upper_char(lk_char c);
73  bool convert_integer(const lk_string &str, int *x);
74  bool convert_double(const lk_string &str, double *x);
75  std::string to_utf8(const lk_string &str);
76  lk_string from_utf8(const std::string &str);
77  lk_string from_utf8(const char *str);
78  lk_string to_string(lk_char c);
79 
80  extern int _node_alloc;
81 
82  class attr_t
83  {
84  public:
85  virtual ~attr_t() { };
86  };
87 
96  class srcpos_t
97  {
98  public:
99  static const srcpos_t npos;
100 
101  srcpos_t() { line = 0; stmt = 0; stmt_end = 0; }
102  srcpos_t(const lk_string &f, int l, int s, int e = 0) : file(f), line(l), stmt(s), stmt_end(e) { }
103  lk_string file;
104  int line, stmt, stmt_end;
105  };
106 
107  bool operator==(const srcpos_t &, const srcpos_t &);
108 
118  class node_t
119  {
120  private:
121  srcpos_t m_srcpos;
122  public:
123  attr_t *attr;
124  node_t(srcpos_t pos) : m_srcpos(pos), attr(0) { _node_alloc++; }
125  virtual ~node_t() { _node_alloc--; if (attr) delete attr; }
126  inline int line() { return m_srcpos.line; }
127  inline lk_string file() { return m_srcpos.file; }
128  inline srcpos_t srcpos() { return m_srcpos; }
129  };
130 
131  class list_t : public node_t
132  {
133  public:
134  std::vector<node_t*> items;
135  list_t(srcpos_t pos) : node_t(pos) { }
136  virtual ~list_t() { for (size_t i = 0; i < items.size(); i++) delete items[i]; }
137  };
138 
139  class iter_t : public node_t
140  {
141  public:
142  node_t *init, *test, *adv, *block;
143  iter_t(srcpos_t pos, node_t *i, node_t *t, node_t *a, node_t *b) : node_t(pos), init(i), test(t), adv(a), block(b) { }
144  virtual ~iter_t() { if (init) delete init; if (test) delete test; if (adv) delete adv; if (block) delete block; }
145  };
146 
147  class cond_t : public node_t
148  {
149  public:
150  node_t *test, *on_true, *on_false;
151  bool ternary;
152  cond_t(srcpos_t pos, node_t *t, node_t *ot, node_t *of, bool ter) : node_t(pos), test(t), on_true(ot), on_false(of), ternary(ter) { }
153  virtual ~cond_t() { if (test) delete test; if (on_true) delete on_true; if (on_false) delete on_false; }
154  };
155 
156  class expr_t : public node_t
157  {
158  public:
159  enum {
160  INVALID,
161 
162  PLUS,
163  MINUS,
164  MULT,
165  DIV,
166  INCR,
167  DECR,
168  DEFINE,
169  ASSIGN,
170  LOGIOR,
171  LOGIAND,
172  NOT,
173  EQ,
174  NE,
175  LT,
176  LE,
177  GT,
178  GE,
179  EXP,
180  NEG,
181  INDEX,
182  HASH,
183  CALL,
184  THISCALL,
185  SIZEOF,
186  KEYSOF,
187  TYPEOF,
188  INITVEC,
189  INITHASH,
190  SWITCH,
191  PLUSEQ,
192  MINUSEQ,
193  MULTEQ,
194  DIVEQ,
195  MINUSAT,
196  WHEREAT
197  };
198  int oper;
199  node_t *left, *right;
200  const char *operstr();
201  expr_t(srcpos_t pos, int op, node_t *l, node_t *r) : node_t(pos), oper(op), left(l), right(r) { }
202  virtual ~expr_t() { if (left) delete left; if (right) delete right; }
203  };
204 
205  class iden_t : public node_t
206  {
207  public:
208  lk_string name;
209  bool constval;
210  bool globalval;
211  bool special;
212  iden_t(srcpos_t pos, const lk_string &n, bool cons, bool glob, bool speci) : node_t(pos), name(n), constval(cons), globalval(glob), special(speci) { }
213  virtual ~iden_t() { }
214  };
215 
216  class ctlstmt_t : public node_t
217  {
218  public:
219  enum {
220  INVALID,
221 
222  RETURN,
223  EXIT,
224  BREAK,
225  CONTINUE
226  };
227 
228  const char *ctlstr();
229 
230  int ictl;
231  node_t *rexpr;
232  ctlstmt_t(srcpos_t pos, int ctl, node_t *ex = 0) : node_t(pos), ictl(ctl), rexpr(ex) { }
233  virtual ~ctlstmt_t() { if (rexpr) delete rexpr; }
234  };
235 
236  class constant_t : public node_t
237  {
238  public:
239  double value;
240  constant_t(srcpos_t pos, double v) : node_t(pos), value(v) { }
241  virtual ~constant_t() { }
242  };
243 
244  class literal_t : public node_t
245  {
246  public:
247  lk_string value;
248  literal_t(srcpos_t pos, const lk_string &s) : node_t(pos), value(s) { }
249  virtual ~literal_t() { }
250  };
251 
252  class null_t : public node_t
253  {
254  public:
255  null_t(srcpos_t pos) : node_t(pos) { }
256  virtual ~null_t() { }
257  };
258 
259  void pretty_print(lk_string &str, node_t *root, int level);
260 };
261 
262 #endif
Forms nodes of recursive descent tree.
Definition: absyn.h:118
Definition: absyn.h:66
Records position in source script.
Definition: absyn.h:96
diff --git a/doc/html/codegen_8h_source.html b/doc/html/codegen_8h_source.html index 744164ee..41cf9724 100644 --- a/doc/html/codegen_8h_source.html +++ b/doc/html/codegen_8h_source.html @@ -89,7 +89,7 @@
codegen.h
-
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Sustainable Energy, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_codegen_h
26 #define __lk_codegen_h
27 
28 #include <lk/absyn.h>
29 #include <lk/vm.h>
30 
31 namespace lk {
32 
43  class codegen
44  {
45  public:
46  codegen();
47 
48  lk_string error() { return m_errStr; }
49 
51  bool generate(lk::node_t *root);
53  size_t get(bytecode &b);
55  void textout(lk_string &assembly, lk_string &bytecode);
56 
57  private:
58 
66  struct instr {
67  instr(srcpos_t sp, Opcode _op, int _arg, const char *lbl = 0)
68  : pos(sp), op(_op), arg(_arg) {
69  label = 0;
70  if (lbl) label = new lk_string(lbl);
71  }
72 
73  instr(const instr &cpy)
74  {
75  copy(cpy);
76  }
77  ~instr() {
78  if (label) delete label;
79  }
80  void copy(const instr &cpy)
81  {
82  pos = cpy.pos;
83  op = cpy.op;
84  arg = cpy.arg;
85  label = 0;
86  if (cpy.label)
87  label = new lk_string(*cpy.label);
88  }
89 
90  instr &operator=(const instr &rhs) {
91  copy(rhs);
92  return *this;
93  }
94 
95  lk::srcpos_t pos;
96  Opcode op;
97  int arg;
98  lk_string *label;
99  };
100 
102  std::vector<instr> m_asm;
104  typedef unordered_map< lk_string, int, lk_string_hash, lk_string_equal > LabelMap;
105  LabelMap m_labelAddr;
106  std::vector< vardata_t > m_constData;
107  std::vector< lk_string > m_idList;
108  int m_labelCounter;
110  std::vector<lk_string> m_breakAddr, m_continueAddr;
111  lk_string m_errStr;
112 
113  bool error(const char *fmt, ...);
114  bool error(const lk_string &s);
115 
116  int place_identifier(const lk_string &id);
117  int place_const(vardata_t &d);
118  int const_value(double value);
119  int const_literal(const lk_string &lit);
120  lk_string new_label();
121  void place_label(const lk_string &s);
122  int emit( srcpos_t pos, Opcode o, int arg = 0);
123  int emit(srcpos_t pos, Opcode o, const lk_string &L);
124  bool initialize_const_vec( lk::list_t *v, vardata_t &vvec );
125  bool initialize_const_hash( lk::list_t *v, vardata_t &vhash );
126  bool pfgen_stmt(lk::node_t *root, unsigned int flags);
127  bool pfgen(lk::node_t *root, unsigned int flags);
128  };
129 }; // namespace lk
130 
131 #endif
Stores instruction stack information.
Definition: vm.h:61
+
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Energy Innovation, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_codegen_h
26 #define __lk_codegen_h
27 
28 #include <lk/absyn.h>
29 #include <lk/vm.h>
30 
31 namespace lk {
32 
43  class codegen
44  {
45  public:
46  codegen();
47 
48  lk_string error() { return m_errStr; }
49 
51  bool generate(lk::node_t *root);
53  size_t get(bytecode &b);
55  void textout(lk_string &assembly, lk_string &bytecode);
56 
57  private:
58 
66  struct instr {
67  instr(srcpos_t sp, Opcode _op, int _arg, const char *lbl = 0)
68  : pos(sp), op(_op), arg(_arg) {
69  label = 0;
70  if (lbl) label = new lk_string(lbl);
71  }
72 
73  instr(const instr &cpy)
74  {
75  copy(cpy);
76  }
77  ~instr() {
78  if (label) delete label;
79  }
80  void copy(const instr &cpy)
81  {
82  pos = cpy.pos;
83  op = cpy.op;
84  arg = cpy.arg;
85  label = 0;
86  if (cpy.label)
87  label = new lk_string(*cpy.label);
88  }
89 
90  instr &operator=(const instr &rhs) {
91  copy(rhs);
92  return *this;
93  }
94 
95  lk::srcpos_t pos;
96  Opcode op;
97  int arg;
98  lk_string *label;
99  };
100 
102  std::vector<instr> m_asm;
104  typedef unordered_map< lk_string, int, lk_string_hash, lk_string_equal > LabelMap;
105  LabelMap m_labelAddr;
106  std::vector< vardata_t > m_constData;
107  std::vector< lk_string > m_idList;
108  int m_labelCounter;
110  std::vector<lk_string> m_breakAddr, m_continueAddr;
111  lk_string m_errStr;
112 
113  bool error(const char *fmt, ...);
114  bool error(const lk_string &s);
115 
116  int place_identifier(const lk_string &id);
117  int place_const(vardata_t &d);
118  int const_value(double value);
119  int const_literal(const lk_string &lit);
120  lk_string new_label();
121  void place_label(const lk_string &s);
122  int emit( srcpos_t pos, Opcode o, int arg = 0);
123  int emit(srcpos_t pos, Opcode o, const lk_string &L);
124  bool initialize_const_vec( lk::list_t *v, vardata_t &vvec );
125  bool initialize_const_hash( lk::list_t *v, vardata_t &vhash );
126  bool pfgen_stmt(lk::node_t *root, unsigned int flags);
127  bool pfgen(lk::node_t *root, unsigned int flags);
128  };
129 }; // namespace lk
130 
131 #endif
Stores instruction stack information.
Definition: vm.h:61
Codegen produces bytecode from a tree of nodes.
Definition: codegen.h:43
void textout(lk_string &assembly, lk_string &bytecode)
writes the bytecode into assembly
Definition: codegen.cpp:81
Forms nodes of recursive descent tree.
Definition: absyn.h:118
diff --git a/doc/html/env_8h_source.html b/doc/html/env_8h_source.html index fc1f6e13..1e05daf6 100644 --- a/doc/html/env_8h_source.html +++ b/doc/html/env_8h_source.html @@ -89,7 +89,7 @@
env.h
-
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Sustainable Energy, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_var_h
26 #define __lk_var_h
27 
28 #include <vector>
29 #include <cstdio>
30 #include <cstdarg>
31 #include <exception>
32 
33 #include <lk/absyn.h>
34 #include <lk/invoke.h>
35 
37 #define LK_DOC( fn, desc, sig ) if (cxt.doc_mode()) { cxt.document( lk::doc_t(fn , "", desc, sig ) ); return; }
38 #define LK_DOC1( fn, notes, desc1, sig1 ) if (cxt.doc_mode()) { cxt.document( lk::doc_t(fn , notes, desc1, sig1 )); return; }
39 #define LK_DOC2( fn, notes, desc1, sig1, desc2, sig2 ) if (cxt.doc_mode()) { cxt.document( lk::doc_t(fn , notes, desc1, sig1, desc2, sig2 )); return; }
40 #define LK_DOC3( fn, notes, desc1, sig1, desc2, sig2, desc3, sig3 ) if (cxt.doc_mode()) { cxt.document( lk::doc_t(fn , notes, desc1, sig1, desc2, sig2, desc3, sig3 )); return; }
41 
42 
43 namespace lk {
44  class vardata_t;
45  struct fcallinfo_t;
46  struct bytecode;
47  typedef unordered_map< lk_string, vardata_t*, lk_string_hash, lk_string_equal > varhash_t;
48 
57  class error_t : public std::exception
58  {
59  public:
60  error_t() : text("general data exception") { }
61  error_t(const lk_string &s) : text(s) { }
62  error_t(const char *fmt, ...) {
63  char buf[512];
64  va_list args;
65  va_start(args, fmt);
66 #ifdef _WIN32
67  _vsnprintf(buf, 511, fmt, args);
68 #else
69  vsnprintf(buf, 511, fmt, args);
70 #endif
71  va_end(args);
72  text = buf;
73  }
74  //error_t(const lk_string &t) : text(t) { }
75  virtual ~error_t() throw() { }
76  lk_string text;
77  virtual const char *what() const throw (){ return text.c_str(); }
78  };
79 
87  class vardata_t
88  {
89  private:
90  unsigned char m_type;
97  union {
98  void *p;
99  double v;
100  } m_u;
101 
102  void set_type( unsigned char ty );
103  void assert_modify() throw( error_t );
104 
105  public:
107  static const unsigned char NULLVAL = 1;
108  static const unsigned char REFERENCE = 2;
109  static const unsigned char NUMBER = 3;
110  static const unsigned char STRING = 4;
111  static const unsigned char VECTOR = 5;
112  static const unsigned char HASH = 6;
113  static const unsigned char FUNCTION = 7;
114  static const unsigned char EXTFUNC = 8;
115  static const unsigned char INTFUNC = 9;
116 
117  static const unsigned char TYPEMASK = 0x0F;
118  static const unsigned char FLAGMASK = 0xF0;
119 
121  static const unsigned char ASSIGNED = 1;
122  static const unsigned char CONSTVAL = 2;
123  static const unsigned char GLOBALVAL = 3;
124 
125  vardata_t();
126  vardata_t(const vardata_t &cp);
127  ~vardata_t();
128 
129  inline unsigned char type() const { return (m_type & TYPEMASK); }
130  const char *typestr() const;
131 
132  void set_flag(unsigned char flag) { m_type |= (0x01 << flag) << 4; }
133  void clear_flag(unsigned char flag) { m_type &= ~((0x01 << flag) << 4); }
134  bool flagval(unsigned char flag) const { return ((m_type >> flag) >> 4) & 0x01; }
135 
136  bool as_boolean() const;
137  unsigned int as_unsigned() const;
138  int as_integer() const;
139  lk_string as_string() const;
140  double as_number() const;
141 
142  bool equals( vardata_t &rhs ) const;
143  bool lessthan( vardata_t &rhs ) const;
144 
145  void nullify();
146 
147  void deep_localize();
148 
149  bool copy(vardata_t &rhs) throw(error_t);
150  vardata_t &operator=(const vardata_t &rhs) throw(error_t)
151  {
152  copy(const_cast<vardata_t&>(rhs));
153  return *this;
154  }
156  inline vardata_t &deref() const throw (error_t) {
157  vardata_t *p = const_cast<vardata_t*>(this);
158  while (p->type() == REFERENCE) {
159  vardata_t *pref = reinterpret_cast<vardata_t*>(p->m_u.p);
160  if (p == pref) throw error_t("self referential reference");
161  p = pref;
162  }
163  if (!p) throw error_t("dereference resulted in null target");
164  return *p;
165  }
166 
167  void assign( double d ) throw( error_t );
168  void assign( const char *s ) throw( error_t );
169  void assign( const lk_string &s ) throw( error_t );
170  void empty_vector() throw( error_t );
171  void empty_hash() throw( error_t );
172  void assign( const lk_string &key, vardata_t *val ) throw( error_t );
173  void unassign( const lk_string &key ) throw( error_t );
174  void assign( expr_t *func ) throw( error_t );
175  void assign( vardata_t *ref ) throw( error_t );
176 
177  void assign_fcall( fcallinfo_t *fci ) throw (error_t);
178  void assign_faddr( size_t bcip ) throw(error_t);
179 
180  void resize( size_t n ) throw( error_t );
181 
182  vardata_t *ref() const;
183  double num() const throw(error_t);
184  lk_string str() const throw(error_t);
185  expr_t *func() const throw(error_t);
186  vardata_t *index(size_t idx) const throw(error_t);
187  size_t length() const ;
188  vardata_t *lookup( const lk_string &key ) const throw(error_t);
189  fcallinfo_t *fcall() const throw(error_t);
190  size_t faddr() const throw(error_t);
191 
192  std::vector<vardata_t> *vec() const throw(error_t);
193  void vec_append( double d ) throw(error_t);
194  void vec_append( const lk_string &s ) throw(error_t);
195 
196  varhash_t *hash() const throw(error_t);
197  void hash_item( const lk_string &key, double d ) throw(error_t);
198  void hash_item( const lk_string &key, const lk_string &s ) throw(error_t);
199  void hash_item( const lk_string &key, const vardata_t &v ) throw(error_t);
200  vardata_t &hash_item( const lk_string &key ) throw(error_t);
201 
202  };
203 
204  class env_t;
205 
214  class objref_t
215  {
216  private:
217  friend class env_t;
218  env_t *m_env;
219 
220  public:
221  objref_t() { m_env = 0; };
222  virtual ~objref_t() { }
223  env_t *get_env() { return m_env; }
224  virtual lk_string type_name() = 0;
225  };
226 
227  class invoke_t;
228 
229  typedef void (*fcall_t)( lk::invoke_t& );
230 
238  struct fcallinfo_t {
239  fcall_t f;
240  lk_invokable f_ext;
241  void *user_data;
242  };
243 
244  typedef unordered_map< lk_string, fcallinfo_t, lk_string_hash, lk_string_equal > funchash_t;
245 
246 
255  class doc_t
256  {
257  friend class invoke_t;
258  public:
259  doc_t() : has_2(false), has_3(false), m_ok(false) { }
260  doc_t(const char *f, const char *n,
261  const char *d1, const char *s1)
262  : func_name(f), notes(n),
263  desc1(d1), sig1(s1),
264  desc2(""), sig2(""),
265  desc3(""), sig3(""),
266  has_2(false), has_3(false), m_ok(false) { }
267 
268  doc_t(const char *f, const char *n,
269  const char *d1, const char *s1,
270  const char *d2, const char *s2)
271  : func_name(f), notes(n),
272  desc1(d1), sig1(s1),
273  desc2(d2), sig2(s2),
274  desc3(""), sig3(""),
275  has_2(true), has_3(false), m_ok(false) { }
276 
277  doc_t(const char *f, const char *n,
278  const char *d1, const char *s1,
279  const char *d2, const char *s2,
280  const char *d3, const char *s3)
281  : func_name(f), notes(n),
282  desc1(d1), sig1(s1),
283  desc2(d2), sig2(s2),
284  desc3(d3), sig3(s3),
285  has_2(true), has_3(true), m_ok(false) { }
286 
287  lk_string func_name;
288  lk_string notes;
289  lk_string desc1, sig1;
290  lk_string desc2, sig2;
291  lk_string desc3, sig3;
292  bool has_2, has_3;
293 
294  bool ok() { return m_ok; }
295 
296  static bool info(fcallinfo_t *f, doc_t &d);
297  static bool info(fcall_t f, doc_t &d);
298  static bool info(lk_invokable f, doc_t &d);
299  private:
300  void copy_data(doc_t *p);
301  bool m_ok;
302  };
303 
311  class invoke_t
312  {
313  friend class doc_t;
314  private:
315  doc_t *m_docPtr;
316  env_t *m_env;
317  vardata_t &m_resultVal;
318  std::vector< vardata_t > m_argList;
319 
320  lk_string m_error;
321  bool m_hasError;
322 
323  void *m_userData;
324 
325  // for threading existing bytecode
326  bytecode *m_bc;
327 
328  public:
329  invoke_t(env_t *e, vardata_t &result, void *user_data = 0, bytecode *bc = 0)
330  : m_docPtr(0), m_env(e), m_resultVal(result), m_hasError(false), m_userData(user_data), m_bc(bc) { }
331 
332  bool doc_mode();
333  void document(doc_t d);
334  env_t *env() { return m_env; }
335  vardata_t &result() { return m_resultVal; }
336  void *user_data() { return m_userData; }
337  bytecode *bc() { return m_bc; }
338 
339 
340  std::vector< vardata_t > &arg_list() { return m_argList; }
341  size_t arg_count() { return m_argList.size(); }
342 
344  vardata_t &arg(size_t idx) throw( error_t ) {
345  if (idx < m_argList.size()) return m_argList[idx].deref();
346  else throw error_t( "invalid access to function argument %d, only %d given", idx, m_argList.size());
347  }
348 
349  void error(const lk_string &text) { m_error = text; m_hasError = true; }
350  lk_string error() { return m_error; }
351  bool has_error() { return m_hasError; }
352  };
353 
361  class env_t
362  {
363  public:
364 
372  struct dynlib_t
373  {
374  lk_string path;
375  void *handle;
376  lk_invokable *functions;
377  };
378 
379  private:
380  env_t *m_parent;
381 
382  varhash_t m_varHash;
383  varhash_t::iterator m_varIter;
384 
385  funchash_t m_funcHash;
386  std::vector< objref_t* > m_objTable;
387 
388  std::vector< dynlib_t > m_dynlibList;
389 
390  bool register_ext_func(lk_invokable f, void *user_data = 0);
391  void unregister_ext_func(lk_invokable f);
392 
393  public:
394 
395  env_t();
396  env_t(env_t *p);
397  virtual ~env_t();
398 
399  void clear_vars();
400  void clear_objs();
401 
402  void assign(const lk_string &name, vardata_t *value);
403  void unassign(const lk_string &name);
404  vardata_t *lookup(const lk_string &name, bool search_hierarchy);
405  bool first(lk_string &key, vardata_t *&value);
406  bool next(lk_string &key, vardata_t *&value);
407  unsigned int size();
408  void set_parent(env_t *p);
409  env_t *parent();
410  env_t *global();
411 
412  bool register_func(fcall_t f, void *user_data = 0);
413  bool register_funcs(std::vector<fcall_t> l, void *user_data = 0);
414  bool register_funcs(fcall_t list[], void *user_data = 0); // null item terminated list
415 
416  bool load_library(const lk_string &path);
417  bool unload_library(const lk_string &path);
418  std::vector< dynlib_t* > libraries();
419 
420  fcallinfo_t *lookup_func(const lk_string &name);
421  std::vector<lk_string> list_funcs();
422 
423  size_t insert_object( objref_t *o );
424  bool destroy_object( objref_t *o );
425  objref_t *query_object( size_t ref );
426 
427  void call( const lk_string &name,
428  std::vector< vardata_t > &args,
429  vardata_t &result ) throw( error_t );
430 
431  };
432 
434  void external_call( lk_invokable p, lk::invoke_t &cxt );
435 
436 }; // namespace lk
437 
438 #endif
Stores instruction stack information.
Definition: vm.h:61
+
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Energy Innovation, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_var_h
26 #define __lk_var_h
27 
28 #include <vector>
29 #include <cstdio>
30 #include <cstdarg>
31 #include <exception>
32 
33 #include <lk/absyn.h>
34 #include <lk/invoke.h>
35 
37 #define LK_DOC( fn, desc, sig ) if (cxt.doc_mode()) { cxt.document( lk::doc_t(fn , "", desc, sig ) ); return; }
38 #define LK_DOC1( fn, notes, desc1, sig1 ) if (cxt.doc_mode()) { cxt.document( lk::doc_t(fn , notes, desc1, sig1 )); return; }
39 #define LK_DOC2( fn, notes, desc1, sig1, desc2, sig2 ) if (cxt.doc_mode()) { cxt.document( lk::doc_t(fn , notes, desc1, sig1, desc2, sig2 )); return; }
40 #define LK_DOC3( fn, notes, desc1, sig1, desc2, sig2, desc3, sig3 ) if (cxt.doc_mode()) { cxt.document( lk::doc_t(fn , notes, desc1, sig1, desc2, sig2, desc3, sig3 )); return; }
41 
42 
43 namespace lk {
44  class vardata_t;
45  struct fcallinfo_t;
46  struct bytecode;
47  typedef unordered_map< lk_string, vardata_t*, lk_string_hash, lk_string_equal > varhash_t;
48 
57  class error_t : public std::exception
58  {
59  public:
60  error_t() : text("general data exception") { }
61  error_t(const lk_string &s) : text(s) { }
62  error_t(const char *fmt, ...) {
63  char buf[512];
64  va_list args;
65  va_start(args, fmt);
66 #ifdef _WIN32
67  _vsnprintf(buf, 511, fmt, args);
68 #else
69  vsnprintf(buf, 511, fmt, args);
70 #endif
71  va_end(args);
72  text = buf;
73  }
74  //error_t(const lk_string &t) : text(t) { }
75  virtual ~error_t() throw() { }
76  lk_string text;
77  virtual const char *what() const throw (){ return text.c_str(); }
78  };
79 
87  class vardata_t
88  {
89  private:
90  unsigned char m_type;
97  union {
98  void *p;
99  double v;
100  } m_u;
101 
102  void set_type( unsigned char ty );
103  void assert_modify() throw( error_t );
104 
105  public:
107  static const unsigned char NULLVAL = 1;
108  static const unsigned char REFERENCE = 2;
109  static const unsigned char NUMBER = 3;
110  static const unsigned char STRING = 4;
111  static const unsigned char VECTOR = 5;
112  static const unsigned char HASH = 6;
113  static const unsigned char FUNCTION = 7;
114  static const unsigned char EXTFUNC = 8;
115  static const unsigned char INTFUNC = 9;
116 
117  static const unsigned char TYPEMASK = 0x0F;
118  static const unsigned char FLAGMASK = 0xF0;
119 
121  static const unsigned char ASSIGNED = 1;
122  static const unsigned char CONSTVAL = 2;
123  static const unsigned char GLOBALVAL = 3;
124 
125  vardata_t();
126  vardata_t(const vardata_t &cp);
127  ~vardata_t();
128 
129  inline unsigned char type() const { return (m_type & TYPEMASK); }
130  const char *typestr() const;
131 
132  void set_flag(unsigned char flag) { m_type |= (0x01 << flag) << 4; }
133  void clear_flag(unsigned char flag) { m_type &= ~((0x01 << flag) << 4); }
134  bool flagval(unsigned char flag) const { return ((m_type >> flag) >> 4) & 0x01; }
135 
136  bool as_boolean() const;
137  unsigned int as_unsigned() const;
138  int as_integer() const;
139  lk_string as_string() const;
140  double as_number() const;
141 
142  bool equals( vardata_t &rhs ) const;
143  bool lessthan( vardata_t &rhs ) const;
144 
145  void nullify();
146 
147  void deep_localize();
148 
149  bool copy(vardata_t &rhs) throw(error_t);
150  vardata_t &operator=(const vardata_t &rhs) throw(error_t)
151  {
152  copy(const_cast<vardata_t&>(rhs));
153  return *this;
154  }
156  inline vardata_t &deref() const throw (error_t) {
157  vardata_t *p = const_cast<vardata_t*>(this);
158  while (p->type() == REFERENCE) {
159  vardata_t *pref = reinterpret_cast<vardata_t*>(p->m_u.p);
160  if (p == pref) throw error_t("self referential reference");
161  p = pref;
162  }
163  if (!p) throw error_t("dereference resulted in null target");
164  return *p;
165  }
166 
167  void assign( double d ) throw( error_t );
168  void assign( const char *s ) throw( error_t );
169  void assign( const lk_string &s ) throw( error_t );
170  void empty_vector() throw( error_t );
171  void empty_hash() throw( error_t );
172  void assign( const lk_string &key, vardata_t *val ) throw( error_t );
173  void unassign( const lk_string &key ) throw( error_t );
174  void assign( expr_t *func ) throw( error_t );
175  void assign( vardata_t *ref ) throw( error_t );
176 
177  void assign_fcall( fcallinfo_t *fci ) throw (error_t);
178  void assign_faddr( size_t bcip ) throw(error_t);
179 
180  void resize( size_t n ) throw( error_t );
181 
182  vardata_t *ref() const;
183  double num() const throw(error_t);
184  lk_string str() const throw(error_t);
185  expr_t *func() const throw(error_t);
186  vardata_t *index(size_t idx) const throw(error_t);
187  size_t length() const ;
188  vardata_t *lookup( const lk_string &key ) const throw(error_t);
189  fcallinfo_t *fcall() const throw(error_t);
190  size_t faddr() const throw(error_t);
191 
192  std::vector<vardata_t> *vec() const throw(error_t);
193  void vec_append( double d ) throw(error_t);
194  void vec_append( const lk_string &s ) throw(error_t);
195 
196  varhash_t *hash() const throw(error_t);
197  void hash_item( const lk_string &key, double d ) throw(error_t);
198  void hash_item( const lk_string &key, const lk_string &s ) throw(error_t);
199  void hash_item( const lk_string &key, const vardata_t &v ) throw(error_t);
200  vardata_t &hash_item( const lk_string &key ) throw(error_t);
201 
202  };
203 
204  class env_t;
205 
214  class objref_t
215  {
216  private:
217  friend class env_t;
218  env_t *m_env;
219 
220  public:
221  objref_t() { m_env = 0; };
222  virtual ~objref_t() { }
223  env_t *get_env() { return m_env; }
224  virtual lk_string type_name() = 0;
225  };
226 
227  class invoke_t;
228 
229  typedef void (*fcall_t)( lk::invoke_t& );
230 
238  struct fcallinfo_t {
239  fcall_t f;
240  lk_invokable f_ext;
241  void *user_data;
242  };
243 
244  typedef unordered_map< lk_string, fcallinfo_t, lk_string_hash, lk_string_equal > funchash_t;
245 
246 
255  class doc_t
256  {
257  friend class invoke_t;
258  public:
259  doc_t() : has_2(false), has_3(false), m_ok(false) { }
260  doc_t(const char *f, const char *n,
261  const char *d1, const char *s1)
262  : func_name(f), notes(n),
263  desc1(d1), sig1(s1),
264  desc2(""), sig2(""),
265  desc3(""), sig3(""),
266  has_2(false), has_3(false), m_ok(false) { }
267 
268  doc_t(const char *f, const char *n,
269  const char *d1, const char *s1,
270  const char *d2, const char *s2)
271  : func_name(f), notes(n),
272  desc1(d1), sig1(s1),
273  desc2(d2), sig2(s2),
274  desc3(""), sig3(""),
275  has_2(true), has_3(false), m_ok(false) { }
276 
277  doc_t(const char *f, const char *n,
278  const char *d1, const char *s1,
279  const char *d2, const char *s2,
280  const char *d3, const char *s3)
281  : func_name(f), notes(n),
282  desc1(d1), sig1(s1),
283  desc2(d2), sig2(s2),
284  desc3(d3), sig3(s3),
285  has_2(true), has_3(true), m_ok(false) { }
286 
287  lk_string func_name;
288  lk_string notes;
289  lk_string desc1, sig1;
290  lk_string desc2, sig2;
291  lk_string desc3, sig3;
292  bool has_2, has_3;
293 
294  bool ok() { return m_ok; }
295 
296  static bool info(fcallinfo_t *f, doc_t &d);
297  static bool info(fcall_t f, doc_t &d);
298  static bool info(lk_invokable f, doc_t &d);
299  private:
300  void copy_data(doc_t *p);
301  bool m_ok;
302  };
303 
311  class invoke_t
312  {
313  friend class doc_t;
314  private:
315  doc_t *m_docPtr;
316  env_t *m_env;
317  vardata_t &m_resultVal;
318  std::vector< vardata_t > m_argList;
319 
320  lk_string m_error;
321  bool m_hasError;
322 
323  void *m_userData;
324 
325  // for threading existing bytecode
326  bytecode *m_bc;
327 
328  public:
329  invoke_t(env_t *e, vardata_t &result, void *user_data = 0, bytecode *bc = 0)
330  : m_docPtr(0), m_env(e), m_resultVal(result), m_hasError(false), m_userData(user_data), m_bc(bc) { }
331 
332  bool doc_mode();
333  void document(doc_t d);
334  env_t *env() { return m_env; }
335  vardata_t &result() { return m_resultVal; }
336  void *user_data() { return m_userData; }
337  bytecode *bc() { return m_bc; }
338 
339 
340  std::vector< vardata_t > &arg_list() { return m_argList; }
341  size_t arg_count() { return m_argList.size(); }
342 
344  vardata_t &arg(size_t idx) throw( error_t ) {
345  if (idx < m_argList.size()) return m_argList[idx].deref();
346  else throw error_t( "invalid access to function argument %d, only %d given", idx, m_argList.size());
347  }
348 
349  void error(const lk_string &text) { m_error = text; m_hasError = true; }
350  lk_string error() { return m_error; }
351  bool has_error() { return m_hasError; }
352  };
353 
361  class env_t
362  {
363  public:
364 
372  struct dynlib_t
373  {
374  lk_string path;
375  void *handle;
376  lk_invokable *functions;
377  };
378 
379  private:
380  env_t *m_parent;
381 
382  varhash_t m_varHash;
383  varhash_t::iterator m_varIter;
384 
385  funchash_t m_funcHash;
386  std::vector< objref_t* > m_objTable;
387 
388  std::vector< dynlib_t > m_dynlibList;
389 
390  bool register_ext_func(lk_invokable f, void *user_data = 0);
391  void unregister_ext_func(lk_invokable f);
392 
393  public:
394 
395  env_t();
396  env_t(env_t *p);
397  virtual ~env_t();
398 
399  void clear_vars();
400  void clear_objs();
401 
402  void assign(const lk_string &name, vardata_t *value);
403  void unassign(const lk_string &name);
404  vardata_t *lookup(const lk_string &name, bool search_hierarchy);
405  bool first(lk_string &key, vardata_t *&value);
406  bool next(lk_string &key, vardata_t *&value);
407  unsigned int size();
408  void set_parent(env_t *p);
409  env_t *parent();
410  env_t *global();
411 
412  bool register_func(fcall_t f, void *user_data = 0);
413  bool register_funcs(std::vector<fcall_t> l, void *user_data = 0);
414  bool register_funcs(fcall_t list[], void *user_data = 0); // null item terminated list
415 
416  bool load_library(const lk_string &path);
417  bool unload_library(const lk_string &path);
418  std::vector< dynlib_t* > libraries();
419 
420  fcallinfo_t *lookup_func(const lk_string &name);
421  std::vector<lk_string> list_funcs();
422 
423  size_t insert_object( objref_t *o );
424  bool destroy_object( objref_t *o );
425  objref_t *query_object( size_t ref );
426 
427  void call( const lk_string &name,
428  std::vector< vardata_t > &args,
429  vardata_t &result ) throw( error_t );
430 
431  };
432 
434  void external_call( lk_invokable p, lk::invoke_t &cxt );
435 
436 }; // namespace lk
437 
438 #endif
Stores instruction stack information.
Definition: vm.h:61
void assign_fcall(fcallinfo_t *fci)
assigns as EXTFUNC type whose value points to the function&#39;s fci
Definition: env.cpp:541
Documents LK functions.
Definition: env.h:255
Definition: absyn.h:66
diff --git a/doc/html/eval_8h_source.html b/doc/html/eval_8h_source.html index f596924d..d52168b5 100644 --- a/doc/html/eval_8h_source.html +++ b/doc/html/eval_8h_source.html @@ -89,7 +89,7 @@
eval.h
-
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Sustainable Energy, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_eval_h
26 #define __lk_eval_h
27 
28 #include <lk/absyn.h>
29 #include <lk/env.h>
30 
31 namespace lk {
32  enum { CTL_NONE, CTL_BREAK, CTL_CONTINUE, CTL_RETURN, CTL_EXIT };
33 
34  class eval
35  {
36  lk::node_t *m_tree;
37  lk::env_t m_localEnv;
38  lk::env_t *m_env;
39  vardata_t m_result;
40  std::vector< lk_string > m_errors;
41 
42  public:
43  eval(lk::node_t *tree);
44  eval(lk::node_t *tree, lk::env_t *env);
45  virtual ~eval();
46 
47  bool run();
48 
49  virtual bool special_set(const lk_string &name, vardata_t &val);
50  virtual bool special_get(const lk_string &name, vardata_t &val);
51  inline virtual bool on_run(int /* line */) { return true; }
52 
53  std::vector<lk_string> &errors() { return m_errors; }
54  size_t error_count() { return m_errors.size(); }
55  lk_string get_error(size_t i) { if (i < m_errors.size()) return m_errors[i]; else return lk_string(""); }
56 
57  lk::env_t &env() { return *m_env; }
58  vardata_t &result() { return m_result; }
59 
60  protected:
61  bool interpret(node_t *root,
62  lk::env_t *cur_env,
63  vardata_t &result,
64  unsigned int flags, /* normally 0 */
65  unsigned int &ctl_id);
66 
67  bool do_op_eq(void(*oper)(lk::vardata_t &, lk::vardata_t &),
68  lk::expr_t *n, lk::env_t *cur_env, unsigned int &flags, unsigned int &ctl_id,
69  lk::vardata_t &result, lk::vardata_t &l, lk::vardata_t &r);
70  };
71 };
72 
73 #endif
Forms nodes of recursive descent tree.
Definition: absyn.h:118
+
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Energy Innovation, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_eval_h
26 #define __lk_eval_h
27 
28 #include <lk/absyn.h>
29 #include <lk/env.h>
30 
31 namespace lk {
32  enum { CTL_NONE, CTL_BREAK, CTL_CONTINUE, CTL_RETURN, CTL_EXIT };
33 
34  class eval
35  {
36  lk::node_t *m_tree;
37  lk::env_t m_localEnv;
38  lk::env_t *m_env;
39  vardata_t m_result;
40  std::vector< lk_string > m_errors;
41 
42  public:
43  eval(lk::node_t *tree);
44  eval(lk::node_t *tree, lk::env_t *env);
45  virtual ~eval();
46 
47  bool run();
48 
49  virtual bool special_set(const lk_string &name, vardata_t &val);
50  virtual bool special_get(const lk_string &name, vardata_t &val);
51  inline virtual bool on_run(int /* line */) { return true; }
52 
53  std::vector<lk_string> &errors() { return m_errors; }
54  size_t error_count() { return m_errors.size(); }
55  lk_string get_error(size_t i) { if (i < m_errors.size()) return m_errors[i]; else return lk_string(""); }
56 
57  lk::env_t &env() { return *m_env; }
58  vardata_t &result() { return m_result; }
59 
60  protected:
61  bool interpret(node_t *root,
62  lk::env_t *cur_env,
63  vardata_t &result,
64  unsigned int flags, /* normally 0 */
65  unsigned int &ctl_id);
66 
67  bool do_op_eq(void(*oper)(lk::vardata_t &, lk::vardata_t &),
68  lk::expr_t *n, lk::env_t *cur_env, unsigned int &flags, unsigned int &ctl_id,
69  lk::vardata_t &result, lk::vardata_t &l, lk::vardata_t &r);
70  };
71 };
72 
73 #endif
Forms nodes of recursive descent tree.
Definition: absyn.h:118
Definition: absyn.h:66
Vardata_t form the execution stack of the vm: stores identifiers and expressions, arguments and resul...
Definition: env.h:87
Definition: env.h:361
diff --git a/doc/html/invoke_8h_source.html b/doc/html/invoke_8h_source.html index fb651304..217ef159 100644 --- a/doc/html/invoke_8h_source.html +++ b/doc/html/invoke_8h_source.html @@ -89,7 +89,7 @@
invoke.h
-
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Sustainable Energy, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_invoke_h
26 #define __lk_invoke_h
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32  typedef void* lk_var_t;
33 
34 #define LK_NULL 1
35 #define LK_NUMBER 3
36 #define LK_STRING 4
37 #define LK_ARRAY 5
38 #define LK_TABLE 6
39 
40 struct __lk_invoke_t
41 {
42  void *__pinvoke;
43  void *__hiter;
44  void *__errbuf;
45  void *__sbuf;
46  void *__callargvec;
47  void *__callresult;
48 
49  int (*doc_mode)( struct __lk_invoke_t* );
50  void (*document)( struct __lk_invoke_t*, const char *fn, const char *desc, const char *sig );
51  void (*document2)( struct __lk_invoke_t*, const char *fn, const char *notes,
52  const char *desc1, const char *sig1,
53  const char *desc2, const char *sig2 );
54  void (*document3)( struct __lk_invoke_t*, const char *fn, const char *notes,
55  const char *desc1, const char *sig1,
56  const char *desc2, const char *sig2,
57  const char *desc3, const char *sig3);
58 
59  void(*error)(struct __lk_invoke_t*, const char *);
60  int(*arg_count)(struct __lk_invoke_t*);
61  lk_var_t(*arg)(struct __lk_invoke_t*, int);
62 
63  int(*type)(struct __lk_invoke_t*, lk_var_t);
64  const char *(*as_string)(struct __lk_invoke_t*, lk_var_t); // returns utf8
65  double(*as_number)(struct __lk_invoke_t*, lk_var_t);
66  int(*as_integer)(struct __lk_invoke_t*, lk_var_t);
67  int(*as_boolean)(struct __lk_invoke_t*, lk_var_t);
68 
69  int(*vec_count)(struct __lk_invoke_t*, lk_var_t);
70  lk_var_t(*vec_index)(struct __lk_invoke_t*, lk_var_t, int);
71 
72  int(*tab_count) (struct __lk_invoke_t*, lk_var_t);
73  const char * (*tab_first_key)(struct __lk_invoke_t*, lk_var_t);
74  const char * (*tab_next_key)(struct __lk_invoke_t*, lk_var_t);
75  lk_var_t(*tab_value)(struct __lk_invoke_t*, lk_var_t, const char *);
76 
77  lk_var_t(*result)(struct __lk_invoke_t*);
78 
79  // variable modifications
80  void(*set_null) (struct __lk_invoke_t*, lk_var_t);
81  void(*set_string)(struct __lk_invoke_t*, lk_var_t, const char *); // values are utf8
82  void(*set_number)(struct __lk_invoke_t*, lk_var_t, double);
83 
84  void(*set_number_vec)(struct __lk_invoke_t*, lk_var_t, double *, int);
85  void(*make_vec)(struct __lk_invoke_t*, lk_var_t);
86  void(*reserve)(struct __lk_invoke_t*, lk_var_t, int len);
87  lk_var_t(*append_number)(struct __lk_invoke_t*, lk_var_t, double);
88  lk_var_t(*append_string)(struct __lk_invoke_t*, lk_var_t, const char *);
89  lk_var_t(*append_null)(struct __lk_invoke_t*, lk_var_t);
90 
91  void(*make_tab)(struct __lk_invoke_t*, lk_var_t);
92  lk_var_t(*tab_set_number)(struct __lk_invoke_t*, lk_var_t, const char *, double);
93  lk_var_t(*tab_set_string)(struct __lk_invoke_t*, lk_var_t, const char *, const char *);
94  lk_var_t(*tab_set_null)(struct __lk_invoke_t*, lk_var_t, const char *);
95 
96  // creating, querying, destroying externally defined object types
97  int(*insert_object)(struct __lk_invoke_t*, const char *type, void*, void(*)(void *, void *), void *);
98  void *(*query_object)(struct __lk_invoke_t*, int);
99  void(*destroy_object)(struct __lk_invoke_t*, int);
100 
101  // invoking other functions in LK environment (i.e. for callbacks)
102  void(*clear_call_args)(struct __lk_invoke_t*);
103  lk_var_t(*append_call_arg)(struct __lk_invoke_t*);
104  lk_var_t(*call_result)(struct __lk_invoke_t*);
105  const char *(*call)(struct __lk_invoke_t*, const char *name); // returns 0 on success, error message otherwise.
106  };
107 
108  // function table must look like
109  typedef void(*lk_invokable)(struct __lk_invoke_t *);
110 
111 #define LK_FUNCTION( name ) void name( struct __lk_invoke_t *lk )
112 
113 #define LK_DOCUMENT( fn, desc, sig ) if (lk->doc_mode(lk)) { lk->document( lk, fn, desc, sig ); return; }
114 #define LK_DOCUMENT2( fn, notes, desc1, sig1, desc2, sig2 ) if (lk->doc_mode(lk)) { lk->document2( lk, fn, notes, desc1, sig1, desc2, sig2 ); return; }
115 #define LK_DOCUMENT3( fn, notes, desc1, sig1, desc2, sig2, desc3, sig3 ) if (lk->doc_mode(lk)) { lk->document3( lk, fn, notes, desc1, sig1, desc2, sig2, desc3, sig3 ); return; }
116 
117  // helper access functions
118 
119 #define lk_error( msg ) lk->error(lk, msg)
120 #define lk_arg_count( ) lk->arg_count(lk)
121 #define lk_arg( idx ) lk->arg(lk, idx)
122 #define lk_type( var ) lk->type(lk, var)
123 #define lk_as_string( var ) lk->as_string(lk, var)
124 #define lk_as_number( var ) lk->as_number(lk, var)
125 #define lk_as_integer( var ) lk->as_integer(lk, var)
126 #define lk_as_boolean( var ) lk->as_boolean(lk, var)
127 #define lk_length( var ) lk->vec_count(lk, var)
128 #define lk_index( var, idx ) lk->vec_index(lk, var, idx)
129 #define lk_table_size( var ) lk->tab_count(lk, var)
130 #define lk_first_key( var ) lk->tab_first_key(lk, var)
131 #define lk_next_key( var ) lk->tab_next_key(lk, var)
132 #define lk_value( var, key ) lk->tab_value(lk, var, key)
133 #define lk_result( ) lk->result(lk)
134 #define lk_return_number( val ) lk->set_number(lk, lk->result(lk), val )
135 #define lk_return_string( str ) lk->set_string(lk, lk->result(lk), str )
136 #define lk_set_null( var ) lk->set_null(lk, var)
137 #define lk_set_string( var, str ) lk->set_string(lk, var, str)
138 #define lk_set_number( var, val ) lk->set_number(lk, var, val)
139 #define lk_set_number_array( var, arr, len ) lk->set_number_vec(lk, var, arr, len)
140 #define lk_make_array( var ) lk->make_vec(lk)
141 #define lk_reserve( var, len ) lk->reserve(lk, var, len)
142 #define lk_append_number( var, val ) lk->append_number(lk, var, val)
143 #define lk_append_string( var, str ) lk->append_string(lk, var, str)
144 #define lk_append_null( var ) lk->append_null(lk, var)
145 #define lk_make_table( var ) lk->make_tab(lk, var)
146 #define lk_table_set_number( var, key, val ) lk->tab_set_number( lk, var, key, val )
147 #define lk_table_set_string( var, key, str ) lk->tab_set_string( lk, var, key, str )
148 #define lk_table_set_null( var, key ) lk->tab_set_null(lk, var, key)
149 #define lk_insert_object( type, obj, freefunc, cbdata ) lk->insert_object(lk, type, obj, freefunc, cbdata)
150 #define lk_query_object( handle ) lk->query_object(lk, handle)
151 #define lk_destroy_object( handle ) lk->destroy_object(lk, handle)
152 #define lk_clear_call_args( ) lk->clear_call_args(lk)
153 #define lk_append_call_arg( ) lk->append_call_arg(lk)
154 #define lk_call_result( ) lk->call_result(lk)
155 #define lk_call( name ) lk->call(lk,name)
156 
157  // DLL must export 2 functions:
158  // int lk_extension_api_version()
159  // lk_invokable *lk_function_list()
160 
161 #define LK_EXTENSION_API_VERSION 1003
162 
163 #if defined(__WINDOWS__)||defined(WIN32)||defined(_WIN32)||defined(__MINGW___)||defined(_MSC_VER)
164 #define LKAPIEXPORT __declspec(dllexport)
165 #else
166 #define LKAPIEXPORT
167 #endif
168 
169 #define LK_BEGIN_EXTENSION() \
170  LKAPIEXPORT int lk_extension_api_version() \
171  { return LK_EXTENSION_API_VERSION; } \
172  LKAPIEXPORT lk_invokable *lk_function_list() { \
173  static lk_invokable _ll[] = {
174 #define LK_END_EXTENSION() ,0 }; return _ll; }
175 
176  /* examples: (these two functions are identical)
177 
178  void mean_func( struct __lk_invoke_t *lk )
179  {
180  LK_DOCUMENT( "mean", "Returns the average of an array of numbers.", "(array):number" );
181 
182  lk->set_number( lk->result(lk), 1.3 );
183  }
184 
185  LK_FUNCTION( mean_func2 )
186  {
187  LK_DOCUMENT( "mean2", "Returns the average of an array of numbers.", "(array):number" );
188 
189  lk_return_number( 1.3 );
190  }
191 
192  LK_BEGIN_EXTENSION()
193  mean_func, sigma_func, sum_func,
194  xmult_func, average_func
195  LK_END_EXTENSION()
196 
197  */
198 
199 #ifdef __cplusplus
200 }
201 #endif
202 
203 #endif
+
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Energy Innovation, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_invoke_h
26 #define __lk_invoke_h
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32  typedef void* lk_var_t;
33 
34 #define LK_NULL 1
35 #define LK_NUMBER 3
36 #define LK_STRING 4
37 #define LK_ARRAY 5
38 #define LK_TABLE 6
39 
40 struct __lk_invoke_t
41 {
42  void *__pinvoke;
43  void *__hiter;
44  void *__errbuf;
45  void *__sbuf;
46  void *__callargvec;
47  void *__callresult;
48 
49  int (*doc_mode)( struct __lk_invoke_t* );
50  void (*document)( struct __lk_invoke_t*, const char *fn, const char *desc, const char *sig );
51  void (*document2)( struct __lk_invoke_t*, const char *fn, const char *notes,
52  const char *desc1, const char *sig1,
53  const char *desc2, const char *sig2 );
54  void (*document3)( struct __lk_invoke_t*, const char *fn, const char *notes,
55  const char *desc1, const char *sig1,
56  const char *desc2, const char *sig2,
57  const char *desc3, const char *sig3);
58 
59  void(*error)(struct __lk_invoke_t*, const char *);
60  int(*arg_count)(struct __lk_invoke_t*);
61  lk_var_t(*arg)(struct __lk_invoke_t*, int);
62 
63  int(*type)(struct __lk_invoke_t*, lk_var_t);
64  const char *(*as_string)(struct __lk_invoke_t*, lk_var_t); // returns utf8
65  double(*as_number)(struct __lk_invoke_t*, lk_var_t);
66  int(*as_integer)(struct __lk_invoke_t*, lk_var_t);
67  int(*as_boolean)(struct __lk_invoke_t*, lk_var_t);
68 
69  int(*vec_count)(struct __lk_invoke_t*, lk_var_t);
70  lk_var_t(*vec_index)(struct __lk_invoke_t*, lk_var_t, int);
71 
72  int(*tab_count) (struct __lk_invoke_t*, lk_var_t);
73  const char * (*tab_first_key)(struct __lk_invoke_t*, lk_var_t);
74  const char * (*tab_next_key)(struct __lk_invoke_t*, lk_var_t);
75  lk_var_t(*tab_value)(struct __lk_invoke_t*, lk_var_t, const char *);
76 
77  lk_var_t(*result)(struct __lk_invoke_t*);
78 
79  // variable modifications
80  void(*set_null) (struct __lk_invoke_t*, lk_var_t);
81  void(*set_string)(struct __lk_invoke_t*, lk_var_t, const char *); // values are utf8
82  void(*set_number)(struct __lk_invoke_t*, lk_var_t, double);
83 
84  void(*set_number_vec)(struct __lk_invoke_t*, lk_var_t, double *, int);
85  void(*make_vec)(struct __lk_invoke_t*, lk_var_t);
86  void(*reserve)(struct __lk_invoke_t*, lk_var_t, int len);
87  lk_var_t(*append_number)(struct __lk_invoke_t*, lk_var_t, double);
88  lk_var_t(*append_string)(struct __lk_invoke_t*, lk_var_t, const char *);
89  lk_var_t(*append_null)(struct __lk_invoke_t*, lk_var_t);
90 
91  void(*make_tab)(struct __lk_invoke_t*, lk_var_t);
92  lk_var_t(*tab_set_number)(struct __lk_invoke_t*, lk_var_t, const char *, double);
93  lk_var_t(*tab_set_string)(struct __lk_invoke_t*, lk_var_t, const char *, const char *);
94  lk_var_t(*tab_set_null)(struct __lk_invoke_t*, lk_var_t, const char *);
95 
96  // creating, querying, destroying externally defined object types
97  int(*insert_object)(struct __lk_invoke_t*, const char *type, void*, void(*)(void *, void *), void *);
98  void *(*query_object)(struct __lk_invoke_t*, int);
99  void(*destroy_object)(struct __lk_invoke_t*, int);
100 
101  // invoking other functions in LK environment (i.e. for callbacks)
102  void(*clear_call_args)(struct __lk_invoke_t*);
103  lk_var_t(*append_call_arg)(struct __lk_invoke_t*);
104  lk_var_t(*call_result)(struct __lk_invoke_t*);
105  const char *(*call)(struct __lk_invoke_t*, const char *name); // returns 0 on success, error message otherwise.
106  };
107 
108  // function table must look like
109  typedef void(*lk_invokable)(struct __lk_invoke_t *);
110 
111 #define LK_FUNCTION( name ) void name( struct __lk_invoke_t *lk )
112 
113 #define LK_DOCUMENT( fn, desc, sig ) if (lk->doc_mode(lk)) { lk->document( lk, fn, desc, sig ); return; }
114 #define LK_DOCUMENT2( fn, notes, desc1, sig1, desc2, sig2 ) if (lk->doc_mode(lk)) { lk->document2( lk, fn, notes, desc1, sig1, desc2, sig2 ); return; }
115 #define LK_DOCUMENT3( fn, notes, desc1, sig1, desc2, sig2, desc3, sig3 ) if (lk->doc_mode(lk)) { lk->document3( lk, fn, notes, desc1, sig1, desc2, sig2, desc3, sig3 ); return; }
116 
117  // helper access functions
118 
119 #define lk_error( msg ) lk->error(lk, msg)
120 #define lk_arg_count( ) lk->arg_count(lk)
121 #define lk_arg( idx ) lk->arg(lk, idx)
122 #define lk_type( var ) lk->type(lk, var)
123 #define lk_as_string( var ) lk->as_string(lk, var)
124 #define lk_as_number( var ) lk->as_number(lk, var)
125 #define lk_as_integer( var ) lk->as_integer(lk, var)
126 #define lk_as_boolean( var ) lk->as_boolean(lk, var)
127 #define lk_length( var ) lk->vec_count(lk, var)
128 #define lk_index( var, idx ) lk->vec_index(lk, var, idx)
129 #define lk_table_size( var ) lk->tab_count(lk, var)
130 #define lk_first_key( var ) lk->tab_first_key(lk, var)
131 #define lk_next_key( var ) lk->tab_next_key(lk, var)
132 #define lk_value( var, key ) lk->tab_value(lk, var, key)
133 #define lk_result( ) lk->result(lk)
134 #define lk_return_number( val ) lk->set_number(lk, lk->result(lk), val )
135 #define lk_return_string( str ) lk->set_string(lk, lk->result(lk), str )
136 #define lk_set_null( var ) lk->set_null(lk, var)
137 #define lk_set_string( var, str ) lk->set_string(lk, var, str)
138 #define lk_set_number( var, val ) lk->set_number(lk, var, val)
139 #define lk_set_number_array( var, arr, len ) lk->set_number_vec(lk, var, arr, len)
140 #define lk_make_array( var ) lk->make_vec(lk)
141 #define lk_reserve( var, len ) lk->reserve(lk, var, len)
142 #define lk_append_number( var, val ) lk->append_number(lk, var, val)
143 #define lk_append_string( var, str ) lk->append_string(lk, var, str)
144 #define lk_append_null( var ) lk->append_null(lk, var)
145 #define lk_make_table( var ) lk->make_tab(lk, var)
146 #define lk_table_set_number( var, key, val ) lk->tab_set_number( lk, var, key, val )
147 #define lk_table_set_string( var, key, str ) lk->tab_set_string( lk, var, key, str )
148 #define lk_table_set_null( var, key ) lk->tab_set_null(lk, var, key)
149 #define lk_insert_object( type, obj, freefunc, cbdata ) lk->insert_object(lk, type, obj, freefunc, cbdata)
150 #define lk_query_object( handle ) lk->query_object(lk, handle)
151 #define lk_destroy_object( handle ) lk->destroy_object(lk, handle)
152 #define lk_clear_call_args( ) lk->clear_call_args(lk)
153 #define lk_append_call_arg( ) lk->append_call_arg(lk)
154 #define lk_call_result( ) lk->call_result(lk)
155 #define lk_call( name ) lk->call(lk,name)
156 
157  // DLL must export 2 functions:
158  // int lk_extension_api_version()
159  // lk_invokable *lk_function_list()
160 
161 #define LK_EXTENSION_API_VERSION 1003
162 
163 #if defined(__WINDOWS__)||defined(WIN32)||defined(_WIN32)||defined(__MINGW___)||defined(_MSC_VER)
164 #define LKAPIEXPORT __declspec(dllexport)
165 #else
166 #define LKAPIEXPORT
167 #endif
168 
169 #define LK_BEGIN_EXTENSION() \
170  LKAPIEXPORT int lk_extension_api_version() \
171  { return LK_EXTENSION_API_VERSION; } \
172  LKAPIEXPORT lk_invokable *lk_function_list() { \
173  static lk_invokable _ll[] = {
174 #define LK_END_EXTENSION() ,0 }; return _ll; }
175 
176  /* examples: (these two functions are identical)
177 
178  void mean_func( struct __lk_invoke_t *lk )
179  {
180  LK_DOCUMENT( "mean", "Returns the average of an array of numbers.", "(array):number" );
181 
182  lk->set_number( lk->result(lk), 1.3 );
183  }
184 
185  LK_FUNCTION( mean_func2 )
186  {
187  LK_DOCUMENT( "mean2", "Returns the average of an array of numbers.", "(array):number" );
188 
189  lk_return_number( 1.3 );
190  }
191 
192  LK_BEGIN_EXTENSION()
193  mean_func, sigma_func, sum_func,
194  xmult_func, average_func
195  LK_END_EXTENSION()
196 
197  */
198 
199 #ifdef __cplusplus
200 }
201 #endif
202 
203 #endif
-
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Sustainable Energy, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_lex_h
26 #define __lk_lex_h
27 
28 #include <cstdio>
29 
30 #include <lk/absyn.h>
31 
32 namespace lk {
33 
44  class input_base
45  {
46  public:
47  virtual ~input_base() { }
48  virtual char operator*() = 0;
49  virtual char peek() = 0;
50  virtual char operator++(int) = 0;
51  };
52 
53  class input_string : public input_base
54  {
55  protected:
56  char *m_buf;
57  char *m_p;
58  bool allocate(size_t n);
59  public:
60  input_string();
61  input_string(const lk_string &in);
62  virtual ~input_string();
63  virtual char operator*();
64  virtual char operator++(int);
65  virtual char peek();
66  };
67 
68  class input_file : public input_string
69  {
70  public:
71  input_file(const lk_string &file);
72  virtual ~input_file();
73  };
74 
75 
86  class lexer
87  {
88  public:
89  static const char *tokstr(int t);
93  enum {
94  INVALID,
95  END,
96  IDENTIFIER,
97  SPECIAL,
98  NUMBER,
99  LITERAL,
100 
110 
143 
144  };
145 
146  lexer(input_base &input);
147 
148  int next();
149 
150  lk_string text();
151  double value();
152 
153  int line();
154  lk_string error();
155 
156  private:
157  void whitespace();
158  bool comments();
159 
160  lk_string m_error;
161  int m_line;
162  lk_string m_buf;
163  double m_val;
164 
165  input_base &p;
166  };
167 };
168 
169 #endif
-@
Definition: lex.h:142
+
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Energy Innovation, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_lex_h
26 #define __lk_lex_h
27 
28 #include <cstdio>
29 
30 #include <lk/absyn.h>
31 
32 namespace lk {
33 
44  class input_base
45  {
46  public:
47  virtual ~input_base() { }
48  virtual char operator*() = 0;
49  virtual char peek() = 0;
50  virtual char operator++(int) = 0;
51  };
52 
53  class input_string : public input_base
54  {
55  protected:
56  char *m_buf;
57  char *m_p;
58  bool allocate(size_t n);
59  public:
60  input_string();
61  input_string(const lk_string &in);
62  virtual ~input_string();
63  virtual char operator*();
64  virtual char operator++(int);
65  virtual char peek();
66  };
67 
68  class input_file : public input_string
69  {
70  public:
71  input_file(const lk_string &file);
72  virtual ~input_file();
73  };
74 
75 
86  class lexer
87  {
88  public:
89  static const char *tokstr(int t);
93  enum {
94  INVALID,
95  END,
96  IDENTIFIER,
97  SPECIAL,
98  NUMBER,
99  LITERAL,
100 
110 
143 
144  };
145 
146  lexer(input_base &input);
147 
148  int next();
149 
150  lk_string text();
151  double value();
152 
153  int line();
154  lk_string error();
155 
156  private:
157  void whitespace();
158  bool comments();
159 
160  lk_string m_error;
161  int m_line;
162  lk_string m_buf;
163  double m_val;
164 
165  input_base &p;
166  };
167 };
168 
169 #endif
-@
Definition: lex.h:142
int next()
finds the next token, moves the characters to m_buf and returns token type
Definition: lex.cpp:208
->
Definition: lex.h:129
(
Definition: lex.h:104
diff --git a/doc/html/md__c_1__projects_lk__l_i_c_e_n_s_e.html b/doc/html/md__c_1__projects_lk__l_i_c_e_n_s_e.html index e9f9766a..71dd2427 100644 --- a/doc/html/md__c_1__projects_lk__l_i_c_e_n_s_e.html +++ b/doc/html/md__c_1__projects_lk__l_i_c_e_n_s_e.html @@ -89,7 +89,7 @@
LICENSE
-

LK, Copyright (c) 2008-2017, Alliance for Sustainable Energy, LLC. All rights reserved.

+

LK, Copyright (c) 2008-2017, Alliance for Energy Innovation, LLC. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

(1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

(2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

diff --git a/doc/html/parse_8h_source.html b/doc/html/parse_8h_source.html index 4c7ec2bc..f9bb6fb7 100644 --- a/doc/html/parse_8h_source.html +++ b/doc/html/parse_8h_source.html @@ -89,7 +89,7 @@
parse.h
-
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Sustainable Energy, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_parse_h
26 #define __lk_parse_h
27 
28 #include <vector>
29 #include <lk/lex.h>
30 #include <lk/absyn.h>
31 
32 namespace lk
33 {
34 
44  class parser
45  {
46  public:
47  parser(input_base &input, const lk_string &name = "");
48 
49  void add_search_path(const lk_string &path) { m_searchPaths.push_back(path); }
50  void add_search_paths(const std::vector<lk_string> &paths);
51  std::vector<lk_string> get_search_paths() const { return m_searchPaths; }
52 
53  node_t *script();
54  node_t *block();
55  node_t *statement();
56  node_t *test();
57  node_t *enumerate();
58  node_t *loop();
59  node_t *define();
60  node_t *assignment();
61  node_t *ternary();
62  node_t *logicalor();
63  node_t *logicaland();
64  node_t *equality();
65  node_t *relational();
66  node_t *additive();
67  node_t *multiplicative();
68  node_t *exponential();
69  node_t *unary();
70  node_t *postfix();
71  node_t *primary();
72 
73  srcpos_t srcpos();
74  int line() { return lex.line(); }
75  int error_count() { return m_errorList.size(); }
76  lk_string error(int idx, int *line = 0);
77 
78  int token(); // describes what type the current statement should be treated as
79  bool token(int t);
80 
81  void skip(); // moves through input, updating positions
82  bool match(int t);
83  bool match( const char *s );
84 
85  private:
86  list_t *ternarylist( int septok, int endtok );
87  list_t *identifierlist( int septok, int endtok );
88 
89 
90  void error( const lk_string &s );
91  void error( const char *fmt, ... );
92 
93  lexer lex;
94  int m_tokType;
95  int m_lastLine;
96  int m_lastStmt;
97  int m_lastBlockEnd;
98  lk_string m_lexError;
99  bool m_haltFlag;
100  struct errinfo { int line; lk_string text; };
101  std::vector<errinfo> m_errorList;
102  std::vector< lk_string > m_importNameList, m_searchPaths;
103  lk_string m_name;
104  };
105 };
106 
107 #endif
node_t * script()
entry point for parsing a lk script, returns root node of tree
Definition: parse.cpp:158
+
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Energy Innovation, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_parse_h
26 #define __lk_parse_h
27 
28 #include <vector>
29 #include <lk/lex.h>
30 #include <lk/absyn.h>
31 
32 namespace lk
33 {
34 
44  class parser
45  {
46  public:
47  parser(input_base &input, const lk_string &name = "");
48 
49  void add_search_path(const lk_string &path) { m_searchPaths.push_back(path); }
50  void add_search_paths(const std::vector<lk_string> &paths);
51  std::vector<lk_string> get_search_paths() const { return m_searchPaths; }
52 
53  node_t *script();
54  node_t *block();
55  node_t *statement();
56  node_t *test();
57  node_t *enumerate();
58  node_t *loop();
59  node_t *define();
60  node_t *assignment();
61  node_t *ternary();
62  node_t *logicalor();
63  node_t *logicaland();
64  node_t *equality();
65  node_t *relational();
66  node_t *additive();
67  node_t *multiplicative();
68  node_t *exponential();
69  node_t *unary();
70  node_t *postfix();
71  node_t *primary();
72 
73  srcpos_t srcpos();
74  int line() { return lex.line(); }
75  int error_count() { return m_errorList.size(); }
76  lk_string error(int idx, int *line = 0);
77 
78  int token(); // describes what type the current statement should be treated as
79  bool token(int t);
80 
81  void skip(); // moves through input, updating positions
82  bool match(int t);
83  bool match( const char *s );
84 
85  private:
86  list_t *ternarylist( int septok, int endtok );
87  list_t *identifierlist( int septok, int endtok );
88 
89 
90  void error( const lk_string &s );
91  void error( const char *fmt, ... );
92 
93  lexer lex;
94  int m_tokType;
95  int m_lastLine;
96  int m_lastStmt;
97  int m_lastBlockEnd;
98  lk_string m_lexError;
99  bool m_haltFlag;
100  struct errinfo { int line; lk_string text; };
101  std::vector<errinfo> m_errorList;
102  std::vector< lk_string > m_importNameList, m_searchPaths;
103  lk_string m_name;
104  };
105 };
106 
107 #endif
node_t * script()
entry point for parsing a lk script, returns root node of tree
Definition: parse.cpp:158
parser(input_base &input, const lk_string &name="")
initializes a parser and lexer; stores reference to input, initializes values and determines first to...
Definition: parse.cpp:32
Contains functions used to take an lk script as input_bases and to parse them using lexer...
Definition: parse.h:44
Forms nodes of recursive descent tree.
Definition: absyn.h:118
diff --git a/doc/html/stdlib_8h_source.html b/doc/html/stdlib_8h_source.html index 3f5c6964..4ae8a830 100644 --- a/doc/html/stdlib_8h_source.html +++ b/doc/html/stdlib_8h_source.html @@ -89,7 +89,7 @@
stdlib.h
-
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Sustainable Energy, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_stdlib_h
26 #define __lk_stdlib_h
27 
28 #include <lk/env.h>
29 
30 namespace lk {
31  /* these stdlib_xxxx() functions
32  return an array of fcall_t references.
33  the end of the list is denoted by a null fcall_t */
34  fcall_t* stdlib_basic();
35  fcall_t* stdlib_sysio();
36  fcall_t* stdlib_string();
37  fcall_t* stdlib_math();
38 
39  fcall_t* stdlib_thread();
40 #ifdef LK_USE_WXWIDGETS
41  fcall_t* stdlib_wxui();
42 #endif
43 
44  bool tex_doc(const lk_string &file,
45  const lk_string &title,
46  fcall_t *lib);
47  lk_string html_doc(const lk_string &title, fcall_t *lib);
48  lk_string html_doc(fcall_t f);
49 
50  lk_string json_write(const lk::vardata_t &x);
51  bool json_read(const lk_string &json, lk::vardata_t &x, lk_string *err = 0);
52 
53  std::vector< lk_string > dir_list(const lk_string &dir, const lk_string &extlist, bool ret_dirs = false);
54 
55  std::vector< lk_string > split(const lk_string &str, const lk_string &delim, bool ret_empty = false, bool ret_delim = false);
56  lk_string join(const std::vector< lk_string > &list, const lk_string &delim);
57 
58  bool to_integer(const lk_string &str, int *x);
59  bool to_float(const lk_string &str, float *x);
60  bool to_double(const lk_string &str, double *x);
61 
62  lk_string lower_case(const lk_string &in);
63  lk_string upper_case(const lk_string &in);
64 
65  size_t replace(lk_string &s, const lk_string &old_text, const lk_string &new_text);
66 
67  lk_string read_file(const lk_string &file);
68  bool read_line(FILE *fp, lk_string &text, int prealloc = 256);
69 
70  bool rename_file(const lk_string &f0, const lk_string &f1);
71  bool file_exists(const char *file);
72  bool dir_exists(const char *path);
73  bool remove_file(const char *path);
74  bool mkdir(const char *path, bool make_full = false);
75  lk_string path_only(const lk_string &path);
76  lk_string name_only(const lk_string &path);
77  lk_string ext_only(const lk_string &path);
78  char path_separator();
79  lk_string get_cwd();
80  bool set_cwd(const lk_string &path);
81 
82  class sync_piped_process
83  {
84  public:
85  sync_piped_process() { }
86  virtual ~sync_piped_process() { }
87 
88  int spawn(const lk_string &command, const lk_string &workdir = "");
89  virtual void on_stdout(const lk_string &line_text) = 0;
90  };
91 
92  lk_string trim_to_columns(const lk_string &str, int numcols);
93  lk_string format_vl(const lk_string &fmt, const std::vector< vardata_t* > &args);
94  lk_string format(const char *fmt, ...);
95  size_t format_vn(char *buffer, int maxlen, const char *fmt, va_list arglist);
96 
97  double besj0(double x);
98  double besj1(double x);
99  double besy0(double x);
100  double besy1(double x);
101  double besi0(double x);
102  double besk0(double x);
103  double besi1(double x);
104  double besk1(double x);
105 
106  double gammln(double xx);
107  double betacf(double a, double b, double x) throw(lk::error_t);
108  double betai(double a, double b, double x) throw(lk::error_t);
109 
110  double pearson(double *x, double *y, size_t len);
111 
112  void gser(double *gamser, double a, double x, double *gln);
113  void gcf(double *gammcf, double a, double x, double *gln);
114  double gammp(double a, double x);
115  double gammq(double a, double x);
116  double erf(double x);
117  double erfc(double x);
118 };
119 
120 #endif
Definition: absyn.h:66
+
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Energy Innovation, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_stdlib_h
26 #define __lk_stdlib_h
27 
28 #include <lk/env.h>
29 
30 namespace lk {
31  /* these stdlib_xxxx() functions
32  return an array of fcall_t references.
33  the end of the list is denoted by a null fcall_t */
34  fcall_t* stdlib_basic();
35  fcall_t* stdlib_sysio();
36  fcall_t* stdlib_string();
37  fcall_t* stdlib_math();
38 
39  fcall_t* stdlib_thread();
40 #ifdef LK_USE_WXWIDGETS
41  fcall_t* stdlib_wxui();
42 #endif
43 
44  bool tex_doc(const lk_string &file,
45  const lk_string &title,
46  fcall_t *lib);
47  lk_string html_doc(const lk_string &title, fcall_t *lib);
48  lk_string html_doc(fcall_t f);
49 
50  lk_string json_write(const lk::vardata_t &x);
51  bool json_read(const lk_string &json, lk::vardata_t &x, lk_string *err = 0);
52 
53  std::vector< lk_string > dir_list(const lk_string &dir, const lk_string &extlist, bool ret_dirs = false);
54 
55  std::vector< lk_string > split(const lk_string &str, const lk_string &delim, bool ret_empty = false, bool ret_delim = false);
56  lk_string join(const std::vector< lk_string > &list, const lk_string &delim);
57 
58  bool to_integer(const lk_string &str, int *x);
59  bool to_float(const lk_string &str, float *x);
60  bool to_double(const lk_string &str, double *x);
61 
62  lk_string lower_case(const lk_string &in);
63  lk_string upper_case(const lk_string &in);
64 
65  size_t replace(lk_string &s, const lk_string &old_text, const lk_string &new_text);
66 
67  lk_string read_file(const lk_string &file);
68  bool read_line(FILE *fp, lk_string &text, int prealloc = 256);
69 
70  bool rename_file(const lk_string &f0, const lk_string &f1);
71  bool file_exists(const char *file);
72  bool dir_exists(const char *path);
73  bool remove_file(const char *path);
74  bool mkdir(const char *path, bool make_full = false);
75  lk_string path_only(const lk_string &path);
76  lk_string name_only(const lk_string &path);
77  lk_string ext_only(const lk_string &path);
78  char path_separator();
79  lk_string get_cwd();
80  bool set_cwd(const lk_string &path);
81 
82  class sync_piped_process
83  {
84  public:
85  sync_piped_process() { }
86  virtual ~sync_piped_process() { }
87 
88  int spawn(const lk_string &command, const lk_string &workdir = "");
89  virtual void on_stdout(const lk_string &line_text) = 0;
90  };
91 
92  lk_string trim_to_columns(const lk_string &str, int numcols);
93  lk_string format_vl(const lk_string &fmt, const std::vector< vardata_t* > &args);
94  lk_string format(const char *fmt, ...);
95  size_t format_vn(char *buffer, int maxlen, const char *fmt, va_list arglist);
96 
97  double besj0(double x);
98  double besj1(double x);
99  double besy0(double x);
100  double besy1(double x);
101  double besi0(double x);
102  double besk0(double x);
103  double besi1(double x);
104  double besk1(double x);
105 
106  double gammln(double xx);
107  double betacf(double a, double b, double x) throw(lk::error_t);
108  double betai(double a, double b, double x) throw(lk::error_t);
109 
110  double pearson(double *x, double *y, size_t len);
111 
112  void gser(double *gamser, double a, double x, double *gln);
113  void gcf(double *gammcf, double a, double x, double *gln);
114  double gammp(double a, double x);
115  double gammq(double a, double x);
116  double erf(double x);
117  double erfc(double x);
118 };
119 
120 #endif
Definition: absyn.h:66
Vardata_t form the execution stack of the vm: stores identifiers and expressions, arguments and resul...
Definition: env.h:87
diff --git a/doc/html/vm_8h_source.html b/doc/html/vm_8h_source.html index f8cf8b80..e492402d 100644 --- a/doc/html/vm_8h_source.html +++ b/doc/html/vm_8h_source.html @@ -89,7 +89,7 @@
vm.h
-
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Sustainable Energy, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_vm_h
26 #define __lk_vm_h
27 
28 #include <lk/absyn.h>
29 #include <lk/env.h>
30 
31 namespace lk {
32 
37 enum Opcode {
38  ADD, SUB, MUL, DIV, LT, GT, LE, GE, NE, EQ, INC, DEC, OR, AND, NOT, NEG, EXP,
39  PSH,
40  POP,
41  DUP, NUL, ARG, SWI,
42  J,
43  JF, JT, IDX, KEY, MAT, WAT, SET, GET, WR,
44  RREF,
45  LREF,
46  LCREF,
47  LGREF,
48  FREF, CALL, TCALL, RET, END, SZ, KEYS, TYP, VEC, HASH,
49  __MaxOp };
50 struct OpCodeEntry { Opcode op; const char *name; };
51 extern OpCodeEntry op_table[];
52 
61 struct bytecode
62 {
63  std::vector<unsigned int> program;
64  std::vector<vardata_t> constants;
65  std::vector<lk_string> identifiers;
66  std::vector<srcpos_t> debuginfo;
67 };
68 
69 #define OP_PROFILE 1
70 
71 // takes bytecode as input
72 
80 class vm
81 {
82 public:
83 
93  struct frame {
94  frame( lk::env_t *parent, size_t fptr, size_t ret, size_t na )
95  : env( parent), fp(fptr), retaddr(ret), nargs(na), iarg(0), thiscall( false )
96  {
97  }
98 
99  lk::env_t env;
100  size_t fp;
101  size_t retaddr;
102  size_t nargs;
103  size_t iarg;
104  bool thiscall;
105  lk_string id;
106  };
107 
108 private:
109  size_t ip;
110  int sp;
111  std::vector< vardata_t > stack;
112 
113  bytecode *bc;
114  /*
115  std::vector< unsigned int > program;
116  std::vector< vardata_t > constants;
117  std::vector< lk_string > identifiers;
118  std::vector< srcpos_t > debuginfo;
119  */
120 
121  std::vector< frame* > frames;
122  std::vector< bool > brkpt;
123 
124  lk_string errStr;
125  srcpos_t lastbrk;
126 
127  void free_frames();
128  bool error( const char *fmt, ... );
129 
130 
132 #ifdef OP_PROFILE
133  size_t opcount[__MaxOp];
134  void clear_opcount();
135 #endif
136 
137 public:
138  enum ExecMode {
139  NORMAL,
140  DEBUG,
141  STEP,
142  SINGLE
143  };
144 
145  vm( size_t ssize = 4096 );
146  virtual ~vm();
147 
148  bool initialize( lk::env_t *env );
149  bool run( ExecMode mode = NORMAL );
150  lk_string error() { return errStr; }
151  virtual bool on_run( const srcpos_t &spos);
152 
153  void clrbrk();
154  int setbrk( int line, const lk_string &file );
155  std::vector<srcpos_t> getbrk();
156 
157  size_t get_ip() { return ip; }
158  frame **get_frames( size_t *nfrm );
159  vardata_t *get_stack( size_t *psp );
160 
161  void load( bytecode *b );
162  bytecode *get_bytecode() { return bc; }
163 
164  virtual bool special_set( const lk_string &name, vardata_t &val );
165  virtual bool special_get( const lk_string &name, vardata_t &val );
166 
167 #ifdef OP_PROFILE
168  void get_opcount( size_t iop[__MaxOp] );
169 #endif
170 
171 };
172 
173 } // namespace lk
174 
175 #endif
Stores instruction stack information.
Definition: vm.h:61
+
1 /***********************************************************************************************************************
2 * LK, Copyright (c) 2008-2017, Alliance for Energy Innovation, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5 * following conditions are met:
6 *
7 * (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8 * disclaimer.
9 *
10 * (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 *
13 * (3) Neither the name of the copyright holder nor the names of any contributors may be used to endorse or promote
14 * products derived from this software without specific prior written permission from the respective party.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, THE UNITED STATES GOVERNMENT, OR ANY CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 **********************************************************************************************************************/
24 
25 #ifndef __lk_vm_h
26 #define __lk_vm_h
27 
28 #include <lk/absyn.h>
29 #include <lk/env.h>
30 
31 namespace lk {
32 
37 enum Opcode {
38  ADD, SUB, MUL, DIV, LT, GT, LE, GE, NE, EQ, INC, DEC, OR, AND, NOT, NEG, EXP,
39  PSH,
40  POP,
41  DUP, NUL, ARG, SWI,
42  J,
43  JF, JT, IDX, KEY, MAT, WAT, SET, GET, WR,
44  RREF,
45  LREF,
46  LCREF,
47  LGREF,
48  FREF, CALL, TCALL, RET, END, SZ, KEYS, TYP, VEC, HASH,
49  __MaxOp };
50 struct OpCodeEntry { Opcode op; const char *name; };
51 extern OpCodeEntry op_table[];
52 
61 struct bytecode
62 {
63  std::vector<unsigned int> program;
64  std::vector<vardata_t> constants;
65  std::vector<lk_string> identifiers;
66  std::vector<srcpos_t> debuginfo;
67 };
68 
69 #define OP_PROFILE 1
70 
71 // takes bytecode as input
72 
80 class vm
81 {
82 public:
83 
93  struct frame {
94  frame( lk::env_t *parent, size_t fptr, size_t ret, size_t na )
95  : env( parent), fp(fptr), retaddr(ret), nargs(na), iarg(0), thiscall( false )
96  {
97  }
98 
99  lk::env_t env;
100  size_t fp;
101  size_t retaddr;
102  size_t nargs;
103  size_t iarg;
104  bool thiscall;
105  lk_string id;
106  };
107 
108 private:
109  size_t ip;
110  int sp;
111  std::vector< vardata_t > stack;
112 
113  bytecode *bc;
114  /*
115  std::vector< unsigned int > program;
116  std::vector< vardata_t > constants;
117  std::vector< lk_string > identifiers;
118  std::vector< srcpos_t > debuginfo;
119  */
120 
121  std::vector< frame* > frames;
122  std::vector< bool > brkpt;
123 
124  lk_string errStr;
125  srcpos_t lastbrk;
126 
127  void free_frames();
128  bool error( const char *fmt, ... );
129 
130 
132 #ifdef OP_PROFILE
133  size_t opcount[__MaxOp];
134  void clear_opcount();
135 #endif
136 
137 public:
138  enum ExecMode {
139  NORMAL,
140  DEBUG,
141  STEP,
142  SINGLE
143  };
144 
145  vm( size_t ssize = 4096 );
146  virtual ~vm();
147 
148  bool initialize( lk::env_t *env );
149  bool run( ExecMode mode = NORMAL );
150  lk_string error() { return errStr; }
151  virtual bool on_run( const srcpos_t &spos);
152 
153  void clrbrk();
154  int setbrk( int line, const lk_string &file );
155  std::vector<srcpos_t> getbrk();
156 
157  size_t get_ip() { return ip; }
158  frame **get_frames( size_t *nfrm );
159  vardata_t *get_stack( size_t *psp );
160 
161  void load( bytecode *b );
162  bytecode *get_bytecode() { return bc; }
163 
164  virtual bool special_set( const lk_string &name, vardata_t &val );
165  virtual bool special_get( const lk_string &name, vardata_t &val );
166 
167 #ifdef OP_PROFILE
168  void get_opcount( size_t iop[__MaxOp] );
169 #endif
170 
171 };
172 
173 } // namespace lk
174 
175 #endif
Stores instruction stack information.
Definition: vm.h:61
Definition: absyn.h:66
Stores stack frames for vm, default one contains global variables.
Definition: vm.h:93
Records position in source script.
Definition: absyn.h:96
diff --git a/include/lk/absyn.h b/include/lk/absyn.h index 77055a72..614a666d 100644 --- a/include/lk/absyn.h +++ b/include/lk/absyn.h @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/include/lk/codegen.h b/include/lk/codegen.h index 1c3ab8ba..0a46dea2 100644 --- a/include/lk/codegen.h +++ b/include/lk/codegen.h @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/include/lk/env.h b/include/lk/env.h index d44deca6..31a414c9 100644 --- a/include/lk/env.h +++ b/include/lk/env.h @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/include/lk/eval.h b/include/lk/eval.h index 7f73abbb..8fb37f1e 100644 --- a/include/lk/eval.h +++ b/include/lk/eval.h @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/include/lk/invoke.h b/include/lk/invoke.h index b4f0a6b3..f6b97416 100644 --- a/include/lk/invoke.h +++ b/include/lk/invoke.h @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/include/lk/lex.h b/include/lk/lex.h index bf02253a..081f137a 100644 --- a/include/lk/lex.h +++ b/include/lk/lex.h @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/include/lk/parse.h b/include/lk/parse.h index 2d069640..82785105 100644 --- a/include/lk/parse.h +++ b/include/lk/parse.h @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/include/lk/stdlib.h b/include/lk/stdlib.h index f42117a4..53a8597a 100644 --- a/include/lk/stdlib.h +++ b/include/lk/stdlib.h @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/include/lk/vm.h b/include/lk/vm.h index cbc38a3b..fb3d722c 100644 --- a/include/lk/vm.h +++ b/include/lk/vm.h @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/sandbox/sandbox.cpp b/sandbox/sandbox.cpp index ee0c83c0..49cef7ad 100644 --- a/sandbox/sandbox.cpp +++ b/sandbox/sandbox.cpp @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/src/absyn.cpp b/src/absyn.cpp index c4f20b26..866a0fb5 100644 --- a/src/absyn.cpp +++ b/src/absyn.cpp @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/src/codegen.cpp b/src/codegen.cpp index 805909b3..1c51bee0 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/src/env.cpp b/src/env.cpp index 009c2184..958b8b5b 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/src/eval.cpp b/src/eval.cpp index 62309802..a43fb78f 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/src/invoke.cpp b/src/invoke.cpp index 6510c560..6af060cf 100644 --- a/src/invoke.cpp +++ b/src/invoke.cpp @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/src/lex.cpp b/src/lex.cpp index 6ef31bfe..66b984c4 100644 --- a/src/lex.cpp +++ b/src/lex.cpp @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/src/parse.cpp b/src/parse.cpp index cebada88..1caab002 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/src/stdlib.cpp b/src/stdlib.cpp index 109bc200..c8ef89ac 100644 --- a/src/stdlib.cpp +++ b/src/stdlib.cpp @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/src/vm.cpp b/src/vm.cpp index ecbebdc1..45dfb1f1 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -1,7 +1,7 @@ /* BSD 3-Clause License -Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE +Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/lk/blob/develop/LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: