forked from fusionlanguage/fut
/
CiTo.cs
132 lines (124 loc) · 3.73 KB
/
CiTo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// CiTo.cs - Ci translator
//
// Copyright (C) 2011-2019 Piotr Fusik
//
// This file is part of CiTo, see https://github.com/pfusik/cito
//
// CiTo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// CiTo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with CiTo. If not, see http://www.gnu.org/licenses/
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
[assembly: AssemblyTitle("CiTo")]
[assembly: AssemblyDescription("Ci Translator")]
namespace Foxoft.Ci
{
public static class CiTo
{
static void Usage()
{
Console.WriteLine("Usage: cito [OPTIONS] -o FILE INPUT.ci");
Console.WriteLine("Options:");
Console.WriteLine("-l c Translate to C");
Console.WriteLine("-l cpp Translate to C++");
Console.WriteLine("-l cs Translate to C#");
Console.WriteLine("-l java Translate to Java");
Console.WriteLine("-l js Translate to JavaScript");
Console.WriteLine("-o FILE Write to the specified file");
Console.WriteLine("-n NAME Specify C++/C# namespace or Java package");
Console.WriteLine("-D NAME Define conditional compilation symbol");
Console.WriteLine("-I DIR Add directory to resource search path");
Console.WriteLine("--help Display this information");
Console.WriteLine("--version Display version information");
}
public static int Main(string[] args)
{
CiParser parser = new CiParser();
List<string> inputFiles = new List<string>();
List<string> searchDirs = new List<string>();
string lang = null;
string outputFile = null;
string namespace_ = null;
for (int i = 0; i < args.Length; i++) {
string arg = args[i];
if (arg[0] == '-') {
switch (arg) {
case "--help":
Usage();
return 0;
case "--version":
Console.WriteLine("cito 1.0.0");
return 0;
case "-l":
lang = args[++i];
break;
case "-o":
outputFile = args[++i];
break;
case "-n":
namespace_ = args[++i];
break;
case "-D":
string symbol = args[++i];
if (symbol == "true" || symbol == "false")
throw new ArgumentException(symbol + " is reserved");
parser.PreSymbols.Add(symbol);
break;
case "-I":
searchDirs.Add(args[++i]);
break;
default:
throw new ArgumentException("Unknown option: " + arg);
}
}
else {
inputFiles.Add(arg);
}
}
if (lang == null && outputFile != null) {
string ext = Path.GetExtension(outputFile);
if (ext.Length >= 2)
lang = ext.Substring(1);
}
if (lang == null || outputFile == null || inputFiles.Count == 0) {
Usage();
return 1;
}
GenBase gen;
switch (lang) {
case "c": gen = new GenC(); break;
case "cpp": gen = new GenCpp(); break;
case "cs": gen = new GenCs(); break;
case "java": gen = new GenJava(); break;
case "js": gen = new GenJs(); break;
default: throw new ArgumentException("Unknown language: " + lang);
}
gen.Namespace = namespace_;
gen.OutputFile = outputFile;
CiProgram program;
try {
foreach (string inputFile in inputFiles)
parser.Parse(inputFile, File.OpenText(inputFile));
program = parser.Program;
new CiResolver(program, searchDirs);
} catch (CiException ex) {
Console.Error.WriteLine("{0}({1}): ERROR: {2}", ex.Filename, ex.Line, ex.Message);
return 1;
// throw;
}
gen.Write(program);
return 0;
}
}
}