├── .gitignore ├── LICENSE ├── README.md ├── TKT.CLRTest ├── EntryMain.cs ├── Properties │ └── AssemblyInfo.cs ├── S1 │ └── TestBuYu.cs ├── TKT.CLRTest.csproj └── clr1 │ ├── EmitCreateMembers.cs.TXT │ ├── Program.cs │ ├── Program2.cs │ ├── Program3.cs │ ├── Program4.cs │ ├── Program5_Func.cs │ ├── Program6_Generic.cs │ ├── Program7.cs │ ├── Program8_Exception.cs │ ├── TEST_Calculater.cs │ ├── TEST_定时器.cs │ ├── TEST绘图窗体.cs │ └── TestUser32.cs ├── ZCompileCore 0.3.sln ├── ZCompileCore ├── AST │ ├── ASTree.cs │ ├── Exps │ │ ├── AnalyedCallExp.cs │ │ ├── AnalyedExp.cs │ │ ├── BinExp.cs │ │ ├── BinExpUtil.cs │ │ ├── BracketExp.cs │ │ ├── Eds │ │ │ ├── ClosureExp.cs │ │ │ ├── DeExp.cs │ │ │ ├── DiExp.cs │ │ │ ├── DirectMemberExp.cs │ │ │ ├── EveryOneExp.cs │ │ │ ├── InvokeEveryoneBaseExp.cs │ │ │ ├── InvokeEveryoneObejctExp.cs │ │ │ ├── InvokeEveryoneSubejctExp.cs │ │ │ ├── InvokeExp.cs │ │ │ ├── InvokeSimplestExp.cs │ │ │ ├── NewExp.cs │ │ │ ├── NewLambdaExp.cs │ │ │ ├── TypeExp.cs │ │ │ └── VarExp.cs │ │ ├── Exp.cs │ │ ├── FCallExp.cs │ │ ├── FTextExp.cs │ │ ├── IGenerateSet.cs │ │ ├── LiteralExp.cs │ │ ├── NameValueExp.cs │ │ └── UnaryExp.cs │ ├── FileAST.cs │ ├── FileElementAST.cs │ ├── Parts │ │ ├── AgreementAST.cs │ │ ├── Fns │ │ │ ├── FnArg.cs │ │ │ ├── FnBracket.cs │ │ │ ├── FnName.cs │ │ │ └── FnText.cs │ │ ├── ImportPackageAST.cs │ │ ├── MethodAST.cs │ │ ├── PackageAST.cs │ │ ├── PartAST.cs │ │ ├── PropertyAST.cs │ │ └── SimpleUseAST.cs │ ├── Stmts │ │ ├── AssignStmt.cs │ │ ├── BlockStmt.cs │ │ ├── CallStmt.cs │ │ ├── CatchStmt.cs │ │ ├── EveryOneAssignStmt.cs │ │ ├── ForeachStmt.cs │ │ ├── IfStmt.cs │ │ ├── Stmt.cs │ │ ├── TryStmt.cs │ │ └── WhileStmt.cs │ └── Types │ │ ├── ClassAST.cs │ │ ├── EnumAST.cs │ │ ├── NormalClassAST.cs │ │ └── StaticClassAST.cs ├── Analyers │ ├── ImportAnalyer.cs │ ├── PropertyAnalyer.cs │ └── TypeNameAnalyer.cs ├── Builders │ ├── CompileUtil.cs │ ├── FileCompiler.cs │ ├── ZCompileBuilder.cs │ ├── ZCompileClassModel.cs │ └── ZCompileProjectModel.cs ├── CompileConstant.cs ├── Contexts │ ├── AContexts │ │ ├── AnalyExpContext.cs │ │ └── AnalyStmtContext.cs │ ├── ClassContext.cs │ ├── EContexts │ │ ├── EmitClassContext.cs │ │ ├── EmitConstructorContext.cs │ │ ├── EmitExpContext.cs │ │ ├── EmitMethodContext.cs │ │ ├── EmitProjectContext.cs │ │ └── EmitStmtContext.cs │ ├── ImportContext.cs │ ├── MethodContext.cs │ └── ProjectContext.cs ├── Lex │ ├── CodePostion.cs │ ├── SourceReader.cs │ ├── Token.cs │ ├── TokenKind.cs │ └── Tokenizer.cs ├── Loads │ ├── ExternalGcl.cs │ ├── GclUtil.cs │ ├── IGcl.cs │ ├── MappingGcl.cs │ └── TktGcl.cs ├── Parse │ ├── IdentParser.cs │ ├── Parser_exp.cs │ ├── Parser_part.cs │ ├── Parser_stmt.cs │ └── Parser_tool.cs ├── Properties │ └── AssemblyInfo.cs ├── Reports │ ├── CompileException.cs │ ├── Messager.cs │ └── ProjectCompileResult.cs ├── Symbols │ ├── Defs │ │ ├── SymbolArg.cs │ │ ├── SymbolDefClass.cs │ │ ├── SymbolDefField.cs │ │ ├── SymbolDefProperty.cs │ │ └── SymbolVar.cs │ ├── Imports │ │ ├── SymbolEnumItem.cs │ │ ├── SymbolFieldDirect.cs │ │ └── SymbolPropertyDirect.cs │ ├── InstanceSymbol.cs │ ├── SymbolInfo.cs │ └── SymbolTable.cs ├── Tools │ ├── ChineseHelper.cs │ ├── ClassContextHelper.cs │ ├── EmitHelper.cs │ ├── ListHelper.cs │ ├── TextUtil.cs │ └── TxtFileEncoding.cs ├── ZCompileCore.csproj └── ZLangCompiler.cs ├── ZLangRT ├── Attributes │ ├── ZClassAttribute.cs │ ├── ZCodeAttribute.cs │ └── ZMappingAttribute.cs ├── Collections │ ├── PairDict.cs │ └── TKTProcDescDictionary.cs ├── Descs │ ├── CnEnDict.cs │ ├── ExFieldInfo.cs │ ├── ExMethodInfo.cs │ ├── ExPropertyInfo.cs │ ├── TKTConstructorDesc.cs │ ├── TKTProcArg.cs │ ├── TKTProcBracket.cs │ └── TKTProcDesc.cs ├── Loads │ ├── ExternalGcl.cs │ ├── GclHelper.cs │ ├── IGcl.cs │ ├── MappingGcl.cs │ └── TktGcl.cs ├── Properties │ └── AssemblyInfo.cs ├── RTException.cs ├── TKTEnumMap.cs.TXT ├── TKTLambda.cs ├── TKTRoot.cs.TXT ├── Utils │ ├── Calculater.cs │ ├── DebugUtil.cs │ ├── GenericUtil.cs │ ├── Invoker.cs │ ├── ProcDescCodeParser.cs │ ├── ProcDescHelper.cs │ └── ReflectionUtil.cs └── ZLangRT.csproj ├── ZLangSingleCompiler ├── App.config ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── ZLangSingleCompiler.csproj ├── ZSingleCompiler.cs ├── sample │ ├── test.zyy │ ├── 你好.zyy │ ├── 列表例子.zyy │ ├── 对应表例子.zyy │ ├── 循环语句例子.zyy │ ├── 教程例子 │ │ ├── 异常例子.zyy │ │ ├── 循环语句例子.zyy │ │ ├── 打印你好.zyy │ │ ├── 条件语句例子.zyy │ │ ├── 游戏状态.zyy │ │ └── 过程例子.zyy │ ├── 测试如果.zyy │ └── 测试每一个.zyy └── 项目说明.txt ├── Z语言接口编写文档 0.3.doc ├── Z语言教程.chm └── Z语言系统 ├── Properties └── AssemblyInfo.cs ├── VOID.cs ├── Z语言系统.csproj ├── 事物.cs ├── 列表.cs ├── 判断符.cs ├── 可运行条件.cs ├── 可运行语句.cs ├── 对应表.cs ├── 常用数学函数.cs ├── 常用辅助.cs ├── 异常.cs ├── 控制台.cs ├── 整数.cs ├── 文本.cs ├── 时间日期.cs ├── 时间日期辅助.cs ├── 浮点数.cs ├── 类型辅助.cs ├── 补语控制.cs └── 随机数器.cs /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Z语言QQ群:362762284 3 | 4 | # Z语言网址: 5 | 6 | www.zyuyan.org 7 | -------------------------------------------------------------------------------- /TKT.CLRTest/EntryMain.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows.Forms; 7 | using TKT.CLRTest.S1; 8 | 9 | namespace TKT.CLRTest 10 | { 11 | public class EntryMain 12 | { 13 | public static void Main() 14 | { 15 | //TKT.CLRTest.CS3.列表例子.TestEveryOne(); 16 | //Console.ReadKey(); 17 | /*Application.EnableVisualStyles(); 18 | Application.SetCompatibleTextRenderingDefault(false); 19 | Application.Run(new TestTurlet1());*/ 20 | 21 | TestBuYu.Out(5); 22 | Console.ReadKey(); 23 | } 24 | 25 | public static float ToF(int i) 26 | { 27 | return i; 28 | } 29 | 30 | public static float ToF2(int i) 31 | { 32 | float x = i; 33 | return x; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /TKT.CLRTest/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的常规信息通过以下 6 | // 特性集控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("TKT.CLRTest")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("TKT.CLRTest")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 使此程序集中的类型 18 | // 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 19 | // 则将该类型上的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("4b4c89bf-042a-49de-886c-cdf0c6165a71")] 24 | 25 | // 程序集的版本信息由下面四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /TKT.CLRTest/S1/TestBuYu.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Security.Cryptography.X509Certificates; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using Z语言系统; 8 | 9 | namespace TKT.CLRTest.S1 10 | { 11 | class TestBuYu 12 | { 13 | public static void Out(int y) 14 | { 15 | int x = y; 16 | 补语控制.执行_次(() => { Console.Write(" 你好 "+x);},3); 17 | } 18 | 19 | [STAThread] 20 | public static void DY() 21 | { 22 | int num = 100; 23 | 控制台.打印(num); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /TKT.CLRTest/TKT.CLRTest.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {8790C2A5-6055-40BC-AC24-913CD47135F3} 8 | Exe 9 | Properties 10 | TKT.CLRTest 11 | TKT.CLRTest 12 | v4.5 13 | 512 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | {c7d13a96-ef60-4325-93b3-42e27575d53b} 55 | ZLangRT 56 | 57 | 58 | {8e893d3f-cf9c-4b45-b28c-06ca6e0436be} 59 | Z语言系统 60 | 61 | 62 | 63 | 64 | 71 | -------------------------------------------------------------------------------- /TKT.CLRTest/clr1/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using TKT.CLRTest.Lei; 7 | using TKT.RT; 8 | using TKT系统; 9 | 10 | namespace TKT.CLRTest.clr1 11 | { 12 | class Program 13 | { 14 | static void Main2(string[] args) 15 | { 16 | //testPrint("AAAA"); 17 | //Console.ReadKey(); 18 | /*文本 wb = new 文本("123456"); 19 | 控制台.打印(wb);*/ 20 | List list = new List(); 21 | list.Add(1); list.Add(2); list.Add(3); list.Add(4); 22 | List list2 = new List(); 23 | list2.Add(10); list2.Add(20); list2.Add(30); list2.Add(40); 24 | list.RemoveAt(1); 25 | list.InsertRange(1, list2); 26 | string str = string.Join(",",list); 27 | Console.WriteLine(str); 28 | Console.ReadKey(); 29 | } 30 | 31 | void testDecimal() 32 | { 33 | //decimal a = 1.23M; 34 | } 35 | 36 | int testIfElse(int i) 37 | { 38 | if(i==1) 39 | { 40 | return 10; 41 | } 42 | else if (i == 2) 43 | { 44 | return 20; 45 | } 46 | else if (i == 3) 47 | { 48 | return 30; 49 | } 50 | else if (i == 4) 51 | { 52 | return 40; 53 | } 54 | else if (i == 5) 55 | { 56 | return 50; 57 | } 58 | return -1; 59 | } 60 | /* 61 | static void testPrint(string str) 62 | { 63 | 文本 wb = new 文本(str); 64 | 控制台.打印(wb); 65 | } 66 | 67 | static void testRead() 68 | { 69 | 文本 T = 控制台.读取(); 70 | 控制台.打印(T); 71 | }*/ 72 | 73 | public string testGetProperty(TesClassA t1) 74 | { 75 | return t1.Name; 76 | } 77 | 78 | public void setSetProperty(TesClassA t1,string value) 79 | { 80 | t1.Value = value; 81 | } 82 | /* 83 | void testsub() 84 | { 85 | 整数 I = new 整数(9); 86 | I = Calculater.SUB(I, new 整数(8888)); 87 | }*/ 88 | 89 | void testCall() 90 | { 91 | TesClassA t1 = new TesClassA(); 92 | t1.Name = "testCall"; 93 | t1.Out("很好","结束"); 94 | } 95 | 96 | void testCallStatic() 97 | { 98 | TesClassA.Wr("aaa","bbb"); 99 | } 100 | } 101 | 102 | 103 | } 104 | -------------------------------------------------------------------------------- /TKT.CLRTest/clr1/Program3.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using TKT.CLRTest.Lei; 8 | using TKT.RT; 9 | using TKT系统; 10 | 11 | namespace TKT.CLRTest.clr1 12 | { 13 | class Program3 14 | { 15 | static void Main4(string[] args) 16 | { 17 | TesClassA t1 = new TesClassA(); 18 | t1.Name = "aabb"; 19 | t1.Out("1000", "1001"); 20 | } 21 | 22 | static void test() 23 | { 24 | var A = 求和(100, 50); 25 | Console.WriteLine(A); 26 | Console.ReadKey(); 27 | } 28 | 29 | static int 求和(int a,int b) 30 | { 31 | var 结果 = a + b; 32 | return 结果; 33 | } 34 | 35 | static void Test_打印() 36 | { 37 | int a = 100; 38 | 控制台.打印(a); 39 | float b = 1.002f; 40 | 控制台.打印(b); 41 | } 42 | 43 | static void Test_TEnum() 44 | { 45 | Console.WriteLine(TEnum.MUL); 46 | } 47 | 48 | static List testList() 49 | { 50 | List list = new List(); 51 | list.Add(16); 52 | list.Add(32); 53 | return list; 54 | } 55 | 56 | static string getIndex(List list,Dictionary dict) 57 | { 58 | list[5] = 100; 59 | string str = list[4] + dict["aaa"]; 60 | return str; 61 | } 62 | 63 | enum TEnum 64 | { 65 | ADD, 66 | SUB, 67 | MUL 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /TKT.CLRTest/clr1/Program4.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using TKT.RT; 8 | using TKT.RT.Descs; 9 | using TKT系统; 10 | 11 | namespace TKT.CLRTest.clr1 12 | { 13 | class Program4 14 | { 15 | public static int 次数 { get; set; } 16 | 17 | static Program4() 18 | { 19 | 次数 = 0; 20 | } 21 | 22 | static void Main6(string[] args) 23 | { 24 | Print(次数); 25 | Console.WriteLine(typeof(List).Name); 26 | Console.ReadKey(); 27 | } 28 | 29 | static void Print(object obj) 30 | { 31 | Console.WriteLine(obj); 32 | } 33 | 34 | static TKTProcArg TestProcArg() 35 | { 36 | TKTProcArg arg = new TKTProcArg ("GET",typeof(string),true); 37 | TKTProcArg arg2 = new TKTProcArg("GET", typeof(string), false); 38 | return arg; 39 | } 40 | 41 | static T getT(K i,T t) 42 | { 43 | if (i is string) return t; 44 | else return default(T); 45 | } 46 | 47 | static void getT_Test() 48 | { 49 | string k = getT(100,"AASS"); 50 | Console.WriteLine(k); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /TKT.CLRTest/clr1/Program5_Func.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace TKT.CLRTest.clr1 9 | { 10 | class Program5_Func 11 | { 12 | void RunTimes(Action fn, int times) 13 | { 14 | for (int i = 0; i < times; i++) 15 | { 16 | fn(); 17 | } 18 | } 19 | 20 | void RunTimes_test_none() 21 | { 22 | Action fn1 = () => { Console.WriteLine("AAAA"); }; 23 | RunTimes(fn1,5); 24 | } 25 | 26 | string name="NAME"; 27 | 28 | void RunTimes_test_field() 29 | { 30 | Action fn1 = () => { Console.WriteLine(name); }; 31 | RunTimes(fn1,5); 32 | } 33 | 34 | void RunTimes_test_local() 35 | { 36 | string a= name+"_AAA"; 37 | Action fn1 = () => { Console.WriteLine(a); }; 38 | RunTimes(fn1,5); 39 | } 40 | 41 | void RunTimes_test_arg(int i) 42 | { 43 | Action fn1 = () => { Console.WriteLine("bbbb_"+i); }; 44 | RunTimes(fn1,5); 45 | } 46 | 47 | void RunTimes_test_Method() 48 | { 49 | string a = name + "_AAA"; 50 | Action fn1 = () => { Print(a); }; 51 | RunTimes(fn1, 5); 52 | } 53 | 54 | void Print(string str) 55 | { 56 | Console.WriteLine(str); 57 | } 58 | 59 | static void SPrint(string str) 60 | { 61 | Console.WriteLine(str); 62 | } 63 | 64 | void SRunTimes_test_Method() 65 | { 66 | string a = name + "_AAA"; 67 | Action fn1 = () => { SPrint(a); }; 68 | RunTimes(fn1, 5); 69 | } 70 | /* 71 | delegate int Calculate(int a, int b); 72 | void test_delegate(string[] args) 73 | { 74 | int a = 3; 75 | int b = 4; 76 | Calculate result = delegate(int ta, int tb) { return ta + tb; }; 77 | 78 | Console.WriteLine(result(a,b)); 79 | Console.Read(); 80 | }*/ 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /TKT.CLRTest/clr1/Program6_Generic.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using TKT.RT; 8 | using TKT系统; 9 | 10 | namespace TKT.CLRTest.clr1 11 | { 12 | class Program6_Generic 13 | { 14 | T get_T(object t) 15 | { 16 | return (T)(t); 17 | } 18 | 19 | void test_T() 20 | { 21 | string x = get_T("AAAAAA"); 22 | //Console.WriteLine(x); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /TKT.CLRTest/clr1/Program7.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using TKT.RT; 8 | using TKT系统; 9 | 10 | namespace TKT.CLRTest.clr1 11 | { 12 | class Program7 13 | { 14 | public static void Test() 15 | { 16 | 列表 list = new 列表(); 17 | list.Add(100); 18 | list.Add(200); 19 | list.Add(300); 20 | 控制台.打印(list[1]); 21 | Console.ReadKey(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /TKT.CLRTest/clr1/Program8_Exception.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using TKT.RT; 8 | using TKT系统; 9 | 10 | namespace TKT.CLRTest.clr1 11 | { 12 | class Program8_Exception 13 | { 14 | public static void Test() 15 | { 16 | try 17 | { 18 | int x = 控制台.读取整数(); 19 | 控制台.打印(x); 20 | } 21 | catch(Exception ex) 22 | { 23 | Console.WriteLine(ex.Message); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /TKT.CLRTest/clr1/TEST_Calculater.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using System.Windows.Forms; 8 | using TKT.RT; 9 | using TKT.RT.Utils; 10 | using TKT绘图; 11 | using TKT系统; 12 | 13 | namespace TKT.CLRTest.clr1 14 | { 15 | class TEST_Calculater 16 | { 17 | static int 计数器 { get; set; } 18 | 19 | static TEST_Calculater() 20 | { 21 | 计数器 = 0; 22 | } 23 | 24 | static void TEST() 25 | { 26 | Console.WriteLine(Calculater.GE(计数器,0)); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /TKT.CLRTest/clr1/TEST_定时器.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using System.Windows.Forms; 8 | 9 | namespace TKT.CLRTest.clr1 10 | { 11 | class TEST_定时器 12 | { 13 | static int i = 0; 14 | 15 | static void Main2(string[] args) 16 | { 17 | test_定时器(); 18 | 19 | Console.ReadKey(); 20 | } 21 | 22 | static void test_定时器() 23 | { 24 | 定时器 T = new 定时器(1000); 25 | T.运行内容 = () => { Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); i++; }; 26 | T.停止条件 = () => { return i >= 5; }; 27 | T.启动(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /TKT.CLRTest/clr1/TEST绘图窗体.cs: -------------------------------------------------------------------------------- 1 |  2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace TKT.CLRTest.clr1 9 | { 10 | public class TEST绘图窗体 11 | { 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /TKT.CLRTest/clr1/TestUser32.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.InteropServices; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using TKT操作系统; 8 | 9 | namespace TKT.CLRTest.clr1 10 | { 11 | class TestUser32 12 | { 13 | [DllImport("User32.dll ")] 14 | public static extern IntPtr FindWindow(string ClassName, string CaptionName); 15 | [DllImport("User32.dll ")] 16 | public static extern int SendMessage(IntPtr hwad, int wMsg, int lParam, int wParam); 17 | 18 | [DllImport("user32.dll")] 19 | public static extern IntPtr SetFocus(IntPtr hwnd2); 20 | 21 | [DllImport("user32.dll")] 22 | public static extern IntPtr FindWindowEx(IntPtr parenthW, IntPtr child, string s1, string s2); 23 | 24 | public const int WM_SETTEXT = 0x000C; 25 | public const int WM_CHAR = 0x0102; 26 | 27 | public static void RunNotePad() 28 | { 29 | var process = 进程辅助.启动程序("C:\\Windows\\system32\\notepad.exe"); 30 | string className = "Notepad"; 31 | string captionName = "无标题 - 记事本"; 32 | IntPtr hwnd = FindWindow(className, captionName);//找主窗口. 33 | IntPtr hwnd2 = FindWindowEx(hwnd, IntPtr.Zero, "Edit", ""); // 找子窗体 34 | //SendMessage(hwnd22,256,97,0); 35 | SendMessage(hwnd2, WM_CHAR, (int)'h', 0); 36 | SendMessage(hwnd2, WM_CHAR, (int)'e', 0); 37 | SendMessage(hwnd2, WM_CHAR, (int)'l', 0); 38 | SendMessage(hwnd2, WM_CHAR, (int)'l', 0); 39 | SendMessage(hwnd2, WM_CHAR, (int)'o', 0); 40 | Console.ReadLine(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /ZCompileCore 0.3.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.40629.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZCompileCore", "ZCompileCore\ZCompileCore.csproj", "{4C7E5206-D464-4415-B599-5CCB4BDC2041}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TKT.CLRTest", "TKT.CLRTest\TKT.CLRTest.csproj", "{8790C2A5-6055-40BC-AC24-913CD47135F3}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Z语言系统", "Z语言系统\Z语言系统.csproj", "{8E893D3F-CF9C-4B45-B28C-06CA6E0436BE}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZLangRT", "ZLangRT\ZLangRT.csproj", "{C7D13A96-EF60-4325-93B3-42E27575D53B}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZLangSingleCompiler", "ZLangSingleCompiler\ZLangSingleCompiler.csproj", "{4E3CA102-3EA9-455B-B316-8EE86E1D8868}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {4C7E5206-D464-4415-B599-5CCB4BDC2041}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {4C7E5206-D464-4415-B599-5CCB4BDC2041}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {4C7E5206-D464-4415-B599-5CCB4BDC2041}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {4C7E5206-D464-4415-B599-5CCB4BDC2041}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {8790C2A5-6055-40BC-AC24-913CD47135F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {8790C2A5-6055-40BC-AC24-913CD47135F3}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {8790C2A5-6055-40BC-AC24-913CD47135F3}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {8790C2A5-6055-40BC-AC24-913CD47135F3}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {8E893D3F-CF9C-4B45-B28C-06CA6E0436BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {8E893D3F-CF9C-4B45-B28C-06CA6E0436BE}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {8E893D3F-CF9C-4B45-B28C-06CA6E0436BE}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {8E893D3F-CF9C-4B45-B28C-06CA6E0436BE}.Release|Any CPU.Build.0 = Release|Any CPU 34 | {C7D13A96-EF60-4325-93B3-42E27575D53B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {C7D13A96-EF60-4325-93B3-42E27575D53B}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {C7D13A96-EF60-4325-93B3-42E27575D53B}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {C7D13A96-EF60-4325-93B3-42E27575D53B}.Release|Any CPU.Build.0 = Release|Any CPU 38 | {4E3CA102-3EA9-455B-B316-8EE86E1D8868}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 39 | {4E3CA102-3EA9-455B-B316-8EE86E1D8868}.Debug|Any CPU.Build.0 = Debug|Any CPU 40 | {4E3CA102-3EA9-455B-B316-8EE86E1D8868}.Release|Any CPU.ActiveCfg = Release|Any CPU 41 | {4E3CA102-3EA9-455B-B316-8EE86E1D8868}.Release|Any CPU.Build.0 = Release|Any CPU 42 | EndGlobalSection 43 | GlobalSection(SolutionProperties) = preSolution 44 | HideSolutionNode = FALSE 45 | EndGlobalSection 46 | EndGlobal 47 | -------------------------------------------------------------------------------- /ZCompileCore/AST/ASTree.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZCompileCore.Lex; 7 | using ZCompileCore.Reports; 8 | using ZCompileCore.Analys; 9 | 10 | namespace ZCompileCore.AST 11 | { 12 | public abstract class ASTree 13 | { 14 | public int Deep; 15 | public abstract string ToCode(); 16 | 17 | public override string ToString() 18 | { 19 | return ToCode(); 20 | } 21 | 22 | protected void error(CodePostion postion, string msg) 23 | { 24 | Messager.Error(postion.Line, postion.Col, /*this.GetType().Name + ":" + */ msg); 25 | TrueAnalyed = false; 26 | } 27 | 28 | public ASTree() 29 | { 30 | TrueAnalyed = true; 31 | } 32 | 33 | public bool TrueAnalyed { get; set; } 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/AnalyedCallExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZCompileCore.Lex; 7 | 8 | namespace ZCompileCore.AST.Exps 9 | { 10 | public abstract class AnalyedCallExp : AnalyedExp 11 | { 12 | public AnalyedCallExp(Exp srcExp):base(srcExp) 13 | { 14 | 15 | } 16 | public List Elements { get; set; } 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/AnalyedExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZCompileCore.Lex; 7 | 8 | namespace ZCompileCore.AST.Exps 9 | { 10 | public abstract class AnalyedExp:Exp 11 | { 12 | protected Exp SrcExp; 13 | public AnalyedExp(Exp srcExp) 14 | { 15 | SrcExp = srcExp; 16 | } 17 | 18 | public override string ToCode() 19 | { 20 | return SrcExp.ToCode(); 21 | } 22 | 23 | public override CodePostion Postion 24 | { 25 | get 26 | { 27 | return SrcExp.Postion; 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/BinExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Analys.AContexts; 9 | using ZCompileCore.Analys.EContexts; 10 | using ZCompileCore.Lex; 11 | using ZCompileCore.Symbols; 12 | using ZCompileCore.Tools; 13 | using ZCompileCore.Analys; 14 | using ZCompileCore.AST.Exps.Eds; 15 | using ZCompileCore.Reports; 16 | using ZLangRT; 17 | using ZLangRT.Utils; 18 | 19 | namespace ZCompileCore.AST.Exps 20 | { 21 | public class BinExp:Exp 22 | { 23 | public Token OpToken { get; set; } 24 | public Exp LeftExp { get; set; } 25 | public Exp RightExp { get; set; } 26 | private MethodInfo OpMethod { get; set; } 27 | TokenKind OpKind; 28 | 29 | public BinExp() 30 | { 31 | 32 | } 33 | 34 | public override Exp Analy(AnalyExpContext context) 35 | { 36 | base.Analy(context); 37 | var symbols = this.AnalyExpContext.Symbols; 38 | OpKind = OpToken.Kind; 39 | 40 | if (RightExp==null) 41 | { 42 | errorf(OpToken.Postion, "运算符'{0}'右边缺少运算元素", OpToken.GetText()); 43 | return null; 44 | } 45 | else if (LeftExp == null && RightExp != null) 46 | { 47 | UnaryExp unexp = new UnaryExp(OpToken, RightExp); 48 | var exp = unexp.Analy(context); 49 | return exp; 50 | } 51 | 52 | LeftExp = LeftExp.Analy(context);//LeftExp = AnalyExp(LeftExp); 53 | RightExp = RightExp.Analy(context);//RightExp = AnalyExp(RightExp); 54 | 55 | if (LeftExp==null || RightExp==null) 56 | { 57 | TrueAnalyed = false; 58 | return null; 59 | } 60 | 61 | Type ltype = LeftExp.RetType; 62 | Type rtype = RightExp.RetType; 63 | 64 | OpMethod = BinExpUtil.GetCalcMethod(OpKind, ltype, rtype); 65 | if (OpMethod != null) 66 | { 67 | RetType = OpMethod.ReturnType; 68 | } 69 | 70 | if(RetType==null) 71 | { 72 | error("两种类型无法进行'"+OpToken.ToCode() +"'运算"); 73 | } 74 | return this; 75 | } 76 | 77 | public override void Generate(EmitExpContext context) 78 | { 79 | ILGenerator il = context.ILout; 80 | base.GenerateArgsExp(context, OpMethod.GetParameters(), new Exp[] { LeftExp, RightExp }); 81 | EmitHelper.CallDynamic(il, OpMethod, true); 82 | base.GenerateConv(context); 83 | } 84 | public override string ToCode() 85 | { 86 | StringBuilder buf = new StringBuilder(); 87 | buf.Append(LeftExp != null ? LeftExp.ToString() : ""); 88 | buf.Append(OpToken != null ? OpToken.ToCode() : ""); 89 | buf.Append(RightExp != null ? RightExp.ToString() : ""); 90 | return buf.ToString(); 91 | } 92 | 93 | public override CodePostion Postion 94 | { 95 | get 96 | { 97 | return LeftExp.Postion; ; 98 | } 99 | } 100 | 101 | public override void GetNestedFields(Dictionary nestedField) 102 | { 103 | LeftExp.GetNestedFields(nestedField); 104 | RightExp.GetNestedFields(nestedField); 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/Eds/DiExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Analys.AContexts; 9 | using ZCompileCore.Analys.EContexts; 10 | using ZCompileCore.Loads; 11 | using ZCompileCore.Symbols; 12 | using ZCompileCore.Tools; 13 | using ZCompileCore.Analys; 14 | using ZCompileCore.AST.Exps.Eds; 15 | using ZCompileCore.Lex; 16 | using ZLangRT; 17 | using ZLangRT.Descs; 18 | 19 | namespace ZCompileCore.AST.Exps 20 | { 21 | public class DiExp : AnalyedCallExp , IGenerateSet 22 | { 23 | public DiExp(Exp srcExp) 24 | : base(srcExp) 25 | { 26 | 27 | } 28 | public Exp SubjectExp { get; set; } 29 | public Exp ArgExp { get; set; } 30 | public ExPropertyInfo ExProperty; 31 | 32 | public override Exp Analy(AnalyExpContext context) 33 | { 34 | base.Analy(context); 35 | var symbols = this.AnalyExpContext.Symbols; 36 | SubjectExp = SubjectExp.Analy(context);// AnalyExp(SubjectExp); 37 | ArgExp = ArgExp.Analy(context);//AnalyExp(ArgExp); 38 | var propertyName = "Item"; 39 | var subjType = SubjectExp.RetType; 40 | ExProperty = GclUtil.SearchExProperty(propertyName, subjType); //subjType.GetExProperty(propertyName); 41 | 42 | if (ExProperty == null) 43 | { 44 | error(SubjectExp.Postion, "不存在索引"); 45 | return null; 46 | } 47 | else 48 | { 49 | RetType = ExProperty.Property.PropertyType; 50 | } 51 | return this; 52 | } 53 | 54 | public override void Generate(EmitExpContext context) 55 | { 56 | GenerateGet(context); 57 | base.GenerateConv(context); 58 | } 59 | 60 | public void GenerateGet(EmitExpContext context) 61 | { 62 | ILGenerator il = context.ILout; 63 | MethodInfo getMethod = ExProperty.Property.GetGetMethod(); 64 | SubjectExp.Generate(context); 65 | ArgExp.RequireType = getMethod.ReturnType; 66 | ArgExp.Generate(context); 67 | EmitHelper.CallDynamic(il, getMethod, ExProperty.IsSelf); 68 | } 69 | 70 | public void GenerateSet(EmitExpContext context,Exp valueExpr) 71 | { 72 | ILGenerator il = context.ILout; 73 | MethodInfo setMethod = ExProperty.Property.GetSetMethod(); 74 | SubjectExp.Generate(context); 75 | ArgExp.RequireType = setMethod.GetParameters()[0].ParameterType; 76 | ArgExp.Generate(context); 77 | //EmitHelper.Box(il, ArgExp.RetType, setMethod.GetParameters()[0].ParameterType); 78 | valueExpr.RequireType = setMethod.GetParameters()[1].ParameterType; 79 | valueExpr.Generate(context); 80 | EmitHelper.CallDynamic(il, setMethod, ExProperty.IsSelf); 81 | } 82 | 83 | public override void GetNestedFields(Dictionary nestedField) 84 | { 85 | SubjectExp.GetNestedFields(nestedField); 86 | } 87 | 88 | public bool CanWrite 89 | { 90 | get 91 | { 92 | return ExProperty.Property.CanWrite; 93 | } 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/Eds/DirectMemberExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection.Emit; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Analys.AContexts; 8 | using ZCompileCore.Analys.EContexts; 9 | using ZCompileCore.Lex; 10 | using ZCompileCore.Reports; 11 | using ZCompileCore.Symbols; 12 | using ZCompileCore.Symbols.Imports; 13 | using ZCompileCore.Tools; 14 | 15 | namespace ZCompileCore.AST.Exps.Eds 16 | { 17 | public class DirectMemberExp : AnalyedExp, IGenerateSet 18 | { 19 | Token MemberToken { get; set; } 20 | SymbolInfo memberSymbol { get; set; } 21 | 22 | public DirectMemberExp(Exp srcExp, Token varToken, SymbolInfo mebmerSymbol, bool isAssignedBy, bool isAssignedValue) 23 | : base(srcExp) 24 | { 25 | MemberToken = varToken; 26 | memberSymbol = mebmerSymbol; 27 | this.IsAssignedBy = IsAssignedBy; 28 | this.IsAssignedValue = IsAssignedValue; 29 | } 30 | 31 | public override Exp Analy(AnalyExpContext context) 32 | { 33 | base.Analy(context); 34 | var symbols = this.AnalyExpContext.Symbols; 35 | RetType = (memberSymbol as InstanceSymbol).DimType; 36 | return this; 37 | } 38 | 39 | public override void Generate(EmitExpContext context) 40 | { 41 | ILGenerator il = context.ILout; 42 | if (!EmitHelper.EmitSymbolGet(il, (SymbolInfo)memberSymbol)) 43 | { 44 | throw new CompileException("DirectMemberExp生成失败"); 45 | } 46 | base.GenerateConv(context); 47 | } 48 | 49 | public override void GetNestedFields(Dictionary nestedField) 50 | { 51 | //nestedField.Add(memberSymbol.SymbolName, memberSymbol); 52 | } 53 | 54 | public void GenerateSet(EmitExpContext context, Exp valueExp) 55 | { 56 | ILGenerator il = context.ILout; 57 | var symbol = (memberSymbol as SymbolInfo); 58 | EmitHelper.EmitSet_Load(il, symbol); 59 | valueExp.Generate(context); 60 | var b= EmitHelper.EmitSet_Storm(il, symbol); 61 | if (!b) 62 | { 63 | throw new CompileException("DirectMemberExp不能GenerateSet"); 64 | } 65 | } 66 | 67 | public bool CanWrite 68 | { 69 | get 70 | { 71 | if (memberSymbol is SymbolPropertyDirect) 72 | { 73 | return (memberSymbol as SymbolPropertyDirect).CanWrite; 74 | } 75 | else if (memberSymbol is SymbolFieldDirect) 76 | { 77 | return (memberSymbol as SymbolFieldDirect).CanWrite; 78 | } 79 | else 80 | { 81 | return false; 82 | } 83 | } 84 | } 85 | 86 | #region 覆盖 87 | public override string ToCode() 88 | { 89 | return MemberToken.GetText(); 90 | } 91 | 92 | public override CodePostion Postion 93 | { 94 | get 95 | { 96 | return MemberToken.Postion; 97 | } 98 | } 99 | #endregion 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/Eds/EveryOneExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Analys.AContexts; 9 | using ZCompileCore.Analys.EContexts; 10 | using ZCompileCore.AST.Exps.Eds; 11 | using ZCompileCore.Lex; 12 | using ZCompileCore.Reports; 13 | using ZCompileCore.Symbols; 14 | using ZCompileCore.Symbols.Defs; 15 | using ZCompileCore.Analys; 16 | using ZCompileCore.Tools; 17 | using ZLangRT; 18 | using ZCompileCore.Loads; 19 | using ZLangRT.Utils; 20 | 21 | namespace ZCompileCore.AST.Exps 22 | { 23 | public class EveryOneExp : Exp , IGenerateSet 24 | { 25 | public Exp ListExp { get; set; } 26 | public Token MemberToken { get; set; } 27 | 28 | //string propertyName; 29 | //PropertyInfo ExProperty; 30 | 31 | public override Exp Analy(AnalyExpContext context) 32 | { 33 | base.Analy(context); 34 | //SubjectExp.LoadRefTypes(context); 35 | Type[] types = GenericUtil.GetInstanceGenriceType(ListExp.RetType, typeof(Z语言系统.列表<>)); 36 | this.RetType = types[0]; 37 | return this; 38 | } 39 | 40 | public override void GetNestedFields(Dictionary nestedField) 41 | { 42 | ListExp.GetNestedFields(nestedField); 43 | } 44 | 45 | public override void Generate(EmitExpContext context) 46 | { 47 | GenerateGet(context); 48 | //throw new CompileException("EveryOneExp不应该Generate"); 49 | } 50 | 51 | void GenenrateSubject(EmitExpContext context) 52 | { 53 | ILGenerator il = context.ILout; 54 | bool isgen = false; 55 | if ((ListExp is VarExp)) 56 | { 57 | VarExp varexp = ListExp as VarExp; 58 | if (ReflectionUtil.IsStruct(varexp.RetType)) 59 | { 60 | if (varexp.VarSymbol is SymbolVar) 61 | { 62 | il.Emit(OpCodes.Ldloca, (varexp.VarSymbol as SymbolVar).VarBuilder); 63 | isgen = true; 64 | } 65 | else if (varexp.VarSymbol is SymbolArg) 66 | { 67 | il.Emit(OpCodes.Ldarga, (varexp.VarSymbol as SymbolArg).ArgIndex); 68 | isgen = true; 69 | } 70 | } 71 | } 72 | if(!isgen) 73 | { 74 | ListExp.Generate(context); 75 | } 76 | } 77 | 78 | public void GenerateGet(EmitExpContext context) 79 | { 80 | GenenrateSubject(context); 81 | base.GenerateConv(context); 82 | } 83 | 84 | public void GenerateSet(EmitExpContext context,Exp valueExp) 85 | { 86 | throw new CompileException("EveryOneExp不应该GenerateSet"); 87 | } 88 | 89 | public bool CanWrite 90 | { 91 | get 92 | { 93 | return true; 94 | } 95 | } 96 | 97 | public override string ToCode() 98 | { 99 | StringBuilder buf = new StringBuilder(); 100 | buf.Append(ListExp != null ? ListExp.ToCode() : ""); 101 | buf.Append("的"); 102 | buf.Append(MemberToken != null ? MemberToken.GetText() : ""); 103 | return buf.ToString(); 104 | } 105 | 106 | public override CodePostion Postion 107 | { 108 | get 109 | { 110 | return ListExp.Postion; ; 111 | } 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/Eds/InvokeEveryoneObejctExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Reflection.Emit; 5 | using ZCompileCore.Analys.AContexts; 6 | using ZCompileCore.Analys.EContexts; 7 | using ZCompileCore.Loads; 8 | using ZCompileCore.Tools; 9 | using ZCompileCore.Analys; 10 | using ZCompileCore.Symbols; 11 | using ZCompileCore.Symbols.Defs; 12 | using ZLangRT.Descs; 13 | using ZLangRT.Utils; 14 | using Z语言系统; 15 | 16 | namespace ZCompileCore.AST.Exps.Eds 17 | { 18 | public class InvokeEveryoneObejctExp : InvokeEveryoneBaseExp 19 | { 20 | protected EveryOneExp everyoneExp; 21 | 22 | public InvokeEveryoneObejctExp(InvokeExp srcExp) 23 | : base(srcExp) 24 | { 25 | //invokeExp = srcExp; 26 | TKTProcArg arg = invokeExp.ExpProcDesc.GetArg(0); 27 | everyoneExp = (arg.Value as EveryOneExp); 28 | } 29 | 30 | public override Exp Analy(AnalyExpContext context) 31 | { 32 | base.Analy(context); 33 | int foreachIndex = context.StmtContext.MethodContext.CreateForeachIndex(); 34 | var symbols = context.Symbols; 35 | Exp listExp = everyoneExp.ListExp; 36 | //PropertyInfo countProperty = listExp.RetType.GetProperty("Count"); 37 | ExPropertyInfo countProperty = GclUtil.SearchExProperty("Count", listExp.RetType); 38 | 39 | var rgetCountMethod = countProperty.Property.GetGetMethod(); 40 | getCountMethod = new ExMethodInfo(rgetCountMethod, countProperty.IsSelf); 41 | //PropertyInfo itemProperty = listExp.RetType.GetProperty("Item"); 42 | ExPropertyInfo itemProperty = GclUtil.SearchExProperty("Item", listExp.RetType); 43 | 44 | diMethod = new ExMethodInfo(itemProperty.Property.GetGetMethod(), itemProperty.IsSelf); 45 | 46 | createForeachSymbols(context, symbols, foreachIndex, everyoneExp.ListExp.RetType); 47 | this.RetType = invokeExp.CallExMethod.Method.ReturnType; 48 | return this; 49 | } 50 | 51 | public override void Generate(EmitExpContext context) 52 | { 53 | generateEveryOne(context); 54 | base.GenerateConv(context); 55 | } 56 | 57 | void generateEveryOne(EmitExpContext context) 58 | { 59 | ILGenerator il = context.ILout; 60 | var True_Label = il.DefineLabel(); 61 | var False_Label = il.DefineLabel(); 62 | 63 | generateList(context, everyoneExp); 64 | generateCount(context); 65 | genInitIndex(context); 66 | 67 | generateCondition(context); 68 | il.Emit(OpCodes.Brfalse, False_Label); 69 | 70 | //定义一个标签,表示从下面开始进入循环体 71 | il.MarkLabel(True_Label); 72 | genBody(context); 73 | EmitHelper.Inc(il, indexSymbol.VarBuilder); 74 | generateCondition(context); 75 | il.Emit(OpCodes.Brtrue, True_Label); 76 | il.MarkLabel(False_Label); 77 | } 78 | 79 | void genBody(EmitExpContext context) 80 | { 81 | ILGenerator il = context.ILout; 82 | invokeExp.GenerateSubject(context); 83 | generateElement(context); 84 | invokeExp.GenerateFirstArgs(context);//invokeExp.GenerateArgILBox(context, 0); 85 | EmitHelper.CallDynamic(il, invokeExp.CallExMethod); 86 | } 87 | 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/Eds/InvokeSimplestExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Analys.AContexts; 9 | using ZCompileCore.Analys.EContexts; 10 | using ZCompileCore.Lex; 11 | using ZCompileCore.Symbols; 12 | using ZCompileCore.Tools; 13 | using ZCompileCore.Analys; 14 | using ZCompileCore.AST.Exps.Eds; 15 | using ZLangRT; 16 | using ZLangRT.Descs; 17 | 18 | namespace ZCompileCore.AST.Exps 19 | { 20 | public class InvokeSimplestExp : AnalyedExp 21 | { 22 | Token ProcToken; 23 | public TKTProcDesc ProcDesc { get; private set; } 24 | 25 | public InvokeSimplestExp(Exp srcExp,Token token, TKTProcDesc procdesc) 26 | : base(srcExp) 27 | { 28 | ProcToken = token; 29 | ProcDesc = procdesc; 30 | } 31 | 32 | public override Exp Analy(AnalyExpContext context) 33 | { 34 | base.Analy(context); 35 | RetType = ProcDesc.ExMethod.Method.ReturnType; 36 | return this; 37 | } 38 | 39 | public override void Generate(EmitExpContext context) 40 | { 41 | ILGenerator il = context.ILout; 42 | //var symbols = this.AnalyExpContext.Symbols; 43 | ClassContext classContext = this.AnalyExpContext.ClassContext; 44 | if (classContext.OutClassField == null) 45 | { 46 | if (!ProcDesc.ExMethod.Method.IsStatic) 47 | { 48 | il.Emit(OpCodes.Ldarg_0); 49 | } 50 | } 51 | else 52 | { 53 | if (!ProcDesc.ExMethod.Method.IsStatic) 54 | { 55 | il.Emit(OpCodes.Ldarg_0); 56 | EmitHelper.LoadField(il,classContext.OutClassField.GetField()); //il.Emit(OpCodes.Ldfld); 57 | } 58 | } 59 | 60 | EmitHelper.CallDynamic(il, ProcDesc.ExMethod); 61 | base.GenerateConv(context); 62 | } 63 | 64 | public override void GetNestedFields(Dictionary nestedField) 65 | { 66 | 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/Eds/NewLambdaExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Analys.AContexts; 9 | using ZCompileCore.Analys.EContexts; 10 | using ZCompileCore.Symbols; 11 | using ZCompileCore.Symbols.Defs; 12 | using ZCompileCore.Tools; 13 | using ZCompileCore.Analys; 14 | using ZCompileCore.AST.Exps.Eds; 15 | using ZLangRT; 16 | 17 | namespace ZCompileCore.AST.Exps 18 | { 19 | public class NewLambdaExp : AnalyedCallExp 20 | { 21 | ClosureExp lambdaExp; 22 | 23 | public NewLambdaExp(Exp srcExp, Exp actionExp, Type funcType) 24 | : base(srcExp) 25 | { 26 | var nestedFieldExps = new Dictionary(); 27 | actionExp.GetNestedFields(nestedFieldExps); 28 | 29 | lambdaExp = new ClosureExp(this); 30 | lambdaExp.FieldExps = nestedFieldExps; 31 | lambdaExp.BodyExp = actionExp; 32 | lambdaExp.FnRetType = funcType; 33 | if (funcType == TKTLambda.ActionType) 34 | { 35 | lambdaExp.RetType = typeof(void); 36 | } 37 | else if (funcType == TKTLambda.CondtionType) 38 | { 39 | lambdaExp.RetType = typeof(bool); 40 | } 41 | } 42 | 43 | public override Exp Analy(AnalyExpContext context) 44 | { 45 | base.Analy(context); 46 | lambdaExp.Analy(context); 47 | this.RetType = lambdaExp.FnRetType; 48 | return this; 49 | } 50 | 51 | public override void Generate(EmitExpContext context) 52 | { 53 | ILGenerator il = context.ILout; 54 | var symbols = this.AnalyExpContext.Symbols; 55 | lambdaExp.Generate(context); 56 | LocalBuilder lanmbdaLocalBuilder = il.DeclareLocal(lambdaExp.NestedClassContext.EmitContext.CurrentTypeBuilder); 57 | //il.Emit(OpCodes.Ldarg_0); 58 | il.Emit(OpCodes.Newobj, lambdaExp.NewBuilder); 59 | EmitHelper.StormVar(il, lanmbdaLocalBuilder); 60 | 61 | EmitOutClass(context, lanmbdaLocalBuilder); 62 | 63 | foreach (var field in lambdaExp.Fields.Keys) 64 | { 65 | EmitHelper.LoadVar(il, lanmbdaLocalBuilder); 66 | SymbolInfo inMethodSymbol = symbols.Get(field); 67 | EmitHelper.EmitSymbolGet(il, inMethodSymbol); 68 | il.Emit(OpCodes.Stfld, (lambdaExp.Fields[field] as SymbolDefField).GetField()); 69 | } 70 | EmitHelper.LoadVar(il, lanmbdaLocalBuilder); 71 | il.Emit(OpCodes.Ldftn, lambdaExp.NestedMethodContext.EmitContext.CurrentMethodBuilder); 72 | ConstructorInfo[] constructorInfos = lambdaExp.FnRetType.GetConstructors(); 73 | il.Emit(OpCodes.Newobj, constructorInfos[0]); 74 | base.GenerateConv(context); 75 | } 76 | 77 | void EmitOutClass(EmitExpContext context, LocalBuilder lanmbdaLocalBuilder) 78 | { 79 | ILGenerator il = context.ILout; 80 | EmitHelper.LoadVar(il, lanmbdaLocalBuilder); 81 | if (context.StmtContext.MethodEmitContext.CurrentMethodBuilder.IsStatic) 82 | { 83 | il.Emit(OpCodes.Ldnull); 84 | } 85 | else 86 | { 87 | il.Emit(OpCodes.Ldarg_0); 88 | } 89 | 90 | il.Emit(OpCodes.Stfld, (lambdaExp.OutClassField).GetField()); 91 | } 92 | 93 | 94 | 95 | public override void GetNestedFields(Dictionary nestedField) 96 | { 97 | 98 | } 99 | 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/Eds/TypeExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZCompileCore.Analys.AContexts; 7 | using ZCompileCore.Analys.EContexts; 8 | using ZCompileCore.Lex; 9 | using ZCompileCore.Symbols; 10 | using ZCompileCore.Symbols.Defs; 11 | 12 | namespace ZCompileCore.AST.Exps.Eds 13 | { 14 | public class TypeExp : AnalyedExp 15 | { 16 | Token TypeToken { get; set; } 17 | Type EType { get; set; } 18 | SymbolDefClass DType { get; set; } 19 | int type = 0; 20 | 21 | public TypeExp(Exp srcExp,Token typeToken, Type eType):base(srcExp) 22 | { 23 | TypeToken = typeToken; 24 | EType = eType; 25 | type = 1; 26 | } 27 | 28 | public TypeExp(Exp srcExp, Token typeToken, SymbolDefClass dType) 29 | : base(srcExp) 30 | { 31 | TypeToken = typeToken; 32 | DType = dType; 33 | type = 2; 34 | } 35 | 36 | public override Exp Analy(AnalyExpContext context) 37 | { 38 | if (type == 1) 39 | { 40 | RetType = EType; 41 | } 42 | else if(type==2) 43 | { 44 | RetType = DType.ClassBuilder; 45 | } 46 | return this; 47 | } 48 | 49 | public override void Generate(EmitExpContext context) 50 | { 51 | 52 | } 53 | 54 | public override void GetNestedFields(Dictionary nestedField) 55 | { 56 | 57 | } 58 | 59 | #region 覆盖 60 | 61 | public override string ToCode() 62 | { 63 | return TypeToken.GetText(); 64 | } 65 | 66 | public override CodePostion Postion 67 | { 68 | get 69 | { 70 | return TypeToken.Postion; 71 | } 72 | } 73 | #endregion 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/Exp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Analys.AContexts; 9 | using ZCompileCore.Analys.EContexts; 10 | using ZCompileCore.AST.Stmts; 11 | using ZCompileCore.Symbols; 12 | using ZCompileCore.Tools; 13 | using ZCompileCore.Analys; 14 | using ZCompileCore.AST.Exps.Eds; 15 | using ZLangRT.Descs; 16 | 17 | namespace ZCompileCore.AST.Exps 18 | { 19 | public abstract class Exp : FileElementAST 20 | { 21 | public Stmt Stmt { get; set; } 22 | public AnalyExpContext AnalyExpContext { get; set; } 23 | public EmitExpContext EmitExpContext { get; set; } 24 | 25 | public Type RetType { get; set; } 26 | 27 | public abstract void Generate(EmitExpContext context); 28 | 29 | /// 30 | /// 表达式是否是被赋值 31 | /// 32 | public bool IsAssignedBy { get; set; } 33 | /// 34 | /// 表达式是否是赋值表达式 35 | /// 36 | public bool IsAssignedValue { get; set; } 37 | 38 | public virtual Exp Analy(AnalyExpContext context) 39 | { 40 | this.AnalyExpContext = context; 41 | return this; 42 | } 43 | 44 | public abstract void GetNestedFields(Dictionary nestedField); 45 | 46 | /// 47 | /// 期望的类型 48 | /// 49 | public Type RequireType { get; set; } 50 | 51 | 52 | 53 | protected void GenerateConv(EmitExpContext context) 54 | { 55 | EmitHelper.EmitConv(context.ILout, RequireType, RetType); 56 | } 57 | 58 | protected void GenerateArgsExp(EmitExpContext context, ParameterInfo[] paramInfos, Exp[] args) 59 | { 60 | var size = paramInfos.Length; 61 | 62 | for (int i = 0; i < size; i++) 63 | { 64 | Exp argExp = args[i]; 65 | ParameterInfo parameter = paramInfos[i]; 66 | argExp.RequireType = parameter.ParameterType; 67 | argExp.Generate(context); 68 | } 69 | } 70 | 71 | protected void GenerateArgsExp(EmitExpContext context, TKTProcDesc expProcDesc, TKTProcDesc searchedProcDesc) 72 | { 73 | var size = expProcDesc.ArgCount; ; 74 | 75 | for (int i = 0; i < size; i++) 76 | { 77 | var arg = expProcDesc.GetArg(i); 78 | var exp = (arg.Value as Exp); 79 | TKTProcArg procArg = searchedProcDesc.GetArg(i); 80 | exp.RequireType = procArg.ArgType; 81 | exp.Generate(context); 82 | } 83 | } 84 | 85 | protected void GenerateArgsExp(EmitExpContext context, TKTProcDesc expProcDesc, TKTProcDesc searchedProcDesc,int size) 86 | { 87 | for (int i = 0; i < size; i++) 88 | { 89 | var arg = expProcDesc.GetArg(i); 90 | var exp = (arg.Value as Exp); 91 | TKTProcArg procArg = searchedProcDesc.GetArg(i); 92 | exp.RequireType = procArg.ArgType; 93 | exp.Generate(context); 94 | } 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/IGenerateSet.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZCompileCore.Analys.EContexts; 7 | 8 | namespace ZCompileCore.AST.Exps 9 | { 10 | public interface IGenerateSet 11 | { 12 | void GenerateSet(EmitExpContext context, Exp valueExp); 13 | bool CanWrite { get; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/NameValueExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection.Emit; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Analys.AContexts; 8 | using ZCompileCore.Analys.EContexts; 9 | using ZCompileCore.Lex; 10 | using ZCompileCore.Symbols; 11 | using ZCompileCore.Analys; 12 | using ZCompileCore.AST.Exps.Eds; 13 | 14 | namespace ZCompileCore.AST.Exps 15 | { 16 | public class NameValueExp: Exp 17 | { 18 | public Token NameToken; 19 | public Exp ValueExp; 20 | 21 | public NameValueExp() 22 | { 23 | //Elements = new List(); 24 | } 25 | 26 | public override Exp Analy(AnalyExpContext context) 27 | { 28 | base.Analy(context); 29 | ValueExp = ValueExp.Analy(context); 30 | if (ValueExp == null) return null; 31 | RetType = ValueExp.RetType; 32 | return this; 33 | } 34 | 35 | public override void Generate(EmitExpContext context) 36 | { 37 | ILGenerator il = context.ILout; 38 | ValueExp.Generate(context); 39 | } 40 | 41 | public override void GetNestedFields(Dictionary nestedField) 42 | { 43 | ValueExp.GetNestedFields(nestedField); 44 | } 45 | 46 | public override string ToCode() 47 | { 48 | return string.Format("{0}:{1}", NameToken.GetText(), ValueExp.ToCode()); 49 | } 50 | 51 | public override CodePostion Postion 52 | { 53 | get 54 | { 55 | return NameToken.Postion; 56 | } 57 | } 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Exps/UnaryExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Analys.AContexts; 9 | using ZCompileCore.Analys.EContexts; 10 | using ZCompileCore.Lex; 11 | using ZCompileCore.Symbols; 12 | using ZCompileCore.Tools; 13 | using ZCompileCore.Analys; 14 | using ZCompileCore.AST.Exps.Eds; 15 | using ZLangRT; 16 | 17 | namespace ZCompileCore.AST.Exps 18 | { 19 | public class UnaryExp:Exp 20 | { 21 | public Token OpToken { get; set; } 22 | public Exp RightExp { get; set; } 23 | 24 | TokenKind OpKind; 25 | 26 | public UnaryExp(Token token ,Exp rightExp) 27 | { 28 | OpToken = token; 29 | RightExp = rightExp; 30 | } 31 | 32 | public override Exp Analy(AnalyExpContext context) 33 | { 34 | base.Analy(context); 35 | var symbols = this.AnalyExpContext.Symbols; 36 | OpKind = OpToken.Kind; 37 | 38 | if(OpKind!= TokenKind.ADD && OpKind!= TokenKind.SUB) 39 | { 40 | errorf(OpToken.Postion, "运算符'{0}'缺少表达式", OpToken.GetText()); 41 | return null; 42 | } 43 | 44 | RightExp = RightExp.Analy(context);//RightExp = AnalyExp(RightExp); 45 | if(RightExp==null) 46 | { 47 | TrueAnalyed = false; 48 | return null; 49 | } 50 | 51 | Type rtype = RightExp.RetType; 52 | RetType = rtype; 53 | if(rtype!= typeof(int)&& rtype!= typeof(float) && rtype!= typeof(double) && rtype != typeof(decimal)) 54 | { 55 | errorf(RightExp.Postion,"不能进行'{0}'运算",OpToken.GetText()); 56 | return null; 57 | } 58 | 59 | if (OpKind == TokenKind.ADD) 60 | { 61 | return RightExp; 62 | } 63 | return this; 64 | } 65 | 66 | public override void Generate(EmitExpContext context) 67 | { 68 | ILGenerator il = context.ILout; 69 | 70 | EmitHelper.LoadInt(il,0); 71 | if(RetType== typeof(float)) 72 | { 73 | il.Emit(OpCodes.Conv_R4); 74 | } 75 | RightExp.Generate(context); 76 | il.Emit(OpCodes.Sub); 77 | base.GenerateConv(context); 78 | } 79 | 80 | #region 覆盖 81 | public override string ToCode() 82 | { 83 | StringBuilder buf = new StringBuilder(); 84 | buf.Append(OpToken != null ? OpToken.ToCode() : ""); 85 | buf.Append(RightExp != null ? RightExp.ToString() : ""); 86 | return buf.ToString(); 87 | } 88 | 89 | public override CodePostion Postion 90 | { 91 | get 92 | { 93 | return OpToken.Postion; ; 94 | } 95 | } 96 | 97 | public override void GetNestedFields(Dictionary nestedField) 98 | { 99 | RightExp.GetNestedFields(nestedField); 100 | } 101 | #endregion 102 | 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /ZCompileCore/AST/FileElementAST.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZCompileCore.Lex; 7 | using ZCompileCore.Reports; 8 | 9 | namespace ZCompileCore.AST 10 | { 11 | public abstract class FileElementAST : ASTree 12 | { 13 | public abstract CodePostion Postion { get; } 14 | 15 | public void error(string msg) 16 | { 17 | error(this.Postion, /*this.GetType().Name + ":" + */msg); 18 | TrueAnalyed = false; 19 | } 20 | 21 | public void errorf(string msgFormat, params string[] msgParams) 22 | { 23 | string msg = string.Format(msgFormat,msgParams); 24 | error(msg); 25 | } 26 | 27 | public void errorf(CodePostion postion, string msgFormat, params string[] msgParams) 28 | { 29 | string msg = string.Format(msgFormat, msgParams); 30 | error(postion,msg); 31 | } 32 | 33 | public void warningFormat(string msgFormat, params string[] msgParams) 34 | { 35 | string msg = string.Format(msgFormat, msgParams); 36 | Messager.Warning(this.Postion.Line,this.Postion.Col, /*this.GetType().Name + ":" + */msg); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Parts/AgreementAST.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Analys; 8 | using ZCompileCore.Analys.AContexts; 9 | using ZCompileCore.Tools; 10 | using ZLangRT; 11 | using ZLangRT.Descs; 12 | using ZCompileCore.Loads; 13 | using ZLangRT.Utils; 14 | using System.Reflection.Emit; 15 | using ZCompileCore.Lex; 16 | 17 | namespace ZCompileCore.AST.Parts 18 | { 19 | public class AgreementAST : PartAST 20 | { 21 | public Token KeyToken { get; set; } 22 | public List ValueList { get; set; } 23 | 24 | public AgreementAST() 25 | { 26 | ValueList = new List(); 27 | } 28 | 29 | public void Analy() 30 | { 31 | Dictionary dict = new Dictionary(); 32 | foreach (Token item in this.ValueList) 33 | { 34 | string name = item.GetText(); 35 | if(dict.ContainsKey(name)) 36 | { 37 | errorf(item.Postion, "'{0}'已经定义", name); 38 | } 39 | else 40 | { 41 | dict.Add(name, item); 42 | } 43 | } 44 | } 45 | 46 | public void Generate(EnumBuilder enumBuilder) 47 | { 48 | int i = 1; 49 | foreach (Token item in this.ValueList) 50 | { 51 | string name = item.GetText(); 52 | var builder = enumBuilder.DefineLiteral(name, i); 53 | i++; 54 | } 55 | } 56 | #region 位置格式化 57 | public override string ToCode() 58 | { 59 | Deep = 1; 60 | StringBuilder buf = new StringBuilder(); 61 | buf.AppendLine("约定:"); 62 | foreach(var token in ValueList) 63 | { 64 | buf.Append(" "); 65 | buf.AppendLine(token.GetText()); 66 | } 67 | return buf.ToString(); 68 | } 69 | 70 | public override CodePostion Postion 71 | { 72 | get { return KeyToken.Postion; } 73 | } 74 | #endregion 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Parts/Fns/FnArg.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZCompileCore.Lex; 7 | using ZCompileCore.Symbols; 8 | using ZCompileCore.Symbols.Defs; 9 | using ZCompileCore.Analyers; 10 | using ZCompileCore.Analys; 11 | using ZCompileCore.Loads; 12 | 13 | namespace ZCompileCore.AST.Parts.Fns 14 | { 15 | public class FnArg : FileElementAST 16 | { 17 | public Token ArgTypeToken { get; set; } 18 | public Token ArgNameToken { get; set; } 19 | 20 | public string ArgName { get; set; } 21 | public Type ArgType { get; set; } 22 | public bool IsGenericArg { get; set; } 23 | 24 | public bool Analy(FnName fn,int argIndex) 25 | { 26 | var symbols = fn.MethodAST.MethodContext.Symbols; 27 | 28 | if (ArgTypeToken != null && ArgNameToken != null) 29 | { 30 | ArgName = ArgNameToken.GetText(); 31 | if (ArgTypeToken.IsKeyIdent("类型")) 32 | { 33 | ArgType =typeof(object); 34 | IsGenericArg = true; 35 | } 36 | else 37 | { 38 | IsGenericArg = false; 39 | string argTypeName = ArgTypeToken.GetText(); 40 | SymbolInfo symbol = symbols.Get(argTypeName); 41 | if(symbol!=null) 42 | { 43 | SymbolDefClass defClass = symbol as SymbolDefClass; 44 | if(defClass!=null) 45 | { 46 | ArgType = defClass.ClassBuilder; 47 | } 48 | } 49 | 50 | if (ArgType == null) 51 | { 52 | //var gcls = fn.MethodAST.MethodContext.ClassContext.ImportContext.SearchGCL(argTypeName); 53 | //if (gcls.Length == 1) 54 | //{ 55 | // ArgType = gcls[0].ForType; 56 | //} 57 | var gcl = fn.MethodAST.MethodContext.ClassContext.SearchType(argTypeName); // 58 | if(gcl!=null) 59 | { 60 | ArgType = gcl.ForType; 61 | } 62 | if (ArgType == null) 63 | { 64 | error(string.Format("不存在或者不确定'{0}'类型", argTypeName)); 65 | return false; 66 | } 67 | } 68 | } 69 | if (ArgType != null) 70 | { 71 | if (symbols.Contains(ArgName)) 72 | { 73 | errorf("'{0}'已经存在相同的名称", ArgName); 74 | return false; 75 | } 76 | else 77 | { 78 | SymbolArg argSymbol = new SymbolArg(ArgName, ArgType, argIndex, IsGenericArg); 79 | symbols.AddSafe(argSymbol); 80 | } 81 | } 82 | } 83 | else 84 | { 85 | error("参数不正确"); 86 | return false; 87 | } 88 | return true; 89 | } 90 | 91 | #region 位置格式化 92 | public override string ToCode() 93 | { 94 | return string.Format("{0}:{1}", ArgTypeToken.GetText(), ArgNameToken.GetText()); 95 | } 96 | 97 | public override CodePostion Postion 98 | { 99 | get { return ArgTypeToken.Postion; } 100 | } 101 | #endregion 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Parts/Fns/FnBracket.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZCompileCore.Lex; 7 | using ZCompileCore.Analys; 8 | 9 | namespace ZCompileCore.AST.Parts.Fns 10 | { 11 | public class FnBracket : FileElementAST 12 | { 13 | public Token LeftBracketToken { get; set; } 14 | public Token RightBracketToken { get; set; } 15 | public int ArgIndex { get; set; } 16 | public List Args = new List(); 17 | 18 | public int Count 19 | { 20 | get 21 | { 22 | return Args.Count; 23 | } 24 | } 25 | 26 | 27 | #region 位置格式化 28 | public override string ToCode() 29 | { 30 | List buflist = new List(); 31 | foreach (var term in Args) 32 | { 33 | buflist.Add(term.ToCode()); 34 | } 35 | string fnname = string.Join(",", buflist); 36 | return string.Format("({0})", fnname); 37 | } 38 | 39 | public override CodePostion Postion 40 | { 41 | get { return LeftBracketToken.Postion; } 42 | } 43 | #endregion 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Parts/Fns/FnText.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZCompileCore.Lex; 7 | using ZCompileCore.Analys; 8 | 9 | namespace ZCompileCore.AST.Parts.Fns 10 | { 11 | public class FnText : FileElementAST 12 | { 13 | public Token TextToken { get; set; } 14 | public string TextContent { get { return TextToken.GetText(); } } 15 | 16 | public void Analy(FnName fnName) 17 | { 18 | //this.symbols = parentTable; 19 | foreach(var ch in TextContent) 20 | { 21 | if(ch>='0'&& ch<='9') 22 | { 23 | error("过程名称中不能出现数字"); 24 | break; 25 | } 26 | } 27 | } 28 | #region 位置格式化 29 | public override string ToCode() 30 | { 31 | return TextToken.GetText(); 32 | } 33 | 34 | public override CodePostion Postion 35 | { 36 | get { return TextToken.Postion; } 37 | } 38 | #endregion 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Parts/PackageAST.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZCompileCore.Lex; 7 | using ZCompileCore.Analys; 8 | using ZCompileCore.Analys.AContexts; 9 | 10 | namespace ZCompileCore.AST.Parts 11 | { 12 | public class PackageAST:FileElementAST 13 | { 14 | public List PackageTokens { get; set; } 15 | public PackageModel PackageModel { get; set; } 16 | public PackageAST() 17 | { 18 | PackageTokens = new List(); 19 | } 20 | 21 | public PackageModel Analy() 22 | { 23 | if(PackageTokens.Count==0) 24 | { 25 | error("没有包名称"); 26 | return null; 27 | } 28 | List list = new List(); 29 | foreach (Token item in this.PackageTokens) 30 | { 31 | list.Add(item.ToCode()); 32 | } 33 | var FullName = string.Join(".", list); 34 | List list2 = new List(); 35 | int size = PackageTokens.Count; 36 | for (int i = 0; i < size - 1; i++) 37 | { 38 | var item = PackageTokens[i]; 39 | list2.Add(item.ToCode()); 40 | } 41 | var PackageName = string.Join(".", list2); 42 | var TypeName = PackageTokens[size - 1].ToCode(); 43 | PackageModel = new PackageModel(); 44 | PackageModel.FullName = FullName; 45 | PackageModel.PackageName = PackageName; 46 | PackageModel.TypeName = TypeName; 47 | return PackageModel; 48 | } 49 | 50 | #region 位置格式化 51 | public override string ToCode() 52 | { 53 | List list = new List(); 54 | foreach (Token item in this.PackageTokens) 55 | { 56 | list.Add(item.ToCode()); 57 | } 58 | string str =string.Join("/",list); 59 | return str; 60 | } 61 | #endregion 62 | 63 | public override CodePostion Postion 64 | { 65 | get { return PackageTokens[0].Postion; } 66 | } 67 | } 68 | 69 | public class PackageModel 70 | { 71 | public string FullName { get; set; } 72 | public string PackageName { get; set; } 73 | public string TypeName { get; set; } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Parts/PartAST.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZCompileCore.Analys.AContexts; 7 | using ZCompileCore.AST.Stmts; 8 | using ZCompileCore.Lex; 9 | 10 | namespace ZCompileCore.AST.Parts 11 | { 12 | public abstract class PartAST : FileElementAST 13 | { 14 | public ClassContext ClassContext { get;protected set; } 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Parts/SimpleUseAST.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Text; 5 | using ZCompileCore.Analys.AContexts; 6 | using ZCompileCore.Lex; 7 | using ZCompileCore.Loads; 8 | using ZCompileCore.Analys; 9 | using ZLangRT.Descs; 10 | 11 | namespace ZCompileCore.AST.Parts 12 | { 13 | public class SimpleUseAST : PartAST 14 | { 15 | public Token KeyToken; 16 | public List NameTokens { get; set; } 17 | 18 | public SimpleUseAST() 19 | { 20 | NameTokens = new List(); 21 | } 22 | 23 | public void Analy(ImportContext context, ClassContext classContext) 24 | { 25 | foreach(var nameToken in NameTokens) 26 | { 27 | string typeName = nameToken.GetText(); 28 | List gcls = searchType(typeName, classContext); 29 | int count = gcls.Count; 30 | if(count==1) 31 | { 32 | var gcl = gcls[0]; 33 | var importTypes = classContext.ImportContext.DirectClasses; 34 | importTypes.Add(gcl); 35 | } 36 | else if(count==0) 37 | { 38 | errorf("没有找到类型'{0}'", typeName); 39 | } 40 | else 41 | { 42 | errorf("有多个'{0}'类型", typeName); 43 | } 44 | } 45 | } 46 | 47 | public List searchType(string typeName, ClassContext classContext) 48 | { 49 | List list = new List(); 50 | var refTypes = classContext.ProjectContext.TKTTypes; 51 | var libWords = classContext.ProjectContext.LibWords; 52 | foreach (var type in refTypes) 53 | { 54 | if (type.IsPublic && type.Name == typeName) 55 | { 56 | var gcl = GclUtil.Load(type, getWordDict(libWords, type)); 57 | if (gcl != null) 58 | { 59 | list.Add(gcl); 60 | } 61 | } 62 | } 63 | return list; 64 | } 65 | 66 | CnEnDict getWordDict(Dictionary libWords,Type type) 67 | { 68 | if(libWords.ContainsKey(type.Assembly)) 69 | { 70 | return libWords[type.Assembly]; 71 | } 72 | else 73 | { 74 | return null; 75 | } 76 | } 77 | 78 | #region 位置格式化 79 | public override string ToCode() 80 | { 81 | Deep = 1; 82 | StringBuilder buf = new StringBuilder(); 83 | buf.Append(KeyToken.GetText()); 84 | buf.Append(":"); 85 | List tempList = new List(); 86 | foreach(var name in NameTokens) 87 | { 88 | buf.Append(name.GetText()); 89 | } 90 | buf.Append(string.Join(",",tempList)); 91 | buf.AppendLine(";"); 92 | return buf.ToString(); 93 | } 94 | 95 | public override CodePostion Postion 96 | { 97 | get { return KeyToken.Postion; } 98 | } 99 | #endregion 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Stmts/BlockStmt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection.Emit; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Analys.AContexts; 8 | using ZCompileCore.Analys.EContexts; 9 | using ZCompileCore.Lex; 10 | using ZCompileCore.Tools; 11 | using ZCompileCore.Analys; 12 | using ZCompileCore.AST.Exps; 13 | 14 | namespace ZCompileCore.AST.Stmts 15 | { 16 | public class BlockStmt:Stmt 17 | { 18 | List Body = new List(); 19 | 20 | public void Analy(MethodContext methodContext) 21 | { 22 | var stmtContext = new AnalyStmtContext(methodContext,"BlockStmt"); 23 | Analy(stmtContext); 24 | } 25 | 26 | public override void Analy(AnalyStmtContext context) 27 | { 28 | base.Analy(context); 29 | 30 | foreach (var stmt in Body) 31 | { 32 | stmt.Method = this.Method; 33 | stmt.Analy(context); 34 | } 35 | //处理catch 36 | List catchIndex = new List(); 37 | for (int i = 0; i < Body.Count; i++) 38 | { 39 | if(Body[i] is CatchStmt) 40 | { 41 | catchIndex.Add(i); 42 | } 43 | } 44 | 45 | if(catchIndex.Count>0) 46 | { 47 | List Body2 = new List(); 48 | catchIndex.Insert(0, -1); 49 | for (int i = 0; i < catchIndex.Count-1; i++) 50 | { 51 | Body2.Add(new TryStmt()); 52 | List subList = ListHelper.SubList(Body,catchIndex[i]+1, catchIndex[i+1]); 53 | Body2.AddRange(subList); 54 | } 55 | List subList2 = ListHelper.SubList(Body, ListHelper.Last(catchIndex)+1, Body.Count); 56 | Body2.AddRange(subList2); 57 | Body = Body2; 58 | } 59 | } 60 | 61 | public override void Generate(EmitStmtContext context) 62 | { 63 | foreach (var stmt in Body) 64 | { 65 | stmt.Generate(context); 66 | } 67 | } 68 | 69 | public void Add(Stmt stmt) 70 | { 71 | Body.Add(stmt); 72 | } 73 | 74 | #region 覆盖 75 | public override string ToCode() 76 | { 77 | StringBuilder buf = new StringBuilder(); 78 | 79 | buf.Append(getStmtPrefix()); 80 | buf.AppendLine(); 81 | foreach (var fe in Body) 82 | { 83 | buf.Append(fe.ToCode()); 84 | buf.AppendLine(); 85 | } 86 | return buf.ToString(); 87 | } 88 | 89 | public override CodePostion Postion 90 | { 91 | get 92 | { 93 | return Body[0].Postion; 94 | } 95 | } 96 | #endregion 97 | 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Stmts/CallStmt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Analys.AContexts; 9 | using ZCompileCore.Analys.EContexts; 10 | using ZCompileCore.AST.Exps; 11 | using ZCompileCore.Lex; 12 | using ZCompileCore.Analys; 13 | using ZCompileCore.Tools; 14 | using ZLangRT; 15 | 16 | namespace ZCompileCore.AST.Stmts 17 | { 18 | public class CallStmt:Stmt 19 | { 20 | public Exp CallExpr { get; set; } 21 | Exp resultExp; 22 | 23 | public override void Analy(AnalyStmtContext context) 24 | { 25 | base.Analy(context); 26 | resultExp = AnalyExp(CallExpr); 27 | if(resultExp==null) 28 | { 29 | TrueAnalyed = false; 30 | } 31 | } 32 | 33 | public override void Generate(EmitStmtContext context) 34 | { 35 | ILGenerator il = context.ILout; 36 | MarkSequencePoint(context); 37 | EmitExpContext expContext = new EmitExpContext(context); 38 | resultExp.Generate(expContext); 39 | if (resultExp.RetType != typeof(void)) 40 | { 41 | il.Emit(OpCodes.Pop); 42 | } 43 | } 44 | 45 | #region 覆盖 46 | public override string ToCode() 47 | { 48 | StringBuilder buff = new StringBuilder(); 49 | if (CallExpr != null) 50 | { 51 | buff.Append(getStmtPrefix()); 52 | buff.Append(CallExpr.ToCode()); 53 | } 54 | buff.Append(";"); 55 | return buff.ToString(); 56 | } 57 | 58 | public override CodePostion Postion 59 | { 60 | get 61 | { 62 | return CallExpr.Postion; 63 | } 64 | } 65 | #endregion 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Stmts/CatchStmt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection.Emit; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Analys.AContexts; 8 | using ZCompileCore.Analys.EContexts; 9 | using ZCompileCore.Lex; 10 | using ZCompileCore.Symbols.Defs; 11 | using ZCompileCore.Tools; 12 | using ZCompileCore.Analys; 13 | using Z语言系统; 14 | using ZLangRT; 15 | using ZCompileCore.AST.Exps; 16 | 17 | namespace ZCompileCore.AST.Stmts 18 | { 19 | public class CatchStmt:Stmt 20 | { 21 | public Token CatchToken { get; set; } 22 | public Token ExceptionTypeToken { get; set; } 23 | public Token ExceptionNameToken { get; set; } 24 | public BlockStmt CatchBody { get; set; } 25 | 26 | string exTypeName; 27 | string exName; 28 | Type exType; 29 | SymbolVar exSymbol; 30 | 31 | public override void Analy(AnalyStmtContext context) 32 | { 33 | base.Analy(context); 34 | var symbols = this.AnalyStmtContext.Symbols; 35 | exTypeName = ExceptionTypeToken.GetText(); 36 | exName = ExceptionNameToken.GetText(); 37 | exType = context.MethodContext.ClassContext.SearchType(exTypeName).ForType; 38 | if(exType==null) 39 | { 40 | errorf(ExceptionTypeToken.Postion,"类型'{0}'不存在",exTypeName); 41 | } 42 | var exSymbol2 = symbols.Get(exName); 43 | if (exSymbol2 == null) 44 | { 45 | exSymbol = new SymbolVar(exName, exType); 46 | exSymbol.LoacalVarIndex = context.MethodContext.CreateLocalVarIndex(exName); 47 | } 48 | else 49 | { 50 | if(exSymbol2 is SymbolVar) 51 | { 52 | exSymbol = exSymbol2 as SymbolVar; 53 | if(exSymbol.DimType!=exType) 54 | { 55 | errorf(ExceptionNameToken.Postion, "变量'{0}'的类型与异常的类型不一致", exName); 56 | } 57 | } 58 | else 59 | { 60 | errorf(ExceptionNameToken.Postion, "变量名称'{0}'已经使用过", exName); 61 | } 62 | } 63 | symbols.Add(exSymbol); 64 | analySubStmt(CatchBody); 65 | } 66 | 67 | public override void Generate(EmitStmtContext context) 68 | { 69 | ILGenerator il = context.ILout; 70 | MarkSequencePoint(context); 71 | il.BeginCatchBlock(exType); 72 | EmitHelper.StormVar(il, exSymbol.VarBuilder); 73 | CatchBody.Generate(context); 74 | il.EndExceptionBlock(); 75 | } 76 | 77 | #region 覆盖 78 | public override string ToCode() 79 | { 80 | StringBuilder buf = new StringBuilder(); 81 | buf.Append(getStmtPrefix()); 82 | buf.AppendFormat("{0}({1}:{2})", CatchToken.GetText(),ExceptionTypeToken.GetText(),ExceptionNameToken.GetText()); 83 | buf.AppendLine(); 84 | buf.Append(CatchBody.ToString()); 85 | return buf.ToString(); 86 | } 87 | 88 | public override CodePostion Postion 89 | { 90 | get 91 | { 92 | return CatchToken.Postion; 93 | } 94 | } 95 | #endregion 96 | 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Stmts/Stmt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection.Emit; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Analys.AContexts; 8 | using ZCompileCore.Analys.EContexts; 9 | using ZCompileCore.AST.Exps; 10 | using ZCompileCore.AST.Parts; 11 | using ZCompileCore.Analys; 12 | 13 | namespace ZCompileCore.AST.Stmts 14 | { 15 | public abstract class Stmt : FileElementAST 16 | { 17 | public MethodAST Method { get; set; } 18 | public AnalyStmtContext AnalyStmtContext { get; set; } 19 | 20 | public abstract void Generate(EmitStmtContext context); 21 | 22 | public virtual void Analy(AnalyStmtContext context) 23 | { 24 | this.AnalyStmtContext = context; 25 | } 26 | 27 | protected string getStmtPrefix() 28 | { 29 | StringBuilder buff = new StringBuilder(); 30 | int temp = Deep; 31 | while (temp > 0) 32 | { 33 | buff.Append(" "); 34 | temp--; 35 | } 36 | return buff.ToString(); 37 | } 38 | protected AnalyExpContext _AnalyExpContext; 39 | protected Exp AnalyExp(Exp exp) 40 | { 41 | exp.Stmt = this; 42 | if(_AnalyExpContext==null) 43 | _AnalyExpContext = new AnalyExpContext(this.AnalyStmtContext); 44 | return exp.Analy(_AnalyExpContext); 45 | } 46 | 47 | protected void analySubStmt(Stmt stmt) 48 | { 49 | stmt.Method = this.Method; 50 | stmt.Analy(this.AnalyStmtContext); 51 | } 52 | 53 | protected void MarkSequencePoint(EmitStmtContext context) 54 | { 55 | ILGenerator il = context.ILout; 56 | if (context.MethodEmitContext.ClassContext.IDoc != null) 57 | { 58 | il.MarkSequencePoint(context.MethodEmitContext.ClassContext.IDoc, this.Postion.Line, this.Postion.Col, this.Postion.Line, this.Postion.Col+this.ToCode().Length); 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Stmts/TryStmt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection.Emit; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Analys.AContexts; 8 | using ZCompileCore.Analys.EContexts; 9 | using ZCompileCore.Lex; 10 | using ZCompileCore.Analys; 11 | using ZCompileCore.Tools; 12 | using Z语言系统; 13 | using ZLangRT; 14 | using ZCompileCore.AST.Exps; 15 | 16 | 17 | namespace ZCompileCore.AST.Stmts 18 | { 19 | public class TryStmt:Stmt 20 | { 21 | public override void Analy(AnalyStmtContext context) 22 | { 23 | //base.LoadRefTypes(context); 24 | //var symbols = this.AnalyStmtContext.Symbols; 25 | } 26 | 27 | public override void Generate(EmitStmtContext context) 28 | { 29 | ILGenerator il = context.ILout; 30 | Label tryLabel = il.BeginExceptionBlock(); 31 | } 32 | 33 | #region 覆盖 34 | public override string ToCode() 35 | { 36 | return (getStmtPrefix() + "try"); 37 | } 38 | 39 | public override CodePostion Postion 40 | { 41 | get 42 | { 43 | return new CodePostion(0,0); 44 | } 45 | } 46 | #endregion 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Stmts/WhileStmt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection.Emit; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Analys.AContexts; 8 | using ZCompileCore.Analys.EContexts; 9 | using ZCompileCore.AST.Exps; 10 | using ZCompileCore.Lex; 11 | using ZCompileCore.Tools; 12 | using ZCompileCore.Analys; 13 | using Z语言系统; 14 | using ZLangRT; 15 | 16 | 17 | namespace ZCompileCore.AST.Stmts 18 | { 19 | public class WhileStmt:Stmt 20 | { 21 | public Token WhileToken { get; set; } 22 | public Exp Condition { get; set; } 23 | public BlockStmt Body { get; set; } 24 | 25 | Type logicType = typeof(bool); 26 | 27 | public WhileStmt() 28 | { 29 | 30 | } 31 | 32 | public override void Analy(AnalyStmtContext context) 33 | { 34 | base.Analy(context); 35 | var symbols = this.AnalyStmtContext.Symbols; 36 | Condition=AnalyExp(Condition); 37 | if (Condition == null) 38 | { 39 | TrueAnalyed = false; 40 | } 41 | else if (Condition.RetType == null || Condition.RetType != logicType) 42 | { 43 | error(Condition.Postion, "循环当语句的条件表达式不是判断表达式"); 44 | } 45 | analySubStmt(Body); 46 | } 47 | 48 | public override void Generate(EmitStmtContext context) 49 | { 50 | ILGenerator il = context.ILout; 51 | EmitExpContext expContext = new EmitExpContext(context); 52 | var True_Label = il.DefineLabel(); 53 | var False_Label = il.DefineLabel(); 54 | 55 | il.MarkLabel(True_Label); 56 | Condition.Generate(expContext); 57 | EmitHelper.LoadInt(il, 1); 58 | il.Emit(OpCodes.Ceq); 59 | il.Emit(OpCodes.Brfalse, False_Label); 60 | Body.Generate(context); 61 | il.Emit(OpCodes.Br, True_Label); 62 | il.MarkLabel(False_Label); 63 | } 64 | 65 | #region 位置格式化 66 | public override string ToCode() 67 | { 68 | StringBuilder buf = new StringBuilder(); 69 | buf.Append(getStmtPrefix()); 70 | buf.AppendFormat("循环当( {0} )", this.Condition.ToCode()); 71 | buf.AppendLine(); 72 | buf.Append(Body.ToString()); 73 | return buf.ToString(); 74 | } 75 | 76 | public override CodePostion Postion 77 | { 78 | get 79 | { 80 | return WhileToken.Postion; 81 | } 82 | } 83 | #endregion 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Types/EnumAST.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Analys.AContexts; 9 | using ZCompileCore.AST.Parts; 10 | using ZCompileCore.Lex; 11 | using ZCompileCore.Analys; 12 | using ZCompileCore.AST.Parts.Fns; 13 | using ZCompileCore.AST.Stmts; 14 | using ZLangRT; 15 | using ZLangRT.Attributes; 16 | 17 | namespace ZCompileCore.AST.Types 18 | { 19 | public class EnumAST : FileElementAST 20 | { 21 | Token nameToken; 22 | AgreementAST ast; 23 | FileAST fileAST; 24 | ProjectContext ProjectContext { get; set; } 25 | 26 | public EnumAST(FileAST fileAST) 27 | { 28 | this.fileAST = fileAST; 29 | if(fileAST.AgreementList.Count==0) 30 | { 31 | errorf("不存在约定值"); 32 | } 33 | else if (fileAST.AgreementList.Count >1 ) 34 | { 35 | errorf("一个文件只能定义一个约定"); 36 | } 37 | else 38 | { 39 | ast = fileAST.AgreementList[0]; 40 | nameToken = fileAST.NameToken; 41 | ProjectContext = fileAST.ProjectContext; 42 | } 43 | } 44 | 45 | public void Analy() 46 | { 47 | if (ast != null) ast.Analy(); 48 | ast.Analy(); 49 | } 50 | 51 | public Type Generate() 52 | { 53 | string enumName = nameToken.GetText(); 54 | string enumFullName = ProjectContext.RootNameSpace + "." + enumName;// FileContext.ProjectContext.RootNameSpace + "." + EnumName; 55 | EnumBuilder enumBuilder = ProjectContext.EmitContext.ModuleBuilder.DefineEnum(enumFullName, TypeAttributes.Public, typeof(int)); 56 | setAttrTktClass(enumBuilder); 57 | ast.Generate(enumBuilder); 58 | /* 59 | int i = 1; 60 | foreach (Token item in this.EnumItems) 61 | { 62 | string name = item.GetText(); 63 | var builder = enumBuilder.DefineLiteral(name,i); 64 | //this.EnumSymbol.EnumValueDict[name].Builder = builder; 65 | i++; 66 | }*/ 67 | Type type= enumBuilder.CreateType(); 68 | return type; 69 | } 70 | 71 | protected void setAttrTktClass(EnumBuilder classBuilder) 72 | { 73 | Type myType = typeof(ZClassAttribute); 74 | ConstructorInfo infoConstructor = myType.GetConstructor(new Type[] { }); 75 | CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(infoConstructor, new object[] { }); 76 | classBuilder.SetCustomAttribute(attributeBuilder); 77 | } 78 | 79 | public override string ToCode() 80 | { 81 | return this.fileAST.ToCode(); 82 | } 83 | 84 | public override CodePostion Postion 85 | { 86 | get { return nameToken.Postion; } 87 | } 88 | } 89 | 90 | 91 | } 92 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Types/NormalClassAST.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Loads; 9 | using ZCompileCore.Reports; 10 | using ZCompileCore.Symbols.Defs; 11 | using ZCompileCore.Analyers; 12 | using ZCompileCore.Analys; 13 | using ZCompileCore.Analys.AContexts; 14 | using ZCompileCore.AST.Parts; 15 | using ZCompileCore.AST.Parts.Fns; 16 | using ZCompileCore.AST.Stmts; 17 | using ZCompileCore.Lex; 18 | using ZLangRT; 19 | using ZLangRT.Attributes; 20 | using Z语言系统; 21 | 22 | namespace ZCompileCore.AST.Types 23 | { 24 | public class NormalClassAST : ClassAST 25 | { 26 | public NormalClassAST(FileAST fileAST) 27 | : base(fileAST) 28 | { 29 | 30 | } 31 | 32 | bool IsStatic = false; 33 | 34 | public Type Compile() 35 | { 36 | bool yes = true; 37 | AnalyImport(); 38 | CompileClassName(); 39 | 40 | TypeBuilder classBuilder = ClassContext.ClassSymbol.ClassBuilder; 41 | AnalyPropertyName(IsStatic); 42 | AnalyMethodName(IsStatic); 43 | yes = yes && !Messager.HasErrorOrWarning(); 44 | yes = yes && compilePropertyBody(IsStatic); 45 | yes = yes && compileMethodBody(IsStatic); 46 | 47 | if (yes) 48 | { 49 | Type type = classBuilder.CreateType(); 50 | return type; 51 | } 52 | else 53 | { 54 | return null; 55 | } 56 | } 57 | 58 | void CompileClassName() 59 | { 60 | checkName(); 61 | var symbols = this.ClassContext.Symbols; 62 | ClassContext.ClassSymbol = new SymbolDefClass(ClassName,false); 63 | if (this.fileAST.ExtendsToken != null) 64 | { 65 | string superName = this.fileAST.ExtendsToken.GetText(); 66 | SuperGcl = this.ClassContext.SearchType(superName); 67 | if (SuperGcl == null) 68 | { 69 | error(this.fileAST.ExtendsToken.Postion, "没有找到类型'" + superName + "'"); 70 | } 71 | else 72 | { 73 | Type type = SuperGcl.ForType; 74 | if (type.IsSealed) 75 | { 76 | error(this.fileAST.ExtendsToken.Postion, "类型'" + superName + "'不能继承"); 77 | } 78 | } 79 | } 80 | else 81 | { 82 | SuperGcl = new MappingGcl(typeof(事物), null); 83 | } 84 | 85 | string fullName = this.ClassContext.ProjectContext.RootNameSpace + "." + ClassName; 86 | TypeBuilder classBuilder = createTypeBuilder(fullName); 87 | 88 | ClassContext.ClassSymbol.ClassBuilder = classBuilder; 89 | ClassContext.EmitContext.CurrentTypeBuilder = classBuilder; 90 | ClassContext.ClassSymbol.BaseGcl = SuperGcl; 91 | ClassContext.EmitContext.IDoc = this.ClassContext.ProjectContext.EmitContext.ModuleBuilder.DefineDocument(this.fileAST.FileName, Guid.Empty, Guid.Empty, Guid.Empty); 92 | setAttrTktClass(classBuilder); 93 | symbols.AddSafe(ClassContext.ClassSymbol); 94 | } 95 | 96 | TypeBuilder createTypeBuilder(string fullName) 97 | { 98 | TypeBuilder classBuilder = this.ClassContext.ProjectContext.EmitContext.ModuleBuilder.DefineType(fullName, TypeAttributes.Public); 99 | if (SuperGcl != null) 100 | { 101 | classBuilder.SetParent(SuperGcl.ForType); 102 | this.ClassContext.ClassSymbol.BaseGcl = SuperGcl; 103 | } 104 | 105 | return classBuilder; 106 | } 107 | 108 | } 109 | 110 | 111 | } 112 | -------------------------------------------------------------------------------- /ZCompileCore/AST/Types/StaticClassAST.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Reports; 9 | using ZCompileCore.Symbols.Defs; 10 | using ZCompileCore.Tools; 11 | using ZCompileCore.Analyers; 12 | using ZCompileCore.Analys; 13 | using ZCompileCore.Analys.AContexts; 14 | using ZCompileCore.AST.Parts; 15 | using ZCompileCore.AST.Parts.Fns; 16 | using ZCompileCore.AST.Stmts; 17 | using ZCompileCore.Lex; 18 | using ZCompileCore.Loads; 19 | using ZLangRT; 20 | using ZLangRT.Attributes; 21 | 22 | namespace ZCompileCore.AST.Types 23 | { 24 | public class StaticClassAST : ClassAST 25 | { 26 | public StaticClassAST(FileAST fileAST):base(fileAST) 27 | { 28 | 29 | } 30 | 31 | bool IsStatic = true; 32 | 33 | public Type Compile() 34 | { 35 | bool yes = true; 36 | AnalyImport(); 37 | CompileClassName(); 38 | 39 | TypeBuilder classBuilder = ClassContext.ClassSymbol.ClassBuilder; 40 | AnalyPropertyName(IsStatic); 41 | AnalyMethodName(IsStatic); 42 | 43 | yes = yes && !Messager.HasErrorOrWarning(); 44 | yes = yes && compilePropertyBody(IsStatic); 45 | yes = yes && compileMethodBody(IsStatic); 46 | if (this.ClassContext.ZeroConstructor == null && ClassContext.InitMemberValueMethod != null) 47 | { 48 | ClassContext.ZeroConstructor = ClassContext.EmitContext.CurrentTypeBuilder.DefineConstructor(MethodAttributes.Static, CallingConventions.Standard, new Type[] { }); 49 | var il = ClassContext.ZeroConstructor.GetILGenerator(); 50 | EmitHelper.CallDynamic(il, ClassContext.InitMemberValueMethod,true);//EmitHelper.CallDynamic(il, ClassContext.InitMemberValueMethod); 51 | il.Emit(OpCodes.Ret); 52 | } 53 | if (yes) 54 | { 55 | Type type = classBuilder.CreateType(); 56 | return type; 57 | } 58 | else 59 | { 60 | return null; 61 | } 62 | } 63 | 64 | void CompileClassName() 65 | { 66 | checkName(); 67 | var symbols = this.ClassContext.Symbols; 68 | ClassContext.ClassSymbol = new SymbolDefClass(ClassName,true); 69 | SuperGcl = null; 70 | string fullName = this.ClassContext.ProjectContext.RootNameSpace + "." + ClassName; 71 | TypeBuilder classBuilder = createTypeBuilder(fullName); 72 | 73 | ClassContext.ClassSymbol.ClassBuilder = classBuilder; 74 | ClassContext.EmitContext.CurrentTypeBuilder = classBuilder; 75 | ClassContext.EmitContext.IDoc = this.ClassContext.ProjectContext.EmitContext.ModuleBuilder.DefineDocument(this.fileAST.FileName, Guid.Empty, Guid.Empty, Guid.Empty); 76 | setAttrTktClass(classBuilder); 77 | symbols.AddSafe(ClassContext.ClassSymbol); 78 | } 79 | 80 | TypeBuilder createTypeBuilder(string fullName) 81 | { 82 | TypeAttributes typeAttrs = TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.Sealed; 83 | TypeBuilder classBuilder = this.ClassContext.ProjectContext.EmitContext.ModuleBuilder.DefineType(fullName,typeAttrs); 84 | return classBuilder; 85 | } 86 | } 87 | 88 | 89 | } 90 | -------------------------------------------------------------------------------- /ZCompileCore/Analyers/ImportAnalyer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Analys.AContexts; 8 | using ZCompileCore.AST.Parts; 9 | using ZCompileCore.Analys; 10 | using ZCompileCore.Loads; 11 | using ZCompileCore.Tools; 12 | using ZLangRT; 13 | using ZLangRT.Descs; 14 | 15 | namespace ZCompileCore.Analyers 16 | { 17 | public class ImportAnalyer 18 | { 19 | ClassContext classContext; 20 | ImportPackageAST importPackage; 21 | Dictionary importPackages; 22 | SimpleUseAST simpleUse; 23 | 24 | public ImportAnalyer(ClassContext classContext, ImportPackageAST importList, SimpleUseAST simpleUse) 25 | { 26 | this.classContext = classContext; 27 | importPackage = importList; 28 | this.simpleUse = simpleUse; 29 | } 30 | 31 | public void Analy() 32 | { 33 | importPackages = new Dictionary(); 34 | var context = classContext.ImportContext; 35 | importPackage.Analy(context, classContext, importPackages); 36 | if (simpleUse != null) 37 | { 38 | simpleUse.Analy(context, classContext); 39 | } 40 | } 41 | 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /ZCompileCore/Analyers/PropertyAnalyer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Analys.AContexts; 9 | using ZCompileCore.Analys.EContexts; 10 | using ZCompileCore.AST.Parts; 11 | using ZCompileCore.Analys; 12 | using ZLangRT; 13 | 14 | namespace ZCompileCore.Analyers 15 | { 16 | public class PropertyAnalyer 17 | { 18 | ClassContext ClassContext; 19 | List PropertyList; 20 | public int Count { get { return PropertyList.Count; } } 21 | 22 | public PropertyAnalyer(ClassContext classContext, List propertyList) 23 | { 24 | ClassContext = classContext; 25 | PropertyList = propertyList; 26 | } 27 | 28 | public void CompileName( bool isStatic ) 29 | { 30 | foreach (var ast in PropertyList) 31 | { 32 | ast.CompileName(ClassContext, isStatic); 33 | } 34 | } 35 | 36 | 37 | public bool CompileBody(bool isStatic) 38 | { 39 | if (Count == 0) return true; 40 | TypeBuilder classBuilder = ClassContext.ClassSymbol.ClassBuilder; 41 | if (ClassContext.InitMemberValueMethod == null) 42 | { 43 | string initMemberValueMethodName = "__InitMemberValueMethod"; 44 | if (isStatic) 45 | { 46 | ClassContext.InitMemberValueMethod = classBuilder.DefineMethod(initMemberValueMethodName, MethodAttributes.Private | MethodAttributes.Static, typeof(void), new Type[] { }); 47 | } 48 | else 49 | { 50 | ClassContext.InitMemberValueMethod = classBuilder.DefineMethod(initMemberValueMethodName, MethodAttributes.Private, typeof(void), new Type[] { }); 51 | } 52 | } 53 | EmitMethodContext context = new EmitMethodContext(); 54 | context.ClassContext = ClassContext.EmitContext; ; 55 | context.SetBuilder( ClassContext.InitMemberValueMethod); 56 | EmitStmtContext stmtContext = new EmitStmtContext(context); 57 | EmitExpContext expContext = new EmitExpContext(stmtContext); 58 | 59 | var il = ClassContext.InitMemberValueMethod.GetILGenerator(); 60 | foreach (var ppt in PropertyList) 61 | { 62 | ppt.GenerateBody(expContext); 63 | } 64 | il.Emit(OpCodes.Ret); 65 | return true; 66 | } 67 | 68 | } 69 | } 70 | 71 | -------------------------------------------------------------------------------- /ZCompileCore/Builders/ZCompileClassModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace ZCompileCore.Builders 8 | { 9 | public class ZCompileClassModel 10 | { 11 | /// 12 | /// 项目实体 13 | /// 14 | public ZCompileProjectModel Project { get; set; } 15 | 16 | /// 17 | /// Z语言源文件 18 | /// 19 | public FileInfo SourceFileInfo { get; set; } 20 | 21 | /// 22 | /// 编译前在Z程序前的补充代码 23 | /// 24 | public string PreSourceCode { get; set; } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ZCompileCore/Builders/ZCompileProjectModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | 8 | namespace ZCompileCore.Builders 9 | { 10 | public class ZCompileProjectModel 11 | { 12 | /// 13 | /// 引用的外部DLL 14 | /// 15 | public List RefDllList { get;private set; } 16 | 17 | /// 18 | /// 引用的包名称 19 | /// 20 | public List RefPackageList { get; private set; } 21 | 22 | /// 23 | /// 项目包名称 24 | /// 25 | public string ProjectPackageName { get; set; } 26 | 27 | /// 28 | /// 生成的文件类型 29 | /// 30 | public PEFileKinds BinaryFileKind { get; set; } 31 | 32 | /// 33 | /// 生成的文件名称(不带扩展名) 34 | /// 35 | public string BinaryFileNameNoEx { get; set; } 36 | 37 | /// 38 | /// 编译后的保存二进制文件的文件夹 39 | /// 40 | public DirectoryInfo BinarySaveDirectoryInfo { get; set; } 41 | 42 | /// 43 | /// 入口类名称 44 | /// 45 | public string EntryClassName { get; set; } 46 | 47 | /// 48 | /// 项目程序文件信息列表 49 | /// 50 | public List SouceFileList { get; private set; } 51 | 52 | /// 53 | /// 项目根目录 54 | /// 55 | public DirectoryInfo ProjectRootDirectoryInfo { get; set; } 56 | 57 | /// 58 | /// 项目文件名称 59 | /// 60 | public string ProjectFileName { get; set; } 61 | 62 | /// 63 | /// 是否保存生成的二进制文件 64 | /// 65 | public bool NeedSave { get; set; } 66 | 67 | public ZCompileProjectModel() 68 | { 69 | RefDllList = new List(); 70 | RefPackageList = new List(); 71 | SouceFileList = new List(); 72 | } 73 | 74 | public void AddClass(ZCompileClassModel zCompileClassModel) 75 | { 76 | zCompileClassModel.Project = this; 77 | SouceFileList.Add(zCompileClassModel); 78 | } 79 | 80 | public void AddRefDll(FileInfo dll) 81 | { 82 | RefDllList.Add(dll); 83 | } 84 | 85 | public void AddRefPackage(string packageName) 86 | { 87 | RefPackageList.Add(packageName); 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /ZCompileCore/CompileConstant.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ZCompileCore 7 | { 8 | public static class CompileConstant 9 | { 10 | public const String LangPackageName = "Z语言系统"; 11 | 12 | public const String CodeFileExt = ".zyy"; 13 | 14 | public const String ProjectFileExt = ".zproj"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ZCompileCore/Contexts/AContexts/AnalyExpContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ZCompileCore.Analys.AContexts 8 | { 9 | public class AnalyExpContext 10 | { 11 | public AnalyStmtContext StmtContext { get; set; } 12 | public SymbolTable Symbols { get; set; } 13 | 14 | public AnalyExpContext(AnalyStmtContext stmtContext) 15 | { 16 | this.StmtContext = stmtContext; 17 | this.Symbols = stmtContext.Symbols; 18 | } 19 | 20 | public ClassContext ClassContext 21 | { 22 | get 23 | { 24 | return this.StmtContext.MethodContext.ClassContext; 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /ZCompileCore/Contexts/AContexts/AnalyStmtContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection.Emit; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace ZCompileCore.Analys.AContexts 9 | { 10 | public class AnalyStmtContext 11 | { 12 | public MethodContext MethodContext { get; set; } 13 | public SymbolTable Symbols { get; set; } 14 | public string Name { get; private set; } 15 | 16 | public AnalyStmtContext(MethodContext methodContext,string name) 17 | { 18 | MethodContext = methodContext; 19 | Name = name; 20 | //this.Symbols = methodContext.Symbols.Push(Name); 21 | this.Symbols = methodContext.Symbols; 22 | } 23 | 24 | public ClassContext ClassContext 25 | { 26 | get 27 | { 28 | return this.MethodContext.ClassContext; 29 | } 30 | } 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /ZCompileCore/Contexts/EContexts/EmitClassContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.SymbolStore; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Reflection.Emit; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace ZCompileCore.Analys.EContexts 11 | { 12 | public class EmitClassContext 13 | { 14 | public EmitProjectContext ProjectContext { get; set; } 15 | //public TypeBuilder ParentTypeBuilder { get; set; } 16 | public TypeBuilder CurrentTypeBuilder { get; set; } 17 | public ISymbolDocumentWriter IDoc { get; set; } 18 | //public bool IsNested { get { return ParentTypeBuilder != null; } } 19 | 20 | public EmitClassContext(EmitProjectContext projectContext) 21 | { 22 | ProjectContext = projectContext; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ZCompileCore/Contexts/EContexts/EmitConstructorContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace ZCompileCore.Analys.EContexts 10 | { 11 | public class EmitConstructorContext 12 | { 13 | public EmitClassContext ClassContext { get; set; } 14 | public ConstructorBuilder CurrentBuilder { get; set; } 15 | public ILGenerator ILout { get { return this.CurrentBuilder.GetILGenerator(); } } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ZCompileCore/Contexts/EContexts/EmitExpContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace ZCompileCore.Analys.EContexts 10 | { 11 | public class EmitExpContext 12 | { 13 | public EmitStmtContext StmtContext { get; set; } 14 | 15 | public ILGenerator ILout { get { return this.StmtContext.ILout; } } 16 | 17 | public EmitExpContext(EmitStmtContext stmtContext) 18 | { 19 | StmtContext = stmtContext; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ZCompileCore/Contexts/EContexts/EmitMethodContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace ZCompileCore.Analys.EContexts 10 | { 11 | public class EmitMethodContext 12 | { 13 | public EmitClassContext ClassContext { get; set; } 14 | //public EmitProjectContext ProjectContext { get { return this.ClassContext.ProjectContext; } } 15 | //public MethodBuilder CurrentMethodBuilder { get; set; } 16 | public MethodBuilder CurrentMethodBuilder { get;private set; } 17 | public ConstructorBuilder CurrentConstructorBuilder { get; private set; } 18 | 19 | public void SetBuilder(MethodBuilder methodBuilder) 20 | { 21 | CurrentMethodBuilder = methodBuilder; 22 | } 23 | 24 | public void SetBuilder(ConstructorBuilder constructorBuilder) 25 | { 26 | CurrentConstructorBuilder = constructorBuilder; 27 | } 28 | 29 | //public EmitMethodContext ParentContext { get; set; } 30 | //private ILGenerator _ILout; 31 | public ILGenerator ILout 32 | { 33 | get { 34 | if (CurrentMethodBuilder!=null) 35 | return this.CurrentMethodBuilder.GetILGenerator(); 36 | else 37 | return this.CurrentConstructorBuilder.GetILGenerator(); 38 | } 39 | } 40 | //{ 41 | // get 42 | // { 43 | // if (_ILout == null) 44 | // { 45 | // _ILout = CurrentMethodBuilder.GetILGenerator(); 46 | // } 47 | // return _ILout; 48 | // } 49 | //} 50 | //public TypeBuilder CurrentTypeBuilder { get; set; } 51 | 52 | //public TypeBuilder NestedTypeBuilder { get; set; } 53 | /* 54 | public EmitMethodContext PushNew() 55 | { 56 | EmitMethodContext emitContext = new EmitMethodContext(); 57 | emitContext.ProjectContext = this.ProjectContext; 58 | //emitContext.ILout = this.ILout; 59 | emitContext.ParentContext = this; 60 | //emitContext.CurrentTypeBuilder = this.CurrentTypeBuilder; 61 | //emitContext.NestedTypeBuilder = this.NestedTypeBuilder; 62 | //emitContext.CurrentMethodBuilder = this.CurrentMethodBuilder; 63 | return emitContext; 64 | }*/ 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /ZCompileCore/Contexts/EContexts/EmitProjectContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace ZCompileCore.Analys.EContexts 10 | { 11 | public class EmitProjectContext 12 | { 13 | public AssemblyName AssemblyName { get; set; } 14 | public AppDomain CurrentAppDomain { get; set; } 15 | public AssemblyBuilder AssemblyBuilder { get; set; } 16 | public ModuleBuilder ModuleBuilder { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ZCompileCore/Contexts/EContexts/EmitStmtContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace ZCompileCore.Analys.EContexts 10 | { 11 | public class EmitStmtContext 12 | { 13 | public EmitMethodContext MethodEmitContext { get;private set; } 14 | 15 | public EmitStmtContext(EmitMethodContext methodContext) 16 | { 17 | MethodEmitContext = methodContext; 18 | } 19 | 20 | public EmitConstructorContext ConstructorEmitContext { get; private set; } 21 | 22 | public EmitStmtContext(EmitConstructorContext constructorContext) 23 | { 24 | ConstructorEmitContext = constructorContext; 25 | } 26 | 27 | public ILGenerator ILout 28 | { 29 | get 30 | { 31 | if (this.MethodEmitContext != null) 32 | return this.MethodEmitContext.ILout; 33 | else 34 | return this.ConstructorEmitContext.ILout; 35 | } 36 | } 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /ZCompileCore/Contexts/MethodContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection.Emit; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Analys.EContexts; 8 | using ZLangRT; 9 | using ZLangRT.Descs; 10 | 11 | namespace ZCompileCore.Analys.AContexts 12 | { 13 | public class MethodContext 14 | { 15 | public ClassContext ClassContext { get; set; } 16 | public TKTProcDesc ProcDesc { get; set; } 17 | public SymbolTable Symbols { get; set; } 18 | public int MethodIndex { get; set; } 19 | 20 | public string Name { get; private set; } 21 | 22 | public MethodContext(ClassContext fileContext,string name) 23 | { 24 | ClassContext = fileContext; 25 | EmitContext = new EmitMethodContext(); 26 | Name = name; 27 | Symbols = fileContext.Symbols.Push(name); 28 | } 29 | 30 | int LoacalVarIndex = -1; 31 | public List LoacalVarList = new List(); 32 | public int CreateLocalVarIndex(string name) 33 | { 34 | LoacalVarIndex++; 35 | LoacalVarList.Add(name); 36 | return LoacalVarIndex; 37 | } 38 | 39 | public EmitMethodContext EmitContext { get; set; } 40 | public Type RetType { get; set; } 41 | public bool RetIsGeneric { get; set; } 42 | 43 | int foreachIndex = -1; 44 | public int CreateForeachIndex() 45 | { 46 | foreachIndex++; 47 | return foreachIndex; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /ZCompileCore/Lex/CodePostion.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ZCompileCore.Lex 8 | { 9 | public class CodePostion 10 | { 11 | public int Line { get; private set; } 12 | public int Col { get; private set; } 13 | 14 | public CodePostion() 15 | { 16 | 17 | } 18 | 19 | public CodePostion(int line,int col) 20 | { 21 | Line = line; 22 | Col = col; 23 | } 24 | 25 | public override string ToString() 26 | { 27 | return string.Format("({0},{1}",Line,Col); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /ZCompileCore/Lex/SourceReader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Diagnostics; 6 | using System.IO; 7 | using ZCompileCore.Tools; 8 | 9 | namespace ZCompileCore.Lex 10 | { 11 | public class SourceReader 12 | { 13 | private string _sourceText = null; 14 | private int pointer = -1; 15 | 16 | private const char END = '\0'; 17 | //private StreamReader _reader = null; 18 | 19 | public SourceReader() 20 | { 21 | 22 | } 23 | 24 | public void ReadFile(string fileName) 25 | { 26 | //_reader = File.OpenText(fileName); 27 | Encoding encoding = TxtFileEncoding.GetEncoding(fileName); 28 | _sourceText = File.ReadAllText(fileName, encoding);//Encoding.Default /*, Encoding.UTF8*/); 29 | pointer = 0; 30 | } 31 | 32 | public void ReadString(string text) 33 | { 34 | _sourceText = text; 35 | pointer = 0; 36 | } 37 | 38 | public void Close() 39 | { 40 | //if (_reader != null) 41 | //{ 42 | // _reader.Close(); 43 | //} 44 | } 45 | 46 | /// 47 | /// 读取下一个字符,而不更改读取器状态或字符源。返回下一个可用字符,而实际上并不从输入流中读取此字符。 48 | /// 一个整数,它表示下一个要读取的字符,或者如果没有更多的可用字符或此流不支持查找,则为 -1。 49 | /// 50 | /// 51 | public int Peek() 52 | { 53 | if (_sourceText != null && pointer < _sourceText.Length) 54 | { 55 | return _sourceText[pointer]; 56 | } 57 | //else if (_reader != null) 58 | //{ 59 | // return _reader.Peek(); 60 | //} 61 | return -1; 62 | } 63 | 64 | public char PeekChar() 65 | { 66 | return (char)(Peek()); 67 | } 68 | 69 | /// 70 | /// 读取输入流中的下一个字符并使该字符的位置提升一个字符。 71 | /// 输入流中的下一个字符,或者如果没有更多的可用字符,则为 -1。默认实现将返回 -1。 72 | /// 73 | /// 74 | public int Read() 75 | { 76 | pointer++; 77 | if (_sourceText != null && pointer < _sourceText.Length-1) 78 | { 79 | char ch = _sourceText[pointer]; 80 | return ch; 81 | } 82 | //else if (_reader != null) 83 | //{ 84 | // return _reader.Read(); 85 | //} 86 | return -1; 87 | } 88 | 89 | public char ReadChar() 90 | { 91 | return (char)(Read()); 92 | } 93 | 94 | public char GetNextChar() 95 | { 96 | if (_sourceText != null && pointer < _sourceText.Length-1) 97 | { 98 | char ch = _sourceText[pointer+1]; 99 | return ch; 100 | } 101 | return END; 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /ZCompileCore/Lex/TokenKind.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ZCompileCore.Lex 7 | { 8 | public enum TokenKind 9 | { 10 | Assign,//= 11 | AssignTo,//=> 12 | LBS,//( 13 | RBS,//) 14 | //LBM,//[ 15 | //RBM,//] 16 | //LBB,//{ 17 | //RBB,//} 18 | //GoesTo,// -> 19 | //Dot,//. 20 | 21 | /// 22 | /// 逗号类型 23 | /// 24 | Comma,//, 25 | 26 | Semi,//; 27 | 28 | ADD,//+ 29 | SUB,//- 30 | MUL,//* 31 | DIV,/// 32 | //Mod,//% 33 | EQ,//== 34 | NE,//!= 35 | GT,//> 36 | GE,//= 37 | LT,//< 38 | LE,//<= 39 | //Inc,//++ 40 | //Dec,//-- 41 | 42 | //Function, 43 | //Var, 44 | //DE,//的 45 | IF,//如果 46 | ELSE,//否则 47 | ELSEIF,//否则如果 48 | //Switch, 49 | //Case, 50 | //For, 51 | //Foreach, 52 | While,//循环当 53 | Foreach,//循环每一个 54 | Catch,//捕捉 55 | //Break, 56 | //Continue, 57 | //Return, 58 | True, 59 | False, 60 | //New, 61 | //Catch, 62 | AND,//并且 63 | OR,//或者 64 | NOT,//! 65 | //Load, 66 | //Using, 67 | 68 | NULL, 69 | LiteralInt, 70 | LiteralFloat, 71 | //LiteralBool, 72 | //LiteralChar, 73 | LiteralString , 74 | 75 | //Function, 76 | //ToolClass, 77 | //Main,//启动 78 | //RightAssign, 79 | 80 | //INT,//整数 81 | //FLOAT,//浮点数 82 | //Bool, 83 | //Char, 84 | //String,//文本 85 | //Object,//事物 86 | Ident, 87 | Caption,//说明 88 | //IdentStmt, 89 | EOF, 90 | //Start, 91 | Colon,//: 92 | //Colond,//::双冒号 93 | //LibType, 94 | //LibMember, 95 | //LibMethod, 96 | Unknow, 97 | Error 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /ZCompileCore/Loads/ExternalGcl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZLangRT.Attributes; 8 | using ZLangRT.Descs; 9 | using ZLangRT.Utils; 10 | 11 | namespace ZCompileCore.Loads 12 | { 13 | public class ExternalGcl : IGcl 14 | { 15 | public Type MType { get;private set; } 16 | public CnEnDict WordDict { get; protected set; } 17 | 18 | public string ShowName { get { return GenericUtil.GetGenericTypeShortName(this.MType); } } 19 | public string RTName { get { return GenericUtil.GetGenericTypeShortName(this.ForType); } } 20 | 21 | public Type ForType 22 | { 23 | get 24 | { 25 | return MType; 26 | } 27 | } 28 | 29 | public ExternalGcl(Type mtype,CnEnDict wordDict) 30 | { 31 | MType = mtype; 32 | WordDict = wordDict; 33 | } 34 | 35 | protected ExternalGcl() 36 | { 37 | 38 | } 39 | 40 | public IGcl CreateNewFor(Type forType) 41 | { 42 | ExternalGcl gcl = new ExternalGcl(); 43 | gcl.MType = forType; 44 | //gcl.ForType = forType; 45 | gcl.WordDict = this.WordDict; 46 | return gcl; 47 | } 48 | 49 | public TKTConstructorDesc SearchConstructor(TKTConstructorDesc desc) 50 | { 51 | return GclUtil.SearchConstructor(desc, this.WordDict, this.ForType); 52 | } 53 | 54 | public ExFieldInfo SearchExField(string name) 55 | { 56 | //return this.ForType.GetField(name); 57 | return GclUtil.SearchExField(name, this.ForType); 58 | } 59 | 60 | public ExPropertyInfo SearchExProperty(string name) 61 | { 62 | return GclUtil.SearchExProperty(name, this.ForType); 63 | } 64 | 65 | public TKTProcDesc SearchProc(TKTProcDesc procDesc) 66 | { 67 | var methodArray = MType.GetMethods(/*BindingFlags.DeclaredOnly*/ ); 68 | foreach (var method in methodArray) 69 | { 70 | if (ReflectionUtil.IsDeclare(MType, method)) 71 | { 72 | ExMethodInfo exMethod = new ExMethodInfo(method, method.DeclaringType == MType); 73 | TKTProcDesc typeProcDesc = ProcDescHelper.CreateProcDesc(exMethod); 74 | if (typeProcDesc.Eq(procDesc)) 75 | { 76 | return typeProcDesc; 77 | } 78 | 79 | } 80 | } 81 | return null; 82 | } 83 | 84 | public override string ToString() 85 | { 86 | return string.Format("ExternalGcl({0})", ShowName); 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /ZCompileCore/Loads/IGcl.cs: -------------------------------------------------------------------------------- 1 |  2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZLangRT.Descs; 9 | 10 | namespace ZCompileCore.Loads 11 | { 12 | public interface IGcl 13 | { 14 | Type ForType { get; } 15 | string ShowName { get; } 16 | string RTName { get; } 17 | ExFieldInfo SearchExField(string name); 18 | ExPropertyInfo SearchExProperty(string name); 19 | TKTProcDesc SearchProc(TKTProcDesc procDesc); 20 | TKTConstructorDesc SearchConstructor(TKTConstructorDesc bracket); 21 | IGcl CreateNewFor(Type forType); 22 | CnEnDict WordDict { get; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ZCompileCore/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的常规信息通过以下 6 | // 特性集控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("ZCompiler")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ZCompiler")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 使此程序集中的类型 18 | // 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 19 | // 则将该类型上的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("7a6ade7b-0f10-4405-aada-a2ca8bb4c660")] 24 | 25 | // 程序集的版本信息由下面四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | [assembly: AssemblyVersion("0.0.4.0")] 32 | [assembly: AssemblyFileVersion("0.0.4.0")] 33 | -------------------------------------------------------------------------------- /ZCompileCore/Reports/CompileException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ZCompileCore.Reports 8 | { 9 | public class CompileException:Exception 10 | { 11 | public CompileException(string message):base(message) 12 | { 13 | 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ZCompileCore/Reports/ProjectCompileResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ZCompileCore.Reports 8 | { 9 | public class ProjectCompileResult 10 | { 11 | public string BinaryFilePath { get; set; } 12 | public List Errors { get; private set; } 13 | public List Warnings { get; private set; } 14 | public List CompiledTypes { get; set; } 15 | 16 | public ProjectCompileResult() 17 | { 18 | Errors = new List(); 19 | Warnings = new List(); 20 | CompiledTypes = new List(); 21 | } 22 | 23 | public bool HasError() 24 | { 25 | return this.Errors.Count>0; 26 | } 27 | 28 | public Type GetCompiledType(string name) 29 | { 30 | foreach (var type in CompiledTypes) 31 | { 32 | if (type.Name == name) 33 | return type; 34 | } 35 | return null; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /ZCompileCore/Symbols/Defs/SymbolArg.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZCompileCore.Reports; 7 | 8 | namespace ZCompileCore.Symbols.Defs 9 | { 10 | public class SymbolArg : InstanceSymbol 11 | { 12 | public Type ArgType { get; set; } 13 | public int ArgIndex { get; set; } 14 | public bool IsGeneric { get; set; } 15 | 16 | public SymbolArg(string name, Type argType,int argIndex,bool isGeneric) 17 | { 18 | this.SymbolName = name; 19 | ArgType = argType; 20 | ArgIndex = argIndex; 21 | IsGeneric = isGeneric; 22 | } 23 | 24 | public override Type DimType 25 | { 26 | get 27 | { 28 | return ArgType; 29 | } 30 | set 31 | { 32 | throw new CompileException("参数的类型不能赋值"); 33 | } 34 | } 35 | 36 | public override bool CanWrite 37 | { 38 | get 39 | { 40 | return true; 41 | } 42 | } 43 | 44 | public override string ToString() 45 | { 46 | return "参数(" + ArgType.Name + ":" + SymbolName + ")"; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /ZCompileCore/Symbols/Defs/SymbolDefClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using ZCompileCore.Analys.AContexts; 9 | using ZCompileCore.Loads; 10 | using ZCompileCore.AST; 11 | using ZCompileCore.AST.Parts; 12 | using ZCompileCore.Tools; 13 | using ZLangRT; 14 | using ZLangRT.Collections; 15 | using ZLangRT.Descs; 16 | 17 | namespace ZCompileCore.Symbols.Defs 18 | { 19 | public class SymbolDefClass : SymbolInfo //SymbolDef//, ITypeSymbol 20 | { 21 | public TKTProcDescDictionary MethodDict { get; private set; } 22 | 23 | public Dictionary PropertyDict { get;private set; } 24 | public TypeBuilder ClassBuilder { get; set; } 25 | public IGcl BaseGcl { get; set; } 26 | public bool IsStatic { get; protected set; } 27 | 28 | //public TKTProcDesc ZeroContructor { get; set; } 29 | 30 | public SymbolDefClass(string name, bool isStatic) 31 | { 32 | this.SymbolName = name; 33 | //MethodDict = new Dictionary(); 34 | MethodDict = new TKTProcDescDictionary(); 35 | PropertyDict = new Dictionary(); 36 | //ConstructorDict = new Dictionary(); 37 | IsStatic = isStatic; 38 | } 39 | 40 | SymbolDefClass parentClass; 41 | public SymbolDefClass(SymbolDefClass parentClass,string name) 42 | { 43 | this.parentClass = parentClass; 44 | this.SymbolName = name; 45 | MethodDict = new TKTProcDescDictionary(); 46 | PropertyDict = new Dictionary(); 47 | } 48 | 49 | public Type GetRealType() 50 | { 51 | return ClassBuilder; 52 | } 53 | 54 | public ExPropertyInfo GetExProperty(string name) 55 | { 56 | if (PropertyDict.ContainsKey(name)) 57 | { 58 | return new ExPropertyInfo( PropertyDict[name].GetProperty(),true); 59 | } 60 | return null; 61 | } 62 | 63 | public bool AddMethod(MethodContext method) 64 | { 65 | if (this.MethodDict.Contains(method.ProcDesc)) 66 | return false; 67 | MethodDict.Add(method.ProcDesc, method); 68 | return true; 69 | } 70 | 71 | public TKTProcDesc SearchProc(/*SymbolTable table, */TKTProcDesc procDesc) 72 | { 73 | TKTProcDesc proc = MethodDict.SearchProc(procDesc); 74 | if(proc!=null) 75 | { 76 | proc.ExMethod = new ExMethodInfo( MethodDict.Get(proc).EmitContext.CurrentMethodBuilder,true); 77 | } 78 | else if (parentClass!=null) 79 | { 80 | proc = parentClass.SearchProc(procDesc); 81 | } 82 | else if (BaseGcl!=null) 83 | { 84 | proc = BaseGcl.SearchProc(procDesc); 85 | } 86 | return proc; 87 | } 88 | 89 | 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /ZCompileCore/Symbols/Defs/SymbolDefField.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | using ZCompileCore.AST; 10 | using ZCompileCore.AST.Parts; 11 | using ZCompileCore.Tools; 12 | using ZLangRT; 13 | 14 | namespace ZCompileCore.Symbols.Defs 15 | { 16 | public class SymbolDefField : InstanceSymbol 17 | { 18 | public Type FieldType { get; private set; } 19 | //public bool IsAssigned { get; set; } 20 | //public FieldBuilder Builder { get; set; } 21 | public bool IsStatic { get; protected set; } 22 | 23 | public SymbolDefField(string name, Type propertyType, bool isStatic) 24 | { 25 | this.SymbolName = name; 26 | FieldType = propertyType; 27 | IsAssigned = true; 28 | IsStatic = isStatic; 29 | } 30 | 31 | FieldInfo Field; 32 | public void SetField(FieldInfo field) 33 | { 34 | Field = field; 35 | } 36 | 37 | public FieldInfo GetField() 38 | { 39 | return Field; 40 | } 41 | 42 | public override Type DimType 43 | { 44 | get{ 45 | return FieldType; 46 | } 47 | set{ 48 | FieldType = value; 49 | } 50 | } 51 | 52 | public override bool CanWrite 53 | { 54 | get 55 | { 56 | return true; 57 | } 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /ZCompileCore/Symbols/Defs/SymbolDefProperty.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Reflection.Emit; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | using ZCompileCore.AST; 10 | using ZCompileCore.AST.Parts; 11 | using ZCompileCore.Tools; 12 | using ZLangRT; 13 | 14 | namespace ZCompileCore.Symbols.Defs 15 | { 16 | public class SymbolDefProperty : InstanceSymbol 17 | { 18 | public Type PropertyType { get; private set; } 19 | //public bool IsAssigned { get; set; } 20 | //public PropertyBuilder Builder { get; set; } 21 | public bool IsStatic { get; protected set; } 22 | 23 | PropertyInfo Property; 24 | public void SetProperty(PropertyInfo property) 25 | { 26 | Property = property; 27 | } 28 | 29 | public PropertyInfo GetProperty() 30 | { 31 | return Property; 32 | } 33 | 34 | public SymbolDefProperty(string name, Type propertyType,bool isStatic) 35 | { 36 | this.SymbolName = name; 37 | PropertyType = propertyType; 38 | IsAssigned = true; 39 | IsStatic = isStatic; 40 | } 41 | 42 | public override Type DimType 43 | { 44 | get 45 | { 46 | return PropertyType; 47 | } 48 | set 49 | { 50 | PropertyType = value; 51 | } 52 | } 53 | 54 | public override bool CanWrite 55 | { 56 | get 57 | { 58 | return true; 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /ZCompileCore/Symbols/Defs/SymbolVar.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection.Emit; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | 9 | namespace ZCompileCore.Symbols.Defs 10 | { 11 | public class SymbolVar : InstanceSymbol 12 | { 13 | //public bool IsAssigned { get; set; } 14 | public int LoacalVarIndex { get; set; } 15 | public LocalBuilder VarBuilder { get; set; } 16 | //public Type DimType { get; set; } 17 | 18 | public SymbolVar(string name) 19 | { 20 | this.SymbolName = name; 21 | } 22 | 23 | public SymbolVar(string name, Type type) 24 | { 25 | this.SymbolName = name; 26 | DimType = type; 27 | } 28 | /* 29 | public override Type GetDimType() 30 | { 31 | return DimType; 32 | }*/ 33 | 34 | public override bool CanWrite 35 | { 36 | get 37 | { 38 | return true; 39 | } 40 | } 41 | 42 | public override string ToString() 43 | { 44 | return "变量(" + DimType.Name + ":" + SymbolName + ")"; 45 | } 46 | 47 | public bool IsInBlock { get; set; } 48 | 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /ZCompileCore/Symbols/Imports/SymbolEnumItem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection.Emit; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Reports; 8 | 9 | 10 | namespace ZCompileCore.Symbols.Imports 11 | { 12 | public class SymbolEnumItem : InstanceSymbol 13 | { 14 | //public SymbolEnumMap MapEnum; 15 | public object EnumValue { get; set; } 16 | 17 | public SymbolEnumItem(/*SymbolEnumMap mapEnum,*/string name,object enumValue) 18 | { 19 | this.SymbolName = name; 20 | //MapEnum = mapEnum; 21 | EnumValue = enumValue; 22 | IsAssigned = true; 23 | } 24 | 25 | public override Type DimType 26 | { 27 | get 28 | { 29 | return EnumValue.GetType(); 30 | } 31 | set 32 | { 33 | throw new CompileException("枚举的类型不能赋值"); 34 | } 35 | } 36 | 37 | public override bool CanWrite 38 | { 39 | get 40 | { 41 | return false; 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /ZCompileCore/Symbols/Imports/SymbolFieldDirect.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Reports; 8 | using ZLangRT.Descs; 9 | 10 | 11 | namespace ZCompileCore.Symbols.Imports 12 | { 13 | public class SymbolFieldDirect: InstanceSymbol 14 | { 15 | public ExFieldInfo ExField { get; set; } 16 | 17 | public SymbolFieldDirect(string name, ExFieldInfo exField) 18 | { 19 | this.SymbolName = name; 20 | ExField = exField; 21 | } 22 | 23 | public override Type DimType 24 | { 25 | get 26 | { 27 | return ExField.Field.FieldType; 28 | } 29 | set 30 | { 31 | throw new CompileException("外部的Field类型不能赋值"); 32 | } 33 | } 34 | 35 | public override bool CanWrite 36 | { 37 | get 38 | { 39 | return true; 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /ZCompileCore/Symbols/Imports/SymbolPropertyDirect.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Reports; 8 | using ZCompileCore.Symbols.Defs; 9 | using ZLangRT.Descs; 10 | 11 | 12 | namespace ZCompileCore.Symbols.Imports 13 | { 14 | public class SymbolPropertyDirect: InstanceSymbol 15 | { 16 | public ExPropertyInfo ExProperty { get; set; } 17 | 18 | public SymbolPropertyDirect(string name, ExPropertyInfo exProperty) 19 | { 20 | this.SymbolName = name; 21 | ExProperty = exProperty; 22 | } 23 | 24 | public override Type DimType 25 | { 26 | get 27 | { 28 | return ExProperty.Property.PropertyType; 29 | } 30 | set 31 | { 32 | throw new CompileException("外部的Field类型不能赋值"); 33 | } 34 | } 35 | 36 | public override bool CanWrite 37 | { 38 | get 39 | { 40 | return ExProperty.Property.CanWrite; 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /ZCompileCore/Symbols/InstanceSymbol.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | using ZLangRT; 9 | 10 | namespace ZCompileCore.Symbols 11 | { 12 | public abstract class InstanceSymbol:SymbolInfo 13 | { 14 | //public abstract Type GetDimType(/*SymbolTable table*/); 15 | public abstract bool CanWrite { get; } 16 | public virtual Type DimType { get; set; } 17 | public bool IsAssigned { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ZCompileCore/Symbols/SymbolInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZCompileCore.Lex; 6 | using System.Reflection.Emit; 7 | 8 | namespace ZCompileCore.Symbols 9 | { 10 | public abstract class SymbolInfo 11 | { 12 | public String SymbolName 13 | { 14 | get; 15 | protected set; 16 | } 17 | } 18 | /* 19 | public abstract class SymbolDef : SymbolInfo 20 | { 21 | //public Type SymbolType { get; set; } 22 | }*/ 23 | /* 24 | public abstract class SymbolImport : SymbolInfo 25 | { 26 | //public abstract Type SymbolType { get; } 27 | }*/ 28 | /* 29 | public abstract class SymbolCommon : SymbolInfo 30 | { 31 | //public Type SymbolType { get; set; } 32 | }*/ 33 | } 34 | -------------------------------------------------------------------------------- /ZCompileCore/Tools/ListHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ZCompileCore.Tools 8 | { 9 | public static class ListHelper 10 | { 11 | public static List> Split(List list,List indexList) 12 | { 13 | List> splitList = new List>(); 14 | if (indexList.Count == 0) 15 | { 16 | splitList.Add(list); 17 | } 18 | else 19 | { 20 | if (First(indexList) != 0) 21 | { 22 | var subs = SubList(list, 0, indexList[0]); 23 | splitList.Add(subs); 24 | } 25 | for (int i = 0; i < indexList.Count - 1; i++) 26 | { 27 | var subs = SubList(list, indexList[i], indexList[i + 1]); 28 | splitList.Add(subs); 29 | } 30 | if (Last(indexList) != list.Count - 1) 31 | { 32 | var subs = SubList(list, 0, indexList[0]); 33 | splitList.Add(subs); 34 | } 35 | } 36 | return splitList; 37 | } 38 | 39 | public static List SubList( List list, int from, int to) 40 | { 41 | List subList = new List(); 42 | if (from == to) return subList; 43 | for (int i = from; i <= to && i < list.Count; i++) 44 | { 45 | subList.Add(list[i]); 46 | } 47 | return subList; 48 | } 49 | 50 | public static T First(List list) 51 | { 52 | return list[0]; 53 | } 54 | 55 | public static T Last(List list) 56 | { 57 | return list[list.Count-1]; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /ZCompileCore/Tools/TextUtil.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ZCompileCore.Tools 8 | { 9 | public static class TextUtil 10 | { 11 | public static bool IsInt(string str) 12 | { 13 | int value = -1; 14 | bool result = int.TryParse(str, out value); 15 | return result; 16 | } 17 | 18 | public static bool IsFloat(string str) 19 | { 20 | float value = -1; 21 | bool result = float.TryParse(str, out value); 22 | return result; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ZCompileCore/Tools/TxtFileEncoding.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace ZCompileCore.Tools 8 | { 9 | public static class TxtFileEncoding 10 | { 11 | /// 12 | /// 取得一个文本文件的编码方式。如果无法在文件头部找到有效的前导符,Encoding.Default将被返回。 13 | /// 14 | /// 文件名。 15 | /// 16 | public static Encoding GetEncoding(string fileName) 17 | { 18 | return GetEncoding(fileName, Encoding.Default); 19 | } 20 | 21 | /// 22 | /// 取得一个文本文件流的编码方式。 23 | /// 24 | /// 文本文件流。 25 | /// 26 | public static Encoding GetEncoding(FileStream stream) 27 | { 28 | return GetEncoding(stream, Encoding.Default); 29 | } 30 | 31 | /// 32 | /// 取得一个文本文件的编码方式。 33 | /// 34 | /// 文件名。 35 | /// 默认编码方式。当该方法无法从文件的头部取得有效的前导符时,将返回该编码方式。 36 | /// 37 | public static Encoding GetEncoding(string fileName, Encoding defaultEncoding) 38 | { 39 | FileStream fs = new FileStream(fileName, FileMode.Open); 40 | Encoding targetEncoding = GetEncoding(fs, defaultEncoding); 41 | fs.Close(); 42 | return targetEncoding; 43 | } 44 | /* 45 | public static bool GetResult(byte[] Value, int index, int judge) 46 | { 47 | return Value.Length > index ? Value[index] == judge : false; 48 | }*/ 49 | 50 | /// 51 | /// 取得一个文本文件流的编码方式。 52 | /// 53 | /// 文本文件流。 54 | /// 默认编码方式。当该方法无法从文件的头部取得有效的前导符时,将返回该编码方式。 55 | /// 56 | public static Encoding GetEncoding(FileStream fStream, Encoding defaultEncoding) 57 | { 58 | Encoding encoding = defaultEncoding; 59 | //FileStream fStream = File.Open(filePath, FileMode.Open); 60 | if (fStream.CanSeek) 61 | { 62 | byte[] data = new byte[4]; 63 | fStream.Read(data, 0, 4); 64 | int markerLength = 0; 65 | switch (data[0]) 66 | { 67 | case 0xef: // UTF8 68 | markerLength = 3; 69 | if (data.Length < 3) 70 | break; 71 | if (data[1] == 0xbb && data[2] == 0xbf) 72 | encoding = Encoding.UTF8; 73 | break; 74 | case 0xfe: // UTF 16 BE 75 | markerLength = 2; 76 | if (data[1] == 0xff) 77 | encoding = Encoding.BigEndianUnicode; 78 | break; 79 | case 0xff: // UTF 16 LE 80 | markerLength = 2; 81 | if (data[1] == 0xfe) 82 | encoding = Encoding.Unicode; 83 | break; 84 | default: 85 | encoding = Encoding.ASCII; 86 | break; 87 | } 88 | } 89 | fStream.Dispose(); 90 | fStream.Close(); 91 | fStream = null; 92 | return encoding; 93 | 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /ZLangRT/Attributes/ZClassAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ZLangRT.Attributes 8 | { 9 | public class ZClassAttribute : Attribute 10 | { 11 | public ZClassAttribute() 12 | { 13 | 14 | } 15 | 16 | public ZClassAttribute(Type baseMappingType) 17 | { 18 | ParentMappingType = baseMappingType; 19 | } 20 | 21 | public Type ParentMappingType { get; private set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ZLangRT/Attributes/ZCodeAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ZLangRT.Attributes 8 | { 9 | [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)] 10 | public class ZCodeAttribute : Attribute 11 | { 12 | public ZCodeAttribute(string procCode) 13 | { 14 | Code = procCode; 15 | } 16 | 17 | public string Code { get; private set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ZLangRT/Attributes/ZMappingAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ZLangRT.Attributes 8 | { 9 | public class ZMappingAttribute : Attribute 10 | { 11 | public ZMappingAttribute(Type type) 12 | { 13 | ForType = type; 14 | } 15 | 16 | public ZMappingAttribute(Type type, Type baseMappingType) 17 | { 18 | ForType = type; 19 | BaseMappingType = baseMappingType; 20 | } 21 | 22 | public Type ForType { get; private set; } 23 | 24 | public Type BaseMappingType { get; private set; } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ZLangRT/Collections/PairDict.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ZLangRT.Collections 8 | { 9 | public class PairDict 10 | { 11 | Dictionary kvdict; 12 | Dictionary vkdict; 13 | 14 | public PairDict() 15 | { 16 | kvdict = new Dictionary(); 17 | vkdict = new Dictionary(); 18 | } 19 | 20 | public bool Add(K k,V v) 21 | { 22 | if (kvdict.ContainsKey(k) || vkdict.ContainsKey(v)) 23 | return false; 24 | kvdict.Add(k, v); 25 | vkdict.Add(v, k); 26 | return true; 27 | } 28 | 29 | public bool Containsk(K k) 30 | { 31 | return kvdict.ContainsKey(k); 32 | } 33 | 34 | public bool Containsv(V v) 35 | { 36 | return vkdict.ContainsKey(v); 37 | } 38 | 39 | public V Getv(K k) 40 | { 41 | return kvdict[k]; 42 | } 43 | 44 | public K Getk(V v) 45 | { 46 | return vkdict[v]; 47 | } 48 | 49 | public int Count() 50 | { 51 | return kvdict.Count; 52 | } 53 | 54 | public List Keys 55 | { 56 | get 57 | { 58 | return kvdict.Keys.ToList(); 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /ZLangRT/Collections/TKTProcDescDictionary.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZLangRT.Descs; 7 | 8 | namespace ZLangRT.Collections 9 | { 10 | public class TKTProcDescDictionary 11 | { 12 | Dictionary dict; 13 | public TKTProcDescDictionary() 14 | { 15 | dict = new Dictionary(); 16 | } 17 | 18 | public void Add(TKTProcDesc procDesc,V v) 19 | { 20 | dict.Add(procDesc, v); 21 | } 22 | 23 | public bool Contains(TKTProcDesc procDesc) 24 | { 25 | foreach (TKTProcDesc key in dict.Keys.ToList()) 26 | { 27 | if (key.Eq(procDesc)) 28 | { 29 | return true; 30 | } 31 | } 32 | return false; 33 | } 34 | 35 | public V Get(TKTProcDesc procDesc) 36 | { 37 | foreach (TKTProcDesc key in dict.Keys.ToList()) 38 | { 39 | if (key.Eq(procDesc)) 40 | { 41 | return dict[key]; 42 | } 43 | } 44 | return default(V); 45 | } 46 | 47 | public TKTProcDesc SearchProc(TKTProcDesc procDesc) 48 | { 49 | foreach (TKTProcDesc key in dict.Keys.ToList()) 50 | { 51 | if (key.Eq(procDesc)) 52 | { 53 | return key; 54 | } 55 | } 56 | return null; 57 | } 58 | 59 | public int Count 60 | { 61 | get 62 | { 63 | return dict.Count; 64 | } 65 | } 66 | 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /ZLangRT/Descs/CnEnDict.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ZLangRT.Descs 8 | { 9 | public abstract class CnEnDict 10 | { 11 | Dictionary ArgNameDict; 12 | 13 | public CnEnDict() 14 | { 15 | ArgNameDict = new Dictionary(); 16 | AddWords(); 17 | } 18 | 19 | public abstract void AddWords(); 20 | 21 | public string Get(string cnword) 22 | { 23 | if (ArgNameDict.ContainsKey(cnword)) return ArgNameDict[cnword]; 24 | else return cnword; 25 | } 26 | 27 | public bool AddSafe(string cn,string en) 28 | { 29 | if (ArgNameDict.ContainsKey(cn)) return false; 30 | ArgNameDict.Add(cn, en); 31 | return true; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /ZLangRT/Descs/ExFieldInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | 7 | namespace ZLangRT.Descs 8 | { 9 | public class ExFieldInfo 10 | { 11 | public FieldInfo Field { get; private set; } 12 | public bool IsSelf { get; private set; } 13 | 14 | public ExFieldInfo(FieldInfo field, bool isSelf) 15 | { 16 | Field = field; 17 | IsSelf = isSelf; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ZLangRT/Descs/ExMethodInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | 7 | namespace ZLangRT.Descs 8 | { 9 | public class ExMethodInfo 10 | { 11 | public MethodInfo Method { get; private set; } 12 | public bool IsSelf { get; private set; } 13 | 14 | public ExMethodInfo(MethodInfo method, bool isSelf) 15 | { 16 | Method = method; 17 | IsSelf = isSelf; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ZLangRT/Descs/ExPropertyInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | 7 | namespace ZLangRT.Descs 8 | { 9 | public class ExPropertyInfo 10 | { 11 | public PropertyInfo Property { get; private set; } 12 | public bool IsSelf { get; private set; } 13 | 14 | public ExPropertyInfo(PropertyInfo property, bool isSelf) 15 | { 16 | Property = property; 17 | IsSelf = isSelf; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ZLangRT/Descs/TKTConstructorDesc.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace ZLangRT.Descs 9 | { 10 | public class TKTConstructorDesc 11 | { 12 | //public List Parts { get; private set; } 13 | public TKTProcBracket Bracket { get; private set; } 14 | 15 | public ConstructorInfo Constructor { get; set; } 16 | //public object Model { get; set; } 17 | 18 | public TKTConstructorDesc() 19 | { 20 | //Parts = new List(); 21 | } 22 | 23 | public TKTConstructorDesc(IEnumerable args) 24 | { 25 | //Parts = new List(); 26 | //Add(args); 27 | Bracket = new TKTProcBracket(args); 28 | } 29 | 30 | public bool Eq(TKTConstructorDesc another) 31 | { 32 | if (this == another) return true; 33 | return EqBrackets(another); 34 | } 35 | 36 | bool EqBrackets(TKTConstructorDesc another) 37 | { 38 | if (this.Bracket == null && another.Bracket == null) return true; 39 | if (this.Bracket != null && another.Bracket != null) return this.Bracket.Eq(another.Bracket); 40 | return false; 41 | } 42 | 43 | public void AdjustBracket(TKTConstructorDesc that) 44 | { 45 | if (this.Bracket == null || that.Bracket == null) return; 46 | that.Bracket = this.Bracket.Adjust(that.Bracket); 47 | } 48 | 49 | public int Count 50 | { 51 | get 52 | { 53 | return (Bracket==null?0:Bracket.Count); 54 | } 55 | } 56 | 57 | public override string ToString() 58 | { 59 | List list = new List(); 60 | if(this.Constructor!=null) 61 | { 62 | list.Add(this.Constructor.DeclaringType.Name); 63 | } 64 | if(this.Bracket!=null) 65 | { 66 | list.Add(Bracket.ToString()); 67 | } 68 | return string.Join("", list); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /ZLangRT/Descs/TKTProcArg.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZLangRT.Utils; 7 | 8 | namespace ZLangRT.Descs 9 | { 10 | public class TKTProcArg 11 | { 12 | public Type ArgType { get; set; } 13 | public string ArgName { get; set; } 14 | public bool IsGenericArg { get; set; } 15 | public object Value { get; set; } 16 | 17 | public TKTProcArg(string argName, Type argType, bool isGenericArg) 18 | { 19 | ArgType = argType; 20 | ArgName = argName; 21 | IsGenericArg = isGenericArg; 22 | } 23 | 24 | public TKTProcArg(Type argType, bool isGenericArg) 25 | { 26 | ArgType = argType; 27 | IsGenericArg = isGenericArg; 28 | } 29 | 30 | static bool eqCondiType(TKTProcArg arg1,TKTProcArg arg2) 31 | { 32 | if (arg1.ArgType == TKTLambda.CondtionType) 33 | { 34 | if (arg2.ArgType == TKTLambda.CondtionType || arg2.ArgType == typeof(bool)) 35 | { 36 | return true; 37 | } 38 | else 39 | { 40 | return false; 41 | } 42 | } 43 | return false; 44 | } 45 | 46 | bool EqArgType(TKTProcArg anthorArg) 47 | { 48 | if (this.ArgType == null || anthorArg.ArgType == null) return true; 49 | if (this.ArgType == TKTLambda.ActionType || anthorArg.ArgType == TKTLambda.ActionType) return true; 50 | if ((this.ArgType == TKTLambda.CondtionType) || (anthorArg.ArgType == TKTLambda.CondtionType)) 51 | { 52 | if (eqCondiType(this, anthorArg) || eqCondiType(anthorArg, this)) 53 | return true; 54 | return false; 55 | } 56 | if (ReflectionUtil.IsNumberType(this.ArgType) && ReflectionUtil.IsNumberType(anthorArg.ArgType)) 57 | { 58 | return (ReflectionUtil.MoreEqNumberType(this.ArgType, anthorArg.ArgType)) ; 59 | } 60 | if (ReflectionUtil.IsExtends(anthorArg.ArgType, this.ArgType)) 61 | { 62 | return true; 63 | } 64 | return false; 65 | } 66 | 67 | public bool Eq(TKTProcArg anthorArg) 68 | { 69 | if (!EqArgType(anthorArg)) return false; 70 | if (anthorArg.ArgName != null && this.ArgName!=null) 71 | { 72 | return anthorArg.ArgName == this.ArgName; 73 | } 74 | if (this.IsGenericArg != anthorArg.IsGenericArg) return false; 75 | return true; 76 | } 77 | 78 | public override string ToString() 79 | { 80 | StringBuilder buff = new StringBuilder(); 81 | buff.Append(ArgType.Name); 82 | if (ArgName != null) 83 | { 84 | buff.Append(":"); 85 | buff.Append(ArgName); 86 | } 87 | return buff.ToString(); 88 | } 89 | 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /ZLangRT/Loads/ExternalGcl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using TKT.RT.Attributes; 8 | using TKT.RT.Descs; 9 | using TKT.RT.Utils; 10 | 11 | namespace TKT.RT.Loads 12 | { 13 | public class ExternalGcl : IGcl 14 | { 15 | public Type MType { get;private set; } 16 | public CnEnDict WordDict { get; protected set; } 17 | 18 | public string ShowName { get {return ReflectionUtil.GetGenericTypeShortName(this.MType); } } 19 | public string RTName { get { return ReflectionUtil.GetGenericTypeShortName(this.ForType); } } 20 | 21 | public Type ForType 22 | { 23 | get 24 | { 25 | return MType; 26 | } 27 | } 28 | 29 | public ExternalGcl(Type mtype,CnEnDict wordDict) 30 | { 31 | MType = mtype; 32 | WordDict = wordDict; 33 | } 34 | 35 | protected ExternalGcl() 36 | { 37 | 38 | } 39 | 40 | public IGcl CreateNewFor(Type forType) 41 | { 42 | ExternalGcl gcl = new ExternalGcl(); 43 | gcl.MType = forType; 44 | //gcl.ForType = forType; 45 | gcl.WordDict = this.WordDict; 46 | return gcl; 47 | } 48 | 49 | public TKTConstructorDesc SearchConstructor(TKTConstructorDesc desc) 50 | { 51 | return GclHelper.SearchConstructor(desc, this.WordDict, this.ForType); 52 | } 53 | 54 | public FieldInfo SearchField(string name) 55 | { 56 | return this.ForType.GetField(name); 57 | } 58 | 59 | public PropertyInfo SearchProperty(string name) 60 | { 61 | return this.ForType.GetProperty(name); 62 | } 63 | 64 | public TKTProcDesc SearchProc(TKTProcDesc procDesc) 65 | { 66 | var methodArray = MType.GetMethods(/*BindingFlags.DeclaredOnly*/ ); 67 | foreach (var method in methodArray) 68 | { 69 | if (ReflectionUtil.IsDeclare(MType, method)) 70 | { 71 | 72 | TKTProcDesc typeProcDesc = ProcDescHelper.CreateProcDesc(method); 73 | if (typeProcDesc.Eq(procDesc)) 74 | { 75 | return typeProcDesc; 76 | } 77 | 78 | } 79 | } 80 | return null; 81 | } 82 | 83 | public override string ToString() 84 | { 85 | return string.Format("ExternalGcl({0})", ShowName); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /ZLangRT/Loads/GclHelper.cs: -------------------------------------------------------------------------------- 1 |  2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using TKT.RT.Attributes; 9 | using TKT.RT.Descs; 10 | using TKT.RT.Utils; 11 | 12 | namespace TKT.RT.Loads 13 | { 14 | public static class GclHelper 15 | { 16 | public static IGcl Load(Type type,CnEnDict wordDict) 17 | { 18 | MappingTypeAttribute mattr = Attribute.GetCustomAttribute(type, typeof(MappingTypeAttribute)) as MappingTypeAttribute; 19 | if (mattr!= null) 20 | { 21 | return new MappingGcl(type,wordDict); 22 | } 23 | 24 | TKTClassAttribute tcAttr = Attribute.GetCustomAttribute(type, typeof(TKTClassAttribute)) as TKTClassAttribute; 25 | if (tcAttr != null) 26 | { 27 | return new TktGcl(type, wordDict); 28 | } 29 | 30 | return null; 31 | } 32 | 33 | 34 | public static TKTConstructorDesc SearchConstructor(TKTConstructorDesc desc, CnEnDict WordDict, Type ForType) 35 | { 36 | TKTConstructorDesc bracket2 = desc; 37 | if (WordDict != null && desc.Bracket.IsNameValue) 38 | { 39 | List args = new List(); 40 | foreach (var arg in desc.Bracket.ListArgs) 41 | { 42 | string newArgName = WordDict.Get(arg.ArgName); 43 | TKTProcArg newArg = new TKTProcArg(newArgName, arg.ArgType, arg.ArgType.IsGenericType); 44 | args.Add(newArg); 45 | } 46 | bracket2 = new TKTConstructorDesc(args); 47 | } 48 | ConstructorInfo[] constructorInfoArray = ForType.GetConstructors(); 49 | foreach (ConstructorInfo ci in constructorInfoArray) 50 | { 51 | if (ci.IsPublic) 52 | { 53 | TKTConstructorDesc bracketCi = ProcDescHelper.CreateProcBracket(ci); 54 | if (bracketCi.Eq(bracket2)) 55 | { 56 | return bracketCi; 57 | } 58 | } 59 | } 60 | return null; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /ZLangRT/Loads/IGcl.cs: -------------------------------------------------------------------------------- 1 |  2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using TKT.RT.Descs; 9 | 10 | namespace TKT.RT.Loads 11 | { 12 | public interface IGcl 13 | { 14 | Type ForType { get; } 15 | string ShowName { get; } 16 | string RTName { get; } 17 | FieldInfo SearchField(string name); 18 | PropertyInfo SearchProperty(string name); 19 | TKTProcDesc SearchProc(TKTProcDesc procDesc); 20 | TKTConstructorDesc SearchConstructor(TKTConstructorDesc bracket); 21 | IGcl CreateNewFor(Type forType); 22 | CnEnDict WordDict { get; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ZLangRT/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的常规信息通过以下 6 | // 特性集控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("ZLangRT")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ZLangRT")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 使此程序集中的类型 18 | // 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 19 | // 则将该类型上的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("f780e002-d703-4003-8b01-6d482483fa92")] 24 | 25 | // 程序集的版本信息由下面四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /ZLangRT/RTException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ZLangRT 7 | { 8 | public class RTException : Exception 9 | { 10 | public RTException() 11 | { 12 | } 13 | 14 | public RTException(string msg) 15 | : base(msg) 16 | { 17 | } 18 | 19 | public RTException(string format, params object[] args) 20 | : base(String.Format(format, args)) 21 | { 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ZLangRT/TKTEnumMap.cs.TXT: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace TKT.RT 9 | { 10 | public abstract class TKTEnumMap 11 | { 12 | public Dictionary DimTable { get; protected set; } 13 | 14 | public TKTEnumMap() 15 | { 16 | DimTable = new Dictionary(); 17 | InitTable(); 18 | } 19 | 20 | public abstract Type TargetType { get; } 21 | protected abstract void InitTable(); 22 | 23 | protected void Reg(string name, string typeName) 24 | { 25 | DimTable.Add(name, typeName); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /ZLangRT/TKTLambda.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ZLangRT 8 | { 9 | public static class TKTLambda 10 | { 11 | public static readonly Type ActionType = typeof(Action); 12 | public static readonly Type CondtionType = typeof(Func); 13 | 14 | public static bool IsFn(Type type) 15 | { 16 | return type == ActionType || type == CondtionType; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ZLangRT/TKTRoot.cs.TXT: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace TKT.RT 8 | { 9 | public abstract class TKTRoot 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ZLangRT/Utils/DebugUtil.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace ZLangRT.Utils 8 | { 9 | public static class DebugUtil 10 | { 11 | public static string StackFramesToString(StackFrame[] stacks) 12 | { 13 | string result = string.Empty; 14 | foreach (StackFrame stack in stacks) 15 | { 16 | result += string.Format("{0} {1} {2} {3}\r\n", stack.GetFileName(), 17 | stack.GetFileLineNumber(), 18 | stack.GetFileColumnNumber(), 19 | stack.GetMethod().ToString()); 20 | } 21 | return result; 22 | } 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ZLangRT/Utils/GenericUtil.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ZLangRT.Utils 7 | { 8 | public static class GenericUtil 9 | { 10 | public static Type GetMakeGenericType(Type type, Type baseType) 11 | { 12 | Type temp = type; 13 | while (true) 14 | { 15 | if (temp == typeof(object)) break; 16 | if (temp.BaseType == null) break; 17 | if (temp.IsGenericType) 18 | { 19 | if (temp.GetGenericTypeDefinition() == baseType) 20 | break; 21 | } 22 | temp = temp.BaseType; 23 | } 24 | return temp; 25 | } 26 | 27 | public static Type[] GetInstanceGenriceType(Type type, Type baseType) 28 | { 29 | if (baseType.IsInterface) 30 | { 31 | Type[] intefaceTypes = type.GetInterfaces(); 32 | foreach (var intfType in intefaceTypes) 33 | { 34 | if (intfType.IsGenericType) 35 | { 36 | if (intfType.GetGenericTypeDefinition() == baseType) 37 | { 38 | Type[] types = intfType.GetGenericArguments(); 39 | return types; 40 | } 41 | } 42 | } 43 | return new Type[] { }; 44 | } 45 | else 46 | { 47 | Type temp = GetMakeGenericType(type, baseType); 48 | if (temp.IsGenericType) 49 | { 50 | Type[] types = type.GetGenericArguments(); 51 | return types; 52 | } 53 | return new Type[] { }; 54 | } 55 | } 56 | 57 | public static string GetGenericTypeShortName(Type type) 58 | { 59 | string name = type.Name; 60 | int index = name.IndexOf("`"); 61 | if (index == -1) return name; 62 | return name.Substring(0, index); 63 | } 64 | 65 | public static int GetGenericTypeArgCount(Type type) 66 | { 67 | string name = type.Name; 68 | int index = name.IndexOf("`"); 69 | if (index == -1) return 0; 70 | string str = name.Substring(index + 1); 71 | return int.Parse(str); 72 | } 73 | 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /ZLangRT/Utils/ProcDescHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZLangRT.Descs; 8 | 9 | namespace ZLangRT.Utils 10 | { 11 | public static class ProcDescHelper 12 | { 13 | public static TKTConstructorDesc CreateProcBracket(ConstructorInfo ci) 14 | { 15 | List args = new List(); 16 | foreach (ParameterInfo param in ci.GetParameters()) 17 | { 18 | TKTProcArg arg = new TKTProcArg(param.Name, param.ParameterType, false); 19 | args.Add(arg); 20 | } 21 | TKTConstructorDesc desc = new TKTConstructorDesc(args); 22 | desc.Constructor=ci; 23 | return desc; 24 | } 25 | 26 | public static TKTProcDesc CreateProcDesc(ExMethodInfo exMethod) 27 | { 28 | var method = exMethod.Method; 29 | TKTProcDesc desc = new TKTProcDesc(); 30 | desc.Add(method.Name); 31 | if(method.IsGenericMethod) 32 | { 33 | foreach (Type paramType in method.GetGenericArguments()) 34 | { 35 | TKTProcArg arg = new TKTProcArg(paramType, true); 36 | desc.Add(arg); 37 | } 38 | } 39 | if (method.GetParameters().Length > 0) 40 | { 41 | List args = new List(); 42 | foreach (ParameterInfo param in method.GetParameters()) 43 | { 44 | TKTProcArg arg = new TKTProcArg(param.Name, param.ParameterType, false); 45 | args.Add(arg); 46 | } 47 | desc.Add(args); 48 | } 49 | desc.ExMethod = exMethod; 50 | return desc; 51 | } 52 | 53 | public static Tuple CreateMethodDesc(TKTProcDesc procDesc) 54 | { 55 | if (procDesc.Parts.Count > 0 && procDesc.Parts[0] is string) 56 | { 57 | string methodName = procDesc.Parts[0] as string; 58 | if (methodName == null) return null; 59 | Type[] types = procDesc.GetArgTypes(0).ToArray(); 60 | return new Tuple(methodName, types); 61 | } 62 | else 63 | { 64 | return null; 65 | } 66 | } 67 | 68 | public static MethodInfo SearchMethod(Type type,TKTProcDesc procDesc) 69 | { 70 | foreach(var method in type.GetMethods()) 71 | { 72 | ExMethodInfo exMethod = new ExMethodInfo(method, method.DeclaringType == type); 73 | TKTProcDesc methodDesc = CreateProcDesc(exMethod); 74 | if (methodDesc.Eq(procDesc)) 75 | return method; 76 | } 77 | return null; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /ZLangRT/ZLangRT.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {C7D13A96-EF60-4325-93B3-42E27575D53B} 8 | Library 9 | Properties 10 | ZLangRT 11 | ZLangRT 12 | v4.0 13 | 512 14 | 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 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 | 76 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using ZCompileCore.Reports; 8 | 9 | namespace ZLangSingleCompiler 10 | { 11 | class Program 12 | { 13 | static void Main(string[] args) 14 | { 15 | string srcFile = "sample/test.zyy"; 16 | srcFile = "sample/对应表例子.zyy"; 17 | srcFile = "sample/你好.zyy"; 18 | srcFile = "sample/列表例子.zyy"; 19 | 20 | if (args.Length > 0) 21 | { 22 | srcFile = args[0]; 23 | } 24 | FileInfo srcFileInfo = new FileInfo(srcFile); 25 | ZSingleCompiler compiler = new ZSingleCompiler(srcFileInfo); 26 | compiler.Compile(); 27 | if (compiler.CompileResult.HasError()) 28 | { 29 | StringBuilder buffBuilder = new StringBuilder(); 30 | buffBuilder.AppendFormat("文件'{0}'有以下错误:\n", srcFile); 31 | foreach (CompileMessage compileMessage in compiler.CompileResult.Errors) 32 | { 33 | if (compileMessage.Line > 0 || compileMessage.Col > 0) 34 | { 35 | buffBuilder.AppendFormat("第{0}行,第{1}列", compileMessage.Line, compileMessage.Col); 36 | } 37 | buffBuilder.AppendFormat("错误:{0}\n", compileMessage.Text); 38 | } 39 | Console.WriteLine(buffBuilder.ToString()); 40 | } 41 | else 42 | { 43 | compiler.Run(); 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的常规信息通过以下 6 | // 特性集控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("ZLangSingleCompiler")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ZLangSingleCompiler")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 使此程序集中的类型 18 | // 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 19 | // 则将该类型上的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("68daca28-e14a-4b78-a3f0-b58d6896fdd4")] 24 | 25 | // 程序集的版本信息由下面四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/ZSingleCompiler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Reflection.Emit; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using ZCompileCore.Builders; 10 | using ZCompileCore.Reports; 11 | using ZLangRT; 12 | using ZLangRT.Utils; 13 | 14 | namespace ZLangSingleCompiler 15 | { 16 | public class ZSingleCompiler 17 | { 18 | public const string ZExt = ".z"; 19 | private ZCompileProjectModel projectModel; 20 | private ZCompileClassModel classModel; 21 | private FileInfo srcFileInfo; 22 | 23 | public ProjectCompileResult CompileResult { get; private set;} 24 | 25 | public ZSingleCompiler(FileInfo zlogoFileInfo) 26 | { 27 | srcFileInfo = zlogoFileInfo; 28 | projectModel = new ZCompileProjectModel(); 29 | classModel = new ZCompileClassModel(); 30 | 31 | projectModel.ProjectRootDirectoryInfo = srcFileInfo.Directory; 32 | projectModel.BinaryFileKind = PEFileKinds.ConsoleApplication; 33 | projectModel.BinarySaveDirectoryInfo = srcFileInfo.Directory; 34 | projectModel.ProjectPackageName = "ZLangSingleFile"; 35 | projectModel.EntryClassName = Path.GetFileNameWithoutExtension(srcFileInfo.FullName); 36 | projectModel.BinaryFileNameNoEx = Path.GetFileNameWithoutExtension(srcFileInfo.FullName); 37 | projectModel.NeedSave = true; 38 | projectModel.AddRefPackage("Z语言系统"); 39 | //projectModel.AddRefDll(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ZLogoEngine.exe"))); 40 | 41 | classModel.SourceFileInfo = srcFileInfo; 42 | /* classModel.PreSourceCode =@" 43 | 44 | 使用包:ZLogoEngine; 45 | 简略使用:颜色,补语控制; 46 | 47 | 属于:绘图窗体; 48 | 49 | ";*/ 50 | projectModel.AddClass(classModel); 51 | } 52 | 53 | public ProjectCompileResult Compile() 54 | { 55 | ZCompileBuilder builder = new ZCompileBuilder(); 56 | ProjectCompileResult result = builder.CompileProject(projectModel); 57 | CompileResult= result; 58 | return result; 59 | } 60 | 61 | public void Run() 62 | { 63 | if (CompileResult == null) 64 | { 65 | Compile(); 66 | } 67 | if (CompileResult == null) 68 | { 69 | return; 70 | } 71 | //Application.EnableVisualStyles(); 72 | //Application.SetCompatibleTextRenderingDefault(false); 73 | //Application.Run(turtleForm); 74 | 75 | if (CompileResult.CompiledTypes.Count > 0) 76 | { 77 | Type type = CompileResult.CompiledTypes[0]; 78 | //TurtleForm turtleForm = ReflectionUtil.NewInstance(type) as TurtleForm; 79 | //turtleForm.Show(); 80 | Invoker.Call(type, "启动"); 81 | } 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/test.zyy: -------------------------------------------------------------------------------- 1 | 简略使用:控制台; 2 | 3 | 过程:启动 4 | //L=整数列表(10,20,30,40,50); 5 | L=文本列表("123","456"); 6 | 换行打印(L的每一个); 7 | L的每一个="000"; 8 | 换行打印(L的每一个); 9 | /*换行; 10 | (L的每一个)显示;*/ 11 | 等待按键; 12 | 13 | 过程:(文本:W)显示 14 | 打印(W); 15 | 16 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/你好.zyy: -------------------------------------------------------------------------------- 1 | 简略使用:控制台,补语控制; 2 | 3 | 过程:启动 4 | (打印(" 你好 "))(3)次; 5 | 等待按键; 6 | 7 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/列表例子.zyy: -------------------------------------------------------------------------------- 1 |  2 | 简略使用:控制台; 3 | 4 | 5 | 过程:打印列表(整数列表:LB) 6 | 循环每一个(LB,E) 7 | 打印(E);打印(" "); 8 | 换行; 9 | 10 | 过程:启动 11 | 12 | A=整数列表(100,200,300,400); 13 | 打印(A的个数); 14 | 换行打印(A第2); 15 | A第1=15; 16 | 换行打印(A第1); 17 | 换行打印("A包含100吗?"+((A)包含(100))); 18 | 换行打印("A包含300吗?"+((A)包含(300))); 19 | (A)插入(250)到第(3); 20 | 换行; 21 | 打印列表(A); 22 | (A)删除(15); 23 | 换行; 24 | 打印列表(A); 25 | 26 | 27 | B=文本列表(); 28 | (B)添加("你好"); 29 | 换行打印("B的个数:"+B的个数); 30 | (B)添加("吗"); 31 | 换行打印("B的个数:"+B的个数); 32 | (B)清空; 33 | 换行打印("B的个数:"+B的个数); 34 | 35 | 等待按键; 36 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/对应表例子.zyy: -------------------------------------------------------------------------------- 1 |  2 | 简略使用:控制台; 3 | 4 | 过程:启动 5 | D=对应表(); 6 | //D=整数文本对应表(); 7 | (D)添加(1,"你好"); 8 | (D)添加(3,"你吃了吗"); 9 | 打印(D第3); 10 | 等待按键; 11 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/循环语句例子.zyy: -------------------------------------------------------------------------------- 1 |  2 | 简略使用:控制台; 3 | 4 | 过程:启动 5 | I=1; 6 | 循环当I<=10 7 | 打印(I); 8 | I=I+1; 9 | 换(2)行; 10 | 11 | L=整数列表(10,20,30,40,50); 12 | 循环每一个(L,C) 13 | 打印(C);换行; 14 | 换行; 15 | 16 | 循环每一个(L,C,S) 17 | 打印("第"+S+"个是:"+C); 18 | 换行; 19 | 换行; 20 | 21 | 等待按键; 22 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/教程例子/异常例子.zyy: -------------------------------------------------------------------------------- 1 | 使用包:Z语言系统; 2 | 简略使用:控制台; 3 | 4 | 过程:启动 5 | (控制台)读取整数(提示语:"请随便输入整数:")=>Z; 6 | 打印("您输入的是"+Z); 7 | 捕捉(异常:YC) 8 | 打印(YC的异常信息); 9 | 换行; 10 | 打印("您输入的不是整数"); 11 | 等待按键; 12 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/教程例子/循环语句例子.zyy: -------------------------------------------------------------------------------- 1 | 简略使用:控制台; 2 | 3 | 4 | 过程:启动 5 | I=1; 6 | 循环当(I<=10) 7 | 打印(I); 8 | I=I+1; 9 | 换(2)行; 10 | 11 | L=整数列表(10,20,30,40,50); 12 | 循环每一个(L,C) 13 | 打印(C);换行; 14 | 换行; 15 | 16 | 循环每一个(L,C,S) 17 | 打印("第"+S+"个是:"+C); 18 | 换行; 19 | 换行; 20 | 21 | 等待按键; 22 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/教程例子/打印你好.zyy: -------------------------------------------------------------------------------- 1 | 属于:唯一类型; 2 | 3 | 过程:启动 4 | (控制台)打印("你好"); 5 | (控制台)等待按键; 6 | 7 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/教程例子/条件语句例子.zyy: -------------------------------------------------------------------------------- 1 |  2 | 简略使用:控制台; 3 | 4 | 过程:启动 5 | (控制台)读取整数(提示语:"请随便输入整数:")=>Z; 6 | 打印("您输入的是"+Z); 7 | 换行; 8 | 如果(Z==1) 9 | 打印("您选择了1方案"); 10 | 否则如果(Z==2) 11 | 打印("您选择了2方案"); 12 | 否则如果(Z==3) 13 | 打印("您选择了3方案"); 14 | 否则 15 | 打印("您选择了错误的方案"); 16 | 换行; 17 | 等待按键; -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/教程例子/游戏状态.zyy: -------------------------------------------------------------------------------- 1 |  2 | 属于:约定类型; 3 | 名称:游戏状态; 4 | 约定: 5 | 开始; 6 | 暂停; 7 | 结束; 8 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/教程例子/过程例子.zyy: -------------------------------------------------------------------------------- 1 |  2 | 简略使用:补语控制,随机数器; 3 | 4 | 5 | //有文字,无参,无补充参数,无结果 过程 6 | 过程:播放提示音 7 | (控制台)响铃; 8 | 9 | //有文字,有一参,无补充参数,无结果 过程 10 | 过程:输出(事物:S) 11 | (控制台)打印(S); 12 | (控制台)换行; 13 | 14 | //有文字,有补充参数,有结果 过程 15 | 过程:生成介于(整数:A,整数:B)的随机整数=>整数 16 | 结果=生成随机数(最小值:A,最大值:B); 17 | 18 | 过程:启动 19 | 播放提示音; 20 | 输出("测试输出过程...."); 21 | 生成介于(1,100)的随机整数=>Z; 22 | 输出("生成的随机数是:"+Z); 23 | (控制台)等待按键; 24 | 25 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/测试如果.zyy: -------------------------------------------------------------------------------- 1 |  2 | 简略使用:控制台; 3 | 4 | 过程:启动 5 | (控制台)读取整数(提示语:"请随便输入整数:")=>Z; 6 | 打印("您输入的是"+Z); 7 | 换行; 8 | 如果Z==1 9 | 打印("您选择了1方案"); 10 | 否则如果Z==2 11 | 打印("您选择了2方案"); 12 | 否则如果Z==3 13 | 打印("您选择了3方案"); 14 | 否则 15 | 打印("您选择了错误的方案"); 16 | 换行; 17 | 等待按键; 18 | 19 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/sample/测试每一个.zyy: -------------------------------------------------------------------------------- 1 | 简略使用:控制台; 2 | 3 | 过程:启动 4 | L=文本列表("1111 ","2222 ","3333 ","4444 "); 5 | 打印(L的每一个); 6 | L的每一个="0000 "; 7 | 换行; 8 | 打印(L的每一个); 9 | 等待按键; 10 | 11 | 过程:(文本:W)显示 12 | 打印(W); 13 | 14 | -------------------------------------------------------------------------------- /ZLangSingleCompiler/项目说明.txt: -------------------------------------------------------------------------------- 1 |  2 | 这是项目是使用ZcomplieCore编译Z语言程序的实例。 3 | 4 | 5 | 在生成的exe文件夹下,打开命令行, 6 | 输入 ZLangSingleCompiler sample\你好.zyy 7 | 就可以编译运行 你好.zyy 8 | 9 | -------------------------------------------------------------------------------- /Z语言接口编写文档 0.3.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TKT2016/ZCompileCore/2e3f15faf092137c1bc04f6b230cf8c3986ce7ce/Z语言接口编写文档 0.3.doc -------------------------------------------------------------------------------- /Z语言教程.chm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TKT2016/ZCompileCore/2e3f15faf092137c1bc04f6b230cf8c3986ce7ce/Z语言教程.chm -------------------------------------------------------------------------------- /Z语言系统/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的常规信息通过以下 6 | // 特性集控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("TKT系统")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("TKT系统")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 使此程序集中的类型 18 | // 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 19 | // 则将该类型上的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("cbb05963-8998-4752-b804-065a018054e5")] 24 | 25 | // 程序集的版本信息由下面四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Z语言系统/VOID.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZMapping(typeof(void))] 11 | public class VOID 12 | { 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Z语言系统/Z语言系统.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {8E893D3F-CF9C-4B45-B28C-06CA6E0436BE} 8 | Library 9 | Properties 10 | Z语言系统 11 | Z语言系统 12 | v4.0 13 | 512 14 | 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 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 | {bdbcdf64-058a-4f6f-a0d9-f74b4f564dd2} 67 | ZLangRT 68 | 69 | 70 | 71 | 78 | -------------------------------------------------------------------------------- /Z语言系统/事物.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZLangRT; 7 | using ZLangRT.Attributes; 8 | 9 | namespace Z语言系统 10 | { 11 | [ZMapping(typeof(object))] 12 | public abstract class 事物 13 | { 14 | [ZCode("生成文本形式")] 15 | public abstract override string ToString(); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Z语言系统/判断符.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZMapping(typeof(bool))] 11 | public class 判断符 12 | { 13 | public static string ToText(bool b) 14 | { 15 | return b ? "是" : "否"; 16 | } 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Z语言系统/可运行条件.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZMapping(typeof(Func))] 11 | public class 可运行条件 12 | { 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Z语言系统/可运行语句.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZMapping(typeof(Action))] 11 | public class 可运行语句 12 | { 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Z语言系统/对应表.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZClass] 11 | public class 对应表 12 | { 13 | Dictionary dict; 14 | 15 | public 对应表() 16 | { 17 | dict = new Dictionary (); 18 | } 19 | 20 | int getIndex(int i) 21 | { 22 | if(i>0) 23 | { 24 | return i - 1; 25 | } 26 | else if(i<0) 27 | { 28 | return i + dict.Count; 29 | } 30 | else 31 | { 32 | throw new Exception("列表的索引不能为0"); 33 | } 34 | } 35 | 36 | public V this[K k] 37 | { 38 | get 39 | { 40 | return dict[k]; 41 | } 42 | set 43 | { 44 | dict[k] = value; 45 | } 46 | } 47 | 48 | public int 个数 49 | { 50 | get 51 | { 52 | return this.dict.Count; 53 | } 54 | } 55 | 56 | public void 添加(K k,V v) 57 | { 58 | dict.Add(k,v); 59 | } 60 | 61 | public bool 删除(K k) 62 | { 63 | if (!dict.ContainsKey(k)) return false; 64 | dict.Remove(k); 65 | return true; 66 | } 67 | 68 | public void 清空() 69 | { 70 | dict.Clear(); 71 | } 72 | 73 | [ZCode("判断存在(K:k)")] 74 | public bool 判断存在(K k) 75 | { 76 | return dict.ContainsKey(k); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Z语言系统/常用数学函数.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZClass] 11 | public class 常用数学函数 //: TKTSimpleClass 12 | { 13 | public static float SIN(float a) 14 | { 15 | return (float)Math.Sin(a); 16 | } 17 | 18 | public static float COS(float a) 19 | { 20 | return (float)Math.Cos(a); 21 | } 22 | 23 | public static float TAN(float a) 24 | { 25 | return (float)Math.Tan(a); 26 | } 27 | 28 | public static int ABS(int a) 29 | { 30 | return (int)Math.Abs(a); 31 | } 32 | 33 | public static float ABS(float a) 34 | { 35 | return Math.Abs(a); 36 | } 37 | 38 | /* 39 | public static float SIN(float a) 40 | { 41 | return (float)Math.C(a); 42 | } 43 | 44 | public static float SIN(float a) 45 | { 46 | return (float)Math.Sin(a); 47 | } 48 | 49 | public static float SIN(float a) 50 | { 51 | return (float)Math.Sin(a); 52 | } 53 | 54 | public static float SIN(float a) 55 | { 56 | return (float)Math.Sin(a); 57 | }*/ 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Z语言系统/常用辅助.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | using ZLangRT; 8 | using ZLangRT.Attributes; 9 | 10 | namespace Z语言系统 11 | { 12 | [ZClass] 13 | public static class 常用辅助 14 | { 15 | [ZCode("执行(可运行语句:act)")] 16 | public static void 执行(Action act) 17 | { 18 | act(); 19 | } 20 | 21 | [ZCode("(可运行语句:act)(int:times)次")] 22 | public static void 执行_次(Action act, int times) 23 | { 24 | for(int i=0;i condition) 32 | { 33 | while(true) 34 | { 35 | if (!condition()) 36 | act(); 37 | } 38 | } 39 | 40 | public static bool 非(bool b) 41 | { 42 | return !b; 43 | } 44 | 45 | [ZCode("系统创建新(类型:T)")] 46 | public static T CreateNew() 47 | { 48 | Type type = typeof(T); 49 | try 50 | { 51 | object obj = System.Activator.CreateInstance(type); 52 | return (T)obj; 53 | } 54 | catch (Exception ex) 55 | { 56 | throw new RTException("类型" + type.Name + "创建过程要求的参数个数不为0"); 57 | } 58 | } 59 | 60 | [ZCode("暂停(int:t)毫秒")] 61 | public static void 暂停(int t) 62 | { 63 | Thread.Sleep(t); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Z语言系统/异常.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZMapping(typeof(Exception))] 11 | public abstract class 异常 12 | { 13 | [ZCode("异常信息")] 14 | public string Message { get; set; } 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Z语言系统/控制台.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZMapping(typeof(Console))] 11 | public abstract class 控制台 12 | { 13 | [ZCode("响铃")] 14 | public abstract void Beep(); 15 | 16 | [ZCode("换行")] 17 | public abstract void WriteLine(); 18 | 19 | [ZCode("换(int:k)行")] 20 | public static void ReadLine(int k) 21 | { 22 | for (int i = 0; i < k; i++) 23 | { 24 | Console.WriteLine(); 25 | } 26 | } 27 | 28 | [ZCode("读取文本")] 29 | public abstract string ReadLine(); 30 | 31 | 32 | [ZCode("等待按键")] 33 | public abstract void ReadKey(); 34 | 35 | public static int 读取整数() 36 | { 37 | string str = Console.ReadLine(); 38 | int value = int.Parse(str); 39 | return value; 40 | } 41 | 42 | public static int 读取整数(string 提示语) 43 | { 44 | Console.Write(提示语); 45 | return 读取整数(); 46 | } 47 | 48 | public static float 读取数字() 49 | { 50 | string str = Console.ReadLine(); 51 | float value = float.Parse(str); 52 | return value; 53 | } 54 | 55 | public static float 读取数字(string 提示语) 56 | { 57 | Console.Write(提示语); 58 | return 读取数字(); 59 | } 60 | 61 | public static string 读取文本(string 提示语) 62 | { 63 | Console.Write(提示语); 64 | return Console.ReadLine(); 65 | } 66 | 67 | public static void 打印(object obj) 68 | { 69 | if (obj is bool) 70 | { 71 | bool b = (bool)obj; 72 | Console.Write(判断符.ToText(b)); 73 | } 74 | else 75 | { 76 | Console.Write(obj); 77 | } 78 | } 79 | 80 | public static void 换行打印(object obj) 81 | { 82 | Console.WriteLine(); 83 | 打印(obj); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Z语言系统/整数.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZMapping(typeof(int))] 11 | public class 整数 12 | { 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Z语言系统/文本.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZMapping(typeof(string))] 11 | public class 文本 12 | { 13 | [ZCode("长度")] 14 | public int Length { get; set; } 15 | 16 | [ZCode("(string:str)以(string:value)结尾")] 17 | public static bool 以_结尾(string str,string value) 18 | { 19 | return str.EndsWith(value, StringComparison.OrdinalIgnoreCase); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Z语言系统/时间日期.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZMapping(typeof(DateTime))] 11 | public class 时间日期 12 | { 13 | [ZCode("年")] 14 | public int Year { get; set; } 15 | 16 | [ZCode("月")] 17 | public int Month { get; set; } 18 | 19 | [ZCode("日")] 20 | public int Day { get; set; } 21 | 22 | [ZCode("时")] 23 | public int Hour { get; set; } 24 | 25 | [ZCode("分")] 26 | public int Minute { get; set; } 27 | 28 | [ZCode("秒")] 29 | public int Second { get; set; } 30 | 31 | [ZCode("毫秒")] 32 | public int Millisecond { get; set; } 33 | 34 | [ZCode("(DateTime:dt1)大于(DateTime:dt2)")] 35 | public static bool 大于(DateTime dt1,DateTime dt2) 36 | { 37 | return dt1 > dt2; 38 | } 39 | 40 | [ZCode("(DateTime:dt1)等于(DateTime:dt2)")] 41 | public static bool 等于(DateTime dt1, DateTime dt2) 42 | { 43 | return dt1 == dt2; 44 | } 45 | 46 | [ZCode("(DateTime:dt1)小于(DateTime:dt2)")] 47 | public static bool 小于(DateTime dt1, DateTime dt2) 48 | { 49 | return dt1 < dt2; 50 | } 51 | 52 | [ZCode("(DateTime:dt)加(int:ms)毫秒")] 53 | public static DateTime 加毫秒(DateTime dt, int ms) 54 | { 55 | return dt.AddMilliseconds(ms); 56 | } 57 | 58 | [ZCode("(DateTime:dt)加(int:s)秒")] 59 | public static DateTime 加秒(DateTime dt, int s) 60 | { 61 | return dt.AddSeconds(s); 62 | } 63 | 64 | [ZCode("(DateTime:dt)加(int:m)分钟")] 65 | public static DateTime 加分(DateTime dt, int m) 66 | { 67 | return dt.AddMinutes(m); 68 | } 69 | 70 | [ZCode("(DateTime:dt)加(int:h)小时")] 71 | public static DateTime 加小时(DateTime dt, int h) 72 | { 73 | return dt.AddHours(h); 74 | } 75 | 76 | [ZCode("(DateTime:dt)加(int:d)天")] 77 | public static DateTime 加天(DateTime dt, int d) 78 | { 79 | return dt.AddDays(d); 80 | } 81 | 82 | [ZCode("(DateTime:dt)加(int:d)天(int:h)小时(int:m)分钟(int:s)秒")] 83 | public static DateTime 加天时分秒(DateTime dt, int d,int h,int m,int s) 84 | { 85 | DateTime d2= dt.AddDays(d); 86 | d2 = d2.AddHours(h); 87 | d2 = d2.AddMinutes(m); 88 | d2 = d2.AddSeconds(s); 89 | return d2; 90 | } 91 | 92 | [ZCode("(DateTime:dt)转化为文本")] 93 | public static string 转化为文本(DateTime dt) 94 | { 95 | return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); 96 | } 97 | 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /Z语言系统/时间日期辅助.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading; 6 | using ZLangRT; 7 | using ZLangRT.Attributes; 8 | 9 | namespace Z语言系统 10 | { 11 | [ZClass] 12 | public class 时间日期辅助 13 | { 14 | [ZCode("现在时间")] 15 | public static DateTime 现在时间() 16 | { 17 | return DateTime.Now; 18 | } 19 | 20 | [ZCode("计算(DateTime:dt)星期几")] 21 | public static int 计算星期几(DateTime dt) 22 | { 23 | DayOfWeek weekday = dt.DayOfWeek; 24 | if(weekday== DayOfWeek.Sunday) 25 | { 26 | return 7; 27 | } 28 | else 29 | { 30 | return (int)weekday; 31 | } 32 | } 33 | 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Z语言系统/浮点数.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZMapping(typeof(float))] 11 | public abstract class 浮点数 12 | { 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Z语言系统/类型辅助.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ZLangRT; 7 | using ZLangRT.Attributes; 8 | using ZLangRT.Descs; 9 | 10 | namespace Z语言系统 11 | { 12 | [ZClass] 13 | public static class 类型辅助 14 | { 15 | [ZCode("(object:obj)是(类型:T)")] 16 | public static bool 是(object obj) 17 | { 18 | return (obj is T); 19 | } 20 | 21 | [ZCode("(object:obj)强制转换为(类型:T)")] 22 | public static T 强制转换(object obj) 23 | { 24 | return (T)(obj); 25 | } 26 | 27 | [ZCode("创建一个新的(类型:T)")] 28 | public static T 创建() 29 | { 30 | Type type = typeof(T); 31 | object obj = Activator.CreateInstance(type); 32 | return (T)obj; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Z语言系统/补语控制.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | using ZLangRT; 8 | using ZLangRT.Attributes; 9 | 10 | namespace Z语言系统 11 | { 12 | [ZClass] 13 | public static class 补语控制 14 | { 15 | [ZCode("执行(可运行语句:act)")] 16 | public static void 执行(Action act) 17 | { 18 | act(); 19 | } 20 | 21 | [ZCode("(可运行语句:act)(int:times)次")] 22 | public static void 执行_次(Action act, int times) 23 | { 24 | for(int i=0;i condition) 33 | { 34 | while(true) 35 | { 36 | if (!condition()) 37 | act(); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Z语言系统/随机数器.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ZLangRT; 6 | using ZLangRT.Attributes; 7 | 8 | namespace Z语言系统 9 | { 10 | [ZClass] 11 | public static class 随机数器 12 | { 13 | static Random rnd = new Random(); 14 | 15 | public static int 生成随机数() 16 | { 17 | return rnd.Next(); 18 | } 19 | /* 20 | public static int 生成随机数(int 最小值,int 最大值) 21 | { 22 | return rnd.Next(最小值, 最大值); 23 | }*/ 24 | 25 | public static int 生成随机数(int 最小值, int 最大值) 26 | { 27 | int x= rnd.Next((int)最小值, (int)最大值); 28 | //Console.WriteLine("最小值=" + 最小值 + ",最大值:" + 最大值+",结果="+x); 29 | return x; 30 | } 31 | 32 | public static int 生成随机数(float 最小值, float 最大值) 33 | { 34 | int x = rnd.Next((int)最小值, (int)最大值); 35 | return x; 36 | } 37 | } 38 | } 39 | --------------------------------------------------------------------------------