Repository navigation
Expand file tree
/
Copy pathfut.cs
More file actions
215 lines (200 loc) · 6.02 KB
/
Copy pathfut.cs
File metadata and controls
215 lines (200 loc) · 6.02 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// fut.cs - Fusion transpiler
//
// Copyright (C) 2011-2026 Piotr Fusik
//
// This file is part of Fusion Transpiler,
// see https://github.com/fusionlanguage/fut
//
// Fusion Transpiler 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.
//
// Fusion Transpiler 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 Fusion Transpiler. If not, see http://www.gnu.org/licenses/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
[assembly: AssemblyTitle("fut")]
[assembly: AssemblyDescription("Fusion Transpiler")]
namespace Fusion
{
class FileGenHost : FuConsoleHost
{
readonly List<string> ResourceDirs = new List<string>();
string Filename = null!;
TextWriter CurrentFile = null!;
public void AddResourceDir(string path) => this.ResourceDirs.Add(path);
List<byte> ReadResource(string name, FuPrefixExpr expr)
{
foreach (string dir in this.ResourceDirs) {
string path = Path.Combine(dir, name);
if (File.Exists(path))
return File.ReadAllBytes(path).ToList();
}
if (File.Exists(name))
return File.ReadAllBytes(name).ToList();
ReportStatementError(expr, $"File '{name}' not found");
return new List<byte>();
}
internal override int GetResourceLength(string name, FuPrefixExpr expr)
{
if (!GetResources().TryGetValue(name, out List<byte>? content)) {
content = ReadResource(name, expr);
GetResources().Add(name, content);
}
return content.Count;
}
public override TextWriter CreateFile(string? directory, string filename)
{
if (directory != null)
filename = Path.Combine(directory, filename);
this.Filename = filename;
this.CurrentFile = new StreamWriter(filename);
return this.CurrentFile;
}
public override void CloseFile()
{
this.CurrentFile.Close();
if (HasErrors())
File.Delete(this.Filename);
}
protected override string ToDirectory(string path) => Directory.Exists(path) ? path : Path.GetDirectoryName(path)!;
}
public static class Fut
{
static FuProgram? ParseAndResolve(FuParser parser, FuSystem system, FuScope parent, List<string> files, FuSema sema, FuConsoleHost host)
{
host.Program = new FuProgram { Parent = parent, System = system };
foreach (string file in files) {
byte[] input = File.ReadAllBytes(file);
parser.Parse(file, input, input.Length);
}
if (host.HasErrors())
return null;
sema.Process();
if (host.HasErrors())
return null;
return host.Program;
}
public static int Main(string[] args)
{
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
FileGenHost host = new FileGenHost();
FuParser parser = new FuParser();
List<string> inputFiles = new List<string>();
List<string> referencedFiles = new List<string>();
string? lang = null;
string? outputFile = null;
string namespace_ = "";
for (int i = 0; i < args.Length; i++) {
string arg = args[i];
if (arg.Length < 2 || arg[0] != '-')
inputFiles.Add(arg);
else if (arg == "--help") {
FuConsoleHost.Usage("fut");
return 0;
}
else if (arg == "--version") {
Console.WriteLine($"Fusion Transpiler {FuConsoleHost.Version} (C#)");
return 0;
}
else if (arg.Length == 2 && i + 1 < args.Length) {
switch (arg[1]) {
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") {
Console.Error.WriteLine($"fut: ERROR: '{symbol}' is reserved");
return 1;
}
parser.AddPreSymbol(symbol);
break;
case 'r':
referencedFiles.Add(args[++i]);
break;
case 'I':
host.AddResourceDir(args[++i]);
break;
default:
Console.Error.WriteLine($"fut: ERROR: Unknown option: {arg}");
return 1;
}
}
else {
Console.Error.WriteLine($"fut: ERROR: Unknown option: {arg}");
return 1;
}
}
if (outputFile == null || inputFiles.Count == 0) {
FuConsoleHost.Usage("fut");
return 1;
}
FuSema sema = new FuSema();
parser.SetHost(host);
sema.SetHost(host);
FuSystem system = FuSystem.New();
FuScope? parent = system;
try {
if (referencedFiles.Count > 0) {
parent = ParseAndResolve(parser, system, parent, referencedFiles, sema, host);
if (parent == null)
return 1;
}
FuProgram? program = ParseAndResolve(parser, system, parent, inputFiles, sema, host);
if (program == null)
return 1;
if (lang != null) {
host.Emit(program, lang, namespace_, outputFile);
return host.HasErrors() ? 1 : 0;
}
for (int i = outputFile.Length; --i >= 0; ) {
char c = outputFile[i];
if (c == '.') {
if (i >= 2
&& (outputFile[i - 2] == '.' || outputFile[i - 2] == ',')
&& string.CompareOrdinal(outputFile, i - 1, "d.ts", 0, 4) == 0
&& (i + 3 == outputFile.Length || outputFile[i + 3] == ','))
continue;
string outputBase = outputFile.Substring(0, i + 1);
int exitCode = 0;
foreach (string outputExt in outputFile.Substring(i + 1).Split(',')) {
host.Emit(program, outputExt, namespace_, outputBase + outputExt);
if (host.HasErrors()) {
host.SetErrors(false);
exitCode = 1;
}
}
return exitCode;
}
if (c == Path.DirectorySeparatorChar
|| c == Path.AltDirectorySeparatorChar
|| c == Path.VolumeSeparatorChar)
break;
}
Console.Error.WriteLine($"fut: ERROR: Don't know what language to translate to: no extension in '{outputFile}' and no '-l' option");
return 1;
}
catch (IOException e) {
Console.Error.WriteLine("fut: ERROR: " + e.Message);
return 1;
}
}
}
}