private void Visit(VarDeclarationStmt declaration) { var il = CurrentMethodIL; TypeInfo varType = new TypeInfo(declaration.Type); if (declaration.Type is ArrayType) { var arrSize = il.DeclareLocal(typeof(int)); var arrayType = declaration.Type as ArrayType; Visit((dynamic)arrayType.SizeExpr); il.Emit(OpCodes.Stloc, arrSize); foreach (var varSymbol in declaration.VarSymbols) { if (varSymbol is GlobalSymbol) { var newField = typeBuilder.DefineField(varSymbol.Name, varType.CILType, FieldAttributes.Static); ((GlobalSymbol)varSymbol).CILField = newField; il.Emit(OpCodes.Ldloc, arrSize); il.Emit(OpCodes.Newarr, varType.CILType.GetElementType()); il.Emit(OpCodes.Stsfld, newField); } else if (varSymbol is VariableSymbol) { var newLocal = il.DeclareLocal(varType.CILType); ((VariableSymbol)varSymbol).CILLocal = newLocal; newLocal.SetLocalSymInfo(varSymbol.Name); il.Emit(OpCodes.Ldloc, arrSize); il.Emit(OpCodes.Newarr, varType.CILType.GetElementType()); il.Emit(OpCodes.Stloc, newLocal); } } } else { foreach (var varSymbol in declaration.VarSymbols) { if (varSymbol is GlobalSymbol) { var newField = typeBuilder.DefineField(varSymbol.Name, varType.CILType, FieldAttributes.Static); ((GlobalSymbol)varSymbol).CILField = newField; } else if (varSymbol is VariableSymbol) { var newLocal = il.DeclareLocal(varType.CILType); newLocal.SetLocalSymInfo(varSymbol.Name); ((VariableSymbol)varSymbol).CILLocal = newLocal; } else { throw new Exception("Unexpected symboltype " + varSymbol.GetType()); } } } }
private VarDeclarationStmt ParseVarDeclaration() { Match(TokenType.KwVar); VarDeclarationStmt declaration = new VarDeclarationStmt(AcceptedToken); do { declaration.Identifiers.Add(ParseIdentifier()); }while (Accept(TokenType.Comma)); Match(TokenType.Colon); declaration.Type = ParseType(); return(declaration); }
private void Visit(VarDeclarationStmt varDeclarationStmt) { foreach (string identifier in varDeclarationStmt.Identifiers) { bool creationSuccess; Symbol createdSymbol; if (Symbols.CurrentScope == 1) { createdSymbol = new GlobalSymbol(identifier, varDeclarationStmt.Type, Symbols.CurrentScope); } else { createdSymbol = new VariableSymbol(identifier, varDeclarationStmt.Type, Symbols.CurrentScope); FunctionStack.Peek().Locals.Add(createdSymbol as VariableSymbol); } creationSuccess = Symbols.AddSymbol(createdSymbol); if (!creationSuccess) { AddError(string.Format("'{0}' is already declared in current scope", identifier), varDeclarationStmt); } varDeclarationStmt.VarSymbols.Add(createdSymbol); } }