ztsdb
interp.hpp
Go to the documentation of this file.
1 // (C) 2016 Leonardo Silvestri
2 //
3 // This file is part of ztsdb.
4 //
5 // ztsdb is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // ztsdb is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with ztsdb. If not, see <http://www.gnu.org/licenses/>.
17 
18 
23 
24 
25 #ifndef INTERP_HPP
26 #define INTERP_HPP
27 
28 #include <string>
29 #include <stdexcept>
30 #include "ast.hpp"
31 #include "valuevar.hpp"
32 #include "env.hpp"
33 
34 
36 namespace zcore {
37  struct InterpCtx;
38 }
40 
41 
43 namespace interp {
44 
49  struct Kont {
50 
57  enum AssignType : uint64_t {
58  NORMAL = 0x00000000,
59  GLOBAL = 0x00000001,
60  ELLIPSIS = 0x00000010,
61  WHILE = 0x00000100,
63  COND = 0x00001000,
64  ARG = 0x00010000,
65  SILENT = 0x00100000,
66  END = 0x01000000,
67  REF = 0x10000000
68  };
69 
70  Kont() = delete;
71 
72  const E* var;
73  const E* control;
76  shared_ptr<BaseFrame> r;
77  shared_ptr<Kont> next;
79  uint64_t atype;
80 
83  inline operator string() const {
84  stringstream ss;
85 
86  // Print out all the assignment flags.
87  auto atype_to_string = [](uint64_t a) {
88  string s;
89  if (a == AssignType::NORMAL) {
90  s = "N";
91  }
92  else {
93  if (a & GLOBAL) s += "G,";
94  if (a & ELLIPSIS) s += "E,";
95  if (a & WHILE) s += "W,";
96  if (a & COND) s += "C,";
97  if (a & ARG) s += "A,";
98  if (a & SILENT) s += "S,";
99  if (a & END) s += "END,";
100  if (a & REF) s += "REF,";
101  if (s.length()) {
102  s = s.substr(0, s.length() - 1);
103  }
104  }
105  return s; };
106 
107  ss << (next && next->var ? to_string(*next->var) : "") << "="
108  << (control ? to_string(*control) : "nullptr")
109  << " ["
110  << " a:" << atype_to_string(atype)
111  << " r:" << r << dec
112  << "]"
113  << " -> " << (next ? string(*next) : "nullptr");
114  return ss.str();
115  }
116  };
117 
125  shared_ptr<Kont> step(shared_ptr<Kont>& k,
126  vector<shpfrm>& frameStack,
127  zcore::InterpCtx& ic);
128 
129 
130  shared_ptr<Kont> buildElChain(const ElNode* eln,
131  unsigned n,
132  shared_ptr<BaseFrame> r,
133  shared_ptr<Kont>& k);
134 
135 } // end namespace interp
136 
137 
138 #endif
E
Definition: ast.hpp:76
interp::Kont::next
shared_ptr< Kont > next
Next continuation.
Definition: interp.hpp:78
interp::Kont::ARG
@ ARG
result is the eval of a function arg
Definition: interp.hpp:64
interp::Kont::WHILE
@ WHILE
result is the eval of a while loop
Definition: interp.hpp:62
interp::Kont::REF
@ REF
indicates the pass-by-reference
Definition: interp.hpp:67
ElNode
Definition: ast.hpp:136
interp::Kont::ELLIPSIS
@ ELLIPSIS
Definition: interp.hpp:60
interp::Kont::NORMAL
@ NORMAL
assign in the local frame
Definition: interp.hpp:58
interp::Kont::atype
uint64_t atype
Assignment type.
Definition: interp.hpp:79
interp::Kont::control
const E * control
The expression to evaluate.
Definition: interp.hpp:75
interp::Kont::COND
@ COND
result is the eval of a while condition
Definition: interp.hpp:63
zcore::InterpCtx
Definition: interp_ctx.hpp:129
interp::Kont::SILENT
@ SILENT
result of assignment will not be printed
Definition: interp.hpp:65
interp::Kont::AssignType
AssignType
Definition: interp.hpp:57
interp
Struct and functions implementing the interpreter.
Definition: env.hpp:36
interp::Kont::GLOBAL
@ GLOBAL
assign in the global frame
Definition: interp.hpp:59
interp::Kont::r
shared_ptr< BaseFrame > r
Definition: interp.hpp:76
interp::step
shared_ptr< Kont > step(shared_ptr< Kont > &k, vector< shpfrm > &frameStack, zcore::InterpCtx &ic)
Definition: interp.cpp:721
interp::Kont::END
@ END
indicates the last assignment
Definition: interp.hpp:66
interp::Kont
Definition: interp.hpp:49
interp::Kont::var
const E * var
Definition: interp.hpp:72