public void SetLocalVariable(LocalVariableExpression localVariableExpression) { switch (localVariableExpression.Index) { case 0: _ilGenerator.Emit(OpCodes.Stloc_0); break; case 1: _ilGenerator.Emit(OpCodes.Stloc_1); break; case 2: _ilGenerator.Emit(OpCodes.Stloc_2); break; case 3: _ilGenerator.Emit(OpCodes.Stloc_3); break; default: _ilGenerator.Emit(OpCodes.Stloc, localVariableExpression.Name); break; } }
protected override IPhpValue VisitLocalVariableExpression(LocalVariableExpression src) { if (state.Principles.CurrentMethod == null) { return(PhpVariableExpression.MakeLocal(src.Name, false)); } var isArgument = state.Principles.CurrentMethod.GetParameters().Any(u => u.Name == src.Name); return(PhpVariableExpression.MakeLocal(src.Name, isArgument)); }
public void EvaluateLocalVariableExpressions() { BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("foo", "bar"); environment.SetValue("one", 1); LocalVariableExpression fooVar = new LocalVariableExpression("foo"); LocalVariableExpression oneVar = new LocalVariableExpression("one"); LocalVariableExpression twoVar = new LocalVariableExpression("two"); Assert.AreEqual("bar", fooVar.Evaluate(environment)); Assert.AreEqual(1, oneVar.Evaluate(environment)); Assert.IsNull(twoVar.Evaluate(environment)); }
private IStatement AssignmentStatement() { VariableExpression varExpression = null; if (_semTokens.IsTypeKeyWord()) { var keywordType = _semTokens.Current.Type; _semTokens.Step(); var type = keywordType.TokenToCompilerType(); if (_localVariables.TryGetValue(_semTokens.Current.Value, out _)) { Throw( $"Variable with name '{_semTokens.Current.Value}' is already declared. Use "); } if (_parameters.ContainsKey(_semTokens.Current.Value)) { Throw( $"A local or parameter named '{_semTokens.Current.Value}' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter"); } varExpression = new LocalVariableExpression(_semTokens.Current.Value, type, _localVariables.Count); _localVariables.Add(varExpression.Name, varExpression.ReturnType); _semTokens.Step(); // variable name } else if (_semTokens.Current?.Type == TokenType.Variable) { varExpression = GetVariable(_semTokens.CurrentWithStep()); } _semTokens.Step(); // assignment sign var expression = Expression(); if (varExpression.CheckCannotImplicitConversion(expression)) { throw new CompileException("Cannot implicitly convert type 'long ' to int"); } ThrowIfNotMatch(TokenType.Semicolon, "Assignment statement must be ended by semicolon"); return(new AssignmentStatement(varExpression, expression)); }
public static void WriteVariable(CodeTextBuilder code, LocalVariableExpression variable) { var variableName = variable.VariableName ?? variable.Variable.Name; code.Write(ToCamelCase(variableName)); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- public virtual void VisitLocalVariableExpression(LocalVariableExpression expression) { VisitAbstractExpression(expression); }
protected override IValue VisitIdentifierName(IdentifierNameSyntax node) { // var tt = context.Roslyn_GetNamedTypeSymbols(null); var sInfo = ModelExtensions.GetSymbolInfo(context.RoslynModel, node); var tInfo2 = context.RoslynModel.GetTypeInfo2(node); var tInfo = tInfo2.TypeInfo; //var sInfo = sInfo2.ty Type type = tInfo.Type == null ? null : context.Roslyn_ResolveType(tInfo.Type); switch (sInfo.Symbol.Kind) { case SymbolKind.Local: //var symbolLocal = info.Symbol as CSharp.SourceLocalSymbol; IValue tmp = new LocalVariableExpression(sInfo.Symbol.Name, new LangType(type)); tmp = ImplicitConvert(tmp, tInfo2); return(Simplify(tmp)); case SymbolKind.NamedType: // var a = sInfo.Symbol.ToDisplayString(); var b = sInfo.Symbol.OriginalDefinition as INamedTypeSymbol; if (b != null) { var c = context.Roslyn_ResolveType(b); return(new TypeValue(c)); } throw new NotSupportedException(); case SymbolKind.Parameter: #warning 'Formalnie to nie jest zmienna lokalna !!'; tmp = new LocalVariableExpression(sInfo.Symbol.Name, new LangType(type)); return(Simplify(tmp)); case SymbolKind.Field: var ax = context.Roslyn_ResolveField(sInfo.Symbol as IFieldSymbol); if (ax.IsStatic) { // throw new NotSupportedException(); var fieldSymbol = sInfo.Symbol as IFieldSymbol; tmp = new ClassFieldAccessExpression(ax, fieldSymbol != null && fieldSymbol.IsConst); return(Simplify(tmp)); } tmp = new InstanceFieldAccessExpression(ax, new ThisExpression(state.CurrentType)); // niczego nie wiem o kontekście -> domyślam się, że this return(Simplify(tmp)); case SymbolKind.Method: { var methodBaseInfo = context.Roslyn_ResolveMethod(sInfo.Symbol as IMethodSymbol); var methodInfo = methodBaseInfo as MethodInfo; if (methodInfo == null) { throw new NotSupportedException(); } var result = new MethodExpression(methodInfo); return(Simplify(result)); } case SymbolKind.Property: { var pi = context.Roslyn_ResolveProperty(sInfo.Symbol as IPropertySymbol); var targetObject = new ThisExpression(state.CurrentType); var result = new CsharpInstancePropertyAccessExpression(pi, targetObject); return(result); } default: throw new NotSupportedException(); } //throw new NotSupportedException(); //var m = info.Symbol; //var info2 = context.RoslynModel.AnalyzeDataFlow(node); ////var info3 = context.RoslynModel.GetDeclaredSymbol(node); //var mg = context.RoslynModel.GetMemberGroup(node); //string identifier = node.Identifier.ValueText.Trim(); // return InternalVisitTextIdentifier(identifier); }
public virtual LocalVariableExpression RewriteLocalVariableExpression(LocalVariableExpression expression) { return(expression); }