Skip to content

Commit 72e7f90

Browse files
committed
finish Error type
1 parent fde69d4 commit 72e7f90

File tree

11 files changed

+132
-117
lines changed

11 files changed

+132
-117
lines changed

README.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,6 @@ fa语言是一款不容易误解、简单易学的编程语言。目前处于测
99

1010
### **qq交流群:1018390466 欢迎一起讨论语法/实现/轮子**
1111

12-
<!--
13-
备注:
14-
循环里检测是否所有路径都是同步,如果是,那么警告;循环加上@Sync那么压制警告;加上@Yield那么每次循环均yield
15-
-->
16-
17-
## TODO
18-
19-
<!--- .Ok ()、.Err () 函数的处理(理想处理方式:传lambda,接收正确结果或错误信息并执行代码)-->
20-
- 自举
21-
- 判断浮点数时误差处理
22-
- 可空类型变量switch
23-
- 类型switch
24-
- 线程锁
25-
- 标准库
26-
2712
## 目标
2813

2914
- 极简(编译器控制变量位于栈上、堆上、是否引用计数、是否原子化等)
@@ -36,6 +21,17 @@ fa语言是一款不容易误解、简单易学的编程语言。目前处于测
3621
<!--- 提前编译多种Native代码与SIMD(比如x86-64编译x86、amd64、MMX、SSE、AVX等),然后分别压缩-->
3722
<!--- 编译出的程序运行后检查当前平台支持的最快速的SIMD指令集,解压指令集代码,并执行-->
3823
<!-- + 在加速效果非常明显的位置编译SIMD代码-->
24+
<!--- 循环里检测是否所有路径都是同步,如果是,那么警告;循环加上@Sync那么压制警告;加上@Yield那么每次循环均yield-->
25+
26+
## TODO
27+
28+
<!--- .Ok ()、.Err () 函数的处理(理想处理方式:传lambda,接收正确结果或错误信息并执行代码)-->
29+
- 自举
30+
- 判断浮点数时误差处理
31+
- 可空类型变量switch
32+
- 类型switch
33+
- 线程锁
34+
- 标准库
3935

4036
## 进度
4137

@@ -56,9 +52,9 @@ fa语言快排实现:
5652

5753
![compile](imgs/20211222001053.jpg)
5854

59-
![stars](https://starchart.cc/fa-org/fa.svg)
55+
<!--![stars](https://starchart.cc/fa-org/fa.svg)
6056
61-
<!--## 当前进度
57+
## 当前进度
6258
6359
- [ ] 基本语法
6460
+ [x] 表达式

fa/fac.Test/BuildTool.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ private static string _RunAndGetReturn (string _code) {
6060
Info.CurrentClass = null;
6161
Info.CurrentFunc = null;
6262
//
63+
Info.Programs.Clear ();
64+
fac.Program.ReadBuildinSourceAsync ().Wait ();
6365
// 读取源码
6466
Info.CurrentFile = Path.Combine (Info.SrcPath, "App.fa");
6567
File.WriteAllText (Info.CurrentFile, _code);
6668
Log.Mark (LogMark.Parse);
67-
Info.Programs.Clear ();
6869
Info.Programs.Add (Common.ParseCode<AstProgram> (Info.CurrentSourceCode = _code));
6970

7071
// 编译

fa/fac/ASTs/AstProgram.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public AstProgram (FaParser.ProgramContext _ctx) {
2828
CurrentSourceCode = Info.CurrentSourceCode;
2929

3030
// 生成模块名
31-
CurrentModule = $"{Info.ProjectName}.{Info.CurrentRelativeFile.Replace ('/', '.').Replace ('\\', '.')}"[..^3];
31+
CurrentModule = Info.CurrentRelativeFile.Replace ('/', '.').Replace ('\\', '.')[..^3];
3232

3333
// 生成命名空间
3434
CurrentNamespace = CurrentModule[..CurrentModule.LastIndexOf ('.')];

fa/fac/ASTs/Exprs/Names/AstExprName_BuildIn.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Linq;
8+
using System.Reflection;
89
using System.Text;
910
using System.Threading.Tasks;
1011

@@ -65,6 +66,12 @@ public override (List<IAstStmt>, IAstExpr) ExpandExpr ((IAstExprName _var, AstSt
6566
public override string GenerateCSharp (int _indent) => Name switch {
6667
"Directory.Create" => "Directory.CreateDirectory",
6768
"@FILE" => Common.WrapStringValue (Info.CurrentFile),
69+
"@FILEDATA" when Info.CurrentFile.StartsWith ("res://") => new Func<string>(() => {
70+
var _assembly = Assembly.GetExecutingAssembly ();
71+
using var _stream = _assembly.GetManifestResourceStream ($"fac.{Info.CurrentFile[6..].Replace ('/', '.')}");
72+
using var _reader = new StreamReader (_stream, Encoding.UTF8);
73+
return Common.WrapStringValue (_reader.ReadToEnd ());
74+
}) (),
6875
"@FILEDATA" => Common.WrapStringValue (File.ReadAllText (Info.CurrentFile, Encoding.UTF8)),
6976
_ => Name,
7077
};

fa/fac/ASTs/Exprs/Names/AstExprName_ClassEnum_Access.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private AstExprName_ClassEnum_Access () { }
2323
public static AstExprName_ClassEnum_Access FromSwitchCond (AstExprName_ClassEnum_New _new_expr) => new AstExprName_ClassEnum_Access { Token = _new_expr.Token, EnumClass = _new_expr.EnumClass, Value = null, EnumItemIndex = _new_expr.EnumItemIndex };
2424

2525
public static AstExprName_ClassEnum_Access FromAccess (IAstExpr _opt, string _enum_name) {
26-
var _class = (_opt.ExpectType ?? _opt.GuessType ()).Optional.AstClass;
26+
var _class = (_opt.ExpectType ?? _opt.GuessType ()).AstClass;
2727
int _p = _enum_name.LastIndexOf ('.');
2828
if (_p != -1) {
2929
string _clsname = _enum_name[.._p];

fa/fac/Info.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,11 @@ public static List<IAstClass> GetTemplateClassFromName (string _name, int _templ
116116
public static string CurrentRelativeFile {
117117
get {
118118
if (CurrentFile.StartsWith ("res://")) {
119-
return CurrentFile;
119+
return CurrentFile[6..];
120120
} else {
121121
string _s = CurrentFile.Substring (SrcPath.Length);
122-
return (_s[0] == '/' || _s[0] == '\\') ? _s.Substring (1) : _s;
122+
_s = (_s[0] == '/' || _s[0] == '\\') ? _s.Substring (1) : _s;
123+
return $"{Info.ProjectName}.{_s}";
123124
}
124125
}
125126
}

fa/fac/Program.cs

Lines changed: 99 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
using System.Threading.Tasks;
1515

1616
namespace fac {
17-
class Program {
17+
public class Program {
18+
/// <summary>
19+
/// 获取文件夹下所有文件
20+
/// </summary>
21+
/// <param name="_path"></param>
22+
/// <returns></returns>
1823
private static List<string> _GetAllFiles (string _path) {
1924
var _files = new List<string> ();
2025
_files.AddRange (Directory.GetFiles (_path));
@@ -25,8 +30,12 @@ private static List<string> _GetAllFiles (string _path) {
2530
return _files;
2631
}
2732

28-
// 调用进程
29-
private static void _process_run (string _cmd) {
33+
/// <summary>
34+
/// 调用进程
35+
/// </summary>
36+
/// <param name="_cmd"></param>
37+
/// <returns></returns>
38+
private static async Task RunProcessAsync (string _cmd) {
3039
int _p = _cmd.IndexOf (' ');
3140
string _file = _p == -1 ? _cmd : _cmd[.._p];
3241
string _arg = _p == -1 ? "" : _cmd[(_p+1)..];
@@ -37,26 +46,23 @@ private static void _process_run (string _cmd) {
3746
_ps.StartInfo.Arguments = _arg;
3847
_ps.StartInfo.CreateNoWindow = false;
3948
_ps.Start ();
40-
_ps.WaitForExit ();
49+
await _ps.WaitForExitAsync ();
4150
}
4251

43-
// 从路径中提取有意义的名称做项目名
52+
/// <summary>
53+
/// 从路径中提取有意义的名称做项目名
54+
/// </summary>
55+
/// <param name="_path"></param>
56+
/// <returns></returns>
4457
private static string _GetLastName (string _path) {
45-
var _sb = new StringBuilder ();
46-
for (int i = _path.Length - 1; i >= 0; --i) {
47-
if (i == '/' || i == '\\' || i == ':' || i == '.') {
48-
if (_sb.Length > 0)
49-
return string.Join ("", _sb.ToString ().Reverse ());
50-
} else {
51-
_sb.Append (_path[i]);
52-
}
53-
}
54-
if (_sb.Length > 0)
55-
return string.Join ("", _sb.ToString ().Reverse ());
56-
return "Hello";
58+
return _path[(_path.LastIndexOfAny (new char[] { '/', '\\' }) + 1)..];
5759
}
5860

59-
// 更新检查
61+
/// <summary>
62+
/// 更新检查
63+
/// </summary>
64+
/// <param name="version"></param>
65+
/// <returns></returns>
6066
private static async Task CheckUpdatesAsync (Version version) {
6167
try {
6268
using var client = new HttpClient { Timeout = TimeSpan.FromSeconds (2) };
@@ -83,38 +89,78 @@ private static async Task CheckUpdatesAsync (Version version) {
8389
} catch (Exception) {
8490
}
8591
}
86-
static void Main (string[] args) {
92+
93+
/// <summary>
94+
/// 打印初始化信息
95+
/// </summary>
96+
private static async Task PrintInfoAsync () {
8797
var _asm = Assembly.GetExecutingAssembly ();
8898
Console.WriteLine ($"fa语言编译器");
8999
Console.WriteLine ($" 版权:{(Attribute.GetCustomAttribute (_asm, typeof (AssemblyCopyrightAttribute)) as AssemblyCopyrightAttribute).Copyright}");
90100
Console.WriteLine ($" 版本:{_asm.GetName ().Version}");
91101
Console.WriteLine ($" 源码:https://github.com/fa-org/fa");
92102
if (!Debugger.IsAttached)
93-
CheckUpdatesAsync (_asm.GetName ().Version).Wait ();
103+
await CheckUpdatesAsync (_asm.GetName ().Version);
94104
Console.WriteLine ();
95-
if (args.Length == 0 || (args.Length == 1 && (args[0] == "--help" || args[0] == "-help" || args[0] == "/help" || args[0] == "-?" || args[0] == "/?"))) {
96-
var _exename = Process.GetCurrentProcess ().MainModule.ModuleName;
97-
if (_exename == "dotnet" || _exename == "dotnet.exe") {
98-
var _location = Assembly.GetEntryAssembly ().Location;
99-
_location = _location[(_location.LastIndexOfAny (new char[] { '/', '\\' }) + 1)..];
100-
_exename = $"{_exename} {_location}";
101-
}
102-
Console.WriteLine ($"用法:");
103-
Console.WriteLine ($"{_exename} fa程序文件目录|fa程序文件.fa [-d] [-r]");
104-
Console.WriteLine ();
105-
Console.WriteLine ($" -d 调试模式,将在控制台打印C#后端输出");
106-
Console.WriteLine ($" -r 发布模式,将编译为可发布版本");
107-
Console.WriteLine ();
108-
Console.WriteLine ($"备注:如果指定目录,则目录名不要以“.fa”结尾,另外fa程序扩展名必须小写");
109-
Console.WriteLine ($"示例:");
110-
if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
111-
Console.WriteLine ($" {_exename} D:\\FaProject -r");
112-
Console.WriteLine ($" {_exename} D:\\FaProject\\Program.fa -d");
113-
} else {
114-
Console.WriteLine ($" {_exename} /FaProject -r");
115-
Console.WriteLine ($" {_exename} /FaProject/Program.fa -d");
105+
}
106+
107+
/// <summary>
108+
/// 打印使用手册
109+
/// </summary>
110+
private static void PrintManual () {
111+
var _exename = Process.GetCurrentProcess ().MainModule.ModuleName;
112+
if (_exename == "dotnet" || _exename == "dotnet.exe") {
113+
var _location = Assembly.GetEntryAssembly ().Location;
114+
_location = _location[(_location.LastIndexOfAny (new char[] { '/', '\\' }) + 1)..];
115+
_exename = $"{_exename} {_location}";
116+
}
117+
Console.WriteLine ($"用法:");
118+
Console.WriteLine ($"{_exename} fa程序文件目录|fa程序文件.fa [-d] [-r]");
119+
Console.WriteLine ();
120+
Console.WriteLine ($" -d 调试模式,将在控制台打印C#后端输出");
121+
Console.WriteLine ($" -r 发布模式,将编译为可发布版本");
122+
Console.WriteLine ();
123+
Console.WriteLine ($"备注:如果指定目录,则目录名不要以“.fa”结尾,另外fa程序扩展名必须小写");
124+
Console.WriteLine ($"示例:");
125+
if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
126+
Console.WriteLine ($" {_exename} D:\\FaProject -r");
127+
Console.WriteLine ($" {_exename} D:\\FaProject\\Program.fa -d");
128+
} else {
129+
Console.WriteLine ($" {_exename} /FaProject -r");
130+
Console.WriteLine ($" {_exename} /FaProject/Program.fa -d");
131+
}
132+
Console.WriteLine ();
133+
}
134+
135+
/// <summary>
136+
/// 读取编译器自带标准库,比如Error等,用于后续part拼接,以及替换掉buildin实现
137+
/// </summary>
138+
public static async Task ReadBuildinSourceAsync () {
139+
var _assembly = Assembly.GetExecutingAssembly ();
140+
var _fa_files = _assembly.GetManifestResourceNames ();
141+
_fa_files = (from p in _fa_files where p[..7] == "fac.fa." select p).ToArray ();
142+
foreach (var _fa_file in _fa_files) {
143+
Info.CurrentFile = $"res://fa/{_fa_file[7..]}";
144+
using var _stream = _assembly.GetManifestResourceStream (_fa_file);
145+
using var _reader = new StreamReader (_stream, Encoding.UTF8);
146+
Info.CurrentSourceCode = await _reader.ReadToEndAsync ();
147+
Log.Mark (LogMark.Parse);
148+
var _program = Common.ParseCode<AstProgram> (Info.CurrentSourceCode);
149+
if (_program == null) {
150+
if (Debugger.IsAttached) {
151+
Console.WriteLine ($"按任意键退出。。。");
152+
Console.ReadKey ();
153+
}
154+
return;
116155
}
117-
Console.WriteLine ();
156+
Info.Programs.Add (_program);
157+
}
158+
}
159+
160+
private static async Task Main (string[] args) {
161+
await PrintInfoAsync ();
162+
if (args.Length == 0 || (args.Length == 1 && (args[0] == "--help" || args[0] == "-help" || args[0] == "/help" || args[0] == "-?" || args[0] == "/?"))) {
163+
PrintManual ();
118164
if (Debugger.IsAttached) {
119165
Console.WriteLine ($"按任意键退出。。。");
120166
Console.ReadKey ();
@@ -131,9 +177,11 @@ static void Main (string[] args) {
131177
Console.WriteLine ($"按任意键退出。。。");
132178
Console.ReadKey ();
133179
}
180+
return;
134181
}
135-
Info.ProjectName = _GetLastName (_path[..^3]);
136-
Info.SrcPath = Path.GetDirectoryName (_path);
182+
_path = _path[.._path.LastIndexOfAny (new char[] { '/', '\\' })];
183+
Info.ProjectName = _GetLastName (_path);
184+
Info.SrcPath = _path;
137185
} else {
138186
// 文件夹
139187
if (!Directory.Exists (_path)) {
@@ -142,6 +190,7 @@ static void Main (string[] args) {
142190
Console.WriteLine ($"按任意键退出。。。");
143191
Console.ReadKey ();
144192
}
193+
return;
145194
}
146195
Info.ProjectName = _GetLastName (_path);
147196
Info.SrcPath = args[0];
@@ -164,31 +213,12 @@ static void Main (string[] args) {
164213
_src_files = (from p in _src_files where p[^3..].ToLower () == ".fa" select p).ToList ();
165214
}
166215

167-
// 读取编译器自带标准库,比如Error等,用于后续part拼接,以及替换掉buildin实现
168-
var _assembly = Assembly.GetExecutingAssembly ();
169-
var _fa_files = _assembly.GetManifestResourceNames ();
170-
_fa_files = (from p in _fa_files where p[..7] == "fac.fa." select p).ToArray ();
171-
foreach (var _fa_file in _fa_files) {
172-
Info.CurrentFile = $"res://fa/{_fa_file[7..]}";
173-
using var _stream = _assembly.GetManifestResourceStream (_fa_file);
174-
using var _reader = new StreamReader (_stream, Encoding.UTF8);
175-
Info.CurrentSourceCode = _reader.ReadToEnd ();
176-
Log.Mark (LogMark.Parse);
177-
var _program = Common.ParseCode<AstProgram> (Info.CurrentSourceCode);
178-
if (_program == null) {
179-
if (Debugger.IsAttached) {
180-
Console.WriteLine ($"按任意键退出。。。");
181-
Console.ReadKey ();
182-
}
183-
return;
184-
}
185-
Info.Programs.Add (_program);
186-
}
216+
await ReadBuildinSourceAsync ();
187217

188218
// 读取源码
189219
foreach (var _src_file in _src_files) {
190220
Info.CurrentFile = _src_file;
191-
Info.CurrentSourceCode = File.ReadAllText (_src_file, Encoding.UTF8);
221+
Info.CurrentSourceCode = await File.ReadAllTextAsync (_src_file, Encoding.UTF8);
192222
Log.Mark (LogMark.Parse);
193223
var _program = Common.ParseCode<AstProgram> (Info.CurrentSourceCode);
194224
if (_program == null) {
@@ -239,20 +269,20 @@ static void Main (string[] args) {
239269
Directory.SetCurrentDirectory (Info.DestPath);
240270
string _dest_src = Info.GenerateCSharp ();
241271
if (!File.Exists (Path.Combine (Info.DestPath, $"{Info.ProjectName}.csproj")))
242-
_process_run ("dotnet new console --force");
272+
await RunProcessAsync ("dotnet new console --force");
243273
if (args.Contains ("-d")) {
244274
Console.WriteLine ("-------------------- generate begin --------------------");
245275
Console.WriteLine (_dest_src);
246276
Console.WriteLine ("-------------------- generate end --------------------");
247277
}
248278
string _dest_file = Path.Combine (Info.DestPath, "Program.cs");
249-
File.WriteAllText (_dest_file, _dest_src, Encoding.UTF8);
279+
await File.WriteAllTextAsync (_dest_file, _dest_src, Encoding.UTF8);
250280
if (args.Contains ("-r")) {
251281
Console.WriteLine ("正在构建。。。");
252-
_process_run ("dotnet publish");
282+
await RunProcessAsync ("dotnet publish");
253283
} else {
254284
Console.WriteLine ("正在构建并运行。。。");
255-
_process_run ("dotnet run");
285+
await RunProcessAsync ("dotnet run");
256286
}
257287
//
258288
if (Debugger.IsAttached) {

fa/fac/fac.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net5.0</TargetFramework>
6-
<AssemblyVersion>0.0.5.0</AssemblyVersion>
7-
<FileVersion>0.0.5.0</FileVersion>
8-
<Version>0.0.5</Version>
9-
<Copyright>Copyright © 2021</Copyright>
6+
<AssemblyVersion>0.0.6.0</AssemblyVersion>
7+
<FileVersion>0.0.6.0</FileVersion>
8+
<Version>0.0.6</Version>
9+
<Copyright>Copyright © 2021-2022</Copyright>
1010
<Authors>fawdlstty</Authors>
1111
<Company>fa-org</Company>
1212
<Description>fa语言编译器(C#后端)</Description>

src/fa/Error.fa

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/fa/Optional.fa

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)