private void RegisterScopeIfNeeded() { if (_scope == null) { _scope = new SymbolScope(); _currentContext.PushScope(_scope); } }
private int ExtractVariableIndex(string name, SymbolScope scope) { if (scope.IsVarDefined(name)) { return(scope.GetVariableNumber(name)); } else { return(-1); } }
private int ExtractMethodIndex(string name, SymbolScope scope) { if (scope.IsMethodDefined(name)) { return(scope.GetMethodNumber(name)); } else { return(-1); } }
public ScriptModuleHandle CreateModule(ICodeSource source) { try { return Compile(source); } finally { _currentContext.PopScope(); _scope = null; } }
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); }
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); }
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; } }
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); } }
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(); }
public void PushScope(SymbolScope scope) { _scopeStack.Add(scope); }
public int ScopeIndex(SymbolScope scope) { return(_scopeStack.IndexOf(scope)); }
public int ScopeIndex(SymbolScope scope) { return _scopeStack.IndexOf(scope); }
private int ExtractVariableIndex(string name, SymbolScope scope) { if (scope.IsVarDefined(name)) { return scope.GetVariableNumber(name); } else return -1; }
public void PushScope(SymbolScope scope) { _moduleCtx.PushScope(scope); _localScopesCount++; }
private int ExtractMethodIndex(string name, SymbolScope scope) { if (scope.IsMethodDefined(name)) { return scope.GetMethodNumber(name); } else return -1; }
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; }