PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/DLR_Main/Languages/Ruby/Ruby/Compiler/Parser/VariableFactory.cs

https://bitbucket.org/mdavid/dlr
C# | 120 lines | 78 code | 28 blank | 14 comment | 2 complexity | dc996316fe980f3ade354a6dc13525b5 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System.Dynamic;
  16. using Microsoft.Scripting;
  17. using Microsoft.Scripting.Utils;
  18. using IronRuby.Compiler.Ast;
  19. namespace IronRuby.Compiler {
  20. internal static class VariableFactory {
  21. public const int Identifier = 0;
  22. public const int Instance = 1;
  23. public const int Global = 2;
  24. public const int Constant = 3;
  25. public const int Class = 4;
  26. public const int Nil = 5;
  27. public const int Self = 6;
  28. public const int True = 7;
  29. public const int False = 8;
  30. public const int File = 9;
  31. public const int Line = 10;
  32. public const int Encoding = 11;
  33. internal static Expression/*!*/ MakeRead(int kind, Parser/*!*/ parser, string name, SourceSpan location) {
  34. switch (kind) {
  35. case Identifier:
  36. return (Expression)parser.CurrentScope.ResolveVariable(name) ?? new MethodCall(null, name, null, null, location);
  37. case Instance:
  38. return new InstanceVariable(name, location);
  39. case Global:
  40. return new GlobalVariable(name, location);
  41. case Constant:
  42. return new ConstantVariable(name, location);
  43. case Class:
  44. return new ClassVariable(name, location);
  45. case Nil:
  46. return Literal.Nil(location);
  47. case Self:
  48. return new SelfReference(location);
  49. case True:
  50. return Literal.True(location);
  51. case False:
  52. return Literal.False(location);
  53. case File:
  54. return new FileLiteral(location);
  55. case Line:
  56. return Literal.Integer(parser.Tokenizer.TokenSpan.Start.Line, location);
  57. case Encoding:
  58. return new EncodingExpression(location);
  59. }
  60. throw Assert.Unreachable;
  61. }
  62. internal static LeftValue/*!*/ MakeLeftValue(int kind, Parser/*!*/ parser, string name, SourceSpan location) {
  63. switch (kind) {
  64. case Identifier:
  65. return parser.CurrentScope.ResolveOrAddVariable(name, location);
  66. case Instance:
  67. return new InstanceVariable(name, location);
  68. case Global:
  69. return new GlobalVariable(name, location);
  70. case Constant:
  71. return new ConstantVariable(name, location);
  72. case Class:
  73. return new ClassVariable(name, location);
  74. case Nil:
  75. return parser.CannotAssignError("nil", location);
  76. case Self:
  77. return parser.CannotAssignError("self", location);
  78. case True:
  79. return parser.CannotAssignError("true", location);
  80. case False:
  81. return parser.CannotAssignError("false", location);
  82. case File:
  83. return parser.CannotAssignError("__FILE__", location);
  84. case Line:
  85. return parser.CannotAssignError("__LINE__", location);
  86. case Encoding:
  87. return parser.CannotAssignError("__ENCODING__", location);
  88. }
  89. return null;
  90. }
  91. }
  92. }