Fahad/MCG-CS/CodeGenerator.cs
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 30 Mar 2022 18:02:40 +0100
changeset 474 726f4e65c0fe
parent 45 7545b1bc1514
permissions -rw-r--r--
made paper changes after ITP comments
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     1
using System;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     2
using System.Collections.Generic;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     3
using System.Text;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     4
// Added
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     5
using System.Reflection;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     6
using System.Reflection.Emit;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     7
using System.IO;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     8
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     9
public sealed class CodeGenerator
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    10
{
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    11
    
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    12
    Dictionary<string, System.Reflection.Emit.LocalBuilder> tblIdentifier;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    13
    Dictionary<string, System.Reflection.Emit.LocalBuilder> tblArguments;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    14
    TypeBuilder _typeBuilder = null;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    15
    System.Reflection.Emit.ILGenerator _ILGenerator = null;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    16
    MemberInfo WriteLine = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    17
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    18
    #region Statement
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    19
    public CodeGenerator(Statement statement, string fileName)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    20
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    21
        string strFileName = Path.GetFileNameWithoutExtension(fileName);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    22
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    23
        #region Define Assembly
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    24
        //AssemblyName _AssemblyName = new AssemblyName(Path.GetFileNameWithoutExtension(fileName));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    25
        AssemblyName _AssemblyName = new AssemblyName(strFileName);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    26
        AppDomain currentDomain = AppDomain.CurrentDomain;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    27
        AssemblyBuilder _AssemblyBuilder = currentDomain.DefineDynamicAssembly(_AssemblyName, AssemblyBuilderAccess.Save);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    28
        #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    29
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    30
        #region Define Module
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    31
        ModuleBuilder _ModuleBuilder = _AssemblyBuilder.DefineDynamicModule(fileName);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    32
        #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    33
        
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    34
        _typeBuilder = _ModuleBuilder.DefineType("CodeGenerator");
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    35
        MethodBuilder _MethodBuilder = _typeBuilder.DefineMethod
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    36
            ("Main", 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    37
            MethodAttributes.Static, 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    38
            typeof(void), 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    39
            System.Type.EmptyTypes);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    40
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    41
        _ILGenerator = _MethodBuilder.GetILGenerator();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    42
        tblIdentifier = new Dictionary<string, System.Reflection.Emit.LocalBuilder>();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    43
        tblArguments = new Dictionary<string, System.Reflection.Emit.LocalBuilder>();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    44
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    45
        // CIL generation
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    46
        GenerateStatement(statement);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    47
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    48
        _ILGenerator.Emit(OpCodes.Ret);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    49
        _typeBuilder.CreateType();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    50
        _ModuleBuilder.CreateGlobalFunctions();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    51
        _AssemblyBuilder.SetEntryPoint(_MethodBuilder);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    52
        _AssemblyBuilder.Save(fileName);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    53
        tblIdentifier = null;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    54
        _ILGenerator = null; 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    55
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    56
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    57
    public void GenerateStatement(Statement _statement)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    58
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    59
        if (_statement is StatementSequence)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    60
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    61
            StatementSequence _StatementSequence = (StatementSequence)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    62
            GenerateStatement(_StatementSequence.Left);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    63
            GenerateStatement(_StatementSequence.Right);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    64
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    65
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    66
        else if (_statement is DeclareVariable)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    67
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    68
            // declare a variable in symbol table
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    69
            DeclareVariable declare = (DeclareVariable)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    70
            tblIdentifier[declare.Identifier] = 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    71
                _ILGenerator.DeclareLocal(GetExpressionType(declare.Expression));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    72
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    73
            // set the initial value
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    74
            Assignment assign = new Assignment();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    75
            assign.Identifier = declare.Identifier;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    76
            assign.Expression = declare.Expression;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    77
            GenerateStatement(assign);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    78
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    79
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    80
        else if (_statement is Assignment)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    81
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    82
            Assignment assign = (Assignment)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    83
            GenerateExpression(assign.Expression, 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    84
                GetExpressionType(assign.Expression));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    85
            if (GetExpressionType(assign.Expression) == typeof(ArithmaticExpression))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    86
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    87
                SaveIdentifier(assign.Identifier, typeof(Int32));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    88
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    89
            else 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    90
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    91
                SaveIdentifier(assign.Identifier, 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    92
                    GetExpressionType(assign.Expression));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    93
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    94
            
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    95
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    96
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    97
        else if (_statement is DeclareFunction) 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    98
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    99
            GenerateStatementMethod(_statement);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   100
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   101
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   102
        else if (_statement is Write)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   103
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   104
            // for print keyword, call .net method for printscreen
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   105
            GenerateExpression(((Write)_statement).Expression,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   106
                typeof(string));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   107
            _ILGenerator.Emit(OpCodes.Call,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   108
                typeof(System.Console).GetMethod("WriteLine",
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   109
                new System.Type[] { typeof(string) }));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   110
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   111
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   112
        else if (_statement is ReadInput)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   113
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   114
            // call the readline method and parse input method
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   115
            _ILGenerator.Emit(OpCodes.Call,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   116
                typeof(System.Console).GetMethod("ReadLine",
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   117
                BindingFlags.Public | BindingFlags.Static,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   118
                null, new System.Type[] { }, null));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   119
            _ILGenerator.Emit(OpCodes.Call,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   120
                typeof(int).GetMethod("Parse",
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   121
                BindingFlags.Public | BindingFlags.Static,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   122
                null, new System.Type[] { typeof(string) }, null));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   123
            // store the input value in local builder
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   124
            SaveIdentifier(((ReadInput)_statement).Identifier, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   125
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   126
        else if (_statement is IfThenElse)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   127
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   128
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   129
            //IfThenElse ifThenElse = (IfThenElse)stmt;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   130
            //RelationalExpression relExpr = (RelationalExpression)ifThenElse.If;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   131
            //// if, left side only
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   132
            //il.Emit(OpCodes.Stloc, symbolTable[relExpr.Left.ToString()]);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   133
            //Label lblIf = il.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   134
            //il.Emit(OpCodes.Br, lblIf);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   135
            //// then
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   136
            //Label lblThen = il.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   137
            //il.MarkLabel(lblThen);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   138
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   139
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   140
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   141
        else if (_statement is While)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   142
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   143
            While _while = (While)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   144
            Label lblTest = _ILGenerator.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   145
            Label lblEnd = _ILGenerator.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   146
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   147
            if (_while.Operand == RelationalOperands.GreaterThan)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   148
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   149
                _ILGenerator.MarkLabel(lblTest);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   150
                GenerateExpression(_while.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   151
                GenerateExpression(_while.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   152
                _ILGenerator.Emit(OpCodes.Cgt);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   153
                _ILGenerator.Emit(OpCodes.Brfalse, lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   154
                GenerateStatement(_while.Body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   155
                _ILGenerator.Emit(OpCodes.Br, lblTest);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   156
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   157
                _ILGenerator.MarkLabel(lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   158
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   159
            else if (_while.Operand == RelationalOperands.EqualTo)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   160
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   161
                _ILGenerator.MarkLabel(lblTest);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   162
                GenerateExpression(_while.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   163
                GenerateExpression(_while.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   164
                _ILGenerator.Emit(OpCodes.Ceq);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   165
                _ILGenerator.Emit(OpCodes.Brfalse, lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   166
                GenerateStatement(_while.Body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   167
                _ILGenerator.Emit(OpCodes.Br, lblTest);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   168
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   169
                _ILGenerator.MarkLabel(lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   170
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   171
            else if (_while.Operand == RelationalOperands.LessThan)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   172
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   173
                _ILGenerator.MarkLabel(lblTest);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   174
                GenerateExpression(_while.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   175
                GenerateExpression(_while.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   176
                _ILGenerator.Emit(OpCodes.Clt);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   177
                _ILGenerator.Emit(OpCodes.Brfalse, lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   178
                GenerateStatement(_while.Body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   179
                _ILGenerator.Emit(OpCodes.Br, lblTest);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   180
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   181
                _ILGenerator.MarkLabel(lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   182
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   183
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   184
        else if (_statement is IfThen)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   185
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   186
            #region
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   187
            //////Label body = il.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   188
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   189
            //////il.Emit(OpCodes.Ldc_I4, 1000);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   190
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   191
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   192
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   193
            /*
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   194
            // var x = 0;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   195
            // if x < 5 then
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   196
            //    print "less than 5";
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   197
            // endif;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   198
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   199
            IfThen ifThen = (IfThen)stmt;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   200
            // jump to test
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   201
            
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   202
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   203
            // **test** if x LessThan 5? (do the test)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   204
            il.MarkLabel(test);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   205
            GenExpr(ifThen.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   206
            
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   207
            */
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   208
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   209
            //Label greaterThan = il.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   210
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   211
            //IfThen ifThen = (IfThen)stmt;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   212
            //GenExpr(ifThen.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   213
            //GenExpr(ifThen.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   214
            //if (ifThen.Operand == RelationalOperands.GreaterThan) 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   215
            //{
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   216
            //    
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   217
            //}
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   218
            #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   219
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   220
            IfThen ifThen = (IfThen)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   221
            Label lblElse = _ILGenerator.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   222
            Label lblEnd = _ILGenerator.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   223
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   224
            #region GreaterThan
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   225
            if (ifThen.Operand == RelationalOperands.GreaterThan)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   226
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   227
                GenerateExpression(ifThen.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   228
                GenerateExpression(ifThen.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   229
                _ILGenerator.Emit(OpCodes.Cgt);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   230
                _ILGenerator.Emit(OpCodes.Brfalse, lblElse);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   231
                GenerateStatement(ifThen.ThenBody);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   232
                _ILGenerator.Emit(OpCodes.Br, lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   233
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   234
                _ILGenerator.MarkLabel(lblElse);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   235
                GenerateStatement(ifThen.ElseBody);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   236
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   237
                _ILGenerator.MarkLabel(lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   238
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   239
            #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   240
            #region EqualTo
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   241
            else if (ifThen.Operand == RelationalOperands.EqualTo)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   242
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   243
                GenerateExpression(ifThen.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   244
                GenerateExpression(ifThen.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   245
                _ILGenerator.Emit(OpCodes.Ceq);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   246
                _ILGenerator.Emit(OpCodes.Brfalse, lblElse);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   247
                GenerateStatement(ifThen.ThenBody);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   248
                _ILGenerator.Emit(OpCodes.Br, lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   249
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   250
                _ILGenerator.MarkLabel(lblElse);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   251
                GenerateStatement(ifThen.ElseBody);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   252
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   253
                _ILGenerator.MarkLabel(lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   254
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   255
            #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   256
            #region LessThan
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   257
            else if (ifThen.Operand == RelationalOperands.LessThan)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   258
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   259
                GenerateExpression(ifThen.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   260
                GenerateExpression(ifThen.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   261
                _ILGenerator.Emit(OpCodes.Clt);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   262
                _ILGenerator.Emit(OpCodes.Brfalse, lblElse);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   263
                GenerateStatement(ifThen.ThenBody);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   264
                _ILGenerator.Emit(OpCodes.Br, lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   265
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   266
                _ILGenerator.MarkLabel(lblElse);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   267
                GenerateStatement(ifThen.ElseBody);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   268
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   269
                _ILGenerator.MarkLabel(lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   270
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   271
            #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   272
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   273
            #region
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   274
            /*
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   275
            Label gtTrue = il.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   276
            Label gtFalse = il.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   277
            
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   278
            
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   279
             */
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   280
            #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   281
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   282
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   283
        else if (_statement is For)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   284
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   285
            // example: 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   286
            // for x = 0 to 100 do
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   287
            //   print "hello";
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   288
            // end;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   289
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   290
            // x = 0
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   291
            For forLoop = (For)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   292
            Assignment assign = new Assignment();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   293
            assign.Identifier = forLoop.Identifier;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   294
            assign.Expression = forLoop.From;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   295
            GenerateStatement(assign);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   296
            // jump to the test
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   297
            Label test = _ILGenerator.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   298
            _ILGenerator.Emit(OpCodes.Br, test);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   299
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   300
            // body statement
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   301
            Label body = _ILGenerator.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   302
            _ILGenerator.MarkLabel(body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   303
            GenerateStatement(forLoop.Body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   304
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   305
            // increase x
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   306
            _ILGenerator.Emit(OpCodes.Ldloc, tblIdentifier[forLoop.Identifier]);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   307
            _ILGenerator.Emit(OpCodes.Ldc_I4, 1);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   308
            _ILGenerator.Emit(OpCodes.Add);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   309
            SaveIdentifier(forLoop.Identifier, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   310
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   311
            // check if x is equal to 100
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   312
            _ILGenerator.MarkLabel(test);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   313
            _ILGenerator.Emit(OpCodes.Ldloc, tblIdentifier[forLoop.Identifier]);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   314
            GenerateExpression(forLoop.To, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   315
            _ILGenerator.Emit(OpCodes.Blt, body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   316
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   317
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   318
        else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   319
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   320
            ExceptionHandler("unable to generate " + _statement.GetType().Name);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   321
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   322
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   323
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   324
    public void ExceptionHandler(string strException) 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   325
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   326
        throw new Exception(strException);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   327
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   328
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   329
    public void SaveIdentifier(string IdentifierName, System.Type IdentifierType)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   330
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   331
        if (tblIdentifier.ContainsKey(IdentifierName))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   332
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   333
            LocalBuilder locb = tblIdentifier[IdentifierName];
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   334
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   335
            if (locb.LocalType == IdentifierType)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   336
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   337
                _ILGenerator.Emit(OpCodes.Stloc, tblIdentifier[IdentifierName]);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   338
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   339
            else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   340
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   341
                ExceptionHandler("'" + IdentifierName + "' is of type " + locb.LocalType.Name + " but saving in different type " + IdentifierType.Name);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   342
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   343
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   344
        else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   345
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   346
            ExceptionHandler("variable not declared '" + IdentifierName + "'");
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   347
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   348
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   349
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   350
    public void GenerateExpression(Expression _expression, System.Type expressionType)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   351
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   352
        System.Type typeOfExpression;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   353
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   354
        if (_expression is AlphaNumericValue)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   355
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   356
            typeOfExpression = typeof(string);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   357
            _ILGenerator.Emit(OpCodes.Ldstr, ((AlphaNumericValue)_expression).Value);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   358
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   359
        else if (_expression is NumericValue)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   360
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   361
            typeOfExpression = typeof(int);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   362
            _ILGenerator.Emit(OpCodes.Ldc_I4, ((NumericValue)_expression).Value);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   363
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   364
        
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   365
        else if (_expression is ArithmaticExpression) 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   366
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   367
            typeOfExpression = GetExpressionType(_expression);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   368
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   369
            ArithmaticExpression arithmaticExpression = (ArithmaticExpression)_expression;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   370
            GenerateExpression(arithmaticExpression.Left, GetExpressionType(arithmaticExpression.Left));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   371
            GenerateExpression(arithmaticExpression.Right, GetExpressionType(arithmaticExpression.Right));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   372
            if (arithmaticExpression.Operand == ArithmaticOperands.Add) 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   373
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   374
                _ILGenerator.Emit(OpCodes.Add);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   375
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   376
            else if (arithmaticExpression.Operand == ArithmaticOperands.Subtract) 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   377
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   378
                _ILGenerator.Emit(OpCodes.Sub);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   379
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   380
            else if (arithmaticExpression.Operand == ArithmaticOperands.Multiply) 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   381
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   382
                _ILGenerator.Emit(OpCodes.Mul);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   383
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   384
            else if (arithmaticExpression.Operand == ArithmaticOperands.Division) 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   385
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   386
                _ILGenerator.Emit(OpCodes.Div);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   387
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   388
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   389
        
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   390
        
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   391
        else if (_expression is Identifier)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   392
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   393
            string identifier = ((Identifier)_expression).IdentifierName;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   394
            typeOfExpression = GetExpressionType(_expression);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   395
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   396
            if (!tblIdentifier.ContainsKey(identifier))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   397
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   398
                ExceptionHandler("undeclared variable '" + identifier + "'");
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   399
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   400
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   401
            _ILGenerator.Emit(OpCodes.Ldloc, tblIdentifier[identifier]);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   402
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   403
        
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   404
        else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   405
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   406
            ExceptionHandler("can't generate " + _expression.GetType().Name);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   407
            typeOfExpression = null;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   408
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   409
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   410
        if (typeOfExpression != expressionType)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   411
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   412
            if (typeOfExpression == typeof(int) &&
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   413
                expressionType == typeof(string))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   414
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   415
                _ILGenerator.Emit(OpCodes.Box, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   416
                _ILGenerator.Emit(OpCodes.Callvirt, typeof(object).GetMethod("ToString"));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   417
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   418
            else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   419
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   420
                ExceptionHandler("can't convert " + typeOfExpression.Name + " to " + expressionType.Name);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   421
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   422
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   423
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   424
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   425
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   426
    public System.Type GetExpressionType(Expression _expression)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   427
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   428
        if (_expression is AlphaNumericValue)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   429
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   430
            return typeof(string);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   431
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   432
        else if (_expression is NumericValue)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   433
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   434
            return typeof(int);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   435
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   436
        else if (_expression is Identifier)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   437
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   438
            Identifier var = (Identifier)_expression;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   439
            if (tblIdentifier.ContainsKey(var.IdentifierName))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   440
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   441
                LocalBuilder locb = tblIdentifier[var.IdentifierName];
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   442
                return locb.LocalType;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   443
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   444
            else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   445
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   446
                ExceptionHandler("variable not declared '" + var.IdentifierName + "'");
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   447
                return null;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   448
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   449
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   450
        else if (_expression is ArithmaticExpression) 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   451
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   452
            return typeof(ArithmaticExpression);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   453
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   454
        else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   455
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   456
            ExceptionHandler("type cannot be generated for " + _expression.GetType().Name);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   457
            return null;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   458
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   459
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   460
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   461
    #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   462
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   463
    #region Method
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   464
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   465
    System.Reflection.Emit.ILGenerator _ILMethod = null;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   466
    public void GenerateStatementMethod(Statement _statement)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   467
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   468
        #region Statement
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   469
        if (_statement is StatementSequence)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   470
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   471
            StatementSequence _StatementSequence = (StatementSequence)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   472
            GenerateStatementMethod(_StatementSequence.Left);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   473
            GenerateStatementMethod(_StatementSequence.Right);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   474
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   475
        #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   476
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   477
        #region Declare Variable
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   478
        else if (_statement is DeclareVariable)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   479
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   480
            // declare a variable in symbol table
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   481
            DeclareVariable declare = (DeclareVariable)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   482
            tblIdentifier[declare.Identifier] =
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   483
                _ILMethod.DeclareLocal(GetExpressionTypeMethod(declare.Expression));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   484
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   485
            // set the initial value
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   486
            Assignment assign = new Assignment();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   487
            assign.Identifier = declare.Identifier;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   488
            assign.Expression = declare.Expression;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   489
            GenerateStatementMethod(assign);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   490
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   491
        #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   492
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   493
        #region Assignment
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   494
        else if (_statement is Assignment)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   495
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   496
            Assignment assign = (Assignment)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   497
            GenerateExpressionMethod(assign.Expression, GetExpressionTypeMethod(assign.Expression));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   498
            if (GetExpressionTypeMethod(assign.Expression) == typeof(ArithmaticExpression))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   499
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   500
                SaveIdentifierMethod(assign.Identifier, typeof(Int32));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   501
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   502
            else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   503
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   504
                SaveIdentifierMethod(assign.Identifier,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   505
                    GetExpressionTypeMethod(assign.Expression));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   506
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   507
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   508
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   509
        #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   510
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   511
        else if (_statement is DeclareFunction)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   512
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   513
            DeclareFunction _DeclareFunction = (DeclareFunction)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   514
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   515
            string strFunctionName = _DeclareFunction.FunctionName;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   516
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   517
            Type ParameterType1 = GetExpressionTypeMethod(_DeclareFunction.Parameter1.Expression);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   518
            Type ParameterType2 = GetExpressionTypeMethod(_DeclareFunction.Parameter2.Expression);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   519
            Type ParameterType3 = GetExpressionTypeMethod(_DeclareFunction.Parameter3.Expression);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   520
            Type[] InputParameters = { ParameterType1, ParameterType2, ParameterType3 };
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   521
            
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   522
            Type ReturnType = typeof(void);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   523
            if(_DeclareFunction.ReturnType == "void")
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   524
                ReturnType = typeof(void);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   525
            else if (_DeclareFunction.ReturnType == "string")
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   526
                ReturnType = typeof(string);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   527
            else if (_DeclareFunction.ReturnType == "numeric")
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   528
                ReturnType = typeof(int);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   529
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   530
            
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   531
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   532
            //FieldBuilder Parameter1 = _typeBuilder.DefineField(_DeclareFunction.Parameter1.Identifier, ParameterType1, FieldAttributes.Private);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   533
            //FieldBuilder Parameter2 = _typeBuilder.DefineField(_DeclareFunction.Parameter2.Identifier, ParameterType2, FieldAttributes.Private);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   534
            //FieldBuilder Parameter3 = _typeBuilder.DefineField(_DeclareFunction.Parameter3.Identifier, ParameterType3, FieldAttributes.Private);            
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   535
            
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   536
            MethodBuilder NewMethod = 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   537
                _typeBuilder.DefineMethod
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   538
                (strFunctionName,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   539
                MethodAttributes.Static,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   540
                ReturnType,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   541
                InputParameters);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   542
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   543
            //ParameterBuilder poolRefBuilder = NewMethod.DefineParameter(1, ParameterAttributes.In, _DeclareFunction.Parameter1.Identifier);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   544
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   545
            _ILMethod = NewMethod.GetILGenerator();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   546
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   547
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   548
            //tblArguments[_DeclareFunction.Parameter0.Identifier] = _ILMethod.
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   549
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   550
            tblArguments[_DeclareFunction.Parameter1.Identifier] = _ILMethod.DeclareLocal(ParameterType1);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   551
            GenerateExpressionMethod(_DeclareFunction.Parameter1.Expression, ParameterType1);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   552
            //_ILMethod.Emit(OpCodes.Starg, 0);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   553
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   554
            tblArguments[_DeclareFunction.Parameter1.Identifier] = _ILMethod.DeclareLocal(ParameterType2);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   555
            GenerateExpressionMethod(_DeclareFunction.Parameter1.Expression, ParameterType2);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   556
            //_ILMethod.Emit(OpCodes.Starg, 1);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   557
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   558
            tblArguments[_DeclareFunction.Parameter2.Identifier] = _ILMethod.DeclareLocal(ParameterType3);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   559
            GenerateExpressionMethod(_DeclareFunction.Parameter2.Expression, ParameterType3);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   560
            //_ILMethod.Emit(OpCodes.Starg, 2);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   561
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   562
            //GenerateStatementMethod(_DeclareFunction.Body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   563
            //_ILMethod.Emit(OpCodes.Ret);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   564
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   565
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   566
        #region write-read
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   567
        else if (_statement is Write)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   568
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   569
            // for print keyword, call .net method for printscreen
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   570
            GenerateExpressionMethod(((Write)_statement).Expression,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   571
                typeof(string));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   572
            _ILMethod.Emit(OpCodes.Call,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   573
                typeof(System.Console).GetMethod("WriteLine",
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   574
                new System.Type[] { typeof(string) }));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   575
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   576
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   577
        else if (_statement is ReadInput)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   578
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   579
            // call the readline method and parse input method
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   580
            _ILMethod.Emit(OpCodes.Call,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   581
                typeof(System.Console).GetMethod("ReadLine",
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   582
                BindingFlags.Public | BindingFlags.Static,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   583
                null, new System.Type[] { }, null));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   584
            _ILMethod.Emit(OpCodes.Call,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   585
                typeof(int).GetMethod("Parse",
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   586
                BindingFlags.Public | BindingFlags.Static,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   587
                null, new System.Type[] { typeof(string) }, null));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   588
            // store the input value in local builder
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   589
            SaveIdentifierMethod(((ReadInput)_statement).Identifier, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   590
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   591
        #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   592
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   593
        #region While
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   594
        else if (_statement is While)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   595
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   596
            While _while = (While)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   597
            Label lblTest = _ILMethod.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   598
            Label lblEnd = _ILMethod.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   599
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   600
            if (_while.Operand == RelationalOperands.GreaterThan)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   601
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   602
                _ILMethod.MarkLabel(lblTest);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   603
                GenerateExpressionMethod(_while.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   604
                GenerateExpressionMethod(_while.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   605
                _ILMethod.Emit(OpCodes.Cgt);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   606
                _ILMethod.Emit(OpCodes.Brfalse, lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   607
                GenerateStatementMethod(_while.Body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   608
                _ILMethod.Emit(OpCodes.Br, lblTest);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   609
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   610
                _ILMethod.MarkLabel(lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   611
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   612
            else if (_while.Operand == RelationalOperands.EqualTo)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   613
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   614
                _ILMethod.MarkLabel(lblTest);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   615
                GenerateExpressionMethod(_while.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   616
                GenerateExpressionMethod(_while.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   617
                _ILMethod.Emit(OpCodes.Ceq);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   618
                _ILMethod.Emit(OpCodes.Brfalse, lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   619
                GenerateStatementMethod(_while.Body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   620
                _ILMethod.Emit(OpCodes.Br, lblTest);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   621
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   622
                _ILMethod.MarkLabel(lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   623
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   624
            else if (_while.Operand == RelationalOperands.LessThan)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   625
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   626
                _ILMethod.MarkLabel(lblTest);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   627
                GenerateExpressionMethod(_while.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   628
                GenerateExpressionMethod(_while.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   629
                _ILMethod.Emit(OpCodes.Clt);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   630
                _ILMethod.Emit(OpCodes.Brfalse, lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   631
                GenerateStatementMethod(_while.Body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   632
                _ILMethod.Emit(OpCodes.Br, lblTest);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   633
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   634
                _ILMethod.MarkLabel(lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   635
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   636
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   637
        #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   638
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   639
        #region If-Then
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   640
        else if (_statement is IfThen)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   641
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   642
            IfThen ifThen = (IfThen)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   643
            Label lblElse = _ILMethod.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   644
            Label lblEnd = _ILMethod.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   645
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   646
            #region GreaterThan
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   647
            if (ifThen.Operand == RelationalOperands.GreaterThan)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   648
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   649
                GenerateExpressionMethod(ifThen.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   650
                GenerateExpressionMethod(ifThen.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   651
                _ILMethod.Emit(OpCodes.Cgt);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   652
                _ILMethod.Emit(OpCodes.Brfalse, lblElse);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   653
                GenerateStatementMethod(ifThen.ThenBody);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   654
                _ILMethod.Emit(OpCodes.Br, lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   655
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   656
                _ILMethod.MarkLabel(lblElse);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   657
                GenerateStatementMethod(ifThen.ElseBody);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   658
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   659
                _ILMethod.MarkLabel(lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   660
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   661
            #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   662
            #region EqualTo
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   663
            else if (ifThen.Operand == RelationalOperands.EqualTo)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   664
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   665
                GenerateExpressionMethod(ifThen.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   666
                GenerateExpressionMethod(ifThen.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   667
                _ILMethod.Emit(OpCodes.Ceq);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   668
                _ILMethod.Emit(OpCodes.Brfalse, lblElse);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   669
                GenerateStatementMethod(ifThen.ThenBody);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   670
                _ILMethod.Emit(OpCodes.Br, lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   671
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   672
                _ILMethod.MarkLabel(lblElse);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   673
                GenerateStatementMethod(ifThen.ElseBody);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   674
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   675
                _ILMethod.MarkLabel(lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   676
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   677
            #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   678
            #region LessThan
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   679
            else if (ifThen.Operand == RelationalOperands.LessThan)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   680
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   681
                GenerateExpressionMethod(ifThen.LeftExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   682
                GenerateExpressionMethod(ifThen.RightExpression, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   683
                _ILMethod.Emit(OpCodes.Clt);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   684
                _ILMethod.Emit(OpCodes.Brfalse, lblElse);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   685
                GenerateStatementMethod(ifThen.ThenBody);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   686
                _ILMethod.Emit(OpCodes.Br, lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   687
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   688
                _ILMethod.MarkLabel(lblElse);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   689
                GenerateStatementMethod(ifThen.ElseBody);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   690
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   691
                _ILMethod.MarkLabel(lblEnd);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   692
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   693
            #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   694
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   695
        #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   696
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   697
        #region for
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   698
        else if (_statement is For)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   699
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   700
            For forLoop = (For)_statement;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   701
            Assignment assign = new Assignment();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   702
            assign.Identifier = forLoop.Identifier;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   703
            assign.Expression = forLoop.From;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   704
            GenerateStatementMethod(assign);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   705
            
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   706
            Label test = _ILMethod.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   707
            _ILMethod.Emit(OpCodes.Br, test);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   708
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   709
            Label body = _ILMethod.DefineLabel();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   710
            _ILMethod.MarkLabel(body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   711
            GenerateStatementMethod(forLoop.Body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   712
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   713
            _ILMethod.Emit(OpCodes.Ldloc, tblIdentifier[forLoop.Identifier]);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   714
            _ILMethod.Emit(OpCodes.Ldc_I4, 1);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   715
            _ILMethod.Emit(OpCodes.Add);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   716
            SaveIdentifierMethod(forLoop.Identifier, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   717
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   718
            _ILMethod.MarkLabel(test);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   719
            _ILMethod.Emit(OpCodes.Ldloc, tblIdentifier[forLoop.Identifier]);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   720
            GenerateExpressionMethod(forLoop.To, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   721
            _ILMethod.Emit(OpCodes.Blt, body);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   722
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   723
        #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   724
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   725
        else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   726
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   727
            ExceptionHandler("unable to generate " + _statement.GetType().Name);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   728
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   729
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   730
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   731
    public void SaveIdentifierMethod(string IdentifierName, System.Type IdentifierType)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   732
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   733
        if (tblIdentifier.ContainsKey(IdentifierName))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   734
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   735
            LocalBuilder locb = tblIdentifier[IdentifierName];
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   736
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   737
            if (locb.LocalType == IdentifierType)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   738
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   739
                _ILMethod.Emit(OpCodes.Stloc, tblIdentifier[IdentifierName]);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   740
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   741
            else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   742
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   743
                ExceptionHandler("'" + IdentifierName + "' is of type " + locb.LocalType.Name + " but saving in different type " + IdentifierType.Name);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   744
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   745
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   746
        else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   747
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   748
            ExceptionHandler("variable not declared '" + IdentifierName + "'");
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   749
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   750
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   751
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   752
    public void DeclareArgument() { }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   753
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   754
    public void GenerateExpressionMethod(Expression _expression, System.Type expressionType)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   755
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   756
        System.Type typeOfExpression;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   757
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   758
        if (_expression is AlphaNumericValue)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   759
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   760
            typeOfExpression = typeof(string);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   761
            _ILMethod.Emit(OpCodes.Ldstr, ((AlphaNumericValue)_expression).Value);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   762
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   763
        else if (_expression is NumericValue)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   764
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   765
            typeOfExpression = typeof(int);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   766
            _ILMethod.Emit(OpCodes.Ldc_I4, ((NumericValue)_expression).Value);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   767
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   768
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   769
        else if (_expression is ArithmaticExpression)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   770
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   771
            typeOfExpression = GetExpressionTypeMethod(_expression);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   772
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   773
            ArithmaticExpression arithmaticExpression = (ArithmaticExpression)_expression;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   774
            GenerateExpressionMethod(arithmaticExpression.Left, GetExpressionTypeMethod(arithmaticExpression.Left));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   775
            GenerateExpressionMethod(arithmaticExpression.Right, GetExpressionTypeMethod(arithmaticExpression.Right));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   776
            if (arithmaticExpression.Operand == ArithmaticOperands.Add)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   777
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   778
                _ILMethod.Emit(OpCodes.Add);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   779
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   780
            else if (arithmaticExpression.Operand == ArithmaticOperands.Subtract)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   781
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   782
                _ILMethod.Emit(OpCodes.Sub);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   783
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   784
            else if (arithmaticExpression.Operand == ArithmaticOperands.Multiply)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   785
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   786
                _ILMethod.Emit(OpCodes.Mul);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   787
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   788
            else if (arithmaticExpression.Operand == ArithmaticOperands.Division)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   789
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   790
                _ILMethod.Emit(OpCodes.Div);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   791
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   792
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   793
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   794
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   795
        else if (_expression is Identifier)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   796
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   797
            string identifier = ((Identifier)_expression).IdentifierName;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   798
            typeOfExpression = GetExpressionTypeMethod(_expression);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   799
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   800
            if (!tblIdentifier.ContainsKey(identifier) && !tblArguments.ContainsKey(identifier))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   801
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   802
                ExceptionHandler("undeclared variable '" + identifier + "'");
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   803
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   804
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   805
            if (tblIdentifier.ContainsKey(identifier))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   806
                _ILMethod.Emit(OpCodes.Ldloc, tblIdentifier[identifier]);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   807
            else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   808
                _ILMethod.Emit(OpCodes.Ldloc, tblArguments[identifier]);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   809
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   810
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   811
        else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   812
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   813
            ExceptionHandler("can't generate " + _expression.GetType().Name);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   814
            typeOfExpression = null;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   815
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   816
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   817
        if (typeOfExpression != expressionType)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   818
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   819
            if (typeOfExpression == typeof(int) &&
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   820
                expressionType == typeof(string))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   821
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   822
                _ILMethod.Emit(OpCodes.Box, typeof(int));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   823
                _ILMethod.Emit(OpCodes.Callvirt, typeof(object).GetMethod("ToString"));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   824
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   825
            else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   826
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   827
                ExceptionHandler("can't convert " + typeOfExpression.Name + " to " + expressionType.Name);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   828
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   829
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   830
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   831
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   832
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   833
    public System.Type GetExpressionTypeMethod(Expression _expression)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   834
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   835
        if (_expression is AlphaNumericValue)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   836
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   837
            return typeof(string);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   838
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   839
        else if (_expression is NumericValue)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   840
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   841
            return typeof(int);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   842
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   843
        else if (_expression is Identifier)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   844
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   845
            Identifier var = (Identifier)_expression;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   846
            if (tblIdentifier.ContainsKey(var.IdentifierName))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   847
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   848
                LocalBuilder locb = tblIdentifier[var.IdentifierName];
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   849
                return locb.LocalType;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   850
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   851
            else if (tblArguments.ContainsKey(var.IdentifierName))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   852
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   853
                LocalBuilder locb = tblArguments[var.IdentifierName];
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   854
                return locb.LocalType;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   855
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   856
            else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   857
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   858
                ExceptionHandler("variable not declared '" + var.IdentifierName + "'");
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   859
                return null;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   860
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   861
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   862
        else if (_expression is ArithmaticExpression)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   863
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   864
            return typeof(ArithmaticExpression);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   865
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   866
        else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   867
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   868
            ExceptionHandler("type cannot be generated for " + _expression.GetType().Name);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   869
            return null;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   870
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   871
    }    
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   872
    #endregion
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   873
}
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   874