statements.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. Statements
  2. ==========
  3. For-loop
  4. ________
  5. For-loops are completely supported in Solang. ``continue`` and ``break`` statements are also supported.
  6. The syntax for ``for`` is quite different from other commonly known programming languages. After the ``for`` keyword,
  7. the lexer expects a yul block delimited with curly brackets that initializes variables for the loop. This block can have
  8. as many statements as needed and is always executed.
  9. After the initialization block, there should be an Yul expression that contains the loop stopping condition. Then comes
  10. the update block, which contains all the statements executed after the main body block, but before the condition check.
  11. The body block is a set of instructions executed during each iteration.
  12. .. code-block:: yul
  13. {
  14. function foo()
  15. {
  16. // Simple for loop
  17. for {let i := 0} lt(i, 10) {i := add(i, 10)} {
  18. let p := funcCall(i, 10)
  19. if eq(p, 5) {
  20. continue
  21. }
  22. if eq(p, 90) {
  23. break
  24. }
  25. }
  26. let a := 0
  27. // More complex loops are also possible
  28. for {
  29. let i := 0
  30. let j := 3
  31. i := add(j, i)
  32. } or(lt(i, 10), lt(j, 5)) {
  33. i := add(i, 1)
  34. j := add(j, 3)
  35. } {
  36. a := add(a, mul(i, j))
  37. }
  38. }
  39. }
  40. If-block
  41. ________
  42. If-block conditions in Yul cannot have an `else`. They act only as a branch if the condition is true
  43. and are totally supported in Solang.
  44. .. code-block:: yul
  45. {
  46. if eq(5, 4) {
  47. funcCall(4, 3)
  48. } // There cannot be an 'else' here
  49. }
  50. Switch
  51. _______
  52. Switch statements are not yet supported in Solang. If there is urgent need to support them,
  53. please, file a GitHub issue in the repository.
  54. Blocks
  55. ______
  56. There can be blocks of code within Yul, defined by curly brackets. They have their own scope and any variable
  57. declared inside a block cannot be accessed outside it. Statements inside a block can access outside variables, though.
  58. .. code-block:: yul
  59. {
  60. function foo() -> ret {
  61. let g := 0
  62. { // This is a code block
  63. let r := 7
  64. ret := mul(g, r)
  65. }
  66. }
  67. }
  68. Variable declaration
  69. ____________________
  70. Variables can be declared in Yul using the `let` keyword. Multiple variables can be declared at the same line
  71. if there is no initializer or the initializer is a function that returns multiple values.
  72. The default type for variables in Yul is ``u256``. If you want to declare a variable with another type, use the colon.
  73. Note that if the variable type and the type of the right hand side of the assignment do not match, there will be an implicit
  74. type conversion to the correct type.
  75. .. code-block:: yul
  76. {
  77. let a, b, c
  78. let d := funCall()
  79. let e : u64 := funcCall()
  80. let g, h, i := multipleReturns()
  81. let j : u32, k : u8 := manyReturns()
  82. }
  83. Assignments
  84. ___________
  85. Variables can be assignment using the ``:=`` operator. If the types do not match,
  86. the compiler performs an implicit conversion, so that the right hand side type matches that of the variable.
  87. Multiple variables can be assigned in a single line if the right hand side is a function call that returns multiple
  88. values.
  89. .. code-block:: yul
  90. {
  91. a := 6
  92. c, d := multipleReturns()
  93. }
  94. Function calls
  95. ______________
  96. Function calls in Yul are identified by the use of parenthesis after an identifier. Standalone function
  97. calls must not return anything. Functions that have multiple returns can only appear in an assignment or definition
  98. of multiple variables.
  99. .. code-block:: yul
  100. {
  101. noReturns()
  102. a := singleReturn()
  103. // multipleReturns() cannot be inside 'add'
  104. let g := add(a, singleReturn())
  105. f, d, e := multipleReturns()
  106. }