The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
Program.cs
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
3 // Project rROTOR
4 // --------------------------------------------------------------------------
5 // File: Program.cs
6 // Author: Francisco Ortin - francisco.ortin@gmail.com
7 // Co-author: Miguel Garcia - miguel.uniovi@gamail.com
8 // Description:
9 // Parses all the command line arguments.
10 // --------------------------------------------------------------------------
11 // Create date: 22-06-2007
12 // Modification date: 23-02-2011
14 
15 using System;
16 using System.IO;
17 using System.Collections.Generic;
18 using System.Windows.Forms;
19 using DynVarManagement;
20 using TargetPlatforms;
21 
22 namespace CommandLine {
23  static class Program {
24  static void Main(string[] args) {
25  if(args.Length == 0)
26  args = new string[] { "sample.cs", "/run" };
27 
28  //ParseArguments(new string[] { "tests/sample.cs", "/target=clr", "/run" });
29  //ParseArguments(new string[] { "tests/sample.cs", "/target=rrotor" });
30  //ParseArguments(new string[] { "tests/sample.cs", "/out=a.out.exe", "/target=clr", "/everythingDynamic" });
31 
32  ParseArguments(args);
33  Inference.Core.Parser.Parse(parameters.InputFileNames, parameters.OutputFileName, parameters.TargetPlatform, Application.StartupPath + "\\", "ilasm.exe", "TypeTable.txt", parameters.Run, parameters.ServerOptimization);
34  }
35 
39  private static Parameters parameters;
40 
41  public static void ParseArguments(string[] args) {
42  // * No parameters
43  if (args.Length == 0) {
44  Console.WriteLine(OptionsConfiguration.noInputMessage);
45  return;
46  }
47  // * Parses each parameter
48  IList<string> inputFileNames = new List<string>();
49  foreach (string parameter in args)
50  ParseParameter(parameter, inputFileNames);
51  // * Not posible to specify -ed and -es
53  Console.Error.WriteLine(OptionsConfiguration.dynAndStaErrorMessage);
54  System.Environment.Exit(3); // 3 == Both dynamic and static options cannot be set
55  }
56  // * Sets the input file names
57  parameters.InputFileNames = new string[inputFileNames.Count];
58  inputFileNames.CopyTo(parameters.InputFileNames, 0);
59  // * Sets the output file name
60  if (parameters.OutputFileName == null)
61  parameters.OutputFileName = ObtainOutputFileName(parameters.InputFileNames);
62  }
63 
64 
70  private static void ParseParameter(string parameter, IList<string> inputFileNames) {
71  foreach (string parameterPrefix in OptionsConfiguration.optionsPrefix) {
72  if (parameter.Substring(0, parameterPrefix.Length).ToLower().Equals(parameterPrefix)) {
73  // * Option
74  ParseOption(parameter.Substring(parameterPrefix.Length, parameter.Length - parameterPrefix.Length).ToLower());
75  return;
76  }
77  }
78  // * Input file
79  inputFileNames.Add(parameter);
80  }
81 
85  private static void SetDefaultParameters() {
86  parameters.Run = OptionsConfiguration.defaultRunOption; // * Default value
87  parameters.TargetPlatform = OptionsConfiguration.defaultTargetPlatform; // * Default value
88  }
89 
94  private static void ParseOption(string option) {
95  // * Help option
96  foreach (string opString in OptionsConfiguration.helpOptions)
97  if (option.Equals(opString)) {
98  // * Help requested
99  Console.WriteLine(OptionsConfiguration.helpMessage);
100  System.Environment.Exit(0);
101  }
102  // * Everything dynamic option
103  foreach (string opString in OptionsConfiguration.everythigDynamicOptions)
104  if (option.Equals(opString)) {
105  DynVarOptions.Instance.EverythingDynamic = true;
106  return;
107  }
108  // * Everything static option
109  foreach (string opString in OptionsConfiguration.everythigStaticOptions)
110  if (option.Equals(opString)) {
111  DynVarOptions.Instance.EverythingStatic = true;
112  return;
113  }
114  // * Run option
115  foreach (string opString in OptionsConfiguration.runOptions)
116  if (option.Equals(opString)) {
117  parameters.Run = true;
118  return;
119  }
120  // * Server option
121  foreach (string opString in OptionsConfiguration.serverOptions)
122  if (option.Equals(opString))
123  {
124  parameters.ServerOptimization = true;
125  return;
126  }
127  // * Out option
128  foreach (string opString in OptionsConfiguration.outputOptions)
129  if (option.StartsWith(opString)) {
130  string outFileName = ParseValue(option.Substring(opString.Length, option.Length - opString.Length));
131  parameters.OutputFileName = outFileName;
132  return;
133  }
134  // * Target option
135  foreach (string opString in OptionsConfiguration.targetOptions)
136  if (option.StartsWith(opString)) {
137  string targetName = ParseValue(option.Substring(opString.Length, option.Length - opString.Length));
138  if (Array.IndexOf(OptionsConfiguration.targetNames, targetName) == -1) {
139  Console.WriteLine(OptionsConfiguration.wrongTarget);
140  System.Environment.Exit(4); // 4 == Bad target
141  }
142  parameters.TargetPlatform = TargetPlatformRepresentation.Instance.getPlatformFromName(targetName);
143  return;
144  }
145  // * If the method has not returned, an error exists
146  Console.Error.WriteLine(OptionsConfiguration.errorMessage);
147  System.Environment.Exit(1); // 1 == Unknown option
148  }
149 
155  private static string ParseValue(string value) {
156  // * Option prefix
157  foreach (string opAssignment in OptionsConfiguration.optionsAssignment)
158  if (value.StartsWith(opAssignment))
159  return value.Substring(opAssignment.Length, value.Length - opAssignment.Length);
160  // * If the method has not returned, an error exists
161  Console.Error.WriteLine(OptionsConfiguration.errorMessage);
162  System.Environment.Exit(2); // 2 == Bad option assignment
163  return null;
164  }
165 
171  private static string ObtainOutputFileName(string[] inputFileNames) {
172  return Path.ChangeExtension(inputFileNames[0], ".exe");
173  }
174 
175  }
176 }
static DynVarOptions Instance
Unique class instance (Singleton)
Offers the global options of the dynamic behaviour configuration.
bool EverythingDynamic
To ignore all the dyn files, setting all the references to dynamic.
bool EverythingStatic
To ignore all the dyn files, setting all the references to dynamic.
static readonly TargetPlatform defaultTargetPlatform