diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /tiger-compiler/tcsh/python/tests/ast.ipynb | |
Diffstat (limited to 'tiger-compiler/tcsh/python/tests/ast.ipynb')
| -rw-r--r-- | tiger-compiler/tcsh/python/tests/ast.ipynb | 394 |
1 files changed, 394 insertions, 0 deletions
diff --git a/tiger-compiler/tcsh/python/tests/ast.ipynb b/tiger-compiler/tcsh/python/tests/ast.ipynb new file mode 100644 index 0000000..51f06ee --- /dev/null +++ b/tiger-compiler/tcsh/python/tests/ast.ipynb @@ -0,0 +1,394 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "3f7f66b6", + "metadata": {}, + "source": [ + "# Import Tiger and Ast" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "9cb3bce5", + "metadata": {}, + "outputs": [], + "source": [ + "import tc" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "be38c5fe", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tc.has(\"ast\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "9fe09a20", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import tiger_ast\n", + "tiger_ast == tc.ast" + ] + }, + { + "cell_type": "markdown", + "id": "3b3a7285", + "metadata": {}, + "source": [ + "# Ast Library" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2be2f25d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['ArrayExp',\n", + " 'ArrayTy',\n", + " 'AssignExp',\n", + " 'Ast',\n", + " 'AstConstVisitor',\n", + " 'AstVisitor',\n", + " 'BreakExp',\n", + " 'CallExp',\n", + " 'CallbackVisitor',\n", + " 'CallbackVisitor_Argument',\n", + " 'CastExp',\n", + " 'ChunkInterface',\n", + " 'ChunkList',\n", + " 'ClassTy',\n", + " 'Dec',\n", + " 'Exp',\n", + " 'Field',\n", + " 'FieldInit',\n", + " 'FieldVar',\n", + " 'ForExp',\n", + " 'FunctionChunk',\n", + " 'FunctionDec',\n", + " 'IfExp',\n", + " 'IntExp',\n", + " 'LetExp',\n", + " 'MethodCallExp',\n", + " 'MethodChunk',\n", + " 'MethodDec',\n", + " 'NameTy',\n", + " 'NilExp',\n", + " 'ObjectExp',\n", + " 'OpExp',\n", + " 'RecordExp',\n", + " 'RecordTy',\n", + " 'SeqExp',\n", + " 'SimpleVar',\n", + " 'StringExp',\n", + " 'SubscriptVar',\n", + " 'SwigPyIterator',\n", + " 'Ty',\n", + " 'TypeChunk',\n", + " 'TypeDec',\n", + " 'Var',\n", + " 'VarChunk',\n", + " 'VarDec',\n", + " 'WhileExp']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def ignore(e):\n", + " if e.startswith(\"_\") or not e[0] == e[0].upper():\n", + " return False\n", + " return e not in (\"Escapable\", \"Typable\", \"TypeConstructor\")\n", + "list(filter(ignore, dir(tc.ast)))" + ] + }, + { + "cell_type": "markdown", + "id": "c2ffa10b", + "metadata": {}, + "source": [ + "# Tiger Magics and AST" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "eecfc7a6", + "metadata": {}, + "outputs": [], + "source": [ + "%%tiger executor\n", + "let\n", + " var b := 5\n", + "in\n", + " print_int(b)\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "2d1e6475", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'tiger_ast.ChunkList'>\n" + ] + } + ], + "source": [ + "ast = executor.ast\n", + "print(type(ast))" + ] + }, + { + "cell_type": "markdown", + "id": "89fbcdc9", + "metadata": {}, + "source": [ + "# Parse Ast\n", + "We use this to only parse the ast and don't run code after TC2, but use Tiger Magics" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "0ee4cd72", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'tiger_ast.ChunkList'>\n" + ] + } + ], + "source": [ + "import tempfile\n", + "\n", + "with tempfile.NamedTemporaryFile() as f:\n", + " f.write(b\"let var b := 5 in print_int(b) end\")\n", + " f.seek(0)\n", + " ast = tc.ti.TiExecutor(f.name).parse()\n", + "print(type(ast))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "0276768a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "primitive print(string : string)\n", + "primitive print_err(string : string)\n", + "primitive print_int(int : int)\n", + "primitive flush()\n", + "primitive getchar() : string\n", + "primitive ord(string : string) : int\n", + "primitive chr(code : int) : string\n", + "primitive size(string : string) : int\n", + "primitive streq(s1 : string, s2 : string) : int\n", + "primitive strcmp(s1 : string, s2 : string) : int\n", + "primitive substring(string : string, start : int, length : int) : string\n", + "primitive concat(fst : string, snd : string) : string\n", + "primitive not(boolean : int) : int\n", + "primitive exit(status : int)\n", + "function _main() =\n", + " (\n", + " let\n", + " var b := 5\n", + " in\n", + " print_int(b)\n", + " end;\n", + " ()\n", + " )\n" + ] + } + ], + "source": [ + "print(ast)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "e9856080", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'tiger_ast.FunctionChunk'>\n", + "<class 'tiger_ast.FunctionDec'>\n", + "\n", + "function _main() =\n", + " (\n", + " let\n", + " var b := 5\n", + " in\n", + " print_int(b)\n", + " end;\n", + " ()\n", + " )\n" + ] + } + ], + "source": [ + "main_chunk = ast[1]\n", + "print(type(main_chunk))\n", + "main_function = main_chunk[0]\n", + "print(type(main_function))\n", + "print(str(main_function))" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "51de4ce3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'tiger_ast.SeqExp'>\n", + "<class 'tiger_ast.LetExp'>\n", + "let\n", + " var b := 5\n", + "in\n", + " print_int(b)\n", + "end\n" + ] + } + ], + "source": [ + "main_body = main_function.body_get()\n", + "print(type(main_body))\n", + "let = main_body.exps_get()[0]\n", + "print(type(let))\n", + "print(let)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "1428a492", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'tiger_ast.VarChunk'>\n", + "<class 'tiger_ast.VarDec'>\n", + "\n", + "var b := 5\n", + "b -> 5\n" + ] + } + ], + "source": [ + "var_chk = let.chunks_get()[0]\n", + "print(type(var_chk))\n", + "var_b = var_chk[0]\n", + "print(type(var_b))\n", + "print(var_b)\n", + "print(\"{} -> {}\".format(var_b.name_get(), var_b.init_get()))" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "d9796c32", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'tiger_ast.CallExp'>\n", + "print_int(b)\n", + "<class 'tiger_ast.SimpleVar'>\n", + "b\n" + ] + } + ], + "source": [ + "print_call = let.body_get()\n", + "print(type(print_call))\n", + "print(print_call)\n", + "print_arg = print_call.args_get()[0]\n", + "print(type(print_arg))\n", + "print(print_arg)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "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.10.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} |
