first impl of builtin member methods (thus far, string.size implemented. more to...
[aqualang.git] / include / jtc.h
blob504247d6e7bb043b07ba6dd5b116efee092a7fcf
2 #include <iostream>
3 #include <fstream>
4 #include <cctype>
5 #include <cmath>
6 #include <string>
7 #include <vector>
8 #include <array>
9 #include <functional>
10 #include <unordered_map>
11 #include <stdexcept>
12 #include <memory>
14 // utility
15 template<class T>
16 bool isint(T x)
18 return x == std::trunc(x);
20 std::string unescape(const std::string &str);
22 class Interpreter;
24 namespace Internal
26 enum TokenType
28 Tok_Number, Tok_Identifier, Tok_String,
29 Tok_LeftParen, Tok_RightParen, Tok_LeftBracket,
30 Tok_RightBracket, Tok_LeftBrace, Tok_RightBrace,
31 Tok_Plus, Tok_Dash, Tok_Star,
32 Tok_Percent, Tok_Slash, Tok_PlusEqual,
33 Tok_DashEqual, Tok_StarEqual, Tok_PercentEqual,
34 Tok_SlashEqual, Tok_Not, Tok_Comma,
35 Tok_Semicolon, Tok_Dot, Tok_Colon,
36 Tok_Equal, Tok_EqOp, Tok_NotEqual,
37 Tok_Greater, Tok_GreaterEqual, Tok_Less,
38 Tok_LessEqual, Tok_LogicalAnd, Tok_LogicalOr,
39 Tok_If, Tok_Else, Tok_While,
40 Tok_For, Tok_Return, Tok_Break,
41 Tok_Continue, Tok_Function, Tok_Expression,
42 Tok_Statement, Tok_Root, Tok_Nil,
43 Tok_Eval, Tok_ParseStatement, Tok_ParseExpression,
44 Tok_Global, Tok_Local, Tok_Print,
45 Tok_Type, Tok_Size, Tok_Switch,
46 Tok_Case, Tok_Import,
49 static const std::unordered_map<std::string, TokenType> keywords =
51 {"if", Tok_If},
52 {"else", Tok_Else},
53 {"while", Tok_While},
54 {"for", Tok_For},
55 {"return", Tok_Return},
56 {"break", Tok_Break},
57 {"continue", Tok_Continue},
58 {"func", Tok_Function},
59 {"nil", Tok_Nil},
60 {"expression", Tok_Expression},
61 {"statement", Tok_Statement},
62 {"eval", Tok_Eval},
63 {"parse_statement", Tok_ParseStatement},
64 {"parse_expression", Tok_ParseExpression},
65 {"global", Tok_Global},
66 {"local", Tok_Local},
67 {"root", Tok_Root},
68 {"type", Tok_Type},
69 {"size", Tok_Size},
70 // these aren't implemented yet
71 {"switch", Tok_Switch},
72 {"case", Tok_Case},
73 {"import", Tok_Import},
76 template<class Iter>
77 struct Token
79 const TokenType type;
80 const Iter begin;
81 const Iter end;
83 Token(TokenType type_, Iter begin_, Iter end_):
84 type(type_),
85 begin(begin_),
86 end(end_)
91 // helper to find line & column position on demand
92 template<class Iter>
93 std::pair<int, int> line_column(Iter begin, Iter pos)
95 int line = 1, column = 0;
96 for(Iter i = begin; i!=pos; ++i)
98 if(*i == '\n')
100 ++line;
101 column = 0;
103 ++column;
105 return std::make_pair(line, column);
108 template<class Iter>
109 std::shared_ptr<std::vector<Token<Iter>>> tokenize(Iter begin, Iter end)
111 auto tokens_ptr = std::make_shared<std::vector<Token<Iter>>>();
112 std::vector<Token<Iter>> &tokens = *tokens_ptr;
113 Iter i = begin;
114 while(i!=end)
116 Iter start = i;
117 switch(*i)
119 case ' ':
120 case '\t':
121 case '\n': // ignore whitespaces
122 ++i;
123 break;
124 case '(':
125 tokens.emplace_back(Tok_LeftParen, i, i+1);
126 ++i;
127 break;
128 case ')':
129 tokens.emplace_back(Tok_RightParen, i, i+1);
130 ++i;
131 break;
132 case '[':
133 tokens.emplace_back(Tok_LeftBracket, i, i+1);
134 ++i;
135 break;
136 case ']':
137 tokens.emplace_back(Tok_RightBracket, i, i+1);
138 ++i;
139 break;
140 case '{':
141 tokens.emplace_back(Tok_LeftBrace, i, i+1);
142 ++i;
143 break;
144 case '}':
145 tokens.emplace_back(Tok_RightBrace, i, i+1);
146 ++i;
147 break;
148 case ',':
149 tokens.emplace_back(Tok_Comma, i, i+1);
150 ++i;
151 break;
152 case '.':
153 tokens.emplace_back(Tok_Dot, i, i+1);
154 ++i;
155 break;
156 case ';':
157 tokens.emplace_back(Tok_Semicolon, i, i+1);
158 ++i;
159 break;
160 case ':':
161 tokens.emplace_back(Tok_Colon, i, i+1);
162 ++i;
163 break;
164 case '+':
165 if(i+1 != end && *(i+1) == '=')
167 tokens.emplace_back(Tok_PlusEqual, i, i+2);
168 i+=2;
170 else
172 tokens.emplace_back(Tok_Plus, i, i+1);
173 ++i;
175 break;
176 case '-':
177 if(i+1 != end && *(i+1) == '=')
179 tokens.emplace_back(Tok_DashEqual, i, i+2);
180 i+=2;
182 else
184 tokens.emplace_back(Tok_Dash, i, i+1);
185 ++i;
187 break;
188 case '*':
189 if(i+1 != end && *(i+1) == '=')
191 tokens.emplace_back(Tok_StarEqual, i, i+2);
192 i+=2;
194 else
196 tokens.emplace_back(Tok_Star, i, i+1);
197 ++i;
199 break;
200 case '%':
201 if(i+1 != end && *(i+1) == '=')
203 tokens.emplace_back(Tok_PercentEqual, i, i+2);
204 i+=2;
206 else
208 tokens.emplace_back(Tok_Percent, i, i+1);
209 ++i;
211 break;
212 case '/':
213 if(i+1 != end && *(i+1) == '/')
215 ++i;
216 while(i!=end && *i != '\n')
218 ++i;
219 } ++i;
221 else if(i+1 != end && *(i+1) == '=')
223 tokens.emplace_back(Tok_SlashEqual, i, i+2);
224 i+=2;
226 else
228 tokens.emplace_back(Tok_Slash, i, i+1);
229 ++i;
231 break;
232 case '<':
233 if(i+1 != end && *(i+1) == '=')
235 tokens.emplace_back(Tok_LessEqual, i, i+2);
236 i+=2;
238 else
240 tokens.emplace_back(Tok_Less, i, i+1);
241 ++i;
243 break;
244 case '>':
245 if(i+1 != end && *(i+1) == '=')
247 tokens.emplace_back(Tok_GreaterEqual, i, i+2);
248 i+=2;
250 else
252 tokens.emplace_back(Tok_Greater, i, i+1);
253 ++i;
255 break;
256 case '=':
257 if(i+1 != end && *(i+1) == '=')
259 tokens.emplace_back(Tok_EqOp, i, i+2);
260 i+=2;
262 else
264 tokens.emplace_back(Tok_Equal, i, i+1);
265 ++i;
267 break;
268 case '!':
269 if(i+1 != end && *(i+1) == '=')
271 tokens.emplace_back(Tok_NotEqual, i, i+2);
272 i+=2;
274 else
276 tokens.emplace_back(Tok_Not, i, i+1);
277 i+=2;
279 break;
280 case '&':
281 if(i+1 != end && *(i+1) == '&')
283 tokens.emplace_back(Tok_LogicalAnd, i, i+2);
284 i+=2;
286 else
288 auto lc = line_column(begin, i);
289 throw std::runtime_error(
290 std::to_string(lc.first) + ":" +
291 std::to_string(lc.second) + ": unrecognized symbol: '" + *i + "'"
294 break;
295 case '|':
296 if(i+1 != end && *(i+1) == '|')
298 tokens.emplace_back(Tok_LogicalOr, i, i+2);
299 i+=2;
301 else
303 auto lc = line_column(begin, i);
304 throw std::runtime_error(
305 std::to_string(lc.first) + ":" +
306 std::to_string(lc.second) + ": unrecognized symbol: '" + *i + "'"
309 break;
310 case '"':
311 ++i;
312 while(i!=end && !(*i == '"' && *(i-1) != '\\'))
314 ++i;
316 ++i;
317 tokens.emplace_back(Tok_String, start, i);
318 break;
319 default:
320 if(std::isdigit(*i))
322 while(i!=end && std::isdigit(*i))
324 ++i;
326 if(i!=end && *i=='.')
328 ++i;
329 while(i!=end && std::isdigit(*i))
331 ++i;
334 if(i!=end && *i=='e')
336 ++i;
337 if(i!=end && *i=='-')
339 ++i;
341 while(i!=end && std::isdigit(*i))
343 ++i;
346 tokens.emplace_back(Tok_Number, start, i);
347 break;
349 else if(std::isalpha(*i) || *i == '_')
351 ++i;
352 while(i!=end && (std::isalnum(*i) || *i == '_'))
354 ++i;
356 auto kw = keywords.find(std::string(start, i));
357 if(kw == keywords.end())
359 tokens.emplace_back(Tok_Identifier, start, i);
361 else
363 tokens.emplace_back(kw->second, start, i);
365 break;
367 else
369 auto lc = line_column(begin, i);
370 throw std::runtime_error(
371 std::to_string(lc.first) + ":" +
372 std::to_string(lc.second) + ": unrecognized symbol: '" + *i + "'"
377 return tokens_ptr;
380 // Abstract Syntax Tree node types
381 template<typename Iter, typename Value, typename FuncType>
382 struct ASTNode: public std::enable_shared_from_this<ASTNode<Iter,Value,FuncType>>
384 Iter begin;
385 Iter end;
386 std::shared_ptr<std::string> source;
387 std::shared_ptr<std::vector<Token<std::string::iterator>>> tokens;
388 std::weak_ptr<ASTNode<Iter,Value,FuncType>> root;
390 virtual Value accept(FuncType&) = 0;
392 virtual std::shared_ptr<ASTNode<Iter,Value,FuncType>>& operator[](size_t) = 0;
394 virtual size_t size() const = 0;
396 virtual void inject_dependencies() = 0;
399 template<class Iter, class Value, class FuncType>
400 struct Variadic : public ASTNode<Iter,Value,FuncType>
402 std::vector<std::shared_ptr<ASTNode<Iter,Value,FuncType>>> children;
404 virtual Value accept(FuncType &f)
406 return f(*this);
409 virtual std::shared_ptr<ASTNode<Iter,Value,FuncType>>& operator[](size_t i)
411 return children[i];
414 virtual size_t size() const
416 return children.size();
419 virtual void inject_dependencies()
421 for(auto& child : children)
423 child->root = this->root;
424 child->source = this->source;
425 child->tokens = this->tokens;
426 child->inject_dependencies();
431 template<class Iter, class Value, class FuncType>
432 struct Ternary : public ASTNode<Iter,Value,FuncType>
434 std::array<std::shared_ptr<ASTNode<Iter,Value,FuncType>>, 3> children;
436 virtual Value accept(FuncType &f)
438 return f(*this);
441 virtual std::shared_ptr<ASTNode<Iter,Value,FuncType>>& operator[](size_t i)
443 return children[i];
446 virtual size_t size() const
448 return children.size();
451 virtual void inject_dependencies()
453 for(auto& child : children)
455 child->root = this->root;
456 child->source = this->source;
457 child->tokens = this->tokens;
458 child->inject_dependencies();
463 template<class Iter, class Value, class FuncType>
464 struct Binary : public ASTNode<Iter,Value,FuncType>
466 std::array<std::shared_ptr<ASTNode<Iter,Value,FuncType>>, 2> children;
468 virtual Value accept(FuncType &f)
470 return f(*this);
473 virtual std::shared_ptr<ASTNode<Iter,Value,FuncType>>& operator[](size_t i)
475 return children[i];
478 virtual size_t size() const
480 return children.size();
483 virtual void inject_dependencies()
485 for(auto& child : children)
487 child->root = this->root;
488 child->source = this->source;
489 child->tokens = this->tokens;
490 child->inject_dependencies();
495 template<class Iter, class Value, class FuncType>
496 struct Unary : public ASTNode<Iter,Value,FuncType>
498 std::array<std::shared_ptr<ASTNode<Iter,Value,FuncType>>, 1> children;
500 virtual Value accept(FuncType &f)
502 return f(*this);
505 virtual std::shared_ptr<ASTNode<Iter,Value,FuncType>>& operator[](size_t i)
507 return children[i];
510 virtual size_t size() const
512 return children.size();
515 virtual void inject_dependencies()
517 for(auto& child : children)
519 child->root = this->root;
520 child->source = this->source;
521 child->tokens = this->tokens;
522 child->inject_dependencies();
527 template<class Iter, class Value, class FuncType>
528 struct Nullary : public ASTNode<Iter,Value,FuncType>
530 std::array<std::shared_ptr<ASTNode<Iter,Value,FuncType>>, 0> children;
532 virtual Value accept(FuncType &f)
534 return f(*this);
537 virtual std::shared_ptr<ASTNode<Iter,Value,FuncType>>& operator[](size_t i)
539 return children[i];
542 virtual size_t size() const
544 return children.size();
547 virtual void inject_dependencies()
549 for(auto& child : children)
551 child->root = this->root;
552 child->source = this->source;
553 child->tokens = this->tokens;
554 child->inject_dependencies();
559 template<class Iter, class Value, class FuncType>
560 struct Variable : public Nullary<Iter,Value,FuncType>
562 Value name;
564 virtual Value accept(FuncType &f)
566 return f(*this);
570 template<class Iter, class Value, class FuncType>
571 struct GlobalVariable : public Variable<Iter,Value,FuncType>
573 virtual Value accept(FuncType &f)
575 return f(*this);
579 template<class Iter, class Value, class FuncType>
580 struct LocalVariable : public Variable<Iter,Value,FuncType>
582 virtual Value accept(FuncType &f)
584 return f(*this);
588 template<class Iter, class Value, class FuncType>
589 struct FieldName : public Nullary<Iter,Value,FuncType>
591 Value name;
592 virtual Value accept(FuncType &f)
594 return f(*this);
598 template<class Iter, class Value, class FuncType>
599 struct Nop : public Nullary<Iter,Value,FuncType>
601 double value;
602 virtual Value accept(FuncType &f)
604 return f(*this);
608 template<class Iter, class Value, class FuncType>
609 struct Constant : public Nullary<Iter,Value,FuncType>
611 Value value;
613 virtual Value accept(FuncType &f)
615 return f(*this);
619 template<class Iter, class Value, class FuncType>
620 struct Nil : public Nullary<Iter,Value,FuncType>
622 virtual Value accept(FuncType &f)
624 return f(*this);
628 template<class Iter, class Value, class FuncType>
629 struct String : public Nullary<Iter,Value,FuncType>
631 Value value;
633 virtual Value accept(FuncType &f)
635 return f(*this);
639 template<class Iter, class Value, class FuncType>
640 struct FieldAccess : public Binary<Iter,Value,FuncType>
642 virtual Value accept(FuncType &f)
644 return f(*this);
648 template<class Iter, class Value, class FuncType>
649 struct FieldAssignment : public Ternary<Iter,Value,FuncType>
651 virtual Value accept(FuncType &f)
653 return f(*this);
657 template<class Iter, class Value, class FuncType>
658 struct AddFieldAssignment : public FieldAssignment<Iter,Value,FuncType>
660 virtual Value accept(FuncType &f)
662 return f(*this);
666 template<class Iter, class Value, class FuncType>
667 struct SubFieldAssignment : public FieldAssignment<Iter,Value,FuncType>
669 virtual Value accept(FuncType &f)
671 return f(*this);
675 template<class Iter, class Value, class FuncType>
676 struct MulFieldAssignment : public FieldAssignment<Iter,Value,FuncType>
678 virtual Value accept(FuncType &f)
680 return f(*this);
684 template<class Iter, class Value, class FuncType>
685 struct DivFieldAssignment : public FieldAssignment<Iter,Value,FuncType>
687 virtual Value accept(FuncType &f)
689 return f(*this);
693 template<class Iter, class Value, class FuncType>
694 struct ModFieldAssignment : public FieldAssignment<Iter,Value,FuncType>
696 virtual Value accept(FuncType &f)
698 return f(*this);
702 template<class Iter, class Value, class FuncType>
703 struct Call : public Variadic<Iter,Value,FuncType>
705 virtual Value accept(FuncType &f)
707 return f(*this);
711 template<class Iter, class Value, class FuncType>
712 struct MemberCall : public Variadic<Iter,Value,FuncType>
714 virtual Value accept(FuncType &f)
716 return f(*this);
720 template<class Iter, class Value, class FuncType>
721 struct EvalStatement : public Unary<Iter,Value,FuncType>
723 virtual Value accept(FuncType &f)
725 return f(*this);
729 template<class Iter, class Value, class FuncType>
730 struct Eval : public Unary<Iter,Value,FuncType>
732 virtual Value accept(FuncType &f)
734 return f(*this);
738 template<class Iter, class Value, class FuncType>
739 struct ParseStatement : public Unary<Iter,Value,FuncType>
741 virtual Value accept(FuncType &f)
743 return f(*this);
747 template<class Iter, class Value, class FuncType>
748 struct ParseExpression : public Unary<Iter,Value,FuncType>
750 virtual Value accept(FuncType &f)
752 return f(*this);
756 template<class Iter, class Value, class FuncType>
757 struct UnaryExpression : public Unary<Iter,Value,FuncType>
759 virtual Value accept(FuncType &f)
761 return f(*this);
765 template<class Iter, class Value, class FuncType>
766 struct UnaryPlusExpression : public UnaryExpression<Iter,Value,FuncType>
768 virtual Value accept(FuncType &f)
770 return f(*this);
774 template<class Iter, class Value, class FuncType>
775 struct UnaryMinusExpression : public UnaryExpression<Iter,Value,FuncType>
777 virtual Value accept(FuncType &f)
779 return f(*this);
783 template<class Iter, class Value, class FuncType>
784 struct UnaryNotExpression : public UnaryExpression<Iter,Value,FuncType>
786 virtual Value accept(FuncType &f)
788 return f(*this);
792 template<class Iter, class Value, class FuncType>
793 struct MultiplicativeExpression : public Binary<Iter,Value,FuncType>
795 virtual Value accept(FuncType &f)
797 return f(*this);
801 template<class Iter, class Value, class FuncType>
802 struct MultiplyExpression : public MultiplicativeExpression<Iter,Value,FuncType>
804 virtual Value accept(FuncType &f)
806 return f(*this);
810 template<class Iter, class Value, class FuncType>
811 struct DivideExpression : public MultiplicativeExpression<Iter,Value,FuncType>
813 virtual Value accept(FuncType &f)
815 return f(*this);
819 template<class Iter, class Value, class FuncType>
820 struct ModuloExpression : public MultiplicativeExpression<Iter,Value,FuncType>
822 virtual Value accept(FuncType &f)
824 return f(*this);
828 template<class Iter, class Value, class FuncType>
829 struct AdditiveExpression : public Binary<Iter,Value,FuncType>
831 virtual Value accept(FuncType &f)
833 return f(*this);
837 template<class Iter, class Value, class FuncType>
838 struct AddExpression : public AdditiveExpression<Iter,Value,FuncType>
840 virtual Value accept(FuncType &f)
842 return f(*this);
846 template<class Iter, class Value, class FuncType>
847 struct SubtractExpression : public AdditiveExpression<Iter,Value,FuncType>
849 virtual Value accept(FuncType &f)
851 return f(*this);
855 template<class Iter, class Value, class FuncType>
856 struct RelationalExpression : public Binary<Iter,Value,FuncType>
858 virtual Value accept(FuncType &f)
860 return f(*this);
864 template<class Iter, class Value, class FuncType>
865 struct LessExpression : public RelationalExpression<Iter,Value,FuncType>
867 virtual Value accept(FuncType &f)
869 return f(*this);
873 template<class Iter, class Value, class FuncType>
874 struct LessEqualExpression : public RelationalExpression<Iter,Value,FuncType>
876 virtual Value accept(FuncType &f)
878 return f(*this);
882 template<class Iter, class Value, class FuncType>
883 struct GreaterExpression : public RelationalExpression<Iter,Value,FuncType>
885 virtual Value accept(FuncType &f)
887 return f(*this);
891 template<class Iter, class Value, class FuncType>
892 struct GreaterEqualExpression : public RelationalExpression<Iter,Value,FuncType>
894 virtual Value accept(FuncType &f)
896 return f(*this);
900 template<class Iter, class Value, class FuncType>
901 struct EqualityExpression : public Binary<Iter,Value,FuncType>
903 virtual Value accept(FuncType &f)
905 return f(*this);
909 template<class Iter, class Value, class FuncType>
910 struct EqualExpression : public EqualityExpression<Iter,Value,FuncType>
912 virtual Value accept(FuncType &f)
914 return f(*this);
918 template<class Iter, class Value, class FuncType>
919 struct NotEqualExpression : public EqualityExpression<Iter,Value,FuncType>
921 virtual Value accept(FuncType &f)
923 return f(*this);
927 template<class Iter, class Value, class FuncType>
928 struct LogicalAndExpression : public Binary<Iter,Value,FuncType>
930 virtual Value accept(FuncType &f)
932 return f(*this);
936 template<class Iter, class Value, class FuncType>
937 struct LogicalOrExpression : public Binary<Iter,Value,FuncType>
939 virtual Value accept(FuncType &f)
941 return f(*this);
945 template<class Iter, class Value, class FuncType>
946 struct AssignmentExpression : public Binary<Iter,Value,FuncType>
948 virtual Value accept(FuncType &f)
950 return f(*this);
954 template<class Iter, class Value, class FuncType>
955 struct GlobalAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
957 virtual Value accept(FuncType &f)
959 return f(*this);
963 template<class Iter, class Value, class FuncType>
964 struct LocalAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
966 virtual Value accept(FuncType &f)
968 return f(*this);
972 template<class Iter, class Value, class FuncType>
973 struct AddAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
975 virtual Value accept(FuncType &f)
977 return f(*this);
981 template<class Iter, class Value, class FuncType>
982 struct SubAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
984 virtual Value accept(FuncType &f)
986 return f(*this);
990 template<class Iter, class Value, class FuncType>
991 struct MulAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
993 virtual Value accept(FuncType &f)
995 return f(*this);
999 template<class Iter, class Value, class FuncType>
1000 struct DivAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
1002 virtual Value accept(FuncType &f)
1004 return f(*this);
1008 template<class Iter, class Value, class FuncType>
1009 struct ModAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
1011 virtual Value accept(FuncType &f)
1013 return f(*this);
1017 template<class Iter, class Value, class FuncType>
1018 struct InitializerAssignmentExpression : public Variadic<Iter,Value,FuncType>
1020 virtual Value accept(FuncType &f)
1022 return f(*this);
1026 template<class Iter, class Value, class FuncType>
1027 struct Block : public Unary<Iter,Value,FuncType>
1029 virtual Value accept(FuncType &f)
1031 return f(*this);
1035 template<class Iter, class Value, class FuncType>
1036 struct TableInitializer : public Variadic<Iter,Value,FuncType>
1038 virtual Value accept(FuncType &f)
1040 return f(*this);
1044 template<class Iter, class Value, class FuncType>
1045 struct ArrayInitializer : public Variadic<Iter,Value,FuncType>
1047 virtual Value accept(FuncType &f)
1049 return f(*this);
1053 template<class Iter, class Value, class FuncType>
1054 struct IdentifierList : public Variadic<Iter,Value,FuncType>
1056 virtual Value accept(FuncType &f)
1058 return f(*this);
1062 template<class Iter, class Value, class FuncType>
1063 struct Function : public Binary<Iter,Value,FuncType>
1065 virtual Value accept(FuncType &f)
1067 return f(*this);
1071 template<class Iter, class Value, class FuncType>
1072 struct Tree : public Unary<Iter,Value,FuncType>
1074 virtual Value accept(FuncType &f)
1076 return f(*this);
1080 template<class Iter, class Value, class FuncType>
1081 struct Root : public Variadic<Iter,Value,FuncType>
1083 virtual Value accept(FuncType &f)
1085 return f(*this);
1089 template<class Iter, class Value, class FuncType>
1090 struct Type : public Unary<Iter,Value,FuncType>
1092 virtual Value accept(FuncType &f)
1094 return f(*this);
1098 template<class Iter, class Value, class FuncType>
1099 struct Size : public Unary<Iter,Value,FuncType>
1101 virtual Value accept(FuncType &f)
1103 return f(*this);
1107 template<class Iter, class Value, class FuncType>
1108 struct StatementList : public Variadic<Iter,Value,FuncType>
1110 virtual Value accept(FuncType &f)
1112 return f(*this);
1116 template<class Iter, class Value, class FuncType>
1117 struct IfStatement : public Variadic<Iter,Value,FuncType>
1119 virtual Value accept(FuncType &f)
1121 return f(*this);
1125 template<class Iter, class Value, class FuncType>
1126 struct WhileStatement : public Binary<Iter,Value,FuncType>
1128 virtual Value accept(FuncType &f)
1130 return f(*this);
1134 template<class Iter, class Value, class FuncType>
1135 struct ForStatement : public Variadic<Iter,Value,FuncType>
1137 virtual Value accept(FuncType &f)
1139 return f(*this);
1143 template<class Iter, class Value, class FuncType>
1144 struct ForEachStatement : public Variadic<Iter,Value,FuncType>
1146 virtual Value accept(FuncType &f)
1148 return f(*this);
1152 template<class Iter, class Value, class FuncType>
1153 struct ReturnStatement : public Variadic<Iter,Value,FuncType>
1155 virtual Value accept(FuncType &f)
1157 return f(*this);
1161 template<class Iter, class Value, class FuncType>
1162 struct ContinueStatement : public Nullary<Iter,Value,FuncType>
1164 virtual Value accept(FuncType &f)
1166 return f(*this);
1170 template<class Iter, class Value, class FuncType>
1171 struct BreakStatement : public Nullary<Iter,Value,FuncType>
1173 virtual Value accept(FuncType &f)
1175 return f(*this);
1179 template<class Iter, class Value, class FuncType>
1180 struct ExpressionStatement : public Unary<Iter,Value,FuncType>
1182 virtual Value accept(FuncType &f)
1184 return f(*this);
1188 template<class Iter, class Value, class FuncType>
1189 struct BuiltinFunction : public Nullary<Iter,Value,FuncType>
1191 virtual Value accept(FuncType &f)
1193 return f(*this);
1195 std::function<void(Interpreter&, std::vector<Value>&)> function;
1198 template<class Iter, class Value, class FuncType>
1199 struct Parens : public Unary<Iter,Value,FuncType>
1201 virtual Value accept(FuncType &f)
1203 return f(*this);
1207 template<class Value, class FuncType>
1208 class Parser
1210 public:
1211 using Iter = std::vector<Token<std::string::iterator>>::iterator;
1212 using return_t = std::shared_ptr<ASTNode<Iter,Value,FuncType>>;
1214 template<typename StrIter>
1215 return_t operator()(StrIter sbegin, StrIter send)
1217 auto source = std::make_shared<std::string>(sbegin, send);
1218 auto tokens = tokenize(source->begin(), source->end());
1219 this->begin = tokens->begin();
1220 this->end = tokens->end();
1221 i = this->begin;
1222 accepted = this->end;
1223 auto node = statement_list();
1224 node->source = source;
1225 node->tokens = tokens;
1226 node->root = node;
1227 node->inject_dependencies();
1228 return node;
1231 template<class StrIter>
1232 return_t parse_expression(StrIter sbegin, StrIter send)
1234 auto source = std::make_shared<std::string>(sbegin, send);
1235 auto tokens = tokenize(source->begin(), source->end());
1236 this->begin = tokens->begin();
1237 this->end = tokens->end();
1238 i = this->begin;
1239 accepted = this->end;
1240 auto node = expression();
1241 node->source = source;
1242 node->tokens = tokens;
1243 node->root = node;
1244 node->inject_dependencies();
1245 return node;
1248 private:
1249 Iter i;
1250 Iter accepted;
1251 Iter begin;
1252 Iter end;
1254 void advance()
1256 if(i==end)
1258 Iter last = end-1;
1259 auto lc = line_column(begin->begin, last->end);
1260 throw std::runtime_error(
1261 std::to_string(lc.first) + ":" +
1262 std::to_string(lc.second) + ": unexpected end of input at");
1264 else
1266 ++i;
1270 void rewind(Iter pos)
1272 i = pos;
1275 bool peek(TokenType t, int amount = 0)
1277 return (((i+amount) != end) && (((i+amount)->type) == t));
1280 bool accept(TokenType t)
1282 if(i != end && i->type == t)
1284 accepted = i;
1285 advance();
1286 return true;
1288 else
1290 return false;
1294 bool end_of_input()
1296 return (i == end);
1299 bool expect(TokenType t)
1301 if(accept(t))
1303 return true;
1305 else
1307 i = std::min(i, end-1);
1308 auto lc = line_column(begin->begin, i->begin);
1309 throw std::runtime_error(
1310 std::to_string(lc.first) + ":" +
1311 std::to_string(lc.second) + ": unexpected token '" +
1312 std::string(i->begin, i->end) + "'"
1314 return false;
1318 void expect(const std::string &expected)
1320 i = std::min(i, end-1);
1321 auto lc = line_column(begin->begin, i->begin);
1322 throw std::runtime_error(
1323 std::to_string(lc.first) + ":" +
1324 std::to_string(lc.second) + ": expected " +
1325 expected+ " but got '" + std::string(i->begin, i->end) + "'"
1329 return_t variable()
1331 expect(Tok_Identifier);
1332 auto node = std::make_shared<Variable<Iter,Value,FuncType>>();
1333 node->begin = accepted;
1334 node->end = accepted+1;
1335 node->name = Value(accepted->begin, accepted->end);
1336 return std::move(node);
1339 return_t field_name()
1341 expect(Tok_Identifier);
1342 auto node = std::make_shared<FieldName<Iter,Value,FuncType>>();
1343 node->begin = accepted;
1344 node->end = accepted+1;
1345 node->name = Value(accepted->begin, accepted->end);
1346 return std::move(node);
1349 return_t identifier_list()
1351 auto node = std::make_shared<IdentifierList<Iter,Value,FuncType>>();
1352 node->begin = i;
1353 expect(Tok_LeftParen);
1354 while(!accept(Tok_RightParen))
1356 node->children.push_back(field_name());
1357 if(!peek(Tok_RightParen))
1359 accept(Tok_Comma);
1362 node->end = accepted+1;
1363 return std::move(node);
1366 return_t function()
1368 auto node = std::make_shared<Function<Iter,Value,FuncType>>();
1369 node->begin = i;
1370 expect(Tok_Function);
1371 node->children[0] = identifier_list();
1372 node->children[1] = statement();
1373 node->end = accepted+1;
1374 return std::move(node);
1377 return_t parse_expression()
1379 auto node = std::make_shared<ParseExpression<Iter,Value,FuncType>>();
1380 node->begin = i;
1381 expect(Tok_ParseExpression);
1382 expect(Tok_LeftParen);
1383 node->children[0] = expression();
1384 expect(Tok_RightParen);
1385 node->end = accepted+1;
1386 return std::move(node);
1389 return_t parse_statement()
1391 auto node = std::make_shared<ParseStatement<Iter,Value,FuncType>>();
1392 node->begin = i;
1393 expect(Tok_ParseStatement);
1394 expect(Tok_LeftParen);
1395 node->children[0] = expression();
1396 expect(Tok_RightParen);
1397 node->end = accepted+1;
1398 return std::move(node);
1401 return_t tree()
1403 auto node = std::make_shared<Tree<Iter,Value,FuncType>>();
1404 node->begin = i;
1405 if(accept(Tok_Expression))
1407 expect(Tok_LeftParen);
1408 node->children[0] = expression();
1409 expect(Tok_RightParen);
1411 else
1413 expect(Tok_Statement);
1414 expect(Tok_LeftParen);
1415 node->children[0] = statement();
1416 expect(Tok_RightParen);
1418 node->end = accepted+1;
1419 return std::move(node);
1422 return_t root()
1424 auto node = std::make_shared<Root<Iter,Value,FuncType>>();
1425 node->begin = i;
1426 expect(Tok_Root);
1427 if(accept(Tok_LeftParen))
1429 node->children.push_back(expression());
1430 expect(Tok_RightParen);
1432 node->end = accepted+1;
1433 return std::move(node);
1436 return_t eval()
1438 auto node = std::make_shared<Eval<Iter,Value,FuncType>>();
1439 node->begin = i;
1440 expect(Tok_Eval);
1441 expect(Tok_LeftParen);
1442 node->children[0] = unary_expression();
1443 expect(Tok_RightParen);
1444 node->end = accepted+1;
1445 return std::move(node);
1448 return_t type()
1450 auto node = std::make_shared<Type<Iter,Value,FuncType>>();
1451 node->begin = i;
1452 expect(Tok_Type);
1453 if(accept(Tok_LeftParen))
1455 node->children[0] = expression();
1456 expect(Tok_RightParen);
1458 node->end = accepted+1;
1459 return std::move(node);
1462 return_t size()
1464 auto node = std::make_shared<Size<Iter,Value,FuncType>>();
1465 node->begin = i;
1466 expect(Tok_Size);
1467 if(accept(Tok_LeftParen))
1469 node->children[0] = expression();
1470 expect(Tok_RightParen);
1472 node->end = accepted+1;
1473 return std::move(node);
1476 return_t table_initializer()
1478 auto node = std::make_shared<TableInitializer<Iter,Value,FuncType>>();
1479 node->begin = i;
1480 expect(Tok_LeftBrace);
1481 while(!accept(Tok_RightBrace))
1483 node->children.push_back(initializer_assignment_expression());
1484 if(!peek(Tok_RightBrace))
1486 accept(Tok_Comma);
1489 node->end = accepted+1;
1490 return std::move(node);
1493 return_t array_initializer()
1495 auto node = std::make_shared<ArrayInitializer<Iter,Value,FuncType>>();
1496 node->begin = i;
1497 expect(Tok_LeftBracket);
1498 while(!accept(Tok_RightBracket))
1500 node->children.push_back(logical_or_expression());
1501 if(!peek(Tok_RightBracket))
1503 accept(Tok_Comma);
1506 node->end = accepted+1;
1507 return std::move(node);
1510 return_t primary_expression()
1512 if (peek(Tok_Identifier) || peek(Tok_Global) || peek(Tok_Local))
1514 return variable();
1516 else if (accept(Tok_Number))
1518 auto node = std::make_shared<Constant<Iter,Value,FuncType>>();
1519 node->begin = accepted;
1520 node->end = accepted+1;
1521 node->value = std::stod(std::string(accepted->begin, accepted->end));
1522 return std::move(node);
1524 else if (accept(Tok_Nil))
1526 auto node = std::make_shared<Nil<Iter,Value,FuncType>>();
1527 node->begin = accepted;
1528 node->end = accepted+1;
1529 return std::move(node);
1531 else if (accept(Tok_String))
1533 auto node = std::make_shared<String<Iter,Value,FuncType>>();
1534 node->begin = accepted;
1535 node->end = accepted+1;
1536 node->value = Value(unescape(std::string(accepted->begin+1, accepted->end-1)));
1537 return std::move(node);
1539 else if (accept(Tok_LeftParen))
1541 auto node = std::make_shared<Parens<Iter,Value,FuncType>>();
1542 node->begin = accepted;
1543 node->children[0] = expression();
1544 expect(Tok_RightParen);
1545 node->end = accepted+1;
1546 return std::move(node);
1548 else if (peek(Tok_LeftBrace))
1550 return table_initializer();
1552 else if (peek(Tok_LeftBracket))
1554 return array_initializer();
1556 else if (peek(Tok_Expression) || peek(Tok_Statement))
1558 return tree();
1560 else if (peek(Tok_Root))
1562 return root();
1564 else if (peek(Tok_Type))
1566 return type();
1568 else if (peek(Tok_Eval))
1570 return eval();
1572 else if (peek(Tok_Size))
1574 return size();
1576 else if (peek(Tok_ParseExpression))
1578 return parse_expression();
1580 else if (peek(Tok_ParseStatement))
1582 return parse_statement();
1584 else if (peek(Tok_Function))
1586 return function();
1588 else
1590 expect("primary expression");
1591 return nullptr;
1595 return_t postfix_expression()
1597 Iter from = i;
1598 return_t left = primary_expression();
1599 while(true)
1601 if(accept(Tok_LeftBracket))
1603 return_t right = expression();
1604 expect(Tok_RightBracket);
1605 if(peek(Tok_Equal) || peek(Tok_PlusEqual) || peek(Tok_DashEqual) ||
1606 peek(Tok_StarEqual) || peek(Tok_SlashEqual) || peek(Tok_PercentEqual))
1608 std::shared_ptr<FieldAssignment<Iter,Value,FuncType>> node;
1609 if(accept(Tok_Equal))
1611 node = std::make_shared<FieldAssignment<Iter,Value,FuncType>>();
1613 else if(accept(Tok_PlusEqual))
1615 node = std::make_shared<AddFieldAssignment<Iter,Value,FuncType>>();
1617 else if(accept(Tok_DashEqual))
1619 node = std::make_shared<SubFieldAssignment<Iter,Value,FuncType>>();
1621 else if(accept(Tok_StarEqual))
1623 node = std::make_shared<MulFieldAssignment<Iter,Value,FuncType>>();
1625 else if(accept(Tok_SlashEqual))
1627 node = std::make_shared<DivFieldAssignment<Iter,Value,FuncType>>();
1629 else if(accept(Tok_PercentEqual))
1631 node = std::make_shared<ModFieldAssignment<Iter,Value,FuncType>>();
1633 node->begin = from;
1634 node->children[0] = std::move(left);
1635 node->children[1] = std::move(right);
1636 node->children[2] = expression();
1637 node->end = accepted+1;
1638 left = std::move(node);
1640 else
1642 auto node = std::make_shared<FieldAccess<Iter,Value,FuncType>>();
1643 node->begin = from;
1644 node->children[0] = std::move(left);
1645 node->children[1] = std::move(right);
1646 node->end = accepted+1;
1647 left = std::move(node);
1650 else if(accept(Tok_Dot))
1652 return_t right = field_name();
1653 if(peek(Tok_Equal) || peek(Tok_PlusEqual) || peek(Tok_DashEqual) ||
1654 peek(Tok_StarEqual) || peek(Tok_SlashEqual) || peek(Tok_PercentEqual))
1656 std::shared_ptr<FieldAssignment<Iter,Value,FuncType>> node;
1657 if(accept(Tok_Equal))
1659 node = std::make_shared<FieldAssignment<Iter,Value,FuncType>>();
1661 else if(accept(Tok_PlusEqual))
1663 node = std::make_shared<AddFieldAssignment<Iter,Value,FuncType>>();
1665 else if(accept(Tok_DashEqual))
1667 node = std::make_shared<SubFieldAssignment<Iter,Value,FuncType>>();
1669 else if(accept(Tok_StarEqual))
1671 node = std::make_shared<MulFieldAssignment<Iter,Value,FuncType>>();
1673 else if(accept(Tok_SlashEqual))
1675 node = std::make_shared<DivFieldAssignment<Iter,Value,FuncType>>();
1677 else if(accept(Tok_PercentEqual))
1679 node = std::make_shared<ModFieldAssignment<Iter,Value,FuncType>>();
1681 node->begin = from;
1682 node->children[0] = std::move(left);
1683 node->children[1] = std::move(right);
1684 node->children[2] = expression();
1685 node->end = accepted+1;
1686 left = std::move(node);
1688 else if(accept(Tok_LeftParen))
1690 auto node = std::make_shared<MemberCall<Iter,Value,FuncType>>();
1691 node->begin = from;
1692 node->children.push_back(std::move(left));
1693 node->children.push_back(std::move(right));
1694 while(!accept(Tok_RightParen))
1696 node->children.push_back(expression());
1697 if(!peek(Tok_RightParen))
1699 accept(Tok_Comma);
1702 node->end = accepted+1;
1703 left = std::move(node);
1705 else
1707 auto node = std::make_shared<FieldAccess<Iter,Value,FuncType>>();
1708 node->begin = from;
1709 node->children[0] = std::move(left);
1710 node->children[1] = std::move(right);
1711 node->end = accepted+1;
1712 left = std::move(node);
1715 else if(accept(Tok_LeftParen))
1717 auto node = std::make_shared<Call<Iter,Value,FuncType>>();
1718 node->begin = from;
1719 node->children.push_back(std::move(left));
1720 while(!accept(Tok_RightParen))
1722 node->children.push_back(expression());
1723 if(!peek(Tok_RightParen))
1725 accept(Tok_Comma);
1728 node->end = accepted+1;
1729 left = std::move(node);
1731 else
1733 return std::move(left);
1738 return_t unary_expression()
1740 if(accept(Tok_Plus) || accept(Tok_Dash) || accept(Tok_Not))
1742 using node_t = std::shared_ptr<UnaryExpression<Iter,Value,FuncType>>;
1743 node_t node;
1744 switch(accepted->type)
1746 case Tok_Plus:
1747 node = node_t(new UnaryPlusExpression<Iter,Value,FuncType>);
1748 break;
1749 case Tok_Dash:
1750 node = node_t(new UnaryMinusExpression<Iter,Value,FuncType>);
1751 break;
1752 case Tok_Not:
1753 node = node_t(new UnaryNotExpression<Iter,Value,FuncType>);
1754 break;
1755 default:
1756 break;
1758 node->begin = accepted;
1759 node->children[0] = postfix_expression();
1760 node->end = accepted+1;
1761 return std::move(node);
1763 else
1765 return postfix_expression();
1769 return_t multiplicative_expression()
1771 Iter from = i;
1772 return_t left = unary_expression();
1773 if(accept(Tok_Star) || accept(Tok_Slash) || accept(Tok_Percent))
1775 using node_t = std::shared_ptr<MultiplicativeExpression<Iter,Value,FuncType>>;
1776 node_t node;
1777 switch(accepted->type)
1779 case Tok_Star:
1780 node = node_t(new MultiplyExpression<Iter,Value,FuncType>);
1781 break;
1782 case Tok_Slash:
1783 node = node_t(new DivideExpression<Iter,Value,FuncType>);
1784 break;
1785 case Tok_Percent:
1786 node = node_t(new ModuloExpression<Iter,Value,FuncType>);
1787 break;
1788 default:
1789 break;
1791 node->begin = from;
1792 node->children[0] = std::move(left);
1793 node->children[1] = multiplicative_expression();
1794 node->end = accepted+1;
1795 return std::move(node);
1797 else
1799 return std::move(left);
1803 return_t additive_expression()
1805 Iter from = i;
1806 return_t left = multiplicative_expression();
1807 if(accept(Tok_Plus) || accept(Tok_Dash))
1809 using node_t = std::shared_ptr<AdditiveExpression<Iter,Value,FuncType>>;
1810 node_t node;
1811 switch(accepted->type)
1813 case Tok_Plus:
1814 node = node_t(new AddExpression<Iter,Value,FuncType>);
1815 break;
1816 case Tok_Dash:
1817 node = node_t(new SubtractExpression<Iter,Value,FuncType>);
1818 break;
1819 default:
1820 break;
1822 node->begin = from;
1823 node->children[0] = std::move(left);
1824 node->children[1] = additive_expression();
1825 node->end = accepted+1;
1826 return std::move(node);
1828 else
1830 return std::move(left);
1834 return_t relational_expression()
1836 Iter from = i;
1837 return_t left = additive_expression();
1838 if(accept(Tok_Less) || accept(Tok_LessEqual) || accept(Tok_Greater) || accept(Tok_GreaterEqual))
1840 using node_t = std::shared_ptr<RelationalExpression<Iter,Value,FuncType>>;
1841 node_t node;
1842 switch(accepted->type)
1844 case Tok_Less:
1845 node = node_t(new LessExpression<Iter,Value,FuncType>);
1846 break;
1847 case Tok_LessEqual:
1848 node = node_t(new LessEqualExpression<Iter,Value,FuncType>);
1849 break;
1850 case Tok_Greater:
1851 node = node_t(new GreaterExpression<Iter,Value,FuncType>);
1852 break;
1853 case Tok_GreaterEqual:
1854 node = node_t(new GreaterEqualExpression<Iter,Value,FuncType>);
1855 break;
1856 default:
1857 break;
1859 node->begin = from;
1860 node->children[0] = std::move(left);
1861 node->children[1] = relational_expression();
1862 node->end = accepted+1;
1863 return std::move(node);
1865 else
1867 return std::move(left);
1871 return_t equality_expression()
1873 Iter from = i;
1874 return_t left = relational_expression();
1875 if(accept(Tok_EqOp) || accept(Tok_NotEqual))
1877 using node_t = std::shared_ptr<EqualityExpression<Iter,Value,FuncType>>;
1878 node_t node;
1879 switch(accepted->type)
1881 case Tok_EqOp:
1882 node = node_t(new EqualExpression<Iter,Value,FuncType>);
1883 break;
1884 case Tok_NotEqual:
1885 node = node_t(new NotEqualExpression<Iter,Value,FuncType>);
1886 break;
1887 default:
1888 break;
1890 node->begin = from;
1891 node->children[0] = std::move(left);
1892 node->children[1] = relational_expression();
1893 node->end = accepted+1;
1894 return std::move(node);
1896 else
1898 return std::move(left);
1902 return_t logical_and_expression()
1904 Iter from = i;
1905 return_t left = equality_expression();
1906 if(accept(Tok_LogicalAnd))
1908 auto node = std::make_shared<LogicalAndExpression<Iter,Value,FuncType>>();
1909 node->begin = from;
1910 node->children[0] = std::move(left);
1911 node->children[1] = equality_expression();
1912 node->end = accepted+1;
1913 return std::move(node);
1915 else
1917 return std::move(left);
1921 return_t logical_or_expression()
1923 Iter from = i;
1924 return_t left = logical_and_expression();
1925 if(accept(Tok_LogicalOr))
1927 auto node = std::make_shared<LogicalOrExpression<Iter,Value,FuncType>>();
1928 node->begin = from;
1929 node->children[0] = std::move(left);
1930 node->children[1] = logical_and_expression();
1931 node->end = accepted+1;
1932 return std::move(node);
1934 else
1936 return std::move(left);
1940 return_t assignment_expression()
1942 Iter from = i;
1943 if(peek(Tok_Global) || peek(Tok_Local))
1945 bool global = accept(Tok_Global);
1946 bool local = accept(Tok_Local);
1947 return_t left = field_name();
1948 if(accept(Tok_Equal))
1950 std::shared_ptr<AssignmentExpression<Iter,Value,FuncType>> node;
1951 node =
1952 global ?
1953 std::make_shared<GlobalAssignmentExpression<Iter,Value,FuncType>>()
1955 local ?
1956 std::make_shared<LocalAssignmentExpression<Iter,Value,FuncType>>()
1958 std::make_shared<AssignmentExpression<Iter,Value,FuncType>>();
1959 node->begin = from;
1960 node->children[0] = std::move(left);
1961 node->children[1] = assignment_expression();
1962 node->end = accepted+1;
1963 return std::move(node);
1966 else if(peek(Tok_Identifier))
1968 return_t left = field_name();
1969 std::shared_ptr<AssignmentExpression<Iter,Value,FuncType>> node;
1970 if(accept(Tok_Equal))
1972 node = std::make_shared<AssignmentExpression<Iter,Value,FuncType>>();
1974 else if(accept(Tok_PlusEqual))
1976 node = std::make_shared<AddAssignmentExpression<Iter,Value,FuncType>>();
1978 else if(accept(Tok_DashEqual))
1980 node = std::make_shared<SubAssignmentExpression<Iter,Value,FuncType>>();
1982 else if(accept(Tok_StarEqual))
1984 node = std::make_shared<MulAssignmentExpression<Iter,Value,FuncType>>();
1986 else if(accept(Tok_SlashEqual))
1988 node = std::make_shared<DivAssignmentExpression<Iter,Value,FuncType>>();
1990 else if(accept(Tok_PercentEqual))
1992 node = std::make_shared<ModAssignmentExpression<Iter,Value,FuncType>>();
1994 else
1996 rewind(from);
1997 return logical_or_expression();
1999 node->begin = from;
2000 node->children[0] = std::move(left);
2001 node->children[1] = assignment_expression();
2002 node->end = accepted+1;
2003 return std::move(node);
2005 rewind(from);
2006 return logical_or_expression();
2009 return_t initializer_assignment_expression()
2011 auto node = std::make_shared<InitializerAssignmentExpression<Iter,Value,FuncType>>();
2012 node->begin = i;
2013 node->children.push_back(logical_or_expression());
2014 if(accept(Tok_Equal))
2016 node->children.push_back(logical_or_expression());
2018 node->end = accepted+1;
2019 return std::move(node);
2022 return_t expression()
2024 return assignment_expression();
2027 return_t block()
2029 auto node = std::make_shared<Block<Iter,Value,FuncType>>();
2030 node->begin = i;
2031 expect(Tok_LeftBrace);
2032 node->children[0] = statement_list();
2033 expect(Tok_RightBrace);
2034 node->end = accepted+1;
2035 return std::move(node);
2038 return_t if_statement()
2040 auto node = std::make_shared<IfStatement<Iter,Value,FuncType>>();
2041 node->begin = i;
2042 expect(Tok_If);
2043 expect(Tok_LeftParen);
2044 node->children.push_back(expression());
2045 expect(Tok_RightParen);
2046 node->children.push_back(statement());
2047 if(accept(Tok_Else))
2049 node->children.push_back(statement());
2051 node->end = accepted+1;
2052 return std::move(node);
2055 return_t while_statement()
2057 auto node = std::make_shared<WhileStatement<Iter,Value,FuncType>>();
2058 node->begin = i;
2059 expect(Tok_While);
2060 expect(Tok_LeftParen);
2061 node->children[0] = expression();
2062 expect(Tok_RightParen);
2063 node->children[1] = statement();
2064 node->end = accepted+1;
2065 return std::move(node);
2068 return_t nop()
2070 auto node = std::make_shared<Nop<Iter,Value,FuncType>>();
2071 node->begin = i;
2072 node->end = i;
2073 return std::move(node);
2076 return_t for_statement()
2078 Iter from = i;
2079 expect(Tok_For);
2080 expect(Tok_LeftParen);
2081 if(peek(Tok_Identifier) && (peek(Tok_Comma, 1) || peek(Tok_Colon, 1)))
2083 auto node = std::make_shared<ForEachStatement<Iter,Value,FuncType>>();
2084 node->begin = from;
2085 node->children.push_back(field_name());
2086 if(accept(Tok_Comma))
2088 node->children.push_back(field_name());
2090 expect(Tok_Colon);
2091 node->children.push_back(expression());
2092 expect(Tok_RightParen);
2093 node->children.push_back(statement());
2094 node->end = accepted+1;
2095 return std::move(node);
2097 else
2099 auto node = std::make_shared<ForStatement<Iter,Value,FuncType>>();
2100 node->begin = from;
2101 if(accept(Tok_Semicolon))
2103 node->children.push_back(nop());
2105 else
2107 node->children.push_back(expression());
2108 expect(Tok_Semicolon);
2110 if(accept(Tok_Semicolon))
2112 node->children.push_back(nop());
2114 else
2116 node->children.push_back(expression());
2117 expect(Tok_Semicolon);
2119 if(accept(Tok_RightParen))
2121 node->children.push_back(nop());
2123 else
2125 node->children.push_back(expression());
2126 expect(Tok_RightParen);
2128 node->children.push_back(statement());
2129 node->end = accepted+1;
2130 return std::move(node);
2134 return_t break_statement()
2136 auto node = std::make_shared<BreakStatement<Iter,Value,FuncType>>();
2137 node->begin = i;
2138 expect(Tok_Break);
2139 accept(Tok_Semicolon);
2140 node->end = accepted+1;
2141 return std::move(node);
2144 return_t continue_statement()
2146 auto node = std::make_shared<ContinueStatement<Iter,Value,FuncType>>();
2147 node->begin = i;
2148 expect(Tok_Continue);
2149 accept(Tok_Semicolon);
2150 node->end = accepted+1;
2151 return std::move(node);
2154 return_t return_statement()
2156 auto node = std::make_shared<ReturnStatement<Iter,Value,FuncType>>();
2157 node->begin = i;
2158 expect(Tok_Return);
2159 if(accept(Tok_Semicolon))
2161 node->end = accepted+1;
2162 return std::move(node);
2164 node->children.push_back(expression());
2165 accept(Tok_Semicolon);
2166 node->end = accepted+1;
2167 return std::move(node);
2170 return_t statement()
2172 if(peek(Tok_LeftBrace))
2174 return block();
2176 else if (peek(Tok_If))
2178 return if_statement();
2180 else if (peek(Tok_While))
2182 return while_statement();
2184 else if (peek(Tok_For))
2186 return for_statement();
2188 else if (peek(Tok_Return))
2190 return return_statement();
2192 else if (peek(Tok_Continue))
2194 return continue_statement();
2196 else if (peek(Tok_Break))
2198 return break_statement();
2200 else
2202 auto node = std::make_shared<ExpressionStatement<Iter,Value,FuncType>>();
2203 node->begin = i;
2204 node->children[0] = expression();
2205 accept(Tok_Semicolon);
2206 node->end = accepted+1;
2207 return std::move(node);
2210 return_t statement_list()
2212 auto node = std::make_shared<StatementList<Iter,Value,FuncType>>();
2213 node->begin = i;
2214 while(!end_of_input() && !peek(Tok_RightBrace))
2216 node->children.push_back(statement());
2218 node->end = accepted+1;
2219 return std::move(node);
2224 template<typename T> class Table;
2226 template<typename T> class Array;
2228 template<typename Iter, typename FuncType>
2229 struct BaseValue
2231 using NumberType = double;
2232 using StringContainer = std::string;
2233 using StringIterator = StringContainer::iterator;
2234 using NumberContainer = NumberType;
2235 using PointerContainer = std::shared_ptr<Internal::ASTNode<Iter, BaseValue, FuncType>>;
2236 using FunctionContainer = std::shared_ptr<Internal::Function<Iter,BaseValue,FuncType>>;
2237 using TreeContainer = PointerContainer;
2238 using ArrayContainer = std::shared_ptr<Array<BaseValue>>;
2239 using TableContainer = std::shared_ptr<Table<BaseValue>>;
2241 struct ProtoStringContainer
2243 StringIterator begin;
2244 StringIterator end;
2245 size_t hash;
2247 ProtoStringContainer(StringIterator begin_, StringIterator end_):
2248 begin(begin_),
2249 end(end_),
2250 hash(std::hash<StringContainer>()(StringContainer(begin_, end_)))
2255 struct UserPtrContainer
2257 void* realptr;
2259 UserPtrContainer(void* ptr): realptr(ptr)
2264 class TagReturn
2268 class TagBreak
2272 class TagContinue
2276 enum class Type
2278 Nil, Number, String, ProtoString,
2279 Tree, Function, Table, Array,
2280 Return, Break, Continue, UserPtr,
2283 private:
2284 Type m_type;
2285 union
2287 NumberContainer m_number;
2288 PointerContainer m_pointer;
2289 UserPtrContainer m_userptr;
2290 TableContainer m_table;
2291 ArrayContainer m_array;
2292 StringContainer m_strval;
2293 ProtoStringContainer m_protostr;
2296 public:
2297 Type type() const
2299 if(m_type == Type::ProtoString)
2301 return Type::String;
2303 else if((m_type >= Type::Return) && (m_type <= Type::Continue))
2305 return Type::Nil;
2307 return m_type;
2310 const char* typeName() const
2312 switch(type())
2314 case Type::Nil:
2315 return "nil";
2316 case Type::Number:
2317 return "number";
2318 case Type::String:
2319 return "string";
2320 case Type::ProtoString:
2321 return "protostring";
2322 case Type::Tree:
2323 return "tree";
2324 case Type::Function:
2325 return "function";
2326 case Type::Table:
2327 return "table";
2328 case Type::Array:
2329 return "array";
2330 case Type::Return:
2331 return "return";
2332 case Type::Break:
2333 return "break";
2334 case Type::Continue:
2335 return "continue";
2336 case Type::UserPtr:
2337 return "userptr";
2338 default:
2341 return "invalid";
2344 Type internal_type() const
2346 return m_type;
2349 const NumberType& number() const
2351 return m_number;
2354 NumberType& number()
2356 return m_number;
2359 const PointerContainer& pointer() const
2361 return m_pointer;
2364 PointerContainer& pointer()
2366 return m_pointer;
2369 void*& userptr()
2371 return m_userptr.realptr;
2374 const void* userptr() const
2376 return m_userptr.realptr;
2379 const FunctionContainer function() const
2381 return std::static_pointer_cast<Internal::Function<Iter,BaseValue,FuncType>>(m_pointer);
2384 FunctionContainer function()
2386 return std::static_pointer_cast<Internal::Function<Iter,BaseValue,FuncType>>(m_pointer);
2389 const TreeContainer tree() const
2391 return m_pointer;
2394 TreeContainer tree()
2396 return m_pointer;
2399 const TableContainer& table() const
2401 return m_table;
2404 TableContainer& table()
2406 return m_table;
2409 const StringContainer string() const
2411 if(m_type == Type::ProtoString)
2413 return std::string(m_protostr.begin, m_protostr.end);
2415 return m_strval;
2418 StringContainer& string()
2420 decay();
2421 return m_strval;
2424 const ArrayContainer& array() const
2426 return m_array;
2429 ArrayContainer& array()
2431 return m_array;
2434 void decay()
2436 if(m_type == Type::ProtoString)
2438 StringIterator tmpbegin = m_protostr.begin;
2439 StringIterator tmpend = m_protostr.end;
2440 m_type = Type::String;
2441 new (&m_strval) StringContainer(tmpbegin, tmpend);
2443 else if((m_type >= Type::Return) && (m_type <= Type::Continue))
2445 m_type = Type::Nil;
2449 BaseValue() : m_type(Type::Nil), m_number(0)
2453 BaseValue(const TagReturn&) : m_type(Type::Return), m_number(0)
2457 BaseValue(const TagBreak&) : m_type(Type::Break), m_number(0)
2461 BaseValue(const TagContinue&) : m_type(Type::Continue), m_number(0)
2465 BaseValue(NumberType value) : m_type(Type::Number), m_number(value)
2469 BaseValue(Type t, const PointerContainer &ptr) : m_type(t), m_pointer(ptr)
2473 BaseValue(UserPtrContainer ptr): m_type(Type::UserPtr), m_userptr(ptr.realptr)
2475 //fprintf(stderr, "BaseValue/userpointer: ptr=%p\n", ptr.realptr);
2478 BaseValue(const TableContainer &ptr) : m_type(Type::Table), m_table(ptr)
2482 BaseValue(const ArrayContainer &ptr) : m_type(Type::Array), m_array(ptr)
2486 BaseValue(const StringContainer &value) : m_type(Type::String), m_strval(value)
2490 BaseValue(StringIterator begin, StringIterator end) : m_type(Type::ProtoString), m_protostr(begin, end)
2494 BaseValue(const BaseValue &that) : m_type(that.m_type)
2496 switch(m_type)
2498 case Type::Return:
2499 case Type::Break:
2500 case Type::Continue:
2501 case Type::Nil:
2502 new (&m_number) NumberContainer(0);
2503 break;
2504 case Type::Number:
2505 new (&m_number) NumberContainer(that.m_number);
2506 break;
2507 case Type::String:
2508 new (&m_strval) StringContainer(that.m_strval);
2509 break;
2510 case Type::ProtoString:
2511 new (&m_protostr) ProtoStringContainer(that.m_protostr);
2512 break;
2513 case Type::Tree:
2514 case Type::Function:
2515 new (&m_pointer) PointerContainer(that.m_pointer);
2516 break;
2517 case Type::Table:
2518 new (&m_table) TableContainer(that.m_table);
2519 break;
2520 case Type::Array:
2521 new (&m_array) ArrayContainer(that.m_array);
2522 break;
2523 case Type::UserPtr:
2524 #if 0
2525 fprintf(stderr, "BaseValue(BaseValue&): this.userptr=%p, that.userptr=%p\n",
2526 m_userptr.realptr, that.m_userptr.realptr
2528 #endif
2529 new (&m_userptr) UserPtrContainer(that.m_userptr);
2530 break;
2531 default:
2532 break;
2536 BaseValue& operator=(const BaseValue &that)
2538 switch(m_type)
2540 case Type::String:
2541 m_strval.~StringContainer();
2542 break;
2543 case Type::Tree:
2544 case Type::Function:
2545 m_pointer.~PointerContainer();
2546 break;
2547 case Type::Table:
2548 m_table.~TableContainer();
2549 break;
2550 case Type::Array:
2551 m_array.~ArrayContainer();
2552 break;
2553 default:
2554 break;
2556 m_type = that.m_type;
2557 switch(m_type)
2559 case Type::Return:
2560 case Type::Break:
2561 case Type::Continue:
2562 case Type::Nil:
2563 new (&m_number) NumberContainer(0);
2564 break;
2565 case Type::Number:
2566 new (&m_number) NumberContainer(that.m_number);
2567 break;
2568 case Type::String:
2569 new (&m_strval) StringContainer(that.m_strval);
2570 break;
2571 case Type::ProtoString:
2572 new (&m_protostr) ProtoStringContainer(that.m_protostr);
2573 break;
2574 case Type::Tree:
2575 case Type::Function:
2576 new (&m_pointer) PointerContainer(that.m_pointer);
2577 break;
2578 case Type::UserPtr:
2579 #if 0
2580 fprintf(stderr, "BaseValue::operator=(BaseValue&): this.userptr=%p, that.userptr=%p\n",
2581 m_userptr.realptr, that.m_userptr.realptr
2583 #endif
2584 new(&m_userptr) UserPtrContainer(that.m_userptr);
2585 break;
2586 case Type::Table:
2587 new (&m_table) TableContainer(that.m_table);
2588 break;
2589 case Type::Array:
2590 new (&m_array) ArrayContainer(that.m_array);
2591 break;
2592 default:
2593 break;
2595 return *this;
2598 ~BaseValue()
2600 switch(m_type)
2602 case Type::String:
2603 m_strval.~StringContainer();
2604 break;
2605 case Type::Tree:
2606 case Type::Function:
2607 m_pointer.~PointerContainer();
2608 break;
2609 case Type::Table:
2610 m_table.~TableContainer();
2611 break;
2612 case Type::Array:
2613 m_array.~ArrayContainer();
2614 break;
2615 default:
2616 break;
2620 bool operator==(const BaseValue &that) const
2622 if(type() != that.type())
2624 return false;
2626 switch(m_type)
2628 case Type::Return:
2629 case Type::Break:
2630 case Type::Continue:
2631 case Type::Nil:
2632 case Type::Number:
2633 return number() == that.number();
2634 case Type::Tree:
2635 case Type::Function:
2636 return pointer() == that.pointer();
2637 case Type::Table:
2638 return table() == that.table();
2639 case Type::Array:
2640 return array() == that.array();
2641 case Type::UserPtr:
2642 return (userptr() == that.userptr());
2643 case Type::String:
2644 if(that.m_type == Type::String)
2646 return m_strval == that.m_strval;
2648 else
2650 if(m_strval.end()-m_strval.begin() != that.m_protostr.end - that.m_protostr.begin)
2652 return false;
2654 return std::equal(m_strval.begin(), m_strval.end(), that.m_protostr.begin);
2656 case Type::ProtoString:
2657 if(that.m_type == Type::String)
2659 if(m_protostr.end - m_protostr.begin != that.m_strval.end() - that.m_strval.begin())
2661 return false;
2663 return std::equal(m_protostr.begin, m_protostr.end, that.m_strval.begin());
2665 else
2667 if(m_protostr.end - m_protostr.begin != that.m_protostr.end - that.m_protostr.begin)
2669 return false;
2671 return std::equal(m_protostr.begin, m_protostr.end, that.m_protostr.begin);
2673 default:
2674 return false;
2678 friend struct std::hash<BaseValue<Iter, FuncType>>;
2681 namespace std
2683 template<typename Iter, typename FuncType>
2684 struct hash<BaseValue<Iter, FuncType>>
2686 public:
2687 using T = BaseValue<Iter, FuncType>;
2688 std::size_t operator()(BaseValue<Iter, FuncType> const& x) const
2690 switch(x.m_type)
2692 case T::Type::Nil:
2693 case T::Type::Number:
2694 return std::hash<typename T::NumberContainer>()(x.number());
2695 case T::Type::String:
2696 return std::hash<typename T::StringContainer>()(x.string());
2697 case T::Type::ProtoString:
2698 return x.m_protostr.hash;
2699 case T::Type::UserPtr:
2700 return std::hash<const void*>()(x.userptr());;
2701 case T::Type::Tree:
2702 case T::Type::Function:
2703 return std::hash<typename T::PointerContainer>()(x.pointer());
2704 case T::Type::Table:
2705 return std::hash<typename T::TableContainer>()(x.table());
2706 case T::Type::Array:
2707 return std::hash<typename T::ArrayContainer>()(x.array());
2708 default:
2709 return 0;
2715 template<class Iter, class FuncType>
2716 std::ostream& operator<<(std::ostream &out, const BaseValue<Iter,FuncType> &v)
2718 using T = BaseValue<Iter,FuncType>;
2719 switch(v.type())
2721 case T::Type::Nil:
2722 return out << "nil";
2723 case T::Type::Number:
2724 return out << v.number();
2725 case T::Type::String:
2726 return out << v.string();
2727 case T::Type::Tree:
2728 return out << "tree@" << v.tree();
2729 case T::Type::Function:
2730 return out << "function@" << v.function();
2731 case T::Type::Table:
2732 return out << "table@" << v.table();
2733 case T::Type::Array:
2734 return out << "array@" << v.array();
2735 case T::Type::UserPtr:
2736 return out << "userpointer@" << v.userptr();
2737 default:
2738 return out;
2742 template<typename Type> class Table
2744 public:
2745 using Iterator = typename std::unordered_map<Type, Type>::iterator;
2747 private:
2748 std::unordered_map<Type, Type> m_data;
2749 Type m_nil;
2751 public:
2752 void set(const Type& key, const Type& value)
2754 if(key.type() == Type::Type::Nil)
2756 return;
2758 if(value.type() == Type::Type::Nil)
2760 m_data.erase(key);
2762 m_data[key] = value;
2765 Type get(const Type& key)
2767 auto iter = m_data.find(key);
2768 if(iter == m_data.end())
2770 return m_nil;
2772 else
2774 return iter->second;
2778 void append(const Table& that)
2780 m_data.insert(that.m_data.begin(), that.m_data.end());
2783 size_t size() const
2785 return m_data.size();
2788 Iterator begin()
2790 return m_data.begin();
2793 Iterator end()
2795 return m_data.end();
2800 template<typename Type>
2801 class Array
2803 public:
2804 using Iterator = typename std::vector<Type>::iterator;
2806 private:
2807 std::vector<Type> m_data;
2808 Type m_nil;
2810 public:
2811 void set(const Type& key, const Type& value)
2813 int index = key.number();
2814 m_data[index] = value;
2817 Type get(const Type& key)
2819 int index = key.number();
2820 return m_data[index];
2823 void append(const Array& that)
2825 m_data.insert(m_data.end(), that.m_data.begin(), that.m_data.end());
2828 void add(const Type& value)
2830 m_data.push_back(value);
2833 size_t size() const
2835 return m_data.size();
2838 Iterator begin()
2840 return m_data.begin();
2843 Iterator end()
2845 return m_data.end();
2849 class Interpreter
2851 public:
2852 using Iter = std::vector<Internal::Token<std::string::iterator>>::iterator;
2853 using FuncType = Interpreter;
2854 using Value = BaseValue<Iter, FuncType>;
2855 using Environment = std::shared_ptr<Table<Value>>;
2856 using EnvStack = std::vector<Environment>;
2857 using FunStack = std::vector<size_t>;
2858 using ParserInstance = Internal::Parser<Value, Interpreter>;
2860 struct FunctionScope
2862 Interpreter* m_self;
2864 FunctionScope(Interpreter* self): m_self(self)
2866 m_self->beginFunctionScope();
2869 ~FunctionScope()
2871 m_self->endFunctionScope();
2875 struct LocalScope
2877 Interpreter& m_self;
2879 LocalScope(Interpreter& self): m_self(self)
2881 m_self.beginLocalScope();
2884 ~LocalScope()
2886 m_self.endLocalScope();
2890 private:
2891 ParserInstance m_parser;
2892 std::vector<Value> m_function_stack;
2893 std::vector<std::unordered_map<std::string, Value>> m_scopes;
2894 EnvStack m_envstack;
2895 FunStack m_funstack;
2897 public:
2898 Value operator()(Internal::Variable<Iter,Value,FuncType> &node);
2900 Value operator()(Internal::FieldName<Iter,Value,FuncType> &node);
2902 Value operator()(Internal::Constant<Iter,Value,FuncType> &node);
2904 Value operator()(Internal::Nop<Iter,Value,FuncType> &);
2906 Value operator()(Internal::Nil<Iter,Value,FuncType> &);
2908 Value operator()(Internal::String<Iter,Value,FuncType> &node);
2910 Value operator()(Internal::Parens<Iter,Value,FuncType> &node);
2912 Value operator()(Internal::ExpressionStatement<Iter,Value,FuncType> &node);
2914 Value operator()(Internal::Type<Iter,Value,FuncType> &node);
2916 Value operator()(Internal::Size<Iter,Value,FuncType> &node);
2918 Value operator()(Internal::FieldAccess<Iter,Value,FuncType> &node);
2920 Value operator()(Internal::FieldAssignment<Iter,Value,FuncType> &node);
2922 Value operator()(Internal::AddFieldAssignment<Iter,Value,FuncType> &node);
2924 Value operator()(Internal::SubFieldAssignment<Iter,Value,FuncType> &node);
2926 Value operator()(Internal::MulFieldAssignment<Iter,Value,FuncType> &node);
2928 Value operator()(Internal::DivFieldAssignment<Iter,Value,FuncType> &node);
2930 Value operator()(Internal::ModFieldAssignment<Iter,Value,FuncType> &node);
2932 Value operator()(Internal::UnaryPlusExpression<Iter,Value,FuncType> &node);
2934 Value operator()(Internal::UnaryMinusExpression<Iter,Value,FuncType> &node);
2936 Value operator()(Internal::UnaryNotExpression<Iter,Value,FuncType> &node);
2938 Value operator()(Internal::MultiplyExpression<Iter,Value,FuncType> &node);
2940 Value operator()(Internal::DivideExpression<Iter,Value,FuncType> &node);
2942 Value operator()(Internal::ModuloExpression<Iter,Value,FuncType> &node);
2944 Value operator()(Internal::AddExpression<Iter,Value,FuncType> &node);
2946 Value operator()(Internal::SubtractExpression<Iter,Value,FuncType> &node);
2948 Value operator()(Internal::LessExpression<Iter,Value,FuncType> &node);
2950 Value operator()(Internal::LessEqualExpression<Iter,Value,FuncType> &node);
2952 Value operator()(Internal::GreaterExpression<Iter,Value,FuncType> &node);
2954 Value operator()(Internal::GreaterEqualExpression<Iter,Value,FuncType> &node);
2956 Value operator()(Internal::EqualExpression<Iter,Value,FuncType> &node);
2958 Value operator()(Internal::NotEqualExpression<Iter,Value,FuncType> &node);
2960 Value operator()(Internal::LogicalAndExpression<Iter,Value,FuncType> &node);
2962 Value operator()(Internal::LogicalOrExpression<Iter,Value,FuncType> &node);
2964 Value operator()(Internal::AssignmentExpression<Iter,Value,FuncType> &node);
2966 Value operator()(Internal::GlobalAssignmentExpression<Iter,Value,FuncType> &node);
2968 Value operator()(Internal::LocalAssignmentExpression<Iter,Value,FuncType> &node);
2970 Value operator()(Internal::AddAssignmentExpression<Iter,Value,FuncType> &node);
2972 Value operator()(Internal::SubAssignmentExpression<Iter,Value,FuncType> &node);
2974 Value operator()(Internal::MulAssignmentExpression<Iter,Value,FuncType> &node);
2976 Value operator()(Internal::DivAssignmentExpression<Iter,Value,FuncType> &node);
2978 Value operator()(Internal::ModAssignmentExpression<Iter,Value,FuncType> &node);
2980 Value operator()(Internal::Block<Iter,Value,FuncType> &node);
2982 Value operator()(Internal::TableInitializer<Iter,Value,FuncType> &node);
2984 Value operator()(Internal::ArrayInitializer<Iter,Value,FuncType> &node);
2986 Value operator()(Internal::Function<Iter,Value,FuncType> &node);
2988 Value operator()(Internal::ReturnStatement<Iter,Value,FuncType> &node);
2990 Value operator()(Internal::IdentifierList<Iter,Value,FuncType> &node);
2992 Value operator()(Internal::BuiltinFunction<Iter,Value,FuncType> &node);
2994 Value operator()(Internal::Call<Iter,Value,FuncType> &node);
2996 Value operator()(Internal::MemberCall<Iter,Value,FuncType> &node);
2998 Value operator()(Internal::EvalStatement<Iter,Value,FuncType> &node);
3000 Value operator()(Internal::Eval<Iter,Value,FuncType> &node);
3002 Value operator()(Internal::Tree<Iter,Value,FuncType> &node);
3004 Value operator()(Internal::Root<Iter,Value,FuncType> &node);
3006 Value operator()(Internal::ParseStatement<Iter,Value,FuncType> &node);
3008 Value operator()(Internal::ParseExpression<Iter,Value,FuncType> &node);
3010 Value operator()(Internal::StatementList<Iter,Value,FuncType> &node);
3012 Value operator()(Internal::IfStatement<Iter,Value,FuncType> &node);
3014 Value operator()(Internal::WhileStatement<Iter,Value,FuncType> &node);
3016 Value operator()(Internal::ForStatement<Iter,Value,FuncType> &node);
3018 Value operator()(Internal::ForEachStatement<Iter,Value,FuncType> &node);
3020 Value operator()(Internal::ContinueStatement<Iter,Value,FuncType> &);
3022 Value operator()(Internal::BreakStatement<Iter,Value,FuncType> &);
3024 template<class Type>
3025 Value operator()(Type& node)
3027 error("unimplemented ast node.", node);
3028 return Value();
3031 Value makeString(const std::string& src)
3033 return Value(src);
3036 std::shared_ptr<Array<Value>> makeArray()
3038 return std::make_shared<Array<Value>>();
3041 std::shared_ptr<Table<Value>> makeTable()
3043 return std::make_shared<Table<Value>>();
3046 Value makeFunction(std::function<void(Interpreter&, std::vector<Value>&)> fun)
3048 auto node = std::make_shared<Internal::Function<Iter,Value,FuncType>>();
3049 node->begin = node->end;
3050 auto nop = std::make_shared<Internal::Nop<Iter,Value,FuncType>>();
3051 nop->begin = nop->end;
3052 node->children[0] = nop;
3053 auto builtin = std::make_shared<Internal::BuiltinFunction<Iter,Value,FuncType>>();
3054 builtin->begin = builtin->end;
3055 builtin->function = fun;
3056 node->children[1] = builtin;
3057 return Value(Value::Type::Function, node);
3060 template<typename Type>
3061 Value makeUserPointer(Type* ptr)
3063 return Value(Value::UserPtrContainer((void*)ptr));
3066 void beginLocalScope()
3068 m_envstack.push_back(std::make_shared<Table<Value>>());
3071 void endLocalScope()
3073 m_envstack.pop_back();
3076 void beginFunctionScope()
3078 m_funstack.push_back(m_envstack.size());
3079 m_envstack.push_back(std::make_shared<Table<Value>>());
3082 void endFunctionScope()
3084 m_envstack.pop_back();
3085 m_funstack.pop_back();
3088 Value get(Value key)
3090 int top = m_envstack.size()-1;
3091 int bottom = m_funstack.back();
3092 for(int i = top; i>=bottom; --i)
3094 Value value = m_envstack[i]->get(key);
3095 if(value.type() != Value::Type::Nil)
3097 return value;
3100 return m_envstack[0]->get(key);
3103 void set(Value key, Value value)
3105 m_envstack[m_funstack.back()]->set(key, value);
3108 void setLocal(Value key, Value value)
3110 m_envstack.back()->set(key, value);
3113 void setGlobal(Value key, Value value)
3115 m_envstack[0]->set(key, value);
3118 template<class T>
3119 void error(const std::string text, T &node)
3121 auto lc = Internal::line_column(node.source->begin(), node.begin->begin);
3122 auto from = node.begin;
3123 auto to = node.end-1;
3124 throw std::runtime_error(
3125 std::to_string(lc.first)+":"+std::to_string(lc.second)+
3126 ": error: " + text + " in: '" + std::string(from->begin, to->end) + "'");
3129 void addFunction(const std::string &name, std::function<void(Interpreter&, std::vector<Value>&)> fun)
3131 setGlobal(Value(name), makeFunction(fun));
3134 Interpreter()
3136 beginFunctionScope();
3139 ~Interpreter()
3141 endFunctionScope();
3144 Value run(const std::string &source)
3146 auto root = m_parser(source.begin(), source.end());
3147 Value result = root->accept(*this);
3148 return result;