PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Scripting/Parser/Helpers/Invokes.cs

http://github.com/polyethene/IronAHK
C# | 126 lines | 102 code | 24 blank | 0 comment | 15 complexity | 4d2f506fd31cda1cb78b866aa7a4778b MD5 | raw file
  1. using System;
  2. using System.CodeDom;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Text;
  8. using IronAHK.Rusty;
  9. namespace IronAHK.Scripting
  10. {
  11. partial class Parser
  12. {
  13. List<CodeMethodInvokeExpression> invokes = new List<CodeMethodInvokeExpression>();
  14. const string invokeCommand = "IsCommand";
  15. #region DOM
  16. CodeMemberMethod LocalMethod(string name)
  17. {
  18. var method = new CodeMemberMethod { Name = name, ReturnType = new CodeTypeReference(typeof(object)) };
  19. var param = new CodeParameterDeclarationExpression(typeof(object[]), args);
  20. param.UserData.Add(Parser.RawData, typeof(object[]));
  21. method.Parameters.Add(param);
  22. return method;
  23. }
  24. CodeMethodInvokeExpression LocalLabelInvoke(string name)
  25. {
  26. var invoke = (CodeMethodInvokeExpression)InternalMethods.LabelCall;
  27. invoke.Parameters.Add(new CodePrimitiveExpression(name));
  28. return invoke;
  29. }
  30. CodeMethodInvokeExpression LocalMethodInvoke(string name)
  31. {
  32. var invoke = new CodeMethodInvokeExpression();
  33. invoke.Method.MethodName = name;
  34. invoke.Method.TargetObject = null;
  35. return invoke;
  36. }
  37. #endregion
  38. #region Resolve
  39. void StdLib()
  40. {
  41. #region Paths
  42. var search = new StringBuilder();
  43. search.Append(Environment.GetEnvironmentVariable(LibEnv));
  44. search.Append(Path.PathSeparator);
  45. search.Append(Path.Combine(Assembly.GetExecutingAssembly().Location, LibDir));
  46. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  47. {
  48. search.Append(Path.PathSeparator);
  49. search.Append(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.Combine("AutoHotkey", LibDir)));
  50. }
  51. else if (Path.DirectorySeparatorChar == '/' && Environment.OSVersion.Platform == PlatformID.Unix)
  52. {
  53. search.Append(Path.PathSeparator);
  54. search.Append("/usr/" + LibDir + "/" + LibExt);
  55. search.Append(Path.PathSeparator);
  56. search.Append("/usr/local/" + LibDir + "/" + LibExt);
  57. search.Append(Path.PathSeparator);
  58. search.Append(Path.Combine(Environment.GetEnvironmentVariable("HOME"), Path.Combine(LibDir, LibExt)));
  59. }
  60. var paths = search.ToString().Split(new[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries);
  61. search = null;
  62. #endregion
  63. foreach (var invoke in invokes)
  64. {
  65. string name = invoke.Method.MethodName;
  66. if (IsLocalMethodReference(name))
  67. {
  68. var obj = new CodeArrayCreateExpression { Size = invoke.Parameters.Count, CreateType = new CodeTypeReference(typeof(object)) };
  69. obj.Initializers.AddRange(invoke.Parameters);
  70. invoke.Parameters.Clear();
  71. invoke.Parameters.Add(obj);
  72. continue;
  73. }
  74. if (invoke.Method.TargetObject != null)
  75. continue;
  76. int z = name.IndexOf(LibSeperator);
  77. if (z != -1)
  78. name = name.Substring(0, z);
  79. foreach (var dir in paths)
  80. {
  81. if (!Directory.Exists(dir))
  82. continue;
  83. string file = Path.Combine(dir, string.Concat(name, ".", LibExt));
  84. if (File.Exists(file))
  85. {
  86. Parse(new StreamReader(file), Path.GetFileName(file));
  87. return;
  88. }
  89. }
  90. }
  91. invokes.Clear();
  92. }
  93. bool IsLocalMethodReference(string name)
  94. {
  95. foreach (var method in methods)
  96. if (method.Key.Equals(name, StringComparison.OrdinalIgnoreCase))
  97. return true;
  98. return false;
  99. }
  100. #endregion
  101. }
  102. }