Exemple #1
0
 public bool TryGet(AxVariablePtr x, out object o)
 {
     return _variables.TryGetValue(x, out o);
 }
Exemple #2
0
 public object Get(AxVariablePtr x)
 {
     return _variables[x];
 }
 public object TryGetVar(AxVariablePtr var)
 {
     object output;
     var success = Variables.TryGetValue(var, out output);
     return success ? output : null;
 }
        private int ParseParameter(string funcString, Dictionary<string, object> Params, List<object> parameters, int oSpaceIndex, out int end, ref int spaceIndex, out int start)
        {
            start = oSpaceIndex + 1;
            end = spaceIndex;

            while (funcString[start] == ' ' || funcString[start] == '\n')
            {
                start++;
            }

            if (funcString[start] == '"') // String
            {
                var sb = new StringBuilder();
                var j = start + 1;
                var escapeState = false;
                while (funcString[j] != '"' || escapeState)
                {
                    if (escapeState)
                    {
                        switch (funcString[j])
                        {
                            case 'n':
                                sb.Append('\n');
                                break;
                            case 'r':
                                sb.Append('\r');
                                break;
                            case '"':
                                sb.Append('"');
                                break;
                            case 't':
                                sb.Append('\t');
                                break;
                            case '0':
                                sb.Append('\0');
                                break;
                            case '\\':
                                sb.Append('\\');
                                break;
                            default:
                                throw new AxException(this, "Malformed escape sequence");
                        }
                        escapeState = false;
                        j++;
                        continue;
                    }
                    if (funcString[j] == '\\')
                    {
                        escapeState = true;
                    }
                    else
                    {
                        sb.Append(funcString[j]);
                    }
                    j++;
                }

                //parameters.Add(new AxString(sb.ToString()));
                parameters.Add(sb.ToString());
                end = j + 1;
                if (end > funcString.Length)
                {
                    end = funcString.Length;
                }
            }
            else if (char.IsDigit(funcString[start]) || funcString[start] == '-' && char.IsDigit(funcString[start + 1])) // Number
            {
                end = funcString.IndexOf(" ", start);
                if (end == -1)
                {
                    end = funcString.Length;
                }
                var num = funcString.Substring(start, end - start);
                if (num.StartsWith("0x"))
                {
                    int paramInt;
                    if (int.TryParse(num.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out paramInt))
                    {
                        parameters.Add(paramInt);
                    }
                    else
                    {
                        throw Error("Malformed hex '{0}' found.", num);
                    }
                }
                else if (num.EndsWith("l"))
                {
                    long paramLong;
                    if (long.TryParse(num.Substring(0, num.Length - 1), out paramLong))
                    {
                        parameters.Add(paramLong);
                    }
                    else
                    {
                        throw Error("Malformed long '{0}' found.", num);
                    }
                }
                else if (num.EndsWith("f"))
                {
                    float paramFloat;
                    if (float.TryParse(num.Substring(0, num.Length - 1), out paramFloat))
                    {
                        parameters.Add(paramFloat);
                    }
                    else
                    {
                        throw Error("Malformed float '{0}' found.", num);
                    }
                }
                else
                {
                    if (num.Contains('.'))
                    {
                        double paramDbl;
                        if (double.TryParse(num, out paramDbl))
                        {
                            parameters.Add(paramDbl);
                        }
                        else
                        {
                            throw Error("Malformed double '{0}' found.", num);
                        }
                    }
                    else
                    {
                        int paramInt;
                        if (int.TryParse(num, out paramInt))
                        {
                            parameters.Add(paramInt);
                        }
                        else
                        {
                            throw Error("Malformed int '{0}' found.", num);
                        }
                    }
                }
            }
            else switch (funcString[start])
            {
                case '(':
                    {
                        var param = Extract(funcString.Substring(start));
                        parameters.Add(CallFuncFromString(param.Item1, Params));
                        end = start + param.Item2;
                    }
                    break;
                case '{':
                    {
                        var extr = Extract(funcString.Substring(start), '{', '}');
                        var func = extr.Item1;
                        var iparameters = new string[0];
                        var paramindex = 0;
                        while (paramindex < func.Length && func[paramindex] != '|')
                        {
                            if (func[paramindex] == '(')
                            {
                                paramindex = -1;
                                break;
                            }
                            paramindex++;
                        }
                        if (paramindex != -1)
                        {
                            iparameters = func.Substring(0, paramindex).Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).Select(x => Prefix + x).ToArray();
                            func = func.Substring(paramindex + 1);
                        }

                        parameters.Add(new AxFunction(iparameters, func, Prefix));
                        end = start + extr.Item2;
                    }
                    break;
                case '%':
                    {
                        var extr = Extract(funcString.Substring(start + 1), '{', '}');
                        var func = extr.Item1;
                        var iparameters = new string[0];
                        var paramindex = 0;
                        while (func[paramindex] != '|')
                        {
                            paramindex++;
                            if (paramindex >= func.Length || func[paramindex] == '(')
                            {
                                paramindex = -1;
                                break;
                            }
                        }
                        if (paramindex != -1)
                        {
                            iparameters = func.Substring(0, paramindex).Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).Select(x => Prefix + x).ToArray();
                            func = func.Substring(paramindex + 1).TrimStart();
                        }

                        parameters.Add(new AxFunction(iparameters, String.Format("(return ({0}))", func), Prefix, true));
                        end = start + extr.Item2 + 1;
                    }
                    break;
                case '[':
                    {
                        var extr = Extract(funcString.Substring(start), '[', ']');
                        var str = extr.Item1;

                        if (str.IndexOf(',') != -1)
                        {
                            var a = str.Split(',').Select(double.Parse);
                            parameters.Add(a);
                        }
                        else if (str.IndexOf("..") != -1)
                        {
                            var rangediff = 1;
                            string[] temp;
                            if (str.IndexOf('|') != -1)
                            {
                                temp = str.Split('|');
                                if (IsDigitOrMinus(temp[1][0]))
                                {
                                    rangediff = int.Parse(temp[1]);
                                }
                                else
                                {
                                    rangediff = (int)GetVar(Prefix + temp[1], Params);
                                }
                                str = temp[0];
                            }

                            temp = str.Split(new[] {".."}, StringSplitOptions.RemoveEmptyEntries);

                            var rangestartstr = temp[0];
                            var rangeendstr = temp[1];
                            object rangestart = 0;
                            object rangeend = 0;

                            if(IsDigitOrMinus(rangestartstr[0]))
                            {
                                rangestart = int.Parse(rangestartstr);
                            }
                            else
                            {
                                var s = 0;
                                ExtrapolateVariable(rangestartstr, rangestartstr.Length, ref rangestart, ref s);
                                rangestart = (int)GetVar(Prefix+rangestartstr, Params);
                            }

                            if (IsDigitOrMinus(rangeendstr[0]))
                            {
                                rangeend = int.Parse(rangeendstr);
                            }
                            else
                            {
                                var s = 0;
                                ExtrapolateVariable(rangeendstr, rangeendstr.Length, ref rangeend, ref s);
                                rangeend = (int)GetVar(Prefix + rangeendstr, Params);
                            }

                            var dbl = CreateRange((int)rangestart, (int)rangeend, rangediff);

                            parameters.Add(dbl);
                        }

                        end = start + extr.Item2;
                    }
                    break;
                case '*':
                    {
                        end = funcString.IndexOf(" ", start);
                        if (end == -1)
                        {
                            end = funcString.Length;
                        }

                        var ptrfunc = funcString.Substring(start + 1, end - start - 1);

                        if (Functions.ContainsKey(Prefix + ptrfunc))
                        {
                            parameters.Add(Functions[Prefix + ptrfunc]);
                        }
                        else
                        {
                            throw Error("Invalid function refference: \"{0}\", function not found.", ptrfunc);
                        }
                    }
                    break;
                case '^':
                    {
                        end = funcString.IndexOf(" ", start);
                        if (end == -1)
                        {
                            end = funcString.Length;
                        }

                        var Variable = funcString.Substring(start + 1, end - start - 1);

                        var varType = VariableType.Null;
                        if (Params.ContainsKey(Variable))
                        {
                            varType = VariableType.Local;
                        }
                        else if (Variables.ContainsKey(Variable))
                        {
                            varType = VariableType.Global;
                        }

                        var varptr = new AxVariablePtr(Prefix + Variable, varType);

                        parameters.Add(varptr);
                    }
                    break;
                case '@':
                    {
                        start++;
                        end = funcString.IndexOf(" ", start);
                        if (end == -1)
                        {
                            end = funcString.Length;
                        }
                        var locindex = funcString.IndexOf("[", start, end - start);
                        var loc2index = funcString.IndexOf(".", start + 1, end - start - 1);
                        if (loc2index != -1 && loc2index < locindex)
                        {
                            locindex = loc2index;
                        }
                        var varName = locindex != -1 ? funcString.Substring(start, locindex - start) : funcString.Substring(start, end - start);
                        varName = Prefix + varName;
                        var var = GetVar(varName, Params);
                        ExtrapolateVariable(funcString, end, ref var, ref locindex);

                        parameters.Add(var);
                    }
                    break;
                case '$':
                    {
                        end = funcString.IndexOf(" ", start);
                        if (end == -1)
                        {
                            end = funcString.Length;
                        }
                        var locindex = funcString.IndexOf("[", start + 1, end - start - 1);
                        var loc2index = funcString.IndexOf(".", start + 1, end - start - 1);
                        if (loc2index != -1 && loc2index < locindex)
                        {
                            locindex = loc2index;
                        }
                        var varName = locindex != -1 ? funcString.Substring(start + 1, locindex - start - 1) : funcString.Substring(start + 1, end - start - 1);

                        var var = Variables[Prefix + varName];
                        ExtrapolateVariable(funcString, end, ref var, ref locindex);

                        parameters.Add(var);
                    }
                    break;
                case '#':
                    {
                        end = funcString.IndexOf(" ", start);
                        if (end == -1)
                        {
                            end = funcString.Length;
                        }
                        var locindex = funcString.IndexOf("[", start, end - start);
                        var loc2index = funcString.IndexOf(".", start + 1, end - start - 1);

                        if (loc2index != -1 && (loc2index < locindex || locindex == -1))
                        {
                            locindex = loc2index;
                        }
                        var varName = locindex != -1 ? funcString.Substring(start + 1, locindex - start - 1) : funcString.Substring(start + 1, end - start - 1);
                        var Var = GetVar(varName, Params);
                        ExtrapolateVariable(funcString, end, ref Var, ref locindex);

                        parameters.Add(Var);
                    }
                    break;
                default:
                    {
                        end = funcString.IndexOf(" ", start);
                        if (end == -1)
                        {
                            end = funcString.Length;
                        }
                        var locindex = funcString.IndexOf("[", start, end - start);
                        var loc2index = funcString.IndexOf(".", start + 1, end - start - 1);

                        if (loc2index != -1 && (loc2index < locindex || locindex == -1))
                        {
                            locindex = loc2index;
                        }
                        var varName = locindex != -1 ? funcString.Substring(start, locindex - start) : funcString.Substring(start, end - start);
                        varName = Prefix + varName;
                        var var = GetVar(varName, Params);
                        ExtrapolateVariable(funcString, end, ref var, ref locindex);

                        parameters.Add(var);
                    }
                    break;
            }

            oSpaceIndex = end;

            spaceIndex = end + 1;
            return oSpaceIndex;
        }
        public object Set(AxVariablePtr var, params object[] values)
        {
            if (var.Type == VariableType.Local)
            {
                throw Error("Cannot use global set to set local variable \"{0}\", please use local set instead.", var);
            }
            var nValues = values.Length == 1 ? values[0] : new List<object>(values);
            if (Variables.ContainsKey(var))
            {
                Variables[var] = nValues;
            }
            else
            {
                Variables.Add(var, nValues);
            }

            return Variables[var];
        }