/** * Add a new named capture variable to this (current) scope. * * @param name name of variable. * @return index of variable */ public int addNamedCaptureVariable(String name) { int index = addVariableThisScope(name); growNamedCaptures(index); return index; }
/** * Make a DASgn or LocalAsgn node based on scope logic * * @param position * @param name * @param value * @return */ public AssignableNode assign(ISourcePosition position, String name, Node value) { return assign(position, name, value, this, 0); }
/** * Make a DVar or LocalVar node based on scoping logic * * @param position the location that in the source that the new node will come from * @param symbolID of the variable to be created is named * @return a DVarNode or LocalVarNode * * Note: This is private code made public only for parser. */ public Node declare(ISourcePosition position, RubySymbol symbolID) { return declare(position, symbolID, 0); }
/** * Get all visible variables that we can see from this scope that have been assigned * (e.g. seen so far) * * @return a list of all names (sans $~ and $_ which are special names) */ public String[] getAllNamesInScope() { String[] names = getVariables(); if (isBlockOrEval) { String[] ourVariables = names; String[] variables = enclosingScope.getAllNamesInScope(); // we know variables cannot be null since this IRStaticScope always returns a non-null array names = new String[variables.length + ourVariables.length]; System.arraycopy(variables, 0, names, 0, variables.length); System.arraycopy(ourVariables, 0, names, variables.length, ourVariables.length); } return names; }
@Deprecated public String shadowing_lvar(String name) { if (name == "_") return name; StaticScope current = getCurrentScope(); if (current.exists(name) >= 0) yyerror("duplicated argument name"); if (current.isBlockScope() && warnings.isVerbose() && current.isDefined(name) >= 0 && Options.PARSER_WARN_LOCAL_SHADOWING.load()) { warnings.warning(ID.STATEMENT_NOT_REACHED, lexer.getPosition(), "shadowing outer local variable - " + name); } return name; }
public ArgsTailHolder new_args_tail(ISourcePosition position, ListNode keywordArg, Token keywordRestArgName, BlockArgNode blockArg) { if (keywordRestArgName == null) return new ArgsTailHolder(position, keywordArg, null, blockArg); String restKwargsName = (String) keywordRestArgName.getValue(); int slot = currentScope.exists(restKwargsName); if (slot == -1) slot = currentScope.addVariable(restKwargsName); KeywordRestArgNode keywordRestArg = new KeywordRestArgNode(position, restKwargsName, slot); return new ArgsTailHolder(position, keywordArg, keywordRestArg, blockArg); }
public IRubyObject arg_var(IRubyObject identifier) { String name = lexer.getIdent(); StaticScope current = getCurrentScope(); // Multiple _ arguments are allowed. To not screw with tons of arity // issues in our runtime we will allocate unnamed bogus vars so things // still work. MRI does not use name as intern'd value so they don't // have this issue. if (name == "_") { int count = 0; while (current.exists(name) >= 0) { name = "_$" + count++; } } current.addVariableThisScope(name); return identifier; }
@Override public Object execute(ParserSupport support, RubyYaccLexer lexer, Object yyVal, Object[] yyVals, int yyTop) { String identifier = (String) ((Token)yyVals[-2+yyTop]).getValue(); if (support.getCurrentScope().getLocalScope().isDefined(identifier) >= 0) { support.yyerror("duplicate optional argument name"); } support.getCurrentScope().getLocalScope().addVariable(identifier); yyVal = support.assignable(((Token)yyVals[-2+yyTop]), ((Node)yyVals[0+yyTop])); return yyVal; } };
public static StaticScope decodeScopeAndDetermineModule(ThreadContext context, StaticScope parent, String scopeString) { StaticScope scope = decodeScope(context, parent, scopeString); scope.determineModule(); return scope; }
public boolean isConstantDefined(String internedName) { return getConstant(internedName) != null; }
private IRubyObject getConstantInnerNoObject(String internedName) { if (previousCRefScope == null) return null; return getConstantInner(internedName); }
public AssignableNode assignableInCurr(ByteList nameBytes, Node value) { RubySymbol name = symbolID(nameBytes); currentScope.addVariableThisScope(name.idString()); return currentScope.assign(lexer.getPosition(), name, makeNullNil(value)); }
public Node declare(ISourcePosition position, RubySymbol symbolID, int depth) { int slot = exists(symbolID.idString()); if (slot >= 0) { return isBlockOrEval ? new DVarNode(position, ((depth << 16) | slot), symbolID) : new LocalVarNode(position, ((depth << 16) | slot), symbolID); } return isBlockOrEval ? enclosingScope.declare(position, symbolID, depth + 1) : new VCallNode(position, symbolID); }
/** * Get all variable names captured (visible) by this scope (sans $~ and $_). * * @return a list of variable names */ public final String[] getAllNamesInScope() { return staticScope.getAllNamesInScope(); }
private List<Integer> allocateNamedLocals(RegexpNode regexpNode) { String[] names = regexpNode.loadPattern(configuration.getRuntime()).getNames(); int length = names.length; List<Integer> locals = new ArrayList<Integer>(); StaticScope scope = getCurrentScope(); for (int i = 0; i < length; i++) { // TODO: Pass by non-local-varnamed things but make sure consistent with list we get from regexp if (RubyYaccLexer.getKeyword(names[i]) == null) { int slot = scope.isDefined(names[i]); if (slot >= 0) { locals.add(slot); } else { locals.add(getCurrentScope().addVariableThisScope(names[i])); } } } return locals; }
public AssignableNode addAssign(ISourcePosition position, RubySymbol symbolID, Node value) { int slot = addVariable(symbolID.idString()); // No bit math to store level since we know level is zero for this case return new DAsgnNode(position, symbolID, slot, value); }
@JRubyMethod public IRubyObject local_variable_set(ThreadContext context, IRubyObject symbol, IRubyObject value) { String name = symbol.asJavaString().intern(); DynamicScope evalScope = binding.getEvalScope(context.runtime); int slot = evalScope.getStaticScope().isDefined(name); if (slot == -1) { // Yay! New variable associated with this binding slot = evalScope.getStaticScope().addVariable(name.intern()); evalScope.growIfNeeded(); } return evalScope.setValue(slot & 0xffff, value, slot >> 16); }
/** * Does the variable exist? * * @param name of the variable to find * @return index of variable or -1 if it does not exist */ public int exists(String name) { return findVariableName(name); }
public ArgumentNode arg_var(ByteList id) { RubySymbol name = symbolID(id); return new ArgumentNode(lexer.getPosition(), name, getCurrentScope().addVariableThisScope(name.idString())); }