示例#1
0
        internal Parser(icode scr, Expr expr)
            : this()
        {
            _scanner = scr;
            if (_expressionScopeStack == null)
            {
                _expressionScopeStack = new Stack<Expr>();
            }

            _expressionScopeStack.Push(expr);
        }
        /// <summary>
        /// Parses the If statment
        /// </summary>
        internal void ParseIfStatementDeclaration(out Expr expr)
        {
            expr = null;
            var ifKeyWord = _scanner.GetToken();
            if (!ifKeyWord.IsReserverdWord())
            {
                throw new NireExecutionException("If key word not found... Parse broke");
            }

            var lparen = _scanner.GetToken();

            using (new ParserContext(ParserContextEnum.IfDecl))
            {
                expr = ParseIfCondition(expr, lparen);

                var bracket = _scanner.GetToken();

                if (bracket.Kind == ByteCodeIdentifiers.TokenCode.LBracket)
                {
                    using (new ParserContext(ParserContextEnum.IfBody))
                    {
                        bracket = ParseToRightBracket(expr, bracket, boe.Peek());
                        boe.Pop();
                    }

                    var isElse = _scanner.Peek();
                    if (isElse.IsReserverdWord() && isElse.IdentiferName.Equals("else"))
                    {
                        _scanner.GetToken();
                        var isIf = _scanner.Peek();
                        if (isIf.IsReserverdWord() && isIf.IdentiferName.Equals("if"))
                        {
                            using (new ParserContext(ParserContextEnum.ElseIfBody))
                            {
                                ParseStatement();
                            }
                        }
                        else
                        {
                            bracket = _scanner.GetToken();
                            if (bracket.Kind == ByteCodeIdentifiers.TokenCode.LBracket)
                            {
                                using (new ParserContext(ParserContextEnum.ElseBody))
                                {
                                    var trueExpression = new BinaryOperatorExpression(true);
                                    ParseToRightBracket(expr, bracket, trueExpression);
                                }
                            }
                        }
                    }
                }
            }
        }
示例#3
0
        internal void InitGenerator(Expr expr)
        {
            ProgramDeclBlock pdb = expr as ProgramDeclBlock;
              string name = pdb.FunctionName;

              Generate(name, "exe", expr);
              GenerateFunctionDeclartion(
            expr,
            assemblyBuilder,
            fullyQualifiedModuleName);

              foreach (Expr statement in expr.Statements)
              {
            if (statement is BlockDeclExpr)
            {
              var t = (BlockDeclExpr) statement;
              if (t.IsMain && generatedMainMethod)
              {
            continue;
              }
              else
              {
            GenerateFunctionDeclartion(
              statement,
              assemblyBuilder,
              name);
              }
            }
            else
            {
              GenerateFunctionDeclartion(
            statement,
            assemblyBuilder,
            name);
            }
              }

              SaveAssembly(
            fullyQualifiedModuleName,
            assemblyBuilder,
            typeBuilder);
        }
        private Expr ParseIfCondition(Expr expr, Token lparen)
        {
            if (_expressionScopeStack.Peek() is IfDeclExpr)
            {
                expr = _expressionScopeStack.Peek();
            }
            else
            {
                expr = new IfDeclExpr();
                _expressionScopeStack.Push(expr);
            }
            if (lparen.Kind == ByteCodeIdentifiers.TokenCode.LParen)
            {
                ParseStatementExpression();
                this.boe.Push(this.BinaryExpression);
                ((IfDeclExpr)expr).AddCondition(this.BinaryExpression);
            }

            return expr;
        }
        internal void AddStatment(Expr expr)
        {
            if (this.statementList == null)
            {
                this.statementList = new List<Expr>();
            }

            this.statementList.Add(expr);
        }
示例#6
0
 internal void ParseReservedWord(Dictionary<string, Parser.ParseType> types, out Expr expr)
 {
     ReservedWords.ParseKeyWord(types, this.identiferName, out expr);
 }
示例#7
0
        private void GenerateFunctionDeclartion(
      Expr expr,
      AssemblyBuilder assemblyBuilder,
      string moduleName)
        {
            GenerateFunction(
            typeBuilder,
            expr);

              //SaveAssembly(
              //    fullyQualifiedModuleName,
              //    assemblyBuilder,
              //    typeBuilder);
        }
示例#8
0
        private void WalkTree(Expr boe)
        {
            if (boe != null)
            {
                try
                {
                    var tboe = boe as BinaryOperatorExpression;
                    if (tboe != null)
                    {
                        WalkTree(((BinaryOperatorExpression)boe).Left);
                        Console.WriteLine(((BinaryOperatorExpression)boe).Operation.GetToken().IdentiferName);
                        WalkTree(((BinaryOperatorExpression)boe).Right);
                    }

                    var ie = boe as IdentifierExpression;
                    if (ie != null)
                    {
                        Console.WriteLine(boe.GetToken().IdentiferName);
                    }

                    var fce = boe as FunctionCallExpression;
                    if (fce != null)
                    {
                        Console.WriteLine(fce.FunctionName);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
示例#9
0
 internal ArithmeticBinaryExpr(OperatorExpression operation,
     Expr left,
     Expr right)
     : base(operation, left, right)
 {
 }
示例#10
0
        internal static void ParseKeyWord(Dictionary<string,Parser.ParseType> types, string token, out Expr expr)
        {
            int i = GetResWordCommand(token);

            string command = commandWordArray[i];
            expr = null;
            types[command](out expr);
        }
示例#11
0
 private void ParseBoolDeclartion(out Expr expr)
 {
     expr = null;
 }
示例#12
0
 private void AddStatementExpressionTreesToFunctionBlock(Expr bin)
 {
     var scopeExpr = _expressionScopeStack.Peek();
     if (scopeExpr != null)
     {
         scopeExpr.AddStatment(bin);
     }
 }
示例#13
0
 internal void ParseWhileStatementDeclaration(out Expr expr)
 {
     expr = null;
 }
示例#14
0
        private void Generate(
      string moduleName,
      string ext,
      Expr expr)
        {
            fullyQualifiedModuleName = moduleName + "." + ext;
              // create a dynamic assembly and module
              AssemblyName assemblyName = new AssemblyName();
              assemblyName.Name = moduleName;
              this.assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
              this.module = assemblyBuilder.DefineDynamicModule(fullyQualifiedModuleName, true);

              string typeName = moduleName + "Type";
              typeBuilder = module.DefineType(
            typeName,
            TypeAttributes.Public | TypeAttributes.Class);

              //MethodBuilder methodbuilder = null;
              //BlockDeclExpr blockDec = expr as BlockDeclExpr;

              //GenerateFunction(typeBuilder, out methodbuilder, expr);
        }
示例#15
0
        private Type[] GetFunctionParameterTypes(Expr expr)
        {
            if (expr != null)
              {
            if (expr is ArrayDeclExpr)
            {
              string identType = ((ArrayDeclExpr) expr).TokenType.IdentiferName;

              Type mapValue = null;
              if (typeMap.TryGetValue(identType, out mapValue))
              {
            return new Type[] {mapValue};
              }
              else
              {
            // TODO need to add code to support user defined types.
              }
            }
              }

              return null;
        }
 internal IfDeclExpr(Expr ifExpr)
 {
     this.ifExpr = ifExpr;
 }
        /// <summary>
        /// Adds the body statements to the condition map
        /// </summary>
        /// <param name="boe">binary expression</param>
        /// <param name="stmt">expression statement</param>
        internal void AddStatementToCondition(BinaryOperatorExpression boe, Expr stmt)
        {
            if (ParserContext.Context == ParserContextEnum.ElseBody)
            {
                if (!conditionExprMap.ContainsKey(boe))
                {
                    this.AddCondition(boe);
                    conditionExprMap[boe].Add(stmt);
                    return;
                }
            }

            conditionExprMap[boe].Add(stmt);
        }
示例#18
0
 private void ParseCharDeclaration(out Expr expr)
 {
     expr = null;
 }
示例#19
0
 internal BinaryOperatorExpression(
     OperatorExpression operation,
     Expr left,
     Expr right)
     : this()
 {
     this.left = left;
     this.right = right;
     this.operation = operation;
 }
示例#20
0
 private void ParseFloatDeclartion(out Expr expr)
 {
     expr = null;
 }
示例#21
0
 internal AssignmentExpr(OperatorExpression operation,
     Expr left,
     Expr right)
     : base(operation, left, right)
 {
 }
示例#22
0
        /// <summary>
        /// Parse the signature of the function
        /// </summary>
        private void ParseFunctionSignature(
            Expr parentNode,
            Token functionName)
        {
            List<Expr> pll = null;
            functionName.Kind = ByteCodeIdentifiers.TokenCode.Function;

            Token paren = _scanner.GetToken();

            /*
            * If there are parameters
            * add them to the parameter list
            */
            if (paren.Value.Equals(constants.LEFTPAREN))
            {
                pll = ParseFunctionParameters();
            }

            if (pll != null && pll.Count > 0)
            {
                var progExpression = parentNode as BlockDeclExpr;
                if (progExpression != null)
                {
                    progExpression.Parameters = pll;
                }

                if (functionName.IdentiferName.Equals(constants.MAIN, StringComparison.OrdinalIgnoreCase))
                {
                    _isMain = true;
                    progExpression.IsMain = true;
                }
            }
        }
 private Token ParseToRightBracket(Expr expr, Token bracket, BinaryOperatorExpression boe)
 {
     while (bracket.Kind != ByteCodeIdentifiers.TokenCode.RBracket)
     {
         ParseStatementExpression();
         ((IfDeclExpr)expr).AddStatementToCondition(boe, this.BinaryExpression);
         bracket = _scanner.GetToken();
     }
     return bracket;
 }
示例#24
0
        private void ParseIntDeclaration(out Expr expr)
        {
            Token ident = _scanner.GetToken();
            if (ident.Kind == ByteCodeIdentifiers.TokenCode.SquareBracketLeft)
            {

            }
            else
            {
                ident = _scanner.GetToken();
            }

            expr = new IdentifierExpression(ident);
        }
示例#25
0
        private void ParseStringDeclartion(out Expr expr)
        {
            Token ident = _scanner.GetToken();
            Token leftBracket = _scanner.GetToken();

            expr = null;

            if (leftBracket.Kind == ByteCodeIdentifiers.TokenCode.SquareBracketLeft)
            {
                Token rightBracket = _scanner.GetToken();

                if (rightBracket.Kind != ByteCodeIdentifiers.TokenCode.SquareBracketRight)
                {
                    throw new NireExecutionException("No right bracket for array declaration");
                }

                expr = ArrayDeclaration(ident);
            }
            else
            {
                ident = _scanner.GetToken();
                expr = new StringDeclExpr(ident);
            }
        }
示例#26
0
        private void GenerateFunction(
      TypeBuilder typeBuilder,
      Expr expr)
        {
            ProgramDeclBlock pdb = expr as ProgramDeclBlock;
              if (pdb != null)
              {
            foreach (var function in pdb.Statements)
            {
              var functionDecl = function as BlockDeclExpr;
              if (functionDecl != null)
              {
            bool isMain = functionDecl.IsMain;
            var parameters = functionDecl.Parameters;

            if (isMain && !generatedMainMethod)
            {
              generatedMainMethod = true;

              GenerateMainMethod(
                typeBuilder,
                functionDecl,
                parameters);
            }

            foreach (var statement in function.Statements)
            {
              BinaryOperatorExpression bin = statement as BinaryOperatorExpression;
              if (bin != null)
              {
                GenerateBinaryExpression(typeBuilder, bin);
              }
            }

            this.methodbuilder.GetILGenerator().Emit(OpCodes.Ret);
              }
            }
              }
        }