compiler.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #pragma once
  2. #include "codeobject.h"
  3. #include "common.h"
  4. #include "expr.h"
  5. #include "obj.h"
  6. namespace pkpy{
  7. class Compiler;
  8. typedef void (Compiler::*PrattCallback)();
  9. struct PrattRule{
  10. PrattCallback prefix;
  11. PrattCallback infix;
  12. Precedence precedence;
  13. };
  14. class Compiler {
  15. PK_ALWAYS_PASS_BY_POINTER(Compiler)
  16. inline static PrattRule rules[kTokenCount];
  17. Lexer lexer;
  18. stack_no_copy<CodeEmitContext> contexts;
  19. VM* vm;
  20. bool unknown_global_scope; // for eval/exec() call
  21. // for parsing token stream
  22. int i = 0;
  23. std::vector<Token> tokens;
  24. const Token& prev() const{ return tokens.at(i-1); }
  25. const Token& curr() const{ return tokens.at(i); }
  26. const Token& next() const{ return tokens.at(i+1); }
  27. const Token& err() const{
  28. if(i >= tokens.size()) return prev();
  29. return curr();
  30. }
  31. void advance(int delta=1) { i += delta; }
  32. CodeEmitContext* ctx() { return &contexts.top(); }
  33. CompileMode mode() const{ return lexer.src->mode; }
  34. NameScope name_scope() const;
  35. CodeObject_ push_global_context();
  36. FuncDecl_ push_f_context(Str name);
  37. void pop_context();
  38. static void init_pratt_rules();
  39. bool match(TokenIndex expected);
  40. void consume(TokenIndex expected);
  41. bool match_newlines_repl();
  42. bool match_newlines(bool repl_throw=false);
  43. bool match_end_stmt();
  44. void consume_end_stmt();
  45. /*************************************************/
  46. void EXPR();
  47. void EXPR_TUPLE(bool allow_slice=false);
  48. Expr_ EXPR_VARS(); // special case for `for loop` and `comp`
  49. template <typename T, typename... Args>
  50. unique_ptr_128<T> make_expr(Args&&... args) {
  51. void* p = pool128_alloc(sizeof(T));
  52. unique_ptr_128<T> expr(new (p) T(std::forward<Args>(args)...));
  53. expr->line = prev().line;
  54. return expr;
  55. }
  56. void consume_comp(unique_ptr_128<CompExpr> ce, Expr_ expr);
  57. void exprLiteral();
  58. void exprLong();
  59. void exprImag();
  60. void exprBytes();
  61. void exprFString();
  62. void exprLambda();
  63. void exprOr();
  64. void exprAnd();
  65. void exprTernary();
  66. void exprBinaryOp();
  67. void exprNot();
  68. void exprUnaryOp();
  69. void exprGroup();
  70. void exprList();
  71. void exprMap();
  72. void exprCall();
  73. void exprName();
  74. void exprAttrib();
  75. void exprSlice0();
  76. void exprSlice1();
  77. void exprSubscr();
  78. void exprLiteral0();
  79. void compile_block_body(void (Compiler::*callback)()=nullptr);
  80. void compile_normal_import();
  81. void compile_from_import();
  82. bool is_expression(bool allow_slice=false);
  83. void parse_expression(int precedence, bool allow_slice=false);
  84. void compile_if_stmt();
  85. void compile_while_loop();
  86. void compile_for_loop();
  87. void compile_try_except();
  88. void compile_decorated();
  89. bool try_compile_assignment();
  90. void compile_stmt();
  91. void consume_type_hints();
  92. void _add_decorators(const Expr_vector& decorators);
  93. void compile_class(const Expr_vector& decorators={});
  94. void _compile_f_args(FuncDecl_ decl, bool enable_type_hints);
  95. void compile_function(const Expr_vector& decorators={});
  96. PyObject* to_object(const TokenValue& value);
  97. PyObject* read_literal();
  98. void SyntaxError(Str msg){ lexer.throw_err("SyntaxError", msg, err().line, err().start); }
  99. void SyntaxError(){ lexer.throw_err("SyntaxError", "invalid syntax", err().line, err().start); }
  100. void IndentationError(Str msg){ lexer.throw_err("IndentationError", msg, err().line, err().start); }
  101. public:
  102. Compiler(VM* vm, std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
  103. CodeObject_ compile();
  104. };
  105. } // namespace pkpy