Esempio n. 1
0
        private static int CountCommas(LexList list, int rank, bool arrayIndexActualsAllowed)
        {
            Stack <string> stack = new Stack <string>();

            if (arrayIndexActualsAllowed)
            {
                int  saveIndex = list.Index - 1;
                bool finished  = false;
                while (!list.AtEnd() && !finished)
                {
                    switch (list.Str)
                    {
                    case ",": if (stack.Count == 0)
                        {
                            rank++;
                        }
                        break;

                    case "(": stack.Push(")"); break;

                    case "{": stack.Push("}"); break;

                    case "[": stack.Push("]"); break;

                    case ")":
                    case "}": if (stack.Pop() != list.Str)
                        {
                            list.ThrowException("Unbalanced brackets in array initialiser");
                        }
                        break;

                    case "]":
                        if (stack.Count == 0)
                        {
                            list.Prev();
                            finished = true;
                        }
                        else
                        {
                            if (stack.Pop() != list.Str)
                            {
                                list.ThrowException("Unbalanced brackets in array initialiser");
                            }
                        }
                        break;
                    }
                    list.Next();
                }
                list.Index = saveIndex;
            }
            else
            {
                while (list.Str == ",")
                {
                    list.Next();
                    rank++;
                }
            }
            return(rank);
        }
Esempio n. 2
0
 public void CheckTypeIs(LexList list, Type type, string msg)
 {
     if (ResultType != type)
     {
         list.ThrowException(msg);
     }
 }
Esempio n. 3
0
 public void CheckEqualityTypes(LexList InputList, Type type)
 {
     if (ResultType == null && type == null)
     {
         return;
     }
     if (ResultType == null && !type.IsValueType)
     {
         return;
     }
     if (!ResultType.IsValueType && (type == null))
     {
         return;
     }
     if (ResultType.IsEnum && type == Enum.GetUnderlyingType(ResultType))
     {
         return;
     }
     if (type.IsEnum && ResultType == Enum.GetUnderlyingType(type))
     {
         return;
     }
     if ((ResultType == type) || ((IsIntType(ResultType) || IsRealType(ResultType)) && (IsIntType(type) || IsRealType(type))))
     {
         return;
     }
     InputList.ThrowException("The left hand operand type '" + ResultType.ToString() + "'\n is not Equality compatible with the right hand operand type '" + type.ToString() + "'.");
 }
Esempio n. 4
0
        private void CompileNextFieldDefinition(LexList theLexList, Type varType)
        {
            string varId = theLexList.GetIdentifier("Expected the name of the field here.");

            MostRecentNameAdded = varId;
            theLexList.CheckStr(";", "Expected a ; here");
            if (MadeFieldsDict.ContainsKey(varId))
            {
                theLexList.ThrowException("This field already defined.");
            }
            if (CompileMethodsDict.ContainsKey(varId))
            {
                theLexList.ThrowException("This field name already used as a method name.");
            }
            MadeFieldsDict.Add(varId, new MadeField()
            {
                VarName = varId, VarType = varType, Index = MadeFieldsDict.Count
            });
            FieldInfo fi = ClassType.GetField("Fields", BindingFlags.Public | BindingFlags.Instance);

            if (fi == null)
            {
                theLexList.ThrowException("Can only add fields to a class that has a 'List<Object> Fields' field.");
            }
            if (fi.FieldType != typeof(List <object>))
            {
                theLexList.ThrowException("Can only add fields to a class where its Fields field is of type List<object>.");
            }
            theLexList.Next();
        }
Esempio n. 5
0
 public LexReadDialog(LexList theList, FontFamily family, double fontSize)
     : base(family, fontSize)
 {
     TheList       = new LexList(theList);
     TheList.Index = theList.Index;
     SetUpLineStarts_LexList();
 }
Esempio n. 6
0
 private void ListAdd(string str, LexKind kind)
 {
     if (kind >= LexKind.Symbol00 && kind <= LexKind.Symbol11 && Expansions.ContainsKey(str))
     {
         object o;
         Expansions.TryGetValue(str, out o);
         if (o is string)
         {
             string so = o as string;
             o = LexList.Get(so);
         }
         if (o is LexList)
         {
             for (int i = 0; i < ((LexList)o).Count; i++)
             {
                 LexToken tok        = ((LexList)o)[i];
                 bool     toPrevious = (kind == LexKind.Symbol10 || kind == LexKind.Symbol11) && i == 0;
                 bool     toNext     = (kind == LexKind.Symbol01 || kind == LexKind.Symbol11) && i == ((LexList)o).Count - 1;
                 ListAddToken(toPrevious, new LexToken(tok), toNext);
             }
         }
         else
         {
             LexToken token = new LexToken(o, List, List.Count);
             List.Add(token);
         }
     }
     else
     {
         ListAddToken(kind == LexKind.Symbol10 || kind == LexKind.Symbol11, new LexToken(str, kind, List, List.Count), kind == LexKind.Symbol01 || kind == LexKind.Symbol11);
     }
 }
Esempio n. 7
0
 public void CheckIsArrayType(LexList InputList, string p)
 {
     if (ResultType.IsArray)
     {
         return;
     }
     InputList.ThrowException(p);
 }
Esempio n. 8
0
 public void CheckIsBoolType(LexList InputList, string msg)
 {
     if (ResultType == typeof(bool))
     {
         return;
     }
     InputList.ThrowException(msg);
 }
Esempio n. 9
0
 public void CheckIsIntType(LexList InputList, string p)
 {
     if (IsIntType(ResultType))
     {
         return;
     }
     InputList.ThrowException(p);
 }
Esempio n. 10
0
 public void ThrowException(string msg, LexList theList)
 {
     ErrorLocation = msg;
     if (LexToken.ShowError != null)
     {
         LexToken.ShowError(msg, theList);
     }
     throw new LexListException(ErrorLocation, this);
 }
Esempio n. 11
0
        private static List <LexToken> Get(ReadOnlyCollection <string> source)
        {
            List <LexToken> list = new List <LexToken>(source.Count);

            foreach (var s in source)
            {
                list.AddRange(LexList.Get(s));
            }
            return(list);
        }
Esempio n. 12
0
        public Type ParseType(LexList list, bool typeOnly, bool arrayIndexActualsAllowed, out bool pendingCloseAngle, BindingFlags visibility)
        {
            Type type = Parse(list, typeOnly, arrayIndexActualsAllowed, out pendingCloseAngle, visibility);

            if (pendingCloseAngle)
            {
                list.ThrowException("Unexpected > here");
            }
            return(type);
        }
Esempio n. 13
0
        private static List <LexToken> Get(string[] source)
        {
            List <LexToken> list = new List <LexToken>(source.Length);

            foreach (var s in source)
            {
                list.AddRange(LexList.Get(s));
            }
            return(list);
        }
Esempio n. 14
0
        public void DeclareArg(Type type, LexToken name, LexList theList)
        {
            VarInfo found;

            if (VarAccess.TryGetValue(name.Str, out found))
            {
                name.ThrowException("Argument name '" + name.Str + "' is already used.", theList);
            }
            VarAccess.Add(name.Str, new VarInfo(type, true, ArgNames.Count, -1));
            ArgNames.Add(name.Str);
            ArgTypes.Add(type);
        }
Esempio n. 15
0
        private void CompileNextMethodHeader(LexList theLexList, bool overwriteAllowed, List <CompileMethod> currentListOfMethods)
        {
            CompileMethod cm = DoOneMethod(theLexList);

            currentListOfMethods.Add(cm);
            AddMethodToDictionary(cm, overwriteAllowed);
            if (cm.MethodName != "FieldsInitialiser")
            {
                MostRecentNameAdded = cm.MethodName;
            }
            if (MadeFieldsDict.ContainsKey(cm.MethodName))
            {
                theLexList.ThrowException("This method name already used for a field.");
            }
        }
Esempio n. 16
0
        public static MethodInfo GetDelegateInfo(LexList list, Type type, out Type returnType, out Type[] paras)
        {
            if (type.BaseType != typeof(MulticastDelegate))
            {
                list.ThrowException("Type '" + type.Name + " is not a multicast delegate type.");
            }
            MethodInfo invoke = type.GetMethod("Invoke");

            if (invoke == null)
            {
                list.ThrowException("Type '" + type.Name + " is not a delegate type.");
            }
            paras      = (from p in invoke.GetParameters() select p.ParameterType).ToArray();
            returnType = invoke.ReturnType;
            return(invoke);
        }
Esempio n. 17
0
        public static Type GetMethodDelegateType(LexList list, Type[] argumentTypes, bool lastIsReturnType)
        {
            if (lastIsReturnType)
            {
                switch (argumentTypes.Length)
                {
                case 1: return(typeof(Func <>).MakeGenericType(argumentTypes));

                case 2: return(typeof(Func <,>).MakeGenericType(argumentTypes));

                case 3: return(typeof(Func <, ,>).MakeGenericType(argumentTypes));

                case 4: return(typeof(Func <, , ,>).MakeGenericType(argumentTypes));

                case 5: return(typeof(Func <, , , ,>).MakeGenericType(argumentTypes));

                case 6: return(typeof(Func <, , , , ,>).MakeGenericType(argumentTypes));

                case 7: return(typeof(Func <, , , , , ,>).MakeGenericType(argumentTypes));
                }
            }
            else
            {
                switch (argumentTypes.Length)
                {
                case 0: return(typeof(Action));

                case 1: return(typeof(Action <>).MakeGenericType(argumentTypes));

                case 2: return(typeof(Action <,>).MakeGenericType(argumentTypes));

                case 3: return(typeof(Action <, ,>).MakeGenericType(argumentTypes));

                case 4: return(typeof(Action <, , ,>).MakeGenericType(argumentTypes));

                case 5: return(typeof(Action <, , , ,>).MakeGenericType(argumentTypes));

                case 6: return(typeof(Action <, , , , ,>).MakeGenericType(argumentTypes));

                case 7: return(typeof(Action <, , , , , ,>).MakeGenericType(argumentTypes));
                }
            }
            list.ThrowException("GetMethodDelegateType program error " + argumentTypes.Length + " " + lastIsReturnType);
            return(null);
        }
Esempio n. 18
0
        private static void ExamineCurly(string line, ref int nesting, ref bool foundCurly)
        {
            LexList list = LexList.Get(line);

            foreach (var tok in list)
            {
                if (tok.Str == "{")
                {
                    nesting++;
                    foundCurly = true;
                }
                else if (tok.Str == "}")
                {
                    nesting--;
                    foundCurly = true;
                }
            }
        }
Esempio n. 19
0
        private void MakeFieldInitialiser(List <CompileMethod> currentListOfMethods)
        {
            LexListBuilder lb = new LexListBuilder();

            lb.AddAndPromoteQuotes("public void FieldsInitialiser () { ");
            lb.AddAndPromoteQuotes("if (Fields == null) Fields = new List<object> () ;");
            foreach (var f in (from f in MadeFieldsDict orderby f.Value.Index select f))
            {
                Type theType = f.Value.VarType;
                lb.AddAndPromoteQuotes("`type `name ;  if (Fields.Count == `index) Fields.Add ( (object)`name ) ; ", "type", theType, "name", f.Value.VarName, "index", f.Value.Index);
                // The conditional 'if (Fields.Count == f.Value.Index)' bit means that this initialisation function can be called repeatedly, and only the new ones will be initialised.
            }
            lb.AddAndPromoteQuotes("}");
            LexList ll = lb.ToLexList();

            ll.CrosslinkBrackets();
            CompileNextMethodHeader(ll, true, currentListOfMethods);
        }
Esempio n. 20
0
        // Syntax:
        // Member  = '.' StaticMember .
        public ExpState ParseMember(Type theClass, LexList list, BindingFlags flags, bool implicitDot)
        {
            if (!implicitDot)
            {
                list.CheckStrAndAdvance(".", "Expected a dot followed by a static member name here.");
            }
            LexToken token = list.CurrentToken();
            string   name  = list.GetIdentifier("Expected the name of a static member here.");

            FieldInfo fi = theClass.GetField(name, flags);

            if (fi != null)
            {
                return(new ExpState(fi));
            }

            list.Prev();
            return(null);
        }
Esempio n. 21
0
        public LocalBuilder DeclareLocal(Type type, LexToken name, int nestingLevel, LexList theList)
        {
            if (!IL.Active)
            {
                return(null);
            }
            VarInfo found;

            if (VarAccess.TryGetValue(name.Str, out found))
            {
                name.ThrowException("Local name '" + name.Str + "' is already used.", theList);
            }
            LocalBuilder lb = IL.DeclareLocal_opCodes(type, name.Str);

            VarAccess.Add(name.Str, new VarInfo(type, false, LocalNames.Count, nestingLevel, lb));
            LocalNames.Add(name.Str);
            LocalTypes.Add(type);
            return(lb);
        }
Esempio n. 22
0
        // Each method declaration starts with a 'private' or a 'public' and then continues as per a method processed in MakeMethod.cs
        private CompileMethod DoOneMethod(LexList theLexList)
        {
            BindingFlags vis = BindingFlags.Public;

            if (theLexList.Str == "public")
            {
                vis = BindingFlags.Public;
            }
            else if (theLexList.Str == "private")
            {
                vis = BindingFlags.NonPublic;
            }
            else
            {
                theLexList.ThrowException("Expected this method to be marked either public or private.");
            }
            theLexList.Next();
            CompileMethod cm = new CompileMethod(Parser, theLexList, ClassType, vis);

            theLexList.CheckStrAndAdvance("}", "Expected a closing } at the end of the method.");
            return(cm);
        }
Esempio n. 23
0
        public void CheckComparisonTypes(LexList InputList, Type type)
        {
            if ((ResultType == typeof(string) || ResultType == typeof(bool) || ResultType == typeof(char)) && (ResultType == type))
            {
                return;
            }
            //if ((IsIntType(ResultType) || IsRealType(ResultType)) && (IsIntType(type) || IsRealType(type))) return;
            if ((IsIntType(ResultType) && IsIntType(type)) ||
                (IsRealType(ResultType) && IsRealType(type)) ||
                (IsIntType(ResultType) && IsRealType(type)) ||
                (IsRealType(ResultType) && IsIntType(type))

                )
            {
                return;//wootra
            }
            if (ResultType.IsEnum && type.IsEnum && ResultType == type)
            {
                return;
            }
            InputList.ThrowException("The left hand operand type '" + ResultType.ToString() + "'\n is not Comparison compatible with the right hand operand type '" + type.ToString() + "'.");
        }
Esempio n. 24
0
 private Type ParseArrayTypeModifier(LexList list, Type returnType, bool arrayIndexActualsAllowed)
 {
     if (list.Str == "[")
     {
         list.Next();
         if (list.Kind == LexKind.End)
         {
             list.ThrowException("Unexpected end of input.");
         }
         if (!arrayIndexActualsAllowed)
         {
             if (list.Str != "]")
             {
                 list.ThrowException("Expected a ']' here.");
             }
             list.Next();
         }
         Type arrayType = returnType.MakeArrayType();
         while (list.Str == "[")
         {
             list.Next();
             arrayType = arrayType.MakeArrayType();
             if (list.Str != "]")
             {
                 list.ThrowException("Expected a ']' here");
             }
             list.Next();
         }
         if (arrayIndexActualsAllowed)
         {
             list.Prev();
         }
         return(arrayType);
     }
     else
     {
         return(returnType);
     }
 }
Esempio n. 25
0
        private Type[] ParseGenericTypeModifier(LexList list, out bool pendingCloseAngle, BindingFlags visibility)
        {
            list.Next();
            pendingCloseAngle = false;
            List <Type> types   = new List <Type>();
            bool        pending = false;

            while (true)
            {
                types.Add(Parse(list, true, false, out pending, visibility));
                if (pending)
                {
                    break;
                }
                else if (list.Kind == LexKind.End)
                {
                    list.ThrowException("Unexpected end of input.");
                }
                else if (list.Str == ",")
                {
                    list.Next();
                }
                else if (list.Str == ">")
                {
                    list.Next();
                    break;
                }
                else if (list.Str == ">>")
                {
                    list.Next();
                    pendingCloseAngle = true;
                    break;
                }
            }
            return(types.ToArray());
        }
Esempio n. 26
0
        private PacketAction ExamineUserInput(ReadOnlyCollection <string> input)
        {
            LexList ll = LexList.Get(input);

            if (ll.Count == 0)
            {
                return(PacketAction.Empty);
            }
            if (ll[0].Kind != LexKind.Delimiter && ll.Count == 1)
            {
                return(PacketAction.Expression);
            }
            Stack <char> nesting = new Stack <char>();

            foreach (LexToken tok in ll)
            {
                if (tok.Str == "(" || tok.Str == "[" || tok.Str == "{")
                {
                    nesting.Push(tok.Str[0]);
                }
                else if (tok.Str == ")")
                {
                    if (nesting.Count == 0 || nesting.Pop() != '(')
                    {
                        return(PacketAction.Empty);
                    }
                }
                else if (tok.Str == "]")
                {
                    if (nesting.Count == 0 || nesting.Pop() != '[')
                    {
                        return(PacketAction.Empty);
                    }
                }
                else if (tok.Str == "}")
                {
                    if (nesting.Count == 0 || nesting.Pop() != '{')
                    {
                        return(PacketAction.Empty);
                    }
                }
            }
            if (nesting.Count != 0)
            {
                return(PacketAction.Empty);
            }
            if (ll.Count < 2)
            {
                return(PacketAction.Empty);
            }
            string first = ll[0].Str;
            string last  = ll[ll.Count - 1].Str;

            if (first == "(" && last == ")")
            {
                return(PacketAction.Expression);
            }
            if (first == "{" && last == "}")
            {
                return(PacketAction.Statement);
            }
            if (CompileOneMethod.IsVarDeclaration(ll) && last == ";")
            {
                return(PacketAction.Field);
            }
            if (first == "public" || first == "private" || first == "[")
            {
                if (last == "}")
                {
                    return(PacketAction.Method);
                }
                return(PacketAction.Empty);
            }
            if (last == ";")
            {
                return(PacketAction.Statement);
            }
            if (last == "+" || last == "-" || last == "*" || last == "/" || last == "||" || last == "&&" || last == "!" || last == "^")
            {
                return(PacketAction.Empty);
            }
            return(PacketAction.Expression);
        }
Esempio n. 27
0
 public OperandSize(LexList list, ExpState state, OperandSizes sizes, Emit e)
 {
     StartType = state.ResultType; Sizes = sizes; List = list; E = e;
 }
Esempio n. 28
0
 static public LexList GetLexList(this IEnumerable <LexList> ie)
 {
     return(LexList.Get(ie));
 }
Esempio n. 29
0
 static public LexList GetLexList(this string s)
 {
     return(LexList.Get(s));
 }
Esempio n. 30
0
 public LexToken(string str, LexKind kind, LexList list) : this(str, kind, list.CopyList(), list.Index)
 {
 }