Ejemplo n.º 1
0
 private void RegisterScopeIfNeeded()
 {
     if (_scope == null)
     {
         _scope = new SymbolScope();
         _currentContext.PushScope(_scope);
     }
 }
Ejemplo n.º 2
0
 private int ExtractVariableIndex(string name, SymbolScope scope)
 {
     if (scope.IsVarDefined(name))
     {
         return(scope.GetVariableNumber(name));
     }
     else
     {
         return(-1);
     }
 }
Ejemplo n.º 3
0
 private int ExtractMethodIndex(string name, SymbolScope scope)
 {
     if (scope.IsMethodDefined(name))
     {
         return(scope.GetMethodNumber(name));
     }
     else
     {
         return(-1);
     }
 }
Ejemplo n.º 4
0
 public ScriptModuleHandle CreateModule(ICodeSource source)
 {
     try
     {
         return Compile(source);
     }
     finally
     {
         _currentContext.PopScope();
         _scope = null;
     }
 }
Ejemplo n.º 5
0
        public int ScopeIndex(SymbolScope scope)
        {
            int idx = _moduleCtx.ScopeIndex(scope);

            if (idx >= 0)
            {
                return(idx + OUTER_CTX_SIZE);
            }
            else
            {
                idx = _outerCtx.ScopeIndex(scope);
            }

            return(idx);
        }
Ejemplo n.º 6
0
        public void InjectGlobalProperty(IValue value, string identifier, bool readOnly)
        {
            if(!Utils.IsValidIdentifier(identifier))
            {
                throw new ArgumentException("Invalid identifier", "identifier");
            }

            if (_globalScope == null)
            {
                _globalScope = new SymbolScope();
                TypeManager.RegisterType("__globalPropertiesHolder", typeof(PropertyBag));
                _injectedProperties = new PropertyBag();
                _symbolScopes.PushScope(_globalScope);
                RegisterObject(_injectedProperties);
            }
            
            _globalScope.DefineVariable(identifier, SymbolType.ContextProperty);
            _injectedProperties.Insert(value, identifier, true, !readOnly);
        }
Ejemplo n.º 7
0
        public int DefineVariable(string name, SymbolType type)
        {
            RegisterScopeIfNeeded();

            try
            {
                int varIdx;
                if (type == SymbolType.Variable)
                    varIdx = _currentContext.DefineVariable(name).CodeIndex;
                else
                    varIdx = _currentContext.DefineProperty(name).CodeIndex;

                _predefinedVariables.Add(varIdx);
                return varIdx;
            }
            catch
            {
                _currentContext.PopScope();
                _scope = null;
                throw;
            }
        }
Ejemplo n.º 8
0
        private void RegisterSymbolScope(IReflectableContext provider, bool asDynamicScope)
        {
            var scope = new SymbolScope();
            scope.IsDynamicScope = asDynamicScope;

            _symbolScopes.PushScope(scope);
            foreach (var item in provider.GetProperties())
            {
                if (item.Type == SymbolType.Variable)
                {
                    _symbolScopes.DefineVariable(item.Identifier);
                }
                else
                {
                    _symbolScopes.DefineProperty(item.Identifier);
                }
            }

            foreach (var item in provider.GetMethods())
            {
                _symbolScopes.DefineMethod(item);
            }
        }
Ejemplo n.º 9
0
        private void BuildSingleMethod()
        {
            if (_lastExtractedLexem.Token == Token.Procedure)
            {
                PushStructureToken(Token.EndProcedure);
                _isFunctionProcessed = false;
                NextToken();
            }
            else if (_lastExtractedLexem.Token == Token.Function)
            {
                PushStructureToken(Token.EndFunction);
                _isFunctionProcessed = true;
                NextToken();
            }
            else
            {
                throw CompilerException.UnexpectedOperation();
            }

            #region Method signature
            // сигнатура
            if (!IsUserSymbol(ref _lastExtractedLexem))
            {
                throw CompilerException.IdentifierExpected();
            }

            int definitionLine = _parser.CurrentLine;
            MethodInfo method = new MethodInfo();
            method.Name = _lastExtractedLexem.Content;
            method.IsFunction = _isFunctionProcessed;

            NextToken();
            if (_lastExtractedLexem.Token != Token.OpenPar)
            {
                throw CompilerException.TokenExpected(Token.OpenPar);
            }
            #endregion

            NextToken();

            #region Parameters list
            var paramsList = new List<ParameterDefinition>();
            var methodCtx = new SymbolScope();

            while (_lastExtractedLexem.Token != Token.ClosePar)
            {
                var param = new ParameterDefinition();
                string name;

                if (_lastExtractedLexem.Token == Token.ByValParam)
                {
                    param.IsByValue = true;
                    NextToken();
                    if (IsUserSymbol(ref _lastExtractedLexem))
                    {
                        name = _lastExtractedLexem.Content;
                    }
                    else
                    {
                        throw CompilerException.IdentifierExpected();
                    }
                }
                else if (IsUserSymbol(ref _lastExtractedLexem))
                {
                    param.IsByValue = false;
                    name = _lastExtractedLexem.Content;
                }
                else
                {
                    throw CompilerException.UnexpectedOperation();
                }

                NextToken();
                if (_lastExtractedLexem.Token == Token.Equal)
                {
                    param.HasDefaultValue = true;
                    NextToken();
                    if (IsLiteral(ref _lastExtractedLexem))
                    {
                        var cd = CreateConstDefinition(ref _lastExtractedLexem);
                        var num = GetConstNumber(ref cd);
                        param.DefaultValueIndex = num;
                        NextToken();
                    }
                    else
                    {
                        throw CompilerException.UnexpectedOperation();
                    }
                }

                if (_lastExtractedLexem.Token == Token.Comma || _lastExtractedLexem.Token == Token.ClosePar)
                {
                    paramsList.Add(param);
                    methodCtx.DefineVariable(name);

                    if(_lastExtractedLexem.Token != Token.ClosePar)
                        NextToken();
                }
                else
                {
                    throw CompilerException.UnexpectedOperation();
                }
            }

            method.Params = paramsList.ToArray();

            #endregion

            NextToken();
            bool isExportedMethod = false;
            if (_lastExtractedLexem.Token == Token.Export)
            {
                isExportedMethod = true;
                NextToken();
            }

            #region Body
            // тело
            var entryPoint = _module.Code.Count;

            try
            {
                _ctx.PushScope(methodCtx);
                DispatchMethodBody();
            }
            finally
            {
                _ctx.PopScope();
            }
            var pop = PopStructureToken();

            var descriptor = new MethodDescriptor();
            descriptor.EntryPoint = entryPoint;
            descriptor.Signature = method;
            descriptor.VariableFrameSize = methodCtx.VariableCount;
            SymbolBinding binding;
            try
            {
                binding = _ctx.DefineMethod(method);
            }
            catch (CompilerException)
            {
                var exc = new CompilerException("Метод с таким именем уже определен: " + method.Name);
                exc.LineNumber = definitionLine;
                exc.Code = _parser.GetCodeLine(exc.LineNumber);
                throw exc;
            }
            _module.MethodRefs.Add(binding);
            _module.Methods.Add(descriptor);

            if (isExportedMethod)
            {
                _module.ExportedMethods.Add(new ExportedSymbol()
                {
                    SymbolicName = method.Name,
                    Index = binding.CodeIndex
                });
            }

            #endregion

            NextToken();
        }
Ejemplo n.º 10
0
 public void PushScope(SymbolScope scope)
 {
     _scopeStack.Add(scope);
 }
Ejemplo n.º 11
0
 public int ScopeIndex(SymbolScope scope)
 {
     return(_scopeStack.IndexOf(scope));
 }
Ejemplo n.º 12
0
 public int ScopeIndex(SymbolScope scope)
 {
     return _scopeStack.IndexOf(scope);
 }
Ejemplo n.º 13
0
 public void PushScope(SymbolScope scope)
 {
     _scopeStack.Add(scope);
 }
Ejemplo n.º 14
0
 private int ExtractVariableIndex(string name, SymbolScope scope)
 {
     if (scope.IsVarDefined(name))
     {
         return scope.GetVariableNumber(name);
     }
     else
         return -1;
 }
Ejemplo n.º 15
0
 public void PushScope(SymbolScope scope)
 {
     _moduleCtx.PushScope(scope);
     _localScopesCount++;
 }
Ejemplo n.º 16
0
 private int ExtractMethodIndex(string name, SymbolScope scope)
 {
     if (scope.IsMethodDefined(name))
     {
         return scope.GetMethodNumber(name);
     }
     else
         return -1;
 }
Ejemplo n.º 17
0
 public void PushScope(SymbolScope scope)
 {
     _moduleCtx.PushScope(scope);
     _localScopesCount++;
 }
Ejemplo n.º 18
0
        public int ScopeIndex(SymbolScope scope)
        {
            int idx = _moduleCtx.ScopeIndex(scope);
            if (idx >= 0)
            {
                return idx + OUTER_CTX_SIZE;
            }
            else
            {
                idx = _outerCtx.ScopeIndex(scope);
            }

            return idx;
        }