Skip to content

Commit 3588eaa

Browse files
committed
0.0.5 complete built-in methods
1 parent f3ec076 commit 3588eaa

File tree

17 files changed

+171
-80
lines changed

17 files changed

+171
-80
lines changed

fa/fac.Test/BuildTool.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private static string _RunAndGetReturn (string _code) {
6262
//
6363
// 读取源码
6464
Info.CurrentFile = Path.Combine (Info.SrcPath, "App.fa");
65+
File.WriteAllText (Info.CurrentFile, _code);
6566
Log.Mark (LogMark.Parse);
6667
Info.Programs.Clear ();
6768
Info.Programs.Add (Common.ParseCode<AstProgram> (Info.CurrentSourceCode = _code));

fa/fac.Test/UnitTest00_hello.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,52 @@ public static void Main () {
1717
string _ret = BuildTool.RunAndGetReturn (_code);
1818
Assert.AreEqual (_ret, "hello");
1919
}
20+
21+
[TestMethod]
22+
public void TestMethod2 () {
23+
// 备注:所有内建方法请参见 fa/fac/ASTs/Exprs/Names/AstExprName_BuildIn.cs
24+
string _code = @"
25+
use fa;
26+
27+
class Program {
28+
public static void Main () {
29+
// 读当前文件
30+
string _src1 = File.ReadAllText (@FILE);
31+
// 直接获取当前文件源码
32+
string _src2 = @FILEDATA;
33+
if _src1 != _src2 {
34+
Console.WriteLine (""error"");
35+
}
36+
// 控制台输出/控制台输出行
37+
Console.Write (""a"");
38+
Console.WriteLine (""b"");
39+
// 检查文件是否存在
40+
if !File.Exists (@FILE) {
41+
Console.WriteLine (""error"");
42+
}
43+
// 写文件
44+
File.WriteAllText (""D:\\a.fa"", ""// this file generate by fa test\r\n"");
45+
// 追加文件
46+
File.AppendAllText (""D:\\a.fa"", @FILEDATA);
47+
// 输出文件大小
48+
string _src3 = File.ReadAllText (""D:\\a.fa"");
49+
Console.Write (""{0}"".Format (_src3.Length));
50+
// 删除临时文件
51+
File.Delete (""D:\\a.fa"");
52+
//// 判断文件夹是否存在
53+
//bool _b = Directory.Exists (""D:\\folder"");
54+
//// 创建文件夹
55+
//Directory.Create (""D:\\folder"");
56+
//// 获取文件夹下所有文件名称
57+
//string[] _files = Directory.GetFiles (""D:\\folder"");
58+
//// 删除文件夹
59+
//Directory.Delete (""D:\\folder"");
60+
}
61+
}
62+
";
63+
string _ret = BuildTool.RunAndGetReturn (_code);
64+
Assert.AreEqual (_ret[..4], "ab\r\n");
65+
Assert.IsTrue (int.Parse (_ret[4..]) > 500);
66+
}
2067
}
2168
}

fa/fac/ASTs/Exprs/AstExpr_AccessBuildIn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public override string GenerateCSharp (int _indent) {
227227
var _attach0 = (AttachArgs?.Count ?? 0) > 0 ? AttachArgs[0].GenerateCSharp (_indent) : "";
228228
return AccessType switch {
229229
AccessBuildInType.ARR_New => $"new {_exp} ()",
230-
AccessBuildInType.ARR_Length => $"{_b}.Count",
230+
AccessBuildInType.ARR_Length => Value.ExpectType is AstType_ArrayWrap ? $"{_b}.Count" : $"{_b}.Length",
231231
AccessBuildInType.ARR_Add => $"{_b}.Add ({_attach0})",
232232
AccessBuildInType.ARR_AccessItem => $"{_b} [{_attach0}]",
233233
AccessBuildInType.OPT_HasValue => $"{_b}.HasValue ()",

fa/fac/ASTs/Exprs/AstExpr_BaseId.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override IAstExpr TraversalCalcType (IAstType _expect_type) {
2727
ExprTraversals.Complete = false;
2828
return this;
2929
} else {
30-
throw new Exception ("执行类型处理步骤时不再允许出现 AstExpr_BaseId 类型对象");
30+
throw new CodeException (Token, $"未定义的标识符 {Id}");
3131
}
3232
}
3333

fa/fac/ASTs/Exprs/AstExpr_OpN.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ public override string GenerateCSharp (int _indent) {
138138
if (Arguments.Any ())
139139
_sb.Remove (_sb.Length - 2, 2);
140140
_sb.Append (")");
141+
if (Value is AstExprName_BuildIn _biexpr1 && _biexpr1.Name == "Directory.GetFiles")
142+
_sb.Append (".ToList ()");
141143
return _sb.ToString ();
142144
}
143145

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

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using fac.ASTs.Types;
1+
using fac.ASTs.Stmts;
2+
using fac.ASTs.Types;
23
using fac.Exceptions;
34
using System;
45
using System.Collections.Generic;
@@ -15,17 +16,27 @@ public class AstExprName_BuildIn: IAstExprName {
1516

1617

1718
private static Dictionary<string, AstExprName_BuildIn> sBuildIn = new Dictionary<string, AstExprName_BuildIn> {
18-
["continue"] = new AstExprName_BuildIn { Token = null, Name = "continue", NameType = "void" },
19-
["break"] = new AstExprName_BuildIn { Token = null, Name = "break", NameType = "void" },
20-
["Console.WriteLine"] = new AstExprName_BuildIn { Token = null, Name = "Console.WriteLine", NameType = "Func<string, void>" },
21-
["Console.Write"] = new AstExprName_BuildIn { Token = null, Name = "Console.Write", NameType = "Func<string, void>" },
22-
["string.Format"] = new AstExprName_BuildIn { Token = null, Name = "string.Format", NameType = "Func<string, params any[], string>" },
23-
["File.Exists"] = new AstExprName_BuildIn { Token = null, Name = "File.Exists", NameType = "Func<string, bool>" },
24-
["File.ReadAllText"] = new AstExprName_BuildIn { Token = null, Name = "File.ReadAllText", NameType = "Func<string, string>" },
25-
["File.WriteAllText"] = new AstExprName_BuildIn { Token = null, Name = "File.WriteAllText", NameType = "Func<string, string, void>" },
26-
["File.AppendAllText"] = new AstExprName_BuildIn { Token = null, Name = "File.AppendAllText", NameType = "Func<string, string, void>" },
27-
["@FILE"] = new AstExprName_BuildIn { Token = null, Name = "@FILE", NameType = "string" },
28-
["@SOURCE"] = new AstExprName_BuildIn { Token = null, Name = "@SOURCE", NameType = "string" },
19+
["continue"] = new AstExprName_BuildIn { Name = "continue", NameType = "void" },
20+
["break"] = new AstExprName_BuildIn { Name = "break", NameType = "void" },
21+
//
22+
["Console.WriteLine"] = new AstExprName_BuildIn { Name = "Console.WriteLine", NameType = "Func<string, void>" },
23+
["Console.Write"] = new AstExprName_BuildIn { Name = "Console.Write", NameType = "Func<string, void>" },
24+
//
25+
["string.Format"] = new AstExprName_BuildIn { Name = "string.Format", NameType = "Func<string, params any[], string>" },
26+
//
27+
["File.Exists"] = new AstExprName_BuildIn { Name = "File.Exists", NameType = "Func<string, bool>" },
28+
["File.ReadAllText"] = new AstExprName_BuildIn { Name = "File.ReadAllText", NameType = "Func<string, string>" },
29+
["File.WriteAllText"] = new AstExprName_BuildIn { Name = "File.WriteAllText", NameType = "Func<string, string, void>" },
30+
["File.AppendAllText"] = new AstExprName_BuildIn { Name = "File.AppendAllText", NameType = "Func<string, string, void>" },
31+
["File.Delete"] = new AstExprName_BuildIn { Name = "File.Delete", NameType = "Func<string, void>" },
32+
//
33+
["Directory.Exists"] = new AstExprName_BuildIn { Name = "Directory.Exists", NameType = "Func<string, bool>" },
34+
["Directory.Create"] = new AstExprName_BuildIn { Name = "Directory.Create", NameType = "Func<string, void>" },
35+
["Directory.Delete"] = new AstExprName_BuildIn { Name = "Directory.Delete", NameType = "Func<string, void>" },
36+
["Directory.GetFiles"] = new AstExprName_BuildIn { Name = "Directory.GetFiles", NameType = "Func<string, string[]>" },
37+
//
38+
["@FILE"] = new AstExprName_BuildIn { Name = "@FILE", NameType = "string" },
39+
["@FILEDATA"] = new AstExprName_BuildIn { Name = "@FILEDATA", NameType = "string" },
2940
};
3041

3142
public static AstExprName_BuildIn FindFromName (string _name) {
@@ -36,29 +47,26 @@ public static AstExprName_BuildIn FindFromName (string _name) {
3647

3748
public override IAstExpr TraversalCalcType (IAstType _expect_type) {
3849
if (ExpectType == null)
39-
ExpectType = NameType != "" ? IAstType.FromName (NameType) : null;
50+
ExpectType = IAstType.FromName (NameType);
4051
return AstExprTypeCast.Make (this, _expect_type);
4152
}
4253

4354
public override IAstType GuessType () {
4455
if (ExpectType == null)
45-
ExpectType = NameType != "" ? IAstType.FromName (NameType) : null;
56+
ExpectType = IAstType.FromName (NameType);
4657
return ExpectType;
4758
}
4859

60+
public override (List<IAstStmt>, IAstExpr) ExpandExpr ((IAstExprName _var, AstStmt_Label _pos)? _cache_err) {
61+
// TODO 扩展写文件之类的错误判断
62+
return (new List<IAstStmt> (), this);
63+
}
64+
4965
public override string GenerateCSharp (int _indent) => Name switch {
50-
"continue" => "continue",
51-
"break" => "break",
52-
"Console.WriteLine" => "Console.WriteLine",
53-
"Console.Write" => "Console.Write",
54-
"string.Format" => "string.Format",
55-
"File.Exists" => "File.Exists",
56-
"File.ReadAllText" => "File.ReadAllText",
57-
"File.WriteAllText" => "File.WriteAllText",
58-
"File.AppendAllText" => "File.AppendAllText",
66+
"Directory.Create" => "Directory.CreateDirectory",
5967
"@FILE" => Common.WrapStringValue (Info.CurrentFile),
60-
"@SOURCE" => Common.WrapStringValue (File.ReadAllText (Info.CurrentFile, Encoding.UTF8)),
61-
_ => throw new UnimplException (Token),
68+
"@FILEDATA" => Common.WrapStringValue (File.ReadAllText (Info.CurrentFile, Encoding.UTF8)),
69+
_ => Name,
6270
};
6371

6472
public override bool AllowAssign () => false;

fa/fac/ASTs/Structs/AstClass.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public bool Compile () {
7272
ClassFuncs[j].BodyCodes.TraversalWraps ((_deep: 1, _group: 0, _loop: 0, _cb: ExprTraversals.Traversal));
7373
Info.InitFunc (ClassFuncs[j]);
7474
ClassFuncs[j].BodyCodes.TraversalWraps ((_deep: 1, _group: 0, _loop: 1, _cb: ExprTraversals.Traversal));
75+
ClassFuncs[j].BodyCodes.TraversalCalcType ();
7576
Info.InitFunc (ClassFuncs[j]);
7677
ClassFuncs[j].BodyCodes.TraversalWraps ((_deep: 1, _group: 0, _loop: i, _cb: ExprTraversals.Traversal));
7778
}

fa/fac/ASTs/Structs/AstEnum.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public bool Compile () {
103103
ClassFuncs[j].BodyCodes.TraversalWraps ((_deep: 1, _group: 0, _loop: 0, _cb: ExprTraversals.Traversal));
104104
Info.InitFunc (ClassFuncs[j]);
105105
ClassFuncs[j].BodyCodes.TraversalWraps ((_deep: 1, _group: 0, _loop: 1, _cb: ExprTraversals.Traversal));
106+
ClassFuncs[j].BodyCodes.TraversalCalcType ();
106107
Info.InitFunc (ClassFuncs[j]);
107108
ClassFuncs[j].BodyCodes.TraversalWraps ((_deep: 1, _group: 0, _loop: i, _cb: ExprTraversals.Traversal));
108109
}

fa/fac/ASTs/Structs/AstTemplateClassInst.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public bool Compile () {
9393
ClassFuncs[j].BodyCodes.TraversalWraps ((_deep: 1, _group: 0, _loop: 0, _cb: ExprTraversals.Traversal));
9494
Info.InitFunc (ClassFuncs[j]);
9595
ClassFuncs[j].BodyCodes.TraversalWraps ((_deep: 1, _group: 0, _loop: 1, _cb: ExprTraversals.Traversal));
96+
ClassFuncs[j].BodyCodes.TraversalCalcType ();
9697
Info.InitFunc (ClassFuncs[j]);
9798
ClassFuncs[j].BodyCodes.TraversalWraps ((_deep: 1, _group: 0, _loop: i, _cb: ExprTraversals.Traversal));
9899
}

fa/fac/ASTs/Structs/AstTemplateEnumInst.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public bool Compile () {
116116
ClassFuncs[j].BodyCodes.TraversalWraps ((_deep: 1, _group: 0, _loop: 0, _cb: ExprTraversals.Traversal));
117117
Info.InitFunc (ClassFuncs[j]);
118118
ClassFuncs[j].BodyCodes.TraversalWraps ((_deep: 1, _group: 0, _loop: 1, _cb: ExprTraversals.Traversal));
119+
ClassFuncs[j].BodyCodes.TraversalCalcType ();
119120
Info.InitFunc (ClassFuncs[j]);
120121
ClassFuncs[j].BodyCodes.TraversalWraps ((_deep: 1, _group: 0, _loop: i, _cb: ExprTraversals.Traversal));
121122
}

0 commit comments

Comments
 (0)