syntax.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Gravity: Syntax</title>
  7. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css" />
  8. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  9. <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,700,800">
  10. <link rel="stylesheet" href="scripts/highlight/styles/github-gist.css">
  11. <link rel="stylesheet" href="stylesheets/styles.css">
  12. </head>
  13. <body>
  14. <!-- BEGIN NAVIGATION BAR -->
  15. <nav class="navbar navbar-default navbar-fixed-top">
  16. <div class="container">
  17. <div class="navbar-header">
  18. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
  19. <span class="sr-only">Toggle navigation</span>
  20. <span class="icon-bar"></span>
  21. <span class="icon-bar"></span>
  22. <span class="icon-bar"></span>
  23. </button>
  24. <a class="navbar-brand" href="http://gravity-lang.org"><img src="images/logo.png" width="32px" height="36px"></a>
  25. </div>
  26. <div id="navbar" class="navbar-collapse collapse">
  27. <ul class="nav navbar-nav navbar-right">
  28. <li class="active"><a href="index.html">Gravity</a></li>
  29. <li><a href="internals/index.html">Internals</a></li>
  30. <li><a href="https://github.com/marcobambini/gravity">GitHub</a></li>
  31. </ul>
  32. </div><!--/.nav-collapse -->
  33. </div>
  34. </nav>
  35. <!-- END NAVIGATION BAR -->
  36. <div class="container body-container">
  37. <div class="main-content">
  38. <div class="row">
  39. <!-- BEGIN SIDEBAR -->
  40. <div class="col-sm-3 border-right section-left">
  41. <div saveheight="1" class="sidebar-nav">
  42. <h4>INTRODUCTION</h4>
  43. <ul>
  44. <li><a href="index.html"><span>Introduction</span></a></li>
  45. <li><a href="getting-started.html"><span>Getting Started</span></a></li>
  46. </ul>
  47. <h4>LANGUAGE GUIDE</h4>
  48. <ul>
  49. <li><a href="syntax.html" class="active"><span>Syntax</span></a></li>
  50. <li><a href="operators.html"><span>Operators</span></a></li>
  51. <li><a href="types.html"><span>Types</span></a></li>
  52. <li><a href="lists.html"><span>Lists</span></a></li>
  53. <li><a href="maps.html"><span>Maps</span></a></li>
  54. <li><a href="enum.html"><span>Enum</span></a></li>
  55. <li><a href="functions.html"><span>Functions</span></a></li>
  56. <li><a href="closures.html"><span>Closures</span></a></li>
  57. <li><a href="classes.html"><span>Classes</span></a></li>
  58. <li><a href="control-flow.html"><span>Control Flow</span></a></li>
  59. <li><a href="loops.html"><span>Loops</span></a></li>
  60. <li><a href="fibers.html"><span>Fibers</span></a></li>
  61. </ul>
  62. <h4>ADVANCED</h4>
  63. <ul>
  64. <li><a href="api.html"><span>Embedding API</span></a></li>
  65. <li><a href="system.html"><span>System class</span></a></li>
  66. <li><a href="math.html"><span>Math class</span></a></li>
  67. <li><a href="unit-test.html"><span>Unit test</span></a></li>
  68. <li><a href="contributing.html"><span>Contributing</span></a></li>
  69. </ul>
  70. </div>
  71. </div>
  72. <!-- END SIDEBAR -->
  73. <div class="col-sm-9 border-left section-right">
  74. <h1 class="section-header">Syntax</h1><hr>
  75. <p class="section-content">
  76. <strong>Gravity</strong> syntax is designed to be familiar to people coming from C-like languages like Javascript, Swift, C++, C# and many more. We started working on this new language a year before Apple announced Swift and we were happily surprised to discovered how similar both syntax appear. Semicolon separator <strong>;</strong> is optional.
  77. <br><br>
  78. How a Gravity program looks:</p>
  79. <pre><code class="swift">
  80. class Rectangle {
  81. // instance variables
  82. var width;
  83. var height;
  84. // instance method
  85. func area() {
  86. return width*height;
  87. }
  88. // constructor
  89. func init(w, h) {
  90. width = w;
  91. height = h;
  92. }
  93. }
  94. func main() {
  95. // initialize a new Rectangle object
  96. var r = Rectangle(20, 10);
  97. // return value is 20*10 = 200
  98. return r.area();
  99. }
  100. </code></pre>
  101. <!-- COMMENTS -->
  102. <h4 class="section-h4">Comments</h4>
  103. <p>Gravity supports both line comments:</p>
  104. <pre><code class="swift">
  105. // This is a line comment
  106. </code></pre>
  107. <p>and block comments:</p>
  108. <pre><code class="swift">
  109. /*
  110. This
  111. is
  112. a
  113. multi-line
  114. comment
  115. */</code></pre>
  116. <p>While Gravity uses C-Style comments, Gravity still supports the common
  117. "#!" shebang to tell your shell what program to execute the file with.
  118. However, the shebang must be on the first line of the file in order to use it in
  119. this way:</p>
  120. <pre><code class="swift">
  121. #!/path/to/gravity
  122. func main() {
  123. System.print("Execute as: path/to/file.gravity");
  124. System.print("Instead of: gravity path/to/file.gravity");
  125. }
  126. </code></pre>
  127. <!-- IMPORTS -->
  128. <h4 class="section-h4">Include</h4>
  129. <p><b>NOTE:</b> currently, the path to the included file is relative to
  130. the location that the gravity executable was run.</p>
  131. <pre><code class="swift">
  132. // adder.gravity
  133. func add(x, y) {
  134. return x+y;
  135. }
  136. </code></pre>
  137. <pre><code class="swift">
  138. // main.gravity
  139. #include "adder.gravity"
  140. func main() {
  141. System.print("5+4=" + add(5,4));
  142. }
  143. </code></pre>
  144. <h4 class="section-h4">Import</h4>
  145. <p>While #include is used to include file at compilation time, the import statement enables you to load modules at runtime...</p>
  146. <!--<p>Gravity supports importing code from other gravity files using the
  147. import statement:</p>-->
  148. <!-- RESERVED KEYWORDS -->
  149. <h4 class="section-h4">Reserved Keywords</h4>
  150. <p>Like many other programming languages Gravity has some reserved keywords that assume a very specific meaning in the context of the source code:</p>
  151. <pre><code class="swift">
  152. if in or is for var and not func else true enum case null
  153. file lazy super false break while class const event _func
  154. _args struct repeat switch return public static extern
  155. import module default private continue internal undefined
  156. </code></pre>
  157. <!-- IDENTIFIERS -->
  158. <h4 class="section-h4">Identifiers</h4>
  159. <p>Identifiers represent a naming rule used to identify objects inside your source code.
  160. Gravity is a case-sensitive language. Identifiers start with a letter or underscore and may contain letters, digits, and underscores (function identifiers can be any of the <a href="operators.html">built-in operators</a> in order to override a default behaviour):</p>
  161. <pre><code class="swift">
  162. a
  163. _thisIsValid
  164. Hello_World
  165. foo123
  166. BYE_BYE
  167. </code></pre>
  168. <!-- BLOCKS -->
  169. <h4 class="section-h4">Blocks and Scope</h4>
  170. <p>Every named identifier introduced in some portion of the source code is introduced in a scope. The scope is the largest part of the source code in which that identifier is valid. The names declared by a declaration are introduced into a specific scope based on the context of the declaration. For instance, local variable declarations introduce the name into the block scope, whereas class member variable declarations introduce the name into class scope.<br><br>There are three scopes defined: <strong>block scope</strong>, <strong>class scope</strong> and <strong>file scope</strong>. Names declared in the block scope become visible immediately after its completed declarator. This means you cannot refer to a name within the block scope until after it has been fully declared. Names declared in the file and class scopes become visible immediately upon executing the starting statement of the script. This means you can refer to a name within the file or class scopes before it has been fully declared.<br><br>These are all valid scopes:</p>
  171. <pre><code class="swift">
  172. // file scope can refer to a name
  173. // before it has been fully declared
  174. func f1() {
  175. return f2();
  176. }
  177. func f2() {
  178. return 42;
  179. }
  180. // block scope can be nested and
  181. // can hide other local variables
  182. func f3() {
  183. var a = 10;
  184. if (a > 0) {
  185. var a = 20;
  186. }
  187. // 10 is returned here
  188. return a;
  189. }
  190. </code></pre>
  191. </div>
  192. </div> <!-- /row -->
  193. </div> <!-- /main-content -->
  194. </div> <!-- /container -->
  195. <!-- BEGIN FOOTER -->
  196. <footer class="navbar-fixed-bottom">
  197. <div class="container footer">
  198. <p>
  199. </p>
  200. </div>
  201. </footer>
  202. <!-- END FOOTER -->
  203. <!-- Bootstrap JS -->
  204. <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  205. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  206. <!-- Highlights JS -->
  207. <script src="scripts/highlight/highlight.min.js"></script>
  208. <script>hljs.initHighlightingOnLoad();</script>
  209. </body>
  210. </html>