├── .gitignore
├── Readme.md
├── Resource
├── Antlr4.Runtime.dll
└── nunit.framework.dll
├── TinyScript.UnitTest
├── ExpressionJitCompilerTest.cs
├── ExpressionTest.cs
├── MockChannel.cs
├── MockIlBuilder.cs
├── Properties
│ └── AssemblyInfo.cs
├── ScriptCompilerTest.cs
├── ScriptTest.cs
├── TestBase.cs
└── TinyScript.UnitTest.csproj
├── TinyScript.sln
├── TinyScript.userprefs
└── TinyScript
├── App.config
├── Builder
├── BooleanOpBuilder.cs
├── DecimalOpBuilder.cs
├── IlBuilder.cs
├── OpBuilder.cs
└── StringOpBuilder.cs
├── Channel.cs
├── CompilerVisitor.cs
├── CoreHelper.cs
├── InterpreterVisitor.cs
├── JitBuilder.cs
├── Parser
├── TinyScript.g4
├── TinyScript.tokens
├── TinyScriptBaseVisitor.cs
├── TinyScriptLexer.cs
├── TinyScriptLexer.tokens
├── TinyScriptParser.cs
└── TinyScriptVisitor.cs
├── ParserRuleContextException.cs
├── Program.cs
├── Properties
└── AssemblyInfo.cs
├── Runner.cs
├── Saveable.cs
└── TinyScript.csproj
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | TinyScript/obj
3 | TinyScript/bin
4 | TinyScript.UnitTest/obj
5 | TinyScript.UnitTest/bin
6 | /.vs/TinyScript/v14/*.suo
7 |
8 | *.userprefs
9 |
--------------------------------------------------------------------------------
/Readme.md:
--------------------------------------------------------------------------------
1 | TinyScript
2 | ==========
3 |
4 | TinyScript 是使用 ANTLR 4.5.2 编写的 C# 版本的简单脚本引擎的例子。
--------------------------------------------------------------------------------
/Resource/Antlr4.Runtime.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lifeng-Liang/TinyScript/2fb886a197fc523a53a1b488e4760ee0bf42cd35/Resource/Antlr4.Runtime.dll
--------------------------------------------------------------------------------
/Resource/nunit.framework.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lifeng-Liang/TinyScript/2fb886a197fc523a53a1b488e4760ee0bf42cd35/Resource/nunit.framework.dll
--------------------------------------------------------------------------------
/TinyScript.UnitTest/ExpressionJitCompilerTest.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 |
3 | namespace TinyScript.UnitTest
4 | {
5 | [TestFixture]
6 | public class ExpressionJitCompilerTest : ExpressionTest
7 | {
8 | protected override void AssertIs(string script, string exp)
9 | {
10 | var builder = new MockIlBuilder();
11 | var r = new Runner(new CompilerVisitor(builder));
12 | r.Run(script);
13 | builder.GenType.RunMain();
14 | AssertIs(exp);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/TinyScript.UnitTest/ExpressionTest.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 |
3 | namespace TinyScript.UnitTest
4 | {
5 | [TestFixture]
6 | public class ExpressionTest : TestBase
7 | {
8 | [Test]
9 | public void TestLiteral()
10 | {
11 | // decimal
12 | AssertIs("print(123);", "123");
13 | AssertIs("print(123_456);", "123456");
14 | AssertIs("print(123_456.789_012);", "123456.789012");
15 | // bool
16 | AssertIs("print(true);", "True");
17 | AssertIs("print(false);", "False");
18 | // string
19 | AssertIs(@"print(""this is a string."");", "this is a string.");
20 | }
21 |
22 | [Test, ExpectedException(typeof(ParserRuleContextException), ExpectedMessage = "[1,8] Cannot convert type [Boolean] to [Decimal].")]
23 | public void TestShouldNotAssignBoolValueToDecimalVariable()
24 | {
25 | AssertIs("decimal n = true;", "");
26 | }
27 |
28 | [Test, ExpectedException(typeof(ParserRuleContextException), ExpectedMessage = "[1,0] Variable [n] not defined.")]
29 | public void TestVariableNotDefined()
30 | {
31 | AssertIs("n = true;", "");
32 | }
33 |
34 | [Test, ExpectedException(typeof(ParserRuleContextException), ExpectedMessage = "[1,8] Use of undeclared identifier [y].")]
35 | public void TestVariableNotDefinedInExpression()
36 | {
37 | AssertIs("var x = y + 2;", "");
38 | }
39 |
40 | [Test, ExpectedException(typeof(ParserRuleContextException), ExpectedMessage = "[1,8] Cannot do operation between [Boolean] and [Decimal].")]
41 | public void TestCompareDifferentTypes()
42 | {
43 | AssertIs("var x = true == 123;", "");
44 | }
45 |
46 | [Test, ExpectedException(typeof(ParserRuleContextException), ExpectedMessage = "[1,8] Variable [y] already defined.")]
47 | public void TestCompareRedefineVariable()
48 | {
49 | AssertIs("var y=2;var y=1;", "");
50 | }
51 |
52 | [Test]
53 | public void TestExpression()
54 | {
55 | // decimal
56 | AssertIs("print( 3 > 4 );", "False");
57 | AssertIs("print( 3 > 3 );", "False");
58 | AssertIs("print( 3 < 4 );", "True");
59 | AssertIs("print( 3 < 3 );", "False");
60 | AssertIs("print( 3 >= 3 );", "True");
61 | AssertIs("print( 3 >= 4 );", "False");
62 | AssertIs("print( 3 <= 3 );", "True");
63 | AssertIs("print( 3 <= 4 );", "True");
64 | // bool
65 | AssertIs("print( true == true );", "True");
66 | AssertIs("print( true != true );", "False");
67 | AssertIs("print( true == false );", "False");
68 | AssertIs("print( true != false );", "True");
69 | AssertIs("print( true && true );", "True");
70 | AssertIs("print( true && false );", "False");
71 | AssertIs("print( true || true );", "True");
72 | AssertIs("print( true || false );", "True");
73 | AssertIs("print( false && false );", "False");
74 | AssertIs("print( false || false );", "False");
75 | // string
76 | AssertIs(@"print( ""123"" == ""123"" );", "True");
77 | AssertIs(@"print( ""123"" == ""1230"" );", "False");
78 | AssertIs(@"print( ""123"" != ""123"" );", "False");
79 | AssertIs(@"print( ""123"" != ""1230"" );", "True");
80 | // caculate
81 | AssertIs(@"print( 123 - 23);", "100");
82 | AssertIs(@"print( 3 + 3 * 5 / 2 );", "10.5");
83 | AssertIs(@"print( (3 + 3) * 5 / 2 );", "15");
84 | AssertIs(@"print( ""result : "" + 123);", "result : 123");
85 | AssertIs(@"print( 123.45 - 1.45 );", "122.00");
86 | AssertIs(@"print( 123.45 - 0.45 );", "123.00");
87 | }
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/TinyScript.UnitTest/MockChannel.cs:
--------------------------------------------------------------------------------
1 | using System.Text;
2 |
3 | namespace TinyScript.UnitTest
4 | {
5 | public class MockChannel : Channel
6 | {
7 | public static readonly MockChannel Instance = new MockChannel();
8 |
9 | public readonly StringBuilder Cache = new StringBuilder();
10 |
11 | private MockChannel()
12 | {
13 | }
14 |
15 | public override void Print(string msg)
16 | {
17 | Cache.AppendLine(msg);
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/TinyScript.UnitTest/MockIlBuilder.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Reflection.Emit;
3 |
4 | namespace TinyScript.UnitTest
5 | {
6 | public class MockIlBuilder : JitBuilder
7 | {
8 | private static readonly MethodInfo _print = typeof(MockIlBuilder).GetMethod("Print");
9 |
10 | public override void EmitPrint()
11 | {
12 | Builder.Emit(OpCodes.Call, _print);
13 | }
14 |
15 | public static void Print(string msg)
16 | {
17 | MockChannel.Instance.Print(msg);
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/TinyScript.UnitTest/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("TinyScript.UnitTest")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("TinyScript.UnitTest")]
13 | [assembly: AssemblyCopyright("Copyright © 2016")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("4a957768-4978-4268-8fed-574d47ad9698")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/TinyScript.UnitTest/ScriptCompilerTest.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 |
3 | namespace TinyScript.UnitTest
4 | {
5 | [TestFixture]
6 | public class ScriptCompilerTest : TestScript
7 | {
8 | protected override void AssertIs(string script, string exp)
9 | {
10 | var builder = new MockIlBuilder();
11 | var r = new Runner(new CompilerVisitor(builder));
12 | r.Run(script);
13 | var main = builder.GenType.GetMethod("Main");
14 | main.Invoke(null, new object[0]);
15 | AssertIs(exp);
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/TinyScript.UnitTest/ScriptTest.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 |
3 | namespace TinyScript.UnitTest
4 | {
5 | [TestFixture]
6 | public class TestScript : TestBase
7 | {
8 | [Test]
9 | public void TestAssianDeclareAndIf()
10 | {
11 | var script = @"var n = 1.5 + (2 + 3) * 2;
12 | print(n);
13 | print(3+4);
14 | print(true);
15 | print(1+2*n);
16 | var x = true;
17 | print(x);
18 | print(x == false);
19 | print(n > 5);
20 | print(n > 105);
21 |
22 | if( n > 18 ) {
23 | print("" n > 18"");
24 | } else {
25 | print("" n <= 18"");
26 | }
27 |
28 | if( n > 5 )
29 | {
30 | print(""n is greater than 5 !!!"");
31 | }
32 |
33 | print(true);
34 |
35 | print(""test ok aaaa!"");
36 |
37 | print(""The value of n is : "" + n);
38 | print(2 + 3 - 1 - 5 * 3 / 2 - -8);
39 | print(!(3>4));
40 | print((2+3)*2);
41 | print(true && true);
42 | print(true && false);
43 | print(true || true);
44 | bool newValue = true || false;
45 | print(newValue);
46 | print( 3 != 2 );
47 | print( 3 != 3 );
48 | print( true == true );
49 | print( true == false );
50 | print( ""abc"" == ""abc"" );
51 | print( ""abc"" == ""abc1"" );
52 | print( 3 < 4 );
53 | print( 3 >= 3 );
54 | ";
55 | AssertIs(script, @"11.5
56 | 7
57 | True
58 | 24.0
59 | True
60 | False
61 | True
62 | False
63 | n <= 18
64 | n is greater than 5 !!!
65 | True
66 | test ok aaaa!
67 | The value of n is : 11.5
68 | 4.5
69 | True
70 | 10
71 | True
72 | False
73 | True
74 | True
75 | True
76 | False
77 | True
78 | False
79 | True
80 | False
81 | True
82 | True");
83 | }
84 |
85 | [Test]
86 | public void TestSum1to100()
87 | {
88 | var script = @"
89 | var sum = 0, i = 1;
90 | while(i<=100)
91 | {
92 | sum = sum + i;
93 | i = i + 1;
94 | }
95 |
96 | print(""sum 1 to 100 is : "" + sum);";
97 | AssertIs(script, "sum 1 to 100 is : 5050");
98 | }
99 |
100 | [Test]
101 | public void TestSum1to100ByDoWhile()
102 | {
103 | var script = @"
104 | decimal sum = 0, i = 1;
105 | do
106 | {
107 | sum = sum + i;
108 | i = i + 1;
109 | } while (i<=100);
110 |
111 | string msg = ""sum 1 to 100 is : "" + sum;
112 | print(msg);";
113 | AssertIs(script, "sum 1 to 100 is : 5050");
114 | }
115 |
116 | [Test]
117 | public void TestFib()
118 | {
119 | var script = @"var fib0 = 1;
120 | var fib1 = 1;
121 | var temp = 0;
122 | for (var i = 1; i <= 10; i=i+1 ) {
123 | temp = fib1;
124 | fib1 = fib0 + fib1;
125 | fib0 = temp;
126 | print(fib1);
127 | }
128 | ";
129 | AssertIs(script, @"2
130 | 3
131 | 5
132 | 8
133 | 13
134 | 21
135 | 34
136 | 55
137 | 89
138 | 144");
139 | }
140 |
141 | [Test]
142 | public void TestNestLoop()
143 | {
144 | var script = @"var n = """";
145 | for(var y=0; y<3; y=y+1) {
146 | for(var x=0; x<3; x=x+1) {
147 | n = n + x + y;
148 | }
149 | }
150 | print(n);";
151 | AssertIs(script, "001020011121021222");
152 | }
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/TinyScript.UnitTest/TestBase.cs:
--------------------------------------------------------------------------------
1 | using NUnit.Framework;
2 |
3 | namespace TinyScript.UnitTest
4 | {
5 | public class TestBase
6 | {
7 | [SetUp]
8 | public void SetUp()
9 | {
10 | MockChannel.Instance.Cache.Clear();
11 | }
12 |
13 | protected void AssertIs(string exp)
14 | {
15 | var expect = (exp + "\r\n").Replace("\r", "");
16 | var act = MockChannel.Instance.Cache.ToString().Replace("\r", "");
17 | Assert.AreEqual(expect, act);
18 | MockChannel.Instance.Cache.Clear();
19 | }
20 |
21 | protected virtual void AssertIs(string script, string exp)
22 | {
23 | var r = new Runner(new InterpreterVisitor(MockChannel.Instance));
24 | r.Run(script);
25 | AssertIs(exp);
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/TinyScript.UnitTest/TinyScript.UnitTest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {4A957768-4978-4268-8FED-574D47AD9698}
8 | Library
9 | Properties
10 | TinyScript.UnitTest
11 | TinyScript.UnitTest
12 | 512
13 |
14 |
15 | true
16 | full
17 | false
18 | bin\Debug\
19 | DEBUG;TRACE
20 | prompt
21 | 4
22 |
23 |
24 | pdbonly
25 | true
26 | bin\Release\
27 | TRACE
28 | prompt
29 | 4
30 |
31 |
32 |
33 | ..\Resource\nunit.framework.dll
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | ..\Resource\Antlr4.Runtime.dll
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | {808CCFA6-F4A7-4295-B4FF-C364C6EAA8E1}
59 | TinyScript
60 |
61 |
62 |
63 |
70 |
--------------------------------------------------------------------------------
/TinyScript.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.23107.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyScript", "TinyScript\TinyScript.csproj", "{808CCFA6-F4A7-4295-B4FF-C364C6EAA8E1}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyScript.UnitTest", "TinyScript.UnitTest\TinyScript.UnitTest.csproj", "{4A957768-4978-4268-8FED-574D47AD9698}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {808CCFA6-F4A7-4295-B4FF-C364C6EAA8E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {808CCFA6-F4A7-4295-B4FF-C364C6EAA8E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {808CCFA6-F4A7-4295-B4FF-C364C6EAA8E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {808CCFA6-F4A7-4295-B4FF-C364C6EAA8E1}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {4A957768-4978-4268-8FED-574D47AD9698}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {4A957768-4978-4268-8FED-574D47AD9698}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {4A957768-4978-4268-8FED-574D47AD9698}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {4A957768-4978-4268-8FED-574D47AD9698}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | EndGlobal
29 |
--------------------------------------------------------------------------------
/TinyScript.userprefs:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/TinyScript/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/TinyScript/Builder/BooleanOpBuilder.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using System;
3 | using System.Reflection;
4 | using System.Reflection.Emit;
5 |
6 | namespace TinyScript.Builder
7 | {
8 | public class BooleanOpBuilder : OpBuilder
9 | {
10 | private static Type _handleType = typeof(bool);
11 | private static MethodInfo _tostr = _handleType.GetMethod("ToString", new Type[0]);
12 |
13 | public BooleanOpBuilder(ParserRuleContext ctx, IlBuilder builder) : base(ctx, builder)
14 | {
15 | }
16 |
17 | protected override void CheckEq()
18 | {
19 | Il.Ceq();
20 | }
21 |
22 | protected override void CheckNeq()
23 | {
24 | Il.Ceq();
25 | ReverseOp();
26 | }
27 |
28 | protected override void DoNot()
29 | {
30 | ReverseOp();
31 | }
32 |
33 | public override void CallToString()
34 | {
35 | var variable = Il.DeclareLocal(_handleType);
36 | Il.SetLocal(variable);
37 | Il.LoadLocalAddress(variable);
38 | Il.CallVirtual(_tostr);
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/TinyScript/Builder/DecimalOpBuilder.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Reflection;
6 | using System.Reflection.Emit;
7 | using System.Text;
8 |
9 | namespace TinyScript.Builder
10 | {
11 | public class DecimalOpBuilder : OpBuilder
12 | {
13 | private static Type _handleType = typeof(decimal);
14 | private static MethodInfo _gt = _handleType.GetMethod("op_GreaterThan");
15 | private static MethodInfo _lt = _handleType.GetMethod("op_LessThan");
16 | private static MethodInfo _gte = _handleType.GetMethod("op_GreaterThanOrEqual");
17 | private static MethodInfo _lte = _handleType.GetMethod("op_LessThanOrEqual");
18 | private static MethodInfo _eq = _handleType.GetMethod("op_Equality");
19 | private static MethodInfo _neq = _handleType.GetMethod("op_Inequality");
20 | private static MethodInfo _add = _handleType.GetMethod("op_Addition");
21 | private static MethodInfo _sub = _handleType.GetMethod("op_Subtraction");
22 | private static MethodInfo _mul = _handleType.GetMethod("op_Multiply");
23 | private static MethodInfo _div = _handleType.GetMethod("op_Division");
24 | private static MethodInfo _neg = _handleType.GetMethod("op_UnaryNegation");
25 | private static MethodInfo _parse = _handleType.GetMethod("Parse", new Type[] { typeof(string) });
26 | private static MethodInfo _tostr = _handleType.GetMethod("ToString", new Type[0]);
27 |
28 | public DecimalOpBuilder(ParserRuleContext ctx, IlBuilder builder) : base(ctx, builder)
29 | {
30 | }
31 |
32 | protected override void CheckEq()
33 | {
34 | Il.Call(_eq);
35 | }
36 |
37 | protected override void CheckNeq()
38 | {
39 | Il.Call(_neq);
40 | }
41 |
42 | protected override void CheckGt()
43 | {
44 | Il.Call(_gt);
45 | }
46 |
47 | protected override void CheckGte()
48 | {
49 | Il.Call(_gte);
50 | }
51 |
52 | protected override void CheckLt()
53 | {
54 | Il.Call(_lt);
55 | }
56 |
57 | protected override void CheckLte()
58 | {
59 | Il.Call(_lte);
60 | }
61 |
62 | protected override void DoAdd()
63 | {
64 | Il.Call(_add);
65 | }
66 |
67 | protected override void DoSub()
68 | {
69 | Il.Call(_sub);
70 | }
71 |
72 | protected override void DoMul()
73 | {
74 | Il.Call(_mul);
75 | }
76 |
77 | protected override void DoDiv()
78 | {
79 | Il.Call(_div);
80 | }
81 |
82 | protected override void DoNeg()
83 | {
84 | Il.Call(_neg);
85 | }
86 |
87 | public override void LoadNum(string num)
88 | {
89 | Il.LoadString(num);
90 | Il.Call(_parse);
91 | }
92 |
93 | public override void CallToString()
94 | {
95 | var variable = Il.DeclareLocal(_handleType);
96 | Il.SetLocal(variable);
97 | Il.LoadLocalAddress(variable);
98 | Il.Call(_tostr);
99 | }
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/TinyScript/Builder/IlBuilder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reflection;
3 | using System.Reflection.Emit;
4 |
5 | namespace TinyScript.Builder
6 | {
7 | public class IlBuilder
8 | {
9 | private static readonly MethodInfo Print = typeof(Console).GetMethod("WriteLine", new[] { typeof(string) });
10 | protected readonly AssemblyBuilder InnerAssembly;
11 | protected readonly ModuleBuilder InnerModule;
12 | protected TypeBuilder Program;
13 | protected MethodBuilder Main;
14 | protected readonly ILGenerator Builder;
15 | protected string ExeName;
16 |
17 | public IlBuilder(string exeName, string className = "Program")
18 | {
19 | ExeName = exeName;
20 | var an = new AssemblyName("TinyScript");
21 | InnerAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(
22 | an, AssemblyBuilderAccess.RunAndSave);
23 | InnerModule = InnerAssembly.DefineDynamicModule("TinyScript", ExeName);
24 | Builder = InitMain(className);
25 | }
26 |
27 | private ILGenerator InitMain(string className)
28 | {
29 | Program = InnerModule.DefineType(className, TypeAttributes.BeforeFieldInit | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.NotPublic);
30 | Program.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
31 | Main = BuildMain();
32 | InnerAssembly.SetEntryPoint(Main);
33 | return Main.GetILGenerator();
34 | }
35 |
36 | protected virtual MethodBuilder BuildMain()
37 | {
38 | return Program.DefineMethod("Main", MethodAttributes.Static | MethodAttributes.Private | MethodAttributes.HideBySig, null, new[] { typeof(string[]) });
39 | }
40 |
41 | public virtual void Run()
42 | {
43 | Builder.Emit(OpCodes.Ret);
44 | var t = Program.CreateType();
45 | var main = t.GetMethod("Main");
46 | main.Invoke(null, new object[0]);
47 | }
48 |
49 | public virtual void Save()
50 | {
51 | Builder.Emit(OpCodes.Ret);
52 | Program.CreateType();
53 | InnerAssembly.Save(ExeName);
54 | }
55 |
56 | public LocalBuilder DeclareLocal(Type type)
57 | {
58 | return Builder.DeclareLocal(type);
59 | }
60 |
61 | public Label DefineLabel()
62 | {
63 | return Builder.DefineLabel();
64 | }
65 |
66 | public void MarkLabel(Label loc)
67 | {
68 | Builder.MarkLabel(loc);
69 | }
70 |
71 | public virtual void EmitPrint()
72 | {
73 | Builder.Emit(OpCodes.Call, Print);
74 | }
75 |
76 | public void LoadInt(int n)
77 | {
78 | switch(n)
79 | {
80 | case 0: Builder.Emit(OpCodes.Ldc_I4_0); return;
81 | case 1: Builder.Emit(OpCodes.Ldc_I4_1); return;
82 | case 2: Builder.Emit(OpCodes.Ldc_I4_2); return;
83 | case 3: Builder.Emit(OpCodes.Ldc_I4_3); return;
84 | case 4: Builder.Emit(OpCodes.Ldc_I4_4); return;
85 | case 5: Builder.Emit(OpCodes.Ldc_I4_5); return;
86 | case 6: Builder.Emit(OpCodes.Ldc_I4_6); return;
87 | case 7: Builder.Emit(OpCodes.Ldc_I4_7); return;
88 | case 8: Builder.Emit(OpCodes.Ldc_I4_8); return;
89 | }
90 | Builder.Emit(OpCodes.Ldc_I4, n);
91 | }
92 |
93 | public void LoadString(string text)
94 | {
95 | Builder.Emit(OpCodes.Ldstr, text);
96 | }
97 |
98 | public void LoadLocal(LocalBuilder local)
99 | {
100 | Builder.Emit(OpCodes.Ldloc, local);
101 | }
102 |
103 | public void LoadLocalAddress(LocalBuilder local)
104 | {
105 | Builder.Emit(OpCodes.Ldloca, local);
106 | }
107 |
108 | public void SetLocal(LocalBuilder local)
109 | {
110 | Builder.Emit(OpCodes.Stloc, local);
111 | }
112 |
113 | public void Nop()
114 | {
115 | Builder.Emit(OpCodes.Nop);
116 | }
117 |
118 | public void BrFalse(Label loc)
119 | {
120 | Builder.Emit(OpCodes.Brfalse, loc);
121 | }
122 |
123 | public void BrTrue(Label loc)
124 | {
125 | Builder.Emit(OpCodes.Brtrue, loc);
126 | }
127 |
128 | public void Br(Label loc)
129 | {
130 | Builder.Emit(OpCodes.Br, loc);
131 | }
132 |
133 | public void Call(MethodInfo info)
134 | {
135 | Builder.Emit(OpCodes.Call, info);
136 | }
137 |
138 | public void CallVirtual(MethodInfo info)
139 | {
140 | Builder.Emit(OpCodes.Callvirt, info);
141 | }
142 |
143 | public void Ceq()
144 | {
145 | Builder.Emit(OpCodes.Ceq);
146 | }
147 |
148 | public void Or()
149 | {
150 | Builder.Emit(OpCodes.Or);
151 | }
152 |
153 | public void And()
154 | {
155 | Builder.Emit(OpCodes.And);
156 | }
157 |
158 | public void Box(Type type)
159 | {
160 | Builder.Emit(OpCodes.Box, type);
161 | }
162 | }
163 | }
164 |
--------------------------------------------------------------------------------
/TinyScript/Builder/OpBuilder.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Reflection.Emit;
6 | using System.Text;
7 |
8 | namespace TinyScript.Builder
9 | {
10 | public class OpBuilder
11 | {
12 | public static OpBuilder GetOpBuilder(Type type, ParserRuleContext ctx, IlBuilder builder)
13 | {
14 | if(type == typeof(bool))
15 | {
16 | return new BooleanOpBuilder(ctx, builder);
17 | }
18 | else if(type == typeof(string))
19 | {
20 | return new StringOpBuilder(ctx, builder);
21 | }
22 | else if(type == typeof(decimal))
23 | {
24 | return new DecimalOpBuilder(ctx, builder);
25 | }
26 | throw ctx.Exception("Unspported operation.");
27 | }
28 |
29 | protected ParserRuleContext Ctx;
30 | protected IlBuilder Il;
31 |
32 | public OpBuilder(ParserRuleContext ctx, IlBuilder builder)
33 | {
34 | Ctx = ctx;
35 | Il = builder;
36 | }
37 |
38 | protected void ReverseOp()
39 | {
40 | Il.LoadInt(0);
41 | Il.Ceq();
42 | }
43 |
44 | public void ProcessCmp(string op)
45 | {
46 | switch (op)
47 | {
48 | case "==": CheckEq(); break;
49 | case "!=": CheckNeq(); break;
50 | case "<": CheckLt(); break;
51 | case "<=": CheckLte(); break;
52 | case ">": CheckGt(); break;
53 | case ">=": CheckGte(); break;
54 | }
55 | }
56 |
57 | protected virtual void CheckEq()
58 | {
59 | throw Ctx.Exception("Sytex error.");
60 | }
61 |
62 | protected virtual void CheckNeq()
63 | {
64 | throw Ctx.Exception("Sytex error.");
65 | }
66 |
67 | protected virtual void CheckGt()
68 | {
69 | throw Ctx.Exception("Sytex error.");
70 | }
71 |
72 | protected virtual void CheckGte()
73 | {
74 | throw Ctx.Exception("Sytex error.");
75 | }
76 |
77 | protected virtual void CheckLt()
78 | {
79 | throw Ctx.Exception("Sytex error.");
80 | }
81 |
82 | protected virtual void CheckLte()
83 | {
84 | throw Ctx.Exception("Sytex error.");
85 | }
86 |
87 | public void ProcessAdd(string op)
88 | {
89 | switch(op)
90 | {
91 | case "+": DoAdd(); break;
92 | case "-": DoSub(); break;
93 | }
94 | }
95 |
96 | protected virtual void DoAdd()
97 | {
98 | throw Ctx.Exception("Sytex error.");
99 | }
100 |
101 | protected virtual void DoSub()
102 | {
103 | throw Ctx.Exception("Sytex error.");
104 | }
105 |
106 | public void ProcessMul(string op)
107 | {
108 | switch(op)
109 | {
110 | case "*": DoMul(); break;
111 | case "/": DoDiv(); break;
112 | }
113 | }
114 |
115 | protected virtual void DoMul()
116 | {
117 | throw Ctx.Exception("Sytex error.");
118 | }
119 |
120 | protected virtual void DoDiv()
121 | {
122 | throw Ctx.Exception("Sytex error.");
123 | }
124 |
125 | public void ProcessUnary(string op)
126 | {
127 | switch(op)
128 | {
129 | case "-": DoNeg(); break;
130 | case "!": DoNot(); break;
131 | }
132 | }
133 |
134 | protected virtual void DoNeg()
135 | {
136 | throw Ctx.Exception("Sytex error.");
137 | }
138 |
139 | protected virtual void DoNot()
140 | {
141 | throw Ctx.Exception("Sytex error.");
142 | }
143 |
144 | public virtual void LoadNum(string num)
145 | {
146 | throw Ctx.Exception("Sytex error.");
147 | }
148 |
149 | public virtual void CallToString()
150 | {
151 | throw Ctx.Exception("Sytex error.");
152 | }
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/TinyScript/Builder/StringOpBuilder.cs:
--------------------------------------------------------------------------------
1 | using Antlr4.Runtime;
2 | using System;
3 | using System.Reflection;
4 | using System.Reflection.Emit;
5 |
6 | namespace TinyScript.Builder
7 | {
8 | public class StringOpBuilder : OpBuilder
9 | {
10 | private static Type _handleType = typeof(string);
11 | private static MethodInfo _eq = _handleType.GetMethod("op_Equality");
12 | private static MethodInfo _neq = _handleType.GetMethod("op_Inequality");
13 | private static MethodInfo _concat = _handleType.GetMethod("Concat", new Type[] { typeof(object), typeof(object) });
14 |
15 | public StringOpBuilder(ParserRuleContext ctx, IlBuilder builder) : base(ctx, builder)
16 | {
17 | }
18 |
19 | protected override void CheckEq()
20 | {
21 | Il.Call(_eq);
22 | }
23 |
24 | protected override void CheckNeq()
25 | {
26 | Il.Call(_neq);
27 | }
28 |
29 | protected override void DoAdd()
30 | {
31 | Il.Call(_concat);
32 | }
33 |
34 | public override void CallToString()
35 | {
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/TinyScript/Channel.cs:
--------------------------------------------------------------------------------
1 | namespace TinyScript
2 | {
3 | public class Channel
4 | {
5 | public virtual void Print(string msg)
6 | {
7 | System.Console.WriteLine(msg);
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/TinyScript/CompilerVisitor.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Reflection.Emit;
4 | using Antlr4.Runtime.Misc;
5 | using Antlr4.Runtime;
6 | using TinyScript.Builder;
7 |
8 | namespace TinyScript
9 | {
10 | public class CompilerVisitor : TinyScriptBaseVisitor