10 #include <unordered_map>
18 return x
== std::trunc(x
);
20 std::string
unescape(const std::string
&str
);
28 Tok_Number
, Tok_Identifier
, Tok_String
,
29 Tok_LeftParen
, Tok_RightParen
, Tok_LeftBracket
,
30 Tok_RightBracket
, Tok_LeftBrace
, Tok_RightBrace
,
31 Tok_Plus
, Tok_Dash
, Tok_Star
,
32 Tok_Percent
, Tok_Slash
, Tok_PlusEqual
,
33 Tok_DashEqual
, Tok_StarEqual
, Tok_PercentEqual
,
34 Tok_SlashEqual
, Tok_Not
, Tok_Comma
,
35 Tok_Semicolon
, Tok_Dot
, Tok_Colon
,
36 Tok_Equal
, Tok_EqOp
, Tok_NotEqual
,
37 Tok_Greater
, Tok_GreaterEqual
, Tok_Less
,
38 Tok_LessEqual
, Tok_LogicalAnd
, Tok_LogicalOr
,
39 Tok_If
, Tok_Else
, Tok_While
,
40 Tok_For
, Tok_Return
, Tok_Break
,
41 Tok_Continue
, Tok_Function
, Tok_Expression
,
42 Tok_Statement
, Tok_Root
, Tok_Nil
,
43 Tok_Eval
, Tok_ParseStatement
, Tok_ParseExpression
,
44 Tok_Global
, Tok_Local
, Tok_Print
,
45 Tok_Type
, Tok_Size
, Tok_Switch
,
49 static const std::unordered_map
<std::string
, TokenType
> keywords
=
55 {"return", Tok_Return
},
57 {"continue", Tok_Continue
},
58 {"func", Tok_Function
},
60 {"expression", Tok_Expression
},
61 {"statement", Tok_Statement
},
63 {"parse_statement", Tok_ParseStatement
},
64 {"parse_expression", Tok_ParseExpression
},
65 {"global", Tok_Global
},
70 // these aren't implemented yet
71 {"switch", Tok_Switch
},
73 {"import", Tok_Import
},
83 Token(TokenType type_
, Iter begin_
, Iter end_
):
91 // helper to find line & column position on demand
93 std::pair
<int, int> line_column(Iter begin
, Iter pos
)
95 int line
= 1, column
= 0;
96 for(Iter i
= begin
; i
!=pos
; ++i
)
105 return std::make_pair(line
, column
);
109 std::shared_ptr
<std::vector
<Token
<Iter
>>> tokenize(Iter begin
, Iter end
)
111 auto tokens_ptr
= std::make_shared
<std::vector
<Token
<Iter
>>>();
112 std::vector
<Token
<Iter
>> &tokens
= *tokens_ptr
;
121 case '\n': // ignore whitespaces
125 tokens
.emplace_back(Tok_LeftParen
, i
, i
+1);
129 tokens
.emplace_back(Tok_RightParen
, i
, i
+1);
133 tokens
.emplace_back(Tok_LeftBracket
, i
, i
+1);
137 tokens
.emplace_back(Tok_RightBracket
, i
, i
+1);
141 tokens
.emplace_back(Tok_LeftBrace
, i
, i
+1);
145 tokens
.emplace_back(Tok_RightBrace
, i
, i
+1);
149 tokens
.emplace_back(Tok_Comma
, i
, i
+1);
153 tokens
.emplace_back(Tok_Dot
, i
, i
+1);
157 tokens
.emplace_back(Tok_Semicolon
, i
, i
+1);
161 tokens
.emplace_back(Tok_Colon
, i
, i
+1);
165 if(i
+1 != end
&& *(i
+1) == '=')
167 tokens
.emplace_back(Tok_PlusEqual
, i
, i
+2);
172 tokens
.emplace_back(Tok_Plus
, i
, i
+1);
177 if(i
+1 != end
&& *(i
+1) == '=')
179 tokens
.emplace_back(Tok_DashEqual
, i
, i
+2);
184 tokens
.emplace_back(Tok_Dash
, i
, i
+1);
189 if(i
+1 != end
&& *(i
+1) == '=')
191 tokens
.emplace_back(Tok_StarEqual
, i
, i
+2);
196 tokens
.emplace_back(Tok_Star
, i
, i
+1);
201 if(i
+1 != end
&& *(i
+1) == '=')
203 tokens
.emplace_back(Tok_PercentEqual
, i
, i
+2);
208 tokens
.emplace_back(Tok_Percent
, i
, i
+1);
213 if(i
+1 != end
&& *(i
+1) == '/')
216 while(i
!=end
&& *i
!= '\n')
221 else if(i
+1 != end
&& *(i
+1) == '=')
223 tokens
.emplace_back(Tok_SlashEqual
, i
, i
+2);
228 tokens
.emplace_back(Tok_Slash
, i
, i
+1);
233 if(i
+1 != end
&& *(i
+1) == '=')
235 tokens
.emplace_back(Tok_LessEqual
, i
, i
+2);
240 tokens
.emplace_back(Tok_Less
, i
, i
+1);
245 if(i
+1 != end
&& *(i
+1) == '=')
247 tokens
.emplace_back(Tok_GreaterEqual
, i
, i
+2);
252 tokens
.emplace_back(Tok_Greater
, i
, i
+1);
257 if(i
+1 != end
&& *(i
+1) == '=')
259 tokens
.emplace_back(Tok_EqOp
, i
, i
+2);
264 tokens
.emplace_back(Tok_Equal
, i
, i
+1);
269 if(i
+1 != end
&& *(i
+1) == '=')
271 tokens
.emplace_back(Tok_NotEqual
, i
, i
+2);
276 tokens
.emplace_back(Tok_Not
, i
, i
+1);
281 if(i
+1 != end
&& *(i
+1) == '&')
283 tokens
.emplace_back(Tok_LogicalAnd
, i
, i
+2);
288 auto lc
= line_column(begin
, i
);
289 throw std::runtime_error(
290 std::to_string(lc
.first
) + ":" +
291 std::to_string(lc
.second
) + ": unrecognized symbol: '" + *i
+ "'"
296 if(i
+1 != end
&& *(i
+1) == '|')
298 tokens
.emplace_back(Tok_LogicalOr
, i
, i
+2);
303 auto lc
= line_column(begin
, i
);
304 throw std::runtime_error(
305 std::to_string(lc
.first
) + ":" +
306 std::to_string(lc
.second
) + ": unrecognized symbol: '" + *i
+ "'"
312 while(i
!=end
&& !(*i
== '"' && *(i
-1) != '\\'))
317 tokens
.emplace_back(Tok_String
, start
, i
);
322 while(i
!=end
&& std::isdigit(*i
))
326 if(i
!=end
&& *i
=='.')
329 while(i
!=end
&& std::isdigit(*i
))
334 if(i
!=end
&& *i
=='e')
337 if(i
!=end
&& *i
=='-')
341 while(i
!=end
&& std::isdigit(*i
))
346 tokens
.emplace_back(Tok_Number
, start
, i
);
349 else if(std::isalpha(*i
) || *i
== '_')
352 while(i
!=end
&& (std::isalnum(*i
) || *i
== '_'))
356 auto kw
= keywords
.find(std::string(start
, i
));
357 if(kw
== keywords
.end())
359 tokens
.emplace_back(Tok_Identifier
, start
, i
);
363 tokens
.emplace_back(kw
->second
, start
, i
);
369 auto lc
= line_column(begin
, i
);
370 throw std::runtime_error(
371 std::to_string(lc
.first
) + ":" +
372 std::to_string(lc
.second
) + ": unrecognized symbol: '" + *i
+ "'"
380 // Abstract Syntax Tree node types
381 template<typename Iter
, typename Value
, typename FuncType
>
382 struct ASTNode
: public std::enable_shared_from_this
<ASTNode
<Iter
,Value
,FuncType
>>
386 std::shared_ptr
<std::string
> source
;
387 std::shared_ptr
<std::vector
<Token
<std::string::iterator
>>> tokens
;
388 std::weak_ptr
<ASTNode
<Iter
,Value
,FuncType
>> root
;
390 virtual Value
accept(FuncType
&) = 0;
392 virtual std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>& operator[](size_t) = 0;
394 virtual size_t size() const = 0;
396 virtual void inject_dependencies() = 0;
399 template<class Iter
, class Value
, class FuncType
>
400 struct Variadic
: public ASTNode
<Iter
,Value
,FuncType
>
402 std::vector
<std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>> children
;
404 virtual Value
accept(FuncType
&f
)
409 virtual std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>& operator[](size_t i
)
414 virtual size_t size() const
416 return children
.size();
419 virtual void inject_dependencies()
421 for(auto& child
: children
)
423 child
->root
= this->root
;
424 child
->source
= this->source
;
425 child
->tokens
= this->tokens
;
426 child
->inject_dependencies();
431 template<class Iter
, class Value
, class FuncType
>
432 struct Ternary
: public ASTNode
<Iter
,Value
,FuncType
>
434 std::array
<std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>, 3> children
;
436 virtual Value
accept(FuncType
&f
)
441 virtual std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>& operator[](size_t i
)
446 virtual size_t size() const
448 return children
.size();
451 virtual void inject_dependencies()
453 for(auto& child
: children
)
455 child
->root
= this->root
;
456 child
->source
= this->source
;
457 child
->tokens
= this->tokens
;
458 child
->inject_dependencies();
463 template<class Iter
, class Value
, class FuncType
>
464 struct Binary
: public ASTNode
<Iter
,Value
,FuncType
>
466 std::array
<std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>, 2> children
;
468 virtual Value
accept(FuncType
&f
)
473 virtual std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>& operator[](size_t i
)
478 virtual size_t size() const
480 return children
.size();
483 virtual void inject_dependencies()
485 for(auto& child
: children
)
487 child
->root
= this->root
;
488 child
->source
= this->source
;
489 child
->tokens
= this->tokens
;
490 child
->inject_dependencies();
495 template<class Iter
, class Value
, class FuncType
>
496 struct Unary
: public ASTNode
<Iter
,Value
,FuncType
>
498 std::array
<std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>, 1> children
;
500 virtual Value
accept(FuncType
&f
)
505 virtual std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>& operator[](size_t i
)
510 virtual size_t size() const
512 return children
.size();
515 virtual void inject_dependencies()
517 for(auto& child
: children
)
519 child
->root
= this->root
;
520 child
->source
= this->source
;
521 child
->tokens
= this->tokens
;
522 child
->inject_dependencies();
527 template<class Iter
, class Value
, class FuncType
>
528 struct Nullary
: public ASTNode
<Iter
,Value
,FuncType
>
530 std::array
<std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>, 0> children
;
532 virtual Value
accept(FuncType
&f
)
537 virtual std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>& operator[](size_t i
)
542 virtual size_t size() const
544 return children
.size();
547 virtual void inject_dependencies()
549 for(auto& child
: children
)
551 child
->root
= this->root
;
552 child
->source
= this->source
;
553 child
->tokens
= this->tokens
;
554 child
->inject_dependencies();
559 template<class Iter
, class Value
, class FuncType
>
560 struct Variable
: public Nullary
<Iter
,Value
,FuncType
>
564 virtual Value
accept(FuncType
&f
)
570 template<class Iter
, class Value
, class FuncType
>
571 struct GlobalVariable
: public Variable
<Iter
,Value
,FuncType
>
573 virtual Value
accept(FuncType
&f
)
579 template<class Iter
, class Value
, class FuncType
>
580 struct LocalVariable
: public Variable
<Iter
,Value
,FuncType
>
582 virtual Value
accept(FuncType
&f
)
588 template<class Iter
, class Value
, class FuncType
>
589 struct FieldName
: public Nullary
<Iter
,Value
,FuncType
>
592 virtual Value
accept(FuncType
&f
)
598 template<class Iter
, class Value
, class FuncType
>
599 struct Nop
: public Nullary
<Iter
,Value
,FuncType
>
602 virtual Value
accept(FuncType
&f
)
608 template<class Iter
, class Value
, class FuncType
>
609 struct Constant
: public Nullary
<Iter
,Value
,FuncType
>
613 virtual Value
accept(FuncType
&f
)
619 template<class Iter
, class Value
, class FuncType
>
620 struct Nil
: public Nullary
<Iter
,Value
,FuncType
>
622 virtual Value
accept(FuncType
&f
)
628 template<class Iter
, class Value
, class FuncType
>
629 struct String
: public Nullary
<Iter
,Value
,FuncType
>
633 virtual Value
accept(FuncType
&f
)
639 template<class Iter
, class Value
, class FuncType
>
640 struct FieldAccess
: public Binary
<Iter
,Value
,FuncType
>
642 virtual Value
accept(FuncType
&f
)
648 template<class Iter
, class Value
, class FuncType
>
649 struct FieldAssignment
: public Ternary
<Iter
,Value
,FuncType
>
651 virtual Value
accept(FuncType
&f
)
657 template<class Iter
, class Value
, class FuncType
>
658 struct AddFieldAssignment
: public FieldAssignment
<Iter
,Value
,FuncType
>
660 virtual Value
accept(FuncType
&f
)
666 template<class Iter
, class Value
, class FuncType
>
667 struct SubFieldAssignment
: public FieldAssignment
<Iter
,Value
,FuncType
>
669 virtual Value
accept(FuncType
&f
)
675 template<class Iter
, class Value
, class FuncType
>
676 struct MulFieldAssignment
: public FieldAssignment
<Iter
,Value
,FuncType
>
678 virtual Value
accept(FuncType
&f
)
684 template<class Iter
, class Value
, class FuncType
>
685 struct DivFieldAssignment
: public FieldAssignment
<Iter
,Value
,FuncType
>
687 virtual Value
accept(FuncType
&f
)
693 template<class Iter
, class Value
, class FuncType
>
694 struct ModFieldAssignment
: public FieldAssignment
<Iter
,Value
,FuncType
>
696 virtual Value
accept(FuncType
&f
)
702 template<class Iter
, class Value
, class FuncType
>
703 struct Call
: public Variadic
<Iter
,Value
,FuncType
>
705 virtual Value
accept(FuncType
&f
)
711 template<class Iter
, class Value
, class FuncType
>
712 struct MemberCall
: public Variadic
<Iter
,Value
,FuncType
>
714 virtual Value
accept(FuncType
&f
)
720 template<class Iter
, class Value
, class FuncType
>
721 struct EvalStatement
: public Unary
<Iter
,Value
,FuncType
>
723 virtual Value
accept(FuncType
&f
)
729 template<class Iter
, class Value
, class FuncType
>
730 struct Eval
: public Unary
<Iter
,Value
,FuncType
>
732 virtual Value
accept(FuncType
&f
)
738 template<class Iter
, class Value
, class FuncType
>
739 struct ParseStatement
: public Unary
<Iter
,Value
,FuncType
>
741 virtual Value
accept(FuncType
&f
)
747 template<class Iter
, class Value
, class FuncType
>
748 struct ParseExpression
: public Unary
<Iter
,Value
,FuncType
>
750 virtual Value
accept(FuncType
&f
)
756 template<class Iter
, class Value
, class FuncType
>
757 struct UnaryExpression
: public Unary
<Iter
,Value
,FuncType
>
759 virtual Value
accept(FuncType
&f
)
765 template<class Iter
, class Value
, class FuncType
>
766 struct UnaryPlusExpression
: public UnaryExpression
<Iter
,Value
,FuncType
>
768 virtual Value
accept(FuncType
&f
)
774 template<class Iter
, class Value
, class FuncType
>
775 struct UnaryMinusExpression
: public UnaryExpression
<Iter
,Value
,FuncType
>
777 virtual Value
accept(FuncType
&f
)
783 template<class Iter
, class Value
, class FuncType
>
784 struct UnaryNotExpression
: public UnaryExpression
<Iter
,Value
,FuncType
>
786 virtual Value
accept(FuncType
&f
)
792 template<class Iter
, class Value
, class FuncType
>
793 struct MultiplicativeExpression
: public Binary
<Iter
,Value
,FuncType
>
795 virtual Value
accept(FuncType
&f
)
801 template<class Iter
, class Value
, class FuncType
>
802 struct MultiplyExpression
: public MultiplicativeExpression
<Iter
,Value
,FuncType
>
804 virtual Value
accept(FuncType
&f
)
810 template<class Iter
, class Value
, class FuncType
>
811 struct DivideExpression
: public MultiplicativeExpression
<Iter
,Value
,FuncType
>
813 virtual Value
accept(FuncType
&f
)
819 template<class Iter
, class Value
, class FuncType
>
820 struct ModuloExpression
: public MultiplicativeExpression
<Iter
,Value
,FuncType
>
822 virtual Value
accept(FuncType
&f
)
828 template<class Iter
, class Value
, class FuncType
>
829 struct AdditiveExpression
: public Binary
<Iter
,Value
,FuncType
>
831 virtual Value
accept(FuncType
&f
)
837 template<class Iter
, class Value
, class FuncType
>
838 struct AddExpression
: public AdditiveExpression
<Iter
,Value
,FuncType
>
840 virtual Value
accept(FuncType
&f
)
846 template<class Iter
, class Value
, class FuncType
>
847 struct SubtractExpression
: public AdditiveExpression
<Iter
,Value
,FuncType
>
849 virtual Value
accept(FuncType
&f
)
855 template<class Iter
, class Value
, class FuncType
>
856 struct RelationalExpression
: public Binary
<Iter
,Value
,FuncType
>
858 virtual Value
accept(FuncType
&f
)
864 template<class Iter
, class Value
, class FuncType
>
865 struct LessExpression
: public RelationalExpression
<Iter
,Value
,FuncType
>
867 virtual Value
accept(FuncType
&f
)
873 template<class Iter
, class Value
, class FuncType
>
874 struct LessEqualExpression
: public RelationalExpression
<Iter
,Value
,FuncType
>
876 virtual Value
accept(FuncType
&f
)
882 template<class Iter
, class Value
, class FuncType
>
883 struct GreaterExpression
: public RelationalExpression
<Iter
,Value
,FuncType
>
885 virtual Value
accept(FuncType
&f
)
891 template<class Iter
, class Value
, class FuncType
>
892 struct GreaterEqualExpression
: public RelationalExpression
<Iter
,Value
,FuncType
>
894 virtual Value
accept(FuncType
&f
)
900 template<class Iter
, class Value
, class FuncType
>
901 struct EqualityExpression
: public Binary
<Iter
,Value
,FuncType
>
903 virtual Value
accept(FuncType
&f
)
909 template<class Iter
, class Value
, class FuncType
>
910 struct EqualExpression
: public EqualityExpression
<Iter
,Value
,FuncType
>
912 virtual Value
accept(FuncType
&f
)
918 template<class Iter
, class Value
, class FuncType
>
919 struct NotEqualExpression
: public EqualityExpression
<Iter
,Value
,FuncType
>
921 virtual Value
accept(FuncType
&f
)
927 template<class Iter
, class Value
, class FuncType
>
928 struct LogicalAndExpression
: public Binary
<Iter
,Value
,FuncType
>
930 virtual Value
accept(FuncType
&f
)
936 template<class Iter
, class Value
, class FuncType
>
937 struct LogicalOrExpression
: public Binary
<Iter
,Value
,FuncType
>
939 virtual Value
accept(FuncType
&f
)
945 template<class Iter
, class Value
, class FuncType
>
946 struct AssignmentExpression
: public Binary
<Iter
,Value
,FuncType
>
948 virtual Value
accept(FuncType
&f
)
954 template<class Iter
, class Value
, class FuncType
>
955 struct GlobalAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
957 virtual Value
accept(FuncType
&f
)
963 template<class Iter
, class Value
, class FuncType
>
964 struct LocalAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
966 virtual Value
accept(FuncType
&f
)
972 template<class Iter
, class Value
, class FuncType
>
973 struct AddAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
975 virtual Value
accept(FuncType
&f
)
981 template<class Iter
, class Value
, class FuncType
>
982 struct SubAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
984 virtual Value
accept(FuncType
&f
)
990 template<class Iter
, class Value
, class FuncType
>
991 struct MulAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
993 virtual Value
accept(FuncType
&f
)
999 template<class Iter
, class Value
, class FuncType
>
1000 struct DivAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
1002 virtual Value
accept(FuncType
&f
)
1008 template<class Iter
, class Value
, class FuncType
>
1009 struct ModAssignmentExpression
: public AssignmentExpression
<Iter
,Value
,FuncType
>
1011 virtual Value
accept(FuncType
&f
)
1017 template<class Iter
, class Value
, class FuncType
>
1018 struct InitializerAssignmentExpression
: public Variadic
<Iter
,Value
,FuncType
>
1020 virtual Value
accept(FuncType
&f
)
1026 template<class Iter
, class Value
, class FuncType
>
1027 struct Block
: public Unary
<Iter
,Value
,FuncType
>
1029 virtual Value
accept(FuncType
&f
)
1035 template<class Iter
, class Value
, class FuncType
>
1036 struct TableInitializer
: public Variadic
<Iter
,Value
,FuncType
>
1038 virtual Value
accept(FuncType
&f
)
1044 template<class Iter
, class Value
, class FuncType
>
1045 struct ArrayInitializer
: public Variadic
<Iter
,Value
,FuncType
>
1047 virtual Value
accept(FuncType
&f
)
1053 template<class Iter
, class Value
, class FuncType
>
1054 struct IdentifierList
: public Variadic
<Iter
,Value
,FuncType
>
1056 virtual Value
accept(FuncType
&f
)
1062 template<class Iter
, class Value
, class FuncType
>
1063 struct Function
: public Binary
<Iter
,Value
,FuncType
>
1065 virtual Value
accept(FuncType
&f
)
1071 template<class Iter
, class Value
, class FuncType
>
1072 struct Tree
: public Unary
<Iter
,Value
,FuncType
>
1074 virtual Value
accept(FuncType
&f
)
1080 template<class Iter
, class Value
, class FuncType
>
1081 struct Root
: public Variadic
<Iter
,Value
,FuncType
>
1083 virtual Value
accept(FuncType
&f
)
1089 template<class Iter
, class Value
, class FuncType
>
1090 struct Type
: public Unary
<Iter
,Value
,FuncType
>
1092 virtual Value
accept(FuncType
&f
)
1098 template<class Iter
, class Value
, class FuncType
>
1099 struct Size
: public Unary
<Iter
,Value
,FuncType
>
1101 virtual Value
accept(FuncType
&f
)
1107 template<class Iter
, class Value
, class FuncType
>
1108 struct StatementList
: public Variadic
<Iter
,Value
,FuncType
>
1110 virtual Value
accept(FuncType
&f
)
1116 template<class Iter
, class Value
, class FuncType
>
1117 struct IfStatement
: public Variadic
<Iter
,Value
,FuncType
>
1119 virtual Value
accept(FuncType
&f
)
1125 template<class Iter
, class Value
, class FuncType
>
1126 struct WhileStatement
: public Binary
<Iter
,Value
,FuncType
>
1128 virtual Value
accept(FuncType
&f
)
1134 template<class Iter
, class Value
, class FuncType
>
1135 struct ForStatement
: public Variadic
<Iter
,Value
,FuncType
>
1137 virtual Value
accept(FuncType
&f
)
1143 template<class Iter
, class Value
, class FuncType
>
1144 struct ForEachStatement
: public Variadic
<Iter
,Value
,FuncType
>
1146 virtual Value
accept(FuncType
&f
)
1152 template<class Iter
, class Value
, class FuncType
>
1153 struct ReturnStatement
: public Variadic
<Iter
,Value
,FuncType
>
1155 virtual Value
accept(FuncType
&f
)
1161 template<class Iter
, class Value
, class FuncType
>
1162 struct ContinueStatement
: public Nullary
<Iter
,Value
,FuncType
>
1164 virtual Value
accept(FuncType
&f
)
1170 template<class Iter
, class Value
, class FuncType
>
1171 struct BreakStatement
: public Nullary
<Iter
,Value
,FuncType
>
1173 virtual Value
accept(FuncType
&f
)
1179 template<class Iter
, class Value
, class FuncType
>
1180 struct ExpressionStatement
: public Unary
<Iter
,Value
,FuncType
>
1182 virtual Value
accept(FuncType
&f
)
1188 template<class Iter
, class Value
, class FuncType
>
1189 struct BuiltinFunction
: public Nullary
<Iter
,Value
,FuncType
>
1191 virtual Value
accept(FuncType
&f
)
1195 std::function
<void(Interpreter
&, std::vector
<Value
>&)> function
;
1198 template<class Iter
, class Value
, class FuncType
>
1199 struct Parens
: public Unary
<Iter
,Value
,FuncType
>
1201 virtual Value
accept(FuncType
&f
)
1207 template<class Value
, class FuncType
>
1211 using Iter
= std::vector
<Token
<std::string::iterator
>>::iterator
;
1212 using return_t
= std::shared_ptr
<ASTNode
<Iter
,Value
,FuncType
>>;
1214 template<typename StrIter
>
1215 return_t
operator()(StrIter sbegin
, StrIter send
)
1217 auto source
= std::make_shared
<std::string
>(sbegin
, send
);
1218 auto tokens
= tokenize(source
->begin(), source
->end());
1219 this->begin
= tokens
->begin();
1220 this->end
= tokens
->end();
1222 accepted
= this->end
;
1223 auto node
= statement_list();
1224 node
->source
= source
;
1225 node
->tokens
= tokens
;
1227 node
->inject_dependencies();
1231 template<class StrIter
>
1232 return_t
parse_expression(StrIter sbegin
, StrIter send
)
1234 auto source
= std::make_shared
<std::string
>(sbegin
, send
);
1235 auto tokens
= tokenize(source
->begin(), source
->end());
1236 this->begin
= tokens
->begin();
1237 this->end
= tokens
->end();
1239 accepted
= this->end
;
1240 auto node
= expression();
1241 node
->source
= source
;
1242 node
->tokens
= tokens
;
1244 node
->inject_dependencies();
1259 auto lc
= line_column(begin
->begin
, last
->end
);
1260 throw std::runtime_error(
1261 std::to_string(lc
.first
) + ":" +
1262 std::to_string(lc
.second
) + ": unexpected end of input at");
1270 void rewind(Iter pos
)
1275 bool peek(TokenType t
, int amount
= 0)
1277 return (((i
+amount
) != end
) && (((i
+amount
)->type
) == t
));
1280 bool accept(TokenType t
)
1282 if(i
!= end
&& i
->type
== t
)
1299 bool expect(TokenType t
)
1307 i
= std::min(i
, end
-1);
1308 auto lc
= line_column(begin
->begin
, i
->begin
);
1309 throw std::runtime_error(
1310 std::to_string(lc
.first
) + ":" +
1311 std::to_string(lc
.second
) + ": unexpected token '" +
1312 std::string(i
->begin
, i
->end
) + "'"
1318 void expect(const std::string
&expected
)
1320 i
= std::min(i
, end
-1);
1321 auto lc
= line_column(begin
->begin
, i
->begin
);
1322 throw std::runtime_error(
1323 std::to_string(lc
.first
) + ":" +
1324 std::to_string(lc
.second
) + ": expected " +
1325 expected
+ " but got '" + std::string(i
->begin
, i
->end
) + "'"
1331 expect(Tok_Identifier
);
1332 auto node
= std::make_shared
<Variable
<Iter
,Value
,FuncType
>>();
1333 node
->begin
= accepted
;
1334 node
->end
= accepted
+1;
1335 node
->name
= Value(accepted
->begin
, accepted
->end
);
1336 return std::move(node
);
1339 return_t
field_name()
1341 expect(Tok_Identifier
);
1342 auto node
= std::make_shared
<FieldName
<Iter
,Value
,FuncType
>>();
1343 node
->begin
= accepted
;
1344 node
->end
= accepted
+1;
1345 node
->name
= Value(accepted
->begin
, accepted
->end
);
1346 return std::move(node
);
1349 return_t
identifier_list()
1351 auto node
= std::make_shared
<IdentifierList
<Iter
,Value
,FuncType
>>();
1353 expect(Tok_LeftParen
);
1354 while(!accept(Tok_RightParen
))
1356 node
->children
.push_back(field_name());
1357 if(!peek(Tok_RightParen
))
1362 node
->end
= accepted
+1;
1363 return std::move(node
);
1368 auto node
= std::make_shared
<Function
<Iter
,Value
,FuncType
>>();
1370 expect(Tok_Function
);
1371 node
->children
[0] = identifier_list();
1372 node
->children
[1] = statement();
1373 node
->end
= accepted
+1;
1374 return std::move(node
);
1377 return_t
parse_expression()
1379 auto node
= std::make_shared
<ParseExpression
<Iter
,Value
,FuncType
>>();
1381 expect(Tok_ParseExpression
);
1382 expect(Tok_LeftParen
);
1383 node
->children
[0] = expression();
1384 expect(Tok_RightParen
);
1385 node
->end
= accepted
+1;
1386 return std::move(node
);
1389 return_t
parse_statement()
1391 auto node
= std::make_shared
<ParseStatement
<Iter
,Value
,FuncType
>>();
1393 expect(Tok_ParseStatement
);
1394 expect(Tok_LeftParen
);
1395 node
->children
[0] = expression();
1396 expect(Tok_RightParen
);
1397 node
->end
= accepted
+1;
1398 return std::move(node
);
1403 auto node
= std::make_shared
<Tree
<Iter
,Value
,FuncType
>>();
1405 if(accept(Tok_Expression
))
1407 expect(Tok_LeftParen
);
1408 node
->children
[0] = expression();
1409 expect(Tok_RightParen
);
1413 expect(Tok_Statement
);
1414 expect(Tok_LeftParen
);
1415 node
->children
[0] = statement();
1416 expect(Tok_RightParen
);
1418 node
->end
= accepted
+1;
1419 return std::move(node
);
1424 auto node
= std::make_shared
<Root
<Iter
,Value
,FuncType
>>();
1427 if(accept(Tok_LeftParen
))
1429 node
->children
.push_back(expression());
1430 expect(Tok_RightParen
);
1432 node
->end
= accepted
+1;
1433 return std::move(node
);
1438 auto node
= std::make_shared
<Eval
<Iter
,Value
,FuncType
>>();
1441 expect(Tok_LeftParen
);
1442 node
->children
[0] = unary_expression();
1443 expect(Tok_RightParen
);
1444 node
->end
= accepted
+1;
1445 return std::move(node
);
1450 auto node
= std::make_shared
<Type
<Iter
,Value
,FuncType
>>();
1453 if(accept(Tok_LeftParen
))
1455 node
->children
[0] = expression();
1456 expect(Tok_RightParen
);
1458 node
->end
= accepted
+1;
1459 return std::move(node
);
1464 auto node
= std::make_shared
<Size
<Iter
,Value
,FuncType
>>();
1467 if(accept(Tok_LeftParen
))
1469 node
->children
[0] = expression();
1470 expect(Tok_RightParen
);
1472 node
->end
= accepted
+1;
1473 return std::move(node
);
1476 return_t
table_initializer()
1478 auto node
= std::make_shared
<TableInitializer
<Iter
,Value
,FuncType
>>();
1480 expect(Tok_LeftBrace
);
1481 while(!accept(Tok_RightBrace
))
1483 node
->children
.push_back(initializer_assignment_expression());
1484 if(!peek(Tok_RightBrace
))
1489 node
->end
= accepted
+1;
1490 return std::move(node
);
1493 return_t
array_initializer()
1495 auto node
= std::make_shared
<ArrayInitializer
<Iter
,Value
,FuncType
>>();
1497 expect(Tok_LeftBracket
);
1498 while(!accept(Tok_RightBracket
))
1500 node
->children
.push_back(logical_or_expression());
1501 if(!peek(Tok_RightBracket
))
1506 node
->end
= accepted
+1;
1507 return std::move(node
);
1510 return_t
primary_expression()
1512 if (peek(Tok_Identifier
) || peek(Tok_Global
) || peek(Tok_Local
))
1516 else if (accept(Tok_Number
))
1518 auto node
= std::make_shared
<Constant
<Iter
,Value
,FuncType
>>();
1519 node
->begin
= accepted
;
1520 node
->end
= accepted
+1;
1521 node
->value
= std::stod(std::string(accepted
->begin
, accepted
->end
));
1522 return std::move(node
);
1524 else if (accept(Tok_Nil
))
1526 auto node
= std::make_shared
<Nil
<Iter
,Value
,FuncType
>>();
1527 node
->begin
= accepted
;
1528 node
->end
= accepted
+1;
1529 return std::move(node
);
1531 else if (accept(Tok_String
))
1533 auto node
= std::make_shared
<String
<Iter
,Value
,FuncType
>>();
1534 node
->begin
= accepted
;
1535 node
->end
= accepted
+1;
1536 node
->value
= Value(unescape(std::string(accepted
->begin
+1, accepted
->end
-1)));
1537 return std::move(node
);
1539 else if (accept(Tok_LeftParen
))
1541 auto node
= std::make_shared
<Parens
<Iter
,Value
,FuncType
>>();
1542 node
->begin
= accepted
;
1543 node
->children
[0] = expression();
1544 expect(Tok_RightParen
);
1545 node
->end
= accepted
+1;
1546 return std::move(node
);
1548 else if (peek(Tok_LeftBrace
))
1550 return table_initializer();
1552 else if (peek(Tok_LeftBracket
))
1554 return array_initializer();
1556 else if (peek(Tok_Expression
) || peek(Tok_Statement
))
1560 else if (peek(Tok_Root
))
1564 else if (peek(Tok_Type
))
1568 else if (peek(Tok_Eval
))
1572 else if (peek(Tok_Size
))
1576 else if (peek(Tok_ParseExpression
))
1578 return parse_expression();
1580 else if (peek(Tok_ParseStatement
))
1582 return parse_statement();
1584 else if (peek(Tok_Function
))
1590 expect("primary expression");
1595 return_t
postfix_expression()
1598 return_t left
= primary_expression();
1601 if(accept(Tok_LeftBracket
))
1603 return_t right
= expression();
1604 expect(Tok_RightBracket
);
1605 if(peek(Tok_Equal
) || peek(Tok_PlusEqual
) || peek(Tok_DashEqual
) ||
1606 peek(Tok_StarEqual
) || peek(Tok_SlashEqual
) || peek(Tok_PercentEqual
))
1608 std::shared_ptr
<FieldAssignment
<Iter
,Value
,FuncType
>> node
;
1609 if(accept(Tok_Equal
))
1611 node
= std::make_shared
<FieldAssignment
<Iter
,Value
,FuncType
>>();
1613 else if(accept(Tok_PlusEqual
))
1615 node
= std::make_shared
<AddFieldAssignment
<Iter
,Value
,FuncType
>>();
1617 else if(accept(Tok_DashEqual
))
1619 node
= std::make_shared
<SubFieldAssignment
<Iter
,Value
,FuncType
>>();
1621 else if(accept(Tok_StarEqual
))
1623 node
= std::make_shared
<MulFieldAssignment
<Iter
,Value
,FuncType
>>();
1625 else if(accept(Tok_SlashEqual
))
1627 node
= std::make_shared
<DivFieldAssignment
<Iter
,Value
,FuncType
>>();
1629 else if(accept(Tok_PercentEqual
))
1631 node
= std::make_shared
<ModFieldAssignment
<Iter
,Value
,FuncType
>>();
1634 node
->children
[0] = std::move(left
);
1635 node
->children
[1] = std::move(right
);
1636 node
->children
[2] = expression();
1637 node
->end
= accepted
+1;
1638 left
= std::move(node
);
1642 auto node
= std::make_shared
<FieldAccess
<Iter
,Value
,FuncType
>>();
1644 node
->children
[0] = std::move(left
);
1645 node
->children
[1] = std::move(right
);
1646 node
->end
= accepted
+1;
1647 left
= std::move(node
);
1650 else if(accept(Tok_Dot
))
1652 return_t right
= field_name();
1653 if(peek(Tok_Equal
) || peek(Tok_PlusEqual
) || peek(Tok_DashEqual
) ||
1654 peek(Tok_StarEqual
) || peek(Tok_SlashEqual
) || peek(Tok_PercentEqual
))
1656 std::shared_ptr
<FieldAssignment
<Iter
,Value
,FuncType
>> node
;
1657 if(accept(Tok_Equal
))
1659 node
= std::make_shared
<FieldAssignment
<Iter
,Value
,FuncType
>>();
1661 else if(accept(Tok_PlusEqual
))
1663 node
= std::make_shared
<AddFieldAssignment
<Iter
,Value
,FuncType
>>();
1665 else if(accept(Tok_DashEqual
))
1667 node
= std::make_shared
<SubFieldAssignment
<Iter
,Value
,FuncType
>>();
1669 else if(accept(Tok_StarEqual
))
1671 node
= std::make_shared
<MulFieldAssignment
<Iter
,Value
,FuncType
>>();
1673 else if(accept(Tok_SlashEqual
))
1675 node
= std::make_shared
<DivFieldAssignment
<Iter
,Value
,FuncType
>>();
1677 else if(accept(Tok_PercentEqual
))
1679 node
= std::make_shared
<ModFieldAssignment
<Iter
,Value
,FuncType
>>();
1682 node
->children
[0] = std::move(left
);
1683 node
->children
[1] = std::move(right
);
1684 node
->children
[2] = expression();
1685 node
->end
= accepted
+1;
1686 left
= std::move(node
);
1688 else if(accept(Tok_LeftParen
))
1690 auto node
= std::make_shared
<MemberCall
<Iter
,Value
,FuncType
>>();
1692 node
->children
.push_back(std::move(left
));
1693 node
->children
.push_back(std::move(right
));
1694 while(!accept(Tok_RightParen
))
1696 node
->children
.push_back(expression());
1697 if(!peek(Tok_RightParen
))
1702 node
->end
= accepted
+1;
1703 left
= std::move(node
);
1707 auto node
= std::make_shared
<FieldAccess
<Iter
,Value
,FuncType
>>();
1709 node
->children
[0] = std::move(left
);
1710 node
->children
[1] = std::move(right
);
1711 node
->end
= accepted
+1;
1712 left
= std::move(node
);
1715 else if(accept(Tok_LeftParen
))
1717 auto node
= std::make_shared
<Call
<Iter
,Value
,FuncType
>>();
1719 node
->children
.push_back(std::move(left
));
1720 while(!accept(Tok_RightParen
))
1722 node
->children
.push_back(expression());
1723 if(!peek(Tok_RightParen
))
1728 node
->end
= accepted
+1;
1729 left
= std::move(node
);
1733 return std::move(left
);
1738 return_t
unary_expression()
1740 if(accept(Tok_Plus
) || accept(Tok_Dash
) || accept(Tok_Not
))
1742 using node_t
= std::shared_ptr
<UnaryExpression
<Iter
,Value
,FuncType
>>;
1744 switch(accepted
->type
)
1747 node
= node_t(new UnaryPlusExpression
<Iter
,Value
,FuncType
>);
1750 node
= node_t(new UnaryMinusExpression
<Iter
,Value
,FuncType
>);
1753 node
= node_t(new UnaryNotExpression
<Iter
,Value
,FuncType
>);
1758 node
->begin
= accepted
;
1759 node
->children
[0] = postfix_expression();
1760 node
->end
= accepted
+1;
1761 return std::move(node
);
1765 return postfix_expression();
1769 return_t
multiplicative_expression()
1772 return_t left
= unary_expression();
1773 if(accept(Tok_Star
) || accept(Tok_Slash
) || accept(Tok_Percent
))
1775 using node_t
= std::shared_ptr
<MultiplicativeExpression
<Iter
,Value
,FuncType
>>;
1777 switch(accepted
->type
)
1780 node
= node_t(new MultiplyExpression
<Iter
,Value
,FuncType
>);
1783 node
= node_t(new DivideExpression
<Iter
,Value
,FuncType
>);
1786 node
= node_t(new ModuloExpression
<Iter
,Value
,FuncType
>);
1792 node
->children
[0] = std::move(left
);
1793 node
->children
[1] = multiplicative_expression();
1794 node
->end
= accepted
+1;
1795 return std::move(node
);
1799 return std::move(left
);
1803 return_t
additive_expression()
1806 return_t left
= multiplicative_expression();
1807 if(accept(Tok_Plus
) || accept(Tok_Dash
))
1809 using node_t
= std::shared_ptr
<AdditiveExpression
<Iter
,Value
,FuncType
>>;
1811 switch(accepted
->type
)
1814 node
= node_t(new AddExpression
<Iter
,Value
,FuncType
>);
1817 node
= node_t(new SubtractExpression
<Iter
,Value
,FuncType
>);
1823 node
->children
[0] = std::move(left
);
1824 node
->children
[1] = additive_expression();
1825 node
->end
= accepted
+1;
1826 return std::move(node
);
1830 return std::move(left
);
1834 return_t
relational_expression()
1837 return_t left
= additive_expression();
1838 if(accept(Tok_Less
) || accept(Tok_LessEqual
) || accept(Tok_Greater
) || accept(Tok_GreaterEqual
))
1840 using node_t
= std::shared_ptr
<RelationalExpression
<Iter
,Value
,FuncType
>>;
1842 switch(accepted
->type
)
1845 node
= node_t(new LessExpression
<Iter
,Value
,FuncType
>);
1848 node
= node_t(new LessEqualExpression
<Iter
,Value
,FuncType
>);
1851 node
= node_t(new GreaterExpression
<Iter
,Value
,FuncType
>);
1853 case Tok_GreaterEqual
:
1854 node
= node_t(new GreaterEqualExpression
<Iter
,Value
,FuncType
>);
1860 node
->children
[0] = std::move(left
);
1861 node
->children
[1] = relational_expression();
1862 node
->end
= accepted
+1;
1863 return std::move(node
);
1867 return std::move(left
);
1871 return_t
equality_expression()
1874 return_t left
= relational_expression();
1875 if(accept(Tok_EqOp
) || accept(Tok_NotEqual
))
1877 using node_t
= std::shared_ptr
<EqualityExpression
<Iter
,Value
,FuncType
>>;
1879 switch(accepted
->type
)
1882 node
= node_t(new EqualExpression
<Iter
,Value
,FuncType
>);
1885 node
= node_t(new NotEqualExpression
<Iter
,Value
,FuncType
>);
1891 node
->children
[0] = std::move(left
);
1892 node
->children
[1] = relational_expression();
1893 node
->end
= accepted
+1;
1894 return std::move(node
);
1898 return std::move(left
);
1902 return_t
logical_and_expression()
1905 return_t left
= equality_expression();
1906 if(accept(Tok_LogicalAnd
))
1908 auto node
= std::make_shared
<LogicalAndExpression
<Iter
,Value
,FuncType
>>();
1910 node
->children
[0] = std::move(left
);
1911 node
->children
[1] = equality_expression();
1912 node
->end
= accepted
+1;
1913 return std::move(node
);
1917 return std::move(left
);
1921 return_t
logical_or_expression()
1924 return_t left
= logical_and_expression();
1925 if(accept(Tok_LogicalOr
))
1927 auto node
= std::make_shared
<LogicalOrExpression
<Iter
,Value
,FuncType
>>();
1929 node
->children
[0] = std::move(left
);
1930 node
->children
[1] = logical_and_expression();
1931 node
->end
= accepted
+1;
1932 return std::move(node
);
1936 return std::move(left
);
1940 return_t
assignment_expression()
1943 if(peek(Tok_Global
) || peek(Tok_Local
))
1945 bool global
= accept(Tok_Global
);
1946 bool local
= accept(Tok_Local
);
1947 return_t left
= field_name();
1948 if(accept(Tok_Equal
))
1950 std::shared_ptr
<AssignmentExpression
<Iter
,Value
,FuncType
>> node
;
1953 std::make_shared
<GlobalAssignmentExpression
<Iter
,Value
,FuncType
>>()
1956 std::make_shared
<LocalAssignmentExpression
<Iter
,Value
,FuncType
>>()
1958 std::make_shared
<AssignmentExpression
<Iter
,Value
,FuncType
>>();
1960 node
->children
[0] = std::move(left
);
1961 node
->children
[1] = assignment_expression();
1962 node
->end
= accepted
+1;
1963 return std::move(node
);
1966 else if(peek(Tok_Identifier
))
1968 return_t left
= field_name();
1969 std::shared_ptr
<AssignmentExpression
<Iter
,Value
,FuncType
>> node
;
1970 if(accept(Tok_Equal
))
1972 node
= std::make_shared
<AssignmentExpression
<Iter
,Value
,FuncType
>>();
1974 else if(accept(Tok_PlusEqual
))
1976 node
= std::make_shared
<AddAssignmentExpression
<Iter
,Value
,FuncType
>>();
1978 else if(accept(Tok_DashEqual
))
1980 node
= std::make_shared
<SubAssignmentExpression
<Iter
,Value
,FuncType
>>();
1982 else if(accept(Tok_StarEqual
))
1984 node
= std::make_shared
<MulAssignmentExpression
<Iter
,Value
,FuncType
>>();
1986 else if(accept(Tok_SlashEqual
))
1988 node
= std::make_shared
<DivAssignmentExpression
<Iter
,Value
,FuncType
>>();
1990 else if(accept(Tok_PercentEqual
))
1992 node
= std::make_shared
<ModAssignmentExpression
<Iter
,Value
,FuncType
>>();
1997 return logical_or_expression();
2000 node
->children
[0] = std::move(left
);
2001 node
->children
[1] = assignment_expression();
2002 node
->end
= accepted
+1;
2003 return std::move(node
);
2006 return logical_or_expression();
2009 return_t
initializer_assignment_expression()
2011 auto node
= std::make_shared
<InitializerAssignmentExpression
<Iter
,Value
,FuncType
>>();
2013 node
->children
.push_back(logical_or_expression());
2014 if(accept(Tok_Equal
))
2016 node
->children
.push_back(logical_or_expression());
2018 node
->end
= accepted
+1;
2019 return std::move(node
);
2022 return_t
expression()
2024 return assignment_expression();
2029 auto node
= std::make_shared
<Block
<Iter
,Value
,FuncType
>>();
2031 expect(Tok_LeftBrace
);
2032 node
->children
[0] = statement_list();
2033 expect(Tok_RightBrace
);
2034 node
->end
= accepted
+1;
2035 return std::move(node
);
2038 return_t
if_statement()
2040 auto node
= std::make_shared
<IfStatement
<Iter
,Value
,FuncType
>>();
2043 expect(Tok_LeftParen
);
2044 node
->children
.push_back(expression());
2045 expect(Tok_RightParen
);
2046 node
->children
.push_back(statement());
2047 if(accept(Tok_Else
))
2049 node
->children
.push_back(statement());
2051 node
->end
= accepted
+1;
2052 return std::move(node
);
2055 return_t
while_statement()
2057 auto node
= std::make_shared
<WhileStatement
<Iter
,Value
,FuncType
>>();
2060 expect(Tok_LeftParen
);
2061 node
->children
[0] = expression();
2062 expect(Tok_RightParen
);
2063 node
->children
[1] = statement();
2064 node
->end
= accepted
+1;
2065 return std::move(node
);
2070 auto node
= std::make_shared
<Nop
<Iter
,Value
,FuncType
>>();
2073 return std::move(node
);
2076 return_t
for_statement()
2080 expect(Tok_LeftParen
);
2081 if(peek(Tok_Identifier
) && (peek(Tok_Comma
, 1) || peek(Tok_Colon
, 1)))
2083 auto node
= std::make_shared
<ForEachStatement
<Iter
,Value
,FuncType
>>();
2085 node
->children
.push_back(field_name());
2086 if(accept(Tok_Comma
))
2088 node
->children
.push_back(field_name());
2091 node
->children
.push_back(expression());
2092 expect(Tok_RightParen
);
2093 node
->children
.push_back(statement());
2094 node
->end
= accepted
+1;
2095 return std::move(node
);
2099 auto node
= std::make_shared
<ForStatement
<Iter
,Value
,FuncType
>>();
2101 if(accept(Tok_Semicolon
))
2103 node
->children
.push_back(nop());
2107 node
->children
.push_back(expression());
2108 expect(Tok_Semicolon
);
2110 if(accept(Tok_Semicolon
))
2112 node
->children
.push_back(nop());
2116 node
->children
.push_back(expression());
2117 expect(Tok_Semicolon
);
2119 if(accept(Tok_RightParen
))
2121 node
->children
.push_back(nop());
2125 node
->children
.push_back(expression());
2126 expect(Tok_RightParen
);
2128 node
->children
.push_back(statement());
2129 node
->end
= accepted
+1;
2130 return std::move(node
);
2134 return_t
break_statement()
2136 auto node
= std::make_shared
<BreakStatement
<Iter
,Value
,FuncType
>>();
2139 accept(Tok_Semicolon
);
2140 node
->end
= accepted
+1;
2141 return std::move(node
);
2144 return_t
continue_statement()
2146 auto node
= std::make_shared
<ContinueStatement
<Iter
,Value
,FuncType
>>();
2148 expect(Tok_Continue
);
2149 accept(Tok_Semicolon
);
2150 node
->end
= accepted
+1;
2151 return std::move(node
);
2154 return_t
return_statement()
2156 auto node
= std::make_shared
<ReturnStatement
<Iter
,Value
,FuncType
>>();
2159 if(accept(Tok_Semicolon
))
2161 node
->end
= accepted
+1;
2162 return std::move(node
);
2164 node
->children
.push_back(expression());
2165 accept(Tok_Semicolon
);
2166 node
->end
= accepted
+1;
2167 return std::move(node
);
2170 return_t
statement()
2172 if(peek(Tok_LeftBrace
))
2176 else if (peek(Tok_If
))
2178 return if_statement();
2180 else if (peek(Tok_While
))
2182 return while_statement();
2184 else if (peek(Tok_For
))
2186 return for_statement();
2188 else if (peek(Tok_Return
))
2190 return return_statement();
2192 else if (peek(Tok_Continue
))
2194 return continue_statement();
2196 else if (peek(Tok_Break
))
2198 return break_statement();
2202 auto node
= std::make_shared
<ExpressionStatement
<Iter
,Value
,FuncType
>>();
2204 node
->children
[0] = expression();
2205 accept(Tok_Semicolon
);
2206 node
->end
= accepted
+1;
2207 return std::move(node
);
2210 return_t
statement_list()
2212 auto node
= std::make_shared
<StatementList
<Iter
,Value
,FuncType
>>();
2214 while(!end_of_input() && !peek(Tok_RightBrace
))
2216 node
->children
.push_back(statement());
2218 node
->end
= accepted
+1;
2219 return std::move(node
);
2224 template<typename T
> class Table
;
2226 template<typename T
> class Array
;
2228 template<typename Iter
, typename FuncType
>
2231 using NumberType
= double;
2232 using StringContainer
= std::string
;
2233 using StringIterator
= StringContainer::iterator
;
2234 using NumberContainer
= NumberType
;
2235 using PointerContainer
= std::shared_ptr
<Internal::ASTNode
<Iter
, BaseValue
, FuncType
>>;
2236 using FunctionContainer
= std::shared_ptr
<Internal::Function
<Iter
,BaseValue
,FuncType
>>;
2237 using TreeContainer
= PointerContainer
;
2238 using ArrayContainer
= std::shared_ptr
<Array
<BaseValue
>>;
2239 using TableContainer
= std::shared_ptr
<Table
<BaseValue
>>;
2241 struct ProtoStringContainer
2243 StringIterator begin
;
2247 ProtoStringContainer(StringIterator begin_
, StringIterator end_
):
2250 hash(std::hash
<StringContainer
>()(StringContainer(begin_
, end_
)))
2255 struct UserPtrContainer
2259 UserPtrContainer(void* ptr
): realptr(ptr
)
2278 Nil
, Number
, String
, ProtoString
,
2279 Tree
, Function
, Table
, Array
,
2280 Return
, Break
, Continue
, UserPtr
,
2287 NumberContainer m_number
;
2288 PointerContainer m_pointer
;
2289 UserPtrContainer m_userptr
;
2290 TableContainer m_table
;
2291 ArrayContainer m_array
;
2292 StringContainer m_strval
;
2293 ProtoStringContainer m_protostr
;
2299 if(m_type
== Type::ProtoString
)
2301 return Type::String
;
2303 else if((m_type
>= Type::Return
) && (m_type
<= Type::Continue
))
2310 const char* typeName() const
2320 case Type::ProtoString
:
2321 return "protostring";
2324 case Type::Function
:
2334 case Type::Continue
:
2344 Type
internal_type() const
2349 const NumberType
& number() const
2354 NumberType
& number()
2359 const PointerContainer
& pointer() const
2364 PointerContainer
& pointer()
2371 return m_userptr
.realptr
;
2374 const void* userptr() const
2376 return m_userptr
.realptr
;
2379 const FunctionContainer
function() const
2381 return std::static_pointer_cast
<Internal::Function
<Iter
,BaseValue
,FuncType
>>(m_pointer
);
2384 FunctionContainer
function()
2386 return std::static_pointer_cast
<Internal::Function
<Iter
,BaseValue
,FuncType
>>(m_pointer
);
2389 const TreeContainer
tree() const
2394 TreeContainer
tree()
2399 const TableContainer
& table() const
2404 TableContainer
& table()
2409 const StringContainer
string() const
2411 if(m_type
== Type::ProtoString
)
2413 return std::string(m_protostr
.begin
, m_protostr
.end
);
2418 StringContainer
& string()
2424 const ArrayContainer
& array() const
2429 ArrayContainer
& array()
2436 if(m_type
== Type::ProtoString
)
2438 StringIterator tmpbegin
= m_protostr
.begin
;
2439 StringIterator tmpend
= m_protostr
.end
;
2440 m_type
= Type::String
;
2441 new (&m_strval
) StringContainer(tmpbegin
, tmpend
);
2443 else if((m_type
>= Type::Return
) && (m_type
<= Type::Continue
))
2449 BaseValue() : m_type(Type::Nil
), m_number(0)
2453 BaseValue(const TagReturn
&) : m_type(Type::Return
), m_number(0)
2457 BaseValue(const TagBreak
&) : m_type(Type::Break
), m_number(0)
2461 BaseValue(const TagContinue
&) : m_type(Type::Continue
), m_number(0)
2465 BaseValue(NumberType value
) : m_type(Type::Number
), m_number(value
)
2469 BaseValue(Type t
, const PointerContainer
&ptr
) : m_type(t
), m_pointer(ptr
)
2473 BaseValue(UserPtrContainer ptr
): m_type(Type::UserPtr
), m_userptr(ptr
.realptr
)
2475 //fprintf(stderr, "BaseValue/userpointer: ptr=%p\n", ptr.realptr);
2478 BaseValue(const TableContainer
&ptr
) : m_type(Type::Table
), m_table(ptr
)
2482 BaseValue(const ArrayContainer
&ptr
) : m_type(Type::Array
), m_array(ptr
)
2486 BaseValue(const StringContainer
&value
) : m_type(Type::String
), m_strval(value
)
2490 BaseValue(StringIterator begin
, StringIterator end
) : m_type(Type::ProtoString
), m_protostr(begin
, end
)
2494 BaseValue(const BaseValue
&that
) : m_type(that
.m_type
)
2500 case Type::Continue
:
2502 new (&m_number
) NumberContainer(0);
2505 new (&m_number
) NumberContainer(that
.m_number
);
2508 new (&m_strval
) StringContainer(that
.m_strval
);
2510 case Type::ProtoString
:
2511 new (&m_protostr
) ProtoStringContainer(that
.m_protostr
);
2514 case Type::Function
:
2515 new (&m_pointer
) PointerContainer(that
.m_pointer
);
2518 new (&m_table
) TableContainer(that
.m_table
);
2521 new (&m_array
) ArrayContainer(that
.m_array
);
2525 fprintf(stderr
, "BaseValue(BaseValue&): this.userptr=%p, that.userptr=%p\n",
2526 m_userptr
.realptr
, that
.m_userptr
.realptr
2529 new (&m_userptr
) UserPtrContainer(that
.m_userptr
);
2536 BaseValue
& operator=(const BaseValue
&that
)
2541 m_strval
.~StringContainer();
2544 case Type::Function
:
2545 m_pointer
.~PointerContainer();
2548 m_table
.~TableContainer();
2551 m_array
.~ArrayContainer();
2556 m_type
= that
.m_type
;
2561 case Type::Continue
:
2563 new (&m_number
) NumberContainer(0);
2566 new (&m_number
) NumberContainer(that
.m_number
);
2569 new (&m_strval
) StringContainer(that
.m_strval
);
2571 case Type::ProtoString
:
2572 new (&m_protostr
) ProtoStringContainer(that
.m_protostr
);
2575 case Type::Function
:
2576 new (&m_pointer
) PointerContainer(that
.m_pointer
);
2580 fprintf(stderr
, "BaseValue::operator=(BaseValue&): this.userptr=%p, that.userptr=%p\n",
2581 m_userptr
.realptr
, that
.m_userptr
.realptr
2584 new(&m_userptr
) UserPtrContainer(that
.m_userptr
);
2587 new (&m_table
) TableContainer(that
.m_table
);
2590 new (&m_array
) ArrayContainer(that
.m_array
);
2603 m_strval
.~StringContainer();
2606 case Type::Function
:
2607 m_pointer
.~PointerContainer();
2610 m_table
.~TableContainer();
2613 m_array
.~ArrayContainer();
2620 bool operator==(const BaseValue
&that
) const
2622 if(type() != that
.type())
2630 case Type::Continue
:
2633 return number() == that
.number();
2635 case Type::Function
:
2636 return pointer() == that
.pointer();
2638 return table() == that
.table();
2640 return array() == that
.array();
2642 return (userptr() == that
.userptr());
2644 if(that
.m_type
== Type::String
)
2646 return m_strval
== that
.m_strval
;
2650 if(m_strval
.end()-m_strval
.begin() != that
.m_protostr
.end
- that
.m_protostr
.begin
)
2654 return std::equal(m_strval
.begin(), m_strval
.end(), that
.m_protostr
.begin
);
2656 case Type::ProtoString
:
2657 if(that
.m_type
== Type::String
)
2659 if(m_protostr
.end
- m_protostr
.begin
!= that
.m_strval
.end() - that
.m_strval
.begin())
2663 return std::equal(m_protostr
.begin
, m_protostr
.end
, that
.m_strval
.begin());
2667 if(m_protostr
.end
- m_protostr
.begin
!= that
.m_protostr
.end
- that
.m_protostr
.begin
)
2671 return std::equal(m_protostr
.begin
, m_protostr
.end
, that
.m_protostr
.begin
);
2678 friend struct std::hash
<BaseValue
<Iter
, FuncType
>>;
2683 template<typename Iter
, typename FuncType
>
2684 struct hash
<BaseValue
<Iter
, FuncType
>>
2687 using T
= BaseValue
<Iter
, FuncType
>;
2688 std::size_t operator()(BaseValue
<Iter
, FuncType
> const& x
) const
2693 case T::Type::Number
:
2694 return std::hash
<typename
T::NumberContainer
>()(x
.number());
2695 case T::Type::String
:
2696 return std::hash
<typename
T::StringContainer
>()(x
.string());
2697 case T::Type::ProtoString
:
2698 return x
.m_protostr
.hash
;
2699 case T::Type::UserPtr
:
2700 return std::hash
<const void*>()(x
.userptr());;
2702 case T::Type::Function
:
2703 return std::hash
<typename
T::PointerContainer
>()(x
.pointer());
2704 case T::Type::Table
:
2705 return std::hash
<typename
T::TableContainer
>()(x
.table());
2706 case T::Type::Array
:
2707 return std::hash
<typename
T::ArrayContainer
>()(x
.array());
2715 template<class Iter
, class FuncType
>
2716 std::ostream
& operator<<(std::ostream
&out
, const BaseValue
<Iter
,FuncType
> &v
)
2718 using T
= BaseValue
<Iter
,FuncType
>;
2722 return out
<< "nil";
2723 case T::Type::Number
:
2724 return out
<< v
.number();
2725 case T::Type::String
:
2726 return out
<< v
.string();
2728 return out
<< "tree@" << v
.tree();
2729 case T::Type::Function
:
2730 return out
<< "function@" << v
.function();
2731 case T::Type::Table
:
2732 return out
<< "table@" << v
.table();
2733 case T::Type::Array
:
2734 return out
<< "array@" << v
.array();
2735 case T::Type::UserPtr
:
2736 return out
<< "userpointer@" << v
.userptr();
2742 template<typename Type
> class Table
2745 using Iterator
= typename
std::unordered_map
<Type
, Type
>::iterator
;
2748 std::unordered_map
<Type
, Type
> m_data
;
2752 void set(const Type
& key
, const Type
& value
)
2754 if(key
.type() == Type::Type::Nil
)
2758 if(value
.type() == Type::Type::Nil
)
2762 m_data
[key
] = value
;
2765 Type
get(const Type
& key
)
2767 auto iter
= m_data
.find(key
);
2768 if(iter
== m_data
.end())
2774 return iter
->second
;
2778 void append(const Table
& that
)
2780 m_data
.insert(that
.m_data
.begin(), that
.m_data
.end());
2785 return m_data
.size();
2790 return m_data
.begin();
2795 return m_data
.end();
2800 template<typename Type
>
2804 using Iterator
= typename
std::vector
<Type
>::iterator
;
2807 std::vector
<Type
> m_data
;
2811 void set(const Type
& key
, const Type
& value
)
2813 int index
= key
.number();
2814 m_data
[index
] = value
;
2817 Type
get(const Type
& key
)
2819 int index
= key
.number();
2820 return m_data
[index
];
2823 void append(const Array
& that
)
2825 m_data
.insert(m_data
.end(), that
.m_data
.begin(), that
.m_data
.end());
2828 void add(const Type
& value
)
2830 m_data
.push_back(value
);
2835 return m_data
.size();
2840 return m_data
.begin();
2845 return m_data
.end();
2852 using Iter
= std::vector
<Internal::Token
<std::string::iterator
>>::iterator
;
2853 using FuncType
= Interpreter
;
2854 using Value
= BaseValue
<Iter
, FuncType
>;
2855 using Environment
= std::shared_ptr
<Table
<Value
>>;
2856 using EnvStack
= std::vector
<Environment
>;
2857 using FunStack
= std::vector
<size_t>;
2858 using ParserInstance
= Internal::Parser
<Value
, Interpreter
>;
2860 struct FunctionScope
2862 Interpreter
* m_self
;
2864 FunctionScope(Interpreter
* self
): m_self(self
)
2866 m_self
->beginFunctionScope();
2871 m_self
->endFunctionScope();
2877 Interpreter
& m_self
;
2879 LocalScope(Interpreter
& self
): m_self(self
)
2881 m_self
.beginLocalScope();
2886 m_self
.endLocalScope();
2891 ParserInstance m_parser
;
2892 std::vector
<Value
> m_function_stack
;
2893 std::vector
<std::unordered_map
<std::string
, Value
>> m_scopes
;
2894 EnvStack m_envstack
;
2895 FunStack m_funstack
;
2898 Value
operator()(Internal::Variable
<Iter
,Value
,FuncType
> &node
);
2900 Value
operator()(Internal::FieldName
<Iter
,Value
,FuncType
> &node
);
2902 Value
operator()(Internal::Constant
<Iter
,Value
,FuncType
> &node
);
2904 Value
operator()(Internal::Nop
<Iter
,Value
,FuncType
> &);
2906 Value
operator()(Internal::Nil
<Iter
,Value
,FuncType
> &);
2908 Value
operator()(Internal::String
<Iter
,Value
,FuncType
> &node
);
2910 Value
operator()(Internal::Parens
<Iter
,Value
,FuncType
> &node
);
2912 Value
operator()(Internal::ExpressionStatement
<Iter
,Value
,FuncType
> &node
);
2914 Value
operator()(Internal::Type
<Iter
,Value
,FuncType
> &node
);
2916 Value
operator()(Internal::Size
<Iter
,Value
,FuncType
> &node
);
2918 Value
operator()(Internal::FieldAccess
<Iter
,Value
,FuncType
> &node
);
2920 Value
operator()(Internal::FieldAssignment
<Iter
,Value
,FuncType
> &node
);
2922 Value
operator()(Internal::AddFieldAssignment
<Iter
,Value
,FuncType
> &node
);
2924 Value
operator()(Internal::SubFieldAssignment
<Iter
,Value
,FuncType
> &node
);
2926 Value
operator()(Internal::MulFieldAssignment
<Iter
,Value
,FuncType
> &node
);
2928 Value
operator()(Internal::DivFieldAssignment
<Iter
,Value
,FuncType
> &node
);
2930 Value
operator()(Internal::ModFieldAssignment
<Iter
,Value
,FuncType
> &node
);
2932 Value
operator()(Internal::UnaryPlusExpression
<Iter
,Value
,FuncType
> &node
);
2934 Value
operator()(Internal::UnaryMinusExpression
<Iter
,Value
,FuncType
> &node
);
2936 Value
operator()(Internal::UnaryNotExpression
<Iter
,Value
,FuncType
> &node
);
2938 Value
operator()(Internal::MultiplyExpression
<Iter
,Value
,FuncType
> &node
);
2940 Value
operator()(Internal::DivideExpression
<Iter
,Value
,FuncType
> &node
);
2942 Value
operator()(Internal::ModuloExpression
<Iter
,Value
,FuncType
> &node
);
2944 Value
operator()(Internal::AddExpression
<Iter
,Value
,FuncType
> &node
);
2946 Value
operator()(Internal::SubtractExpression
<Iter
,Value
,FuncType
> &node
);
2948 Value
operator()(Internal::LessExpression
<Iter
,Value
,FuncType
> &node
);
2950 Value
operator()(Internal::LessEqualExpression
<Iter
,Value
,FuncType
> &node
);
2952 Value
operator()(Internal::GreaterExpression
<Iter
,Value
,FuncType
> &node
);
2954 Value
operator()(Internal::GreaterEqualExpression
<Iter
,Value
,FuncType
> &node
);
2956 Value
operator()(Internal::EqualExpression
<Iter
,Value
,FuncType
> &node
);
2958 Value
operator()(Internal::NotEqualExpression
<Iter
,Value
,FuncType
> &node
);
2960 Value
operator()(Internal::LogicalAndExpression
<Iter
,Value
,FuncType
> &node
);
2962 Value
operator()(Internal::LogicalOrExpression
<Iter
,Value
,FuncType
> &node
);
2964 Value
operator()(Internal::AssignmentExpression
<Iter
,Value
,FuncType
> &node
);
2966 Value
operator()(Internal::GlobalAssignmentExpression
<Iter
,Value
,FuncType
> &node
);
2968 Value
operator()(Internal::LocalAssignmentExpression
<Iter
,Value
,FuncType
> &node
);
2970 Value
operator()(Internal::AddAssignmentExpression
<Iter
,Value
,FuncType
> &node
);
2972 Value
operator()(Internal::SubAssignmentExpression
<Iter
,Value
,FuncType
> &node
);
2974 Value
operator()(Internal::MulAssignmentExpression
<Iter
,Value
,FuncType
> &node
);
2976 Value
operator()(Internal::DivAssignmentExpression
<Iter
,Value
,FuncType
> &node
);
2978 Value
operator()(Internal::ModAssignmentExpression
<Iter
,Value
,FuncType
> &node
);
2980 Value
operator()(Internal::Block
<Iter
,Value
,FuncType
> &node
);
2982 Value
operator()(Internal::TableInitializer
<Iter
,Value
,FuncType
> &node
);
2984 Value
operator()(Internal::ArrayInitializer
<Iter
,Value
,FuncType
> &node
);
2986 Value
operator()(Internal::Function
<Iter
,Value
,FuncType
> &node
);
2988 Value
operator()(Internal::ReturnStatement
<Iter
,Value
,FuncType
> &node
);
2990 Value
operator()(Internal::IdentifierList
<Iter
,Value
,FuncType
> &node
);
2992 Value
operator()(Internal::BuiltinFunction
<Iter
,Value
,FuncType
> &node
);
2994 Value
operator()(Internal::Call
<Iter
,Value
,FuncType
> &node
);
2996 Value
operator()(Internal::MemberCall
<Iter
,Value
,FuncType
> &node
);
2998 Value
operator()(Internal::EvalStatement
<Iter
,Value
,FuncType
> &node
);
3000 Value
operator()(Internal::Eval
<Iter
,Value
,FuncType
> &node
);
3002 Value
operator()(Internal::Tree
<Iter
,Value
,FuncType
> &node
);
3004 Value
operator()(Internal::Root
<Iter
,Value
,FuncType
> &node
);
3006 Value
operator()(Internal::ParseStatement
<Iter
,Value
,FuncType
> &node
);
3008 Value
operator()(Internal::ParseExpression
<Iter
,Value
,FuncType
> &node
);
3010 Value
operator()(Internal::StatementList
<Iter
,Value
,FuncType
> &node
);
3012 Value
operator()(Internal::IfStatement
<Iter
,Value
,FuncType
> &node
);
3014 Value
operator()(Internal::WhileStatement
<Iter
,Value
,FuncType
> &node
);
3016 Value
operator()(Internal::ForStatement
<Iter
,Value
,FuncType
> &node
);
3018 Value
operator()(Internal::ForEachStatement
<Iter
,Value
,FuncType
> &node
);
3020 Value
operator()(Internal::ContinueStatement
<Iter
,Value
,FuncType
> &);
3022 Value
operator()(Internal::BreakStatement
<Iter
,Value
,FuncType
> &);
3024 template<class Type
>
3025 Value
operator()(Type
& node
)
3027 error("unimplemented ast node.", node
);
3031 Value
makeString(const std::string
& src
)
3036 std::shared_ptr
<Array
<Value
>> makeArray()
3038 return std::make_shared
<Array
<Value
>>();
3041 std::shared_ptr
<Table
<Value
>> makeTable()
3043 return std::make_shared
<Table
<Value
>>();
3046 Value
makeFunction(std::function
<void(Interpreter
&, std::vector
<Value
>&)> fun
)
3048 auto node
= std::make_shared
<Internal::Function
<Iter
,Value
,FuncType
>>();
3049 node
->begin
= node
->end
;
3050 auto nop
= std::make_shared
<Internal::Nop
<Iter
,Value
,FuncType
>>();
3051 nop
->begin
= nop
->end
;
3052 node
->children
[0] = nop
;
3053 auto builtin
= std::make_shared
<Internal::BuiltinFunction
<Iter
,Value
,FuncType
>>();
3054 builtin
->begin
= builtin
->end
;
3055 builtin
->function
= fun
;
3056 node
->children
[1] = builtin
;
3057 return Value(Value::Type::Function
, node
);
3060 template<typename Type
>
3061 Value
makeUserPointer(Type
* ptr
)
3063 return Value(Value::UserPtrContainer((void*)ptr
));
3066 void beginLocalScope()
3068 m_envstack
.push_back(std::make_shared
<Table
<Value
>>());
3071 void endLocalScope()
3073 m_envstack
.pop_back();
3076 void beginFunctionScope()
3078 m_funstack
.push_back(m_envstack
.size());
3079 m_envstack
.push_back(std::make_shared
<Table
<Value
>>());
3082 void endFunctionScope()
3084 m_envstack
.pop_back();
3085 m_funstack
.pop_back();
3088 Value
get(Value key
)
3090 int top
= m_envstack
.size()-1;
3091 int bottom
= m_funstack
.back();
3092 for(int i
= top
; i
>=bottom
; --i
)
3094 Value value
= m_envstack
[i
]->get(key
);
3095 if(value
.type() != Value::Type::Nil
)
3100 return m_envstack
[0]->get(key
);
3103 void set(Value key
, Value value
)
3105 m_envstack
[m_funstack
.back()]->set(key
, value
);
3108 void setLocal(Value key
, Value value
)
3110 m_envstack
.back()->set(key
, value
);
3113 void setGlobal(Value key
, Value value
)
3115 m_envstack
[0]->set(key
, value
);
3119 void error(const std::string text
, T
&node
)
3121 auto lc
= Internal::line_column(node
.source
->begin(), node
.begin
->begin
);
3122 auto from
= node
.begin
;
3123 auto to
= node
.end
-1;
3124 throw std::runtime_error(
3125 std::to_string(lc
.first
)+":"+std::to_string(lc
.second
)+
3126 ": error: " + text
+ " in: '" + std::string(from
->begin
, to
->end
) + "'");
3129 void addFunction(const std::string
&name
, std::function
<void(Interpreter
&, std::vector
<Value
>&)> fun
)
3131 setGlobal(Value(name
), makeFunction(fun
));
3136 beginFunctionScope();
3144 Value
run(const std::string
&source
)
3146 auto root
= m_parser(source
.begin(), source
.end());
3147 Value result
= root
->accept(*this);