Skip to content

iSimar/PascalToCLR-FinalProject-4TB3

Repository files navigation

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Adding CLR as Target for P0\n",
    "### By **Simarpreet Singh (1216728)**\n",
    "\n",
    "#### Introduction\n",
    "I will be extending the P0 complier to convert pascal code into CIL. This project will allow me to have a better understanding of compliers. CLR (Common Language Runtime) is a program used by many Microsoft software products like .NET, it is used to convert CIL (Common Intermediate Language) to native code.  CIL is also stack-oriented language which gives it an advantage of being able to represent expressions. Being able to represent expressions in a trivial way is idea for a computer constructions. More over after talking to Mr. Sekerinski, I will also be extending P0 with external calls, so that I can make calls to the CLR library from with the P0 program itself, this will extend the usability of the P0 program. This a challenge I would face during the project. \n",
    "\n",
    "#### Milestone 1 (Date: March 18, 2017)\n",
    "\n",
    "As for the first milestone submission of the project I'm submitting conversion of only declaration and assignment statements. Conversion of Control structures and Procedures are planned features for the prototype. I still have to link the .NET with P0 so the that external .NET libaries are usable with it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "First we will be importing the complierString function from P0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "importing Jupyter notebook from P0.ipynb\n",
      "importing Jupyter notebook from SC.ipynb\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<style>\n",
       "div.prompt {display:none}\n",
       "</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "importing Jupyter notebook from ST.ipynb\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<style>\n",
       "div.prompt {display:none}\n",
       "</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import nbimporter\n",
    "from P0 import compileString"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Here we will be creating simple test case functions which use the P0's complieString function "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# test01 consists of a simple declaration\n",
    "# and assignment statement using variable x\n",
    "def test01():\n",
    "    \"\"\"input & output\"\"\"\n",
    "    compileString(\"\"\"\n",
    "program p;\n",
    "  var x: integer;\n",
    "  begin\n",
    "    x:=1\n",
    "  end\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "importing Jupyter notebook from CGclr.ipynb\n",
      ".assembly extern mscorlib{}\n",
      ".assembly 'main'{}\n",
      "\n",
      "\n",
      ".class private auto ansi beforefieldinit MainClass\n",
      "\textends [mscorlib]System.Object\n",
      "{\n",
      "\t.field static int32 x\n",
      "\t.method private static hidebysig default void Main (string[] args)\n",
      "\t\tcil managed\n",
      "\t{\n",
      "\t\t.entrypoint\n",
      "\t\tldc.i4.1\n",
      "\t\tstsfld int32 MainClass::x\n",
      "\t\tret\n",
      "\t}\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "test01()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".assembly extern mscorlib{}\n",
      ".assembly 'main'{}\n",
      "\n",
      "\n",
      ".class private auto ansi beforefieldinit MainClass\n",
      "\textends [mscorlib]System.Object\n",
      "{\n",
      "\t.field static int32 x\n",
      "\t.field static bool y\n",
      "\t.field static bool z\n",
      "\t.method private static hidebysig default void Main (string[] args)\n",
      "\t\tcil managed\n",
      "\t{\n",
      "\t\t.entrypoint\n",
      "\t\tldc.i4.1\n",
      "\t\tstsfld int32 MainClass::x\n",
      "\t\tret\n",
      "\t}\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# test02 consists mutiple gobal variable declarations\n",
    "def test02():\n",
    "    \"\"\"input & output\"\"\"\n",
    "    compileString(\"\"\"\n",
    "program p;\n",
    "  var x: integer;\n",
    "  var y: boolean;\n",
    "  var z: boolean;\n",
    "  begin\n",
    "    x:=1\n",
    "  end\n",
    "\"\"\")\n",
    "test02()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".assembly extern mscorlib{}\n",
      ".assembly 'main'{}\n",
      "\n",
      "\n",
      ".class private auto ansi beforefieldinit MainClass\n",
      "\textends [mscorlib]System.Object\n",
      "{\n",
      "\t.field static int32 x\n",
      "\t.field static bool y\n",
      "\t.field static bool z\n",
      "\t.method private static hidebysig default void Main (string[] args)\n",
      "\t\tcil managed\n",
      "\t{\n",
      "\t\t.entrypoint\n",
      "\t\tldc.i4.1\n",
      "\t\tstsfld int32 MainClass::x\n",
      "\t\tldc.i4.1\n",
      "\t\tstsfld bool MainClass::y\n",
      "\t\tldc.i4.0\n",
      "\t\tstsfld bool MainClass::z\n",
      "\t\tret\n",
      "\t}\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# test03 consists multiple gobal variable declarations\n",
    "# and multiple assignment statements\n",
    "def test03():\n",
    "    \"\"\"input & output\"\"\"\n",
    "    compileString(\"\"\"\n",
    "program p;\n",
    "  var x: integer;\n",
    "  var y, z: boolean;\n",
    "  begin\n",
    "    x:=1;\n",
    "    y:=true;\n",
    "    z:=false\n",
    "  end\n",
    "\"\"\")\n",
    "test03()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".assembly extern mscorlib{}\n",
      ".assembly 'main'{}\n",
      "\n",
      "\n",
      ".class private auto ansi beforefieldinit MainClass\n",
      "\textends [mscorlib]System.Object\n",
      "{\n",
      "\t.field static int32 x\n",
      "\t.field static int32 y\n",
      "\t.field static int32 z\n",
      "\t.method private static hidebysig default void Main (string[] args)\n",
      "\t\tcil managed\n",
      "\t{\n",
      "\t\t.entrypoint\n",
      "\t\tldc.i4.1\n",
      "\t\tstsfld int32 MainClass::x\n",
      "\t\tldc.i4.2\n",
      "\t\tstsfld int32 MainClass::y\n",
      "\t\tldsfld int32 MainClass::x\n",
      "\t\tldsfld int32 MainClass::y\n",
      "\t\tadd\n",
      "\t\tstsfld int32 MainClass::z\n",
      "\t\tret\n",
      "\t}\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# test04 consists simple add statement of two global variables\n",
    "def test04():\n",
    "    \"\"\"input & output\"\"\"\n",
    "    compileString(\"\"\"\n",
    "program p;\n",
    "  var x, y, z: integer;\n",
    "  begin\n",
    "    x:=1;\n",
    "    y:=2;\n",
    "    z:=x+y\n",
    "  end\n",
    "\"\"\")\n",
    "test04()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".assembly extern mscorlib{}\n",
      ".assembly 'main'{}\n",
      "\n",
      "\n",
      ".class private auto ansi beforefieldinit MainClass\n",
      "\textends [mscorlib]System.Object\n",
      "{\n",
      "\t.field static int32 x\n",
      "\t.field static int32 y\n",
      "\t.field static int32 z\n",
      "\t.field static int32 a\n",
      "\t.field static int32 b\n",
      "\t.field static int32 c\n",
      "\t.method private static hidebysig default void Main (string[] args)\n",
      "\t\tcil managed\n",
      "\t{\n",
      "\t\t.entrypoint\n",
      "\t\tldc.i4.1\n",
      "\t\tstsfld int32 MainClass::x\n",
      "\t\tldc.i4.2\n",
      "\t\tstsfld int32 MainClass::y\n",
      "\t\tldsfld int32 MainClass::x\n",
      "\t\tldsfld int32 MainClass::y\n",
      "\t\tadd\n",
      "\t\tstsfld int32 MainClass::z\n",
      "\t\tldsfld int32 MainClass::z\n",
      "\t\tldsfld int32 MainClass::x\n",
      "\t\tsub\n",
      "\t\tstsfld int32 MainClass::a\n",
      "\t\tldsfld int32 MainClass::x\n",
      "\t\tldc.i4.3\n",
      "\t\tmul\n",
      "\t\tstsfld int32 MainClass::b\n",
      "\t\tldc.i4.2\n",
      "\t\tstsfld int32 MainClass::c\n",
      "\t\tldc.i4.8\n",
      "\t\tldsfld int32 MainClass::y\n",
      "\t\trem\n",
      "\t\tstsfld int32 MainClass::c\n",
      "\t\tret\n",
      "\t}\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# test05 consists simple arithmetic statements\n",
    "def test05():\n",
    "    \"\"\"input & output\"\"\"\n",
    "    compileString(\"\"\"\n",
    "program p;\n",
    "  var x, y, z, a, b, c: integer;\n",
    "  begin\n",
    "    x:=1;\n",
    "    y:=2;\n",
    "    z:=x+y;\n",
    "    a:=z-x;\n",
    "    b:=x*3;\n",
    "    c:=5 div 2;\n",
    "    c:=8 mod y\n",
    "  end\n",
    "\"\"\")\n",
    "test05()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".assembly extern mscorlib{}\n",
      ".assembly 'main'{}\n",
      "\n",
      "\n",
      ".class private auto ansi beforefieldinit MainClass\n",
      "\textends [mscorlib]System.Object\n",
      "{\n",
      "\t.field static int32 x\n",
      "\t.method private static hidebysig default void Main (string[] args)\n",
      "\t\tcil managed\n",
      "\t{\n",
      "\t\t.entrypoint\n",
      "\t\tcall string class [mscorlib]System.Console::ReadLine()\n",
      "\t\tcall int32 class [mscorlib]System.Convert::ToInt32(string)\n",
      "\t\tstsfld int32 MainClass::x\n",
      "\t\tldsfld int32 MainClass::x\n",
      "\t\tldc.i4.3\n",
      "\t\tmul\n",
      "\t\tstsfld int32 MainClass::x\n",
      "\t\tldsfld int32 MainClass::x\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tcall void class [mscorlib]System.Console::WriteLine()\n",
      "\t\tcall void class [mscorlib]System.Console::WriteLine()\n",
      "\t\tldsfld int32 MainClass::x\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tmul\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tret\n",
      "\t}\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# test06\n",
    "def test06():\n",
    "    compileString(\"\"\"\n",
    "    program p;\n",
    "      var x: integer;\n",
    "      begin read(x);\n",
    "        x := x * 3;\n",
    "        write(x);\n",
    "        writeln();\n",
    "        writeln();\n",
    "        write(x * 9)\n",
    "      end\n",
    "    \"\"\")\n",
    "test06()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".assembly extern mscorlib{}\n",
      ".assembly 'main'{}\n",
      "\n",
      "\n",
      ".class private auto ansi beforefieldinit MainClass\n",
      "\textends [mscorlib]System.Object\n",
      "{\n",
      "\t.field static bool b\n",
      "\t.field static int32 i\n",
      "\t\n",
      "\n",
      "\t.class nested private auto ansi beforefieldinit q\n",
      "\t\textends [mscorlib]System.Object\n",
      "\t{\n",
      "\t\t.method public static hidebysig\n",
      "\t\t\tdefault void main_proc (bool z)  cil managed\n",
      "\t\t{\n",
      "\t\t\t.locals init (\n",
      "\t\t\t\tint32 V_0)\n",
      "\t\t\tldc.i4.1\n",
      "\t\t\tstarg.s 0\n",
      "\t\t\tldc.i4.4\n",
      "\t\t\tstloc.0\n",
      "\t\t\tldsfld int32 MainClass::i\n",
      "\t\t\tcall void class MainClass/q/p::main_proc(int32)\n",
      "\t\t\tret\n",
      "\t\t}\n",
      "\t\t\n",
      "\n",
      "\t\t.class nested private auto ansi beforefieldinit p\n",
      "\t\t\textends [mscorlib]System.Object\n",
      "\t\t{\n",
      "\t\t\t.method public static hidebysig\n",
      "\t\t\t\tdefault void main_proc (int32 x)  cil managed\n",
      "\t\t\t{\n",
      "\t\t\t\tldc.i4.0\n",
      "\t\t\t\tstsfld bool MainClass::b\n",
      "\t\t\t\tldarg.0\n",
      "\t\t\t\tldc.i4.1\n",
      "\t\t\t\tadd\n",
      "\t\t\t\tstarg.s 0\n",
      "\t\t\t\tret\n",
      "\t\t\t}\n",
      "\t\t}\n",
      "\t\t\n",
      "\n",
      "\t}\n",
      "\t\n",
      "\n",
      "\t.method private static hidebysig default void Main (string[] args)\n",
      "\t\tcil managed\n",
      "\t{\n",
      "\t\t.entrypoint\n",
      "\t\tldsfld bool MainClass::b\n",
      "\t\tcall void class MainClass/q::main_proc(bool)\n",
      "\t\tret\n",
      "\t}\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "def test07():\n",
    "    compileString(\"\"\"\n",
    "program p;\n",
    "  var b: boolean;\n",
    "  var i: integer;\n",
    "  procedure q(var z: boolean);\n",
    "    var y: integer;\n",
    "    procedure p(var x: integer);\n",
    "      begin b := false; x := x + 1\n",
    "      end;\n",
    "    begin z := true; y := 4; p(i)\n",
    "    end;\n",
    "  begin q(b)\n",
    "  end\n",
    "\"\"\")\n",
    "test07()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".assembly extern mscorlib{}\n",
      ".assembly 'main'{}\n",
      "\n",
      "\n",
      ".class private auto ansi beforefieldinit a\n",
      "\textends [mscorlib]System.Object\n",
      "{\n",
      "\t.field  public int32[] arr\n",
      "\t.method public hidebysig specialname rtspecialname\n",
      "\t\tinstance default void '.ctor' ()  cil managed \n",
      "\t{\n",
      "\t\t.maxstack 8\n",
      "\t\tldarg.0\n",
      "\t\tldc.i4.s 0x8\n",
      "\t\tnewarr [mscorlib]System.Int32\n",
      "\t\tstfld int32[] a::arr\n",
      "\t\tldarg.0\n",
      "\t\tcall instance void object::'.ctor'()\n",
      "\t\tret\n",
      "\t}\n",
      "}\n",
      ".class private auto ansi beforefieldinit r\n",
      "\textends [mscorlib]System.Object\n",
      "{\n",
      "\t.field  public int32 f\n",
      "\t.field  public class a g\n",
      "\t.field  public int32 h\n",
      "\t.method public hidebysig specialname rtspecialname\n",
      "\t\tinstance default void '.ctor' ()  cil managed \n",
      "\t{\n",
      "\t\t.maxstack 8\n",
      "\t\tldarg.0\n",
      "\t\tnewobj instance void class a::'.ctor'()\n",
      "\t\tstfld class a r::g\n",
      "\t\tldarg.0\n",
      "\t\tcall instance void object::'.ctor'()\n",
      "\t\tret\n",
      "\t}\n",
      "}\n",
      "\n",
      "\n",
      ".class private auto ansi beforefieldinit MainClass\n",
      "\textends [mscorlib]System.Object\n",
      "{\n",
      "\t.field static class a v\n",
      "\t.field static class r w\n",
      "\t.field static int32 x\n",
      "\t\n",
      "\n",
      "\t.class nested private auto ansi beforefieldinit q\n",
      "\t\textends [mscorlib]System.Object\n",
      "\t{\n",
      "\t\t.method public static hidebysig\n",
      "\t\t\tdefault void main_proc (class a c, class r d)  cil managed\n",
      "\t\t{\n",
      "\t\t\t.locals init (\n",
      "\t\t\t\tint32 V_0)\n",
      "\t\t\tldc.i4.3\n",
      "\t\t\tstloc.0\n",
      "\t\t\tldarg.1\n",
      "\t\t\tldfld int32 r::h\n",
      "\t\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\t\tldarg.0\n",
      "\t\t\tldfld int32[] a::arr\n",
      "\t\t\tldc.i4.1\n",
      "\t\t\tldelem.i4\n",
      "\t\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\t\tldarg.1\n",
      "\t\t\tldfld class a r::g\n",
      "\t\t\tldfld int32[] a::arr\n",
      "\t\t\tldloc.0\n",
      "\t\t\tldelem.i4\n",
      "\t\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\t\tcall void class [mscorlib]System.Console::WriteLine()\n",
      "\t\t\tldarg.0\n",
      "\t\t\tldfld int32[] a::arr\n",
      "\t\t\tldc.i4.7\n",
      "\t\t\tldc.i4.7\n",
      "\t\t\tstelem.i4\n",
      "\t\t\tldarg.0\n",
      "\t\t\tldfld int32[] a::arr\n",
      "\t\t\tldloc.0\n",
      "\t\t\tldc.i4.4\n",
      "\t\t\tadd\n",
      "\t\t\tldelem.i4\n",
      "\t\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\t\tldarg.1\n",
      "\t\t\tldfld class a r::g\n",
      "\t\t\tldfld int32[] a::arr\n",
      "\t\t\tldloc.0\n",
      "\t\t\tldc.i4.2\n",
      "\t\t\tmul\n",
      "\t\t\tldc.i4.7\n",
      "\t\t\tstelem.i4\n",
      "\t\t\tldarg.1\n",
      "\t\t\tldfld class a r::g\n",
      "\t\t\tldfld int32[] a::arr\n",
      "\t\t\tldc.i4.6\n",
      "\t\t\tldelem.i4\n",
      "\t\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\t\tret\n",
      "\t\t}\n",
      "\t}\n",
      "\t\n",
      "\n",
      "\t.method private static hidebysig default void Main (string[] args)\n",
      "\t\tcil managed\n",
      "\t{\n",
      "\t\t.entrypoint\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tstsfld int32 MainClass::x\n",
      "\t\tldsfld class r MainClass::w\n",
      "\t\tldc.i4.5\n",
      "\t\tstfld int32 r::h\n",
      "\t\tldsfld class r MainClass::w\n",
      "\t\tldfld int32 r::h\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tldsfld class a MainClass::v\n",
      "\t\tldfld int32[] a::arr\n",
      "\t\tldc.i4.1\n",
      "\t\tldc.i4.3\n",
      "\t\tstelem.i4\n",
      "\t\tldsfld class a MainClass::v\n",
      "\t\tldfld int32[] a::arr\n",
      "\t\tldsfld int32 MainClass::x\n",
      "\t\tldc.i4.8\n",
      "\t\tsub\n",
      "\t\tldelem.i4\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tldsfld class r MainClass::w\n",
      "\t\tldfld class a r::g\n",
      "\t\tldfld int32[] a::arr\n",
      "\t\tldsfld int32 MainClass::x\n",
      "\t\tldc.i4.3\n",
      "\t\tdiv\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tstelem.i4\n",
      "\t\tldsfld class r MainClass::w\n",
      "\t\tldfld class a r::g\n",
      "\t\tldfld int32[] a::arr\n",
      "\t\tldc.i4.3\n",
      "\t\tldelem.i4\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tcall void class [mscorlib]System.Console::WriteLine()\n",
      "\t\tldsfld class a MainClass::v\n",
      "\t\tldsfld class r MainClass::w\n",
      "\t\tcall void class MainClass/q::main_proc(class a, class r)\n",
      "\t\tcall void class [mscorlib]System.Console::WriteLine()\n",
      "\t\tldsfld class a MainClass::v\n",
      "\t\tldfld int32[] a::arr\n",
      "\t\tldc.i4.7\n",
      "\t\tldelem.i4\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tldsfld class r MainClass::w\n",
      "\t\tldfld class a r::g\n",
      "\t\tldfld int32[] a::arr\n",
      "\t\tldc.i4.6\n",
      "\t\tldelem.i4\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tret\n",
      "\t}\n",
      "\t.method private static hidebysig specialname rtspecialname\n",
      "\t\tdefault void '.cctor' ()  cil managed \n",
      "\t{\n",
      "\t\t.maxstack 8\n",
      "\t\tnewobj instance void class a::'.ctor'()\n",
      "\t\tstsfld class a MainClass::v\n",
      "\t\tnewobj instance void class r::'.ctor'()\n",
      "\t\tstsfld class r MainClass::w\n",
      "\t\tret\n",
      "\t}\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "def test08():\n",
    "    compileString(\"\"\"\n",
    "program p;\n",
    "  type a = array [0 .. 7] of integer;\n",
    "  type r = record f: integer; g: a; h: integer end;\n",
    "  var v: a;\n",
    "  var w: r;\n",
    "  var x: integer;\n",
    "  procedure q(var c: a; var d: r);\n",
    "    var y: integer;\n",
    "    begin y := 3;\n",
    "      write(d.h); write(c[1]); write(d.g[y]); {writes 5, 3, 9}\n",
    "      writeln(); c[7] := 7; write(c[y+4]); {writes 7}\n",
    "      d.g[y*2] := 7; write(d.g[6]) {writes 7}\n",
    "    end;\n",
    "  begin x := 9;\n",
    "    w.h := 12 - 7; write(w.h); {writes 5}\n",
    "    v[1] := 3; write(v[x-8]); {writes 3}\n",
    "    w.g[x div 3] := 9; write(w.g[3]); {writes 9}\n",
    "    writeln(); q(v, w); writeln();\n",
    "    write(v[7]); write(w.g[6]) {writes 7, 7}\n",
    "  end\n",
    "\"\"\")\n",
    "test08()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".assembly extern mscorlib{}\n",
      ".assembly 'main'{}\n",
      "\n",
      "\n",
      ".class private auto ansi beforefieldinit MainClass\n",
      "\textends [mscorlib]System.Object\n",
      "{\n",
      "\t.field static int32 x\n",
      "\t.field static int32 y\n",
      "\t.field static int32 z\n",
      "\t.field static bool b\n",
      "\t.field static bool t\n",
      "\t.field static bool f\n",
      "\t.method private static hidebysig default void Main (string[] args)\n",
      "\t\tcil managed\n",
      "\t{\n",
      "\t\t.entrypoint\n",
      "\t\tldc.i4.7\n",
      "\t\tstsfld int32 MainClass::x\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tstsfld int32 MainClass::y\n",
      "\t\tldc.i4.s 0xb\n",
      "\t\tstsfld int32 MainClass::z\n",
      "\t\tldc.i4.1\n",
      "\t\tstsfld bool MainClass::t\n",
      "\t\tldc.i4.0\n",
      "\t\tstsfld bool MainClass::f\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tldsfld bool MainClass::t\n",
      "\t\tbrfalse IL_001\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_002\n",
      "\tIL_001:\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_002:\n",
      "\t\tldsfld bool MainClass::f\n",
      "\t\tbrfalse IL_003\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_004\n",
      "\tIL_003:\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_004:\n",
      "\t\tldsfld bool MainClass::t\n",
      "\t\tbrtrue IL_005\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_006\n",
      "\tIL_005:\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_006:\n",
      "\t\tldsfld bool MainClass::f\n",
      "\t\tbrtrue IL_007\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_008\n",
      "\tIL_007:\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_008:\n",
      "\t\tldsfld bool MainClass::t\n",
      "\t\tbrtrue IL_009\n",
      "\t\tldsfld bool MainClass::t\n",
      "\t\tbrfalse IL_0010\n",
      "\tIL_009:\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_0011\n",
      "\tIL_0010:\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_0011:\n",
      "\t\tldsfld bool MainClass::t\n",
      "\t\tbrtrue IL_0012\n",
      "\t\tldsfld bool MainClass::f\n",
      "\t\tbrfalse IL_0013\n",
      "\tIL_0012:\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_0014\n",
      "\tIL_0013:\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_0014:\n",
      "\t\tldsfld bool MainClass::f\n",
      "\t\tbrtrue IL_0015\n",
      "\t\tldsfld bool MainClass::t\n",
      "\t\tbrfalse IL_0016\n",
      "\tIL_0015:\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_0017\n",
      "\tIL_0016:\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_0017:\n",
      "\t\tldsfld bool MainClass::f\n",
      "\t\tbrtrue IL_0018\n",
      "\t\tldsfld bool MainClass::f\n",
      "\t\tbrfalse IL_0019\n",
      "\tIL_0018:\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_0020\n",
      "\tIL_0019:\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_0020:\n",
      "\t\tldsfld bool MainClass::t\n",
      "\t\tbrfalse IL_0021\n",
      "\t\tldsfld bool MainClass::t\n",
      "\t\tbrfalse IL_0021\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_0022\n",
      "\tIL_0021:\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_0022:\n",
      "\t\tldsfld bool MainClass::t\n",
      "\t\tbrfalse IL_0023\n",
      "\t\tldsfld bool MainClass::f\n",
      "\t\tbrfalse IL_0023\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_0024\n",
      "\tIL_0023:\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_0024:\n",
      "\t\tldsfld bool MainClass::f\n",
      "\t\tbrfalse IL_0025\n",
      "\t\tldsfld bool MainClass::t\n",
      "\t\tbrfalse IL_0025\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_0026\n",
      "\tIL_0025:\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_0026:\n",
      "\t\tldsfld bool MainClass::f\n",
      "\t\tbrfalse IL_0027\n",
      "\t\tldsfld bool MainClass::f\n",
      "\t\tbrfalse IL_0027\n",
      "\t\tldc.i4.7\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_0028\n",
      "\tIL_0027:\n",
      "\t\tldc.i4.s 0x9\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_0028:\n",
      "\t\tcall void class [mscorlib]System.Console::WriteLine()\n",
      "\t\tldc.i4.1\n",
      "\t\tstsfld bool MainClass::b\n",
      "\t\tldsfld bool MainClass::b\n",
      "\t\tbrfalse IL_0029\n",
      "\t\tldc.i4.3\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_0030\n",
      "\tIL_0029:\n",
      "\t\tldc.i4.5\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_0030:\n",
      "\t\tldc.i4.0\n",
      "\t\tstsfld bool MainClass::b\n",
      "\t\tldsfld bool MainClass::b\n",
      "\t\tbrfalse IL_0031\n",
      "\t\tldc.i4.3\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_0032\n",
      "\tIL_0031:\n",
      "\t\tldc.i4.5\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_0032:\n",
      "\t\tldsfld int32 MainClass::x\n",
      "\t\tldsfld int32 MainClass::y\n",
      "\t\tclt\n",
      "\t\tstsfld bool MainClass::b\n",
      "\t\tldsfld bool MainClass::b\n",
      "\t\tbrfalse IL_0033\n",
      "\t\tldsfld int32 MainClass::x\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_0034\n",
      "\tIL_0033:\n",
      "\t\tldsfld int32 MainClass::y\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_0034:\n",
      "\t\tldsfld int32 MainClass::x\n",
      "\t\tldsfld int32 MainClass::y\n",
      "\t\tcgt\n",
      "\t\tstsfld bool MainClass::b\n",
      "\t\tldsfld bool MainClass::b\n",
      "\t\tbrfalse IL_0035\n",
      "\t\tldc.i4.3\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\t\tbr IL_0036\n",
      "\tIL_0035:\n",
      "\t\tldc.i4.5\n",
      "\t\tcall void class [mscorlib]System.Console::Write(int32)\n",
      "\tIL_0036:\n",
      "\t\tret\n",
      "\t}\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "def test09():\n",
    "    compileString(\"\"\"\n",
    "    program p;\n",
    "        const five = 5;\n",
    "      const seven = 7;\n",
    "      const always = true;\n",
    "      const never = false;\n",
    "      var x, y, z: integer;\n",
    "      var b, t, f: boolean;\n",
    "      begin \n",
    "            x := seven; y := 9; z := 11; t := true; f := false;\n",
    "            if true then write(7) else write(9);    {writes 7}\n",
    "            if false then write(7) else write(9);   {writes 9}\n",
    "            if t then write(7) else write(9);       {writes 7}\n",
    "            if f then write(7) else write(9);       {writes 9}\n",
    "            if not t then write(7) else write(9);   {writes 9}\n",
    "            if not f then write(7) else write(9);   {writes 7}\n",
    "            if t or t then write(7) else write(9);  {writes 7}\n",
    "            if t or f then write(7) else write(9);  {writes 7}\n",
    "            if f or t then write(7) else write(9);  {writes 7}\n",
    "            if f or f then write(7) else write(9);  {writes 9}\n",
    "            if t and t then write(7) else write(9); {writes 7}\n",
    "            if t and f then write(7) else write(9); {writes 9}\n",
    "            if f and t then write(7) else write(9); {writes 9}\n",
    "            if f and f then write(7) else write(9); {writes 9}\n",
    "            writeln();\n",
    "            b := true;\n",
    "            if b then write(3) else write(5); {writes 3}\n",
    "            b := false;\n",
    "            if b then write(3) else write(5); {writes 5}\n",
    "            b := x < y;\n",
    "            if b then write(x) else write(y); {writes 7}\n",
    "            b := (x > y);\n",
    "            if b then write(3) else write(5) {writes 3}\n",
    "      end\n",
    "    \"\"\")\n",
    "test09()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published