示例#1
0
        public override bool Build(ref CodeNode _this, int expressionDepth, Dictionary <string, VariableDescriptor> variables, CodeContext codeContext, InternalCompilerMessageCallback message, FunctionInfo stats, Options opts)
        {
            _codeContext = codeContext;

            VariableDescriptor desc = null;

            if (!variables.TryGetValue(_variableName, out desc) || desc == null)
            {
                desc = new VariableDescriptor(this, 1)
                {
                    isDefined = false
                };
                variables[_variableName] = this.Descriptor;
            }
            else
            {
                if (!desc.references.Contains(this))
                {
                    desc.references.Add(this);
                }
                _descriptor = desc;
            }

            if (_variableName == "this")
            {
                stats.ContainsThis        = true;
                desc.definitionScopeLevel = -1;
            }
            else if (((codeContext & CodeContext.InWith) != 0) || (stats.ContainsEval && !desc.isDefined))
            {
                ScopeLevel = -Math.Abs(ScopeLevel);
                desc.definitionScopeLevel = -Math.Abs(desc.definitionScopeLevel);
            }

            _forceThrow |= desc.lexicalScope;

            if (expressionDepth >= 0 && expressionDepth < 2 && desc.IsDefined && !desc.lexicalScope && (opts & Options.SuppressUselessExpressionsElimination) == 0)
            {
                _this      = null;
                Eliminated = true;
                if (message != null)
                {
                    message(MessageLevel.Warning, Position, Length, "Unused getting of defined variable was removed. Maybe something missing.");
                }
            }
            else if (_variableName == "arguments" && (codeContext & CodeContext.InFunction) != 0)
            {
                if (stats != null)
                {
                    stats.ContainsArguments = true;
                }

                _this = new GetArgumentsExpression(ScopeLevel)
                {
                    _descriptor = _descriptor
                };
            }

            return(false);
        }
示例#2
0
        public override void RebuildScope(FunctionInfo functionInfo, Dictionary <string, VariableDescriptor> transferedVariables, int scopeBias)
        {
            body.RebuildScope(functionInfo, transferedVariables, scopeBias);

            if (catchBody != null)
            {
                VariableDescriptor variableToRestore = null;
                if (transferedVariables != null)
                {
                    transferedVariables.TryGetValue(catchVariableDesc.name, out variableToRestore);
                    transferedVariables[catchVariableDesc.name] = catchVariableDesc;
                }

                catchBody.RebuildScope(functionInfo, transferedVariables, scopeBias);

                if (transferedVariables != null)
                {
                    if (variableToRestore != null)
                    {
                        transferedVariables[variableToRestore.name] = variableToRestore;
                    }
                    else
                    {
                        transferedVariables.Remove(catchVariableDesc.name);
                    }
                }
            }

            finallyBody?.RebuildScope(functionInfo, transferedVariables, scopeBias);
        }
示例#3
0
        public override bool Build(ref CodeNode _this, int expressionDepth, Dictionary <string, VariableDescriptor> variables, CodeContext codeContext, InternalCompilerMessageCallback message, FunctionInfo stats, Options opts)
        {
            if (stats != null)
            {
                stats.UseCall = true;
            }

            this._codeContext = codeContext;

            var super = _left as Super;

            if (super != null)
            {
                super.ctorMode = true;
                _callMode      = CallMode.Super;
            }

            for (var i = 0; i < _arguments.Length; i++)
            {
                Parser.Build(ref _arguments[i], expressionDepth + 1, variables, codeContext | CodeContext.InExpression, message, stats, opts);
            }

            base.Build(ref _this, expressionDepth, variables, codeContext, message, stats, opts);
            if (_left is Variable)
            {
                var name = _left.ToString();
                if (name == "eval" && stats != null)
                {
                    stats.ContainsEval = true;
                    foreach (var variable in variables)
                    {
                        variable.Value.captured = true;
                    }
                }
                VariableDescriptor f = null;
                if (variables.TryGetValue(name, out f))
                {
                    var func = f.initializer as FunctionDefinition;
                    if (func != null)
                    {
                        for (var i = 0; i < func.parameters.Length; i++)
                        {
                            if (i >= _arguments.Length)
                            {
                                break;
                            }
                            if (func.parameters[i].lastPredictedType == PredictedType.Unknown)
                            {
                                func.parameters[i].lastPredictedType = _arguments[i].ResultType;
                            }
                            else if (Tools.CompareWithMask(func.parameters[i].lastPredictedType, _arguments[i].ResultType, PredictedType.Group) != 0)
                            {
                                func.parameters[i].lastPredictedType = PredictedType.Ambiguous;
                            }
                        }
                    }
                }
            }
            return(false);
        }
示例#4
0
        public static void ProccessDataSet(IDataSet <int, int> data_set)
        {
            //DataSetHybridFloat32 data_set = ReadSer();
            IDataContext data_context = data_set.DataContext;

            // 1.  Prediction Classical :
            // 1.1 Check single feature predictors for label
            for (int feature_0_index = 0; feature_0_index < data_context.FeatureCount; feature_0_index++)
            {
                VariableDescriptor feature_descriptor = data_context.GetFeatureDescriptor(feature_0_index);
                //data_set_labeled.GetFeatureData(feature_0_index);
            }
            // 1.2 Check double feature predictors for label

            // 1.3 split the data in a test and training set
            Tuple <IDataSet <int, int>, IDataSet <int, int> > split = data_set.Split();
            IDataSet <int, int> training_set = split.Item1;
            IDataSet <int, int> test_set     = split.Item2;
            // 1.4 Check greedy joint predictors for label



            // 2.  Prediction Advanced
            // 2.1 Naive bayes (bag of words like)  Report ROC, Best Feature space


            // 2.2 For each level build random forests                  Report ROC, Best Feature space
            //ROCReport random_forest_report = BuildAndEvaluate();
            // 3.  Clustering Classical
            // 3.1 Correlation clustering shortest link, complete link. Report cluster typing at point

            // 4.  Clustering Advance
            // 4.1 Correlation clustering shortest link, complete link. Report cluster typing at point
        }
示例#5
0
 public ModelFeatureInterval(VariableDescriptor feature_descriptor)
 {
     if (feature_descriptor.DataLevel != DataLevel.INTERVAL)
     {
         throw new Exception("Not a interval feature");
     }
     this.feature_descriptor = feature_descriptor;
     this.Name = feature_descriptor.Name;
 }
示例#6
0
        public override bool Build(ref CodeNode _this, int expressionDepth, Dictionary <string, VariableDescriptor> variables, CodeContext codeContext, InternalCompilerMessageCallback message, FunctionInfo stats, Options opts)
        {
            if (Built)
            {
                return(false);
            }
            Built = true;

            _codeContext = codeContext;

            if ((codeContext & CodeContext.InExpression) == 0)
            {
                stats.WithLexicalEnvironment = true;
            }

            VariableDescriptor descriptorToRestore = null;

            if (!string.IsNullOrEmpty(Name))
            {
                variables.TryGetValue(Name, out descriptorToRestore);
                variables[Name] = Reference._descriptor;
            }

            Parser.Build(ref _constructor, expressionDepth, variables, codeContext | CodeContext.InClassDefenition | CodeContext.InClassConstructor, message, stats, opts);
            Parser.Build(ref _baseClass, expressionDepth, variables, codeContext, message, stats, opts);

            foreach (var member in _members)
            {
                Parser.Build
                (
                    ref member._value,
                    expressionDepth,
                    variables,
                    codeContext | CodeContext.InClassDefenition | (member._static ? CodeContext.InStaticMember : 0),
                    message,
                    stats,
                    opts
                );
            }

            foreach (var prop in computedProperties)
            {
                Parser.Build(ref prop._name, 2, variables, codeContext | CodeContext.InExpression, message, stats, opts);
                Parser.Build(ref prop._value, 2, variables, codeContext | CodeContext.InExpression, message, stats, opts);
            }

            if (descriptorToRestore != null)
            {
                variables[descriptorToRestore.name] = descriptorToRestore;
            }
            else if (!string.IsNullOrEmpty(Name))
            {
                variables.Remove(Name);
            }

            return(false);
        }
示例#7
0
 public EntityReference(EntityDefinition entityDefinition)
 {
     ScopeLevel  = 1;
     _descriptor = new VariableDescriptor(entityDefinition.Name, 1)
     {
         lexicalScope = !entityDefinition.Hoist,
         initializer  = entityDefinition
     };
 }
示例#8
0
文件: TryCatch.cs 项目: lulzzz/sito
        public override bool Build(ref CodeNode _this, int expressionDepth, Dictionary <string, VariableDescriptor> variables, CodeContext codeContext, InternalCompilerMessageCallback message, FunctionInfo stats, Options opts)
        {
            if (stats != null)
            {
                stats.ContainsTry = true;
            }

            Parser.Build(ref body, expressionDepth, variables, codeContext | CodeContext.Conditional, message, stats, opts);
            var catchPosition = Position;

            if (catchBody != null)
            {
                _catch = true;
                catchVariableDesc.owner = this;
                VariableDescriptor oldVarDesc = null;
                variables.TryGetValue(catchVariableDesc.name, out oldVarDesc);
                variables[catchVariableDesc.name] = catchVariableDesc;
                catchPosition = catchBody.Position;
                Parser.Build(ref catchBody, expressionDepth, variables, codeContext | CodeContext.Conditional, message, stats, opts);
                if (oldVarDesc != null)
                {
                    variables[catchVariableDesc.name] = oldVarDesc;
                }
                else
                {
                    variables.Remove(catchVariableDesc.name);
                }
            }

            var finallyPosition = 0;

            if (finallyBody != null)
            {
                finallyPosition = finallyBody.Position;
                Parser.Build(ref finallyBody, expressionDepth, variables, codeContext, message, stats, opts);
            }

            if (body == null || (body is Empty))
            {
                message?.Invoke(MessageLevel.Warning, Position, Length, "Empty (or reduced to empty) try" + (catchBody != null ? "..catch" : "") + (finallyBody != null ? "..finally" : "") + " block. Maybe, something missing.");

                _this = finallyBody;
            }

            if (_catch && (catchBody == null || (catchBody is Empty)))
            {
                message?.Invoke(MessageLevel.Warning, catchPosition, (catchBody ?? this).Length, "Empty (or reduced to empty) catch block. Do not ignore exceptions.");
            }

            if (finallyPosition != 0 && (finallyBody == null || (finallyBody is Empty)))
            {
                message?.Invoke(MessageLevel.Warning, catchPosition, (catchBody ?? this).Length, "Empty (or reduced to empty) finally block.");
            }

            return(false);
        }
示例#9
0
        private static void makeAndSaveProblemDefinition()
        {
            var pd = new ProblemDefinition();

            /* Add a design space descriptor so that optimizatoin
             * methods for discrete variables can be used. Here we
             * make a very generous discretization, which amounts
             * to 2 million steps in each of the 2 design variables. */
            var dsd = new DesignSpaceDescription(2);

            for (var i = 0; i < 2; i++)
            {
                dsd[i] = new VariableDescriptor(-5000, 5000, 100.0);
            }
            pd.Add(dsd);

            /* Add three convergence criteria */
            pd.Add(new DeltaXConvergence(0.0001));
            pd.Add(new MaxAgeConvergence(100, 0.000000001));
            pd.Add(new MaxFnEvalsConvergence(50000));
            pd.Add(new MaxSpanInPopulationConvergence(1));

            /* setting the number of convergence criteria needed is not necessary
             * since we will be using the default value of 1. Interesting to un-
             * comment the next line and see how it affects the process. */
            //pd.NumConvergeCriteriaNeeded = 2;

            /* Add the objective function. */
            var objfn = new polynomialObjFn();

            objfn.Add("x1^2");
            objfn.Add("x2^2");
            objfn.Add("-2*x1");
            objfn.Add("-10*x2");
            objfn.Add("26");
            /* this is a simple parabola center at {1, 5} */
            pd.Add(objfn);

            var g1 = new polynomialInequality();

            g1.Add("-x1");
            g1.Add("x2"); /* this inequality translates to x2 - x1 < 0
                           * of simply x1 > x2. */
            pd.Add(g1);

            pd.Add(new double[] { 1500.0, 700.0 });
            var stream = new FileStream(filename, FileMode.Create);

            pd.SaveProbToXml(stream);
        }
示例#10
0
        internal static VariableDescriptor[] extractVariables(ParseInfo state, int oldVariablesCount)
        {
            VariableDescriptor[] variables = emptyVariables;
            var count = 0;

            for (var i = oldVariablesCount; i < state.Variables.Count; i++)
            {
                if (state.Variables[i].definitionScopeLevel == state.lexicalScopeLevel)
                {
                    count++;
                }
            }

            if (count > 0)
            {
                variables = new VariableDescriptor[count];
                HashSet <string> declaredVariables = null;
                if (state.lexicalScopeLevel != state.functionScopeLevel)
                {
                    declaredVariables = new HashSet <string>();
                }

                for (int i = oldVariablesCount, targetIndex = 0; i < state.Variables.Count; i++)
                {
                    if (state.Variables[i].definitionScopeLevel == state.lexicalScopeLevel)
                    {
                        variables[targetIndex] = state.Variables[i];
                        if (declaredVariables != null)
                        {
                            if (declaredVariables.Contains(variables[targetIndex].name) && variables[targetIndex].lexicalScope)
                            {
                                ExceptionHelper.ThrowSyntaxError("Variable \"" + variables[targetIndex].name + "\" already has been defined", state.Code, i);
                            }

                            declaredVariables.Add(variables[targetIndex].name);
                        }
                        targetIndex++;
                    }
                    else if (targetIndex != 0)
                    {
                        state.Variables[i - targetIndex] = state.Variables[i];
                    }
                }

                state.Variables.RemoveRange(state.Variables.Count - count, count);
            }

            return(variables);
        }
示例#11
0
        internal Merker GetMerker(VariableDescriptor v)
        {
            Merker m = null;

            m = cur.memory.FirstOrDefault(z => z.vd == v);
            if (m == null)
            {
                m = global.memory.FirstOrDefault(z => z.vd == v);
            }
            if (m == null)
            {
                m = LoadNativeFunctions(v);
            }

            return(m);
        }
示例#12
0
        private void AddDependences(AnnotationInstance ci)
        {
            VariableCollector vc = new VariableCollector();

            vc.VisitExpr(ci.Expr);

            foreach (var v in vc.usedVars.Where(Item => varDepAnalyser.VariableRelevantToAnalysis(Item, ci.Proc)))
            {
                VariableDescriptor vd =
                    varDepAnalyser.MakeDescriptor(ci.Proc, v);
                if (!variableDirectlyReferredToByAnnotations.ContainsKey(vd))
                {
                    variableDirectlyReferredToByAnnotations[vd] = new HashSet <string>();
                }
                variableDirectlyReferredToByAnnotations[vd].Add(ci.AnnotationIdentifier);

                foreach (var w in varDepAnalyser.DependsOn(vd))
                {
                    annotationDependsOn[ci.AnnotationIdentifier].Add(w);
                }
            }
        }
 public ModelFeatureNominal(VariableDescriptor feature_descriptor)
 {
     if (feature_descriptor.DataLevel != DataLevel.NOMINAL)
     {
         throw new Exception("Not a nominal feature");
     }
     this.feature_descriptor = feature_descriptor;
     this.Name = feature_descriptor.Name;
     if (feature_descriptor.ValueCount < 20)
     {
         this.FeatureValueTypes = feature_descriptor.ValueStrings;
     }
     else
     {
         IList <string> all_value_types      = feature_descriptor.ValueStrings;
         IList <string> selected_value_types = new List <string>();
         for (int index = 0; index < 20; index++)
         {
             selected_value_types.Add(all_value_types[index]);
         }
         selected_value_types.Add("And many others");
         this.FeatureValueTypes = selected_value_types;
     }
 }
示例#14
0
        internal static Expression Parse(ParseInfo state, ref int index, FunctionKind kind)
        {
            string code     = state.Code;
            int    position = index;

            switch (kind)
            {
            case FunctionKind.AsyncAnonymousFunction:
            case FunctionKind.AnonymousFunction:
            case FunctionKind.AnonymousGenerator:
            {
                break;
            }

            case FunctionKind.Function:
            {
                if (!Parser.Validate(code, "function", ref position))
                {
                    return(null);
                }

                if (code[position] == '*')
                {
                    kind = FunctionKind.Generator;
                    position++;
                }
                else if ((code[position] != '(') && (!Tools.IsWhiteSpace(code[position])))
                {
                    return(null);
                }

                break;
            }

            case FunctionKind.Getter:
            {
                if (!Parser.Validate(code, "get ", ref position))
                {
                    return(null);
                }

                break;
            }

            case FunctionKind.Setter:
            {
                if (!Parser.Validate(code, "set ", ref position))
                {
                    return(null);
                }

                break;
            }

            case FunctionKind.MethodGenerator:
            case FunctionKind.Method:
            {
                if (Parser.Validate(code, "async", ref position))
                {
                    kind = FunctionKind.AsyncMethod;
                }

                if (code[position] == '*')
                {
                    kind = FunctionKind.MethodGenerator;
                    position++;
                }
                else if (kind == FunctionKind.MethodGenerator)
                {
                    throw new ArgumentException("mode");
                }

                break;
            }

            case FunctionKind.AsyncArrow:
            {
                if (!Parser.Validate(code, "async", ref position))
                {
                    return(null);
                }

                break;
            }

            case FunctionKind.Arrow:
            {
                break;
            }

            case FunctionKind.AsyncFunction:
            {
                if (!Parser.Validate(code, "async", ref position))
                {
                    return(null);
                }

                Tools.SkipSpaces(code, ref position);

                if (!Parser.Validate(code, "function", ref position))
                {
                    return(null);
                }

                break;
            }

            default:
                throw new NotImplementedException(kind.ToString());
            }

            Tools.SkipSpaces(state.Code, ref position);

            var       parameters                = new List <ParameterDescriptor>();
            CodeBlock body                      = null;
            string    name                      = null;
            bool      arrowWithSinglePrm        = false;
            int       nameStartPos              = 0;
            bool      containsDestructuringPrms = false;

            if (kind != FunctionKind.Arrow)
            {
                if (code[position] != '(')
                {
                    nameStartPos = position;
                    if (Parser.ValidateName(code, ref position, false, true, state.strict))
                    {
                        name = Tools.Unescape(code.Substring(nameStartPos, position - nameStartPos), state.strict);
                    }
                    else if ((kind == FunctionKind.Getter || kind == FunctionKind.Setter) && Parser.ValidateString(code, ref position, false))
                    {
                        name = Tools.Unescape(code.Substring(nameStartPos + 1, position - nameStartPos - 2), state.strict);
                    }
                    else if ((kind == FunctionKind.Getter || kind == FunctionKind.Setter) && Parser.ValidateNumber(code, ref position))
                    {
                        name = Tools.Unescape(code.Substring(nameStartPos, position - nameStartPos), state.strict);
                    }
                    else
                    {
                        ExceptionHelper.ThrowSyntaxError("Invalid function name", code, nameStartPos, position - nameStartPos);
                    }

                    Tools.SkipSpaces(code, ref position);

                    if (code[position] != '(')
                    {
                        ExceptionHelper.ThrowUnknownToken(code, position);
                    }
                }
                else if (kind == FunctionKind.Getter || kind == FunctionKind.Setter)
                {
                    ExceptionHelper.ThrowSyntaxError("Getter and Setter must have name", code, index);
                }
                else if (kind == FunctionKind.Method || kind == FunctionKind.MethodGenerator || kind == FunctionKind.AsyncMethod)
                {
                    ExceptionHelper.ThrowSyntaxError("Method must has name", code, index);
                }

                position++;
            }
            else if (code[position] != '(')
            {
                arrowWithSinglePrm = true;
            }
            else
            {
                position++;
            }

            Tools.SkipSpaces(code, ref position);

            if (code[position] == ',')
            {
                ExceptionHelper.ThrowSyntaxError(Strings.UnexpectedToken, code, position);
            }

            while (code[position] != ')')
            {
                if (parameters.Count == 255 || (kind == FunctionKind.Setter && parameters.Count == 1) || kind == FunctionKind.Getter)
                {
                    ExceptionHelper.ThrowSyntaxError(string.Format(Strings.TooManyArgumentsForFunction, name), code, index);
                }

                bool rest = Parser.Validate(code, "...", ref position);

                Expression destructor = null;
                int        n          = position;
                if (!Parser.ValidateName(code, ref position, state.strict))
                {
                    if (code[position] == '{')
                    {
                        destructor = (Expression)ObjectDefinition.Parse(state, ref position);
                    }
                    else if (code[position] == '[')
                    {
                        destructor = (Expression)ArrayDefinition.Parse(state, ref position);
                    }

                    if (destructor == null)
                    {
                        ExceptionHelper.ThrowUnknownToken(code, nameStartPos);
                    }

                    containsDestructuringPrms = true;
                }

                var pname     = Tools.Unescape(code.Substring(n, position - n), state.strict);
                var reference = new ParameterReference(pname, rest, state.lexicalScopeLevel + 1)
                {
                    Position = n,
                    Length   = position - n
                };
                var desc = reference.Descriptor as ParameterDescriptor;

                if (destructor != null)
                {
                    desc.Destructor = new ObjectDesctructor(destructor);
                }

                parameters.Add(desc);

                Tools.SkipSpaces(state.Code, ref position);
                if (arrowWithSinglePrm)
                {
                    position--;
                    break;
                }

                if (code[position] == '=')
                {
                    if (rest)
                    {
                        ExceptionHelper.ThrowSyntaxError("Rest parameters can not have an initializer", code, position);
                    }
                    do
                    {
                        position++;
                    }while (Tools.IsWhiteSpace(code[position]));
                    desc.initializer = ExpressionTree.Parse(state, ref position, false, false) as Expression;
                }

                if (code[position] == ',')
                {
                    if (rest)
                    {
                        ExceptionHelper.ThrowSyntaxError("Rest parameters must be the last in parameters list", code, position);
                    }
                    do
                    {
                        position++;
                    }while (Tools.IsWhiteSpace(code[position]));
                }
            }

            if (kind == FunctionKind.Setter)
            {
                if (parameters.Count != 1)
                {
                    ExceptionHelper.ThrowSyntaxError("Setter must has only one argument", code, index);
                }
            }

            position++;
            Tools.SkipSpaces(code, ref position);

            if (kind == FunctionKind.Arrow || kind == FunctionKind.AsyncArrow)
            {
                if (!Parser.Validate(code, "=>", ref position))
                {
                    ExceptionHelper.ThrowSyntaxError("Expected \"=>\"", code, position);
                }
                Tools.SkipSpaces(code, ref position);
            }

            if (code[position] != '{')
            {
                var oldFunctionScopeLevel = state.functionScopeLevel;
                state.functionScopeLevel = ++state.lexicalScopeLevel;

                try
                {
                    if (kind == FunctionKind.Arrow || kind == FunctionKind.AsyncArrow)
                    {
                        body = new CodeBlock(new CodeNode[]
                        {
                            new Return(ExpressionTree.Parse(state, ref position, processComma: false) as Expression)
                        })
                        {
                            _variables = new VariableDescriptor[0]
                        };

                        body.Position = body._lines[0].Position;
                        body.Length   = body._lines[0].Length;
                    }
                    else
                    {
                        ExceptionHelper.ThrowUnknownToken(code, position);
                    }
                }
                finally
                {
                    state.functionScopeLevel = oldFunctionScopeLevel;
                    state.lexicalScopeLevel--;
                }
            }
            else
            {
                var oldCodeContext = state.CodeContext;
                state.CodeContext &= ~(CodeContext.InExpression | CodeContext.Conditional | CodeContext.InEval);
                if (kind == FunctionKind.Generator || kind == FunctionKind.MethodGenerator || kind == FunctionKind.AnonymousGenerator)
                {
                    state.CodeContext |= CodeContext.InGenerator;
                }
                else if (kind == FunctionKind.AsyncFunction || kind == FunctionKind.AsyncMethod || kind == FunctionKind.AsyncAnonymousFunction || kind == FunctionKind.AsyncArrow)
                {
                    state.CodeContext |= CodeContext.InAsync;
                }
                state.CodeContext |= CodeContext.InFunction;

                var labels = state.Labels;
                state.Labels = new List <string>();
                state.AllowReturn++;
                try
                {
                    state.AllowBreak.Push(false);
                    state.AllowContinue.Push(false);
                    state.AllowDirectives = true;
                    body = CodeBlock.Parse(state, ref position) as CodeBlock;
                    if (containsDestructuringPrms)
                    {
                        var destructuringTargets = new List <VariableDescriptor>();
                        var assignments          = new List <Expression>();
                        for (var i = 0; i < parameters.Count; i++)
                        {
                            if (parameters[i].Destructor != null)
                            {
                                var targets = parameters[i].Destructor.GetTargetVariables();
                                for (var j = 0; j < targets.Count; j++)
                                {
                                    destructuringTargets.Add(new VariableDescriptor(targets[j].Name, state.functionScopeLevel));
                                }

                                assignments.Add(new Assignment(parameters[i].Destructor, parameters[i].references[0]));
                            }
                        }

                        var newLines = new CodeNode[body._lines.Length + 1];
                        System.Array.Copy(body._lines, 0, newLines, 1, body._lines.Length);
                        newLines[0] = new VariableDefinition(destructuringTargets.ToArray(), assignments.ToArray(), VariableKind.AutoGeneratedParameters);
                        body._lines = newLines;
                    }
                }
                finally
                {
                    state.CodeContext = oldCodeContext;
                    state.AllowBreak.Pop();
                    state.AllowContinue.Pop();
                    state.AllowDirectives = false;
                    state.Labels          = labels;
                    state.AllowReturn--;
                }

                if (kind == FunctionKind.Function && string.IsNullOrEmpty(name))
                {
                    kind = FunctionKind.AnonymousFunction;
                }
            }

            if (body._strict || (parameters.Count > 0 && parameters[parameters.Count - 1].IsRest) || kind == FunctionKind.Arrow)
            {
                for (var j = parameters.Count; j-- > 1;)
                {
                    for (var k = j; k-- > 0;)
                    {
                        if (parameters[j].Name == parameters[k].Name)
                        {
                            ExceptionHelper.ThrowSyntaxError("Duplicate names of function parameters not allowed in strict mode", code, index);
                        }
                    }
                }

                if (name == "arguments" || name == "eval")
                {
                    ExceptionHelper.ThrowSyntaxError("Functions name can not be \"arguments\" or \"eval\" in strict mode at", code, index);
                }

                for (int j = parameters.Count; j-- > 0;)
                {
                    if (parameters[j].Name == "arguments" || parameters[j].Name == "eval")
                    {
                        ExceptionHelper.ThrowSyntaxError("Parameters name cannot be \"arguments\" or \"eval\" in strict mode at", code, parameters[j].references[0].Position, parameters[j].references[0].Length);
                    }
                }
            }

            var func = new FunctionDefinition(name)
            {
                parameters = parameters.ToArray(),
                _body      = body,
                kind       = kind,
                Position   = index,
                Length     = position - index,
#if DEBUG
                trace = body.directives != null?body.directives.Contains("debug trace") : false
#endif
            };

            if (!string.IsNullOrEmpty(name))
            {
                func.Reference.ScopeLevel = state.lexicalScopeLevel;
                func.Reference.Position   = nameStartPos;
                func.Reference.Length     = name.Length;

                func.reference._descriptor.definitionScopeLevel = func.reference.ScopeLevel;
            }

            if (parameters.Count != 0)
            {
                var newVariablesCount = body._variables.Length + parameters.Count;

                for (var j = 0; j < body._variables.Length; j++)
                {
                    for (var k = 0; k < parameters.Count; k++)
                    {
                        if (body._variables[j].name == parameters[k].name)
                        {
                            newVariablesCount--;
                            break;
                        }
                    }
                }

                var newVariables = new VariableDescriptor[newVariablesCount];
                for (var j = 0; j < parameters.Count; j++)
                {
                    newVariables[j] = parameters[parameters.Count - j - 1]; // порядок определяет приоритет
                    for (var k = 0; k < body._variables.Length; k++)
                    {
                        if (body._variables[k] != null && body._variables[k].name == parameters[j].name)
                        {
                            if (body._variables[k].initializer != null)
                            {
                                newVariables[j] = body._variables[k];
                            }
                            else
                            {
                                body._variables[k].lexicalScope = false;
                            }
                            body._variables[k] = null;
                            break;
                        }
                    }
                }

                for (int j = 0, k = parameters.Count; j < body._variables.Length; j++)
                {
                    if (body._variables[j] != null)
                    {
                        newVariables[k++] = body._variables[j];
                    }
                }

                body._variables = newVariables;
            }

            if ((state.CodeContext & CodeContext.InExpression) == 0 && kind == FunctionKind.Function)
            // Позволяет делать вызов сразу при объявлении функции
            // (в таком случае функция не добавляется в контекст).
            // Если убрать проверку, то в тех сулчаях,
            // когда определение и вызов стоят внутри выражения,
            // будет выдано исключение, потому,
            // что тогда это уже не определение и вызов функции,
            // а часть выражения, которые не могут начинаться со слова "function".
            // За красивыми словами "может/не может" кроется другая хрень: если бы это было выражение,
            // то прямо тут надо было бы разбирать тот оператор, который стоит после определения функции,
            // что не разумно
            {
                var tindex = position;
                while (position < code.Length && Tools.IsWhiteSpace(code[position]) && !Tools.IsLineTerminator(code[position]))
                {
                    position++;
                }

                if (position < code.Length && code[position] == '(')
                {
                    var args = new List <Expression>();
                    position++;
                    for (; ;)
                    {
                        while (Tools.IsWhiteSpace(code[position]))
                        {
                            position++;
                        }
                        if (code[position] == ')')
                        {
                            break;
                        }
                        else if (code[position] == ',')
                        {
                            do
                            {
                                position++;
                            }while (Tools.IsWhiteSpace(code[position]));
                        }
                        args.Add(ExpressionTree.Parse(state, ref position, false, false));
                    }

                    position++;
                    index = position;
                    while (position < code.Length && Tools.IsWhiteSpace(code[position]))
                    {
                        position++;
                    }

                    if (position < code.Length && code[position] == ';')
                    {
                        ExceptionHelper.Throw((new SyntaxError("Expression can not start with word \"function\"")));
                    }

                    return(new Call(func, args.ToArray()));
                }
                else
                {
                    position = tindex;
                }
            }

            if ((state.CodeContext & CodeContext.InExpression) == 0 &&
                (kind != FunctionKind.Arrow || (state.CodeContext & CodeContext.InEval) == 0))
            {
                if (string.IsNullOrEmpty(name))
                {
                    ExceptionHelper.ThrowSyntaxError("Function must has name", state.Code, index);
                }

                state.Variables.Add(func.reference._descriptor);
            }

            index = position;
            return(func);
        }
示例#15
0
        public override bool Build(ref CodeNode _this, int expressionDepth, Dictionary <string, VariableDescriptor> variables, CodeContext codeContext, InternalCompilerMessageCallback message, FunctionInfo stats, Options opts)
        {
            if (_body.built)
            {
                return(false);
            }

            if (stats != null)
            {
                stats.ContainsInnerEntities = true;
            }

            _codeContext = codeContext;

            if ((codeContext & CodeContext.InLoop) != 0 && message != null)
            {
                message(MessageLevel.Warning, Position, EndPosition - Position, Strings.FunctionInLoop);
            }

            /*
             *  Если переменная за время построения функции получит хоть одну ссылку плюсом,
             *  значит её следует пометить захваченной. Для этого необходимо запомнить количество
             *  ссылок для всех пеменных
             */
            var numbersOfReferences = new Dictionary <string, int>();

            foreach (var variable in variables)
            {
                numbersOfReferences[variable.Key] = variable.Value.references.Count;
            }

            VariableDescriptor descriptorToRestore = null;

            if (!string.IsNullOrEmpty(_name))
            {
                variables.TryGetValue(_name, out descriptorToRestore);
                variables[_name] = reference._descriptor;
            }

            _functionInfo.ContainsRestParameters = parameters.Length > 0 && parameters[parameters.Length - 1].IsRest;

            var bodyCode = _body as CodeNode;

            bodyCode.Build(
                ref bodyCode,
                0,
                variables,
                codeContext & ~(CodeContext.Conditional
                                | CodeContext.InExpression
                                | CodeContext.InEval)
                | CodeContext.InFunction,
                message,
                _functionInfo,
                opts);
            _body = bodyCode as CodeBlock;

            if (message != null)
            {
                for (var i = parameters.Length; i-- > 0;)
                {
                    if (parameters[i].ReferenceCount == 1)
                    {
                        message(MessageLevel.Recomendation, parameters[i].references[0].Position, 0, "Unused parameter \"" + parameters[i].name + "\"");
                    }
                    else
                    {
                        break;
                    }
                }
            }

            _body._suppressScopeIsolation = SuppressScopeIsolationMode.Suppress;
            checkUsings();
            if (stats != null)
            {
                stats.ContainsDebugger     |= _functionInfo.ContainsDebugger;
                stats.ContainsEval         |= _functionInfo.ContainsEval;
                stats.ContainsInnerEntities = true;
                stats.ContainsTry          |= _functionInfo.ContainsTry;
                stats.ContainsWith         |= _functionInfo.ContainsWith;
                stats.NeedDecompose        |= _functionInfo.NeedDecompose;
                stats.UseCall      |= _functionInfo.UseCall;
                stats.UseGetMember |= _functionInfo.UseGetMember;
                stats.ContainsThis |= _functionInfo.ContainsThis;
            }

            if (descriptorToRestore != null)
            {
                variables[descriptorToRestore.name] = descriptorToRestore;
            }
            else if (!string.IsNullOrEmpty(_name))
            {
                variables.Remove(_name);
            }

            foreach (var variable in variables)
            {
                int count = 0;
                if (!numbersOfReferences.TryGetValue(variable.Key, out count) || count != variable.Value.references.Count)
                {
                    variable.Value.captured = true;
                    if ((codeContext & CodeContext.InWith) != 0)
                    {
                        for (var i = count; i < variable.Value.references.Count; i++)
                        {
                            variable.Value.references[i].ScopeLevel = -System.Math.Abs(variable.Value.references[i].ScopeLevel);
                        }
                    }
                }
            }

            return(false);
        }
示例#16
0
        private Merker LoadNativeFunctions(VariableDescriptor v)
        {
            Merker m;

            switch (v.Name)
            {
            case "TwiControl":
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 1, vd = v, pIn = 1
                };
                break;

            case "TwiStatus":
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 2, vd = v, pOut = 1
                };
                break;

            case "TwiPutByte":
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 3, vd = v, pIn = 1
                };
                break;

            case "TwiGetByte":
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 4, vd = v, pOut = 1
                };
                break;

            case "NodeStatus":
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 5, vd = v, pOut = 1
                };
                break;

            case "getMilliseconds":
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 6, vd = v, pOut = 1
                };
                break;

            case "getSeconds":
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 7, vd = v, pOut = 1
                };
                break;

            case "Random":
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 8, vd = v, pOut = 1
                };
                break;

            case "NowSeconds": // total seconds since 0:00:00
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 9, vd = v, pOut = 1
                };
                break;

            case "Today":   //  (year[0..99]<<24) | (month[1..12]<<16) | (day[1..31]<<8) | (dayOfWeek[1-Monday..7-Sunday])
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 10, vd = v, pOut = 1
                };
                break;

            case "UartInit": // void UartInit(port, speed)
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 20, vd = v, pIn = 2
                };
                break;

            case "UartBytesToRead": // int UartBytesToRead(port)
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 21, vd = v, pOut = 1, pIn = 1
                };
                break;

            case "UartGetByte": // int UartGetByte(port)
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 22, vd = v, pOut = 1, pIn = 1
                };
                break;

            case "UartPutByte": // bool UartPutByte(port, data)
                m = new Merker()
                {
                    type = EP_Type.API, Addr = 23, vd = v, pOut = 1, pIn = 2
                };
                break;

            default:
                return(null);
            }
            global.memory.Add(m);
            return(m);
        }
        // This is the set of valid AGMA gear pitches (in unit of inch^-1).

        private static void Main()
        {
            //var opty = new GradientBasedOptimization();
            //var opty = new HillClimbing();
            var opty = new GeneticAlgorithm(100);

            var numGears = 2 * NumGearPairs;

            /* here is the Dependent Analysis. Take a look at the file/class ForceVelocityPositionAnalysis.cs
             * and notice that it inherits from IDependent Analysis. By adding this to the optimization method
             * (line 122), we are ensuring that it is called for any new decision variables found in the process.*/
            var FVPAnalysis = new ForceVelocityPositionAnalysis(numGears, outputTorque, inputSpeed, inputPosition);

            opty.Add(FVPAnalysis);

            /* here is the objective function, minimize mass. Note that it will hold a reference to the
            * ForceVelocityPositionAnalysis so that it can reference it for exact values of diamter. */
            opty.Add(new massObjective(FVPAnalysis, gearDensity));

            /* here is an inequality constraint for fitting within the box described above. Again, it
             * needs to position and diameter information stored in ForceVelocityPositionAnalysis */
            opty.Add(new boundingboxConstraint(FVPAnalysis, boxMinX, boxMaxX, boxMinY, boxMaxY, boxMinZ,
                                               boxMaxZ));

            /* on and on: stress inequality, output Location, output Speed equalities. Details can be found in
             * http://dx.doi.org/10.1115/DETC2009-86780 */
            opty.Add(new stressConstraint(FVPAnalysis, Nf, SFB, SFC));
            opty.Add(new outputLocationConstraint(FVPAnalysis, locationTol, outputX, outputY, outputZ));
            opty.Add(new outputSpeedConstraint(FVPAnalysis, speedTol, outputSpeed));
            for (var i = 0; i < NumGearPairs - 1; i++)
            {
                // each mating gear pair must have the same pitch.
                opty.Add(new samePitch(i * 4 + 1, (i + 1) * 4 + 1));
            }

            /******** Set up Design Space *************/

            /* for the GA and the Hill Climbing, a compete discrete space is needed. Face width and
             * location parameters should be continuous though. Consider removing the 800's below
             * when using a mixed optimization method. */
            var dsd = new DesignSpaceDescription(numGears * 4);

            for (var i = 0; i < numGears; i++)
            {
                dsd[4 * i]     = new VariableDescriptor(5, 1000, 1.0); // number of teeth: integers between 5 and 1000
                dsd[4 * i + 1] = new VariableDescriptor(ValidPitches); // pitches from AGMA standard
                dsd[4 * i + 2] = new VariableDescriptor(0, 50, 800);   // face width is between 0 and 50 inches
                dsd[4 * i + 3] = new VariableDescriptor(0, 500, 800);  //location is either an angle or a length
                // a max of 500 inches is generous
            }
            opty.Add(dsd);
            /******** Set up Optimization *************/

            /* the following mish-mash is similiar to previous project - just trying to find a
             * combination of methods that'll lead to the optimial optimization algorithm. */
            //abstractSearchDirection searchDirMethod = new SteepestDescent();
            //opty.Add(searchDirMethod);
            //abstractLineSearch lineSearchMethod = new ArithmeticMean(0.0001, 1, 100);
            //opty.Add(lineSearchMethod);
            opty.Add(new LatinHyperCube(dsd, VariablesInScope.BothDiscreteAndReal));
            opty.Add(new GACrossoverBitString(dsd));
            opty.Add(new GAMutationBitString(dsd));
            opty.Add(new PNormProportionalSelection(optimize.minimize, true, 0.7));
            //opty.Add(new RandomNeighborGenerator(dsd,3000));
            //opty.Add(new KeepSingleBest(optimize.minimize));
            opty.Add(new squaredExteriorPenalty(opty, 10));
            opty.Add(new MaxAgeConvergence(40, 0.001));
            opty.Add(new MaxFnEvalsConvergence(10000));
            opty.Add(new MaxSpanInPopulationConvergence(15));
            double[] xStar;
            Parameters.Verbosity = VerbosityLevels.AboveNormal;
            // this next line is to set the Debug statements from OOOT to the Console.
            Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
            var timer = Stopwatch.StartNew();
            var fStar = opty.Run(out xStar, numGears * 4);

            printResults(opty, xStar, fStar, timer);
            Console.ReadKey();
        }
示例#18
0
        internal Merker DefineMerker(VariableDescriptor v, EP_Type type = EP_Type.NONE)
        {
            Merker m = null;
            uint   addr;

            m = cur.memory.FirstOrDefault(z => z.vd == v);
            if (m == null)
            {
                m = global.memory.FirstOrDefault(z => z.vd == v);
            }
            if (m == null)
            {
                addr = uint.MaxValue;
                var nt = Periphery.MsDevice.NTTable.FirstOrDefault(z => v.Name.StartsWith(z.Item1));

                if (nt != null && v.Name.Length > 2 && UInt32.TryParse(v.Name.Substring(2), out addr))
                {
                    addr = (uint)((uint)(((byte)v.Name[0]) << 24) | (uint)(((byte)v.Name[1]) << 16) | addr & 0xFFFF);
                    type = (nt.Item2 & Periphery.MsDevice.DType.Output) != 0?EP_Type.OUTPUT:EP_Type.INPUT;
                }
                else if (type == EP_Type.NONE)
                {
                    if (v.Initializer != null && v.Initializer is FunctionDefinition)
                    {
                        type = EP_Type.FUNCTION;
                    }
                    else if (v.LexicalScope)
                    {
                        type = EP_Type.LOCAL;
                        addr = (uint)cur.memory.Where(z => z.type == EP_Type.LOCAL).Count();
                        if (addr > 15)
                        {
                            throw new ArgumentOutOfRangeException("Too many local variables: " + v.Name + cur.fm == null ? string.Empty : ("in " + cur.fm.ToString()));
                        }
                    }
                    else
                    {
                        type = EP_Type.SINT32;
                        addr = uint.MaxValue;
                    }
                }
                m = new Merker()
                {
                    type = type, vd = v, pName = v.Name, Addr = addr, init = v.Initializer
                };

                if (type == EP_Type.API || type == EP_Type.INPUT || type == EP_Type.OUTPUT)
                {
                    global.memory.Add(m);
                    m.fName = v.Name;
                }
                else
                {
                    cur.memory.Add(m);
                    m.fName = (cur == global ? v.Name : cur.fm.fName + (cur.fm.type == EP_Type.FUNCTION ? "+" : ".") + v.Name);
                }
            }
            else if (m.type != type && m.type == EP_Type.NONE)
            {
                m.type = type;
            }
            return(m);
        }
        public void IsOfKindReturnsTrueForEqualKind()
        {
            var descriptor = new VariableDescriptor("test1", NotZero.Instance);

            Assert.IsTrue(descriptor.IsOfKind <NotZero>());
        }
示例#20
0
        public override bool Build(ref CodeNode _this, int expressionDepth, Dictionary <string, VariableDescriptor> variables, CodeContext codeContext, InternalCompilerMessageCallback message, FunctionInfo stats, Options opts)
        {
            if (built)
            {
                return(false);
            }
            built = true;

            List <VariableDescriptor> variablesToRestore = null;

            if (_variables != null && _variables.Length != 0)
            {
                for (var i = 0; i < _variables.Length; i++)
                {
                    VariableDescriptor desc = null;
                    if (variables.TryGetValue(_variables[i].name, out desc) && desc.definitionScopeLevel < _variables[i].definitionScopeLevel)
                    {
                        if (variablesToRestore == null)
                        {
                            variablesToRestore = new List <VariableDescriptor>();
                        }
                        variablesToRestore.Add(desc);
                    }

                    variables[_variables[i].name] = _variables[i];

                    _variables[i].owner = this;
                }

                for (var i = 0; i < _variables.Length; i++)
                {
                    Parser.Build(
                        ref _variables[i].initializer,
                        System.Math.Max(2, expressionDepth),
                        variables,
                        codeContext | (this._strict ? CodeContext.Strict : CodeContext.None),
                        message,
                        stats,
                        opts);
                }
            }

            for (var i = 0; i < _lines.Length; i++)
            {
                var ed = _lines[i] as EntityDefinition;
                if (ed != null && ed.Hoist)
                {
                    _lines[i] = null;
                }
            }

            bool unreachable = false;

            for (int i = 0; i < _lines.Length; i++)
            {
                if (_lines[i] != null)
                {
                    if (_lines[i] is Empty)
                    {
                        _lines[i] = null;
                    }
                    else
                    {
                        if (unreachable && message != null)
                        {
                            message(MessageLevel.CriticalWarning, _lines[i].Position, _lines[i].Length, "Unreachable code detected.");
                        }
                        var cn = _lines[i];
                        Parser.Build(ref cn, (codeContext & CodeContext.InEval) != 0 ? 2 : System.Math.Max(1, expressionDepth), variables, codeContext | (this._strict ? CodeContext.Strict : CodeContext.None), message, stats, opts);
                        if (cn is Empty)
                        {
                            _lines[i] = null;
                        }
                        else
                        {
                            _lines[i] = cn;
                        }
                        unreachable |= cn is Return || cn is Break || cn is Continue || cn is Throw;
                    }
                }
            }

            int f = _lines.Length, t = _lines.Length - 1;

            for (; f-- > 0;)
            {
                if (_lines[f] != null && _lines[t] == null)
                {
                    _lines[t] = _lines[f];
                    _lines[f] = null;
                }
                if (_lines[t] != null)
                {
                    t--;
                }
            }

            if (expressionDepth > 0 && (_variables == null || _variables.Length == 0))
            {
                if (_lines.Length == 0)
                {
                    _this = Empty.Instance;
                }
            }
            else
            {
                if (message != null)
                {
                    for (var i = 0; i < _variables.Length; i++)
                    {
                        if (_variables[i].ReferenceCount == 1)
                        {
                            message(
                                MessageLevel.Recomendation,
                                _variables[i].references[0].Position,
                                0,
                                "Unused variable \"" + _variables[i].name + "\"");
                        }
                        else
                        {
                            break;
                        }
                    }
                }
#if (NET40 || INLINE) && JIT
                compiledVersion = JITHelpers.compile(this, depth >= 0);
#endif
            }

            if (t >= 0 && this == _this)
            {
                var newBody = new CodeNode[_lines.Length - t - 1];
                f = 0;
                while (++t < _lines.Length)
                {
                    newBody[f++] = _lines[t];
                }
                _lines = newBody;
            }

            if (_variables != null && _variables.Length != 0)
            {
                for (var i = 0; i < _variables.Length; i++)
                {
                    variables.Remove(_variables[i].name);
                }
            }

            if (variablesToRestore != null)
            {
                for (var i = 0; i < variablesToRestore.Count; i++)
                {
                    variables[variablesToRestore[i].name] = variablesToRestore[i];
                }
            }

            return(false);
        }
        public void IsNotOfKindReturnsFalseForEqualKind()
        {
            var descriptor = new VariableDescriptor("test2", new Definition(new FlowNode(new Label())));

            Assert.IsFalse(descriptor.IsNotOfKind <Definition>());
        }
示例#22
0
        protected void GenerateLocalVar(StringBuilder sb, VariableDescriptor varDescr)
        {
            string typeName = GetCsTypeName(varDescr.VarType);

            sb.AppendFormat("{0} {1} = ({0})context[\"{1}\"];", typeName, varDescr.Name);
        }
        public void IsNotOfKindReturnsTrueForUnequalKind()
        {
            var descriptor = new VariableDescriptor("test2", Positive.Instance);

            Assert.IsTrue(descriptor.IsNotOfKind <NotZero>());
        }
示例#24
0
        internal static CodeNode Parse(ParseInfo state, ref int index, bool forForLoop)
        {
            int position = index;

            Tools.SkipSpaces(state.Code, ref position);

            VariableKind mode;

            if (Parser.Validate(state.Code, "var ", ref position))
            {
                mode = VariableKind.FunctionScope;
            }
            else if (Parser.Validate(state.Code, "let ", ref position))
            {
                mode = VariableKind.LexicalScope;
            }
            else if (Parser.Validate(state.Code, "const ", ref position))
            {
                mode = VariableKind.ConstantInLexicalScope;
            }
            else
            {
                return(null);
            }

            var level        = mode <= VariableKind.FunctionScope ? state.FunctionScopeLevel : state.LexicalScopeLevel;
            var initializers = new List <Expression>();
            var names        = new List <string>();
            int s            = position;

            while ((state.Code[position] != ';') && (state.Code[position] != '}') && !Tools.IsLineTerminator(state.Code[position]))
            {
                Tools.SkipSpaces(state.Code, ref position);

                if (state.Code[position] != '[' && state.Code[position] != '{' && !Parser.ValidateName(state.Code, position, state.Strict))
                {
                    if (Parser.ValidateName(state.Code, ref position, false, true, state.Strict))
                    {
                        ExceptionHelper.ThrowSyntaxError('\"' + Tools.Unescape(state.Code.Substring(s, position - s), state.Strict) + "\" is a reserved word, but used as a variable. " + CodeCoordinates.FromTextPosition(state.Code, s, position - s));
                    }
                    ExceptionHelper.ThrowSyntaxError("Invalid variable definition at " + CodeCoordinates.FromTextPosition(state.Code, s, position - s));
                }

                var expression = ExpressionTree.Parse(state, ref position, processComma: false, forForLoop: forForLoop);
                if (expression is VariableReference)
                {
                    var name = expression.ToString();
                    if (state.Strict)
                    {
                        if (name == "arguments" || name == "eval")
                        {
                            ExceptionHelper.ThrowSyntaxError("Varible name cannot be \"arguments\" or \"eval\" in strict mode", state.Code, s, position - s);
                        }
                    }

                    names.Add(name);
                    initializers.Add(expression);
                }
                else
                {
                    var valid = false;
                    var expr  = expression as ExpressionTree;
                    if (expr != null)
                    {
                        if (expr.Type == OperationType.None && expr._right == null)
                        {
                            expr = expr._left as ExpressionTree;
                        }
                        valid |= expr != null && expr.Type == OperationType.Assignment;

                        if (valid)
                        {
                            if (expr._left is ObjectDesctructor)
                            {
                                var expressions = (expr._left as ObjectDesctructor).GetTargetVariables();
                                for (var i = 0; i < expressions.Count; i++)
                                {
                                    names.Add(expressions[i].ToString());
                                    initializers.Add(expressions[i]);
                                }

                                initializers.Add(expr);
                            }
                            else
                            {
                                names.Add(expr._left.ToString());
                                initializers.Add(expression);
                            }
                        }
                    }
                    else
                    {
                        var cnst = expression as Constant;
                        valid = cnst != null && cnst.value == JSValue.undefined;
                        if (valid)
                        {
                            initializers.Add(expression);
                            names.Add(cnst.value.ToString());
                        }
                    }

                    if (!valid)
                    {
                        ExceptionHelper.ThrowSyntaxError("Invalid variable initializer", state.Code, position);
                    }
                }

                s = position;

                if (position >= state.Code.Length)
                {
                    break;
                }

                Tools.SkipSpaces(state.Code, ref s);
                if (s >= state.Code.Length)
                {
                    break;
                }
                if (state.Code[s] == ',')
                {
                    position = s + 1;
                    Tools.SkipSpaces(state.Code, ref position);
                }
                else
                {
                    break;
                }
            }

            if (names.Count == 0)
            {
                throw new InvalidOperationException("code (" + position + ")");
            }

            if (!forForLoop && position < state.Code.Length && state.Code[position] == ';')
            {
                position++;
            }
            else
            {
                position = s;
            }

            var variables = new VariableDescriptor[names.Count];

            for (int i = 0, skiped = 0; i < names.Count; i++)
            {
                bool skip = false;
                for (var j = 0; j < state.Variables.Count - i + skiped; j++)
                {
                    if (state.Variables[j].name == names[i] && state.Variables[j].definitionScopeLevel >= level)
                    {
                        if (state.Variables[j].lexicalScope || mode > VariableKind.FunctionScope)
                        {
                            ExceptionHelper.ThrowSyntaxError(string.Format(Strings.IdentifierAlreadyDeclared, names[i]), state.Code, index);
                        }

                        skip         = true;
                        variables[i] = state.Variables[j];
                        skiped++;
                        break;
                    }
                }

                if (skip)
                {
                    continue;
                }

                variables[i] = new VariableDescriptor(names[i], level)
                {
                    lexicalScope = mode > VariableKind.FunctionScope,
                    isReadOnly   = mode == VariableKind.ConstantInLexicalScope
                };

                state.Variables.Add(variables[i]);
            }

            var pos = index;

            index = position;
            return(new VariableDefinition(variables, initializers.ToArray(), mode)
            {
                Position = pos,
                Length = index - pos
            });
        }