コード例 #1
0
ファイル: Assign.cs プロジェクト: gtwijma/ExpressionBuilder
		public Expression ToExpression(ParseContext context)
		{
			switch (AssignType)
			{
				case (AssignementOperator.Assign):
				{
					//if (RValue.ParsedType.IsValueType)
					{
						return Expression.Assign(LValue.ToExpression(context),Expression.Convert(RValue.ToExpression(context),LValue.ParsedType));
					}
					//return Expression.Assign(LValue.ToExpression(context), RValue.ToExpression(context));
				}
				case (AssignementOperator.MultiplyAssign):
					return Expression.MultiplyAssign(LValue.ToExpression(context), RValue.ToExpression(context));
				case (AssignementOperator.SubtractAssign):
					return Expression.SubtractAssign(LValue.ToExpression(context), RValue.ToExpression(context));
				case (AssignementOperator.SumAssign):
					{
						if (LValue.ParsedType == typeof(string) || RValue.ParsedType == typeof(string))
						{
							Expression<Func<object, object, string>> func = ((a, b) => SumAssign(a, b));
							return Expression.Assign(LValue.ToExpression(context),
																Expression.Invoke(func, LValue.ToExpression(context), RValue.ToExpression(context)));

						}
						return Expression.AddAssign(LValue.ToExpression(context), RValue.ToExpression(context));
					}
			}
			throw new InvalidEnumArgumentException();
		}
コード例 #2
0
ファイル: Assign.cs プロジェクト: gtwijma/ExpressionBuilder
		public void PreParseExpression(ParseContext context)
		{
			RValue.PreParseExpression(context);
			LValue.PreParseExpression(context);

			ParsedType = LValue.ParsedType;
		}
コード例 #3
0
		public void PreParseExpression(ParseContext context)
		{
			_paramTypes = new List<Type>();
			for (int i = 0; i < Parameters.Length; i++)
			{
				Parameters[i].PreParseExpression(context);
				_paramTypes.Add(Parameters[i].ParsedType);
			}

			Type type = StaticDataType;
			if (StaticDataType == null)
			{
				type = Variable.ParsedType;
				if (Variable is OperationVariable)
				{
					var variable = context.GetVariable(((OperationVariable)Variable).Name);
					type = variable.DataType;
				}
			}

			var methodInfo = type.GetMethod(
				MethodName,
				BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |BindingFlags.Static,
				null,
				_paramTypes.ToArray(),
				null);
			ParsedType = methodInfo.ReturnType;
		}
コード例 #4
0
		public void PreParseExpression(ParseContext context)
		{
			if (_value == null)
				ParsedType = typeof(object);
			else
				ParsedType = _value.GetType();
		}
コード例 #5
0
		public override void PreParseExpression(ParseContext context)
		{
			RValue.PreParseExpression(context);
			_rType = RValue.ParsedType;
			LValue.PreParseExpression(context);
			_lType = LValue.ParsedType;
			ParsedType = typeof(bool);
		}
コード例 #6
0
		public override void PreParseExpression(ParseContext context)
		{
			for (int i = 0; i < SubConditions.Count; i++)
			{
				SubConditions[i].PreParseExpression(context);
			}
			ParsedType = typeof(bool);
		}
コード例 #7
0
		internal Expression DefaultInitialize(ParseContext context)
		{
			if (VariableDeclaration.Expression == null)
			{
				VariableDeclaration.Expression = ToExpression(context);
			}
			return Expression.Assign(VariableDeclaration.Expression, Expression.Default(VariableDeclaration.DataType));
		}
コード例 #8
0
		public string ToString(ParseContext context)
		{
			if (!string.IsNullOrWhiteSpace(context.ReturnVariable))
			{
				return "return " + context.ReturnVariable;
			}
			return "return";
		}
コード例 #9
0
		public string ToString(ParseContext context)
		{
			var result = "new " + ReflectionUtil.TypeToString(DataType) + "(";
			for (int i = 0; i < Variables.Length; i++)
			{
				if (i > 0) result += ", ";
				result += Variables[i].ToString(context);
			}
			return result + ")";
		}
コード例 #10
0
		public void PreParseExpression(ParseContext context)
		{
			_constructorTypes = new List<Type>();
			foreach (var param in Variables)
			{
				param.PreParseExpression(context);
				_constructorTypes.Add(param.ParsedType);
			}
			ParsedType = DataType;
		}
コード例 #11
0
		public string ToString(ParseContext context)
		{
			if (_value == null) return "null";
			var type = _value.GetType();
			if (type.IsValueType || type.IsEnum)
			{
				return _value.ToString();
			}
			return "\"" + _value + "\"";
		}
コード例 #12
0
		public override string ToString(ParseContext context)
		{
			var res = "(";
			var operation = IsAnd ? "&&" : "||";
			for (int i = 0; i < SubConditions.Count; i++)
			{
				if (i > 0) res += operation;
				res += SubConditions[i].ToString(context);
			}
			return res + ")";
		}
コード例 #13
0
        public Expression ToExpression(ParseContext context)
        {
            var pars = new List<Expression>();

            foreach (var param in Variables)
            {
                pars.Add(param.ToExpression(context));
            }

            return Expression.NewArrayInit(DataType.GetElementType(), pars);
        }
コード例 #14
0
		public void PreParseExpression(ParseContext context)
		{
			_paramTypes = new List<Type>();
			for (int i = 0; i < Parameters.Length; i++)
			{
				Parameters[i].PreParseExpression(context);
				_paramTypes.Add(Parameters[i].ParsedType);
			}

			ParsedType = null;
		}
コード例 #15
0
		public Expression ToExpression(ParseContext context)
		{
			var variable = context.GetVariable(Name);
			if (variable != null)
			{
				if (variable.Expression == null)
				{
					variable.Expression = Expression.Parameter(ParsedType, Name);
				}
				return variable.Expression;
			}
			return Expression.Parameter(ParsedType, Name);
		}
コード例 #16
0
ファイル: While.cs プロジェクト: gtwijma/ExpressionBuilder
		public void PreParseExpression(ParseContext context)
		{
			//var pl = context.Current;
			Condition.PreParseExpression(context);
			context.AddLevel();

			foreach (var line in CodeLines)
			{
				line.PreParseExpression(context);
			}

			context.RemoveLevel();
		}
コード例 #17
0
		public override Expression ToExpression(ParseContext context)
		{
			Expression result = null;
			for (int index = (SubConditions.Count - 2); index >= 0; index--)
			{
				if (result == null)
				{
					var l = SubConditions[index].ToExpression(context);
					var r = SubConditions[index + 1].ToExpression(context);
					result = BuildOperation(l, r);
				}
				else
				{
					var l = SubConditions[index].ToExpression(context);
					result = BuildOperation(l, result);
				}
			}
			return result;
		}
コード例 #18
0
		public string ToString(ParseContext context)
		{
			var result = string.Empty;
			if (StaticDataType == null)
			{
				result = Variable.ToString(context);
			}
			else
			{
				result = ReflectionUtil.TypeToString(StaticDataType);
			}
			result += "." + MethodName + "(";
			for (int i = 0; i < Parameters.Length; i++)
			{
				if (i > 0) result += ", ";
				result += Parameters[i].ToString(context);
			}
			return result + ")";
		}
コード例 #19
0
ファイル: While.cs プロジェクト: gtwijma/ExpressionBuilder
		public string ToString(ParseContext context)
		{
			var result = "while(" + Condition.ToString(context) + ")\n";
			result += context.Pad + "{\n";
			context.AddLevel();

			foreach (var line in CodeLines)
			{
				var createVariable = line as CreateVariable;
				if (createVariable != null)
				{
					createVariable.DefaultInitialize(context);
				}
				result += context.Pad + line.ToString(context) + ";\n";
			}

			context.RemoveLevel();
			result += context.Pad + "}";
			return result;
		}
コード例 #20
0
		public Expression ToExpression(ParseContext context)
		{
			var pars = new List<Expression>();

			foreach (var param in Variables)
			{
				pars.Add(param.ToExpression(context));
			}
			var constructor = ReflectionUtil.GetConstructor(DataType, _constructorTypes);
			if (constructor.GoodFrom >= 0)
			{
				var startDefault = constructor.GoodFrom;
				while (startDefault < constructor.ParamValues.Count)
				{
					pars.Add(Operation.Constant(constructor.ParamValues[constructor.GoodFrom]).ToExpression(context));
					startDefault++;
				}
			}

			return Expression.New(constructor.Method as ConstructorInfo, pars);
		}
コード例 #21
0
		public ParseLevel(ParseContext parseContext)
		{
			_variables = new Dictionary<string, Variable>(StringComparer.InvariantCultureIgnoreCase);
			_parseContext = parseContext;
		}
コード例 #22
0
ファイル: If.cs プロジェクト: gtwijma/ExpressionBuilder
		public Expression ToExpression(ParseContext context)
		{
			var conditionExpression = Condition.ToExpression(context);
			context.AddLevel();

			var thenLine = new List<Expression>();
			var listOfThenVars = new List<ParameterExpression>();
			foreach (var line in ThenCodeLines)
			{
				var expLine = line.ToExpression(context);

				var createVariable = line as CreateVariable;
				if (createVariable != null)
				{
					listOfThenVars.Add((ParameterExpression)expLine);
					expLine = createVariable.DefaultInitialize(context);
				}
				thenLine.Add(expLine);
			}
			var thenBlock = Expression.Block(listOfThenVars.ToArray(), thenLine);

			context.RemoveLevel();

			var elseLine = new List<Expression>();
			if (ElseCodeLines.Count > 0)
			{
				context.AddLevel();
				var listOfElseVars = new List<ParameterExpression>();
				foreach (var line in ElseCodeLines)
				{
					var expLine = line.ToExpression(context);

					var createVariable = line as CreateVariable;
					if (createVariable != null)
					{
						listOfElseVars.Add((ParameterExpression)expLine);
						expLine = createVariable.DefaultInitialize(context);
					}
					elseLine.Add(expLine);
				}

				context.RemoveLevel();
				var elseBlock = Expression.Block(listOfElseVars, elseLine);
				return Expression.IfThenElse(conditionExpression, thenBlock, elseBlock);
			}
			return Expression.IfThen(conditionExpression, thenBlock);
		}
コード例 #23
0
		public Expression ToExpression(ParseContext context)
		{
			var pars = new List<Expression>();

			foreach (var param in Parameters)
			{
				pars.Add(param.ToExpression(context));
			}
			if (_paramTypes == null)
			{
				_paramTypes = new List<Type>();
				for (int i = 0; i < Parameters.Length; i++)
				{
					Parameters[i].PreParseExpression(context);
					_paramTypes.Add(Parameters[i].ParsedType);
				}
			}
			Type type = StaticDataType;
			if (StaticDataType == null)
			{
				type = Variable.ParsedType;
				if (Variable is OperationVariable)
				{
					var variable = context.GetVariable(((OperationVariable)Variable).Name);
					type = variable.DataType;
				}
			}

			var method = ReflectionUtil.GetMethod(type, MethodName, _paramTypes);

			if (method.GoodFrom >= 0)
			{
				var startDefault = method.GoodFrom;
				while (startDefault < method.ParamValues.Count)
				{
					pars.Add(Operation.Constant(method.ParamValues[method.GoodFrom]).ToExpression(context));
					startDefault++;
				}
			}

			var my = (MethodInfo)method.Method;
			if ((my.Attributes & MethodAttributes.Static)==0)
			{
				return Expression.Call(Variable.ToExpression(context), method.Method as MethodInfo, pars);
			}
			return Expression.Call(method.Method as MethodInfo, pars);
		}
コード例 #24
0
		public abstract Expression ToExpression(ParseContext context);
コード例 #25
0
		public Expression ToExpression(ParseContext context)
		{
			context.Current.AddVariable(VariableDeclaration);
			VariableDeclaration.Expression = Expression.Variable(VariableDeclaration.DataType, VariableDeclaration.Name);
			return VariableDeclaration.Expression;
		}
コード例 #26
0
		public void PreParseExpression(ParseContext context)
		{
			context.Current.AddVariable(VariableDeclaration);
			ParsedType = VariableDeclaration.DataType;
		}
コード例 #27
0
		public Expression ToExpression(ParseContext context)
		{
			return Expression.Constant(_value);
		}
コード例 #28
0
		public abstract void PreParseExpression(ParseContext context);
コード例 #29
0
ファイル: While.cs プロジェクト: gtwijma/ExpressionBuilder
		public Expression ToExpression(ParseContext context)
		{
			var conditionExpression = Condition.ToExpression(context);
			context.AddLevel();

			var thenLine = new List<Expression>();
			var listOfThenVars = new List<ParameterExpression>();
			foreach (var line in CodeLines)
			{
				var expLine = line.ToExpression(context);

				var createVariable = line as CreateVariable;
				if (createVariable != null)
				{
					listOfThenVars.Add((ParameterExpression)expLine);
					expLine = createVariable.DefaultInitialize(context);
				}
				thenLine.Add(expLine);
			}
			var thenBlock = Expression.Block(listOfThenVars.ToArray(), thenLine);

			context.RemoveLevel();

			LabelTarget label = Expression.Label(Guid.NewGuid().ToString());
			var ifThenElse = Expression.IfThenElse(
																conditionExpression,
																thenBlock,
																Expression.Break(label));
			return Expression.Loop(ifThenElse, label);
		}
コード例 #30
0
		public string ToString(ParseContext context)
		{
			return ReflectionUtil.TypeToString(VariableDeclaration.DataType) + " " + VariableDeclaration.Name;
		}