6.万花筒:扩展语言:用户定义的运算符

    • 第6章简介
    • 用户定义的运算符:理念
    • 用户定义的二元运算符
    • 用户定义的一元运算符
    • 造轮子
    • 完整的代码清单

    6.2 用户定义的运算符:理念

    我们将添加到Kaleidoscope的“运算符重载”比C ++等语言更通用。在C ++中,您只能重新定义现有运算符:您无法以编程方式更改语法,引入新运算符,更改优先级等。在本章中,我们将此功能添加到Kaleidoscope中,这将使用户完善支持的运算符集。

    在这样的教程中进入用户定义的运算符的目的是展示使用手写解析器的强大功能和灵活性。到目前为止,我们实现的解析器使用递归下降语法的大多数部分和运算符优先级解析表达式。详细信息请参见第2章。通过使用运算符优先级解析,很容易让程序员在语法中引入新的运算符:语法在JIT运行时可以动态扩展。

    我们将添加的两个特定功能是可编程的一元运算符(现在,Kaleidoscope完全没有一元运算符)以及二元运算符。一个例子是:

    许多语言都希望能够在语言本身中实现其标准运行时库。在Kaleidoscope中,我们可以在库中实现该语言的重要部分!

    我们将这些功能的实现分解为两部分:实现对用户定义的二元运算符的支持以及添加一元运算符。

    使用我们当前的框架,添加对用户定义的二元运算符的支持非常简单。我们首先添加对一元/二元关键字的支持:

    1. ...
    2. // operators
    3. tok_binary = -11,
    4. tok_unary = -12
    5. };
    6. ...
    7. static int gettok() {
    8. ...
    9. if (IdentifierStr == "for")
    10. return tok_for;
    11. if (IdentifierStr == "in")
    12. return tok_in;
    13. if (IdentifierStr == "binary")
    14. return tok_binary;
    15. if (IdentifierStr == "unary")
    16. return tok_unary;
    17. return tok_identifier;

    这只是增加了对一元和二元关键字的词法分析器支持,就像我们在前几章中所做的那样。关于我们当前AST的一个好处是,我们通过使用它们的ASCII代码作为操作码来表示具有完全泛化的二元运算符。对于我们的扩展运算符,我们将使用相同的表示,因此我们不需要任何新的AST或解析器支持。

    另一方面,我们必须能够在“def binary |”中表示这些新运算符的定义 5“功能定义的一部分。到目前为止,在我们的语法中,函数定义的“名称”被解析为“原型”生成并进入 PrototypeASTAST节点。为了将我们新的用户定义的运算符表示为原型,我们必须PrototypeAST像这样扩展AST节点:

    1. /// PrototypeAST - This class represents the "prototype" for a function,
    2. /// which captures its argument names as well as if it is an operator.
    3. class PrototypeAST {
    4. std::string Name;
    5. std::vector<std::string> Args;
    6. bool IsOperator;
    7. unsigned Precedence; // Precedence if a binary op.
    8. public:
    9. PrototypeAST(const std::string &name, std::vector<std::string> Args,
    10. bool IsOperator = false, unsigned Prec = 0)
    11. : Name(name), Args(std::move(Args)), IsOperator(IsOperator),
    12. Precedence(Prec) {}
    13. Function *codegen();
    14. const std::string &getName() const { return Name; }
    15. bool isUnaryOp() const { return IsOperator && Args.size() == 1; }
    16. bool isBinaryOp() const { return IsOperator && Args.size() == 2; }
    17. char getOperatorName() const {
    18. assert(isUnaryOp() || isBinaryOp());
    19. return Name[Name.size() - 1];
    20. }
    21. unsigned getBinaryPrecedence() const { return Precedence; }
    22. };

    基本上,除了知道原型的名称之外,我们现在还要跟踪它是否是运算符,如果是,运算符的优先级是什么。优先级仅用于二元运算符(如下所示,它不适用于一元运算符)。既然我们有办法为用户定义的运算符表示原型,我们需要解析它:

    1. /// prototype
    2. /// ::= id '(' id* ')'
    3. /// ::= binary LETTER number? (id, id)
    4. static std::unique_ptr<PrototypeAST> ParsePrototype() {
    5. std::string FnName;
    6. unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
    7. unsigned BinaryPrecedence = 30;
    8. switch (CurTok) {
    9. default:
    10. return LogErrorP("Expected function name in prototype");
    11. case tok_identifier:
    12. FnName = IdentifierStr;
    13. Kind = 0;
    14. getNextToken();
    15. break;
    16. case tok_binary:
    17. getNextToken();
    18. if (!isascii(CurTok))
    19. return LogErrorP("Expected binary operator");
    20. FnName = "binary";
    21. FnName += (char)CurTok;
    22. Kind = 2;
    23. getNextToken();
    24. // Read the precedence if present.
    25. if (CurTok == tok_number) {
    26. if (NumVal < 1 || NumVal > 100)
    27. return LogErrorP("Invalid precedence: must be 1..100");
    28. BinaryPrecedence = (unsigned)NumVal;
    29. getNextToken();
    30. }
    31. break;
    32. }
    33. if (CurTok != '(')
    34. return LogErrorP("Expected '(' in prototype");
    35. std::vector<std::string> ArgNames;
    36. while (getNextToken() == tok_identifier)
    37. ArgNames.push_back(IdentifierStr);
    38. if (CurTok != ')')
    39. return LogErrorP("Expected ')' in prototype");
    40. // success.
    41. getNextToken(); // eat ')'.
    42. // Verify right number of names for operator.
    43. if (Kind && ArgNames.size() != Kind)
    44. return LogErrorP("Invalid number of operands for operator");
    45. return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames), Kind != 0,
    46. BinaryPrecedence);
    47. }

    下一个有趣的事情是codegen支持这些二元运算符。鉴于我们当前的结构,这是我们现有二元运算符节点的默认情况的简单添加:

    1. Value *BinaryExprAST::codegen() {
    2. Value *L = LHS->codegen();
    3. Value *R = RHS->codegen();
    4. if (!L || !R)
    5. return nullptr;
    6. switch (Op) {
    7. case '+':
    8. return Builder.CreateFAdd(L, R, "addtmp");
    9. case '-':
    10. return Builder.CreateFSub(L, R, "subtmp");
    11. case '*':
    12. return Builder.CreateFMul(L, R, "multmp");
    13. case '<':
    14. L = Builder.CreateFCmpULT(L, R, "cmptmp");
    15. // Convert bool 0/1 to double 0.0 or 1.0
    16. return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext),
    17. "booltmp");
    18. default:
    19. break;
    20. }
    21. // If it wasn't a builtin binary operator, it must be a user defined one. Emit
    22. // a call to it.
    23. Function *F = getFunction(std::string("binary") + Op);
    24. assert(F && "binary operator not found!");
    25. Value *Ops[2] = { L, R };
    26. }
    1. Function *FunctionAST::codegen() {
    2. // Transfer ownership of the prototype to the FunctionProtos map, but keep a
    3. // reference to it for use below.
    4. auto &P = *Proto;
    5. FunctionProtos[Proto->getName()] = std::move(Proto);
    6. Function *TheFunction = getFunction(P.getName());
    7. if (!TheFunction)
    8. // If this is an operator, install it.
    9. if (P.isBinaryOp())
    10. BinopPrecedence[P.getOperatorName()] = P.getBinaryPrecedence();
    11. // Create a new basic block to start insertion into.
    12. BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
    13. ...

    基本上,在对代码进行代码化之前,如果它是用户定义的运算符,我们会在优先级表中注册它。这允许二元运算符解析我们已有的逻辑来处理它。由于我们正在研究一个完全通用的运算符优先级解析器,所以我们需要做的就是“扩展语法”。

    现在我们有了有用的用户定义二元运算符。这构建了我们为其他运营商构建的先前框架。添加一元运算符更具挑战性,因为我们还没有任何框架 - 让我们看看它需要什么。

    6.4 用户定义的一元运算符

    由于我们目前不支持Kaleidoscope语言中的一元运算符,因此我们需要添加所有内容以支持它们。上面,我们为词法分析器添加了对“一元”关键字的简单支持。除此之外,我们需要一个AST节点:

    到目前为止,这个AST节点非常简单明了。它直接镜像二元运算符AST节点,除了它只有一个子节点。有了这个,我们需要添加解析逻辑。解析一元运算符非常简单:我们将添加一个新函数来执行此操作:

    1. /// unary
    2. /// ::= primary
    3. /// ::= '!' unary
    4. static std::unique_ptr<ExprAST> ParseUnary() {
    5. // If the current token is not an operator, it must be a primary expr.
    6. if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
    7. return ParsePrimary();
    8. // If this is a unary operator, read it.
    9. int Opc = CurTok;
    10. getNextToken();
    11. if (auto Operand = ParseUnary())
    12. return llvm::make_unique<UnaryExprAST>(Opc, std::move(Operand));
    13. return nullptr;
    14. }

    我们添加的语法非常简单。如果我们在解析主运算符时看到一元运算符,我们将运算符作为前缀并将剩余的块解析为另一个一元运算符。这允许我们处理多个一元运算符(例如“!! x”)。请注意,一元运算符不能像二元运算符那样具有模糊的解析,因此不需要优先级信息。

    这个函数的问题是我们需要从某个地方调用ParseUnary。为此,我们改变ParsePrimary的先前调用者来调用ParseUnary:

    1. /// binoprhs
    2. /// ::= ('+' unary)*
    3. static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,
    4. std::unique_ptr<ExprAST> LHS) {
    5. ...
    6. // Parse the unary expression after the binary operator.
    7. auto RHS = ParseUnary();
    8. if (!RHS)
    9. return nullptr;
    10. ...
    11. }
    12. /// expression
    13. /// ::= unary binoprhs
    14. ///
    15. static std::unique_ptr<ExprAST> ParseExpression() {
    16. auto LHS = ParseUnary();
    17. if (!LHS)
    18. return nullptr;
    19. return ParseBinOpRHS(0, std::move(LHS));
    20. }

    通过这两个简单的更改,我们现在能够解析一元运算符并为它们构建AST。接下来,我们需要为原型添加解析器支持,以解析一元运算符原型。我们将上面的二元运算符代码扩展为:

    1. /// prototype
    2. /// ::= id '(' id* ')'
    3. /// ::= binary LETTER number? (id, id)
    4. /// ::= unary LETTER (id)
    5. static std::unique_ptr<PrototypeAST> ParsePrototype() {
    6. std::string FnName;
    7. unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
    8. unsigned BinaryPrecedence = 30;
    9. switch (CurTok) {
    10. default:
    11. return LogErrorP("Expected function name in prototype");
    12. case tok_identifier:
    13. FnName = IdentifierStr;
    14. Kind = 0;
    15. getNextToken();
    16. break;
    17. case tok_unary:
    18. getNextToken();
    19. if (!isascii(CurTok))
    20. return LogErrorP("Expected unary operator");
    21. FnName = "unary";
    22. FnName += (char)CurTok;
    23. Kind = 1;
    24. getNextToken();
    25. break;
    26. case tok_binary:
    27. ...

    与二元运算符一样,我们将一元运算符命名为包含运算符字符的名称。这有助于我们在代码生成时间。说到,我们需要添加的最后一部分是对一元运算符的codegen支持。它看起来像这样:

    1. Value *UnaryExprAST::codegen() {
    2. Value *OperandV = Operand->codegen();
    3. if (!OperandV)
    4. return nullptr;
    5. Function *F = getFunction(std::string("unary") + Opcode);
    6. if (!F)
    7. return LogErrorV("Unknown unary operator");
    8. return Builder.CreateCall(F, OperandV, "unop");
    9. }
    10. 此代码与二进制运算符的代码类似,但更简单。它更简单,主要是因为它不需要处理任何预定义的运算符。
    1. ready> extern printd(x);
    2. Read extern:
    3. declare double @printd(double)
    4. ready> def binary : 1 (x y) 0; # Low-precedence operator that ignores operands.
    5. ...
    6. ready> printd(123) : printd(456) : printd(789);
    7. 123.000000
    8. 456.000000
    9. 789.000000
    10. Evaluated to 0.000000

    我们还可以定义一堆其他“原始”操作,例如:

    鉴于之前的if / then / else支持,我们还可以为I / O定义有趣的函数。例如,下面打印出一个字符,其“密度”反映传入的值:值越低,字符越密集:

    1. ready> extern putchard(char);
    2. ...
    3. ready> def printdensity(d)
    4. if d > 8 then
    5. putchard(32) # ' '
    6. else if d > 4 then
    7. putchard(46) # '.'
    8. else if d > 2 then
    9. putchard(43) # '+'
    10. else
    11. putchard(42); # '*'
    12. ...
    13. ready> printdensity(1): printdensity(2): printdensity(3):
    14. printdensity(4): printdensity(5): printdensity(9):
    15. putchard(10);
    16. **++.
    17. Evaluated to 0.000000

    基于这些简单的原始操作,我们可以开始定义更有趣的事情。例如,这是一个小函数,它确定复平面中某个函数发散所需的迭代次数:

    1. # Determine whether the specific location diverges.
    2. # Solve for z = z^2 + c in the complex plane.
    3. def mandelconverger(real imag iters creal cimag)
    4. if iters > 255 | (real*real + imag*imag > 4) then
    5. iters
    6. else
    7. mandelconverger(real*real - imag*imag + creal,
    8. 2*real*imag + cimag,
    9. iters+1, creal, cimag);
    10. # Return the number of iterations required for the iteration to escape
    11. def mandelconverge(real imag)
    12. mandelconverger(real, imag, 0, real, imag);

    这个“ ”功能是一个美丽的小动物,是计算Mandelbrot Set的基础。我们的 函数返回复杂轨道逃逸所需的迭代次数,饱和到255.这本身并不是一个非常有用的函数,但是如果你在二维平面上绘制它的值,你可以看到Mandelbrot集合。鉴于我们仅限于在这里使用putchard,我们惊人的图形输出是有限的,但我们可以使用上面的密度绘图仪鞭打一些东西:z = z2 + cmandelconverge

    1. # Compute and plot the mandelbrot set with the specified 2 dimensional range
    2. # info.
    3. def mandelhelp(xmin xmax xstep ymin ymax ystep)
    4. for y = ymin, y < ymax, ystep in (
    5. (for x = xmin, x < xmax, xstep in
    6. : putchard(10)
    7. )
    8. # mandel - This is a convenient helper function for plotting the mandelbrot set
    9. def mandel(realstart imagstart realmag imagmag)
    10. mandelhelp(realstart, realstart+realmag*78, realmag,
    11. imagstart, imagstart+imagmag*40, imagmag);

    鉴于此,我们可以尝试绘制出mandelbrot集!让我们尝试一下:

    1. ready> mandel(-2.3, -1.3, 0.05, 0.07);
    2. *******************************+++++++++++*************************************
    3. *************************+++++++++++++++++++++++*******************************
    4. **********************+++++++++++++++++++++++++++++****************************
    5. *******************+++++++++++++++++++++.. ...++++++++*************************
    6. *****************++++++++++++++++++++++.... ...+++++++++***********************
    7. ***************+++++++++++++++++++++++..... ...+++++++++*********************
    8. **************+++++++++++++++++++++++.... ....+++++++++********************
    9. *************++++++++++++++++++++++...... .....++++++++*******************
    10. ************+++++++++++++++++++++....... .......+++++++******************
    11. ***********+++++++++++++++++++.... ... .+++++++*****************
    12. **********+++++++++++++++++....... .+++++++****************
    13. *********++++++++++++++........... ...+++++++***************
    14. ********++++++++++++............ ...++++++++**************
    15. ********++++++++++... .......... .++++++++**************
    16. *******+++++++++..... .+++++++++*************
    17. *******++++++++...... ..+++++++++*************
    18. *******++++++....... ..+++++++++*************
    19. *******+++++...... ..+++++++++*************
    20. *******.... .... ...+++++++++*************
    21. *******.... . ...+++++++++*************
    22. *******+++++...... ...+++++++++*************
    23. *******++++++....... ..+++++++++*************
    24. *******++++++++...... .+++++++++*************
    25. *******+++++++++..... ..+++++++++*************
    26. ********++++++++++... .......... .++++++++**************
    27. ********++++++++++++............ ...++++++++**************
    28. *********++++++++++++++.......... ...+++++++***************
    29. **********++++++++++++++++........ .+++++++****************
    30. **********++++++++++++++++++++.... ... ..+++++++****************
    31. ***********++++++++++++++++++++++....... .......++++++++*****************
    32. ************+++++++++++++++++++++++...... ......++++++++******************
    33. **************+++++++++++++++++++++++.... ....++++++++********************
    34. ***************+++++++++++++++++++++++..... ...+++++++++*********************
    35. *****************++++++++++++++++++++++.... ...++++++++***********************
    36. *******************+++++++++++++++++++++......++++++++*************************
    37. *********************++++++++++++++++++++++.++++++++***************************
    38. *************************+++++++++++++++++++++++*******************************
    39. ******************************+++++++++++++************************************
    40. *******************************************************************************
    41. *******************************************************************************
    42. *******************************************************************************
    43. Evaluated to 0.000000
    44. ready> mandel(-2, -1, 0.02, 0.04);
    45. **************************+++++++++++++++++++++++++++++++++++++++++++++++++++++
    46. ***********************++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    47. *********************+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
    48. *******************+++++++++++++++++++++++++++++++++++++++++++++++++++++++++...
    49. *****************+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.....
    50. ***************++++++++++++++++++++++++++++++++++++++++++++++++++++++++........
    51. **************++++++++++++++++++++++++++++++++++++++++++++++++++++++...........
    52. ************+++++++++++++++++++++++++++++++++++++++++++++++++++++..............
    53. ***********++++++++++++++++++++++++++++++++++++++++++++++++++........ .
    54. **********++++++++++++++++++++++++++++++++++++++++++++++.............
    55. ********+++++++++++++++++++++++++++++++++++++++++++..................
    56. *******+++++++++++++++++++++++++++++++++++++++.......................
    57. ******+++++++++++++++++++++++++++++++++++...........................
    58. *****++++++++++++++++++++++++++++++++............................
    59. *****++++++++++++++++++++++++++++...............................
    60. ****++++++++++++++++++++++++++...... .........................
    61. ***++++++++++++++++++++++++......... ...... ...........
    62. ***++++++++++++++++++++++............
    63. **+++++++++++++++++++++..............
    64. **+++++++++++++++++++................
    65. *++++++++++++++++++.................
    66. *++++++++++++++++............ ...
    67. *++++++++++++++..............
    68. *+++....++++................
    69. *.......... ...........
    70. *
    71. *.......... ...........
    72. *+++....++++................
    73. *++++++++++++++..............
    74. *++++++++++++++++............ ...
    75. *++++++++++++++++++.................
    76. **+++++++++++++++++++................
    77. **+++++++++++++++++++++..............
    78. ***++++++++++++++++++++++............
    79. ***++++++++++++++++++++++++......... ...... ...........
    80. ****++++++++++++++++++++++++++...... .........................
    81. *****++++++++++++++++++++++++++++...............................
    82. *****++++++++++++++++++++++++++++++++............................
    83. ******+++++++++++++++++++++++++++++++++++...........................
    84. *******+++++++++++++++++++++++++++++++++++++++.......................
    85. ********+++++++++++++++++++++++++++++++++++++++++++..................
    86. Evaluated to 0.000000
    87. ready> mandel(-0.9, -1.4, 0.02, 0.03);
    88. *******************************************************************************
    89. *******************************************************************************
    90. *******************************************************************************
    91. **********+++++++++++++++++++++************************************************
    92. *+++++++++++++++++++++++++++++++++++++++***************************************
    93. +++++++++++++++++++++++++++++++++++++++++++++**********************************
    94. ++++++++++++++++++++++++++++++++++++++++++++++++++*****************************
    95. ++++++++++++++++++++++++++++++++++++++++++++++++++++++*************************
    96. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++**********************
    97. +++++++++++++++++++++++++++++++++.........++++++++++++++++++*******************
    98. +++++++++++++++++++++++++++++++.... ......+++++++++++++++++++****************
    99. +++++++++++++++++++++++++++++....... ........+++++++++++++++++++**************
    100. ++++++++++++++++++++++++++++........ ........++++++++++++++++++++************
    101. +++++++++++++++++++++++++++......... .. ...+++++++++++++++++++++**********
    102. ++++++++++++++++++++++++++........... ....++++++++++++++++++++++********
    103. ++++++++++++++++++++++++............. .......++++++++++++++++++++++******
    104. +++++++++++++++++++++++............. ........+++++++++++++++++++++++****
    105. ++++++++++++++++++++++........... ..........++++++++++++++++++++++***
    106. ++++++++++++++++++++........... .........++++++++++++++++++++++*
    107. ++++++++++++++++++............ ...........++++++++++++++++++++
    108. ++++++++++++++++............... .............++++++++++++++++++
    109. ++++++++++++++................. ...............++++++++++++++++
    110. ++++++++++++.................. .................++++++++++++++
    111. +++++++++.................. .................+++++++++++++
    112. ++++++........ . ......... ..++++++++++++
    113. ++............ ...... ....++++++++++
    114. .............. ...++++++++++
    115. .............. ....+++++++++
    116. .............. .....++++++++
    117. ............. ......++++++++
    118. ........... .......++++++++
    119. ......... ........+++++++
    120. ......... ........+++++++
    121. ......... ....+++++++
    122. ........ ...+++++++
    123. ....... ...+++++++
    124. ....+++++++
    125. .....+++++++
    126. ....+++++++
    127. ....+++++++
    128. ....+++++++
    129. Evaluated to 0.000000
    130. ready> ^D

    此时,您可能已经开始意识到Kaleidoscope是一种真实而强大的语言。它可能不是自相似的:),但它可以用于绘制事物!

    有了这个,我们总结了本教程的“添加用户定义的运算符”一章。我们已经成功地扩充了我们的语言,添加了扩展库中语言的能力,并且我们已经展示了如何在Kaleidoscope中构建一个简单但有趣的最终用户应用程序。此时,Kaleidoscope可以构建各种功能性的应用程序,并且可以调用具有副作用的函数,但它实际上无法定义和变异变量本身。

    引人注目的是,变量变异是某些语言的一个重要特征,如何在不必向前端添加“SSA构造”阶段的情况下添加对可变变量的支持并不是很明显。在下一章中,我们将介绍如何在不在前端构建SSA的情况下添加变量变异。

    6.6 完整的代码清单

    以下是我们运行示例的完整代码清单,增强了对用户定义运算符的支持。要构建此示例,请使用:

    1. # Compile
    2. clang++ -g toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core mcjit native` -O3 -o toy

    在某些平台上,您需要在链接时指定-rdynamic或-Wl,-export-dynamic。这可确保主可执行文件中定义的符号导出到动态链接器,因此可在运行时进行符号解析。如果将支持代码编译到共享库中,则不需要这样做,但这样做会导致Windows出现问题。

    这是代码: