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); } } } } } } }
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); }
internal void ParseReservedWord(Dictionary<string, Parser.ParseType> types, out Expr expr) { ReservedWords.ParseKeyWord(types, this.identiferName, out expr); }
private void GenerateFunctionDeclartion( Expr expr, AssemblyBuilder assemblyBuilder, string moduleName) { GenerateFunction( typeBuilder, expr); //SaveAssembly( // fullyQualifiedModuleName, // assemblyBuilder, // typeBuilder); }
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); } } }
internal ArithmeticBinaryExpr(OperatorExpression operation, Expr left, Expr right) : base(operation, left, right) { }
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); }
private void ParseBoolDeclartion(out Expr expr) { expr = null; }
private void AddStatementExpressionTreesToFunctionBlock(Expr bin) { var scopeExpr = _expressionScopeStack.Peek(); if (scopeExpr != null) { scopeExpr.AddStatment(bin); } }
internal void ParseWhileStatementDeclaration(out Expr expr) { expr = null; }
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); }
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); }
private void ParseCharDeclaration(out Expr expr) { expr = null; }
internal BinaryOperatorExpression( OperatorExpression operation, Expr left, Expr right) : this() { this.left = left; this.right = right; this.operation = operation; }
private void ParseFloatDeclartion(out Expr expr) { expr = null; }
internal AssignmentExpr(OperatorExpression operation, Expr left, Expr right) : base(operation, left, right) { }
/// <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; }
private void ParseIntDeclaration(out Expr expr) { Token ident = _scanner.GetToken(); if (ident.Kind == ByteCodeIdentifiers.TokenCode.SquareBracketLeft) { } else { ident = _scanner.GetToken(); } expr = new IdentifierExpression(ident); }
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); } }
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); } } } }