Ejemplo n.º 1
0
 public override Type Evaluate(CodeContext c)
 {
     LocalBuilder local;
     int parameter;
     if (c.Locals.TryGetValue(variablename,out local))
     {
         c.IL.Emit(OpCodes.Ldloc,local);
         return local.LocalType;
     }
     else if (c.Parameters.TryGetValue(variablename,out parameter))
     {
         if (c.Method.IsStatic)
         {
             c.IL.Emit(OpCodes.Ldarg,parameter);
         }
         else
         {
             c.IL.Emit(OpCodes.Ldarg,parameter + 1);
         }
         return c.ParameterTypes[parameter];
     }
     else
     {
         throw new ApplicationException();
     }
 }
Ejemplo n.º 2
0
 public override void Compile(CodeContext c)
 {
     foreach (Statement statement in statements)
     {
         statement.Compile(c);
     }
 }
Ejemplo n.º 3
0
 public override Type Evaluate(CodeContext c)
 {
     Type lefttype = Left.Evaluate(c);
     Type righttype = Right.Evaluate(c);
     if (lefttype == typeof(int) && righttype == typeof(int))
     {
         c.IL.Emit(OpCodes.Clt);
     }
     else if (lefttype == typeof(Fixed) && righttype == typeof(Fixed))
     {
         c.IL.Emit(OpCodes.Call,typeof(Fixed).GetMethod("op_LessThan"));
     }
     else if (lefttype == typeof(Fixed) && righttype == typeof(int))
     {
         c.IL.Emit(OpCodes.Call,typeof(Fixed).GetMethod("FromInt"));
         c.IL.Emit(OpCodes.Call,typeof(Fixed).GetMethod("op_LessThan"));
     }
     else if (lefttype == typeof(int) && righttype == typeof(Fixed))
     {
         c.IL.Emit(OpCodes.Stloc_0);
         c.IL.Emit(OpCodes.Call,typeof(Fixed).GetMethod("FromInt"));
         c.IL.Emit(OpCodes.Ldloc_0);
         c.IL.Emit(OpCodes.Call,typeof(Fixed).GetMethod("op_LessThan"));
     }
     else
     {
         throw new ApplicationException();
     }
     return typeof(bool);
 }
Ejemplo n.º 4
0
 public override void Assign(CodeContext c,Expression source)
 {
     LocalBuilder local;
     int parameter;
     if (c.Locals.TryGetValue(variablename,out local))
     {
         Type type = source.Evaluate(c);
         c.Convert(type,local.LocalType);
         c.IL.Emit(OpCodes.Stloc,local);
     }
     else if (c.Parameters.TryGetValue(variablename,out parameter))
     {
         Type type = source.Evaluate(c);
         c.Convert(type,c.ParameterTypes[parameter]);
         if (c.Method.IsStatic)
         {
             c.IL.Emit(OpCodes.Starg,parameter);
         }
         else
         {
             c.IL.Emit(OpCodes.Starg,parameter + 1);
         }
     }
     else
     {
         throw new ApplicationException();
     }
 }
Ejemplo n.º 5
0
 public override void Compile(CodeContext c)
 {
     Type type = expression.Evaluate(c);
     LocalBuilder local = c.IL.DeclareLocal(type);
     c.IL.Emit(OpCodes.Stloc,local);
     c.Locals.Add(variablename,local);
 }
Ejemplo n.º 6
0
 public override Type Evaluate(CodeContext c)
 {
     Type type = Inner.Evaluate(c);
     MemberInfo member = GetMember(type,ScriptAccessType.Get);
     if (member.MemberType == MemberTypes.Field)
     {
         FieldInfo field = (FieldInfo)member;
         c.IL.Emit(OpCodes.Ldfld,field);
         return field.FieldType;
     }
     else if (member.MemberType == MemberTypes.Property)
     {
         PropertyInfo property = (PropertyInfo)member;
         if (!property.CanRead)
         {
             throw new ApplicationException();
         }
         c.IL.Emit(OpCodes.Callvirt,property.GetGetMethod());
         return property.PropertyType;
     }
     else
     {
         throw new ApplicationException();
     }
 }
Ejemplo n.º 7
0
 public override void Assign(CodeContext c,Expression source)
 {
     Type type = Inner.Evaluate(c);
     MemberInfo member = GetMember(type,ScriptAccessType.Set);
     if (member.MemberType == MemberTypes.Field)
     {
         FieldInfo field = (FieldInfo)member;
         if (field.IsInitOnly || field.IsLiteral)
         {
             throw new ApplicationException();
         }
         Type sourcetype = source.Evaluate(c);
         c.Convert(sourcetype,field.FieldType);
         c.IL.Emit(OpCodes.Stfld,field);
     }
     else if (member.MemberType == MemberTypes.Property)
     {
         PropertyInfo property = (PropertyInfo)member;
         if (!property.CanWrite)
         {
             throw new ApplicationException();
         }
         Type sourcetype = source.Evaluate(c);
         c.Convert(sourcetype,property.PropertyType);
         c.IL.Emit(OpCodes.Callvirt,property.GetSetMethod());
     }
     else
     {
         throw new ApplicationException();
     }
 }
Ejemplo n.º 8
0
 public override void Execute(CodeContext c)
 {
     Type type = Inner.Invoke(c,arguments);
     if (type != typeof(void))
     {
         c.IL.Emit(OpCodes.Pop);
     }
 }
Ejemplo n.º 9
0
 public override Type Evaluate(CodeContext c)
 {
     Type type = Inner.Invoke(c,arguments);
     if (type == typeof(void))
     {
         throw new ApplicationException();
     }
     return type;
 }
Ejemplo n.º 10
0
 public CodeContext(CodeContext next) : base(next)
 {
     if (next == null)
         throw new ArgumentNullException("next");
     this.method = next.method;
     this.il = next.il;
     this.locals = new Dictionary<string,LocalBuilder>(next.locals);
     this.parameters = next.parameters;
     this.parametertypes = next.parametertypes;
 }
Ejemplo n.º 11
0
 public override Type Evaluate(CodeContext c)
 {
     if (base.Evaluate(c) != typeof(bool))
     {
         throw new ApplicationException();
     }
     c.IL.Emit(OpCodes.Ldc_I4_0);
     c.IL.Emit(OpCodes.Ceq);
     return typeof(bool);
 }
Ejemplo n.º 12
0
 public override Type Evaluate(CodeContext c)
 {
     Type type = Inner.Evaluate(c);
     if (type != typeof(bool))
     {
         throw new ApplicationException();
     }
     c.IL.Emit(OpCodes.Not);
     return typeof(bool);
 }
Ejemplo n.º 13
0
 public override Type Evaluate(CodeContext c)
 {
     Type lefttype = Left.Evaluate(c);
     Type righttype = Right.Evaluate(c);
     if (lefttype != typeof(int) || righttype != typeof(int))
     {
         throw new ApplicationException();
     }
     c.IL.Emit(OpCodes.Shl);
     return typeof(int);
 }
Ejemplo n.º 14
0
 public override void Compile(DefinitionContext c)
 {
     if (method == null)
         throw new InvalidOperationException();
     CodeContext c2 = new CodeContext(c,method);
     // Fixed local is used for some expressions
     c2.IL.DeclareLocal(typeof(Fixed));
     c2.ParameterTypes.Add(typeof(ScriptLinedefSpecialEnvironment));
     statement.Compile(c2);
     c2.IL.Emit(OpCodes.Newobj,typeof(ApplicationException).GetConstructor(Type.EmptyTypes));
     c2.IL.ThrowException(typeof(ApplicationException));
 }
Ejemplo n.º 15
0
 public override Type Evaluate(CodeContext c)
 {
     Label shortcircuit = c.IL.DefineLabel();
     Type lefttype = Left.Evaluate(c);
     c.IL.Emit(OpCodes.Dup);
     c.IL.Emit(OpCodes.Brfalse,shortcircuit);
     Type righttype = Right.Evaluate(c);
     if (lefttype != typeof(bool) || righttype != typeof(bool))
     {
         throw new ApplicationException();
     }
     c.IL.Emit(OpCodes.And);
     c.IL.MarkLabel(shortcircuit);
     return typeof(bool);
 }
Ejemplo n.º 16
0
 public override Type Evaluate(CodeContext c)
 {
     Type lefttype = Left.Evaluate(c);
     Type righttype = Right.Evaluate(c);
     if (lefttype != null && lefttype.IsValueType)
     {
         throw new ApplicationException();
     }
     if (righttype != null && righttype.IsValueType)
     {
         throw new ApplicationException();
     }
     c.IL.Emit(OpCodes.Ceq);
     return typeof(bool);
 }
Ejemplo n.º 17
0
 public override void Compile(CodeContext c)
 {
     Label elselabel = c.IL.DefineLabel();
     Label endlabel = c.IL.DefineLabel();
     Type type = condition.Evaluate(c);
     if (type != typeof(bool))
     {
         throw new ApplicationException();
     }
     c.IL.Emit(OpCodes.Brfalse,elselabel);
     thenstatement.SubCompile(c);
     c.IL.Emit(OpCodes.Br,endlabel);
     c.IL.MarkLabel(elselabel);
     elsestatement.SubCompile(c);
     c.IL.MarkLabel(endlabel);
 }
Ejemplo n.º 18
0
 public override void Compile(CodeContext c)
 {
     Label looplabel = c.IL.DefineLabel();
     Label elselabel = c.IL.DefineLabel();
     Label endlabel = c.IL.DefineLabel();
     LocalBuilder enumerator = c.IL.DeclareLocal(typeof(IEnumerator));
     Type collectiontype = collection.Evaluate(c);
     if (collectiontype == null)
     {
         throw new ApplicationException();
     }
     if (!typeof(IEnumerable).IsAssignableFrom(collectiontype))
     {
         throw new ApplicationException();
     }
     Type valuetype;
     if (!c.Types.TryGetValue(variabletype,out valuetype))
     {
         throw new ApplicationException();
     }
     LocalBuilder value = c.IL.DeclareLocal(valuetype);
     CodeContext c2 = new CodeContext(c);
     c2.Locals.Add(variablename,value);
     if (collectiontype.IsValueType)
     {
         c.IL.Emit(OpCodes.Constrained,typeof(IEnumerable));
     }
     c.IL.Emit(OpCodes.Callvirt,typeof(IEnumerable).GetMethod("GetEnumerator"));
     c.IL.Emit(OpCodes.Stloc,enumerator);
     c.IL.MarkLabel(looplabel);
     c.IL.Emit(OpCodes.Ldloc,enumerator);
     c.IL.Emit(OpCodes.Callvirt,typeof(IEnumerator).GetMethod("MoveNext"));
     c.IL.Emit(OpCodes.Brfalse,elselabel);
     c.IL.Emit(OpCodes.Ldloc,enumerator);
     c.IL.Emit(OpCodes.Callvirt,typeof(IEnumerator).GetProperty("Current").GetGetMethod());
     c.IL.Emit(OpCodes.Isinst,valuetype);
     c.IL.Emit(OpCodes.Dup);
     c.IL.Emit(OpCodes.Stloc,value);
     c.IL.Emit(OpCodes.Brfalse,looplabel);
     loopstatement.SubCompile(c2);
     c.IL.Emit(OpCodes.Br,looplabel);
     c.IL.MarkLabel(elselabel);
     elsestatement.SubCompile(c);
     c.IL.MarkLabel(endlabel);
 }
Ejemplo n.º 19
0
 public override Type Evaluate(CodeContext c)
 {
     Type type = Inner.Evaluate(c);
     if (type == typeof(int))
     {
         c.IL.Emit(OpCodes.Neg);
         return typeof(int);
     }
     else if (type == typeof(Fixed))
     {
         c.IL.Emit(OpCodes.Call,typeof(Fixed).GetMethod("op_UnaryNegation"));
         return typeof(Fixed);
     }
     else
     {
         throw new ApplicationException();
     }
 }
Ejemplo n.º 20
0
 public override void Compile(CodeContext c)
 {
     if (expression == null)
     {
         if (c.ReturnType != typeof(void))
         {
             throw new ApplicationException();
         }
         c.IL.Emit(OpCodes.Ret);
     }
     else if (expression != null)
     {
         if (c.ReturnType == typeof(void))
         {
             throw new ApplicationException();
         }
         Type type = expression.Evaluate(c);
         c.Convert(type,c.ReturnType);
         c.IL.Emit(OpCodes.Ret);
     }
 }
Ejemplo n.º 21
0
 public override Type Evaluate(CodeContext c)
 {
     if (c.ParameterTypes.Count == 0)
     {
         throw new ApplicationException();
     }
     c.IL.Emit(OpCodes.Ldarg_0);
     if (variablename == "$world")
     {
         if (!typeof(ScriptEnvironment).IsAssignableFrom(c.ParameterTypes[0]))
             throw new ApplicationException();
         c.IL.Emit(OpCodes.Callvirt,typeof(ScriptEnvironment).GetProperty("World").GetGetMethod());
         return typeof(World);
     }
     else if (variablename == "$activator")
     {
         if (!typeof(ScriptSpecialEnvironment).IsAssignableFrom(c.ParameterTypes[0]))
             throw new ApplicationException();
         c.IL.Emit(OpCodes.Callvirt,typeof(ScriptSpecialEnvironment).GetProperty("Activator").GetGetMethod());
         return typeof(Actor);
     }
     else if (variablename == "$linedef")
     {
         if (!typeof(ScriptLinedefSpecialEnvironment).IsAssignableFrom(c.ParameterTypes[0]))
             throw new ApplicationException();
         c.IL.Emit(OpCodes.Callvirt,typeof(ScriptLinedefSpecialEnvironment).GetProperty("Linedef").GetGetMethod());
         return typeof(Linedef);
     }
     else if (variablename == "$isbackside")
     {
         if (!typeof(ScriptLinedefSpecialEnvironment).IsAssignableFrom(c.ParameterTypes[0]))
             throw new ApplicationException();
         c.IL.Emit(OpCodes.Callvirt,typeof(ScriptLinedefSpecialEnvironment).GetProperty("IsBackSide").GetGetMethod());
         return typeof(bool);
     }
     else
     {
         throw new ApplicationException();
     }
 }
Ejemplo n.º 22
0
 public override void Compile(DefinitionContext c)
 {
     if (method == null)
         throw new InvalidOperationException();
     CodeContext c2 = new CodeContext(c,method);
     // Fixed local is used for some expressions
     c2.IL.DeclareLocal(typeof(Fixed));
     c2.ParameterTypes.Add(typeof(ScriptEnvironment));
     for (int i = 0;i < parametertypes.Length;i++)
     {
         c2.ParameterTypes.Add(realparametertypes[i + 1]);
         c2.Parameters.Add(parameternames[i],i + 1);
     }
     statement.Compile(c2);
     if (realreturntype == typeof(void))
     {
         c2.IL.Emit(OpCodes.Ret);
     }
     else
     {
         c2.IL.Emit(OpCodes.Newobj,typeof(ApplicationException).GetConstructor(Type.EmptyTypes));
         c2.IL.ThrowException(typeof(ApplicationException));
     }
 }
Ejemplo n.º 23
0
 public override void Execute(CodeContext c)
 {
     Left.Assign(c,Right);
 }
Ejemplo n.º 24
0
 public override Type Evaluate(CodeContext c)
 {
     Left.Assign(c,Right);
     return Left.Evaluate(c);
 }
Ejemplo n.º 25
0
 public override Type Evaluate(CodeContext c)
 {
     c.IL.Emit(OpCodes.Ldc_I4,value);
     return typeof(int);
 }
Ejemplo n.º 26
0
 public override Type Evaluate(CodeContext c)
 {
     c.IL.Emit(OpCodes.Ldc_I4_1);
     return typeof(bool);
 }
Ejemplo n.º 27
0
 public virtual void Execute(CodeContext c)
 {
     throw new ApplicationException();
 }
Ejemplo n.º 28
0
 public abstract Type Evaluate(CodeContext c);
Ejemplo n.º 29
0
 public virtual Type Invoke(CodeContext c,Expression[] arguments)
 {
     throw new ApplicationException();
 }
Ejemplo n.º 30
0
 public virtual void Assign(CodeContext c,Expression source)
 {
     throw new ApplicationException();
 }