public MixinBlock LookupMixin(string name, int lookupDepth = 0) { MixinBlock ret; if (Mixins.TryGetValue(name, out ret)) { return(ret); } if (lookupDepth > 100) { Current.RecordError(ErrorType.Compiler, InvocationSite, "Scope max depth exceeded, probably infinite recursion"); throw new StoppedCompilingException(); } Value var; if (Variables.TryGetValue(name, out var)) { var func = var as FuncValue; if (func != null) { return(LookupMixin(func.Name, lookupDepth + 1)); } } if (ParentScope != null) { return(ParentScope.LookupMixin(name, lookupDepth + 1)); } return(null); }
/// <summary> /// Find named scopes that match this named scope use. /// </summary> /// <returns>An enumerable of named scopes with the same name as this use</returns> public override IEnumerable <INamedScope> FindMatches() { if (ChildScopeUse != null) { var globalScope = ParentScope.GetParentScopesAndSelf <INamespaceDefinition>().Where(p => p.IsGlobal).FirstOrDefault(); if (null == globalScope) { throw new ScopeDetachedException(this.ParentScope); } INamedScopeUse current = ChildScopeUse; IEnumerable <INamedScope> matches = null; while (current != null) { if (matches == null) { matches = globalScope.GetChildScopesWithId <INamedScope>(current.Name); } else { matches = GetChildScopesWithName(matches, current.Name); } current = current.ChildScopeUse; } return(matches == null ? Enumerable.Empty <INamedScope>() : matches); } else { return(base.FindMatches()); } }
public List <IValue> Evaluate(List <IValue> arguments, Scope scope) { Scope currentScope = ParentScope.EmptyChildScope(); currentScope["self"] = this; //Bind arguments to names for (int i = 0; i < ArgumentNames.Count; ++i) { string argName = ArgumentNames[i]; if (argName == "...") { currentScope[argName] = new ListTable(arguments.Skip(i)); } else { IValue argValue = i >= arguments.Count ? Value.Nil : arguments[i]; currentScope[argName] = argValue; } } // If the function has no named parameters, then bind them to $0, $1... if (ArgumentNames.Count == 0) { currentScope["$args"] = new ListTable(arguments); for (int i = 0; i < arguments.Count; ++i) { currentScope["$" + i] = arguments[i]; } } return(WulInterpreter.Interpret(Body, currentScope)); }
public (ReferenceValue Target, ScopeValue Scope)? FindVariableAndScope(string name, bool includeParent, VariableSearchMode mode) { if (string.IsNullOrEmpty(name)) { return(null); } IEnumerable <KeyValuePair <string, ReferenceValue> > items; switch (mode) { case VariableSearchMode.All: items = LocalVariables; break; case VariableSearchMode.OnlyConstant: items = LocalVariables.Where(e => e.Value.IsConstant); break; case VariableSearchMode.OnlyNonConstant: items = LocalVariables.Where(e => !e.Value.IsConstant); break; default: throw new ArgumentOutOfRangeException(nameof(mode), mode, $"Unknown VariableSearchMode {mode}"); } var result = items.Where(e => e.Key == name).ToList(); if (result.Any()) { return(result.First().Value, this); } return(includeParent ? ParentScope?.FindVariableAndScope(name, true, mode) : null); }
/// <summary> /// Gets the value in this scope /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="searchParentScope"></param> /// <returns></returns> public T GetValue <T>(string key, bool searchParentScope) { if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(ICollection <>)) { if (!_items.TryGetValue(key, out object coll)) { var t = typeof(T).GetGenericArguments()[0]; coll = Activator.CreateInstance(typeof(SolidConfigurationScopeCollection <>).MakeGenericType(t)); if (searchParentScope && ParentScope != null) { var parent = (SolidConfigurationScopeCollection)(object)ParentScope.GetValue <T>(key, searchParentScope); ((SolidConfigurationScopeCollection)coll).Parent = parent; } _items[key] = coll; } return((T)coll); } if (_items.TryGetValue(key, out object val)) { if (val is Func <T> del) { return(del()); } return((T)val); } if (searchParentScope && ParentScope != null) { return(ParentScope.GetValue <T>(key, searchParentScope)); } return(default(T)); }
public IScope?FindScope(string name) { if (Name == name) { return(this); } return(ParentScope?.FindScope(name)); }
/// <summary> /// Sets the value in this scope /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="writeInParentScopes"></param> public void SetValue <T>(string key, T value, bool writeInParentScopes = false) { _items[key] = value; if (writeInParentScopes && ParentScope != null) { ParentScope.SetValue(key, value, writeInParentScopes); } }
protected LanguageSymbol(LanguageScope parentScope, string symbolRoleName) : base(parentScope) { ObjectName = "tmp_" + ObjectId.ToString("X");//.PadLeft(9, '_'); SymbolRole = ParentScope.RootAst.RoleDictionary[symbolRoleName]; ParentScope.AddLangugeSymbol(this); }
protected LanguageSymbol(string symbolName, LanguageScope parentScope, string symbolRoleName) : base(parentScope) { ObjectName = symbolName; SymbolRole = ParentScope.RootAst.RoleDictionary[symbolRoleName]; ParentScope.AddLangugeSymbol(this); }
/// <summary> /// This is called when a session has a failure /// </summary> public override void FailScope() { if (ParentScope != null) { ParentScope.FailScope(); } base.FailScope(); }
protected internal Direction(NRefactory.DirectionExpression directionExpression, IScope scope, INRefcatoryExpressionVisitor visitor) : base(scope, visitor) { _directionExpression = directionExpression; IdentifierParameter = directionExpression.Expression.AcceptVisitor(Visitor, scope) as Identifier; _outParameter = ParentScope.Find(IdentifierParameter.Name); InternalType = IdentifierParameter.Type; }
/// <summary> /// Registers a new local variable in the nearest supporting scope /// </summary> /// <param name="identifier">The identifier to be used for accessing this variable</param> public virtual ParameterExpression RegisterLocal(string identifier) { if (ParentScope == null) { throw new LithiumCompilerException("Cannot register local variable outside of a valid block scope."); } return(ParentScope.RegisterLocal(identifier)); }
/// <summary> /// Gets the <see cref="LabelTarget"/> used to complete a "goto {label}" operation. /// </summary> /// <param name="labelName">The name of the label to jump to</param> public virtual LabelTarget GetJumpLabel(string labelName) { if (ParentScope == null) { throw new LithiumCompilerException("Couldn't locate label {0} to jump to.", labelName); } return(ParentScope.GetJumpLabel(labelName)); }
/// <summary> /// Registers a Goto label which can be used to execute a jump within the code. /// </summary> /// <param name="labelName">The name of the label to register</param> public virtual Expression RegisterJumpTarget(string labelName) { if (ParentScope == null) { throw new LithiumCompilerException("Cannot register jump label in current scope or any of its parents."); } return(ParentScope.RegisterJumpTarget(labelName)); }
/// <summary> /// Gets the <see cref="LabelTarget"/> used to exit the current loop /// </summary> public virtual LabelTarget GetExitLabel() { if (ParentScope == null) { throw new LithiumCompilerException("Break expression did not exist within a valid loop."); } return(ParentScope.GetExitLabel()); }
/// <summary> /// Gets the <see cref="LabelTarget"/> used to exit the current function /// </summary> public virtual LabelTarget GetReturnLabel() { if (ParentScope == null) { throw new LithiumCompilerException("Return expression did not exist within a valid function scope."); } return(ParentScope.GetReturnLabel()); }
/// <summary> /// This method should return the session instance associated with the key. /// </summary> /// <param name="key">an object instance</param> /// <returns> /// the session instance or null if none was found /// </returns> public override ISession GetSession(object key) { if (ParentScope != null) { return(ParentScope.GetSession(new KeyHolder(key, _connection.ConnectionString, _connection.GetHashCode()))); } return(base.GetSession(key)); }
private void GetAddressProperties(TAddressProperties result) { if (ParentScope != null) { ParentScope.GetAddressProperties(result); } result.OverrideProperties(AddressRelatedProperties); }
public virtual QualifiedName GetQualifiedName(QualifiedName name) { name.Prepend(this.Name); if (this.ParentScope == null) { return(name); } return(ParentScope.GetQualifiedName(name)); }
private void GetProperties(TScopeProperties result) { if (ParentScope != null) { ParentScope.GetProperties(result); } result.OverrideProperties(Properties); }
public override void Flush() { if (ParentScope != null) { ParentScope.Flush(); } base.Flush(); }
/// <summary> /// Helper method to invoke the default channel instance creator /// </summary> /// <returns>The created channel.</returns> /// <param name="attribute">The attribute describing the channel to create.</param> protected IChannel <T> BaseCreateChannel <T>(ChannelNameAttribute attribute) { if (ParentScope != Root) { return(ParentScope.GetOrCreate <T>(attribute)); } return(ChannelManager.CreateChannelForScope <T>(attribute)); }
/// <summary> /// Removes any program elements defined in the given file. If the scope is defined entirely /// within the given file, then it removes itself from its parent. /// </summary> /// <param name="fileName">The file to remove.</param> /// <returns>A collection of any unresolved scopes that result from removing the file. The /// caller is responsible for re-resolving these as appropriate.</returns> public virtual Collection <IScope> RemoveFile(string fileName) { if (LocationDictionary.ContainsKey(fileName)) { if (LocationDictionary.Count == 1) { //this scope exists solely in the file to be deleted if (ParentScope != null) { ParentScope.RemoveChild(this); ParentScope = null; } } else { Debug.WriteLine("Found Scope with more than one location. Should this be possible?"); foreach (var loc in Locations) { Debug.WriteLine("Location: " + loc); } //Remove the file from the children var unresolvedChildScopes = new List <IScope>(); foreach (var child in ChildScopes.ToList()) { var result = child.RemoveFile(fileName); if (result != null) { unresolvedChildScopes.AddRange(result); } } if (unresolvedChildScopes.Count > 0) { foreach (var child in unresolvedChildScopes) { AddChildScope(child); } } //remove method calls var callsInFile = MethodCallCollection.Where(call => call.Location.SourceFileName == fileName).ToList(); foreach (var call in callsInFile) { MethodCallCollection.Remove(call); } //remove declared variables var declsInFile = DeclaredVariablesDictionary.Where(kvp => kvp.Value.Location.SourceFileName == fileName).ToList(); foreach (var kvp in declsInFile) { DeclaredVariablesDictionary.Remove(kvp.Key); } //update locations LocationDictionary.Remove(fileName); } } return(null); }
/// <summary> /// Resolve a name inside the table /// </summary> /// <param name="name">The name to be resolved</param> /// <returns>A symbol with that name or null if not found</returns> public virtual Symbol Resolve(string name) { Symbol s; if (_symbols.TryGetValue(name, out s)) { return(s); } return(ParentScope?.Resolve(name)); }
/// <summary> /// Gets am expression for a local variable or global environment object which /// will return the value for a variable when executed. /// </summary> /// <param name="identifier">The variable's identifier</param> public virtual Expression GetVariableForRead(string identifier) { if (!LocalVariables.Any() && ParentScope == null) { return(Expressions.MakeGlobalVariableAccessor(Environment, identifier)); } //Navigate up the scope tree to see if we can find a local variable with the given identifier return(LocalVariables.SingleOrDefault(x => x.Name == identifier) ?? ParentScope.GetVariableForRead(identifier)); }
/// <summary> /// This method is invoked when no session was available /// at and the <see cref="ISessionFactoryHolder"/> /// just created one. So it registers the session created /// within this scope using a key. The scope implementation /// shouldn't make any assumption on what the key /// actually is as we reserve the right to change it /// <seealso cref="IsKeyKnown"/> /// </summary> /// <param name="key">an object instance</param> /// <param name="session">An instance of <c>ISession</c></param> public override void RegisterSession(object key, ISession session) { if (ParentScope != null) { ParentScope.RegisterSession(new KeyHolder(key, _connection.ConnectionString, _connection.GetHashCode()), session); return; } base.RegisterSession(key, session); }
/// <summary> /// Find this variable in the local scope, OR in whichever /// parent scope is found first when walking up the parent chain, /// according to niormal scoping rules. /// <returns>null if no hit was found, or the Variable if it was</returns> /// </summary> public Variable GetNested(string name) { Variable res; if (!Variables.TryGetValue(name, out res) && ParentScope != null) { res = ParentScope.GetNested(name); } return(res); }
public VariableDeclaration GlobalVariableSearch(string name, int upToLevel) { if (ScopeLevel < upToLevel) { return(null); } var result = LocalVariableSearch(name) ?? ParentScope.GlobalVariableSearch(name, upToLevel); return(result); }
public int LookupSymbolScope(string name) { _logger.Log($"Lookup Scope: {name}"); var current = symbols.FirstOrDefault(p => p.Name.ToLower() == name.ToLower()); if (current == null && ParentScope != null) { return(ParentScope.LookupSymbolScope(name)); } return(ScopeLevel); }
public Symbol LookupSymbol(string name, bool searchParent) { _logger.Log($"Lookup: {name}"); var current = symbols.FirstOrDefault(p => p.Name.ToLower() == name.ToLower()); if (current == null && ParentScope != null && searchParent) { return(ParentScope.LookupSymbol(name, true)); } return(current); }