more cleanup in cmakelists
[aqualang.git] / include / aqua.h
blob081c8746d7da416e09272d1782c75ccd257e67b7
2 #include <iostream>
3 #include <fstream>
4 #include <sstream>
5 #include <string>
6 #include <vector>
7 #include <array>
8 #include <functional>
9 #include <unordered_map>
10 #include <stdexcept>
11 #include <memory>
12 #include <cctype>
13 #include <cmath>
15 namespace Aqua
17 namespace Utils
19 std::string Unescape(const std::string &str);
21 // helper to find line & column position on demand
22 template<class Iter>
23 std::pair<int, int> LineColumn(Iter begin, Iter pos)
25 int line = 1;
26 int column = 0;
27 for(Iter i = begin; i!=pos; ++i)
29 if(*i == '\n')
31 ++line;
32 column = 0;
34 ++column;
36 return std::make_pair(line, column);
40 class Interpreter;
42 namespace Internal
44 enum TokenType
46 Tok_Number, Tok_Identifier, Tok_String,
47 Tok_LeftParen, Tok_RightParen, Tok_LeftBracket,
48 Tok_RightBracket, Tok_LeftBrace, Tok_RightBrace,
49 Tok_Plus, Tok_Dash, Tok_Star,
50 Tok_Percent, Tok_Slash, Tok_PlusEqual,
51 Tok_DashEqual, Tok_StarEqual, Tok_PercentEqual,
52 Tok_SlashEqual, Tok_Not, Tok_Comma,
53 Tok_Semicolon, Tok_Dot, Tok_Colon,
54 Tok_Equal, Tok_EqOp, Tok_NotEqual,
55 Tok_Greater, Tok_GreaterEqual, Tok_Less,
56 Tok_LessEqual, Tok_LogicalAnd, Tok_LogicalOr,
57 Tok_If, Tok_Else, Tok_While,
58 Tok_For, Tok_Return, Tok_Break,
59 Tok_Continue, Tok_Function, Tok_Expression,
60 Tok_Statement, Tok_Root, Tok_Nil,
61 Tok_Eval, Tok_ParseStatement, Tok_ParseExpression,
62 Tok_Global, Tok_Local, Tok_Print,
63 Tok_TypeOf, Tok_SizeOf, Tok_Switch,
64 Tok_Case, Tok_Import,
67 static const std::unordered_map<std::string, TokenType> keywords =
69 {"if", Tok_If},
70 {"else", Tok_Else},
71 {"while", Tok_While},
72 {"for", Tok_For},
73 {"return", Tok_Return},
74 {"break", Tok_Break},
75 {"continue", Tok_Continue},
76 {"func", Tok_Function},
77 {"nil", Tok_Nil},
78 {"expression", Tok_Expression},
79 {"statement", Tok_Statement},
80 {"eval", Tok_Eval},
81 {"parse_statement", Tok_ParseStatement},
82 {"parse_expression", Tok_ParseExpression},
83 {"global", Tok_Global},
84 {"local", Tok_Local},
85 {"root", Tok_Root},
86 {"typeof", Tok_TypeOf},
87 {"sizeof", Tok_SizeOf},
89 // these aren't implemented yet
90 {"switch", Tok_Switch},
91 {"case", Tok_Case},
92 {"import", Tok_Import},
95 template<class Iter>
96 struct Token
98 const TokenType type;
99 const Iter begin;
100 const Iter end;
102 Token(TokenType type_, Iter begin_, Iter end_):
103 type(type_),
104 begin(begin_),
105 end(end_)
110 template<class Iter>
111 std::shared_ptr<std::vector<Token<Iter>>> tokenize(Iter begin, Iter end)
113 auto tokens_ptr = std::make_shared<std::vector<Token<Iter>>>();
114 std::vector<Token<Iter>> &tokens = *tokens_ptr;
115 Iter i = begin;
116 while(i!=end)
118 Iter start = i;
119 switch(*i)
121 case ' ':
122 case '\t':
123 case '\n': // ignore whitespaces
124 ++i;
125 break;
126 case '(':
127 tokens.emplace_back(Tok_LeftParen, i, i+1);
128 ++i;
129 break;
130 case ')':
131 tokens.emplace_back(Tok_RightParen, i, i+1);
132 ++i;
133 break;
134 case '[':
135 tokens.emplace_back(Tok_LeftBracket, i, i+1);
136 ++i;
137 break;
138 case ']':
139 tokens.emplace_back(Tok_RightBracket, i, i+1);
140 ++i;
141 break;
142 case '{':
143 tokens.emplace_back(Tok_LeftBrace, i, i+1);
144 ++i;
145 break;
146 case '}':
147 tokens.emplace_back(Tok_RightBrace, i, i+1);
148 ++i;
149 break;
150 case ',':
151 tokens.emplace_back(Tok_Comma, i, i+1);
152 ++i;
153 break;
154 case '.':
155 tokens.emplace_back(Tok_Dot, i, i+1);
156 ++i;
157 break;
158 case ';':
159 tokens.emplace_back(Tok_Semicolon, i, i+1);
160 ++i;
161 break;
162 case ':':
163 tokens.emplace_back(Tok_Colon, i, i+1);
164 ++i;
165 break;
166 case '+':
167 if(i+1 != end && *(i+1) == '=')
169 tokens.emplace_back(Tok_PlusEqual, i, i+2);
170 i+=2;
172 else
174 tokens.emplace_back(Tok_Plus, i, i+1);
175 ++i;
177 break;
178 case '-':
179 if(i+1 != end && *(i+1) == '=')
181 tokens.emplace_back(Tok_DashEqual, i, i+2);
182 i+=2;
184 else
186 tokens.emplace_back(Tok_Dash, i, i+1);
187 ++i;
189 break;
190 case '*':
191 if(i+1 != end && *(i+1) == '=')
193 tokens.emplace_back(Tok_StarEqual, i, i+2);
194 i+=2;
196 else
198 tokens.emplace_back(Tok_Star, i, i+1);
199 ++i;
201 break;
202 case '%':
203 if(i+1 != end && *(i+1) == '=')
205 tokens.emplace_back(Tok_PercentEqual, i, i+2);
206 i+=2;
208 else
210 tokens.emplace_back(Tok_Percent, i, i+1);
211 ++i;
213 break;
214 case '/':
215 if(i+1 != end && *(i+1) == '/')
217 ++i;
218 while(i!=end && *i != '\n')
220 ++i;
221 } ++i;
223 else if(i+1 != end && *(i+1) == '=')
225 tokens.emplace_back(Tok_SlashEqual, i, i+2);
226 i+=2;
228 else
230 tokens.emplace_back(Tok_Slash, i, i+1);
231 ++i;
233 break;
234 case '<':
235 if(i+1 != end && *(i+1) == '=')
237 tokens.emplace_back(Tok_LessEqual, i, i+2);
238 i+=2;
240 else
242 tokens.emplace_back(Tok_Less, i, i+1);
243 ++i;
245 break;
246 case '>':
247 if(i+1 != end && *(i+1) == '=')
249 tokens.emplace_back(Tok_GreaterEqual, i, i+2);
250 i+=2;
252 else
254 tokens.emplace_back(Tok_Greater, i, i+1);
255 ++i;
257 break;
258 case '=':
259 if(i+1 != end && *(i+1) == '=')
261 tokens.emplace_back(Tok_EqOp, i, i+2);
262 i+=2;
264 else
266 tokens.emplace_back(Tok_Equal, i, i+1);
267 ++i;
269 break;
270 case '!':
271 if(i+1 != end && *(i+1) == '=')
273 tokens.emplace_back(Tok_NotEqual, i, i+2);
274 i+=2;
276 else
278 tokens.emplace_back(Tok_Not, i, i+1);
279 i+=2;
281 break;
282 case '&':
283 if(i+1 != end && *(i+1) == '&')
285 tokens.emplace_back(Tok_LogicalAnd, i, i+2);
286 i+=2;
288 else
290 auto lc = Utils::LineColumn(begin, i);
291 throw std::runtime_error(
292 std::to_string(lc.first) + ":" +
293 std::to_string(lc.second) + ": unrecognized symbol: '" + *i + "'"
296 break;
297 case '|':
298 if(i+1 != end && *(i+1) == '|')
300 tokens.emplace_back(Tok_LogicalOr, i, i+2);
301 i+=2;
303 else
305 auto lc = Utils::LineColumn(begin, i);
306 throw std::runtime_error(
307 std::to_string(lc.first) + ":" +
308 std::to_string(lc.second) + ": unrecognized symbol: '" + *i + "'"
311 break;
312 case '"':
313 ++i;
314 while(i!=end && !(*i == '"' && *(i-1) != '\\'))
316 ++i;
318 ++i;
319 tokens.emplace_back(Tok_String, start, i);
320 break;
321 default:
322 if(std::isdigit(*i))
324 while(i!=end && std::isdigit(*i))
326 ++i;
328 if(i!=end && (*i == '.'))
330 ++i;
331 while((i != end) && std::isdigit(*i))
333 ++i;
336 if((i != end) && (*i == 'e'))
338 ++i;
339 if((i != end) && (*i == '-'))
341 ++i;
343 while(i!=end && std::isdigit(*i))
345 ++i;
348 tokens.emplace_back(Tok_Number, start, i);
349 break;
351 else if(std::isalpha(*i) || (*i == '_') || (*i == '$'))
353 ++i;
354 while(i!=end && (std::isalnum(*i) || *i == '_'))
356 ++i;
358 auto kw = keywords.find(std::string(start, i));
359 if(kw == keywords.end())
361 tokens.emplace_back(Tok_Identifier, start, i);
363 else
365 tokens.emplace_back(kw->second, start, i);
367 break;
369 else
371 auto lc = Utils::LineColumn(begin, i);
372 throw std::runtime_error(
373 std::to_string(lc.first) + ":" +
374 std::to_string(lc.second) + ": unrecognized symbol: '" + *i + "'"
379 return tokens_ptr;
382 // Abstract Syntax Tree node types
383 template<typename Iter, typename Value, typename FuncType>
384 struct ASTNode: public std::enable_shared_from_this<ASTNode<Iter,Value,FuncType>>
386 Iter begin;
387 Iter end;
388 std::shared_ptr<std::string> source;
389 std::shared_ptr<std::vector<Token<std::string::iterator>>> tokens;
390 std::weak_ptr<ASTNode<Iter,Value,FuncType>> root;
392 virtual Value accept(FuncType&) = 0;
394 virtual std::shared_ptr<ASTNode<Iter,Value,FuncType>>& operator[](size_t) = 0;
396 virtual size_t size() const = 0;
398 virtual void inject_dependencies() = 0;
401 template<class Iter, class Value, class FuncType>
402 struct Variadic : public ASTNode<Iter,Value,FuncType>
404 std::vector<std::shared_ptr<ASTNode<Iter,Value,FuncType>>> children;
406 virtual Value accept(FuncType &f)
408 return f(*this);
411 virtual std::shared_ptr<ASTNode<Iter,Value,FuncType>>& operator[](size_t i)
413 return children[i];
416 virtual size_t size() const
418 return children.size();
421 virtual void inject_dependencies()
423 for(auto& child : children)
425 child->root = this->root;
426 child->source = this->source;
427 child->tokens = this->tokens;
428 child->inject_dependencies();
433 template<class Iter, class Value, class FuncType>
434 struct Ternary : public ASTNode<Iter,Value,FuncType>
436 std::array<std::shared_ptr<ASTNode<Iter,Value,FuncType>>, 3> children;
438 virtual Value accept(FuncType &f)
440 return f(*this);
443 virtual std::shared_ptr<ASTNode<Iter,Value,FuncType>>& operator[](size_t i)
445 return children[i];
448 virtual size_t size() const
450 return children.size();
453 virtual void inject_dependencies()
455 for(auto& child : children)
457 child->root = this->root;
458 child->source = this->source;
459 child->tokens = this->tokens;
460 child->inject_dependencies();
465 template<class Iter, class Value, class FuncType>
466 struct Binary : public ASTNode<Iter,Value,FuncType>
468 std::array<std::shared_ptr<ASTNode<Iter,Value,FuncType>>, 2> children;
470 virtual Value accept(FuncType &f)
472 return f(*this);
475 virtual std::shared_ptr<ASTNode<Iter,Value,FuncType>>& operator[](size_t i)
477 return children[i];
480 virtual size_t size() const
482 return children.size();
485 virtual void inject_dependencies()
487 for(auto& child : children)
489 child->root = this->root;
490 child->source = this->source;
491 child->tokens = this->tokens;
492 child->inject_dependencies();
497 template<class Iter, class Value, class FuncType>
498 struct Unary : public ASTNode<Iter,Value,FuncType>
500 std::array<std::shared_ptr<ASTNode<Iter,Value,FuncType>>, 1> children;
502 virtual Value accept(FuncType &f)
504 return f(*this);
507 virtual std::shared_ptr<ASTNode<Iter,Value,FuncType>>& operator[](size_t i)
509 return children[i];
512 virtual size_t size() const
514 return children.size();
517 virtual void inject_dependencies()
519 for(auto& child : children)
521 child->root = this->root;
522 child->source = this->source;
523 child->tokens = this->tokens;
524 child->inject_dependencies();
529 template<class Iter, class Value, class FuncType>
530 struct Nullary : public ASTNode<Iter,Value,FuncType>
532 std::array<std::shared_ptr<ASTNode<Iter,Value,FuncType>>, 0> children;
534 virtual Value accept(FuncType &f)
536 return f(*this);
539 virtual std::shared_ptr<ASTNode<Iter,Value,FuncType>>& operator[](size_t i)
541 return children[i];
544 virtual size_t size() const
546 return children.size();
549 virtual void inject_dependencies()
551 for(auto& child : children)
553 child->root = this->root;
554 child->source = this->source;
555 child->tokens = this->tokens;
556 child->inject_dependencies();
561 template<class Iter, class Value, class FuncType>
562 struct Variable : public Nullary<Iter,Value,FuncType>
564 Value name;
566 virtual Value accept(FuncType &f)
568 return f(*this);
572 template<class Iter, class Value, class FuncType>
573 struct GlobalVariable : public Variable<Iter,Value,FuncType>
575 virtual Value accept(FuncType &f)
577 return f(*this);
581 template<class Iter, class Value, class FuncType>
582 struct LocalVariable : public Variable<Iter,Value,FuncType>
584 virtual Value accept(FuncType &f)
586 return f(*this);
590 template<class Iter, class Value, class FuncType>
591 struct FieldName : public Nullary<Iter,Value,FuncType>
593 Value name;
594 virtual Value accept(FuncType &f)
596 return f(*this);
600 template<class Iter, class Value, class FuncType>
601 struct Nop : public Nullary<Iter,Value,FuncType>
603 double value;
604 virtual Value accept(FuncType &f)
606 return f(*this);
610 template<class Iter, class Value, class FuncType>
611 struct Constant : public Nullary<Iter,Value,FuncType>
613 Value value;
615 virtual Value accept(FuncType &f)
617 return f(*this);
621 template<class Iter, class Value, class FuncType>
622 struct Nil : public Nullary<Iter,Value,FuncType>
624 virtual Value accept(FuncType &f)
626 return f(*this);
630 template<class Iter, class Value, class FuncType>
631 struct String : public Nullary<Iter,Value,FuncType>
633 Value value;
635 virtual Value accept(FuncType &f)
637 return f(*this);
641 template<class Iter, class Value, class FuncType>
642 struct FieldAccess : public Binary<Iter,Value,FuncType>
644 virtual Value accept(FuncType &f)
646 return f(*this);
650 template<class Iter, class Value, class FuncType>
651 struct FieldAssignment : public Ternary<Iter,Value,FuncType>
653 virtual Value accept(FuncType &f)
655 return f(*this);
659 template<class Iter, class Value, class FuncType>
660 struct AddFieldAssignment : public FieldAssignment<Iter,Value,FuncType>
662 virtual Value accept(FuncType &f)
664 return f(*this);
668 template<class Iter, class Value, class FuncType>
669 struct SubFieldAssignment : public FieldAssignment<Iter,Value,FuncType>
671 virtual Value accept(FuncType &f)
673 return f(*this);
677 template<class Iter, class Value, class FuncType>
678 struct MulFieldAssignment : public FieldAssignment<Iter,Value,FuncType>
680 virtual Value accept(FuncType &f)
682 return f(*this);
686 template<class Iter, class Value, class FuncType>
687 struct DivFieldAssignment : public FieldAssignment<Iter,Value,FuncType>
689 virtual Value accept(FuncType &f)
691 return f(*this);
695 template<class Iter, class Value, class FuncType>
696 struct ModFieldAssignment : public FieldAssignment<Iter,Value,FuncType>
698 virtual Value accept(FuncType &f)
700 return f(*this);
704 template<class Iter, class Value, class FuncType>
705 struct Call : public Variadic<Iter,Value,FuncType>
707 virtual Value accept(FuncType &f)
709 return f(*this);
713 template<class Iter, class Value, class FuncType>
714 struct MemberCall : public Variadic<Iter,Value,FuncType>
716 virtual Value accept(FuncType &f)
718 return f(*this);
723 template<class Iter, class Value, class FuncType>
724 struct EvalStatement : public Unary<Iter,Value,FuncType>
726 virtual Value accept(FuncType &f)
728 return f(*this);
733 template<class Iter, class Value, class FuncType>
734 struct Eval : public Unary<Iter,Value,FuncType>
736 virtual Value accept(FuncType &f)
738 return f(*this);
742 template<class Iter, class Value, class FuncType>
743 struct ParseStatement : public Unary<Iter,Value,FuncType>
745 virtual Value accept(FuncType &f)
747 return f(*this);
751 template<class Iter, class Value, class FuncType>
752 struct ParseExpression : public Unary<Iter,Value,FuncType>
754 virtual Value accept(FuncType &f)
756 return f(*this);
760 template<class Iter, class Value, class FuncType>
761 struct UnaryExpression : public Unary<Iter,Value,FuncType>
763 virtual Value accept(FuncType &f)
765 return f(*this);
769 template<class Iter, class Value, class FuncType>
770 struct UnaryPlusExpression : public UnaryExpression<Iter,Value,FuncType>
772 virtual Value accept(FuncType &f)
774 return f(*this);
778 template<class Iter, class Value, class FuncType>
779 struct UnaryMinusExpression : public UnaryExpression<Iter,Value,FuncType>
781 virtual Value accept(FuncType &f)
783 return f(*this);
787 template<class Iter, class Value, class FuncType>
788 struct UnaryNotExpression : public UnaryExpression<Iter,Value,FuncType>
790 virtual Value accept(FuncType &f)
792 return f(*this);
796 template<class Iter, class Value, class FuncType>
797 struct MultiplicativeExpression : public Binary<Iter,Value,FuncType>
799 virtual Value accept(FuncType &f)
801 return f(*this);
805 template<class Iter, class Value, class FuncType>
806 struct MultiplyExpression : public MultiplicativeExpression<Iter,Value,FuncType>
808 virtual Value accept(FuncType &f)
810 return f(*this);
814 template<class Iter, class Value, class FuncType>
815 struct DivideExpression : public MultiplicativeExpression<Iter,Value,FuncType>
817 virtual Value accept(FuncType &f)
819 return f(*this);
823 template<class Iter, class Value, class FuncType>
824 struct ModuloExpression : public MultiplicativeExpression<Iter,Value,FuncType>
826 virtual Value accept(FuncType &f)
828 return f(*this);
832 template<class Iter, class Value, class FuncType>
833 struct AdditiveExpression : public Binary<Iter,Value,FuncType>
835 virtual Value accept(FuncType &f)
837 return f(*this);
841 template<class Iter, class Value, class FuncType>
842 struct AddExpression : public AdditiveExpression<Iter,Value,FuncType>
844 virtual Value accept(FuncType &f)
846 return f(*this);
850 template<class Iter, class Value, class FuncType>
851 struct SubtractExpression : public AdditiveExpression<Iter,Value,FuncType>
853 virtual Value accept(FuncType &f)
855 return f(*this);
859 template<class Iter, class Value, class FuncType>
860 struct RelationalExpression : public Binary<Iter,Value,FuncType>
862 virtual Value accept(FuncType &f)
864 return f(*this);
868 template<class Iter, class Value, class FuncType>
869 struct LessExpression : public RelationalExpression<Iter,Value,FuncType>
871 virtual Value accept(FuncType &f)
873 return f(*this);
877 template<class Iter, class Value, class FuncType>
878 struct LessEqualExpression : public RelationalExpression<Iter,Value,FuncType>
880 virtual Value accept(FuncType &f)
882 return f(*this);
886 template<class Iter, class Value, class FuncType>
887 struct GreaterExpression : public RelationalExpression<Iter,Value,FuncType>
889 virtual Value accept(FuncType &f)
891 return f(*this);
895 template<class Iter, class Value, class FuncType>
896 struct GreaterEqualExpression : public RelationalExpression<Iter,Value,FuncType>
898 virtual Value accept(FuncType &f)
900 return f(*this);
904 template<class Iter, class Value, class FuncType>
905 struct EqualityExpression : public Binary<Iter,Value,FuncType>
907 virtual Value accept(FuncType &f)
909 return f(*this);
913 template<class Iter, class Value, class FuncType>
914 struct EqualExpression : public EqualityExpression<Iter,Value,FuncType>
916 virtual Value accept(FuncType &f)
918 return f(*this);
922 template<class Iter, class Value, class FuncType>
923 struct NotEqualExpression : public EqualityExpression<Iter,Value,FuncType>
925 virtual Value accept(FuncType &f)
927 return f(*this);
931 template<class Iter, class Value, class FuncType>
932 struct LogicalAndExpression : public Binary<Iter,Value,FuncType>
934 virtual Value accept(FuncType &f)
936 return f(*this);
940 template<class Iter, class Value, class FuncType>
941 struct LogicalOrExpression : public Binary<Iter,Value,FuncType>
943 virtual Value accept(FuncType &f)
945 return f(*this);
949 template<class Iter, class Value, class FuncType>
950 struct AssignmentExpression : public Binary<Iter,Value,FuncType>
952 virtual Value accept(FuncType &f)
954 return f(*this);
958 template<class Iter, class Value, class FuncType>
959 struct GlobalAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
961 virtual Value accept(FuncType &f)
963 return f(*this);
967 template<class Iter, class Value, class FuncType>
968 struct LocalAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
970 virtual Value accept(FuncType &f)
972 return f(*this);
976 template<class Iter, class Value, class FuncType>
977 struct AddAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
979 virtual Value accept(FuncType &f)
981 return f(*this);
985 template<class Iter, class Value, class FuncType>
986 struct SubAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
988 virtual Value accept(FuncType &f)
990 return f(*this);
994 template<class Iter, class Value, class FuncType>
995 struct MulAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
997 virtual Value accept(FuncType &f)
999 return f(*this);
1003 template<class Iter, class Value, class FuncType>
1004 struct DivAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
1006 virtual Value accept(FuncType &f)
1008 return f(*this);
1012 template<class Iter, class Value, class FuncType>
1013 struct ModAssignmentExpression : public AssignmentExpression<Iter,Value,FuncType>
1015 virtual Value accept(FuncType &f)
1017 return f(*this);
1021 template<class Iter, class Value, class FuncType>
1022 struct InitializerAssignmentExpression : public Variadic<Iter,Value,FuncType>
1024 virtual Value accept(FuncType &f)
1026 return f(*this);
1030 template<class Iter, class Value, class FuncType>
1031 struct Block : public Unary<Iter,Value,FuncType>
1033 virtual Value accept(FuncType &f)
1035 return f(*this);
1039 template<class Iter, class Value, class FuncType>
1040 struct TableInitializer : public Variadic<Iter,Value,FuncType>
1042 virtual Value accept(FuncType &f)
1044 return f(*this);
1048 template<class Iter, class Value, class FuncType>
1049 struct ArrayInitializer : public Variadic<Iter,Value,FuncType>
1051 virtual Value accept(FuncType &f)
1053 return f(*this);
1057 template<class Iter, class Value, class FuncType>
1058 struct IdentifierList : public Variadic<Iter,Value,FuncType>
1060 virtual Value accept(FuncType &f)
1062 return f(*this);
1066 template<class Iter, class Value, class FuncType>
1067 struct Function : public Binary<Iter,Value,FuncType>
1069 virtual Value accept(FuncType &f)
1071 return f(*this);
1075 template<class Iter, class Value, class FuncType>
1076 struct Tree : public Unary<Iter,Value,FuncType>
1078 virtual Value accept(FuncType &f)
1080 return f(*this);
1084 template<class Iter, class Value, class FuncType>
1085 struct Root : public Variadic<Iter,Value,FuncType>
1087 virtual Value accept(FuncType &f)
1089 return f(*this);
1093 template<class Iter, class Value, class FuncType>
1094 struct Type : public Unary<Iter,Value,FuncType>
1096 virtual Value accept(FuncType &f)
1098 return f(*this);
1102 template<class Iter, class Value, class FuncType>
1103 struct Size : public Unary<Iter,Value,FuncType>
1105 virtual Value accept(FuncType &f)
1107 return f(*this);
1111 template<class Iter, class Value, class FuncType>
1112 struct DoImport : public Unary<Iter,Value,FuncType>
1114 virtual Value accept(FuncType &f)
1116 return f(*this);
1121 template<class Iter, class Value, class FuncType>
1122 struct StatementList : public Variadic<Iter,Value,FuncType>
1124 virtual Value accept(FuncType &f)
1126 return f(*this);
1130 template<class Iter, class Value, class FuncType>
1131 struct IfStatement : public Variadic<Iter,Value,FuncType>
1133 virtual Value accept(FuncType &f)
1135 return f(*this);
1139 template<class Iter, class Value, class FuncType>
1140 struct WhileStatement : public Binary<Iter,Value,FuncType>
1142 virtual Value accept(FuncType &f)
1144 return f(*this);
1148 template<class Iter, class Value, class FuncType>
1149 struct ForStatement : public Variadic<Iter,Value,FuncType>
1151 virtual Value accept(FuncType &f)
1153 return f(*this);
1157 template<class Iter, class Value, class FuncType>
1158 struct ForEachStatement : public Variadic<Iter,Value,FuncType>
1160 virtual Value accept(FuncType &f)
1162 return f(*this);
1166 template<class Iter, class Value, class FuncType>
1167 struct ReturnStatement : public Variadic<Iter,Value,FuncType>
1169 virtual Value accept(FuncType &f)
1171 return f(*this);
1175 template<class Iter, class Value, class FuncType>
1176 struct ContinueStatement : public Nullary<Iter,Value,FuncType>
1178 virtual Value accept(FuncType &f)
1180 return f(*this);
1184 template<class Iter, class Value, class FuncType>
1185 struct BreakStatement : public Nullary<Iter,Value,FuncType>
1187 virtual Value accept(FuncType &f)
1189 return f(*this);
1193 template<class Iter, class Value, class FuncType>
1194 struct ExpressionStatement : public Unary<Iter,Value,FuncType>
1196 virtual Value accept(FuncType &f)
1198 return f(*this);
1202 template<class Iter, class Value, class FuncType>
1203 struct BuiltinFunction : public Nullary<Iter,Value,FuncType>
1205 virtual Value accept(FuncType &f)
1207 return f(*this);
1209 std::function<void(Interpreter&, std::vector<Value>&)> function;
1212 template<class Iter, class Value, class FuncType>
1213 struct Parens : public Unary<Iter,Value,FuncType>
1215 virtual Value accept(FuncType &f)
1217 return f(*this);
1221 template<class Value, class FuncType>
1222 class Parser
1224 public:
1225 using Iter = std::vector<Token<std::string::iterator>>::iterator;
1226 using return_t = std::shared_ptr<ASTNode<Iter,Value,FuncType>>;
1228 template<typename StrIter>
1229 return_t operator()(StrIter sbegin, StrIter send)
1231 auto source = std::make_shared<std::string>(sbegin, send);
1232 auto tokens = tokenize(source->begin(), source->end());
1233 this->begin = tokens->begin();
1234 this->end = tokens->end();
1235 i = this->begin;
1236 accepted = this->end;
1237 auto node = statement_list();
1238 node->source = source;
1239 node->tokens = tokens;
1240 node->root = node;
1241 node->inject_dependencies();
1242 return node;
1245 template<class StrIter>
1246 return_t parse_expression(StrIter sbegin, StrIter send)
1248 auto source = std::make_shared<std::string>(sbegin, send);
1249 auto tokens = tokenize(source->begin(), source->end());
1250 this->begin = tokens->begin();
1251 this->end = tokens->end();
1252 i = this->begin;
1253 accepted = this->end;
1254 auto node = expression();
1255 node->source = source;
1256 node->tokens = tokens;
1257 node->root = node;
1258 node->inject_dependencies();
1259 return node;
1262 private:
1263 Iter i;
1264 Iter accepted;
1266 public:
1267 Iter begin;
1268 Iter end;
1270 public:
1271 void advance()
1273 if(i==end)
1275 Iter last = end-1;
1276 auto lc = Utils::LineColumn(begin->begin, last->end);
1277 throw std::runtime_error(
1278 std::to_string(lc.first) + ":" +
1279 std::to_string(lc.second) + ": unexpected end of input at");
1281 else
1283 ++i;
1287 void rewind(Iter pos)
1289 i = pos;
1292 bool peek(TokenType t, int amount = 0)
1294 return (((i+amount) != end) && (((i+amount)->type) == t));
1297 bool accept(TokenType t)
1299 if(i != end && i->type == t)
1301 accepted = i;
1302 advance();
1303 return true;
1305 else
1307 return false;
1311 bool end_of_input()
1313 return (i == end);
1316 bool expect(TokenType t)
1318 if(accept(t))
1320 return true;
1322 else
1324 i = std::min(i, end-1);
1325 auto lc = Utils::LineColumn(begin->begin, i->begin);
1326 throw std::runtime_error(
1327 std::to_string(lc.first) + ":" +
1328 std::to_string(lc.second) + ": unexpected token '" +
1329 std::string(i->begin, i->end) + "'"
1331 return false;
1335 void expect(const std::string &expected)
1337 i = std::min(i, end-1);
1338 auto lc = Utils::LineColumn(begin->begin, i->begin);
1339 throw std::runtime_error(
1340 std::to_string(lc.first) + ":" +
1341 std::to_string(lc.second) + ": expected " +
1342 expected+ " but got '" + std::string(i->begin, i->end) + "'"
1346 return_t variable()
1348 expect(Tok_Identifier);
1349 auto node = std::make_shared<Variable<Iter,Value,FuncType>>();
1350 node->begin = accepted;
1351 node->end = accepted+1;
1352 node->name = Value(accepted->begin, accepted->end);
1353 return std::move(node);
1356 return_t field_name()
1358 expect(Tok_Identifier);
1359 auto node = std::make_shared<FieldName<Iter,Value,FuncType>>();
1360 node->begin = accepted;
1361 node->end = accepted+1;
1362 node->name = Value(accepted->begin, accepted->end);
1363 return std::move(node);
1366 return_t identifier_list()
1368 auto node = std::make_shared<IdentifierList<Iter,Value,FuncType>>();
1369 node->begin = i;
1370 expect(Tok_LeftParen);
1371 while(!accept(Tok_RightParen))
1373 node->children.push_back(field_name());
1374 if(!peek(Tok_RightParen))
1376 accept(Tok_Comma);
1379 node->end = accepted+1;
1380 return std::move(node);
1383 return_t function()
1385 auto node = std::make_shared<Function<Iter,Value,FuncType>>();
1386 node->begin = i;
1387 expect(Tok_Function);
1388 node->children[0] = identifier_list();
1389 node->children[1] = statement();
1390 node->end = accepted+1;
1391 return std::move(node);
1394 return_t parse_expression()
1396 auto node = std::make_shared<ParseExpression<Iter,Value,FuncType>>();
1397 node->begin = i;
1398 expect(Tok_ParseExpression);
1399 expect(Tok_LeftParen);
1400 node->children[0] = expression();
1401 expect(Tok_RightParen);
1402 node->end = accepted+1;
1403 return std::move(node);
1406 return_t parse_statement()
1408 auto node = std::make_shared<ParseStatement<Iter,Value,FuncType>>();
1409 node->begin = i;
1410 expect(Tok_ParseStatement);
1411 expect(Tok_LeftParen);
1412 node->children[0] = expression();
1413 expect(Tok_RightParen);
1414 node->end = accepted+1;
1415 return std::move(node);
1418 return_t tree()
1420 auto node = std::make_shared<Tree<Iter,Value,FuncType>>();
1421 node->begin = i;
1422 if(accept(Tok_Expression))
1424 expect(Tok_LeftParen);
1425 node->children[0] = expression();
1426 expect(Tok_RightParen);
1428 else
1430 expect(Tok_Statement);
1431 expect(Tok_LeftParen);
1432 node->children[0] = statement();
1433 expect(Tok_RightParen);
1435 node->end = accepted+1;
1436 return std::move(node);
1439 return_t root()
1441 auto node = std::make_shared<Root<Iter,Value,FuncType>>();
1442 node->begin = i;
1443 expect(Tok_Root);
1444 if(accept(Tok_LeftParen))
1446 node->children.push_back(expression());
1447 expect(Tok_RightParen);
1449 node->end = accepted+1;
1450 return std::move(node);
1453 return_t eval()
1455 auto node = std::make_shared<Eval<Iter,Value,FuncType>>();
1456 node->begin = i;
1457 expect(Tok_Eval);
1458 expect(Tok_LeftParen);
1459 node->children[0] = unary_expression();
1460 expect(Tok_RightParen);
1461 node->end = accepted+1;
1462 return std::move(node);
1465 return_t type()
1467 auto node = std::make_shared<Type<Iter,Value,FuncType>>();
1468 node->begin = i;
1469 expect(Tok_TypeOf);
1470 if(accept(Tok_LeftParen))
1472 node->children[0] = expression();
1473 expect(Tok_RightParen);
1475 node->end = accepted+1;
1476 return std::move(node);
1479 return_t size()
1481 auto node = std::make_shared<Size<Iter,Value,FuncType>>();
1482 node->begin = i;
1483 expect(Tok_SizeOf);
1484 if(accept(Tok_LeftParen))
1486 node->children[0] = expression();
1487 expect(Tok_RightParen);
1489 node->end = accepted+1;
1490 return std::move(node);
1493 return_t do_import()
1495 auto node = std::make_shared<DoImport<Iter,Value,FuncType>>();
1496 node->begin = i;
1497 expect(Tok_Import);
1498 if(accept(Tok_LeftParen))
1500 node->children[0] = expression();
1501 expect(Tok_RightParen);
1503 node->end = accepted+1;
1504 return std::move(node);
1507 return_t table_initializer()
1509 auto node = std::make_shared<TableInitializer<Iter,Value,FuncType>>();
1510 node->begin = i;
1511 expect(Tok_LeftBrace);
1512 while(!accept(Tok_RightBrace))
1514 node->children.push_back(initializer_assignment_expression());
1515 if(!peek(Tok_RightBrace))
1517 accept(Tok_Comma);
1520 node->end = accepted+1;
1521 return std::move(node);
1524 return_t array_initializer()
1526 auto node = std::make_shared<ArrayInitializer<Iter,Value,FuncType>>();
1527 node->begin = i;
1528 expect(Tok_LeftBracket);
1529 while(!accept(Tok_RightBracket))
1531 node->children.push_back(logical_or_expression());
1532 if(!peek(Tok_RightBracket))
1534 accept(Tok_Comma);
1537 node->end = accepted+1;
1538 return std::move(node);
1541 return_t primary_expression()
1543 if (peek(Tok_Identifier) || peek(Tok_Global) || peek(Tok_Local))
1545 return variable();
1547 else if (accept(Tok_Number))
1549 auto node = std::make_shared<Constant<Iter,Value,FuncType>>();
1550 node->begin = accepted;
1551 node->end = accepted+1;
1552 node->value = Value(std::stod(std::string(accepted->begin, accepted->end)));
1553 return std::move(node);
1555 else if (accept(Tok_Nil))
1557 auto node = std::make_shared<Nil<Iter,Value,FuncType>>();
1558 node->begin = accepted;
1559 node->end = accepted+1;
1560 return std::move(node);
1562 else if (accept(Tok_String))
1564 auto node = std::make_shared<String<Iter,Value,FuncType>>();
1565 node->begin = accepted;
1566 node->end = accepted+1;
1567 node->value = Value(Utils::Unescape(std::string(accepted->begin+1, accepted->end-1)));
1568 return std::move(node);
1570 else if (accept(Tok_LeftParen))
1572 auto node = std::make_shared<Parens<Iter,Value,FuncType>>();
1573 node->begin = accepted;
1574 node->children[0] = expression();
1575 expect(Tok_RightParen);
1576 node->end = accepted+1;
1577 return std::move(node);
1579 else if (peek(Tok_LeftBrace))
1581 return table_initializer();
1583 else if (peek(Tok_LeftBracket))
1585 return array_initializer();
1587 else if (peek(Tok_Expression) || peek(Tok_Statement))
1589 return tree();
1591 else if (peek(Tok_Root))
1593 return root();
1595 else if (peek(Tok_TypeOf))
1597 return type();
1599 else if (peek(Tok_Eval))
1601 return eval();
1603 else if (peek(Tok_SizeOf))
1605 return size();
1607 else if(peek(Tok_Import))
1609 return do_import();
1611 else if (peek(Tok_ParseExpression))
1613 return parse_expression();
1615 else if (peek(Tok_ParseStatement))
1617 return parse_statement();
1619 else if (peek(Tok_Function))
1621 return function();
1623 else
1625 expect("primary expression");
1626 return nullptr;
1630 return_t postfix_expression()
1632 Iter from = i;
1633 return_t left = primary_expression();
1634 while(true)
1636 if(accept(Tok_LeftBracket))
1638 return_t right = expression();
1639 expect(Tok_RightBracket);
1640 if(peek(Tok_Equal) || peek(Tok_PlusEqual) || peek(Tok_DashEqual) ||
1641 peek(Tok_StarEqual) || peek(Tok_SlashEqual) || peek(Tok_PercentEqual))
1643 std::shared_ptr<FieldAssignment<Iter,Value,FuncType>> node;
1644 if(accept(Tok_Equal))
1646 node = std::make_shared<FieldAssignment<Iter,Value,FuncType>>();
1648 else if(accept(Tok_PlusEqual))
1650 node = std::make_shared<AddFieldAssignment<Iter,Value,FuncType>>();
1652 else if(accept(Tok_DashEqual))
1654 node = std::make_shared<SubFieldAssignment<Iter,Value,FuncType>>();
1656 else if(accept(Tok_StarEqual))
1658 node = std::make_shared<MulFieldAssignment<Iter,Value,FuncType>>();
1660 else if(accept(Tok_SlashEqual))
1662 node = std::make_shared<DivFieldAssignment<Iter,Value,FuncType>>();
1664 else if(accept(Tok_PercentEqual))
1666 node = std::make_shared<ModFieldAssignment<Iter,Value,FuncType>>();
1668 node->begin = from;
1669 node->children[0] = std::move(left);
1670 node->children[1] = std::move(right);
1671 node->children[2] = expression();
1672 node->end = accepted+1;
1673 left = std::move(node);
1675 else
1677 auto node = std::make_shared<FieldAccess<Iter,Value,FuncType>>();
1678 node->begin = from;
1679 node->children[0] = std::move(left);
1680 node->children[1] = std::move(right);
1681 node->end = accepted+1;
1682 left = std::move(node);
1685 else if(accept(Tok_Dot))
1687 return_t right = field_name();
1688 if(peek(Tok_Equal) || peek(Tok_PlusEqual) || peek(Tok_DashEqual) ||
1689 peek(Tok_StarEqual) || peek(Tok_SlashEqual) || peek(Tok_PercentEqual))
1691 std::shared_ptr<FieldAssignment<Iter,Value,FuncType>> node;
1692 if(accept(Tok_Equal))
1694 node = std::make_shared<FieldAssignment<Iter,Value,FuncType>>();
1696 else if(accept(Tok_PlusEqual))
1698 node = std::make_shared<AddFieldAssignment<Iter,Value,FuncType>>();
1700 else if(accept(Tok_DashEqual))
1702 node = std::make_shared<SubFieldAssignment<Iter,Value,FuncType>>();
1704 else if(accept(Tok_StarEqual))
1706 node = std::make_shared<MulFieldAssignment<Iter,Value,FuncType>>();
1708 else if(accept(Tok_SlashEqual))
1710 node = std::make_shared<DivFieldAssignment<Iter,Value,FuncType>>();
1712 else if(accept(Tok_PercentEqual))
1714 node = std::make_shared<ModFieldAssignment<Iter,Value,FuncType>>();
1716 node->begin = from;
1717 node->children[0] = std::move(left);
1718 node->children[1] = std::move(right);
1719 node->children[2] = expression();
1720 node->end = accepted+1;
1721 left = std::move(node);
1723 else if(accept(Tok_LeftParen))
1725 auto node = std::make_shared<MemberCall<Iter,Value,FuncType>>();
1726 node->begin = from;
1727 node->children.push_back(std::move(left));
1728 node->children.push_back(std::move(right));
1729 while(!accept(Tok_RightParen))
1731 node->children.push_back(expression());
1732 if(!peek(Tok_RightParen))
1734 accept(Tok_Comma);
1737 node->end = accepted+1;
1738 left = std::move(node);
1740 else
1742 auto node = std::make_shared<FieldAccess<Iter,Value,FuncType>>();
1743 node->begin = from;
1744 node->children[0] = std::move(left);
1745 node->children[1] = std::move(right);
1746 node->end = accepted+1;
1747 left = std::move(node);
1750 else if(accept(Tok_LeftParen))
1752 auto node = std::make_shared<Call<Iter,Value,FuncType>>();
1753 node->begin = from;
1754 node->children.push_back(std::move(left));
1755 while(!accept(Tok_RightParen))
1757 node->children.push_back(expression());
1758 if(!peek(Tok_RightParen))
1760 accept(Tok_Comma);
1763 node->end = accepted+1;
1764 left = std::move(node);
1766 else
1768 return std::move(left);
1773 return_t unary_expression()
1775 if(accept(Tok_Plus) || accept(Tok_Dash) || accept(Tok_Not))
1777 using node_t = std::shared_ptr<UnaryExpression<Iter,Value,FuncType>>;
1778 node_t node;
1779 switch(accepted->type)
1781 case Tok_Plus:
1782 node = node_t(new UnaryPlusExpression<Iter,Value,FuncType>);
1783 break;
1784 case Tok_Dash:
1785 node = node_t(new UnaryMinusExpression<Iter,Value,FuncType>);
1786 break;
1787 case Tok_Not:
1788 node = node_t(new UnaryNotExpression<Iter,Value,FuncType>);
1789 break;
1790 default:
1791 break;
1793 node->begin = accepted;
1794 node->children[0] = postfix_expression();
1795 node->end = accepted+1;
1796 return std::move(node);
1798 else
1800 return postfix_expression();
1804 return_t multiplicative_expression()
1806 Iter from = i;
1807 return_t left = unary_expression();
1808 if(accept(Tok_Star) || accept(Tok_Slash) || accept(Tok_Percent))
1810 using node_t = std::shared_ptr<MultiplicativeExpression<Iter,Value,FuncType>>;
1811 node_t node;
1812 switch(accepted->type)
1814 case Tok_Star:
1815 node = node_t(new MultiplyExpression<Iter,Value,FuncType>);
1816 break;
1817 case Tok_Slash:
1818 node = node_t(new DivideExpression<Iter,Value,FuncType>);
1819 break;
1820 case Tok_Percent:
1821 node = node_t(new ModuloExpression<Iter,Value,FuncType>);
1822 break;
1823 default:
1824 break;
1826 node->begin = from;
1827 node->children[0] = std::move(left);
1828 node->children[1] = multiplicative_expression();
1829 node->end = accepted+1;
1830 return std::move(node);
1832 else
1834 return std::move(left);
1838 return_t additive_expression()
1840 Iter from = i;
1841 return_t left = multiplicative_expression();
1842 if(accept(Tok_Plus) || accept(Tok_Dash))
1844 using node_t = std::shared_ptr<AdditiveExpression<Iter,Value,FuncType>>;
1845 node_t node;
1846 switch(accepted->type)
1848 case Tok_Plus:
1849 node = node_t(new AddExpression<Iter,Value,FuncType>);
1850 break;
1851 case Tok_Dash:
1852 node = node_t(new SubtractExpression<Iter,Value,FuncType>);
1853 break;
1854 default:
1855 break;
1857 node->begin = from;
1858 node->children[0] = std::move(left);
1859 node->children[1] = additive_expression();
1860 node->end = accepted+1;
1861 return std::move(node);
1863 else
1865 return std::move(left);
1869 return_t relational_expression()
1871 Iter from = i;
1872 return_t left = additive_expression();
1873 if(accept(Tok_Less) || accept(Tok_LessEqual) || accept(Tok_Greater) || accept(Tok_GreaterEqual))
1875 using node_t = std::shared_ptr<RelationalExpression<Iter,Value,FuncType>>;
1876 node_t node;
1877 switch(accepted->type)
1879 case Tok_Less:
1880 node = node_t(new LessExpression<Iter,Value,FuncType>);
1881 break;
1882 case Tok_LessEqual:
1883 node = node_t(new LessEqualExpression<Iter,Value,FuncType>);
1884 break;
1885 case Tok_Greater:
1886 node = node_t(new GreaterExpression<Iter,Value,FuncType>);
1887 break;
1888 case Tok_GreaterEqual:
1889 node = node_t(new GreaterEqualExpression<Iter,Value,FuncType>);
1890 break;
1891 default:
1892 break;
1894 node->begin = from;
1895 node->children[0] = std::move(left);
1896 node->children[1] = relational_expression();
1897 node->end = accepted+1;
1898 return std::move(node);
1900 else
1902 return std::move(left);
1906 return_t equality_expression()
1908 Iter from = i;
1909 return_t left = relational_expression();
1910 if(accept(Tok_EqOp) || accept(Tok_NotEqual))
1912 using node_t = std::shared_ptr<EqualityExpression<Iter,Value,FuncType>>;
1913 node_t node;
1914 switch(accepted->type)
1916 case Tok_EqOp:
1917 node = node_t(new EqualExpression<Iter,Value,FuncType>);
1918 break;
1919 case Tok_NotEqual:
1920 node = node_t(new NotEqualExpression<Iter,Value,FuncType>);
1921 break;
1922 default:
1923 break;
1925 node->begin = from;
1926 node->children[0] = std::move(left);
1927 node->children[1] = relational_expression();
1928 node->end = accepted+1;
1929 return std::move(node);
1931 else
1933 return std::move(left);
1937 return_t logical_and_expression()
1939 Iter from = i;
1940 return_t left = equality_expression();
1941 if(accept(Tok_LogicalAnd))
1943 auto node = std::make_shared<LogicalAndExpression<Iter,Value,FuncType>>();
1944 node->begin = from;
1945 node->children[0] = std::move(left);
1946 node->children[1] = equality_expression();
1947 node->end = accepted+1;
1948 return std::move(node);
1950 else
1952 return std::move(left);
1956 return_t logical_or_expression()
1958 Iter from = i;
1959 return_t left = logical_and_expression();
1960 if(accept(Tok_LogicalOr))
1962 auto node = std::make_shared<LogicalOrExpression<Iter,Value,FuncType>>();
1963 node->begin = from;
1964 node->children[0] = std::move(left);
1965 node->children[1] = logical_and_expression();
1966 node->end = accepted+1;
1967 return std::move(node);
1969 else
1971 return std::move(left);
1975 return_t assignment_expression()
1977 Iter from = i;
1978 if(peek(Tok_Global) || peek(Tok_Local))
1980 bool global = accept(Tok_Global);
1981 bool local = accept(Tok_Local);
1982 return_t left = field_name();
1983 if(accept(Tok_Equal))
1985 std::shared_ptr<AssignmentExpression<Iter,Value,FuncType>> node;
1986 node =
1987 global ?
1988 std::make_shared<GlobalAssignmentExpression<Iter,Value,FuncType>>()
1990 local ?
1991 std::make_shared<LocalAssignmentExpression<Iter,Value,FuncType>>()
1993 std::make_shared<AssignmentExpression<Iter,Value,FuncType>>();
1994 node->begin = from;
1995 node->children[0] = std::move(left);
1996 node->children[1] = assignment_expression();
1997 node->end = accepted+1;
1998 return std::move(node);
2001 else if(peek(Tok_Identifier))
2003 return_t left = field_name();
2004 std::shared_ptr<AssignmentExpression<Iter,Value,FuncType>> node;
2005 if(accept(Tok_Equal))
2007 node = std::make_shared<AssignmentExpression<Iter,Value,FuncType>>();
2009 else if(accept(Tok_PlusEqual))
2011 node = std::make_shared<AddAssignmentExpression<Iter,Value,FuncType>>();
2013 else if(accept(Tok_DashEqual))
2015 node = std::make_shared<SubAssignmentExpression<Iter,Value,FuncType>>();
2017 else if(accept(Tok_StarEqual))
2019 node = std::make_shared<MulAssignmentExpression<Iter,Value,FuncType>>();
2021 else if(accept(Tok_SlashEqual))
2023 node = std::make_shared<DivAssignmentExpression<Iter,Value,FuncType>>();
2025 else if(accept(Tok_PercentEqual))
2027 node = std::make_shared<ModAssignmentExpression<Iter,Value,FuncType>>();
2029 else
2031 rewind(from);
2032 return logical_or_expression();
2034 node->begin = from;
2035 node->children[0] = std::move(left);
2036 node->children[1] = assignment_expression();
2037 node->end = accepted+1;
2038 return std::move(node);
2040 rewind(from);
2041 return logical_or_expression();
2044 return_t initializer_assignment_expression()
2046 auto node = std::make_shared<InitializerAssignmentExpression<Iter,Value,FuncType>>();
2047 node->begin = i;
2048 node->children.push_back(logical_or_expression());
2049 if(accept(Tok_Equal))
2051 node->children.push_back(logical_or_expression());
2053 node->end = accepted+1;
2054 return std::move(node);
2057 return_t expression()
2059 return assignment_expression();
2062 return_t block()
2064 auto node = std::make_shared<Block<Iter,Value,FuncType>>();
2065 node->begin = i;
2066 expect(Tok_LeftBrace);
2067 node->children[0] = statement_list();
2068 expect(Tok_RightBrace);
2069 node->end = accepted+1;
2070 return std::move(node);
2073 return_t if_statement()
2075 auto node = std::make_shared<IfStatement<Iter,Value,FuncType>>();
2076 node->begin = i;
2077 expect(Tok_If);
2078 expect(Tok_LeftParen);
2079 node->children.push_back(expression());
2080 expect(Tok_RightParen);
2081 node->children.push_back(statement());
2082 if(accept(Tok_Else))
2084 node->children.push_back(statement());
2086 node->end = accepted+1;
2087 return std::move(node);
2090 return_t while_statement()
2092 auto node = std::make_shared<WhileStatement<Iter,Value,FuncType>>();
2093 node->begin = i;
2094 expect(Tok_While);
2095 expect(Tok_LeftParen);
2096 node->children[0] = expression();
2097 expect(Tok_RightParen);
2098 node->children[1] = statement();
2099 node->end = accepted+1;
2100 return std::move(node);
2103 return_t nop()
2105 auto node = std::make_shared<Nop<Iter,Value,FuncType>>();
2106 node->begin = i;
2107 node->end = i;
2108 return std::move(node);
2111 return_t for_statement()
2113 Iter from = i;
2114 expect(Tok_For);
2115 expect(Tok_LeftParen);
2116 if(peek(Tok_Identifier) && (peek(Tok_Comma, 1) || peek(Tok_Colon, 1)))
2118 auto node = std::make_shared<ForEachStatement<Iter,Value,FuncType>>();
2119 node->begin = from;
2120 node->children.push_back(field_name());
2121 if(accept(Tok_Comma))
2123 node->children.push_back(field_name());
2125 expect(Tok_Colon);
2126 node->children.push_back(expression());
2127 expect(Tok_RightParen);
2128 node->children.push_back(statement());
2129 node->end = accepted+1;
2130 return std::move(node);
2132 else
2134 auto node = std::make_shared<ForStatement<Iter,Value,FuncType>>();
2135 node->begin = from;
2136 if(accept(Tok_Semicolon))
2138 node->children.push_back(nop());
2140 else
2142 node->children.push_back(expression());
2143 expect(Tok_Semicolon);
2145 if(accept(Tok_Semicolon))
2147 node->children.push_back(nop());
2149 else
2151 node->children.push_back(expression());
2152 expect(Tok_Semicolon);
2154 if(accept(Tok_RightParen))
2156 node->children.push_back(nop());
2158 else
2160 node->children.push_back(expression());
2161 expect(Tok_RightParen);
2163 node->children.push_back(statement());
2164 node->end = accepted+1;
2165 return std::move(node);
2169 return_t break_statement()
2171 auto node = std::make_shared<BreakStatement<Iter,Value,FuncType>>();
2172 node->begin = i;
2173 expect(Tok_Break);
2174 accept(Tok_Semicolon);
2175 node->end = accepted+1;
2176 return std::move(node);
2179 return_t continue_statement()
2181 auto node = std::make_shared<ContinueStatement<Iter,Value,FuncType>>();
2182 node->begin = i;
2183 expect(Tok_Continue);
2184 accept(Tok_Semicolon);
2185 node->end = accepted+1;
2186 return std::move(node);
2189 return_t return_statement()
2191 auto node = std::make_shared<ReturnStatement<Iter,Value,FuncType>>();
2192 node->begin = i;
2193 expect(Tok_Return);
2194 if(accept(Tok_Semicolon))
2196 node->end = accepted+1;
2197 return std::move(node);
2199 node->children.push_back(expression());
2200 accept(Tok_Semicolon);
2201 node->end = accepted+1;
2202 return std::move(node);
2205 return_t statement()
2207 if(peek(Tok_LeftBrace))
2209 return block();
2211 else if (peek(Tok_If))
2213 return if_statement();
2215 else if (peek(Tok_While))
2217 return while_statement();
2219 else if (peek(Tok_For))
2221 return for_statement();
2223 else if (peek(Tok_Return))
2225 return return_statement();
2227 else if (peek(Tok_Continue))
2229 return continue_statement();
2231 else if (peek(Tok_Break))
2233 return break_statement();
2235 else
2237 auto node = std::make_shared<ExpressionStatement<Iter,Value,FuncType>>();
2238 node->begin = i;
2239 node->children[0] = expression();
2240 accept(Tok_Semicolon);
2241 node->end = accepted+1;
2242 return std::move(node);
2245 return_t statement_list()
2247 auto node = std::make_shared<StatementList<Iter,Value,FuncType>>();
2248 node->begin = i;
2249 while(!end_of_input() && !peek(Tok_RightBrace))
2251 node->children.push_back(statement());
2253 node->end = accepted+1;
2254 return std::move(node);
2259 template<typename T> class Table;
2261 template<typename T> class Array;
2263 template<typename Iter, typename FuncType>
2264 struct BaseValue
2266 using NumberType = double;
2267 using StringContainer = std::string;
2268 using StringIterator = StringContainer::iterator;
2269 using NumberContainer = NumberType;
2270 using PointerContainer = std::shared_ptr<Internal::ASTNode<Iter, BaseValue, FuncType>>;
2271 using FunctionContainer = std::shared_ptr<Internal::Function<Iter,BaseValue,FuncType>>;
2272 using TreeContainer = PointerContainer;
2273 using ArrayContainer = std::shared_ptr<Array<BaseValue>>;
2274 using TableContainer = std::shared_ptr<Table<BaseValue>>;
2276 struct ProtoStringContainer
2278 StringIterator begin;
2279 StringIterator end;
2280 size_t hash;
2282 ProtoStringContainer(StringIterator begin_, StringIterator end_):
2283 begin(begin_),
2284 end(end_),
2285 hash(std::hash<StringContainer>()(StringContainer(begin_, end_)))
2290 struct UserPtrContainer
2292 void* realptr;
2294 UserPtrContainer(void* ptr): realptr(ptr)
2299 class TagReturn
2303 class TagBreak
2307 class TagContinue
2311 enum class Type
2313 Nil, Number, String, ProtoString,
2314 Tree, Function, Table, Array,
2315 Return, Break, Continue, UserPtr,
2318 private:
2319 Type m_type;
2320 union
2322 NumberContainer m_number;
2323 PointerContainer m_pointer;
2324 UserPtrContainer m_userptr;
2325 TableContainer m_table;
2326 ArrayContainer m_array;
2327 StringContainer m_strval;
2328 ProtoStringContainer m_protostr;
2331 public:
2332 Type type() const
2334 if(m_type == Type::ProtoString)
2336 return Type::String;
2338 else if((m_type >= Type::Return) && (m_type <= Type::Continue))
2340 return Type::Nil;
2342 return m_type;
2345 const char* typeName() const
2347 switch(type())
2349 case Type::Nil:
2350 return "nil";
2351 case Type::Number:
2352 return "number";
2353 case Type::String:
2354 return "string";
2355 case Type::ProtoString:
2356 return "protostring";
2357 case Type::Tree:
2358 return "tree";
2359 case Type::Function:
2360 return "function";
2361 case Type::Table:
2362 return "table";
2363 case Type::Array:
2364 return "array";
2365 case Type::Return:
2366 return "return";
2367 case Type::Break:
2368 return "break";
2369 case Type::Continue:
2370 return "continue";
2371 case Type::UserPtr:
2372 return "userptr";
2373 default:
2376 return "invalid";
2379 Type internal_type() const
2381 return m_type;
2384 const NumberType& number() const
2386 return m_number;
2389 NumberType& number()
2391 return m_number;
2394 const PointerContainer& pointer() const
2396 return m_pointer;
2399 PointerContainer& pointer()
2401 return m_pointer;
2404 void*& userptr()
2406 return m_userptr.realptr;
2409 const void* userptr() const
2411 return m_userptr.realptr;
2414 const FunctionContainer function() const
2416 return std::static_pointer_cast<Internal::Function<Iter,BaseValue,FuncType>>(m_pointer);
2419 FunctionContainer function()
2421 return std::static_pointer_cast<Internal::Function<Iter,BaseValue,FuncType>>(m_pointer);
2424 const TreeContainer tree() const
2426 return m_pointer;
2429 TreeContainer tree()
2431 return m_pointer;
2434 const TableContainer& table() const
2436 return m_table;
2439 TableContainer& table()
2441 return m_table;
2444 const StringContainer string() const
2446 if(m_type == Type::ProtoString)
2448 return std::string(m_protostr.begin, m_protostr.end);
2450 return m_strval;
2453 StringContainer& string()
2455 decay();
2456 return m_strval;
2459 const ArrayContainer& array() const
2461 return m_array;
2464 ArrayContainer& array()
2466 return m_array;
2469 void decay()
2471 if(m_type == Type::ProtoString)
2473 StringIterator tmpbegin = m_protostr.begin;
2474 StringIterator tmpend = m_protostr.end;
2475 m_type = Type::String;
2476 new (&m_strval) StringContainer(tmpbegin, tmpend);
2478 else if((m_type >= Type::Return) && (m_type <= Type::Continue))
2480 m_type = Type::Nil;
2484 explicit BaseValue() : m_type(Type::Nil), m_number(0)
2488 explicit BaseValue(const TagReturn&) : m_type(Type::Return), m_number(0)
2492 explicit BaseValue(const TagBreak&) : m_type(Type::Break), m_number(0)
2496 explicit BaseValue(const TagContinue&) : m_type(Type::Continue), m_number(0)
2500 explicit BaseValue(NumberType value) : m_type(Type::Number), m_number(value)
2504 explicit BaseValue(Type t, const PointerContainer &ptr) : m_type(t), m_pointer(ptr)
2508 explicit BaseValue(UserPtrContainer ptr): m_type(Type::UserPtr), m_userptr(ptr.realptr)
2510 //fprintf(stderr, "BaseValue/userpointer: ptr=%p\n", ptr.realptr);
2513 BaseValue(const TableContainer &ptr) : m_type(Type::Table), m_table(ptr)
2517 BaseValue(const ArrayContainer &ptr) : m_type(Type::Array), m_array(ptr)
2521 BaseValue(const StringContainer &value) : m_type(Type::String), m_strval(value)
2525 BaseValue(StringIterator begin, StringIterator end) : m_type(Type::ProtoString), m_protostr(begin, end)
2529 BaseValue(const BaseValue &that) : m_type(that.m_type)
2531 switch(m_type)
2533 case Type::Return:
2534 case Type::Break:
2535 case Type::Continue:
2536 case Type::Nil:
2537 new (&m_number) NumberContainer(0);
2538 break;
2539 case Type::Number:
2540 new (&m_number) NumberContainer(that.m_number);
2541 break;
2542 case Type::String:
2543 new (&m_strval) StringContainer(that.m_strval);
2544 break;
2545 case Type::ProtoString:
2546 new (&m_protostr) ProtoStringContainer(that.m_protostr);
2547 break;
2548 case Type::Tree:
2549 case Type::Function:
2550 new (&m_pointer) PointerContainer(that.m_pointer);
2551 break;
2552 case Type::Table:
2553 new (&m_table) TableContainer(that.m_table);
2554 break;
2555 case Type::Array:
2556 new (&m_array) ArrayContainer(that.m_array);
2557 break;
2558 case Type::UserPtr:
2559 #if 0
2560 fprintf(stderr, "BaseValue(BaseValue&): this.userptr=%p, that.userptr=%p\n",
2561 m_userptr.realptr, that.m_userptr.realptr
2563 #endif
2564 new (&m_userptr) UserPtrContainer(that.m_userptr);
2565 break;
2566 default:
2567 break;
2571 BaseValue& operator=(const BaseValue &that)
2573 switch(m_type)
2575 case Type::String:
2576 m_strval.~StringContainer();
2577 break;
2578 case Type::Tree:
2579 case Type::Function:
2580 m_pointer.~PointerContainer();
2581 break;
2582 case Type::Table:
2583 m_table.~TableContainer();
2584 break;
2585 case Type::Array:
2586 m_array.~ArrayContainer();
2587 break;
2588 default:
2589 break;
2591 m_type = that.m_type;
2592 switch(m_type)
2594 case Type::Return:
2595 case Type::Break:
2596 case Type::Continue:
2597 case Type::Nil:
2598 new (&m_number) NumberContainer(0);
2599 break;
2600 case Type::Number:
2601 new (&m_number) NumberContainer(that.m_number);
2602 break;
2603 case Type::String:
2604 new (&m_strval) StringContainer(that.m_strval);
2605 break;
2606 case Type::ProtoString:
2607 new (&m_protostr) ProtoStringContainer(that.m_protostr);
2608 break;
2609 case Type::Tree:
2610 case Type::Function:
2611 new (&m_pointer) PointerContainer(that.m_pointer);
2612 break;
2613 case Type::UserPtr:
2614 #if 0
2615 fprintf(stderr, "BaseValue::operator=(BaseValue&): this.userptr=%p, that.userptr=%p\n",
2616 m_userptr.realptr, that.m_userptr.realptr
2618 #endif
2619 new(&m_userptr) UserPtrContainer(that.m_userptr);
2620 break;
2621 case Type::Table:
2622 new (&m_table) TableContainer(that.m_table);
2623 break;
2624 case Type::Array:
2625 new (&m_array) ArrayContainer(that.m_array);
2626 break;
2627 default:
2628 break;
2630 return *this;
2633 ~BaseValue()
2635 switch(m_type)
2637 case Type::String:
2638 m_strval.~StringContainer();
2639 break;
2640 case Type::Tree:
2641 case Type::Function:
2642 m_pointer.~PointerContainer();
2643 break;
2644 case Type::Table:
2645 m_table.~TableContainer();
2646 break;
2647 case Type::Array:
2648 m_array.~ArrayContainer();
2649 break;
2650 default:
2651 break;
2655 bool operator==(const BaseValue &that) const
2657 if(type() != that.type())
2659 return false;
2661 switch(m_type)
2663 case Type::Return:
2664 case Type::Break:
2665 case Type::Continue:
2666 case Type::Nil:
2667 case Type::Number:
2668 return number() == that.number();
2669 case Type::Tree:
2670 case Type::Function:
2671 return pointer() == that.pointer();
2672 case Type::Table:
2673 return table() == that.table();
2674 case Type::Array:
2675 return array() == that.array();
2676 case Type::UserPtr:
2677 return (userptr() == that.userptr());
2678 case Type::String:
2679 if(that.m_type == Type::String)
2681 return m_strval == that.m_strval;
2683 else
2685 if(m_strval.end()-m_strval.begin() != that.m_protostr.end - that.m_protostr.begin)
2687 return false;
2689 return std::equal(m_strval.begin(), m_strval.end(), that.m_protostr.begin);
2691 case Type::ProtoString:
2692 if(that.m_type == Type::String)
2694 if(m_protostr.end - m_protostr.begin != that.m_strval.end() - that.m_strval.begin())
2696 return false;
2698 return std::equal(m_protostr.begin, m_protostr.end, that.m_strval.begin());
2700 else
2702 if(m_protostr.end - m_protostr.begin != that.m_protostr.end - that.m_protostr.begin)
2704 return false;
2706 return std::equal(m_protostr.begin, m_protostr.end, that.m_protostr.begin);
2708 default:
2709 return false;
2713 friend struct std::hash<BaseValue<Iter, FuncType>>;
2717 namespace std
2719 template<typename Iter, typename FuncType>
2720 struct hash<Aqua::BaseValue<Iter, FuncType>>
2722 public:
2723 using T = Aqua::BaseValue<Iter, FuncType>;
2724 std::size_t operator()(Aqua::BaseValue<Iter, FuncType> const& x) const
2726 switch(x.m_type)
2728 case T::Type::Nil:
2729 case T::Type::Number:
2730 return std::hash<typename T::NumberContainer>()(x.number());
2731 case T::Type::String:
2732 return std::hash<typename T::StringContainer>()(x.string());
2733 case T::Type::ProtoString:
2734 return x.m_protostr.hash;
2735 case T::Type::UserPtr:
2736 return std::hash<const void*>()(x.userptr());;
2737 case T::Type::Tree:
2738 case T::Type::Function:
2739 return std::hash<typename T::PointerContainer>()(x.pointer());
2740 case T::Type::Table:
2741 return std::hash<typename T::TableContainer>()(x.table());
2742 case T::Type::Array:
2743 return std::hash<typename T::ArrayContainer>()(x.array());
2744 default:
2745 return 0;
2751 template<class Iter, class FuncType>
2752 std::ostream& operator<<(std::ostream &out, const Aqua::BaseValue<Iter,FuncType> &v)
2754 using T = Aqua::BaseValue<Iter,FuncType>;
2755 switch(v.type())
2757 case T::Type::Nil:
2758 return out << "nil";
2759 case T::Type::Number:
2760 return out << v.number();
2761 case T::Type::String:
2762 return out << v.string();
2763 case T::Type::Tree:
2764 return out << "tree@" << v.tree();
2765 case T::Type::Function:
2766 return out << "function@" << v.function();
2767 case T::Type::Table:
2768 return out << "table@" << v.table();
2769 case T::Type::Array:
2770 return out << "array@" << v.array();
2771 case T::Type::UserPtr:
2772 return out << "userpointer@" << v.userptr();
2773 default:
2774 return out;
2778 namespace Aqua
2780 template<typename Type> class Table
2782 public:
2783 using Iterator = typename std::unordered_map<Type, Type>::iterator;
2785 private:
2786 std::unordered_map<Type, Type> m_data;
2787 Type m_nil;
2789 public:
2790 void set(const Type& key, const Type& value)
2792 if(key.type() == Type::Type::Nil)
2794 return;
2796 if(value.type() == Type::Type::Nil)
2798 m_data.erase(key);
2800 m_data[key] = value;
2803 Type get(const Type& key)
2805 auto iter = m_data.find(key);
2806 if(iter == m_data.end())
2808 return m_nil;
2810 else
2812 return iter->second;
2816 void append(const Table& that)
2818 m_data.insert(that.m_data.begin(), that.m_data.end());
2821 size_t size() const
2823 return m_data.size();
2826 Iterator begin()
2828 return m_data.begin();
2831 Iterator end()
2833 return m_data.end();
2837 template<typename Type>
2838 class Array
2840 public:
2841 using Iterator = typename std::vector<Type>::iterator;
2843 private:
2844 std::vector<Type> m_data;
2845 Type m_nil;
2847 public:
2848 void set(const Type& key, const Type& value)
2850 int index = key.number();
2851 m_data[index] = value;
2854 Type get(const Type& key)
2856 int index = key.number();
2857 return m_data[index];
2860 void append(const Array& that)
2862 m_data.insert(m_data.end(), that.m_data.begin(), that.m_data.end());
2865 void add(const Type& value)
2867 m_data.push_back(value);
2870 size_t size() const
2872 return m_data.size();
2875 Iterator begin()
2877 return m_data.begin();
2880 Iterator end()
2882 return m_data.end();
2886 class Interpreter
2888 public:
2889 using Iter = std::vector<Internal::Token<std::string::iterator>>::iterator;
2890 using FuncType = Interpreter;
2891 using Value = BaseValue<Iter, FuncType>;
2892 using Environment = std::shared_ptr<Table<Value>>;
2893 using EnvStack = std::vector<Environment>;
2894 using FunStack = std::vector<size_t>;
2895 using ParserInstance = Internal::Parser<Value, Interpreter>;
2896 using ModulePath = std::vector<std::string>;
2897 using FuncProto = std::function<void(Interpreter&, std::vector<Value>&)>;
2899 struct FunctionScope
2901 Interpreter* m_self;
2903 FunctionScope(Interpreter* self): m_self(self)
2905 m_self->beginFunctionScope();
2908 ~FunctionScope()
2910 m_self->endFunctionScope();
2914 struct LocalScope
2916 Interpreter& m_self;
2918 LocalScope(Interpreter& self): m_self(self)
2920 m_self.beginLocalScope();
2923 ~LocalScope()
2925 m_self.endLocalScope();
2929 private:
2930 ParserInstance m_parser;
2931 std::vector<Value> m_function_stack;
2932 std::vector<std::unordered_map<std::string, Value>> m_scopes;
2933 EnvStack m_envstack;
2934 FunStack m_funstack;
2935 ModulePath m_modpath;
2937 public:
2938 Value operator()(Internal::Variable<Iter,Value,FuncType> &node);
2940 Value operator()(Internal::FieldName<Iter,Value,FuncType> &node);
2942 Value operator()(Internal::Constant<Iter,Value,FuncType> &node);
2944 Value operator()(Internal::Nop<Iter,Value,FuncType> &);
2946 Value operator()(Internal::Nil<Iter,Value,FuncType> &);
2948 Value operator()(Internal::String<Iter,Value,FuncType> &node);
2950 Value operator()(Internal::Parens<Iter,Value,FuncType> &node);
2952 Value operator()(Internal::ExpressionStatement<Iter,Value,FuncType> &node);
2954 Value operator()(Internal::Type<Iter,Value,FuncType> &node);
2956 Value operator()(Internal::Size<Iter,Value,FuncType> &node);
2958 Value operator()(Internal::DoImport<Iter,Value,FuncType>& node);
2960 Value operator()(Internal::FieldAccess<Iter,Value,FuncType> &node);
2962 Value operator()(Internal::FieldAssignment<Iter,Value,FuncType> &node);
2964 Value operator()(Internal::AddFieldAssignment<Iter,Value,FuncType> &node);
2966 Value operator()(Internal::SubFieldAssignment<Iter,Value,FuncType> &node);
2968 Value operator()(Internal::MulFieldAssignment<Iter,Value,FuncType> &node);
2970 Value operator()(Internal::DivFieldAssignment<Iter,Value,FuncType> &node);
2972 Value operator()(Internal::ModFieldAssignment<Iter,Value,FuncType> &node);
2974 Value operator()(Internal::UnaryPlusExpression<Iter,Value,FuncType> &node);
2976 Value operator()(Internal::UnaryMinusExpression<Iter,Value,FuncType> &node);
2978 Value operator()(Internal::UnaryNotExpression<Iter,Value,FuncType> &node);
2980 Value operator()(Internal::MultiplyExpression<Iter,Value,FuncType> &node);
2982 Value operator()(Internal::DivideExpression<Iter,Value,FuncType> &node);
2984 Value operator()(Internal::ModuloExpression<Iter,Value,FuncType> &node);
2986 Value operator()(Internal::AddExpression<Iter,Value,FuncType> &node);
2988 Value operator()(Internal::SubtractExpression<Iter,Value,FuncType> &node);
2990 Value operator()(Internal::LessExpression<Iter,Value,FuncType> &node);
2992 Value operator()(Internal::LessEqualExpression<Iter,Value,FuncType> &node);
2994 Value operator()(Internal::GreaterExpression<Iter,Value,FuncType> &node);
2996 Value operator()(Internal::GreaterEqualExpression<Iter,Value,FuncType> &node);
2998 Value operator()(Internal::EqualExpression<Iter,Value,FuncType> &node);
3000 Value operator()(Internal::NotEqualExpression<Iter,Value,FuncType> &node);
3002 Value operator()(Internal::LogicalAndExpression<Iter,Value,FuncType> &node);
3004 Value operator()(Internal::LogicalOrExpression<Iter,Value,FuncType> &node);
3006 Value operator()(Internal::AssignmentExpression<Iter,Value,FuncType> &node);
3008 Value operator()(Internal::GlobalAssignmentExpression<Iter,Value,FuncType> &node);
3010 Value operator()(Internal::LocalAssignmentExpression<Iter,Value,FuncType> &node);
3012 Value operator()(Internal::AddAssignmentExpression<Iter,Value,FuncType> &node);
3014 Value operator()(Internal::SubAssignmentExpression<Iter,Value,FuncType> &node);
3016 Value operator()(Internal::MulAssignmentExpression<Iter,Value,FuncType> &node);
3018 Value operator()(Internal::DivAssignmentExpression<Iter,Value,FuncType> &node);
3020 Value operator()(Internal::ModAssignmentExpression<Iter,Value,FuncType> &node);
3022 Value operator()(Internal::Block<Iter,Value,FuncType> &node);
3024 Value operator()(Internal::TableInitializer<Iter,Value,FuncType> &node);
3026 Value operator()(Internal::ArrayInitializer<Iter,Value,FuncType> &node);
3028 Value operator()(Internal::Function<Iter,Value,FuncType> &node);
3030 Value operator()(Internal::ReturnStatement<Iter,Value,FuncType> &node);
3032 Value operator()(Internal::IdentifierList<Iter,Value,FuncType> &node);
3034 Value operator()(Internal::BuiltinFunction<Iter,Value,FuncType> &node);
3036 Value operator()(Internal::Call<Iter,Value,FuncType> &node);
3038 Value operator()(Internal::MemberCall<Iter,Value,FuncType> &node);
3040 //Value operator()(Internal::EvalStatement<Iter,Value,FuncType> &node);
3042 Value operator()(Internal::Eval<Iter,Value,FuncType> &node);
3044 Value operator()(Internal::Tree<Iter,Value,FuncType> &node);
3046 Value operator()(Internal::Root<Iter,Value,FuncType> &node);
3048 Value operator()(Internal::ParseStatement<Iter,Value,FuncType> &node);
3050 Value operator()(Internal::ParseExpression<Iter,Value,FuncType> &node);
3052 Value operator()(Internal::StatementList<Iter,Value,FuncType> &node);
3054 Value operator()(Internal::IfStatement<Iter,Value,FuncType> &node);
3056 Value operator()(Internal::WhileStatement<Iter,Value,FuncType> &node);
3058 Value operator()(Internal::ForStatement<Iter,Value,FuncType> &node);
3060 Value operator()(Internal::ForEachStatement<Iter,Value,FuncType> &node);
3062 Value operator()(Internal::ContinueStatement<Iter,Value,FuncType> &);
3064 Value operator()(Internal::BreakStatement<Iter,Value,FuncType> &);
3066 template<class Type>
3067 Value operator()(Type& node)
3069 error("unimplemented ast node.", node);
3070 return Value();
3073 template<typename Type>
3074 void error(const std::string text, Type& node)
3076 std::stringstream strm;
3077 auto lc = Utils::LineColumn(node.source->begin(), node.begin->begin);
3078 auto from = node.begin;
3079 auto to = node.end-1;
3080 auto line = lc.first;
3081 auto column = lc.second;
3082 auto source = std::string(from->begin, to->end);
3083 strm << "<input>:" << line << ":" << column << " error: " << text << " in: " << source;
3084 throw std::runtime_error(strm.str());
3087 template<typename Type>
3088 Value makeUserPointer(Type* ptr)
3090 return Value(Value::UserPtrContainer((void*)ptr));
3093 Value makeString(const std::string& src);
3095 Value makeNumber(Value::NumberType val);
3097 std::shared_ptr<Array<Value>> makeArray();
3099 std::shared_ptr<Table<Value>> makeTable();
3101 Value makeFunction(FuncProto fun);
3103 bool need(const Value& raw_in, Value& dest, Value::Type expected);
3105 bool needNumber(const Value& raw_in, Value& dest);
3107 bool needString(const Value& raw_in, Value& dest);
3109 bool needFunction(const Value& raw_in, Value& dest);
3111 void beginLocalScope();
3113 void endLocalScope();
3115 void beginFunctionScope();
3117 void endFunctionScope();
3119 Value get(Value key);
3121 void set(Value key, Value value);
3123 void setLocal(Value key, Value value);
3125 void setGlobal(Value key, Value value);
3127 void setGlobalFunc(const std::string &name, FuncProto fun);
3129 ModulePath getModulePath();
3131 void addModulePath(const std::string& path);
3133 Interpreter();
3135 ~Interpreter();
3137 Value eval(const std::string &source);
3139 Value run(const std::string &source);