void generate() { List <ParameterExpression> vars = VarExpressionsList.GetList(); if (_isArray) { for (int i = 0; i < vars.Count; i++) { if (_identifier.value == vars[i].Name) { bool isConst = true; for (int j = 0; j < vars.Count; j++) { if (_elementIndex.value == vars[j].Name) { isConst = false; ExpressionsList.AddExpression(Expression.Call(null, typeof(Console).GetMethod("Write", new System.Type[] { typeof(String) }), Expression.Constant(vars[i].Name))); ExpressionsList.AddExpression(Expression.Call(null, typeof(Console).GetMethod("Write", new System.Type[] { typeof(String) }), Expression.Constant("["))); ExpressionsList.AddExpression(Expression.Call(null, typeof(Console).GetMethod("Write", new System.Type[] { typeof(int) }), vars[j])); ExpressionsList.AddExpression(Expression.Call(null, typeof(Console).GetMethod("Write", new System.Type[] { typeof(String) }), Expression.Constant("]"))); Expression constant = Expression.Call(typeof(Console).GetMethod("ReadLine")); constant = Expression.Call(typeof(Int32).GetMethod("Parse", new System.Type[] { typeof(string) }), constant); ExpressionsList.AddExpression(Expression.Assign(Expression.ArrayAccess(vars[i], vars[j]), constant)); break; } } if (isConst) { ExpressionsList.AddExpression(Expression.Call(null, typeof(Console).GetMethod("Write", new System.Type[] { typeof(String) }), Expression.Constant(vars[i].Name))); ExpressionsList.AddExpression(Expression.Call(null, typeof(Console).GetMethod("Write", new System.Type[] { typeof(String) }), Expression.Constant("["))); ExpressionsList.AddExpression(Expression.Call(null, typeof(Console).GetMethod("Write", new System.Type[] { typeof(int) }), Expression.Constant(Int32.Parse(_elementIndex.value)))); ExpressionsList.AddExpression(Expression.Call(null, typeof(Console).GetMethod("Write", new System.Type[] { typeof(String) }), Expression.Constant("]"))); ExpressionsList.AddExpression(Expression.Call(null, typeof(Console).GetMethod("Write", new System.Type[] { typeof(String) }), Expression.Constant(": "))); Expression constant = Expression.Call(typeof(Console).GetMethod("ReadLine")); constant = Expression.Call(typeof(Int32).GetMethod("Parse", new System.Type[] { typeof(string) }), constant); ExpressionsList.AddExpression(Expression.Assign(Expression.ArrayAccess(vars[i], Expression.Constant(Int32.Parse(_elementIndex.value))), constant)); } } } } else { for (int i = 0; i < vars.Count; i++) { if (_identifier.value == vars[i].Name) { ExpressionsList.AddExpression(Expression.Call(null, typeof(Console).GetMethod("Write", new System.Type[] { typeof(String) }), Expression.Constant(vars[i].Name))); ExpressionsList.AddExpression(Expression.Call(null, typeof(Console).GetMethod("Write", new System.Type[] { typeof(String) }), Expression.Constant(": "))); Expression constant = Expression.Call(typeof(Console).GetMethod("ReadLine")); constant = Expression.Call(typeof(Int32).GetMethod("Parse", new System.Type[] { typeof(string) }), constant); ExpressionsList.AddExpression(Expression.Assign(vars[i], constant)); return; } } } }
void generateRightExpression(Expression left) { LabelTarget label = Expression.Label(typeof(void)); int i = ExpressionsList.GetList().Count; foreach (ITree node in _rightExpression) { if (node.getMethodName() == Constants.ASSIGNMENT_STATMENT) { AssignmentProcessor assignmenterProcessor = new AssignmentProcessor(); assignmenterProcessor.process((AssignmentStatment)node); } else if (node.getMethodName() == Constants.READ_STATMENT) { ReaderProcessor readerProcessor = new ReaderProcessor(); readerProcessor.process((ReadStatment)node); } else if (node.getMethodName() == Constants.WRITE_STATMENT) { WriterProcessor writeProcessor = new WriterProcessor(); writeProcessor.process((WriteStatment)node); } else if (node.getMethodName() == Constants.IF_STATMENT) { IfStatmentProcessor ifStatmentProcessor = new IfStatmentProcessor(); ifStatmentProcessor.process((IfStatment)node); } else if (node.getMethodName() == Constants.WHILE_STATMENT) { WhileProcessor whileProcessor = new WhileProcessor(); whileProcessor.process((WhileStatment)node); } } List <Expression> list = new List <Expression>(); int x = i; while (ExpressionsList.GetList().Count != i) { list.Add(ExpressionsList.GetElement(x)); ExpressionsList.RemoveElement(x); } Expression rigth = Expression.Block(list); var exitLabel = Expression.Label(); var block = Expression.Loop( Expression.IfThenElse( left, rigth, Expression.Break(label)), label); ExpressionsList.AddExpression(block); }
void generate() { Expression left = generateLeftExpression(); Expression block = generateRightExpression(_thenExpression); if (isElseAppear) { ExpressionsList.AddExpression(Expression.IfThenElse(left, block, generateRightExpression(_elseExpression))); } else { ExpressionsList.AddExpression(Expression.IfThen(left, block)); } }
void processStamentPart(StatmentPart statmentPart) { AppDomain domain = AppDomain.CurrentDomain; AssemblyName assemblyName = new AssemblyName(Program.programName); AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave, "./"); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, assemblyName.Name + ".exe"); TypeBuilder typeBuilder = moduleBuilder.DefineType("Project.Program", TypeAttributes.Public | TypeAttributes.AutoClass | TypeAttributes.BeforeFieldInit); MethodBuilder methodBuilder = typeBuilder.DefineMethod("Main", MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig); StatmentPartProcessor statmentPartProcessor = new StatmentPartProcessor(); Statment statment = (Statment)statmentPart.getTokensList()[0]; foreach (ITree node in statment.getTokensList()) { if (node.getMethodName() == Constants.ASSIGNMENT_STATMENT) { AssignmentProcessor assignmenterProcessor = new AssignmentProcessor(); assignmenterProcessor.process((AssignmentStatment)node); } else if (node.getMethodName() == Constants.READ_STATMENT) { ReaderProcessor readerProcessor = new ReaderProcessor(); readerProcessor.process((ReadStatment)node); } else if (node.getMethodName() == Constants.WRITE_STATMENT) { WriterProcessor writeProcessor = new WriterProcessor(); writeProcessor.process((WriteStatment)node); } else if (node.getMethodName() == Constants.IF_STATMENT) { IfStatmentProcessor ifStatmentProcessor = new IfStatmentProcessor(); ifStatmentProcessor.process((IfStatment)node); } else if (node.getMethodName() == Constants.WHILE_STATMENT) { WhileProcessor whileProcessor = new WhileProcessor(); whileProcessor.process((WhileStatment)node); } } ExpressionsList.AddExpression(Expression.Call(typeof(Console).GetMethod("Read"))); List <Expression> list = ExpressionsList.GetList(); BlockExpression blockExpression = Expression.Block(VarExpressionsList.GetList(), ExpressionsList.GetList()); Expression.Lambda <Action>(blockExpression).CompileToMethod(methodBuilder); assemblyBuilder.SetEntryPoint(methodBuilder); typeBuilder.CreateType(); assemblyBuilder.Save(assemblyName.Name + ".exe"); }
Expression generateRightExpression(List <object> expression) { int i = ExpressionsList.GetList().Count; foreach (ITree node in expression) { if (node.getMethodName() == Constants.ASSIGNMENT_STATMENT) { AssignmentProcessor assignmenterProcessor = new AssignmentProcessor(); assignmenterProcessor.process((AssignmentStatment)node); } else if (node.getMethodName() == Constants.READ_STATMENT) { ReaderProcessor readerProcessor = new ReaderProcessor(); readerProcessor.process((ReadStatment)node); } else if (node.getMethodName() == Constants.WRITE_STATMENT) { WriterProcessor writeProcessor = new WriterProcessor(); writeProcessor.process((WriteStatment)node); } else if (node.getMethodName() == Constants.IF_STATMENT) { IfStatmentProcessor ifStatmentProcessor = new IfStatmentProcessor(); ifStatmentProcessor.process((IfStatment)node); } else if (node.getMethodName() == Constants.WHILE_STATMENT) { WhileProcessor whileProcessor = new WhileProcessor(); whileProcessor.process((WhileStatment)node); } } List <Expression> list = new List <Expression>() { Expression.Empty() }; int x = i; while (ExpressionsList.GetList().Count != i) { list.Add(ExpressionsList.GetElement(x)); ExpressionsList.RemoveElement(x); } return(Expression.Block(list)); }
void generate()//генерируй здесь код { ParameterExpression ex; if (_isArray) { ex = Expression.Variable(typeof(int[]), identifier.value); VarExpressionsList.AddExpression(ex); ExpressionsList.AddExpression(ex); //добавить ренжу ExpressionsList.AddExpression(Expression.Assign(ex, Expression.NewArrayBounds(typeof(int), Expression.Constant(Int32.Parse(_length.value))))); } else { ex = Expression.Variable(typeof(int), identifier.value); VarExpressionsList.AddExpression(ex); ExpressionsList.AddExpression(ex); } }
void generate() { List <ParameterExpression> vars = VarExpressionsList.GetList(); if (!_isArrayElementLeft) { //Array Expression left = GetConst(_leftOp.value); for (int i = 0; i < vars.Count; i++) { if (_leftOp.value == vars[i].Name) { if (vars[i].Type == typeof(int[])) { for (int j = 0; j < _rightOp.Count; j++) { ExpressionsList.AddExpression(Expression.Assign(Expression.ArrayAccess(vars[i], Expression.Constant(j)), Expression.Constant(Int32.Parse(_rightOp[j].value)))); } return; } else { Expression result = GetConst(_rightOp[0].value); for (int j = 0; j < _rightOp.Count - 1; j += 2) { Expression rigth = GetConst(_rightOp[j + 2].value); if (_rightOp[j + 1].value == "[") { result = Expression.ArrayAccess(GetConst(_rightOp[j].value), GetConst(_rightOp[j + 2].value)); j = j + 2; } switch (_rightOp[j + 1].value) { case "+": result = Expression.Add(result, GetConst(_rightOp[j + 2].value)); break; case "-": result = Expression.Subtract(result, GetConst(_rightOp[j + 2].value)); break; case "*": result = Expression.Multiply(result, GetConst(_rightOp[j + 2].value)); break; case "%": result = Expression.Modulo(result, GetConst(_rightOp[j + 2].value)); break; case "/": result = Expression.Divide(result, GetConst(_rightOp[j + 2].value)); break; } } ExpressionsList.AddExpression(Expression.Assign(vars[i], result)); return; } } } } else { Expression left = Expression.ArrayAccess(GetConst(_leftOp.value), GetConst(_elementIndex.value)); Expression result = GetConst(_rightOp[0].value); for (int j = 0; j < _rightOp.Count - 1; j += 2) { Expression rigth = GetConst(_rightOp[j + 2].value); if (_rightOp[j + 1].value == "[") { result = Expression.ArrayAccess(GetConst(_rightOp[j].value), GetConst(_rightOp[j + 2].value)); j = j + 2; } switch (_rightOp[j + 1].value) { case "+": result = Expression.Add(result, GetConst(_rightOp[j + 2].value)); break; case "-": result = Expression.Subtract(result, GetConst(_rightOp[j + 2].value)); break; case "*": result = Expression.Multiply(result, GetConst(_rightOp[j + 2].value)); break; case "%": result = Expression.Modulo(result, GetConst(_rightOp[j + 2].value)); break; case "/": result = Expression.Divide(result, GetConst(_rightOp[j + 2].value)); break; } } ExpressionsList.AddExpression(Expression.Assign(left, result)); return; } }