示例#1
0
 protected override IEnumerable <IAstElement> VisitOrTransformChildren(AstElementTransform transform)
 {
     if (this.Result != null)
     {
         yield return(this.Result = (IAstExpression)transform(this.Result));
     }
 }
示例#2
0
文件: Program.cs 项目: ashmind/light
        private static object CompileAndEvaluate(IAstExpression expression)
        {
            var ast = new AstRoot(
                new ImportDefinition(new CompositeName("System")),
                new ImportDefinition(new CompositeName("System", "Linq")),
                new AstTypeDefinition(
                    TypeDefintionTypes.Class, "Interactive",
                    new AstFunctionDefinition("Evaluate", No.Parameters, new[] { new AstReturnStatement(expression) }, AstImplicitType.Instance) {
                        Compilation = { Static = true }
                    }
                )
            );
            ast = (AstRoot)processor.Process(ast);

            var stream = new MemoryStream();
            compiler.Compile(ast, stream, new CompilationArguments {
                AssemblyName = "Dynamic-" + Guid.NewGuid(),
                AssemblyVersion = new Version("1.0.0.0"),
                Target = CompilationTarget.Library
            });
            var assembly = Assembly.Load(stream.ToArray()); // this obviously leaks memory
            var evaluate = assembly.GetType("Interactive", true).GetMethod("Evaluate");

            return evaluate.Invoke(null, null);
        }
示例#3
0
        private void EmitStelem(ILProcessor processor, TypeReference elementType, IAstExpression element, CilCompilationContext context)
        {
            if (elementType.IsPrimitive)
            {
                if (!StelemCodes.ContainsKey(elementType.MetadataType))
                {
                    throw new NotImplementedException("ListInitializerCompiler.EmitStelem: Element metadata type " + elementType.MetadataType + " is not yet supported.");
                }

                context.Compile(element);
                processor.Emit(StelemCodes[elementType.MetadataType]);
                return;
            }

            if (elementType.IsValueType)
            {
                processor.Emit(OpCodes.Ldelema, elementType);
                context.Compile(element);
                processor.Emit(OpCodes.Stobj, elementType);
                return;
            }

            context.Compile(element);
            processor.Emit(OpCodes.Stelem_Ref);
        }
示例#4
0
        public AstPropertyDefinition(string name, IAstTypeReference type, IAstExpression assignedValue)
        {
            Argument.RequireNotNullAndNotEmpty("name", name);

            this.Name          = name;
            this.Type          = type;
            this.AssignedValue = assignedValue;
        }
示例#5
0
        public AstPropertyDefinition(string name, IAstTypeReference type, IAstExpression assignedValue)
        {
            Argument.RequireNotNullAndNotEmpty("name", name);

            this.Name = name;
            this.Type = type;
            this.AssignedValue = assignedValue;
        }
示例#6
0
        protected override IEnumerable <IAstElement> VisitOrTransformChildren(AstElementTransform transform)
        {
            yield return(this.Type = (IAstTypeReference)transform(this.Type));

            if (this.AssignedValue != null)
            {
                yield return(this.AssignedValue = (IAstExpression)transform(this.AssignedValue));
            }
        }
示例#7
0
        public static void CompileTarget(ILProcessor processor, IAstExpression target, IAstMethodReference function, CilCompilationContext context)
        {
            context.Compile(target);
            if (function.Location == MethodLocation.Extension)
                return;

            var targetType = context.ConvertReference(target.ExpressionType);
            if (!targetType.IsValueType)
                return;

            var variable = context.DefineVariable("x", targetType);
            processor.Emit(OpCodes.Stloc, variable);
            processor.Emit(OpCodes.Ldloca_S, variable);
        }
示例#8
0
        public static void CompileTarget(ILProcessor processor, IAstExpression target, IAstMethodReference function, CilCompilationContext context)
        {
            context.Compile(target);
            if (function.Location == MethodLocation.Extension)
            {
                return;
            }

            var targetType = context.ConvertReference(target.ExpressionType);

            if (!targetType.IsValueType)
            {
                return;
            }

            var variable = context.DefineVariable("x", targetType);

            processor.Emit(OpCodes.Stloc, variable);
            processor.Emit(OpCodes.Ldloca_S, variable);
        }
示例#9
0
 public AstVariableDefinition(string name, IAstTypeReference type, IAstExpression value)
 {
     this.Name = @name;
     this.Type = type;
     this.AssignedValue = value;
 }
示例#10
0
        private void CompileFieldOrPropertyAssignment(ILProcessor processor, AstPropertyExpression property, IAstExpression value, CilCompilationContext context)
        {
            processor.Emit(OpCodes.Ldarg_0);
            context.Compile(value);
            var fieldOrProperty = context.ConvertReference(property.Reference);
            var field           = fieldOrProperty.As <FieldReference>();

            if (field == null)
            {
                throw new NotImplementedException("AssignmentCompiler: Assignment to " + fieldOrProperty + " is not yet supported.");
            }

            processor.Emit(OpCodes.Stfld, field);
        }
示例#11
0
 protected override IEnumerable<IAstElement> VisitOrTransformChildren(AstElementTransform transform)
 {
     yield return this.Left = (IAstExpression)transform(this.Left);
     yield return this.Operator = (IAstMethodReference)transform(this.Operator);
     yield return this.Right = (IAstExpression)transform(this.Right);
 }
示例#12
0
        protected override IEnumerable <IAstElement> VisitOrTransformChildren(AstElementTransform transform)
        {
            yield return(this.Target = (IAstAssignable)transform(this.Target));

            yield return(this.Value = (IAstExpression)transform(this.Value));
        }
示例#13
0
 protected override IEnumerable<IAstElement> VisitOrTransformChildren(AstElementTransform transform)
 {
     if (this.Result != null)
         yield return this.Result = (IAstExpression)transform(this.Result);
 }
示例#14
0
 public AstReturnStatement(IAstExpression result)
 {
     Result = result;
 }
示例#15
0
 public AstVariableDefinition(string name, IAstTypeReference type, IAstExpression value)
 {
     this.Name          = @name;
     this.Type          = type;
     this.AssignedValue = value;
 }
示例#16
0
 public AstReturnStatement(IAstExpression result)
 {
     Result = result;
 }
示例#17
0
 public AssignmentStatement(IAstAssignable target, IAstExpression value)
 {
     this.Target = target;
     this.Value = value;
 }
示例#18
0
 public AssignmentStatement(IAstAssignable target, IAstExpression value)
 {
     this.Target = target;
     this.Value  = value;
 }
示例#19
0
 protected override IEnumerable<IAstElement> VisitOrTransformChildren(AstElementTransform transform)
 {
     yield return this.Target = (IAstAssignable)transform(this.Target);
     yield return this.Value = (IAstExpression)transform(this.Value);
 }
示例#20
0
 public BinaryExpression(IAstExpression left, IAstMethodReference @operator, IAstExpression right)
 {
     this.Left     = left;
     this.Operator = @operator;
     this.Right    = right;
 }
示例#21
0
 public BinaryExpression(IAstExpression left, IAstMethodReference @operator, IAstExpression right)
 {
     this.Left = left;
     this.Operator = @operator;
     this.Right = right;
 }