| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,11 +16,11 @@ | |||
| 16 | 16 | "are encouraged to use them independently for other applications and build tools\n", | |
| 17 | 17 | "on top:\n", | |
| 18 | 18 | "\n", | |
| 19 | - "* Abstract Syntax Tree (AST), module `lfortran.ast`: Represents any Fortran\n", | ||
| 19 | + "* Abstract Syntax Tree (AST): Represents any Fortran\n", | ||
| 20 | 20 | " source code, strictly based on syntax, no semantic is included. The AST\n", | |
| 21 | 21 | " module can convert itself to Fortran source code.\n", | |
| 22 | 22 | "\n", | |
| 23 | - "* Abstract Semantic Representation (ASR), module `lfortran.asr`: Represents a\n", | ||
| 23 | + "* Abstract Semantic Representation (ASR): Represents a\n", | ||
| 24 | 24 | " valid Fortran source code, all semantic is included. Invalid Fortran code is\n", | |
| 25 | 25 | " not allowed (an error will be given). The ASR module can convert itself to an\n", | |
| 26 | 26 | " AST.\n", | |
@@ -37,38 +37,39 @@ | |||
| 37 | 37 | "metadata": {}, | |
| 38 | 38 | "outputs": [], | |
| 39 | 39 | "source": [ | |
| 40 | - "from lfortran.ast import src_to_ast, print_tree\n", | ||
| 41 | - "from lfortran.ast.ast_to_src import ast_to_src\n", | ||
| 42 | - "src = \"\"\"\\\n", | ||
| 43 | 40 | "integer function f(a, b) result(r)\n", | |
| 44 | 41 | "integer, intent(in) :: a, b\n", | |
| 45 | 42 | "r = a + b\n", | |
| 46 | - "end function\n", | ||
| 47 | - "\"\"\"\n", | ||
| 48 | - "ast = src_to_ast(src, translation_unit=False)" | ||
| 43 | + "end function" | ||
| 49 | 44 | ] | |
| 50 | 45 | }, | |
| 51 | 46 | { | |
| 52 | 47 | "cell_type": "markdown", | |
| 53 | 48 | "metadata": {}, | |
| 54 | 49 | "source": [ | |
| 55 | - "We can pretty print it using `print_tree()` ([#59](https://gitlab.com/lfortran/lfortran/issues/59)):" | ||
| 50 | + "We can pretty print it using the `%%showast` magic:" | ||
| 56 | 51 | ] | |
| 57 | 52 | }, | |
| 58 | 53 | { | |
| 59 | 54 | "cell_type": "code", | |
| 60 | 55 | "execution_count": null, | |
| 61 | - "metadata": {}, | ||
| 56 | + "metadata": { | ||
| 57 | + "scrolled": true | ||
| 58 | + }, | ||
| 62 | 59 | "outputs": [], | |
| 63 | 60 | "source": [ | |
| 64 | - "print_tree(ast)" | ||
| 61 | + "%%showast\n", | ||
| 62 | + "integer function f(a, b) result(r)\n", | ||
| 63 | + "integer, intent(in) :: a, b\n", | ||
| 64 | + "r = a + b\n", | ||
| 65 | + "end function" | ||
| 65 | 66 | ] | |
| 66 | 67 | }, | |
| 67 | 68 | { | |
| 68 | 69 | "cell_type": "markdown", | |
| 69 | 70 | "metadata": {}, | |
| 70 | 71 | "source": [ | |
| 71 | - "We can convert AST to Fortran source code using `ast_to_src()`:" | ||
| 72 | + "We can convert AST to Fortran source code using `%%showfmt`:" | ||
| 72 | 73 | ] | |
| 73 | 74 | }, | |
| 74 | 75 | { | |
@@ -77,7 +78,11 @@ | |||
| 77 | 78 | "metadata": {}, | |
| 78 | 79 | "outputs": [], | |
| 79 | 80 | "source": [ | |
| 80 | - "print(ast_to_src(ast))" | ||
| 81 | + "%%showfmt\n", | ||
| 82 | + "integer function f(a, b) result(r)\n", | ||
| 83 | + "integer, intent(in) :: a, b\n", | ||
| 84 | + "r = a + b\n", | ||
| 85 | + "end function" | ||
| 81 | 86 | ] | |
| 82 | 87 | }, | |
| 83 | 88 | { | |
@@ -87,27 +92,14 @@ | |||
| 87 | 92 | "All AST nodes and their arguments are described in\n", | |
| 88 | 93 | "[AST.asdl](https://gitlab.com/lfortran/lfortran/blob/master/grammar/AST.asdl).\n", | |
| 89 | 94 | "\n", | |
| 90 | - "## Abstract Semantic Representation (ASR)\n", | ||
| 91 | - "\n", | ||
| 92 | - "We can convert AST to ASR using the `ast_to_asr()` function:" | ||
| 93 | - ] | ||
| 94 | - }, | ||
| 95 | - { | ||
| 96 | - "cell_type": "code", | ||
| 97 | - "execution_count": null, | ||
| 98 | - "metadata": {}, | ||
| 99 | - "outputs": [], | ||
| 100 | - "source": [ | ||
| 101 | - "from lfortran.semantic.ast_to_asr import ast_to_asr\n", | ||
| 102 | - "from lfortran.asr.pprint import pprint_asr\n", | ||
| 103 | - "asr = ast_to_asr(ast)" | ||
| 95 | + "## Abstract Semantic Representation (ASR)" | ||
| 104 | 96 | ] | |
| 105 | 97 | }, | |
| 106 | 98 | { | |
| 107 | 99 | "cell_type": "markdown", | |
| 108 | 100 | "metadata": {}, | |
| 109 | 101 | "source": [ | |
| 110 | - "We can pretty print using the `pprint_asr()` function:" | ||
| 102 | + "We can pretty print using the `%%showasr` magic:" | ||
| 111 | 103 | ] | |
| 112 | 104 | }, | |
| 113 | 105 | { | |
@@ -116,40 +108,11 @@ | |||
| 116 | 108 | "metadata": {}, | |
| 117 | 109 | "outputs": [], | |
| 118 | 110 | "source": [ | |
| 119 | - "pprint_asr(asr)" | ||
| 120 | - ] | ||
| 121 | - }, | ||
| 122 | - { | ||
| 123 | - "cell_type": "markdown", | ||
| 124 | - "metadata": {}, | ||
| 125 | - "source": [ | ||
| 126 | - "For example to get the variables defined in the function scope:" | ||
| 127 | - ] | ||
| 128 | - }, | ||
| 129 | - { | ||
| 130 | - "cell_type": "code", | ||
| 131 | - "execution_count": null, | ||
| 132 | - "metadata": {}, | ||
| 133 | - "outputs": [], | ||
| 134 | - "source": [ | ||
| 135 | - "asr.global_scope.symbols[\"f\"].symtab.symbols.keys()" | ||
| 136 | - ] | ||
| 137 | - }, | ||
| 138 | - { | ||
| 139 | - "cell_type": "markdown", | ||
| 140 | - "metadata": {}, | ||
| 141 | - "source": [ | ||
| 142 | - "We can convert ASR to AST and to source code:" | ||
| 143 | - ] | ||
| 144 | - }, | ||
| 145 | - { | ||
| 146 | - "cell_type": "code", | ||
| 147 | - "execution_count": null, | ||
| 148 | - "metadata": {}, | ||
| 149 | - "outputs": [], | ||
| 150 | - "source": [ | ||
| 151 | - "from lfortran.asr.asr_to_ast import asr_to_ast\n", | ||
| 152 | - "print(ast_to_src(asr_to_ast(asr)))" | ||
| 111 | + "%%showasr\n", | ||
| 112 | + "integer function f(a, b) result(r)\n", | ||
| 113 | + "integer, intent(in) :: a, b\n", | ||
| 114 | + "r = a + b\n", | ||
| 115 | + "end function" | ||
| 153 | 116 | ] | |
| 154 | 117 | }, | |
| 155 | 118 | { | |
@@ -163,21 +126,15 @@ | |||
| 163 | 126 | ], | |
| 164 | 127 | "metadata": { | |
| 165 | 128 | "kernelspec": { | |
| 166 | - "display_name": "Python 3", | ||
| 167 | - "language": "python", | ||
| 168 | - "name": "python3" | ||
| 129 | + "display_name": "Fortran", | ||
| 130 | + "language": "fortran", | ||
| 131 | + "name": "fortran" | ||
| 169 | 132 | }, | |
| 170 | 133 | "language_info": { | |
| 171 | - "codemirror_mode": { | ||
| 172 | - "name": "ipython", | ||
| 173 | - "version": 3 | ||
| 174 | - }, | ||
| 175 | - "file_extension": ".py", | ||
| 176 | - "mimetype": "text/x-python", | ||
| 177 | - "name": "python", | ||
| 178 | - "nbconvert_exporter": "python", | ||
| 179 | - "pygments_lexer": "ipython3", | ||
| 180 | - "version": "3.7.3" | ||
| 134 | + "file_extension": ".f90", | ||
| 135 | + "mimetype": "text/x-fortran", | ||
| 136 | + "name": "fortran", | ||
| 137 | + "version": "2018" | ||
| 181 | 138 | } | |
| 182 | 139 | }, | |
| 183 | 140 | "nbformat": 4, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments