9 #include <unordered_map>
19 std::string
Unescape(const std::string
&str
);
21 // helper to find line & column position on demand
23 std::pair
<int, int> LineColumn(Iter begin
, Iter pos
)
27 for(Iter i
= begin
; i
!=pos
; ++i
)
36 return std::make_pair(line
, column
);
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
,
67 static const std::unordered_map
<std::string
, TokenType
> keywords
=
73 {"return", Tok_Return
},
75 {"continue", Tok_Continue
},
76 {"func", Tok_Function
},
78 {"expression", Tok_Expression
},
79 {"statement", Tok_Statement
},
81 {"parse_statement", Tok_ParseStatement
},
82 {"parse_expression", Tok_ParseExpression
},
83 {"global", Tok_Global
},
86 {"typeof", Tok_TypeOf
},
87 {"sizeof", Tok_SizeOf
},
89 // these aren't implemented yet
90 {"switch", Tok_Switch
},
92 {"import", Tok_Import
},
102 Token(TokenType type_
, Iter begin_
, Iter end_
):
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
;
123 case '\n': // ignore whitespaces
127 tokens
.emplace_back(Tok_LeftParen
, i
, i
+1);
131 tokens
.emplace_back(Tok_RightParen
, i
, i
+1);
135 tokens
.emplace_back(Tok_LeftBracket
, i
, i
+1);
139 tokens
.emplace_back(Tok_RightBracket
, i
, i
+1);
143 tokens
.emplace_back(Tok_LeftBrace
, i
, i
+1);
147 tokens
.emplace_back(Tok_RightBrace
, i
, i
+1);
151 tokens
.emplace_back(Tok_Comma
, i
, i
+1);
155 tokens
.emplace_back(Tok_Dot
, i
, i
+1);
159 tokens
.emplace_back(Tok_Semicolon
, i
, i
+1);
163 tokens
.emplace_back(Tok_Colon
, i
, i
+1);
167 if(i
+1 != end
&& *(i
+1) == '=')
169 tokens
.emplace_back(Tok_PlusEqual
, i
, i
+2);
174 tokens
.emplace_back(Tok_Plus
, i
, i
+1);
179 if(i
+1 != end
&& *(i
+1) == '=')
181 tokens
.emplace_back(Tok_DashEqual
, i
, i
+2);
186 tokens
.emplace_back(Tok_Dash
, i
, i
+1);
191 if(i
+1 != end
&& *(i
+1) == '=')
193 tokens
.emplace_back(Tok_StarEqual
, i
, i
+2);
198 tokens
.emplace_back(Tok_Star
, i
, i
+1);
203 if(i
+1 != end
&& *(i
+1) == '=')
205 tokens
.emplace_back(Tok_PercentEqual
, i
, i
+2);
210 tokens
.emplace_back(Tok_Percent
, i
, i
+1);
215 if(i
+1 != end
&& *(i
+1) == '/')
218 while(i
!=end
&& *i
!= '\n')
223 else if(i
+1 != end
&& *(i
+1) == '=')
225 tokens
.emplace_back(Tok_SlashEqual
, i
, i
+2);
230 tokens
.emplace_back(Tok_Slash
, i
, i
+1);
235 if(i
+1 != end
&& *(i
+1) == '=')
237 tokens
.emplace_back(Tok_LessEqual
, i
, i
+2);
242 tokens
.emplace_back(Tok_Less
, i
, i
+1);
247 if(i
+1 != end
&& *(i
+1) == '=')
249 tokens
.emplace_back(Tok_GreaterEqual
, i
, i
+2);
254 tokens
.emplace_back(Tok_Greater
, i
, i
+1);
259 if(i
+1 != end
&& *(i
+1) == '=')
261 tokens
.emplace_back(Tok_EqOp
, i
, i
+2);
266 tokens
.emplace_back(Tok_Equal
, i
, i
+1);
271 if(i
+1 != end
&& *(i
+1) == '=')
273 tokens
.emplace_back(Tok_NotEqual
, i
, i
+2);
278 tokens
.emplace_back(Tok_Not
, i
, i
+1);
283 if(i
+1 != end
&& *(i
+1) == '&')
285 tokens
.emplace_back(Tok_LogicalAnd
, i
, i
+2);
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
+ "'"
298 if(i
+1 != end
&& *(i
+1) == '|')
300 tokens
.emplace_back(Tok_LogicalOr
, i
, i
+2);
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
+ "'"
314 while(i
!=end
&& !(*i
== '"' && *(i
-1) != '\\'))
319 tokens
.emplace_back(Tok_String
, start
, i
);
324 while(i
!=end
&& std::isdigit(*i
))
328 if(i
!=end
&& (*i
== '.'))
331 while((i
!= end
) && std::isdigit(*i
))
336 if((i
!= end
) && (*i
== 'e'))
339 if((i
!= end
) && (*i
== '-'))
343 while(i
!=end
&& std::isdigit(*i
))
348 tokens
.emplace_back(Tok_Number
, start
, i
);
351 else if(std::isalpha(*i
) || (*i
== '_') || (*i
== '$'))
354 while(i
!=end
&& (std::isalnum(*i
) || *i
== '_'))
358 auto kw
= keywords
.find(std::string(start
, i
));
359 if(kw
== keywords
.end())
361 tokens
.emplace_back(Tok_Identifier
, start
, i
);
365 tokens
.emplace_back(kw
->second
, start
, i
);
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
+ "'"
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
>>
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
)
411 virtual std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>& operator[](size_t 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
)
443 virtual std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>& operator[](size_t 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
)
475 virtual std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>& operator[](size_t 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
)
507 virtual std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>& operator[](size_t 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
)
539 virtual std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>& operator[](size_t 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
>
566 virtual Value
accept(FuncType
&f
)
572 template<class Iter
, class Value
, class FuncType
>
573 struct GlobalVariable
: public Variable
<Iter
,Value
,FuncType
>
575 virtual Value
accept(FuncType
&f
)
581 template<class Iter
, class Value
, class FuncType
>
582 struct LocalVariable
: public Variable
<Iter
,Value
,FuncType
>
584 virtual Value
accept(FuncType
&f
)
590 template<class Iter
, class Value
, class FuncType
>
591 struct FieldName
: public Nullary
<Iter
,Value
,FuncType
>
594 virtual Value
accept(FuncType
&f
)
600 template<class Iter
, class Value
, class FuncType
>
601 struct Nop
: public Nullary
<Iter
,Value
,FuncType
>
604 virtual Value
accept(FuncType
&f
)
610 template<class Iter
, class Value
, class FuncType
>
611 struct Constant
: public Nullary
<Iter
,Value
,FuncType
>
615 virtual Value
accept(FuncType
&f
)
621 template<class Iter
, class Value
, class FuncType
>
622 struct Nil
: public Nullary
<Iter
,Value
,FuncType
>
624 virtual Value
accept(FuncType
&f
)
630 template<class Iter
, class Value
, class FuncType
>
631 struct String
: public Nullary
<Iter
,Value
,FuncType
>
635 virtual Value
accept(FuncType
&f
)
641 template<class Iter
, class Value
, class FuncType
>
642 struct FieldAccess
: public Binary
<Iter
,Value
,FuncType
>
644 virtual Value
accept(FuncType
&f
)
650 template<class Iter
, class Value
, class FuncType
>
651 struct FieldAssignment
: public Ternary
<Iter
,Value
,FuncType
>
653 virtual Value
accept(FuncType
&f
)
659 template<class Iter
, class Value
, class FuncType
>
660 struct AddFieldAssignment
: public FieldAssignment
<Iter
,Value
,FuncType
>
662 virtual Value
accept(FuncType
&f
)
668 template<class Iter
, class Value
, class FuncType
>
669 struct SubFieldAssignment
: public FieldAssignment
<Iter
,Value
,FuncType
>
671 virtual Value
accept(FuncType
&f
)
677 template<class Iter
, class Value
, class FuncType
>
678 struct MulFieldAssignment
: public FieldAssignment
<Iter
,Value
,FuncType
>
680 virtual Value
accept(FuncType
&f
)
686 template<class Iter
, class Value
, class FuncType
>
687 struct DivFieldAssignment
: public FieldAssignment
<Iter
,Value
,FuncType
>
689 virtual Value
accept(FuncType
&f
)
695 template<class Iter
, class Value
, class FuncType
>
696 struct ModFieldAssignment
: public FieldAssignment
<Iter
,Value
,FuncType
>
698 virtual Value
accept(FuncType
&f
)
704 template<class Iter
, class Value
, class FuncType
>
705 struct Call
: public Variadic
<Iter
,Value
,FuncType
>
707 virtual Value
accept(FuncType
&f
)
713 template<class Iter
, class Value
, class FuncType
>
714 struct MemberCall
: public Variadic
<Iter
,Value
,FuncType
>
716 virtual Value
accept(FuncType
&f
)
723 template<class Iter, class Value, class FuncType>
724 struct EvalStatement : public Unary<Iter,Value,FuncType>
726 virtual Value accept(FuncType &f)
733 template<class Iter
, class Value
, class FuncType
>
734 struct Eval
: public Unary
<Iter
,Value
,FuncType
>
736 virtual Value
accept(FuncType
&f
)
742 template<class Iter
, class Value
, class FuncType
>
743 struct ParseStatement
: public Unary
<Iter
,Value
,FuncType
>
745 virtual Value
accept(FuncType
&f
)
751 template<class Iter
, class Value
, class FuncType
>
752 struct ParseExpression
: public Unary
<Iter
,Value
,FuncType
>
754 virtual Value
accept(FuncType
&f
)
760 template<class Iter
, class Value
, class FuncType
>
761 struct UnaryExpression
: public Unary
<Iter
,Value
,FuncType
>
763 virtual Value
accept(FuncType
&f
)
769 template<class Iter
, class Value
, class FuncType
>
770 struct UnaryPlusExpression
: public UnaryExpression
<Iter
,Value
,FuncType
>
772 virtual Value
accept(FuncType
&f
)
778 template<class Iter
, class Value
, class FuncType
>
779 struct UnaryMinusExpression
: public UnaryExpression
<Iter
,Value
,FuncType
>
781 virtual Value
accept(FuncType
&f
)
787 template<class Iter
, class Value
, class FuncType
>
788 struct UnaryNotExpression
: public UnaryExpression
<Iter
,Value
,FuncType
>
790 virtual Value
accept(FuncType
&f
)
796 template<class Iter
, class Value
, class FuncType
>
797 struct MultiplicativeExpression
: public Binary
<Iter
,Value
,FuncType
>
799 virtual Value
accept(FuncType
&f
)
805 template<class Iter
, class Value
, class FuncType
>
806 struct MultiplyExpression
: public MultiplicativeExpression
<Iter
,Value
,FuncType
>
808 virtual Value
accept(FuncType
&f
)
814 template<class Iter
, class Value
, class FuncType
>
815 struct DivideExpression
: public MultiplicativeExpression
<Iter
,Value
,FuncType
>
817 virtual Value
accept(FuncType
&f
)
823 template<class Iter
, class Value
, class FuncType
>
824 struct ModuloExpression
: public MultiplicativeExpression
<Iter
,Value
,FuncType
>
826 virtual Value
accept(FuncType
&f
)
832 template<class Iter
, class Value
, class FuncType
>
833 struct AdditiveExpression
: public Binary
<Iter
,Value
,FuncType
>
835 virtual Value
accept(FuncType
&f
)
841 template<class Iter
, class Value
, class FuncType
>
842 struct AddExpression
: public AdditiveExpression
<Iter
,Value
,FuncType
>
844 virtual Value
accept(FuncType
&f
)
850 template<class Iter
, class Value
, class FuncType
>
851 struct SubtractExpression
: public AdditiveExpression
<Iter
,Value
,FuncType
>
853 virtual Value
accept(FuncType
&f
)
859 template<class Iter
, class Value
, class FuncType
>
860 struct RelationalExpression
: public Binary
<Iter
,Value
,FuncType
>
862 virtual Value
accept(FuncType
&f
)
868 template<class Iter
, class Value
, class FuncType
>
869 struct LessExpression
: public RelationalExpression
<Iter
,Value
,FuncType
>
871 virtual Value
accept(FuncType
&f
)
877 template<class Iter
, class Value
, class FuncType
>
878 struct LessEqualExpression
: public RelationalExpression
<Iter
,Value
,FuncType
>
880 virtual Value
accept(FuncType
&f
)
886 template<class Iter
, class Value
, class FuncType
>
887 struct GreaterExpression
: public RelationalExpression
<Iter
,Value
,FuncType
>
889 virtual Value
accept(FuncType
&f
)
895 template<class Iter
, class Value
, class FuncType
>
896 struct GreaterEqualExpression
: public RelationalExpression
<Iter
,Value
,FuncType
>
898 virtual Value
accept(FuncType
&f
)
904 template<class Iter
, class Value
, class FuncType
>
905 struct EqualityExpression
: public Binary
<Iter
,Value
,FuncType
>
907 virtual Value
accept(FuncType
&f
)
913 template<class Iter
, class Value
, class FuncType
>
914 struct EqualExpression
: public EqualityExpression
<Iter
,Value
,FuncType
>
916 virtual Value
accept(FuncType
&f
)
922 template<class Iter
, class Value
, class FuncType
>
923 struct NotEqualExpression
: public EqualityExpression
<Iter
,Value
,FuncType
>
925 virtual Value
accept(FuncType
&f
)
931 template<class Iter
, class Value
, class FuncType
>
932 struct LogicalAndExpression
: public Binary
<Iter
,Value
,FuncType
>
934 virtual Value
accept(FuncType
&f
)
940 template<class Iter
, class Value
, class FuncType
>
941 struct LogicalOrExpression
: public Binary
<Iter
,Value
,FuncType
>
943 virtual Value
accept(FuncType
&f
)
949 template<class Iter
, class Value
, class FuncType
>
950 struct AssignmentExpression
: public Binary
<Iter
,Value
,FuncType
>
952 virtual Value
accept(FuncType
&f
)
958 template<class Iter
, class Value
, class FuncType
>
959 struct GlobalAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
961 virtual Value
accept(FuncType
&f
)
967 template<class Iter
, class Value
, class FuncType
>
968 struct LocalAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
970 virtual Value
accept(FuncType
&f
)
976 template<class Iter
, class Value
, class FuncType
>
977 struct AddAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
979 virtual Value
accept(FuncType
&f
)
985 template<class Iter
, class Value
, class FuncType
>
986 struct SubAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
988 virtual Value
accept(FuncType
&f
)
994 template<class Iter
, class Value
, class FuncType
>
995 struct MulAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
997 virtual Value
accept(FuncType
&f
)
1003 template<class Iter
, class Value
, class FuncType
>
1004 struct DivAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
1006 virtual Value
accept(FuncType
&f
)
1012 template<class Iter
, class Value
, class FuncType
>
1013 struct ModAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
1015 virtual Value
accept(FuncType
&f
)
1021 template<class Iter
, class Value
, class FuncType
>
1022 struct InitializerAssignmentExpression
: public Variadic
<Iter
,Value
,FuncType
>
1024 virtual Value
accept(FuncType
&f
)
1030 template<class Iter
, class Value
, class FuncType
>
1031 struct Block
: public Unary
<Iter
,Value
,FuncType
>
1033 virtual Value
accept(FuncType
&f
)
1039 template<class Iter
, class Value
, class FuncType
>
1040 struct TableInitializer
: public Variadic
<Iter
,Value
,FuncType
>
1042 virtual Value
accept(FuncType
&f
)
1048 template<class Iter
, class Value
, class FuncType
>
1049 struct ArrayInitializer
: public Variadic
<Iter
,Value
,FuncType
>
1051 virtual Value
accept(FuncType
&f
)
1057 template<class Iter
, class Value
, class FuncType
>
1058 struct IdentifierList
: public Variadic
<Iter
,Value
,FuncType
>
1060 virtual Value
accept(FuncType
&f
)
1066 template<class Iter
, class Value
, class FuncType
>
1067 struct Function
: public Binary
<Iter
,Value
,FuncType
>
1069 virtual Value
accept(FuncType
&f
)
1075 template<class Iter
, class Value
, class FuncType
>
1076 struct Tree
: public Unary
<Iter
,Value
,FuncType
>
1078 virtual Value
accept(FuncType
&f
)
1084 template<class Iter
, class Value
, class FuncType
>
1085 struct Root
: public Variadic
<Iter
,Value
,FuncType
>
1087 virtual Value
accept(FuncType
&f
)
1093 template<class Iter
, class Value
, class FuncType
>
1094 struct Type
: public Unary
<Iter
,Value
,FuncType
>
1096 virtual Value
accept(FuncType
&f
)
1102 template<class Iter
, class Value
, class FuncType
>
1103 struct Size
: public Unary
<Iter
,Value
,FuncType
>
1105 virtual Value
accept(FuncType
&f
)
1111 template<class Iter
, class Value
, class FuncType
>
1112 struct DoImport
: public Unary
<Iter
,Value
,FuncType
>
1114 virtual Value
accept(FuncType
&f
)
1121 template<class Iter
, class Value
, class FuncType
>
1122 struct StatementList
: public Variadic
<Iter
,Value
,FuncType
>
1124 virtual Value
accept(FuncType
&f
)
1130 template<class Iter
, class Value
, class FuncType
>
1131 struct IfStatement
: public Variadic
<Iter
,Value
,FuncType
>
1133 virtual Value
accept(FuncType
&f
)
1139 template<class Iter
, class Value
, class FuncType
>
1140 struct WhileStatement
: public Binary
<Iter
,Value
,FuncType
>
1142 virtual Value
accept(FuncType
&f
)
1148 template<class Iter
, class Value
, class FuncType
>
1149 struct ForStatement
: public Variadic
<Iter
,Value
,FuncType
>
1151 virtual Value
accept(FuncType
&f
)
1157 template<class Iter
, class Value
, class FuncType
>
1158 struct ForEachStatement
: public Variadic
<Iter
,Value
,FuncType
>
1160 virtual Value
accept(FuncType
&f
)
1166 template<class Iter
, class Value
, class FuncType
>
1167 struct ReturnStatement
: public Variadic
<Iter
,Value
,FuncType
>
1169 virtual Value
accept(FuncType
&f
)
1175 template<class Iter
, class Value
, class FuncType
>
1176 struct ContinueStatement
: public Nullary
<Iter
,Value
,FuncType
>
1178 virtual Value
accept(FuncType
&f
)
1184 template<class Iter
, class Value
, class FuncType
>
1185 struct BreakStatement
: public Nullary
<Iter
,Value
,FuncType
>
1187 virtual Value
accept(FuncType
&f
)
1193 template<class Iter
, class Value
, class FuncType
>
1194 struct ExpressionStatement
: public Unary
<Iter
,Value
,FuncType
>
1196 virtual Value
accept(FuncType
&f
)
1202 template<class Iter
, class Value
, class FuncType
>
1203 struct BuiltinFunction
: public Nullary
<Iter
,Value
,FuncType
>
1205 virtual Value
accept(FuncType
&f
)
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
)
1221 template<class Value
, class FuncType
>
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();
1236 accepted
= this->end
;
1237 auto node
= statement_list();
1238 node
->source
= source
;
1239 node
->tokens
= tokens
;
1241 node
->inject_dependencies();
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();
1253 accepted
= this->end
;
1254 auto node
= expression();
1255 node
->source
= source
;
1256 node
->tokens
= tokens
;
1258 node
->inject_dependencies();
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");
1287 void rewind(Iter 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
)
1316 bool expect(TokenType t
)
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
) + "'"
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
) + "'"
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
>>();
1370 expect(Tok_LeftParen
);
1371 while(!accept(Tok_RightParen
))
1373 node
->children
.push_back(field_name());
1374 if(!peek(Tok_RightParen
))
1379 node
->end
= accepted
+1;
1380 return std::move(node
);
1385 auto node
= std::make_shared
<Function
<Iter
,Value
,FuncType
>>();
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
>>();
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
>>();
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
);
1420 auto node
= std::make_shared
<Tree
<Iter
,Value
,FuncType
>>();
1422 if(accept(Tok_Expression
))
1424 expect(Tok_LeftParen
);
1425 node
->children
[0] = expression();
1426 expect(Tok_RightParen
);
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
);
1441 auto node
= std::make_shared
<Root
<Iter
,Value
,FuncType
>>();
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
);
1455 auto node
= std::make_shared
<Eval
<Iter
,Value
,FuncType
>>();
1458 expect(Tok_LeftParen
);
1459 node
->children
[0] = unary_expression();
1460 expect(Tok_RightParen
);
1461 node
->end
= accepted
+1;
1462 return std::move(node
);
1467 auto node
= std::make_shared
<Type
<Iter
,Value
,FuncType
>>();
1470 if(accept(Tok_LeftParen
))
1472 node
->children
[0] = expression();
1473 expect(Tok_RightParen
);
1475 node
->end
= accepted
+1;
1476 return std::move(node
);
1481 auto node
= std::make_shared
<Size
<Iter
,Value
,FuncType
>>();
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
>>();
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
>>();
1511 expect(Tok_LeftBrace
);
1512 while(!accept(Tok_RightBrace
))
1514 node
->children
.push_back(initializer_assignment_expression());
1515 if(!peek(Tok_RightBrace
))
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
>>();
1528 expect(Tok_LeftBracket
);
1529 while(!accept(Tok_RightBracket
))
1531 node
->children
.push_back(logical_or_expression());
1532 if(!peek(Tok_RightBracket
))
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
))
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
))
1591 else if (peek(Tok_Root
))
1595 else if (peek(Tok_TypeOf
))
1599 else if (peek(Tok_Eval
))
1603 else if (peek(Tok_SizeOf
))
1607 else if(peek(Tok_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
))
1625 expect("primary expression");
1630 return_t
postfix_expression()
1633 return_t left
= primary_expression();
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
>>();
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
);
1677 auto node
= std::make_shared
<FieldAccess
<Iter
,Value
,FuncType
>>();
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
>>();
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
>>();
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
))
1737 node
->end
= accepted
+1;
1738 left
= std::move(node
);
1742 auto node
= std::make_shared
<FieldAccess
<Iter
,Value
,FuncType
>>();
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
>>();
1754 node
->children
.push_back(std::move(left
));
1755 while(!accept(Tok_RightParen
))
1757 node
->children
.push_back(expression());
1758 if(!peek(Tok_RightParen
))
1763 node
->end
= accepted
+1;
1764 left
= std::move(node
);
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
>>;
1779 switch(accepted
->type
)
1782 node
= node_t(new UnaryPlusExpression
<Iter
,Value
,FuncType
>);
1785 node
= node_t(new UnaryMinusExpression
<Iter
,Value
,FuncType
>);
1788 node
= node_t(new UnaryNotExpression
<Iter
,Value
,FuncType
>);
1793 node
->begin
= accepted
;
1794 node
->children
[0] = postfix_expression();
1795 node
->end
= accepted
+1;
1796 return std::move(node
);
1800 return postfix_expression();
1804 return_t
multiplicative_expression()
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
>>;
1812 switch(accepted
->type
)
1815 node
= node_t(new MultiplyExpression
<Iter
,Value
,FuncType
>);
1818 node
= node_t(new DivideExpression
<Iter
,Value
,FuncType
>);
1821 node
= node_t(new ModuloExpression
<Iter
,Value
,FuncType
>);
1827 node
->children
[0] = std::move(left
);
1828 node
->children
[1] = multiplicative_expression();
1829 node
->end
= accepted
+1;
1830 return std::move(node
);
1834 return std::move(left
);
1838 return_t
additive_expression()
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
>>;
1846 switch(accepted
->type
)
1849 node
= node_t(new AddExpression
<Iter
,Value
,FuncType
>);
1852 node
= node_t(new SubtractExpression
<Iter
,Value
,FuncType
>);
1858 node
->children
[0] = std::move(left
);
1859 node
->children
[1] = additive_expression();
1860 node
->end
= accepted
+1;
1861 return std::move(node
);
1865 return std::move(left
);
1869 return_t
relational_expression()
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
>>;
1877 switch(accepted
->type
)
1880 node
= node_t(new LessExpression
<Iter
,Value
,FuncType
>);
1883 node
= node_t(new LessEqualExpression
<Iter
,Value
,FuncType
>);
1886 node
= node_t(new GreaterExpression
<Iter
,Value
,FuncType
>);
1888 case Tok_GreaterEqual
:
1889 node
= node_t(new GreaterEqualExpression
<Iter
,Value
,FuncType
>);
1895 node
->children
[0] = std::move(left
);
1896 node
->children
[1] = relational_expression();
1897 node
->end
= accepted
+1;
1898 return std::move(node
);
1902 return std::move(left
);
1906 return_t
equality_expression()
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
>>;
1914 switch(accepted
->type
)
1917 node
= node_t(new EqualExpression
<Iter
,Value
,FuncType
>);
1920 node
= node_t(new NotEqualExpression
<Iter
,Value
,FuncType
>);
1926 node
->children
[0] = std::move(left
);
1927 node
->children
[1] = relational_expression();
1928 node
->end
= accepted
+1;
1929 return std::move(node
);
1933 return std::move(left
);
1937 return_t
logical_and_expression()
1940 return_t left
= equality_expression();
1941 if(accept(Tok_LogicalAnd
))
1943 auto node
= std::make_shared
<LogicalAndExpression
<Iter
,Value
,FuncType
>>();
1945 node
->children
[0] = std::move(left
);
1946 node
->children
[1] = equality_expression();
1947 node
->end
= accepted
+1;
1948 return std::move(node
);
1952 return std::move(left
);
1956 return_t
logical_or_expression()
1959 return_t left
= logical_and_expression();
1960 if(accept(Tok_LogicalOr
))
1962 auto node
= std::make_shared
<LogicalOrExpression
<Iter
,Value
,FuncType
>>();
1964 node
->children
[0] = std::move(left
);
1965 node
->children
[1] = logical_and_expression();
1966 node
->end
= accepted
+1;
1967 return std::move(node
);
1971 return std::move(left
);
1975 return_t
assignment_expression()
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
;
1988 std::make_shared
<GlobalAssignmentExpression
<Iter
,Value
,FuncType
>>()
1991 std::make_shared
<LocalAssignmentExpression
<Iter
,Value
,FuncType
>>()
1993 std::make_shared
<AssignmentExpression
<Iter
,Value
,FuncType
>>();
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
>>();
2032 return logical_or_expression();
2035 node
->children
[0] = std::move(left
);
2036 node
->children
[1] = assignment_expression();
2037 node
->end
= accepted
+1;
2038 return std::move(node
);
2041 return logical_or_expression();
2044 return_t
initializer_assignment_expression()
2046 auto node
= std::make_shared
<InitializerAssignmentExpression
<Iter
,Value
,FuncType
>>();
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();
2064 auto node
= std::make_shared
<Block
<Iter
,Value
,FuncType
>>();
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
>>();
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
>>();
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
);
2105 auto node
= std::make_shared
<Nop
<Iter
,Value
,FuncType
>>();
2108 return std::move(node
);
2111 return_t
for_statement()
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
>>();
2120 node
->children
.push_back(field_name());
2121 if(accept(Tok_Comma
))
2123 node
->children
.push_back(field_name());
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
);
2134 auto node
= std::make_shared
<ForStatement
<Iter
,Value
,FuncType
>>();
2136 if(accept(Tok_Semicolon
))
2138 node
->children
.push_back(nop());
2142 node
->children
.push_back(expression());
2143 expect(Tok_Semicolon
);
2145 if(accept(Tok_Semicolon
))
2147 node
->children
.push_back(nop());
2151 node
->children
.push_back(expression());
2152 expect(Tok_Semicolon
);
2154 if(accept(Tok_RightParen
))
2156 node
->children
.push_back(nop());
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
>>();
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
>>();
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
>>();
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
))
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();
2237 auto node
= std::make_shared
<ExpressionStatement
<Iter
,Value
,FuncType
>>();
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
>>();
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
>
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
;
2282 ProtoStringContainer(StringIterator begin_
, StringIterator end_
):
2285 hash(std::hash
<StringContainer
>()(StringContainer(begin_
, end_
)))
2290 struct UserPtrContainer
2294 UserPtrContainer(void* ptr
): realptr(ptr
)
2313 Nil
, Number
, String
, ProtoString
,
2314 Tree
, Function
, Table
, Array
,
2315 Return
, Break
, Continue
, UserPtr
,
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
;
2334 if(m_type
== Type::ProtoString
)
2336 return Type::String
;
2338 else if((m_type
>= Type::Return
) && (m_type
<= Type::Continue
))
2345 const char* typeName() const
2355 case Type::ProtoString
:
2356 return "protostring";
2359 case Type::Function
:
2369 case Type::Continue
:
2379 Type
internal_type() const
2384 const NumberType
& number() const
2389 NumberType
& number()
2394 const PointerContainer
& pointer() const
2399 PointerContainer
& pointer()
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
2429 TreeContainer
tree()
2434 const TableContainer
& table() const
2439 TableContainer
& table()
2444 const StringContainer
string() const
2446 if(m_type
== Type::ProtoString
)
2448 return std::string(m_protostr
.begin
, m_protostr
.end
);
2453 StringContainer
& string()
2459 const ArrayContainer
& array() const
2464 ArrayContainer
& array()
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
))
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
)
2535 case Type::Continue
:
2537 new (&m_number
) NumberContainer(0);
2540 new (&m_number
) NumberContainer(that
.m_number
);
2543 new (&m_strval
) StringContainer(that
.m_strval
);
2545 case Type::ProtoString
:
2546 new (&m_protostr
) ProtoStringContainer(that
.m_protostr
);
2549 case Type::Function
:
2550 new (&m_pointer
) PointerContainer(that
.m_pointer
);
2553 new (&m_table
) TableContainer(that
.m_table
);
2556 new (&m_array
) ArrayContainer(that
.m_array
);
2560 fprintf(stderr
, "BaseValue(BaseValue&): this.userptr=%p, that.userptr=%p\n",
2561 m_userptr
.realptr
, that
.m_userptr
.realptr
2564 new (&m_userptr
) UserPtrContainer(that
.m_userptr
);
2571 BaseValue
& operator=(const BaseValue
&that
)
2576 m_strval
.~StringContainer();
2579 case Type::Function
:
2580 m_pointer
.~PointerContainer();
2583 m_table
.~TableContainer();
2586 m_array
.~ArrayContainer();
2591 m_type
= that
.m_type
;
2596 case Type::Continue
:
2598 new (&m_number
) NumberContainer(0);
2601 new (&m_number
) NumberContainer(that
.m_number
);
2604 new (&m_strval
) StringContainer(that
.m_strval
);
2606 case Type::ProtoString
:
2607 new (&m_protostr
) ProtoStringContainer(that
.m_protostr
);
2610 case Type::Function
:
2611 new (&m_pointer
) PointerContainer(that
.m_pointer
);
2615 fprintf(stderr
, "BaseValue::operator=(BaseValue&): this.userptr=%p, that.userptr=%p\n",
2616 m_userptr
.realptr
, that
.m_userptr
.realptr
2619 new(&m_userptr
) UserPtrContainer(that
.m_userptr
);
2622 new (&m_table
) TableContainer(that
.m_table
);
2625 new (&m_array
) ArrayContainer(that
.m_array
);
2638 m_strval
.~StringContainer();
2641 case Type::Function
:
2642 m_pointer
.~PointerContainer();
2645 m_table
.~TableContainer();
2648 m_array
.~ArrayContainer();
2655 bool operator==(const BaseValue
&that
) const
2657 if(type() != that
.type())
2665 case Type::Continue
:
2668 return number() == that
.number();
2670 case Type::Function
:
2671 return pointer() == that
.pointer();
2673 return table() == that
.table();
2675 return array() == that
.array();
2677 return (userptr() == that
.userptr());
2679 if(that
.m_type
== Type::String
)
2681 return m_strval
== that
.m_strval
;
2685 if(m_strval
.end()-m_strval
.begin() != that
.m_protostr
.end
- that
.m_protostr
.begin
)
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())
2698 return std::equal(m_protostr
.begin
, m_protostr
.end
, that
.m_strval
.begin());
2702 if(m_protostr
.end
- m_protostr
.begin
!= that
.m_protostr
.end
- that
.m_protostr
.begin
)
2706 return std::equal(m_protostr
.begin
, m_protostr
.end
, that
.m_protostr
.begin
);
2713 friend struct std::hash
<BaseValue
<Iter
, FuncType
>>;
2719 template<typename Iter
, typename FuncType
>
2720 struct hash
<Aqua::BaseValue
<Iter
, FuncType
>>
2723 using T
= Aqua::BaseValue
<Iter
, FuncType
>;
2724 std::size_t operator()(Aqua::BaseValue
<Iter
, FuncType
> const& x
) const
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());;
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());
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
>;
2758 return out
<< "nil";
2759 case T::Type::Number
:
2760 return out
<< v
.number();
2761 case T::Type::String
:
2762 return out
<< v
.string();
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();
2780 template<typename Type
> class Table
2783 using Iterator
= typename
std::unordered_map
<Type
, Type
>::iterator
;
2786 std::unordered_map
<Type
, Type
> m_data
;
2790 void set(const Type
& key
, const Type
& value
)
2792 if(key
.type() == Type::Type::Nil
)
2796 if(value
.type() == Type::Type::Nil
)
2800 m_data
[key
] = value
;
2803 Type
get(const Type
& key
)
2805 auto iter
= m_data
.find(key
);
2806 if(iter
== m_data
.end())
2812 return iter
->second
;
2816 void append(const Table
& that
)
2818 m_data
.insert(that
.m_data
.begin(), that
.m_data
.end());
2823 return m_data
.size();
2828 return m_data
.begin();
2833 return m_data
.end();
2837 template<typename Type
>
2841 using Iterator
= typename
std::vector
<Type
>::iterator
;
2844 std::vector
<Type
> m_data
;
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
);
2872 return m_data
.size();
2877 return m_data
.begin();
2882 return m_data
.end();
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();
2910 m_self
->endFunctionScope();
2916 Interpreter
& m_self
;
2918 LocalScope(Interpreter
& self
): m_self(self
)
2920 m_self
.beginLocalScope();
2925 m_self
.endLocalScope();
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
;
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
);
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
);
3137 Value
eval(const std::string
&source
);
3139 Value
run(const std::string
&source
);