Skip to content

Commit

Permalink
Update the notebook to use LFortran kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
certik committed Mar 7, 2021
1 parent 7666332 commit f9b38dc
Showing 1 changed file with 32 additions and 75 deletions.
107 changes: 32 additions & 75 deletions doc/nb/developer_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"are encouraged to use them independently for other applications and build tools\n",
"on top:\n",
"\n",
"* Abstract Syntax Tree (AST), module `lfortran.ast`: Represents any Fortran\n",
"* Abstract Syntax Tree (AST): Represents any Fortran\n",
" source code, strictly based on syntax, no semantic is included. The AST\n",
" module can convert itself to Fortran source code.\n",
"\n",
"* Abstract Semantic Representation (ASR), module `lfortran.asr`: Represents a\n",
"* Abstract Semantic Representation (ASR): Represents a\n",
" valid Fortran source code, all semantic is included. Invalid Fortran code is\n",
" not allowed (an error will be given). The ASR module can convert itself to an\n",
" AST.\n",
Expand All @@ -37,38 +37,39 @@
"metadata": {},
"outputs": [],
"source": [
"from lfortran.ast import src_to_ast, print_tree\n",
"from lfortran.ast.ast_to_src import ast_to_src\n",
"src = \"\"\"\\\n",
"integer function f(a, b) result(r)\n",
"integer, intent(in) :: a, b\n",
"r = a + b\n",
"end function\n",
"\"\"\"\n",
"ast = src_to_ast(src, translation_unit=False)"
"end function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can pretty print it using `print_tree()` ([#59](https://gitlab.com/lfortran/lfortran/issues/59)):"
"We can pretty print it using the `%%showast` magic:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"print_tree(ast)"
"%%showast\n",
"integer function f(a, b) result(r)\n",
"integer, intent(in) :: a, b\n",
"r = a + b\n",
"end function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can convert AST to Fortran source code using `ast_to_src()`:"
"We can convert AST to Fortran source code using `%%showfmt`:"
]
},
{
Expand All @@ -77,7 +78,11 @@
"metadata": {},
"outputs": [],
"source": [
"print(ast_to_src(ast))"
"%%showfmt\n",
"integer function f(a, b) result(r)\n",
"integer, intent(in) :: a, b\n",
"r = a + b\n",
"end function"
]
},
{
Expand All @@ -87,27 +92,14 @@
"All AST nodes and their arguments are described in\n",
"[AST.asdl](https://gitlab.com/lfortran/lfortran/blob/master/grammar/AST.asdl).\n",
"\n",
"## Abstract Semantic Representation (ASR)\n",
"\n",
"We can convert AST to ASR using the `ast_to_asr()` function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from lfortran.semantic.ast_to_asr import ast_to_asr\n",
"from lfortran.asr.pprint import pprint_asr\n",
"asr = ast_to_asr(ast)"
"## Abstract Semantic Representation (ASR)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can pretty print using the `pprint_asr()` function:"
"We can pretty print using the `%%showasr` magic:"
]
},
{
Expand All @@ -116,40 +108,11 @@
"metadata": {},
"outputs": [],
"source": [
"pprint_asr(asr)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For example to get the variables defined in the function scope:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"asr.global_scope.symbols[\"f\"].symtab.symbols.keys()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can convert ASR to AST and to source code:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from lfortran.asr.asr_to_ast import asr_to_ast\n",
"print(ast_to_src(asr_to_ast(asr)))"
"%%showasr\n",
"integer function f(a, b) result(r)\n",
"integer, intent(in) :: a, b\n",
"r = a + b\n",
"end function"
]
},
{
Expand All @@ -163,21 +126,15 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
"display_name": "Fortran",
"language": "fortran",
"name": "fortran"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"file_extension": ".f90",
"mimetype": "text/x-fortran",
"name": "fortran",
"version": "2018"
}
},
"nbformat": 4,
Expand Down

0 comments on commit f9b38dc

Please sign in to comment.