├── .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, ISaveable 11 | { 12 | public class Variable 13 | { 14 | public Type Type; 15 | public LocalBuilder Value; 16 | } 17 | 18 | private readonly IlBuilder _builder; 19 | private readonly Dictionary _variables = new Dictionary(); 20 | 21 | public CompilerVisitor(IlBuilder builder) 22 | { 23 | _builder = builder; 24 | } 25 | 26 | public void Save() 27 | { 28 | _builder.Save(); 29 | } 30 | 31 | public override object VisitDeclareExpression([NotNull] TinyScriptParser.DeclareExpressionContext context) 32 | { 33 | var type = GetDeclType(context, context.basicType().GetText()); 34 | foreach (var assign in context.declarators().assign()) 35 | { 36 | var name = assign.Identifier().GetText(); 37 | Variable obj; 38 | if(_variables.TryGetValue(name, out obj)) 39 | { 40 | throw context.Exception("Variable [{0}] already defined.", name); 41 | } 42 | _variables.Add(name, new Variable { Type = type }); 43 | } 44 | return VisitDeclarators(context.declarators()); 45 | } 46 | 47 | private Type GetDeclType(ParserRuleContext context, string typeName) 48 | { 49 | switch(typeName) 50 | { 51 | case "decimal": return typeof(decimal); 52 | case "bool": return typeof(bool); 53 | case "string": return typeof(string); 54 | case "var": return null; 55 | } 56 | throw context.Exception("Unknown type declare : [{0}]", typeName); 57 | } 58 | 59 | public override object VisitDeclarators([NotNull] TinyScriptParser.DeclaratorsContext context) 60 | { 61 | base.VisitDeclarators(context); 62 | return null; 63 | } 64 | 65 | public override object VisitAssign([NotNull] TinyScriptParser.AssignContext context) 66 | { 67 | var type = (Type)VisitExpression(context.expression()); 68 | var name = context.Identifier().GetText(); 69 | Variable value; 70 | if(_variables.TryGetValue(name, out value)) 71 | { 72 | if (value.Value == null) 73 | { 74 | LocalBuilder variable; 75 | if(value.Type == null) 76 | { 77 | variable = _builder.DeclareLocal(type); 78 | value.Type = type; 79 | } 80 | else if(value.Type != type) 81 | { 82 | throw context.Exception("Cannot convert type [{0}] to [{1}].", type.Name, value.Type.Name); 83 | } 84 | else 85 | { 86 | variable = _builder.DeclareLocal(type); 87 | } 88 | value.Value = variable; 89 | } 90 | _builder.SetLocal(value.Value); 91 | return null; 92 | } 93 | throw context.Exception("Variable [{0}] not defined.", name); 94 | } 95 | 96 | public override object VisitExpression([NotNull] TinyScriptParser.ExpressionContext context) 97 | { 98 | var ret = VisitAndAndExpression(context.andAndExpression(0)); 99 | if(context.ChildCount > 1) 100 | { 101 | for (int i = 2; i < context.ChildCount; i+=2) 102 | { 103 | VisitAndAndExpression((TinyScriptParser.AndAndExpressionContext)context.GetChild(i)); 104 | _builder.Or(); 105 | } 106 | return typeof(bool); 107 | } 108 | return ret; 109 | } 110 | 111 | public override object VisitAndAndExpression([NotNull] TinyScriptParser.AndAndExpressionContext context) 112 | { 113 | var ret = VisitCmpExpression(context.cmpExpression(0)); 114 | if (context.ChildCount > 1) 115 | { 116 | for (int i = 2; i < context.ChildCount; i += 2) 117 | { 118 | VisitCmpExpression((TinyScriptParser.CmpExpressionContext)context.GetChild(i)); 119 | _builder.And(); 120 | } 121 | return typeof(bool); 122 | } 123 | return ret; 124 | } 125 | 126 | public override object VisitCmpExpression([NotNull] TinyScriptParser.CmpExpressionContext context) 127 | { 128 | var ret = (Type)VisitAddExpression(context.addExpression(0)); 129 | if (context.ChildCount > 1) 130 | { 131 | var t2 = (Type)VisitAddExpression(context.addExpression(1)); 132 | var op = context.GetChild(1).GetText(); 133 | var cmpType = GetOpType(context, ret, t2); 134 | var b = OpBuilder.GetOpBuilder(cmpType, context, _builder); 135 | b.ProcessCmp(op); 136 | return typeof(bool); 137 | } 138 | return ret; 139 | } 140 | 141 | private Type GetOpType(ParserRuleContext ctx, Type t1, Type t2) 142 | { 143 | if(t1 == t2) 144 | { 145 | return t1; 146 | } 147 | throw ctx.Exception("Cannot do operation between [{0}] and [{1}].", t1.Name, t2.Name); 148 | } 149 | 150 | public override object VisitAddExpression([NotNull] TinyScriptParser.AddExpressionContext context) 151 | { 152 | var ret = (Type)VisitMulExpression(context.mulExpression(0)); 153 | if(context.ChildCount > 1) 154 | { 155 | Type typeAdd = null; 156 | for (int i = 1; i < context.ChildCount; i+=2) 157 | { 158 | var op = context.GetChild(i).GetText(); 159 | var t2 = (Type)VisitMulExpression((TinyScriptParser.MulExpressionContext)context.GetChild(i + 1)); 160 | typeAdd = ret == typeof(string) ? ret : GetOpType(context, ret, t2); 161 | if(ret == typeof(string) && t2.IsValueType) 162 | { 163 | _builder.Box(t2); 164 | } 165 | var b = OpBuilder.GetOpBuilder(typeAdd, context, _builder); 166 | b.ProcessAdd(op); 167 | } 168 | return typeAdd; 169 | } 170 | return ret; 171 | } 172 | 173 | public override object VisitMulExpression([NotNull] TinyScriptParser.MulExpressionContext context) 174 | { 175 | var ret = (Type)VisitUnaryExpression(context.unaryExpression(0)); 176 | if (context.ChildCount > 1) 177 | { 178 | Type typeAdd = null; 179 | for (int i = 1; i < context.ChildCount; i+=2) 180 | { 181 | var op = context.GetChild(i).GetText(); 182 | var t2 = (Type)VisitUnaryExpression((TinyScriptParser.UnaryExpressionContext)context.GetChild(i + 1)); 183 | typeAdd = GetOpType(context, ret, t2); 184 | var b = OpBuilder.GetOpBuilder(typeAdd, context, _builder); 185 | b.ProcessMul(op); 186 | } 187 | return typeAdd; 188 | } 189 | return ret; 190 | } 191 | 192 | public override object VisitUnaryExpression([NotNull] TinyScriptParser.UnaryExpressionContext context) 193 | { 194 | if(context.ChildCount == 1) 195 | { 196 | return VisitPrimaryExpression(context.primaryExpression()); 197 | } 198 | var ret = (Type)VisitUnaryExpression(context.unaryExpression()); 199 | var op = context.GetChild(0).GetText(); 200 | var b = OpBuilder.GetOpBuilder(ret, context, _builder); 201 | b.ProcessUnary(op); 202 | return ret; 203 | } 204 | 205 | public override object VisitPrimaryExpression([NotNull] TinyScriptParser.PrimaryExpressionContext context) 206 | { 207 | if(context.ChildCount == 1) 208 | { 209 | var c = context.GetChild(0); 210 | if(c is TinyScriptParser.VariableExpressionContext) 211 | { 212 | return VisitVariableExpression(context.variableExpression()); 213 | } 214 | var num = context.numericLiteral().GetText().Replace("_", ""); 215 | var b = OpBuilder.GetOpBuilder(typeof(decimal), context, _builder); 216 | b.LoadNum(num); 217 | return typeof(decimal); 218 | } 219 | return VisitExpression(context.expression()); 220 | } 221 | 222 | public override object VisitVariableExpression([NotNull] TinyScriptParser.VariableExpressionContext context) 223 | { 224 | var c = context.GetChild(0); 225 | var text = c.GetText(); 226 | switch(text) 227 | { 228 | case "true": _builder.LoadInt(1); return typeof(bool); 229 | case "false": _builder.LoadInt(0); return typeof(bool); 230 | } 231 | if(text.StartsWith("\"")) 232 | { 233 | var str = text.Substring(1, text.Length - 2); 234 | _builder.LoadString(str); 235 | return typeof(string); 236 | } 237 | Variable variable; 238 | if(_variables.TryGetValue(text, out variable)) 239 | { 240 | _builder.LoadLocal(variable.Value); 241 | return variable.Type; 242 | } 243 | throw context.Exception("Use of undeclared identifier [{0}].", text); 244 | } 245 | 246 | public override object VisitPrintStatement([NotNull] TinyScriptParser.PrintStatementContext context) 247 | { 248 | var ret = (Type)VisitExpression(context.expression()); 249 | var b = OpBuilder.GetOpBuilder(ret, context, _builder); 250 | b.CallToString(); 251 | _builder.EmitPrint(); 252 | return null; 253 | } 254 | 255 | public override object VisitIfStatement([NotNull] TinyScriptParser.IfStatementContext context) 256 | { 257 | VisitQuoteExpr(context.quoteExpr()); 258 | var l1 = _builder.DefineLabel(); 259 | _builder.BrFalse(l1); 260 | VisitBlockStatement(context.blockStatement(0)); 261 | if(context.ChildCount == 5) 262 | { 263 | var l2 = _builder.DefineLabel(); 264 | _builder.Br(l2); 265 | _builder.Nop(); 266 | _builder.MarkLabel(l1); 267 | VisitBlockStatement(context.blockStatement(1)); 268 | _builder.Nop(); 269 | _builder.MarkLabel(l2); 270 | } 271 | else 272 | { 273 | _builder.Nop(); 274 | _builder.MarkLabel(l1); 275 | } 276 | return null; 277 | } 278 | 279 | public override object VisitQuoteExpr([NotNull] TinyScriptParser.QuoteExprContext context) 280 | { 281 | AssertIsBool(context, VisitExpression(context.expression())); 282 | return typeof(bool); 283 | } 284 | 285 | private void AssertIsBool(ParserRuleContext ctx, object result) 286 | { 287 | if ((Type)result != typeof(bool)) 288 | { 289 | throw ctx.Exception("Confition result of IF statement must be boolean."); 290 | } 291 | } 292 | 293 | public override object VisitWhileStatement([NotNull] TinyScriptParser.WhileStatementContext context) 294 | { 295 | var l1 = _builder.DefineLabel(); 296 | _builder.MarkLabel(l1); 297 | AssertIsBool(context, VisitExpression(context.expression())); 298 | var l2 = _builder.DefineLabel(); 299 | _builder.BrFalse(l2); 300 | VisitBlockStatement(context.blockStatement()); 301 | _builder.Br(l1); 302 | _builder.MarkLabel(l2); 303 | _builder.Nop(); 304 | return null; 305 | } 306 | 307 | public override object VisitDoWhileStatement([NotNull] TinyScriptParser.DoWhileStatementContext context) 308 | { 309 | var l1 = _builder.DefineLabel(); 310 | _builder.MarkLabel(l1); 311 | VisitBlockStatement(context.blockStatement()); 312 | AssertIsBool(context, VisitExpression(context.expression())); 313 | _builder.BrTrue(l1); 314 | return null; 315 | } 316 | 317 | public override object VisitForStatement([NotNull] TinyScriptParser.ForStatementContext context) 318 | { 319 | VisitCommonExpression(context.commonExpression()); 320 | var l1 = _builder.DefineLabel(); 321 | _builder.MarkLabel(l1); 322 | AssertIsBool(context, VisitExpression(context.expression())); 323 | var l2 = _builder.DefineLabel(); 324 | _builder.BrFalse(l2); 325 | VisitBlockStatement(context.blockStatement()); 326 | VisitAssignAbleStatement(context.assignAbleStatement()); 327 | _builder.Br(l1); 328 | _builder.MarkLabel(l2); 329 | _builder.Nop(); 330 | return null; 331 | } 332 | } 333 | } 334 | -------------------------------------------------------------------------------- /TinyScript/CoreHelper.cs: -------------------------------------------------------------------------------- 1 | using Antlr4.Runtime; 2 | using System; 3 | 4 | namespace TinyScript 5 | { 6 | public static class CoreHelper 7 | { 8 | public static ParserRuleContextException Exception(this ParserRuleContext ctx, string template, params object[] args) 9 | { 10 | return new ParserRuleContextException(ctx, template, args); 11 | } 12 | 13 | public static void Log(this ParserRuleContext ctx) 14 | { 15 | Console.WriteLine(">>>[{0},{1}][{2}] ChildCount : {3}, Text : {4}", 16 | ctx.Start.Line, ctx.Start.Column, ctx.GetType().Name, ctx.ChildCount, ctx.GetText()); 17 | } 18 | 19 | public static void RunMain(this Type type) 20 | { 21 | var main = type.GetMethod("Main"); 22 | main.Invoke(null, new object[0]); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /TinyScript/InterpreterVisitor.cs: -------------------------------------------------------------------------------- 1 | using Antlr4.Runtime; 2 | using Antlr4.Runtime.Misc; 3 | using System.Collections.Generic; 4 | 5 | namespace TinyScript 6 | { 7 | public class VarValue 8 | { 9 | public int StartIndex; 10 | public object Value; 11 | 12 | public VarValue (int startIndex, object value) 13 | { 14 | StartIndex = startIndex; 15 | Value = value; 16 | } 17 | } 18 | 19 | public class InterpreterVisitor : TinyScriptBaseVisitor 20 | { 21 | private Dictionary Variables = new Dictionary(); 22 | private Channel _channel; 23 | 24 | public InterpreterVisitor(Channel channel) 25 | { 26 | _channel = channel; 27 | } 28 | 29 | public override object VisitDeclareExpression([NotNull] TinyScriptParser.DeclareExpressionContext context) 30 | { 31 | var assigns = context.declarators().assign(); 32 | foreach (var assign in assigns) 33 | { 34 | var name = assign.Identifier().GetText(); 35 | VarValue obj; 36 | if(Variables.TryGetValue(name, out obj)) 37 | { 38 | if (obj.StartIndex == assign.Start.StartIndex) { 39 | return base.VisitDeclareExpression(context); 40 | } 41 | throw context.Exception("Variable [{0}] already defined.", name); 42 | } 43 | switch (context.basicType().GetText()) 44 | { 45 | case "decimal": 46 | Variables.Add(name, new VarValue(assign.Start.StopIndex, (decimal)0)); 47 | break; 48 | case "string": 49 | Variables.Add(name, new VarValue(assign.Start.StopIndex, "")); 50 | break; 51 | case "bool": 52 | Variables.Add(name, new VarValue(assign.Start.StopIndex, false)); 53 | break; 54 | case "var": 55 | Variables.Add(name, new VarValue(assign.Start.StopIndex, null)); 56 | break; 57 | } 58 | } 59 | return base.VisitDeclareExpression(context); 60 | } 61 | 62 | public override object VisitAssign([NotNull] TinyScriptParser.AssignContext context) 63 | { 64 | var name = context.Identifier().GetText(); 65 | VarValue obj; 66 | if (!Variables.TryGetValue(name, out obj)) 67 | { 68 | throw context.Exception("Variable [{0}] not defined.", name); 69 | } 70 | var r = base.VisitAssign(context); 71 | if (obj != null && obj.Value != null) 72 | { 73 | if (obj.Value.GetType() != r.GetType()) 74 | { 75 | throw context.Exception("Cannot convert type [{1}] to [{0}].", obj.Value.GetType().Name, r.GetType().Name); 76 | } 77 | } 78 | Variables[name] = new VarValue(obj.StartIndex, r); 79 | return null; 80 | } 81 | 82 | public override object VisitPrintStatement([NotNull] TinyScriptParser.PrintStatementContext context) 83 | { 84 | var r = VisitExpression(context.expression()); 85 | _channel.Print(r.ToString()); 86 | return null; 87 | } 88 | 89 | public override object VisitUnaryExpression([NotNull] TinyScriptParser.UnaryExpressionContext context) 90 | { 91 | if (context.ChildCount > 1) 92 | { 93 | switch (context.GetChild(0).GetText()) 94 | { 95 | case "-": return -((decimal)VisitUnaryExpression(context.unaryExpression())); 96 | case "!": return !((bool)VisitUnaryExpression(context.unaryExpression())); 97 | } 98 | } 99 | return VisitPrimaryExpression(context.primaryExpression()); 100 | } 101 | 102 | public override object VisitExpression([NotNull] TinyScriptParser.ExpressionContext context) 103 | { 104 | var a = VisitAndAndExpression(context.andAndExpression(0)); 105 | for (int i = 1; i < context.ChildCount; i += 2) 106 | { 107 | var b = (bool)VisitAndAndExpression((TinyScriptParser.AndAndExpressionContext)context.GetChild(i + 1)); 108 | a = (bool)a || b; 109 | } 110 | return a; 111 | } 112 | 113 | public override object VisitAndAndExpression([NotNull] TinyScriptParser.AndAndExpressionContext context) 114 | { 115 | var a = VisitCmpExpression(context.cmpExpression(0)); 116 | for (int i = 1; i < context.ChildCount; i += 2) 117 | { 118 | var b = (bool)VisitCmpExpression((TinyScriptParser.CmpExpressionContext)context.GetChild(i + 1)); 119 | a = (bool)a && b; 120 | } 121 | return a; 122 | } 123 | 124 | public override object VisitAddExpression([NotNull] TinyScriptParser.AddExpressionContext context) 125 | { 126 | var a = VisitMulExpression(context.mulExpression(0)); 127 | for (int i = 1; i < context.ChildCount; i += 2) 128 | { 129 | var op = context.GetChild(i).GetText(); 130 | var b = VisitMulExpression((TinyScriptParser.MulExpressionContext)context.GetChild(i + 1)); 131 | switch (op) 132 | { 133 | case "+": 134 | if (a is string || b is string) 135 | { 136 | a = a.ToString() + b.ToString(); 137 | } 138 | else 139 | { 140 | a = (decimal)a + (decimal)b; 141 | } 142 | break; 143 | case "-": 144 | a = (decimal)a - (decimal)b; 145 | break; 146 | } 147 | } 148 | return a; 149 | } 150 | 151 | public override object VisitMulExpression([NotNull] TinyScriptParser.MulExpressionContext context) 152 | { 153 | var a = VisitUnaryExpression(context.unaryExpression(0)); 154 | for (int i = 1; i < context.ChildCount; i += 2) 155 | { 156 | var op = context.GetChild(i).GetText(); 157 | var b = (decimal)VisitUnaryExpression((TinyScriptParser.UnaryExpressionContext)context.GetChild(i + 1)); 158 | switch (op) 159 | { 160 | case "*": 161 | a = (decimal)a * b; 162 | break; 163 | case "/": 164 | a = (decimal)a / b; 165 | break; 166 | } 167 | } 168 | return a; 169 | } 170 | 171 | public override object VisitPrimaryExpression([NotNull] TinyScriptParser.PrimaryExpressionContext context) 172 | { 173 | if (context.ChildCount == 1) 174 | { 175 | var c = context.GetChild(0); 176 | if (c is TinyScriptParser.VariableExpressionContext) 177 | { 178 | return VisitVariableExpression((TinyScriptParser.VariableExpressionContext)c); 179 | } 180 | else 181 | { 182 | return VisitNumericLiteral(context.numericLiteral()); 183 | } 184 | } 185 | else 186 | { 187 | return VisitExpression(context.expression()); 188 | } 189 | } 190 | 191 | public override object VisitQuoteExpr([NotNull] TinyScriptParser.QuoteExprContext context) 192 | { 193 | return VisitExpression(context.expression()); 194 | } 195 | 196 | public override object VisitNumericLiteral([NotNull] TinyScriptParser.NumericLiteralContext context) 197 | { 198 | var text = context.Decimal().GetText().Replace("_", ""); 199 | return decimal.Parse(text); 200 | } 201 | 202 | public override object VisitVariableExpression([NotNull] TinyScriptParser.VariableExpressionContext context) 203 | { 204 | var n = context.GetText(); 205 | if (n == "true") 206 | { 207 | return true; 208 | } 209 | else if (n == "false") 210 | { 211 | return false; 212 | } 213 | else if (n.StartsWith("\"")) 214 | { 215 | return n.Substring(1, n.Length - 2); 216 | } 217 | VarValue obj; 218 | if (!Variables.TryGetValue(n, out obj)) 219 | { 220 | throw context.Exception("Use of undeclared identifier [{0}].", n); 221 | } 222 | return obj.Value; 223 | } 224 | 225 | public override object VisitCmpExpression([NotNull] TinyScriptParser.CmpExpressionContext context) 226 | { 227 | var a = VisitAddExpression(context.addExpression(0)); 228 | if (context.ChildCount > 2) 229 | { 230 | var b = VisitAddExpression(context.addExpression(1)); 231 | var op = context.GetChild(1).GetText(); 232 | switch (op) 233 | { 234 | case "==": 235 | return ValueEquals(a, b, context); 236 | case "!=": 237 | return !ValueEquals(a, b, context); 238 | case "<": 239 | return (decimal)a < (decimal)b; 240 | case "<=": 241 | return (decimal)a <= (decimal)b; 242 | case ">": 243 | return (decimal)a > (decimal)b; 244 | case ">=": 245 | return (decimal)a >= (decimal)b; 246 | } 247 | throw context.Exception("Unsupported operation."); 248 | } 249 | return a; 250 | } 251 | 252 | private bool ValueEquals(object a, object b, ParserRuleContext ctx) 253 | { 254 | if(a is bool && b is bool) 255 | { 256 | return (bool)a == (bool)b; 257 | } 258 | else if(a is decimal && b is decimal) 259 | { 260 | return (decimal)a == (decimal)b; 261 | } 262 | else if(a is string && b is string) 263 | { 264 | return (string)a == (string)b; 265 | } 266 | throw ctx.Exception("Cannot do operation between [{0}] and [{1}].", a.GetType().Name, b.GetType().Name); 267 | } 268 | 269 | public override object VisitIfStatement([NotNull] TinyScriptParser.IfStatementContext context) 270 | { 271 | var condition = (bool)VisitQuoteExpr(context.quoteExpr()); 272 | if (condition) 273 | { 274 | VisitBlockStatement(context.blockStatement(0)); 275 | } 276 | else if (context.ChildCount == 5) 277 | { 278 | VisitBlockStatement(context.blockStatement(1)); 279 | } 280 | return null; 281 | } 282 | 283 | public override object VisitWhileStatement([NotNull] TinyScriptParser.WhileStatementContext context) 284 | { 285 | while (true) 286 | { 287 | var condition = (bool)VisitExpression(context.expression()); 288 | if (!(condition)) { break; } 289 | VisitBlockStatement(context.blockStatement()); 290 | } 291 | return null; 292 | } 293 | 294 | public override object VisitDoWhileStatement([NotNull] TinyScriptParser.DoWhileStatementContext context) 295 | { 296 | bool condition; 297 | do 298 | { 299 | VisitBlockStatement(context.blockStatement()); 300 | condition = (bool)VisitExpression(context.expression()); 301 | } while (condition); 302 | return null; 303 | } 304 | 305 | public override object VisitForStatement([NotNull] TinyScriptParser.ForStatementContext context) 306 | { 307 | for (VisitCommonExpression(context.commonExpression()); 308 | (bool)VisitExpression(context.expression()); 309 | VisitAssignAbleStatement(context.assignAbleStatement())) 310 | { 311 | VisitBlockStatement(context.blockStatement()); 312 | } 313 | return null; 314 | } 315 | } 316 | } 317 | -------------------------------------------------------------------------------- /TinyScript/JitBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Reflection.Emit; 4 | using TinyScript.Builder; 5 | 6 | namespace TinyScript 7 | { 8 | public class JitBuilder : IlBuilder 9 | { 10 | private static int _index; 11 | public Type GenType; 12 | 13 | public JitBuilder() : base("Jit.exe", "p" + _index) 14 | { 15 | _index++; 16 | } 17 | 18 | protected override MethodBuilder BuildMain() 19 | { 20 | return Program.DefineMethod("Main", MethodAttributes.Static | MethodAttributes.Public, null, new Type[0]); 21 | } 22 | 23 | public override void Save() 24 | { 25 | Builder.Emit(OpCodes.Ret); 26 | GenType = Program.CreateType(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /TinyScript/Parser/TinyScript.g4: -------------------------------------------------------------------------------- 1 | grammar TinyScript; 2 | 3 | options { 4 | language=CSharp; 5 | } 6 | 7 | //============================================================== 8 | 9 | program 10 | : statement+ EOF 11 | ; 12 | 13 | //============================================================== 14 | 15 | statement 16 | : ifStatement 17 | | blockStatement 18 | | assignStatement 19 | | declareStatement 20 | | printStatement 21 | | whileStatement 22 | | doWhileStatement 23 | | forStatement 24 | ; 25 | ifStatement 26 | : 'if' quoteExpr blockStatement 27 | | 'if' quoteExpr blockStatement 'else' blockStatement 28 | ; 29 | quoteExpr 30 | : '(' expression ')' 31 | ; 32 | blockStatement 33 | : '{' (statement)* '}' 34 | ; 35 | assignStatement 36 | : assign ';' 37 | ; 38 | declareStatement 39 | : declareExpression ';' 40 | ; 41 | printStatement 42 | : 'print' '(' expression ')' ';' 43 | ; 44 | whileStatement 45 | : 'while' '(' expression ')' blockStatement 46 | ; 47 | doWhileStatement 48 | : 'do' blockStatement 'while' '(' expression ')' ';' 49 | ; 50 | forStatement 51 | : 'for' '(' commonExpression ';' expression ';' assignAbleStatement ')' blockStatement 52 | ; 53 | commonExpression 54 | : declareExpression 55 | | assignAbleStatement 56 | ; 57 | assignAbleStatement 58 | : assign 59 | | expression 60 | ; 61 | 62 | //============================================================== 63 | 64 | declareExpression 65 | : basicType declarators 66 | ; 67 | expression 68 | : andAndExpression ('||' andAndExpression)* 69 | ; 70 | andAndExpression 71 | : cmpExpression ('&&' cmpExpression)* 72 | ; 73 | cmpExpression 74 | : addExpression (('==' | '!=' | '<' | '<=' | '>' | '>=') addExpression)? 75 | ; 76 | addExpression 77 | : mulExpression (('+' | '-') mulExpression)* 78 | ; 79 | mulExpression 80 | : unaryExpression (('*' | '/') unaryExpression)* 81 | ; 82 | unaryExpression 83 | : primaryExpression 84 | | ('-' | '!') unaryExpression 85 | ; 86 | primaryExpression 87 | : variableExpression 88 | | numericLiteral 89 | | '(' expression ')' 90 | ; 91 | variableExpression 92 | : Identifier 93 | | 'true' 94 | | 'false' 95 | | StringLiteral 96 | ; 97 | //============================================================== 98 | 99 | basicType 100 | : 'decimal' 101 | | 'string' 102 | | 'bool' 103 | | 'var' 104 | ; 105 | declarators 106 | : assign (',' assign)* 107 | ; 108 | assign 109 | : Identifier '=' expression 110 | ; 111 | 112 | //============================================================== 113 | 114 | numericLiteral 115 | : Decimal 116 | ; 117 | StringLiteral 118 | : '"' (~[\\\r\n])*? '"' 119 | ; 120 | Decimal 121 | : '0' ('.' (DigitChar)* NonZeroDigit)? 122 | | NonZeroDigit (DigitChar)* ('.' (DigitChar)* NonZeroDigit)? 123 | ; 124 | fragment NonZeroDigit 125 | : '1'..'9' 126 | ; 127 | fragment DigitChar 128 | : '0' 129 | | NonZeroDigit 130 | | '_' 131 | ; 132 | 133 | //============================================================== 134 | 135 | Identifier 136 | : (IdentifierStart) (IdentifierChar)* 137 | ; 138 | fragment IdentifierChar 139 | : (IdentifierStart) 140 | | '0' 141 | | NonZeroDigit 142 | ; 143 | fragment IdentifierStart 144 | : '_' 145 | | Letter 146 | // | UniversalAlpha 147 | ; 148 | fragment Letter 149 | : 'a'..'z' 150 | | 'A'..'Z' 151 | ; 152 | 153 | //============================================================== 154 | 155 | LineComment 156 | : '//' ~('\n'|'\r')* '\r'? '\n' -> skip 157 | ; 158 | Comment 159 | : '/*' (.)*? '*/' -> skip 160 | ; 161 | WhiteSpace 162 | : (' ' | '\r' | '\n' | '\t') -> skip 163 | ; 164 | -------------------------------------------------------------------------------- /TinyScript/Parser/TinyScript.tokens: -------------------------------------------------------------------------------- 1 | T__0=1 2 | T__1=2 3 | T__2=3 4 | T__3=4 5 | T__4=5 6 | T__5=6 7 | T__6=7 8 | T__7=8 9 | T__8=9 10 | T__9=10 11 | T__10=11 12 | T__11=12 13 | T__12=13 14 | T__13=14 15 | T__14=15 16 | T__15=16 17 | T__16=17 18 | T__17=18 19 | T__18=19 20 | T__19=20 21 | T__20=21 22 | T__21=22 23 | T__22=23 24 | T__23=24 25 | T__24=25 26 | T__25=26 27 | T__26=27 28 | T__27=28 29 | T__28=29 30 | T__29=30 31 | T__30=31 32 | T__31=32 33 | StringLiteral=33 34 | Decimal=34 35 | Identifier=35 36 | LineComment=36 37 | Comment=37 38 | WhiteSpace=38 39 | 'if'=1 40 | 'else'=2 41 | '('=3 42 | ')'=4 43 | '{'=5 44 | '}'=6 45 | ';'=7 46 | 'print'=8 47 | 'while'=9 48 | 'do'=10 49 | 'for'=11 50 | '||'=12 51 | '&&'=13 52 | '=='=14 53 | '!='=15 54 | '<'=16 55 | '<='=17 56 | '>'=18 57 | '>='=19 58 | '+'=20 59 | '-'=21 60 | '*'=22 61 | '/'=23 62 | '!'=24 63 | 'true'=25 64 | 'false'=26 65 | 'decimal'=27 66 | 'string'=28 67 | 'bool'=29 68 | 'var'=30 69 | ','=31 70 | '='=32 71 | -------------------------------------------------------------------------------- /TinyScript/Parser/TinyScriptBaseVisitor.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // ANTLR Version: 4.5.2 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | // Generated from TinyScript.g4 by ANTLR 4.5.2 12 | 13 | // Unreachable code detected 14 | #pragma warning disable 0162 15 | // The variable '...' is assigned but its value is never used 16 | #pragma warning disable 0219 17 | // Missing XML comment for publicly visible type or member '...' 18 | #pragma warning disable 1591 19 | // Ambiguous reference in cref attribute 20 | #pragma warning disable 419 21 | 22 | using Antlr4.Runtime.Misc; 23 | using Antlr4.Runtime.Tree; 24 | using IToken = Antlr4.Runtime.IToken; 25 | using ParserRuleContext = Antlr4.Runtime.ParserRuleContext; 26 | 27 | /// 28 | /// This class provides an empty implementation of , 29 | /// which can be extended to create a visitor which only needs to handle a subset 30 | /// of the available methods. 31 | /// 32 | /// The return type of the visit operation. 33 | [System.CodeDom.Compiler.GeneratedCode("ANTLR", "4.5.2")] 34 | [System.CLSCompliant(false)] 35 | public partial class TinyScriptBaseVisitor : AbstractParseTreeVisitor, ITinyScriptVisitor { 36 | /// 37 | /// Visit a parse tree produced by . 38 | /// 39 | /// The default implementation returns the result of calling 40 | /// on . 41 | /// 42 | /// 43 | /// The parse tree. 44 | /// The visitor result. 45 | public virtual Result VisitProgram([NotNull] TinyScriptParser.ProgramContext context) { return VisitChildren(context); } 46 | /// 47 | /// Visit a parse tree produced by . 48 | /// 49 | /// The default implementation returns the result of calling 50 | /// on . 51 | /// 52 | /// 53 | /// The parse tree. 54 | /// The visitor result. 55 | public virtual Result VisitStatement([NotNull] TinyScriptParser.StatementContext context) { return VisitChildren(context); } 56 | /// 57 | /// Visit a parse tree produced by . 58 | /// 59 | /// The default implementation returns the result of calling 60 | /// on . 61 | /// 62 | /// 63 | /// The parse tree. 64 | /// The visitor result. 65 | public virtual Result VisitIfStatement([NotNull] TinyScriptParser.IfStatementContext context) { return VisitChildren(context); } 66 | /// 67 | /// Visit a parse tree produced by . 68 | /// 69 | /// The default implementation returns the result of calling 70 | /// on . 71 | /// 72 | /// 73 | /// The parse tree. 74 | /// The visitor result. 75 | public virtual Result VisitQuoteExpr([NotNull] TinyScriptParser.QuoteExprContext context) { return VisitChildren(context); } 76 | /// 77 | /// Visit a parse tree produced by . 78 | /// 79 | /// The default implementation returns the result of calling 80 | /// on . 81 | /// 82 | /// 83 | /// The parse tree. 84 | /// The visitor result. 85 | public virtual Result VisitBlockStatement([NotNull] TinyScriptParser.BlockStatementContext context) { return VisitChildren(context); } 86 | /// 87 | /// Visit a parse tree produced by . 88 | /// 89 | /// The default implementation returns the result of calling 90 | /// on . 91 | /// 92 | /// 93 | /// The parse tree. 94 | /// The visitor result. 95 | public virtual Result VisitAssignStatement([NotNull] TinyScriptParser.AssignStatementContext context) { return VisitChildren(context); } 96 | /// 97 | /// Visit a parse tree produced by . 98 | /// 99 | /// The default implementation returns the result of calling 100 | /// on . 101 | /// 102 | /// 103 | /// The parse tree. 104 | /// The visitor result. 105 | public virtual Result VisitDeclareStatement([NotNull] TinyScriptParser.DeclareStatementContext context) { return VisitChildren(context); } 106 | /// 107 | /// Visit a parse tree produced by . 108 | /// 109 | /// The default implementation returns the result of calling 110 | /// on . 111 | /// 112 | /// 113 | /// The parse tree. 114 | /// The visitor result. 115 | public virtual Result VisitPrintStatement([NotNull] TinyScriptParser.PrintStatementContext context) { return VisitChildren(context); } 116 | /// 117 | /// Visit a parse tree produced by . 118 | /// 119 | /// The default implementation returns the result of calling 120 | /// on . 121 | /// 122 | /// 123 | /// The parse tree. 124 | /// The visitor result. 125 | public virtual Result VisitWhileStatement([NotNull] TinyScriptParser.WhileStatementContext context) { return VisitChildren(context); } 126 | /// 127 | /// Visit a parse tree produced by . 128 | /// 129 | /// The default implementation returns the result of calling 130 | /// on . 131 | /// 132 | /// 133 | /// The parse tree. 134 | /// The visitor result. 135 | public virtual Result VisitDoWhileStatement([NotNull] TinyScriptParser.DoWhileStatementContext context) { return VisitChildren(context); } 136 | /// 137 | /// Visit a parse tree produced by . 138 | /// 139 | /// The default implementation returns the result of calling 140 | /// on . 141 | /// 142 | /// 143 | /// The parse tree. 144 | /// The visitor result. 145 | public virtual Result VisitForStatement([NotNull] TinyScriptParser.ForStatementContext context) { return VisitChildren(context); } 146 | /// 147 | /// Visit a parse tree produced by . 148 | /// 149 | /// The default implementation returns the result of calling 150 | /// on . 151 | /// 152 | /// 153 | /// The parse tree. 154 | /// The visitor result. 155 | public virtual Result VisitCommonExpression([NotNull] TinyScriptParser.CommonExpressionContext context) { return VisitChildren(context); } 156 | /// 157 | /// Visit a parse tree produced by . 158 | /// 159 | /// The default implementation returns the result of calling 160 | /// on . 161 | /// 162 | /// 163 | /// The parse tree. 164 | /// The visitor result. 165 | public virtual Result VisitAssignAbleStatement([NotNull] TinyScriptParser.AssignAbleStatementContext context) { return VisitChildren(context); } 166 | /// 167 | /// Visit a parse tree produced by . 168 | /// 169 | /// The default implementation returns the result of calling 170 | /// on . 171 | /// 172 | /// 173 | /// The parse tree. 174 | /// The visitor result. 175 | public virtual Result VisitDeclareExpression([NotNull] TinyScriptParser.DeclareExpressionContext context) { return VisitChildren(context); } 176 | /// 177 | /// Visit a parse tree produced by . 178 | /// 179 | /// The default implementation returns the result of calling 180 | /// on . 181 | /// 182 | /// 183 | /// The parse tree. 184 | /// The visitor result. 185 | public virtual Result VisitExpression([NotNull] TinyScriptParser.ExpressionContext context) { return VisitChildren(context); } 186 | /// 187 | /// Visit a parse tree produced by . 188 | /// 189 | /// The default implementation returns the result of calling 190 | /// on . 191 | /// 192 | /// 193 | /// The parse tree. 194 | /// The visitor result. 195 | public virtual Result VisitAndAndExpression([NotNull] TinyScriptParser.AndAndExpressionContext context) { return VisitChildren(context); } 196 | /// 197 | /// Visit a parse tree produced by . 198 | /// 199 | /// The default implementation returns the result of calling 200 | /// on . 201 | /// 202 | /// 203 | /// The parse tree. 204 | /// The visitor result. 205 | public virtual Result VisitCmpExpression([NotNull] TinyScriptParser.CmpExpressionContext context) { return VisitChildren(context); } 206 | /// 207 | /// Visit a parse tree produced by . 208 | /// 209 | /// The default implementation returns the result of calling 210 | /// on . 211 | /// 212 | /// 213 | /// The parse tree. 214 | /// The visitor result. 215 | public virtual Result VisitAddExpression([NotNull] TinyScriptParser.AddExpressionContext context) { return VisitChildren(context); } 216 | /// 217 | /// Visit a parse tree produced by . 218 | /// 219 | /// The default implementation returns the result of calling 220 | /// on . 221 | /// 222 | /// 223 | /// The parse tree. 224 | /// The visitor result. 225 | public virtual Result VisitMulExpression([NotNull] TinyScriptParser.MulExpressionContext context) { return VisitChildren(context); } 226 | /// 227 | /// Visit a parse tree produced by . 228 | /// 229 | /// The default implementation returns the result of calling 230 | /// on . 231 | /// 232 | /// 233 | /// The parse tree. 234 | /// The visitor result. 235 | public virtual Result VisitUnaryExpression([NotNull] TinyScriptParser.UnaryExpressionContext context) { return VisitChildren(context); } 236 | /// 237 | /// Visit a parse tree produced by . 238 | /// 239 | /// The default implementation returns the result of calling 240 | /// on . 241 | /// 242 | /// 243 | /// The parse tree. 244 | /// The visitor result. 245 | public virtual Result VisitPrimaryExpression([NotNull] TinyScriptParser.PrimaryExpressionContext context) { return VisitChildren(context); } 246 | /// 247 | /// Visit a parse tree produced by . 248 | /// 249 | /// The default implementation returns the result of calling 250 | /// on . 251 | /// 252 | /// 253 | /// The parse tree. 254 | /// The visitor result. 255 | public virtual Result VisitVariableExpression([NotNull] TinyScriptParser.VariableExpressionContext context) { return VisitChildren(context); } 256 | /// 257 | /// Visit a parse tree produced by . 258 | /// 259 | /// The default implementation returns the result of calling 260 | /// on . 261 | /// 262 | /// 263 | /// The parse tree. 264 | /// The visitor result. 265 | public virtual Result VisitBasicType([NotNull] TinyScriptParser.BasicTypeContext context) { return VisitChildren(context); } 266 | /// 267 | /// Visit a parse tree produced by . 268 | /// 269 | /// The default implementation returns the result of calling 270 | /// on . 271 | /// 272 | /// 273 | /// The parse tree. 274 | /// The visitor result. 275 | public virtual Result VisitDeclarators([NotNull] TinyScriptParser.DeclaratorsContext context) { return VisitChildren(context); } 276 | /// 277 | /// Visit a parse tree produced by . 278 | /// 279 | /// The default implementation returns the result of calling 280 | /// on . 281 | /// 282 | /// 283 | /// The parse tree. 284 | /// The visitor result. 285 | public virtual Result VisitAssign([NotNull] TinyScriptParser.AssignContext context) { return VisitChildren(context); } 286 | /// 287 | /// Visit a parse tree produced by . 288 | /// 289 | /// The default implementation returns the result of calling 290 | /// on . 291 | /// 292 | /// 293 | /// The parse tree. 294 | /// The visitor result. 295 | public virtual Result VisitNumericLiteral([NotNull] TinyScriptParser.NumericLiteralContext context) { return VisitChildren(context); } 296 | } 297 | -------------------------------------------------------------------------------- /TinyScript/Parser/TinyScriptLexer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // ANTLR Version: 4.5.2 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | // Generated from TinyScript.g4 by ANTLR 4.5.2 12 | 13 | // Unreachable code detected 14 | #pragma warning disable 0162 15 | // The variable '...' is assigned but its value is never used 16 | #pragma warning disable 0219 17 | // Missing XML comment for publicly visible type or member '...' 18 | #pragma warning disable 1591 19 | // Ambiguous reference in cref attribute 20 | #pragma warning disable 419 21 | 22 | using System; 23 | using System.Text; 24 | using Antlr4.Runtime; 25 | using Antlr4.Runtime.Atn; 26 | using Antlr4.Runtime.Misc; 27 | using DFA = Antlr4.Runtime.Dfa.DFA; 28 | 29 | [System.CodeDom.Compiler.GeneratedCode("ANTLR", "4.5.2")] 30 | [System.CLSCompliant(false)] 31 | public partial class TinyScriptLexer : Lexer { 32 | public const int 33 | T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, T__5=6, T__6=7, T__7=8, T__8=9, 34 | T__9=10, T__10=11, T__11=12, T__12=13, T__13=14, T__14=15, T__15=16, T__16=17, 35 | T__17=18, T__18=19, T__19=20, T__20=21, T__21=22, T__22=23, T__23=24, 36 | T__24=25, T__25=26, T__26=27, T__27=28, T__28=29, T__29=30, T__30=31, 37 | T__31=32, StringLiteral=33, Decimal=34, Identifier=35, LineComment=36, 38 | Comment=37, WhiteSpace=38; 39 | public static string[] modeNames = { 40 | "DEFAULT_MODE" 41 | }; 42 | 43 | public static readonly string[] ruleNames = { 44 | "T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", "T__7", "T__8", 45 | "T__9", "T__10", "T__11", "T__12", "T__13", "T__14", "T__15", "T__16", 46 | "T__17", "T__18", "T__19", "T__20", "T__21", "T__22", "T__23", "T__24", 47 | "T__25", "T__26", "T__27", "T__28", "T__29", "T__30", "T__31", "StringLiteral", 48 | "Decimal", "NonZeroDigit", "DigitChar", "Identifier", "IdentifierChar", 49 | "IdentifierStart", "Letter", "LineComment", "Comment", "WhiteSpace" 50 | }; 51 | 52 | 53 | public TinyScriptLexer(ICharStream input) 54 | : base(input) 55 | { 56 | Interpreter = new LexerATNSimulator(this,_ATN); 57 | } 58 | 59 | private static readonly string[] _LiteralNames = { 60 | null, "'if'", "'else'", "'('", "')'", "'{'", "'}'", "';'", "'print'", 61 | "'while'", "'do'", "'for'", "'||'", "'&&'", "'=='", "'!='", "'<'", "'<='", 62 | "'>'", "'>='", "'+'", "'-'", "'*'", "'/'", "'!'", "'true'", "'false'", 63 | "'decimal'", "'string'", "'bool'", "'var'", "','", "'='" 64 | }; 65 | private static readonly string[] _SymbolicNames = { 66 | null, null, null, null, null, null, null, null, null, null, null, null, 67 | null, null, null, null, null, null, null, null, null, null, null, null, 68 | null, null, null, null, null, null, null, null, null, "StringLiteral", 69 | "Decimal", "Identifier", "LineComment", "Comment", "WhiteSpace" 70 | }; 71 | public static readonly IVocabulary DefaultVocabulary = new Vocabulary(_LiteralNames, _SymbolicNames); 72 | 73 | [NotNull] 74 | public override IVocabulary Vocabulary 75 | { 76 | get 77 | { 78 | return DefaultVocabulary; 79 | } 80 | } 81 | 82 | public override string GrammarFileName { get { return "TinyScript.g4"; } } 83 | 84 | public override string[] RuleNames { get { return ruleNames; } } 85 | 86 | public override string[] ModeNames { get { return modeNames; } } 87 | 88 | public override string SerializedAtn { get { return _serializedATN; } } 89 | 90 | private static string _serializedATN = _serializeATN(); 91 | private static string _serializeATN() 92 | { 93 | StringBuilder sb = new StringBuilder(); 94 | sb.Append("\x3\x430\xD6D1\x8206\xAD2D\x4417\xAEF1\x8D80\xAADD\x2(\x127"); 95 | sb.Append("\b\x1\x4\x2\t\x2\x4\x3\t\x3\x4\x4\t\x4\x4\x5\t\x5\x4\x6\t\x6"); 96 | sb.Append("\x4\a\t\a\x4\b\t\b\x4\t\t\t\x4\n\t\n\x4\v\t\v\x4\f\t\f\x4\r"); 97 | sb.Append("\t\r\x4\xE\t\xE\x4\xF\t\xF\x4\x10\t\x10\x4\x11\t\x11\x4\x12"); 98 | sb.Append("\t\x12\x4\x13\t\x13\x4\x14\t\x14\x4\x15\t\x15\x4\x16\t\x16\x4"); 99 | sb.Append("\x17\t\x17\x4\x18\t\x18\x4\x19\t\x19\x4\x1A\t\x1A\x4\x1B\t\x1B"); 100 | sb.Append("\x4\x1C\t\x1C\x4\x1D\t\x1D\x4\x1E\t\x1E\x4\x1F\t\x1F\x4 \t "); 101 | sb.Append("\x4!\t!\x4\"\t\"\x4#\t#\x4$\t$\x4%\t%\x4&\t&\x4\'\t\'\x4(\t"); 102 | sb.Append("(\x4)\t)\x4*\t*\x4+\t+\x4,\t,\x3\x2\x3\x2\x3\x2\x3\x3\x3\x3"); 103 | sb.Append("\x3\x3\x3\x3\x3\x3\x3\x4\x3\x4\x3\x5\x3\x5\x3\x6\x3\x6\x3\a"); 104 | sb.Append("\x3\a\x3\b\x3\b\x3\t\x3\t\x3\t\x3\t\x3\t\x3\t\x3\n\x3\n\x3\n"); 105 | sb.Append("\x3\n\x3\n\x3\n\x3\v\x3\v\x3\v\x3\f\x3\f\x3\f\x3\f\x3\r\x3\r"); 106 | sb.Append("\x3\r\x3\xE\x3\xE\x3\xE\x3\xF\x3\xF\x3\xF\x3\x10\x3\x10\x3\x10"); 107 | sb.Append("\x3\x11\x3\x11\x3\x12\x3\x12\x3\x12\x3\x13\x3\x13\x3\x14\x3"); 108 | sb.Append("\x14\x3\x14\x3\x15\x3\x15\x3\x16\x3\x16\x3\x17\x3\x17\x3\x18"); 109 | sb.Append("\x3\x18\x3\x19\x3\x19\x3\x1A\x3\x1A\x3\x1A\x3\x1A\x3\x1A\x3"); 110 | sb.Append("\x1B\x3\x1B\x3\x1B\x3\x1B\x3\x1B\x3\x1B\x3\x1C\x3\x1C\x3\x1C"); 111 | sb.Append("\x3\x1C\x3\x1C\x3\x1C\x3\x1C\x3\x1C\x3\x1D\x3\x1D\x3\x1D\x3"); 112 | sb.Append("\x1D\x3\x1D\x3\x1D\x3\x1D\x3\x1E\x3\x1E\x3\x1E\x3\x1E\x3\x1E"); 113 | sb.Append("\x3\x1F\x3\x1F\x3\x1F\x3\x1F\x3 \x3 \x3!\x3!\x3\"\x3\"\a\"\xC8"); 114 | sb.Append("\n\"\f\"\xE\"\xCB\v\"\x3\"\x3\"\x3#\x3#\x3#\a#\xD2\n#\f#\xE"); 115 | sb.Append("#\xD5\v#\x3#\x5#\xD8\n#\x3#\x3#\a#\xDC\n#\f#\xE#\xDF\v#\x3#"); 116 | sb.Append("\x3#\a#\xE3\n#\f#\xE#\xE6\v#\x3#\x5#\xE9\n#\x5#\xEB\n#\x3$\x3"); 117 | sb.Append("$\x3%\x3%\x3%\x5%\xF2\n%\x3&\x3&\a&\xF6\n&\f&\xE&\xF9\v&\x3"); 118 | sb.Append("\'\x3\'\x3\'\x5\'\xFE\n\'\x3(\x3(\x5(\x102\n(\x3)\x3)\x3*\x3"); 119 | sb.Append("*\x3*\x3*\a*\x10A\n*\f*\xE*\x10D\v*\x3*\x5*\x110\n*\x3*\x3*"); 120 | sb.Append("\x3*\x3*\x3+\x3+\x3+\x3+\a+\x11A\n+\f+\xE+\x11D\v+\x3+\x3+\x3"); 121 | sb.Append("+\x3+\x3+\x3,\x3,\x3,\x3,\x4\xC9\x11B\x2-\x3\x3\x5\x4\a\x5\t"); 122 | sb.Append("\x6\v\a\r\b\xF\t\x11\n\x13\v\x15\f\x17\r\x19\xE\x1B\xF\x1D\x10"); 123 | sb.Append("\x1F\x11!\x12#\x13%\x14\'\x15)\x16+\x17-\x18/\x19\x31\x1A\x33"); 124 | sb.Append("\x1B\x35\x1C\x37\x1D\x39\x1E;\x1F= ?!\x41\"\x43#\x45$G\x2I\x2"); 125 | sb.Append("K%M\x2O\x2Q\x2S&U\'W(\x3\x2\x6\x5\x2\f\f\xF\xF^^\x4\x2\x43\\"); 126 | sb.Append("\x63|\x4\x2\f\f\xF\xF\x5\x2\v\f\xF\xF\"\"\x131\x2\x3\x3\x2\x2"); 127 | sb.Append("\x2\x2\x5\x3\x2\x2\x2\x2\a\x3\x2\x2\x2\x2\t\x3\x2\x2\x2\x2\v"); 128 | sb.Append("\x3\x2\x2\x2\x2\r\x3\x2\x2\x2\x2\xF\x3\x2\x2\x2\x2\x11\x3\x2"); 129 | sb.Append("\x2\x2\x2\x13\x3\x2\x2\x2\x2\x15\x3\x2\x2\x2\x2\x17\x3\x2\x2"); 130 | sb.Append("\x2\x2\x19\x3\x2\x2\x2\x2\x1B\x3\x2\x2\x2\x2\x1D\x3\x2\x2\x2"); 131 | sb.Append("\x2\x1F\x3\x2\x2\x2\x2!\x3\x2\x2\x2\x2#\x3\x2\x2\x2\x2%\x3\x2"); 132 | sb.Append("\x2\x2\x2\'\x3\x2\x2\x2\x2)\x3\x2\x2\x2\x2+\x3\x2\x2\x2\x2-"); 133 | sb.Append("\x3\x2\x2\x2\x2/\x3\x2\x2\x2\x2\x31\x3\x2\x2\x2\x2\x33\x3\x2"); 134 | sb.Append("\x2\x2\x2\x35\x3\x2\x2\x2\x2\x37\x3\x2\x2\x2\x2\x39\x3\x2\x2"); 135 | sb.Append("\x2\x2;\x3\x2\x2\x2\x2=\x3\x2\x2\x2\x2?\x3\x2\x2\x2\x2\x41\x3"); 136 | sb.Append("\x2\x2\x2\x2\x43\x3\x2\x2\x2\x2\x45\x3\x2\x2\x2\x2K\x3\x2\x2"); 137 | sb.Append("\x2\x2S\x3\x2\x2\x2\x2U\x3\x2\x2\x2\x2W\x3\x2\x2\x2\x3Y\x3\x2"); 138 | sb.Append("\x2\x2\x5\\\x3\x2\x2\x2\a\x61\x3\x2\x2\x2\t\x63\x3\x2\x2\x2"); 139 | sb.Append("\v\x65\x3\x2\x2\x2\rg\x3\x2\x2\x2\xFi\x3\x2\x2\x2\x11k\x3\x2"); 140 | sb.Append("\x2\x2\x13q\x3\x2\x2\x2\x15w\x3\x2\x2\x2\x17z\x3\x2\x2\x2\x19"); 141 | sb.Append("~\x3\x2\x2\x2\x1B\x81\x3\x2\x2\x2\x1D\x84\x3\x2\x2\x2\x1F\x87"); 142 | sb.Append("\x3\x2\x2\x2!\x8A\x3\x2\x2\x2#\x8C\x3\x2\x2\x2%\x8F\x3\x2\x2"); 143 | sb.Append("\x2\'\x91\x3\x2\x2\x2)\x94\x3\x2\x2\x2+\x96\x3\x2\x2\x2-\x98"); 144 | sb.Append("\x3\x2\x2\x2/\x9A\x3\x2\x2\x2\x31\x9C\x3\x2\x2\x2\x33\x9E\x3"); 145 | sb.Append("\x2\x2\x2\x35\xA3\x3\x2\x2\x2\x37\xA9\x3\x2\x2\x2\x39\xB1\x3"); 146 | sb.Append("\x2\x2\x2;\xB8\x3\x2\x2\x2=\xBD\x3\x2\x2\x2?\xC1\x3\x2\x2\x2"); 147 | sb.Append("\x41\xC3\x3\x2\x2\x2\x43\xC5\x3\x2\x2\x2\x45\xEA\x3\x2\x2\x2"); 148 | sb.Append("G\xEC\x3\x2\x2\x2I\xF1\x3\x2\x2\x2K\xF3\x3\x2\x2\x2M\xFD\x3"); 149 | sb.Append("\x2\x2\x2O\x101\x3\x2\x2\x2Q\x103\x3\x2\x2\x2S\x105\x3\x2\x2"); 150 | sb.Append("\x2U\x115\x3\x2\x2\x2W\x123\x3\x2\x2\x2YZ\ak\x2\x2Z[\ah\x2\x2"); 151 | sb.Append("[\x4\x3\x2\x2\x2\\]\ag\x2\x2]^\an\x2\x2^_\au\x2\x2_`\ag\x2\x2"); 152 | sb.Append("`\x6\x3\x2\x2\x2\x61\x62\a*\x2\x2\x62\b\x3\x2\x2\x2\x63\x64"); 153 | sb.Append("\a+\x2\x2\x64\n\x3\x2\x2\x2\x65\x66\a}\x2\x2\x66\f\x3\x2\x2"); 154 | sb.Append("\x2gh\a\x7F\x2\x2h\xE\x3\x2\x2\x2ij\a=\x2\x2j\x10\x3\x2\x2\x2"); 155 | sb.Append("kl\ar\x2\x2lm\at\x2\x2mn\ak\x2\x2no\ap\x2\x2op\av\x2\x2p\x12"); 156 | sb.Append("\x3\x2\x2\x2qr\ay\x2\x2rs\aj\x2\x2st\ak\x2\x2tu\an\x2\x2uv\a"); 157 | sb.Append("g\x2\x2v\x14\x3\x2\x2\x2wx\a\x66\x2\x2xy\aq\x2\x2y\x16\x3\x2"); 158 | sb.Append("\x2\x2z{\ah\x2\x2{|\aq\x2\x2|}\at\x2\x2}\x18\x3\x2\x2\x2~\x7F"); 159 | sb.Append("\a~\x2\x2\x7F\x80\a~\x2\x2\x80\x1A\x3\x2\x2\x2\x81\x82\a(\x2"); 160 | sb.Append("\x2\x82\x83\a(\x2\x2\x83\x1C\x3\x2\x2\x2\x84\x85\a?\x2\x2\x85"); 161 | sb.Append("\x86\a?\x2\x2\x86\x1E\x3\x2\x2\x2\x87\x88\a#\x2\x2\x88\x89\a"); 162 | sb.Append("?\x2\x2\x89 \x3\x2\x2\x2\x8A\x8B\a>\x2\x2\x8B\"\x3\x2\x2\x2"); 163 | sb.Append("\x8C\x8D\a>\x2\x2\x8D\x8E\a?\x2\x2\x8E$\x3\x2\x2\x2\x8F\x90"); 164 | sb.Append("\a@\x2\x2\x90&\x3\x2\x2\x2\x91\x92\a@\x2\x2\x92\x93\a?\x2\x2"); 165 | sb.Append("\x93(\x3\x2\x2\x2\x94\x95\a-\x2\x2\x95*\x3\x2\x2\x2\x96\x97"); 166 | sb.Append("\a/\x2\x2\x97,\x3\x2\x2\x2\x98\x99\a,\x2\x2\x99.\x3\x2\x2\x2"); 167 | sb.Append("\x9A\x9B\a\x31\x2\x2\x9B\x30\x3\x2\x2\x2\x9C\x9D\a#\x2\x2\x9D"); 168 | sb.Append("\x32\x3\x2\x2\x2\x9E\x9F\av\x2\x2\x9F\xA0\at\x2\x2\xA0\xA1\a"); 169 | sb.Append("w\x2\x2\xA1\xA2\ag\x2\x2\xA2\x34\x3\x2\x2\x2\xA3\xA4\ah\x2\x2"); 170 | sb.Append("\xA4\xA5\a\x63\x2\x2\xA5\xA6\an\x2\x2\xA6\xA7\au\x2\x2\xA7\xA8"); 171 | sb.Append("\ag\x2\x2\xA8\x36\x3\x2\x2\x2\xA9\xAA\a\x66\x2\x2\xAA\xAB\a"); 172 | sb.Append("g\x2\x2\xAB\xAC\a\x65\x2\x2\xAC\xAD\ak\x2\x2\xAD\xAE\ao\x2\x2"); 173 | sb.Append("\xAE\xAF\a\x63\x2\x2\xAF\xB0\an\x2\x2\xB0\x38\x3\x2\x2\x2\xB1"); 174 | sb.Append("\xB2\au\x2\x2\xB2\xB3\av\x2\x2\xB3\xB4\at\x2\x2\xB4\xB5\ak\x2"); 175 | sb.Append("\x2\xB5\xB6\ap\x2\x2\xB6\xB7\ai\x2\x2\xB7:\x3\x2\x2\x2\xB8\xB9"); 176 | sb.Append("\a\x64\x2\x2\xB9\xBA\aq\x2\x2\xBA\xBB\aq\x2\x2\xBB\xBC\an\x2"); 177 | sb.Append("\x2\xBC<\x3\x2\x2\x2\xBD\xBE\ax\x2\x2\xBE\xBF\a\x63\x2\x2\xBF"); 178 | sb.Append("\xC0\at\x2\x2\xC0>\x3\x2\x2\x2\xC1\xC2\a.\x2\x2\xC2@\x3\x2\x2"); 179 | sb.Append("\x2\xC3\xC4\a?\x2\x2\xC4\x42\x3\x2\x2\x2\xC5\xC9\a$\x2\x2\xC6"); 180 | sb.Append("\xC8\n\x2\x2\x2\xC7\xC6\x3\x2\x2\x2\xC8\xCB\x3\x2\x2\x2\xC9"); 181 | sb.Append("\xCA\x3\x2\x2\x2\xC9\xC7\x3\x2\x2\x2\xCA\xCC\x3\x2\x2\x2\xCB"); 182 | sb.Append("\xC9\x3\x2\x2\x2\xCC\xCD\a$\x2\x2\xCD\x44\x3\x2\x2\x2\xCE\xD7"); 183 | sb.Append("\a\x32\x2\x2\xCF\xD3\a\x30\x2\x2\xD0\xD2\x5I%\x2\xD1\xD0\x3"); 184 | sb.Append("\x2\x2\x2\xD2\xD5\x3\x2\x2\x2\xD3\xD1\x3\x2\x2\x2\xD3\xD4\x3"); 185 | sb.Append("\x2\x2\x2\xD4\xD6\x3\x2\x2\x2\xD5\xD3\x3\x2\x2\x2\xD6\xD8\x5"); 186 | sb.Append("G$\x2\xD7\xCF\x3\x2\x2\x2\xD7\xD8\x3\x2\x2\x2\xD8\xEB\x3\x2"); 187 | sb.Append("\x2\x2\xD9\xDD\x5G$\x2\xDA\xDC\x5I%\x2\xDB\xDA\x3\x2\x2\x2\xDC"); 188 | sb.Append("\xDF\x3\x2\x2\x2\xDD\xDB\x3\x2\x2\x2\xDD\xDE\x3\x2\x2\x2\xDE"); 189 | sb.Append("\xE8\x3\x2\x2\x2\xDF\xDD\x3\x2\x2\x2\xE0\xE4\a\x30\x2\x2\xE1"); 190 | sb.Append("\xE3\x5I%\x2\xE2\xE1\x3\x2\x2\x2\xE3\xE6\x3\x2\x2\x2\xE4\xE2"); 191 | sb.Append("\x3\x2\x2\x2\xE4\xE5\x3\x2\x2\x2\xE5\xE7\x3\x2\x2\x2\xE6\xE4"); 192 | sb.Append("\x3\x2\x2\x2\xE7\xE9\x5G$\x2\xE8\xE0\x3\x2\x2\x2\xE8\xE9\x3"); 193 | sb.Append("\x2\x2\x2\xE9\xEB\x3\x2\x2\x2\xEA\xCE\x3\x2\x2\x2\xEA\xD9\x3"); 194 | sb.Append("\x2\x2\x2\xEB\x46\x3\x2\x2\x2\xEC\xED\x4\x33;\x2\xEDH\x3\x2"); 195 | sb.Append("\x2\x2\xEE\xF2\a\x32\x2\x2\xEF\xF2\x5G$\x2\xF0\xF2\a\x61\x2"); 196 | sb.Append("\x2\xF1\xEE\x3\x2\x2\x2\xF1\xEF\x3\x2\x2\x2\xF1\xF0\x3\x2\x2"); 197 | sb.Append("\x2\xF2J\x3\x2\x2\x2\xF3\xF7\x5O(\x2\xF4\xF6\x5M\'\x2\xF5\xF4"); 198 | sb.Append("\x3\x2\x2\x2\xF6\xF9\x3\x2\x2\x2\xF7\xF5\x3\x2\x2\x2\xF7\xF8"); 199 | sb.Append("\x3\x2\x2\x2\xF8L\x3\x2\x2\x2\xF9\xF7\x3\x2\x2\x2\xFA\xFE\x5"); 200 | sb.Append("O(\x2\xFB\xFE\a\x32\x2\x2\xFC\xFE\x5G$\x2\xFD\xFA\x3\x2\x2\x2"); 201 | sb.Append("\xFD\xFB\x3\x2\x2\x2\xFD\xFC\x3\x2\x2\x2\xFEN\x3\x2\x2\x2\xFF"); 202 | sb.Append("\x102\a\x61\x2\x2\x100\x102\x5Q)\x2\x101\xFF\x3\x2\x2\x2\x101"); 203 | sb.Append("\x100\x3\x2\x2\x2\x102P\x3\x2\x2\x2\x103\x104\t\x3\x2\x2\x104"); 204 | sb.Append("R\x3\x2\x2\x2\x105\x106\a\x31\x2\x2\x106\x107\a\x31\x2\x2\x107"); 205 | sb.Append("\x10B\x3\x2\x2\x2\x108\x10A\n\x4\x2\x2\x109\x108\x3\x2\x2\x2"); 206 | sb.Append("\x10A\x10D\x3\x2\x2\x2\x10B\x109\x3\x2\x2\x2\x10B\x10C\x3\x2"); 207 | sb.Append("\x2\x2\x10C\x10F\x3\x2\x2\x2\x10D\x10B\x3\x2\x2\x2\x10E\x110"); 208 | sb.Append("\a\xF\x2\x2\x10F\x10E\x3\x2\x2\x2\x10F\x110\x3\x2\x2\x2\x110"); 209 | sb.Append("\x111\x3\x2\x2\x2\x111\x112\a\f\x2\x2\x112\x113\x3\x2\x2\x2"); 210 | sb.Append("\x113\x114\b*\x2\x2\x114T\x3\x2\x2\x2\x115\x116\a\x31\x2\x2"); 211 | sb.Append("\x116\x117\a,\x2\x2\x117\x11B\x3\x2\x2\x2\x118\x11A\v\x2\x2"); 212 | sb.Append("\x2\x119\x118\x3\x2\x2\x2\x11A\x11D\x3\x2\x2\x2\x11B\x11C\x3"); 213 | sb.Append("\x2\x2\x2\x11B\x119\x3\x2\x2\x2\x11C\x11E\x3\x2\x2\x2\x11D\x11B"); 214 | sb.Append("\x3\x2\x2\x2\x11E\x11F\a,\x2\x2\x11F\x120\a\x31\x2\x2\x120\x121"); 215 | sb.Append("\x3\x2\x2\x2\x121\x122\b+\x2\x2\x122V\x3\x2\x2\x2\x123\x124"); 216 | sb.Append("\t\x5\x2\x2\x124\x125\x3\x2\x2\x2\x125\x126\b,\x2\x2\x126X\x3"); 217 | sb.Append("\x2\x2\x2\x11\x2\xC9\xD3\xD7\xDD\xE4\xE8\xEA\xF1\xF7\xFD\x101"); 218 | sb.Append("\x10B\x10F\x11B\x3\b\x2\x2"); 219 | return sb.ToString(); 220 | } 221 | 222 | public static readonly ATN _ATN = 223 | new ATNDeserializer().Deserialize(_serializedATN.ToCharArray()); 224 | } 225 | -------------------------------------------------------------------------------- /TinyScript/Parser/TinyScriptLexer.tokens: -------------------------------------------------------------------------------- 1 | T__0=1 2 | T__1=2 3 | T__2=3 4 | T__3=4 5 | T__4=5 6 | T__5=6 7 | T__6=7 8 | T__7=8 9 | T__8=9 10 | T__9=10 11 | T__10=11 12 | T__11=12 13 | T__12=13 14 | T__13=14 15 | T__14=15 16 | T__15=16 17 | T__16=17 18 | T__17=18 19 | T__18=19 20 | T__19=20 21 | T__20=21 22 | T__21=22 23 | T__22=23 24 | T__23=24 25 | T__24=25 26 | T__25=26 27 | T__26=27 28 | T__27=28 29 | T__28=29 30 | T__29=30 31 | T__30=31 32 | T__31=32 33 | StringLiteral=33 34 | Decimal=34 35 | Identifier=35 36 | LineComment=36 37 | Comment=37 38 | WhiteSpace=38 39 | 'if'=1 40 | 'else'=2 41 | '('=3 42 | ')'=4 43 | '{'=5 44 | '}'=6 45 | ';'=7 46 | 'print'=8 47 | 'while'=9 48 | 'do'=10 49 | 'for'=11 50 | '||'=12 51 | '&&'=13 52 | '=='=14 53 | '!='=15 54 | '<'=16 55 | '<='=17 56 | '>'=18 57 | '>='=19 58 | '+'=20 59 | '-'=21 60 | '*'=22 61 | '/'=23 62 | '!'=24 63 | 'true'=25 64 | 'false'=26 65 | 'decimal'=27 66 | 'string'=28 67 | 'bool'=29 68 | 'var'=30 69 | ','=31 70 | '='=32 71 | -------------------------------------------------------------------------------- /TinyScript/Parser/TinyScriptParser.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // ANTLR Version: 4.5.2 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | // Generated from TinyScript.g4 by ANTLR 4.5.2 12 | 13 | // Unreachable code detected 14 | #pragma warning disable 0162 15 | // The variable '...' is assigned but its value is never used 16 | #pragma warning disable 0219 17 | // Missing XML comment for publicly visible type or member '...' 18 | #pragma warning disable 1591 19 | // Ambiguous reference in cref attribute 20 | #pragma warning disable 419 21 | 22 | using System; 23 | using System.Text; 24 | using System.Diagnostics; 25 | using System.Collections.Generic; 26 | using Antlr4.Runtime; 27 | using Antlr4.Runtime.Atn; 28 | using Antlr4.Runtime.Misc; 29 | using Antlr4.Runtime.Tree; 30 | using DFA = Antlr4.Runtime.Dfa.DFA; 31 | 32 | [System.CodeDom.Compiler.GeneratedCode("ANTLR", "4.5.2")] 33 | [System.CLSCompliant(false)] 34 | public partial class TinyScriptParser : Parser { 35 | public const int 36 | T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, T__5=6, T__6=7, T__7=8, T__8=9, 37 | T__9=10, T__10=11, T__11=12, T__12=13, T__13=14, T__14=15, T__15=16, T__16=17, 38 | T__17=18, T__18=19, T__19=20, T__20=21, T__21=22, T__22=23, T__23=24, 39 | T__24=25, T__25=26, T__26=27, T__27=28, T__28=29, T__29=30, T__30=31, 40 | T__31=32, StringLiteral=33, Decimal=34, Identifier=35, LineComment=36, 41 | Comment=37, WhiteSpace=38; 42 | public const int 43 | RULE_program = 0, RULE_statement = 1, RULE_ifStatement = 2, RULE_quoteExpr = 3, 44 | RULE_blockStatement = 4, RULE_assignStatement = 5, RULE_declareStatement = 6, 45 | RULE_printStatement = 7, RULE_whileStatement = 8, RULE_doWhileStatement = 9, 46 | RULE_forStatement = 10, RULE_commonExpression = 11, RULE_assignAbleStatement = 12, 47 | RULE_declareExpression = 13, RULE_expression = 14, RULE_andAndExpression = 15, 48 | RULE_cmpExpression = 16, RULE_addExpression = 17, RULE_mulExpression = 18, 49 | RULE_unaryExpression = 19, RULE_primaryExpression = 20, RULE_variableExpression = 21, 50 | RULE_basicType = 22, RULE_declarators = 23, RULE_assign = 24, RULE_numericLiteral = 25; 51 | public static readonly string[] ruleNames = { 52 | "program", "statement", "ifStatement", "quoteExpr", "blockStatement", 53 | "assignStatement", "declareStatement", "printStatement", "whileStatement", 54 | "doWhileStatement", "forStatement", "commonExpression", "assignAbleStatement", 55 | "declareExpression", "expression", "andAndExpression", "cmpExpression", 56 | "addExpression", "mulExpression", "unaryExpression", "primaryExpression", 57 | "variableExpression", "basicType", "declarators", "assign", "numericLiteral" 58 | }; 59 | 60 | private static readonly string[] _LiteralNames = { 61 | null, "'if'", "'else'", "'('", "')'", "'{'", "'}'", "';'", "'print'", 62 | "'while'", "'do'", "'for'", "'||'", "'&&'", "'=='", "'!='", "'<'", "'<='", 63 | "'>'", "'>='", "'+'", "'-'", "'*'", "'/'", "'!'", "'true'", "'false'", 64 | "'decimal'", "'string'", "'bool'", "'var'", "','", "'='" 65 | }; 66 | private static readonly string[] _SymbolicNames = { 67 | null, null, null, null, null, null, null, null, null, null, null, null, 68 | null, null, null, null, null, null, null, null, null, null, null, null, 69 | null, null, null, null, null, null, null, null, null, "StringLiteral", 70 | "Decimal", "Identifier", "LineComment", "Comment", "WhiteSpace" 71 | }; 72 | public static readonly IVocabulary DefaultVocabulary = new Vocabulary(_LiteralNames, _SymbolicNames); 73 | 74 | [NotNull] 75 | public override IVocabulary Vocabulary 76 | { 77 | get 78 | { 79 | return DefaultVocabulary; 80 | } 81 | } 82 | 83 | public override string GrammarFileName { get { return "TinyScript.g4"; } } 84 | 85 | public override string[] RuleNames { get { return ruleNames; } } 86 | 87 | public override string SerializedAtn { get { return _serializedATN; } } 88 | 89 | public TinyScriptParser(ITokenStream input) 90 | : base(input) 91 | { 92 | Interpreter = new ParserATNSimulator(this,_ATN); 93 | } 94 | public partial class ProgramContext : ParserRuleContext { 95 | public ITerminalNode Eof() { return GetToken(TinyScriptParser.Eof, 0); } 96 | public StatementContext[] statement() { 97 | return GetRuleContexts(); 98 | } 99 | public StatementContext statement(int i) { 100 | return GetRuleContext(i); 101 | } 102 | public ProgramContext(ParserRuleContext parent, int invokingState) 103 | : base(parent, invokingState) 104 | { 105 | } 106 | public override int RuleIndex { get { return RULE_program; } } 107 | public override TResult Accept(IParseTreeVisitor visitor) { 108 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 109 | if (typedVisitor != null) return typedVisitor.VisitProgram(this); 110 | else return visitor.VisitChildren(this); 111 | } 112 | } 113 | 114 | [RuleVersion(0)] 115 | public ProgramContext program() { 116 | ProgramContext _localctx = new ProgramContext(Context, State); 117 | EnterRule(_localctx, 0, RULE_program); 118 | int _la; 119 | try { 120 | EnterOuterAlt(_localctx, 1); 121 | { 122 | State = 53; 123 | ErrorHandler.Sync(this); 124 | _la = TokenStream.La(1); 125 | do { 126 | { 127 | { 128 | State = 52; statement(); 129 | } 130 | } 131 | State = 55; 132 | ErrorHandler.Sync(this); 133 | _la = TokenStream.La(1); 134 | } while ( (((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__0) | (1L << T__4) | (1L << T__7) | (1L << T__8) | (1L << T__9) | (1L << T__10) | (1L << T__26) | (1L << T__27) | (1L << T__28) | (1L << T__29) | (1L << Identifier))) != 0) ); 135 | State = 57; Match(Eof); 136 | } 137 | } 138 | catch (RecognitionException re) { 139 | _localctx.exception = re; 140 | ErrorHandler.ReportError(this, re); 141 | ErrorHandler.Recover(this, re); 142 | } 143 | finally { 144 | ExitRule(); 145 | } 146 | return _localctx; 147 | } 148 | 149 | public partial class StatementContext : ParserRuleContext { 150 | public IfStatementContext ifStatement() { 151 | return GetRuleContext(0); 152 | } 153 | public BlockStatementContext blockStatement() { 154 | return GetRuleContext(0); 155 | } 156 | public AssignStatementContext assignStatement() { 157 | return GetRuleContext(0); 158 | } 159 | public DeclareStatementContext declareStatement() { 160 | return GetRuleContext(0); 161 | } 162 | public PrintStatementContext printStatement() { 163 | return GetRuleContext(0); 164 | } 165 | public WhileStatementContext whileStatement() { 166 | return GetRuleContext(0); 167 | } 168 | public DoWhileStatementContext doWhileStatement() { 169 | return GetRuleContext(0); 170 | } 171 | public ForStatementContext forStatement() { 172 | return GetRuleContext(0); 173 | } 174 | public StatementContext(ParserRuleContext parent, int invokingState) 175 | : base(parent, invokingState) 176 | { 177 | } 178 | public override int RuleIndex { get { return RULE_statement; } } 179 | public override TResult Accept(IParseTreeVisitor visitor) { 180 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 181 | if (typedVisitor != null) return typedVisitor.VisitStatement(this); 182 | else return visitor.VisitChildren(this); 183 | } 184 | } 185 | 186 | [RuleVersion(0)] 187 | public StatementContext statement() { 188 | StatementContext _localctx = new StatementContext(Context, State); 189 | EnterRule(_localctx, 2, RULE_statement); 190 | try { 191 | State = 67; 192 | switch (TokenStream.La(1)) { 193 | case T__0: 194 | EnterOuterAlt(_localctx, 1); 195 | { 196 | State = 59; ifStatement(); 197 | } 198 | break; 199 | case T__4: 200 | EnterOuterAlt(_localctx, 2); 201 | { 202 | State = 60; blockStatement(); 203 | } 204 | break; 205 | case Identifier: 206 | EnterOuterAlt(_localctx, 3); 207 | { 208 | State = 61; assignStatement(); 209 | } 210 | break; 211 | case T__26: 212 | case T__27: 213 | case T__28: 214 | case T__29: 215 | EnterOuterAlt(_localctx, 4); 216 | { 217 | State = 62; declareStatement(); 218 | } 219 | break; 220 | case T__7: 221 | EnterOuterAlt(_localctx, 5); 222 | { 223 | State = 63; printStatement(); 224 | } 225 | break; 226 | case T__8: 227 | EnterOuterAlt(_localctx, 6); 228 | { 229 | State = 64; whileStatement(); 230 | } 231 | break; 232 | case T__9: 233 | EnterOuterAlt(_localctx, 7); 234 | { 235 | State = 65; doWhileStatement(); 236 | } 237 | break; 238 | case T__10: 239 | EnterOuterAlt(_localctx, 8); 240 | { 241 | State = 66; forStatement(); 242 | } 243 | break; 244 | default: 245 | throw new NoViableAltException(this); 246 | } 247 | } 248 | catch (RecognitionException re) { 249 | _localctx.exception = re; 250 | ErrorHandler.ReportError(this, re); 251 | ErrorHandler.Recover(this, re); 252 | } 253 | finally { 254 | ExitRule(); 255 | } 256 | return _localctx; 257 | } 258 | 259 | public partial class IfStatementContext : ParserRuleContext { 260 | public QuoteExprContext quoteExpr() { 261 | return GetRuleContext(0); 262 | } 263 | public BlockStatementContext[] blockStatement() { 264 | return GetRuleContexts(); 265 | } 266 | public BlockStatementContext blockStatement(int i) { 267 | return GetRuleContext(i); 268 | } 269 | public IfStatementContext(ParserRuleContext parent, int invokingState) 270 | : base(parent, invokingState) 271 | { 272 | } 273 | public override int RuleIndex { get { return RULE_ifStatement; } } 274 | public override TResult Accept(IParseTreeVisitor visitor) { 275 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 276 | if (typedVisitor != null) return typedVisitor.VisitIfStatement(this); 277 | else return visitor.VisitChildren(this); 278 | } 279 | } 280 | 281 | [RuleVersion(0)] 282 | public IfStatementContext ifStatement() { 283 | IfStatementContext _localctx = new IfStatementContext(Context, State); 284 | EnterRule(_localctx, 4, RULE_ifStatement); 285 | try { 286 | State = 79; 287 | ErrorHandler.Sync(this); 288 | switch ( Interpreter.AdaptivePredict(TokenStream,2,Context) ) { 289 | case 1: 290 | EnterOuterAlt(_localctx, 1); 291 | { 292 | State = 69; Match(T__0); 293 | State = 70; quoteExpr(); 294 | State = 71; blockStatement(); 295 | } 296 | break; 297 | case 2: 298 | EnterOuterAlt(_localctx, 2); 299 | { 300 | State = 73; Match(T__0); 301 | State = 74; quoteExpr(); 302 | State = 75; blockStatement(); 303 | State = 76; Match(T__1); 304 | State = 77; blockStatement(); 305 | } 306 | break; 307 | } 308 | } 309 | catch (RecognitionException re) { 310 | _localctx.exception = re; 311 | ErrorHandler.ReportError(this, re); 312 | ErrorHandler.Recover(this, re); 313 | } 314 | finally { 315 | ExitRule(); 316 | } 317 | return _localctx; 318 | } 319 | 320 | public partial class QuoteExprContext : ParserRuleContext { 321 | public ExpressionContext expression() { 322 | return GetRuleContext(0); 323 | } 324 | public QuoteExprContext(ParserRuleContext parent, int invokingState) 325 | : base(parent, invokingState) 326 | { 327 | } 328 | public override int RuleIndex { get { return RULE_quoteExpr; } } 329 | public override TResult Accept(IParseTreeVisitor visitor) { 330 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 331 | if (typedVisitor != null) return typedVisitor.VisitQuoteExpr(this); 332 | else return visitor.VisitChildren(this); 333 | } 334 | } 335 | 336 | [RuleVersion(0)] 337 | public QuoteExprContext quoteExpr() { 338 | QuoteExprContext _localctx = new QuoteExprContext(Context, State); 339 | EnterRule(_localctx, 6, RULE_quoteExpr); 340 | try { 341 | EnterOuterAlt(_localctx, 1); 342 | { 343 | State = 81; Match(T__2); 344 | State = 82; expression(); 345 | State = 83; Match(T__3); 346 | } 347 | } 348 | catch (RecognitionException re) { 349 | _localctx.exception = re; 350 | ErrorHandler.ReportError(this, re); 351 | ErrorHandler.Recover(this, re); 352 | } 353 | finally { 354 | ExitRule(); 355 | } 356 | return _localctx; 357 | } 358 | 359 | public partial class BlockStatementContext : ParserRuleContext { 360 | public StatementContext[] statement() { 361 | return GetRuleContexts(); 362 | } 363 | public StatementContext statement(int i) { 364 | return GetRuleContext(i); 365 | } 366 | public BlockStatementContext(ParserRuleContext parent, int invokingState) 367 | : base(parent, invokingState) 368 | { 369 | } 370 | public override int RuleIndex { get { return RULE_blockStatement; } } 371 | public override TResult Accept(IParseTreeVisitor visitor) { 372 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 373 | if (typedVisitor != null) return typedVisitor.VisitBlockStatement(this); 374 | else return visitor.VisitChildren(this); 375 | } 376 | } 377 | 378 | [RuleVersion(0)] 379 | public BlockStatementContext blockStatement() { 380 | BlockStatementContext _localctx = new BlockStatementContext(Context, State); 381 | EnterRule(_localctx, 8, RULE_blockStatement); 382 | int _la; 383 | try { 384 | EnterOuterAlt(_localctx, 1); 385 | { 386 | State = 85; Match(T__4); 387 | State = 89; 388 | ErrorHandler.Sync(this); 389 | _la = TokenStream.La(1); 390 | while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__0) | (1L << T__4) | (1L << T__7) | (1L << T__8) | (1L << T__9) | (1L << T__10) | (1L << T__26) | (1L << T__27) | (1L << T__28) | (1L << T__29) | (1L << Identifier))) != 0)) { 391 | { 392 | { 393 | State = 86; statement(); 394 | } 395 | } 396 | State = 91; 397 | ErrorHandler.Sync(this); 398 | _la = TokenStream.La(1); 399 | } 400 | State = 92; Match(T__5); 401 | } 402 | } 403 | catch (RecognitionException re) { 404 | _localctx.exception = re; 405 | ErrorHandler.ReportError(this, re); 406 | ErrorHandler.Recover(this, re); 407 | } 408 | finally { 409 | ExitRule(); 410 | } 411 | return _localctx; 412 | } 413 | 414 | public partial class AssignStatementContext : ParserRuleContext { 415 | public AssignContext assign() { 416 | return GetRuleContext(0); 417 | } 418 | public AssignStatementContext(ParserRuleContext parent, int invokingState) 419 | : base(parent, invokingState) 420 | { 421 | } 422 | public override int RuleIndex { get { return RULE_assignStatement; } } 423 | public override TResult Accept(IParseTreeVisitor visitor) { 424 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 425 | if (typedVisitor != null) return typedVisitor.VisitAssignStatement(this); 426 | else return visitor.VisitChildren(this); 427 | } 428 | } 429 | 430 | [RuleVersion(0)] 431 | public AssignStatementContext assignStatement() { 432 | AssignStatementContext _localctx = new AssignStatementContext(Context, State); 433 | EnterRule(_localctx, 10, RULE_assignStatement); 434 | try { 435 | EnterOuterAlt(_localctx, 1); 436 | { 437 | State = 94; assign(); 438 | State = 95; Match(T__6); 439 | } 440 | } 441 | catch (RecognitionException re) { 442 | _localctx.exception = re; 443 | ErrorHandler.ReportError(this, re); 444 | ErrorHandler.Recover(this, re); 445 | } 446 | finally { 447 | ExitRule(); 448 | } 449 | return _localctx; 450 | } 451 | 452 | public partial class DeclareStatementContext : ParserRuleContext { 453 | public DeclareExpressionContext declareExpression() { 454 | return GetRuleContext(0); 455 | } 456 | public DeclareStatementContext(ParserRuleContext parent, int invokingState) 457 | : base(parent, invokingState) 458 | { 459 | } 460 | public override int RuleIndex { get { return RULE_declareStatement; } } 461 | public override TResult Accept(IParseTreeVisitor visitor) { 462 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 463 | if (typedVisitor != null) return typedVisitor.VisitDeclareStatement(this); 464 | else return visitor.VisitChildren(this); 465 | } 466 | } 467 | 468 | [RuleVersion(0)] 469 | public DeclareStatementContext declareStatement() { 470 | DeclareStatementContext _localctx = new DeclareStatementContext(Context, State); 471 | EnterRule(_localctx, 12, RULE_declareStatement); 472 | try { 473 | EnterOuterAlt(_localctx, 1); 474 | { 475 | State = 97; declareExpression(); 476 | State = 98; Match(T__6); 477 | } 478 | } 479 | catch (RecognitionException re) { 480 | _localctx.exception = re; 481 | ErrorHandler.ReportError(this, re); 482 | ErrorHandler.Recover(this, re); 483 | } 484 | finally { 485 | ExitRule(); 486 | } 487 | return _localctx; 488 | } 489 | 490 | public partial class PrintStatementContext : ParserRuleContext { 491 | public ExpressionContext expression() { 492 | return GetRuleContext(0); 493 | } 494 | public PrintStatementContext(ParserRuleContext parent, int invokingState) 495 | : base(parent, invokingState) 496 | { 497 | } 498 | public override int RuleIndex { get { return RULE_printStatement; } } 499 | public override TResult Accept(IParseTreeVisitor visitor) { 500 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 501 | if (typedVisitor != null) return typedVisitor.VisitPrintStatement(this); 502 | else return visitor.VisitChildren(this); 503 | } 504 | } 505 | 506 | [RuleVersion(0)] 507 | public PrintStatementContext printStatement() { 508 | PrintStatementContext _localctx = new PrintStatementContext(Context, State); 509 | EnterRule(_localctx, 14, RULE_printStatement); 510 | try { 511 | EnterOuterAlt(_localctx, 1); 512 | { 513 | State = 100; Match(T__7); 514 | State = 101; Match(T__2); 515 | State = 102; expression(); 516 | State = 103; Match(T__3); 517 | State = 104; Match(T__6); 518 | } 519 | } 520 | catch (RecognitionException re) { 521 | _localctx.exception = re; 522 | ErrorHandler.ReportError(this, re); 523 | ErrorHandler.Recover(this, re); 524 | } 525 | finally { 526 | ExitRule(); 527 | } 528 | return _localctx; 529 | } 530 | 531 | public partial class WhileStatementContext : ParserRuleContext { 532 | public ExpressionContext expression() { 533 | return GetRuleContext(0); 534 | } 535 | public BlockStatementContext blockStatement() { 536 | return GetRuleContext(0); 537 | } 538 | public WhileStatementContext(ParserRuleContext parent, int invokingState) 539 | : base(parent, invokingState) 540 | { 541 | } 542 | public override int RuleIndex { get { return RULE_whileStatement; } } 543 | public override TResult Accept(IParseTreeVisitor visitor) { 544 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 545 | if (typedVisitor != null) return typedVisitor.VisitWhileStatement(this); 546 | else return visitor.VisitChildren(this); 547 | } 548 | } 549 | 550 | [RuleVersion(0)] 551 | public WhileStatementContext whileStatement() { 552 | WhileStatementContext _localctx = new WhileStatementContext(Context, State); 553 | EnterRule(_localctx, 16, RULE_whileStatement); 554 | try { 555 | EnterOuterAlt(_localctx, 1); 556 | { 557 | State = 106; Match(T__8); 558 | State = 107; Match(T__2); 559 | State = 108; expression(); 560 | State = 109; Match(T__3); 561 | State = 110; blockStatement(); 562 | } 563 | } 564 | catch (RecognitionException re) { 565 | _localctx.exception = re; 566 | ErrorHandler.ReportError(this, re); 567 | ErrorHandler.Recover(this, re); 568 | } 569 | finally { 570 | ExitRule(); 571 | } 572 | return _localctx; 573 | } 574 | 575 | public partial class DoWhileStatementContext : ParserRuleContext { 576 | public BlockStatementContext blockStatement() { 577 | return GetRuleContext(0); 578 | } 579 | public ExpressionContext expression() { 580 | return GetRuleContext(0); 581 | } 582 | public DoWhileStatementContext(ParserRuleContext parent, int invokingState) 583 | : base(parent, invokingState) 584 | { 585 | } 586 | public override int RuleIndex { get { return RULE_doWhileStatement; } } 587 | public override TResult Accept(IParseTreeVisitor visitor) { 588 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 589 | if (typedVisitor != null) return typedVisitor.VisitDoWhileStatement(this); 590 | else return visitor.VisitChildren(this); 591 | } 592 | } 593 | 594 | [RuleVersion(0)] 595 | public DoWhileStatementContext doWhileStatement() { 596 | DoWhileStatementContext _localctx = new DoWhileStatementContext(Context, State); 597 | EnterRule(_localctx, 18, RULE_doWhileStatement); 598 | try { 599 | EnterOuterAlt(_localctx, 1); 600 | { 601 | State = 112; Match(T__9); 602 | State = 113; blockStatement(); 603 | State = 114; Match(T__8); 604 | State = 115; Match(T__2); 605 | State = 116; expression(); 606 | State = 117; Match(T__3); 607 | State = 118; Match(T__6); 608 | } 609 | } 610 | catch (RecognitionException re) { 611 | _localctx.exception = re; 612 | ErrorHandler.ReportError(this, re); 613 | ErrorHandler.Recover(this, re); 614 | } 615 | finally { 616 | ExitRule(); 617 | } 618 | return _localctx; 619 | } 620 | 621 | public partial class ForStatementContext : ParserRuleContext { 622 | public CommonExpressionContext commonExpression() { 623 | return GetRuleContext(0); 624 | } 625 | public ExpressionContext expression() { 626 | return GetRuleContext(0); 627 | } 628 | public AssignAbleStatementContext assignAbleStatement() { 629 | return GetRuleContext(0); 630 | } 631 | public BlockStatementContext blockStatement() { 632 | return GetRuleContext(0); 633 | } 634 | public ForStatementContext(ParserRuleContext parent, int invokingState) 635 | : base(parent, invokingState) 636 | { 637 | } 638 | public override int RuleIndex { get { return RULE_forStatement; } } 639 | public override TResult Accept(IParseTreeVisitor visitor) { 640 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 641 | if (typedVisitor != null) return typedVisitor.VisitForStatement(this); 642 | else return visitor.VisitChildren(this); 643 | } 644 | } 645 | 646 | [RuleVersion(0)] 647 | public ForStatementContext forStatement() { 648 | ForStatementContext _localctx = new ForStatementContext(Context, State); 649 | EnterRule(_localctx, 20, RULE_forStatement); 650 | try { 651 | EnterOuterAlt(_localctx, 1); 652 | { 653 | State = 120; Match(T__10); 654 | State = 121; Match(T__2); 655 | State = 122; commonExpression(); 656 | State = 123; Match(T__6); 657 | State = 124; expression(); 658 | State = 125; Match(T__6); 659 | State = 126; assignAbleStatement(); 660 | State = 127; Match(T__3); 661 | State = 128; blockStatement(); 662 | } 663 | } 664 | catch (RecognitionException re) { 665 | _localctx.exception = re; 666 | ErrorHandler.ReportError(this, re); 667 | ErrorHandler.Recover(this, re); 668 | } 669 | finally { 670 | ExitRule(); 671 | } 672 | return _localctx; 673 | } 674 | 675 | public partial class CommonExpressionContext : ParserRuleContext { 676 | public DeclareExpressionContext declareExpression() { 677 | return GetRuleContext(0); 678 | } 679 | public AssignAbleStatementContext assignAbleStatement() { 680 | return GetRuleContext(0); 681 | } 682 | public CommonExpressionContext(ParserRuleContext parent, int invokingState) 683 | : base(parent, invokingState) 684 | { 685 | } 686 | public override int RuleIndex { get { return RULE_commonExpression; } } 687 | public override TResult Accept(IParseTreeVisitor visitor) { 688 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 689 | if (typedVisitor != null) return typedVisitor.VisitCommonExpression(this); 690 | else return visitor.VisitChildren(this); 691 | } 692 | } 693 | 694 | [RuleVersion(0)] 695 | public CommonExpressionContext commonExpression() { 696 | CommonExpressionContext _localctx = new CommonExpressionContext(Context, State); 697 | EnterRule(_localctx, 22, RULE_commonExpression); 698 | try { 699 | State = 132; 700 | switch (TokenStream.La(1)) { 701 | case T__26: 702 | case T__27: 703 | case T__28: 704 | case T__29: 705 | EnterOuterAlt(_localctx, 1); 706 | { 707 | State = 130; declareExpression(); 708 | } 709 | break; 710 | case T__2: 711 | case T__20: 712 | case T__23: 713 | case T__24: 714 | case T__25: 715 | case StringLiteral: 716 | case Decimal: 717 | case Identifier: 718 | EnterOuterAlt(_localctx, 2); 719 | { 720 | State = 131; assignAbleStatement(); 721 | } 722 | break; 723 | default: 724 | throw new NoViableAltException(this); 725 | } 726 | } 727 | catch (RecognitionException re) { 728 | _localctx.exception = re; 729 | ErrorHandler.ReportError(this, re); 730 | ErrorHandler.Recover(this, re); 731 | } 732 | finally { 733 | ExitRule(); 734 | } 735 | return _localctx; 736 | } 737 | 738 | public partial class AssignAbleStatementContext : ParserRuleContext { 739 | public AssignContext assign() { 740 | return GetRuleContext(0); 741 | } 742 | public ExpressionContext expression() { 743 | return GetRuleContext(0); 744 | } 745 | public AssignAbleStatementContext(ParserRuleContext parent, int invokingState) 746 | : base(parent, invokingState) 747 | { 748 | } 749 | public override int RuleIndex { get { return RULE_assignAbleStatement; } } 750 | public override TResult Accept(IParseTreeVisitor visitor) { 751 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 752 | if (typedVisitor != null) return typedVisitor.VisitAssignAbleStatement(this); 753 | else return visitor.VisitChildren(this); 754 | } 755 | } 756 | 757 | [RuleVersion(0)] 758 | public AssignAbleStatementContext assignAbleStatement() { 759 | AssignAbleStatementContext _localctx = new AssignAbleStatementContext(Context, State); 760 | EnterRule(_localctx, 24, RULE_assignAbleStatement); 761 | try { 762 | State = 136; 763 | ErrorHandler.Sync(this); 764 | switch ( Interpreter.AdaptivePredict(TokenStream,5,Context) ) { 765 | case 1: 766 | EnterOuterAlt(_localctx, 1); 767 | { 768 | State = 134; assign(); 769 | } 770 | break; 771 | case 2: 772 | EnterOuterAlt(_localctx, 2); 773 | { 774 | State = 135; expression(); 775 | } 776 | break; 777 | } 778 | } 779 | catch (RecognitionException re) { 780 | _localctx.exception = re; 781 | ErrorHandler.ReportError(this, re); 782 | ErrorHandler.Recover(this, re); 783 | } 784 | finally { 785 | ExitRule(); 786 | } 787 | return _localctx; 788 | } 789 | 790 | public partial class DeclareExpressionContext : ParserRuleContext { 791 | public BasicTypeContext basicType() { 792 | return GetRuleContext(0); 793 | } 794 | public DeclaratorsContext declarators() { 795 | return GetRuleContext(0); 796 | } 797 | public DeclareExpressionContext(ParserRuleContext parent, int invokingState) 798 | : base(parent, invokingState) 799 | { 800 | } 801 | public override int RuleIndex { get { return RULE_declareExpression; } } 802 | public override TResult Accept(IParseTreeVisitor visitor) { 803 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 804 | if (typedVisitor != null) return typedVisitor.VisitDeclareExpression(this); 805 | else return visitor.VisitChildren(this); 806 | } 807 | } 808 | 809 | [RuleVersion(0)] 810 | public DeclareExpressionContext declareExpression() { 811 | DeclareExpressionContext _localctx = new DeclareExpressionContext(Context, State); 812 | EnterRule(_localctx, 26, RULE_declareExpression); 813 | try { 814 | EnterOuterAlt(_localctx, 1); 815 | { 816 | State = 138; basicType(); 817 | State = 139; declarators(); 818 | } 819 | } 820 | catch (RecognitionException re) { 821 | _localctx.exception = re; 822 | ErrorHandler.ReportError(this, re); 823 | ErrorHandler.Recover(this, re); 824 | } 825 | finally { 826 | ExitRule(); 827 | } 828 | return _localctx; 829 | } 830 | 831 | public partial class ExpressionContext : ParserRuleContext { 832 | public AndAndExpressionContext[] andAndExpression() { 833 | return GetRuleContexts(); 834 | } 835 | public AndAndExpressionContext andAndExpression(int i) { 836 | return GetRuleContext(i); 837 | } 838 | public ExpressionContext(ParserRuleContext parent, int invokingState) 839 | : base(parent, invokingState) 840 | { 841 | } 842 | public override int RuleIndex { get { return RULE_expression; } } 843 | public override TResult Accept(IParseTreeVisitor visitor) { 844 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 845 | if (typedVisitor != null) return typedVisitor.VisitExpression(this); 846 | else return visitor.VisitChildren(this); 847 | } 848 | } 849 | 850 | [RuleVersion(0)] 851 | public ExpressionContext expression() { 852 | ExpressionContext _localctx = new ExpressionContext(Context, State); 853 | EnterRule(_localctx, 28, RULE_expression); 854 | int _la; 855 | try { 856 | EnterOuterAlt(_localctx, 1); 857 | { 858 | State = 141; andAndExpression(); 859 | State = 146; 860 | ErrorHandler.Sync(this); 861 | _la = TokenStream.La(1); 862 | while (_la==T__11) { 863 | { 864 | { 865 | State = 142; Match(T__11); 866 | State = 143; andAndExpression(); 867 | } 868 | } 869 | State = 148; 870 | ErrorHandler.Sync(this); 871 | _la = TokenStream.La(1); 872 | } 873 | } 874 | } 875 | catch (RecognitionException re) { 876 | _localctx.exception = re; 877 | ErrorHandler.ReportError(this, re); 878 | ErrorHandler.Recover(this, re); 879 | } 880 | finally { 881 | ExitRule(); 882 | } 883 | return _localctx; 884 | } 885 | 886 | public partial class AndAndExpressionContext : ParserRuleContext { 887 | public CmpExpressionContext[] cmpExpression() { 888 | return GetRuleContexts(); 889 | } 890 | public CmpExpressionContext cmpExpression(int i) { 891 | return GetRuleContext(i); 892 | } 893 | public AndAndExpressionContext(ParserRuleContext parent, int invokingState) 894 | : base(parent, invokingState) 895 | { 896 | } 897 | public override int RuleIndex { get { return RULE_andAndExpression; } } 898 | public override TResult Accept(IParseTreeVisitor visitor) { 899 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 900 | if (typedVisitor != null) return typedVisitor.VisitAndAndExpression(this); 901 | else return visitor.VisitChildren(this); 902 | } 903 | } 904 | 905 | [RuleVersion(0)] 906 | public AndAndExpressionContext andAndExpression() { 907 | AndAndExpressionContext _localctx = new AndAndExpressionContext(Context, State); 908 | EnterRule(_localctx, 30, RULE_andAndExpression); 909 | int _la; 910 | try { 911 | EnterOuterAlt(_localctx, 1); 912 | { 913 | State = 149; cmpExpression(); 914 | State = 154; 915 | ErrorHandler.Sync(this); 916 | _la = TokenStream.La(1); 917 | while (_la==T__12) { 918 | { 919 | { 920 | State = 150; Match(T__12); 921 | State = 151; cmpExpression(); 922 | } 923 | } 924 | State = 156; 925 | ErrorHandler.Sync(this); 926 | _la = TokenStream.La(1); 927 | } 928 | } 929 | } 930 | catch (RecognitionException re) { 931 | _localctx.exception = re; 932 | ErrorHandler.ReportError(this, re); 933 | ErrorHandler.Recover(this, re); 934 | } 935 | finally { 936 | ExitRule(); 937 | } 938 | return _localctx; 939 | } 940 | 941 | public partial class CmpExpressionContext : ParserRuleContext { 942 | public AddExpressionContext[] addExpression() { 943 | return GetRuleContexts(); 944 | } 945 | public AddExpressionContext addExpression(int i) { 946 | return GetRuleContext(i); 947 | } 948 | public CmpExpressionContext(ParserRuleContext parent, int invokingState) 949 | : base(parent, invokingState) 950 | { 951 | } 952 | public override int RuleIndex { get { return RULE_cmpExpression; } } 953 | public override TResult Accept(IParseTreeVisitor visitor) { 954 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 955 | if (typedVisitor != null) return typedVisitor.VisitCmpExpression(this); 956 | else return visitor.VisitChildren(this); 957 | } 958 | } 959 | 960 | [RuleVersion(0)] 961 | public CmpExpressionContext cmpExpression() { 962 | CmpExpressionContext _localctx = new CmpExpressionContext(Context, State); 963 | EnterRule(_localctx, 32, RULE_cmpExpression); 964 | int _la; 965 | try { 966 | EnterOuterAlt(_localctx, 1); 967 | { 968 | State = 157; addExpression(); 969 | State = 160; 970 | _la = TokenStream.La(1); 971 | if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__13) | (1L << T__14) | (1L << T__15) | (1L << T__16) | (1L << T__17) | (1L << T__18))) != 0)) { 972 | { 973 | State = 158; 974 | _la = TokenStream.La(1); 975 | if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__13) | (1L << T__14) | (1L << T__15) | (1L << T__16) | (1L << T__17) | (1L << T__18))) != 0)) ) { 976 | ErrorHandler.RecoverInline(this); 977 | } 978 | else { 979 | Consume(); 980 | } 981 | State = 159; addExpression(); 982 | } 983 | } 984 | 985 | } 986 | } 987 | catch (RecognitionException re) { 988 | _localctx.exception = re; 989 | ErrorHandler.ReportError(this, re); 990 | ErrorHandler.Recover(this, re); 991 | } 992 | finally { 993 | ExitRule(); 994 | } 995 | return _localctx; 996 | } 997 | 998 | public partial class AddExpressionContext : ParserRuleContext { 999 | public MulExpressionContext[] mulExpression() { 1000 | return GetRuleContexts(); 1001 | } 1002 | public MulExpressionContext mulExpression(int i) { 1003 | return GetRuleContext(i); 1004 | } 1005 | public AddExpressionContext(ParserRuleContext parent, int invokingState) 1006 | : base(parent, invokingState) 1007 | { 1008 | } 1009 | public override int RuleIndex { get { return RULE_addExpression; } } 1010 | public override TResult Accept(IParseTreeVisitor visitor) { 1011 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 1012 | if (typedVisitor != null) return typedVisitor.VisitAddExpression(this); 1013 | else return visitor.VisitChildren(this); 1014 | } 1015 | } 1016 | 1017 | [RuleVersion(0)] 1018 | public AddExpressionContext addExpression() { 1019 | AddExpressionContext _localctx = new AddExpressionContext(Context, State); 1020 | EnterRule(_localctx, 34, RULE_addExpression); 1021 | int _la; 1022 | try { 1023 | EnterOuterAlt(_localctx, 1); 1024 | { 1025 | State = 162; mulExpression(); 1026 | State = 167; 1027 | ErrorHandler.Sync(this); 1028 | _la = TokenStream.La(1); 1029 | while (_la==T__19 || _la==T__20) { 1030 | { 1031 | { 1032 | State = 163; 1033 | _la = TokenStream.La(1); 1034 | if ( !(_la==T__19 || _la==T__20) ) { 1035 | ErrorHandler.RecoverInline(this); 1036 | } 1037 | else { 1038 | Consume(); 1039 | } 1040 | State = 164; mulExpression(); 1041 | } 1042 | } 1043 | State = 169; 1044 | ErrorHandler.Sync(this); 1045 | _la = TokenStream.La(1); 1046 | } 1047 | } 1048 | } 1049 | catch (RecognitionException re) { 1050 | _localctx.exception = re; 1051 | ErrorHandler.ReportError(this, re); 1052 | ErrorHandler.Recover(this, re); 1053 | } 1054 | finally { 1055 | ExitRule(); 1056 | } 1057 | return _localctx; 1058 | } 1059 | 1060 | public partial class MulExpressionContext : ParserRuleContext { 1061 | public UnaryExpressionContext[] unaryExpression() { 1062 | return GetRuleContexts(); 1063 | } 1064 | public UnaryExpressionContext unaryExpression(int i) { 1065 | return GetRuleContext(i); 1066 | } 1067 | public MulExpressionContext(ParserRuleContext parent, int invokingState) 1068 | : base(parent, invokingState) 1069 | { 1070 | } 1071 | public override int RuleIndex { get { return RULE_mulExpression; } } 1072 | public override TResult Accept(IParseTreeVisitor visitor) { 1073 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 1074 | if (typedVisitor != null) return typedVisitor.VisitMulExpression(this); 1075 | else return visitor.VisitChildren(this); 1076 | } 1077 | } 1078 | 1079 | [RuleVersion(0)] 1080 | public MulExpressionContext mulExpression() { 1081 | MulExpressionContext _localctx = new MulExpressionContext(Context, State); 1082 | EnterRule(_localctx, 36, RULE_mulExpression); 1083 | int _la; 1084 | try { 1085 | EnterOuterAlt(_localctx, 1); 1086 | { 1087 | State = 170; unaryExpression(); 1088 | State = 175; 1089 | ErrorHandler.Sync(this); 1090 | _la = TokenStream.La(1); 1091 | while (_la==T__21 || _la==T__22) { 1092 | { 1093 | { 1094 | State = 171; 1095 | _la = TokenStream.La(1); 1096 | if ( !(_la==T__21 || _la==T__22) ) { 1097 | ErrorHandler.RecoverInline(this); 1098 | } 1099 | else { 1100 | Consume(); 1101 | } 1102 | State = 172; unaryExpression(); 1103 | } 1104 | } 1105 | State = 177; 1106 | ErrorHandler.Sync(this); 1107 | _la = TokenStream.La(1); 1108 | } 1109 | } 1110 | } 1111 | catch (RecognitionException re) { 1112 | _localctx.exception = re; 1113 | ErrorHandler.ReportError(this, re); 1114 | ErrorHandler.Recover(this, re); 1115 | } 1116 | finally { 1117 | ExitRule(); 1118 | } 1119 | return _localctx; 1120 | } 1121 | 1122 | public partial class UnaryExpressionContext : ParserRuleContext { 1123 | public PrimaryExpressionContext primaryExpression() { 1124 | return GetRuleContext(0); 1125 | } 1126 | public UnaryExpressionContext unaryExpression() { 1127 | return GetRuleContext(0); 1128 | } 1129 | public UnaryExpressionContext(ParserRuleContext parent, int invokingState) 1130 | : base(parent, invokingState) 1131 | { 1132 | } 1133 | public override int RuleIndex { get { return RULE_unaryExpression; } } 1134 | public override TResult Accept(IParseTreeVisitor visitor) { 1135 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 1136 | if (typedVisitor != null) return typedVisitor.VisitUnaryExpression(this); 1137 | else return visitor.VisitChildren(this); 1138 | } 1139 | } 1140 | 1141 | [RuleVersion(0)] 1142 | public UnaryExpressionContext unaryExpression() { 1143 | UnaryExpressionContext _localctx = new UnaryExpressionContext(Context, State); 1144 | EnterRule(_localctx, 38, RULE_unaryExpression); 1145 | int _la; 1146 | try { 1147 | State = 181; 1148 | switch (TokenStream.La(1)) { 1149 | case T__2: 1150 | case T__24: 1151 | case T__25: 1152 | case StringLiteral: 1153 | case Decimal: 1154 | case Identifier: 1155 | EnterOuterAlt(_localctx, 1); 1156 | { 1157 | State = 178; primaryExpression(); 1158 | } 1159 | break; 1160 | case T__20: 1161 | case T__23: 1162 | EnterOuterAlt(_localctx, 2); 1163 | { 1164 | State = 179; 1165 | _la = TokenStream.La(1); 1166 | if ( !(_la==T__20 || _la==T__23) ) { 1167 | ErrorHandler.RecoverInline(this); 1168 | } 1169 | else { 1170 | Consume(); 1171 | } 1172 | State = 180; unaryExpression(); 1173 | } 1174 | break; 1175 | default: 1176 | throw new NoViableAltException(this); 1177 | } 1178 | } 1179 | catch (RecognitionException re) { 1180 | _localctx.exception = re; 1181 | ErrorHandler.ReportError(this, re); 1182 | ErrorHandler.Recover(this, re); 1183 | } 1184 | finally { 1185 | ExitRule(); 1186 | } 1187 | return _localctx; 1188 | } 1189 | 1190 | public partial class PrimaryExpressionContext : ParserRuleContext { 1191 | public VariableExpressionContext variableExpression() { 1192 | return GetRuleContext(0); 1193 | } 1194 | public NumericLiteralContext numericLiteral() { 1195 | return GetRuleContext(0); 1196 | } 1197 | public ExpressionContext expression() { 1198 | return GetRuleContext(0); 1199 | } 1200 | public PrimaryExpressionContext(ParserRuleContext parent, int invokingState) 1201 | : base(parent, invokingState) 1202 | { 1203 | } 1204 | public override int RuleIndex { get { return RULE_primaryExpression; } } 1205 | public override TResult Accept(IParseTreeVisitor visitor) { 1206 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 1207 | if (typedVisitor != null) return typedVisitor.VisitPrimaryExpression(this); 1208 | else return visitor.VisitChildren(this); 1209 | } 1210 | } 1211 | 1212 | [RuleVersion(0)] 1213 | public PrimaryExpressionContext primaryExpression() { 1214 | PrimaryExpressionContext _localctx = new PrimaryExpressionContext(Context, State); 1215 | EnterRule(_localctx, 40, RULE_primaryExpression); 1216 | try { 1217 | State = 189; 1218 | switch (TokenStream.La(1)) { 1219 | case T__24: 1220 | case T__25: 1221 | case StringLiteral: 1222 | case Identifier: 1223 | EnterOuterAlt(_localctx, 1); 1224 | { 1225 | State = 183; variableExpression(); 1226 | } 1227 | break; 1228 | case Decimal: 1229 | EnterOuterAlt(_localctx, 2); 1230 | { 1231 | State = 184; numericLiteral(); 1232 | } 1233 | break; 1234 | case T__2: 1235 | EnterOuterAlt(_localctx, 3); 1236 | { 1237 | State = 185; Match(T__2); 1238 | State = 186; expression(); 1239 | State = 187; Match(T__3); 1240 | } 1241 | break; 1242 | default: 1243 | throw new NoViableAltException(this); 1244 | } 1245 | } 1246 | catch (RecognitionException re) { 1247 | _localctx.exception = re; 1248 | ErrorHandler.ReportError(this, re); 1249 | ErrorHandler.Recover(this, re); 1250 | } 1251 | finally { 1252 | ExitRule(); 1253 | } 1254 | return _localctx; 1255 | } 1256 | 1257 | public partial class VariableExpressionContext : ParserRuleContext { 1258 | public ITerminalNode Identifier() { return GetToken(TinyScriptParser.Identifier, 0); } 1259 | public ITerminalNode StringLiteral() { return GetToken(TinyScriptParser.StringLiteral, 0); } 1260 | public VariableExpressionContext(ParserRuleContext parent, int invokingState) 1261 | : base(parent, invokingState) 1262 | { 1263 | } 1264 | public override int RuleIndex { get { return RULE_variableExpression; } } 1265 | public override TResult Accept(IParseTreeVisitor visitor) { 1266 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 1267 | if (typedVisitor != null) return typedVisitor.VisitVariableExpression(this); 1268 | else return visitor.VisitChildren(this); 1269 | } 1270 | } 1271 | 1272 | [RuleVersion(0)] 1273 | public VariableExpressionContext variableExpression() { 1274 | VariableExpressionContext _localctx = new VariableExpressionContext(Context, State); 1275 | EnterRule(_localctx, 42, RULE_variableExpression); 1276 | int _la; 1277 | try { 1278 | EnterOuterAlt(_localctx, 1); 1279 | { 1280 | State = 191; 1281 | _la = TokenStream.La(1); 1282 | if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__24) | (1L << T__25) | (1L << StringLiteral) | (1L << Identifier))) != 0)) ) { 1283 | ErrorHandler.RecoverInline(this); 1284 | } 1285 | else { 1286 | Consume(); 1287 | } 1288 | } 1289 | } 1290 | catch (RecognitionException re) { 1291 | _localctx.exception = re; 1292 | ErrorHandler.ReportError(this, re); 1293 | ErrorHandler.Recover(this, re); 1294 | } 1295 | finally { 1296 | ExitRule(); 1297 | } 1298 | return _localctx; 1299 | } 1300 | 1301 | public partial class BasicTypeContext : ParserRuleContext { 1302 | public BasicTypeContext(ParserRuleContext parent, int invokingState) 1303 | : base(parent, invokingState) 1304 | { 1305 | } 1306 | public override int RuleIndex { get { return RULE_basicType; } } 1307 | public override TResult Accept(IParseTreeVisitor visitor) { 1308 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 1309 | if (typedVisitor != null) return typedVisitor.VisitBasicType(this); 1310 | else return visitor.VisitChildren(this); 1311 | } 1312 | } 1313 | 1314 | [RuleVersion(0)] 1315 | public BasicTypeContext basicType() { 1316 | BasicTypeContext _localctx = new BasicTypeContext(Context, State); 1317 | EnterRule(_localctx, 44, RULE_basicType); 1318 | int _la; 1319 | try { 1320 | EnterOuterAlt(_localctx, 1); 1321 | { 1322 | State = 193; 1323 | _la = TokenStream.La(1); 1324 | if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__26) | (1L << T__27) | (1L << T__28) | (1L << T__29))) != 0)) ) { 1325 | ErrorHandler.RecoverInline(this); 1326 | } 1327 | else { 1328 | Consume(); 1329 | } 1330 | } 1331 | } 1332 | catch (RecognitionException re) { 1333 | _localctx.exception = re; 1334 | ErrorHandler.ReportError(this, re); 1335 | ErrorHandler.Recover(this, re); 1336 | } 1337 | finally { 1338 | ExitRule(); 1339 | } 1340 | return _localctx; 1341 | } 1342 | 1343 | public partial class DeclaratorsContext : ParserRuleContext { 1344 | public AssignContext[] assign() { 1345 | return GetRuleContexts(); 1346 | } 1347 | public AssignContext assign(int i) { 1348 | return GetRuleContext(i); 1349 | } 1350 | public DeclaratorsContext(ParserRuleContext parent, int invokingState) 1351 | : base(parent, invokingState) 1352 | { 1353 | } 1354 | public override int RuleIndex { get { return RULE_declarators; } } 1355 | public override TResult Accept(IParseTreeVisitor visitor) { 1356 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 1357 | if (typedVisitor != null) return typedVisitor.VisitDeclarators(this); 1358 | else return visitor.VisitChildren(this); 1359 | } 1360 | } 1361 | 1362 | [RuleVersion(0)] 1363 | public DeclaratorsContext declarators() { 1364 | DeclaratorsContext _localctx = new DeclaratorsContext(Context, State); 1365 | EnterRule(_localctx, 46, RULE_declarators); 1366 | int _la; 1367 | try { 1368 | EnterOuterAlt(_localctx, 1); 1369 | { 1370 | State = 195; assign(); 1371 | State = 200; 1372 | ErrorHandler.Sync(this); 1373 | _la = TokenStream.La(1); 1374 | while (_la==T__30) { 1375 | { 1376 | { 1377 | State = 196; Match(T__30); 1378 | State = 197; assign(); 1379 | } 1380 | } 1381 | State = 202; 1382 | ErrorHandler.Sync(this); 1383 | _la = TokenStream.La(1); 1384 | } 1385 | } 1386 | } 1387 | catch (RecognitionException re) { 1388 | _localctx.exception = re; 1389 | ErrorHandler.ReportError(this, re); 1390 | ErrorHandler.Recover(this, re); 1391 | } 1392 | finally { 1393 | ExitRule(); 1394 | } 1395 | return _localctx; 1396 | } 1397 | 1398 | public partial class AssignContext : ParserRuleContext { 1399 | public ITerminalNode Identifier() { return GetToken(TinyScriptParser.Identifier, 0); } 1400 | public ExpressionContext expression() { 1401 | return GetRuleContext(0); 1402 | } 1403 | public AssignContext(ParserRuleContext parent, int invokingState) 1404 | : base(parent, invokingState) 1405 | { 1406 | } 1407 | public override int RuleIndex { get { return RULE_assign; } } 1408 | public override TResult Accept(IParseTreeVisitor visitor) { 1409 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 1410 | if (typedVisitor != null) return typedVisitor.VisitAssign(this); 1411 | else return visitor.VisitChildren(this); 1412 | } 1413 | } 1414 | 1415 | [RuleVersion(0)] 1416 | public AssignContext assign() { 1417 | AssignContext _localctx = new AssignContext(Context, State); 1418 | EnterRule(_localctx, 48, RULE_assign); 1419 | try { 1420 | EnterOuterAlt(_localctx, 1); 1421 | { 1422 | State = 203; Match(Identifier); 1423 | State = 204; Match(T__31); 1424 | State = 205; expression(); 1425 | } 1426 | } 1427 | catch (RecognitionException re) { 1428 | _localctx.exception = re; 1429 | ErrorHandler.ReportError(this, re); 1430 | ErrorHandler.Recover(this, re); 1431 | } 1432 | finally { 1433 | ExitRule(); 1434 | } 1435 | return _localctx; 1436 | } 1437 | 1438 | public partial class NumericLiteralContext : ParserRuleContext { 1439 | public ITerminalNode Decimal() { return GetToken(TinyScriptParser.Decimal, 0); } 1440 | public NumericLiteralContext(ParserRuleContext parent, int invokingState) 1441 | : base(parent, invokingState) 1442 | { 1443 | } 1444 | public override int RuleIndex { get { return RULE_numericLiteral; } } 1445 | public override TResult Accept(IParseTreeVisitor visitor) { 1446 | ITinyScriptVisitor typedVisitor = visitor as ITinyScriptVisitor; 1447 | if (typedVisitor != null) return typedVisitor.VisitNumericLiteral(this); 1448 | else return visitor.VisitChildren(this); 1449 | } 1450 | } 1451 | 1452 | [RuleVersion(0)] 1453 | public NumericLiteralContext numericLiteral() { 1454 | NumericLiteralContext _localctx = new NumericLiteralContext(Context, State); 1455 | EnterRule(_localctx, 50, RULE_numericLiteral); 1456 | try { 1457 | EnterOuterAlt(_localctx, 1); 1458 | { 1459 | State = 207; Match(Decimal); 1460 | } 1461 | } 1462 | catch (RecognitionException re) { 1463 | _localctx.exception = re; 1464 | ErrorHandler.ReportError(this, re); 1465 | ErrorHandler.Recover(this, re); 1466 | } 1467 | finally { 1468 | ExitRule(); 1469 | } 1470 | return _localctx; 1471 | } 1472 | 1473 | private static string _serializedATN = _serializeATN(); 1474 | private static string _serializeATN() 1475 | { 1476 | StringBuilder sb = new StringBuilder(); 1477 | sb.Append("\x3\x430\xD6D1\x8206\xAD2D\x4417\xAEF1\x8D80\xAADD\x3(\xD4"); 1478 | sb.Append("\x4\x2\t\x2\x4\x3\t\x3\x4\x4\t\x4\x4\x5\t\x5\x4\x6\t\x6\x4\a"); 1479 | sb.Append("\t\a\x4\b\t\b\x4\t\t\t\x4\n\t\n\x4\v\t\v\x4\f\t\f\x4\r\t\r\x4"); 1480 | sb.Append("\xE\t\xE\x4\xF\t\xF\x4\x10\t\x10\x4\x11\t\x11\x4\x12\t\x12\x4"); 1481 | sb.Append("\x13\t\x13\x4\x14\t\x14\x4\x15\t\x15\x4\x16\t\x16\x4\x17\t\x17"); 1482 | sb.Append("\x4\x18\t\x18\x4\x19\t\x19\x4\x1A\t\x1A\x4\x1B\t\x1B\x3\x2\x6"); 1483 | sb.Append("\x2\x38\n\x2\r\x2\xE\x2\x39\x3\x2\x3\x2\x3\x3\x3\x3\x3\x3\x3"); 1484 | sb.Append("\x3\x3\x3\x3\x3\x3\x3\x3\x3\x5\x3\x46\n\x3\x3\x4\x3\x4\x3\x4"); 1485 | sb.Append("\x3\x4\x3\x4\x3\x4\x3\x4\x3\x4\x3\x4\x3\x4\x5\x4R\n\x4\x3\x5"); 1486 | sb.Append("\x3\x5\x3\x5\x3\x5\x3\x6\x3\x6\a\x6Z\n\x6\f\x6\xE\x6]\v\x6\x3"); 1487 | sb.Append("\x6\x3\x6\x3\a\x3\a\x3\a\x3\b\x3\b\x3\b\x3\t\x3\t\x3\t\x3\t"); 1488 | sb.Append("\x3\t\x3\t\x3\n\x3\n\x3\n\x3\n\x3\n\x3\n\x3\v\x3\v\x3\v\x3\v"); 1489 | sb.Append("\x3\v\x3\v\x3\v\x3\v\x3\f\x3\f\x3\f\x3\f\x3\f\x3\f\x3\f\x3\f"); 1490 | sb.Append("\x3\f\x3\f\x3\r\x3\r\x5\r\x87\n\r\x3\xE\x3\xE\x5\xE\x8B\n\xE"); 1491 | sb.Append("\x3\xF\x3\xF\x3\xF\x3\x10\x3\x10\x3\x10\a\x10\x93\n\x10\f\x10"); 1492 | sb.Append("\xE\x10\x96\v\x10\x3\x11\x3\x11\x3\x11\a\x11\x9B\n\x11\f\x11"); 1493 | sb.Append("\xE\x11\x9E\v\x11\x3\x12\x3\x12\x3\x12\x5\x12\xA3\n\x12\x3\x13"); 1494 | sb.Append("\x3\x13\x3\x13\a\x13\xA8\n\x13\f\x13\xE\x13\xAB\v\x13\x3\x14"); 1495 | sb.Append("\x3\x14\x3\x14\a\x14\xB0\n\x14\f\x14\xE\x14\xB3\v\x14\x3\x15"); 1496 | sb.Append("\x3\x15\x3\x15\x5\x15\xB8\n\x15\x3\x16\x3\x16\x3\x16\x3\x16"); 1497 | sb.Append("\x3\x16\x3\x16\x5\x16\xC0\n\x16\x3\x17\x3\x17\x3\x18\x3\x18"); 1498 | sb.Append("\x3\x19\x3\x19\x3\x19\a\x19\xC9\n\x19\f\x19\xE\x19\xCC\v\x19"); 1499 | sb.Append("\x3\x1A\x3\x1A\x3\x1A\x3\x1A\x3\x1B\x3\x1B\x3\x1B\x2\x2\x1C"); 1500 | sb.Append("\x2\x4\x6\b\n\f\xE\x10\x12\x14\x16\x18\x1A\x1C\x1E \"$&(*,."); 1501 | sb.Append("\x30\x32\x34\x2\b\x3\x2\x10\x15\x3\x2\x16\x17\x3\x2\x18\x19"); 1502 | sb.Append("\x4\x2\x17\x17\x1A\x1A\x5\x2\x1B\x1C##%%\x3\x2\x1D \xCE\x2\x37"); 1503 | sb.Append("\x3\x2\x2\x2\x4\x45\x3\x2\x2\x2\x6Q\x3\x2\x2\x2\bS\x3\x2\x2"); 1504 | sb.Append("\x2\nW\x3\x2\x2\x2\f`\x3\x2\x2\x2\xE\x63\x3\x2\x2\x2\x10\x66"); 1505 | sb.Append("\x3\x2\x2\x2\x12l\x3\x2\x2\x2\x14r\x3\x2\x2\x2\x16z\x3\x2\x2"); 1506 | sb.Append("\x2\x18\x86\x3\x2\x2\x2\x1A\x8A\x3\x2\x2\x2\x1C\x8C\x3\x2\x2"); 1507 | sb.Append("\x2\x1E\x8F\x3\x2\x2\x2 \x97\x3\x2\x2\x2\"\x9F\x3\x2\x2\x2$"); 1508 | sb.Append("\xA4\x3\x2\x2\x2&\xAC\x3\x2\x2\x2(\xB7\x3\x2\x2\x2*\xBF\x3\x2"); 1509 | sb.Append("\x2\x2,\xC1\x3\x2\x2\x2.\xC3\x3\x2\x2\x2\x30\xC5\x3\x2\x2\x2"); 1510 | sb.Append("\x32\xCD\x3\x2\x2\x2\x34\xD1\x3\x2\x2\x2\x36\x38\x5\x4\x3\x2"); 1511 | sb.Append("\x37\x36\x3\x2\x2\x2\x38\x39\x3\x2\x2\x2\x39\x37\x3\x2\x2\x2"); 1512 | sb.Append("\x39:\x3\x2\x2\x2:;\x3\x2\x2\x2;<\a\x2\x2\x3<\x3\x3\x2\x2\x2"); 1513 | sb.Append("=\x46\x5\x6\x4\x2>\x46\x5\n\x6\x2?\x46\x5\f\a\x2@\x46\x5\xE"); 1514 | sb.Append("\b\x2\x41\x46\x5\x10\t\x2\x42\x46\x5\x12\n\x2\x43\x46\x5\x14"); 1515 | sb.Append("\v\x2\x44\x46\x5\x16\f\x2\x45=\x3\x2\x2\x2\x45>\x3\x2\x2\x2"); 1516 | sb.Append("\x45?\x3\x2\x2\x2\x45@\x3\x2\x2\x2\x45\x41\x3\x2\x2\x2\x45\x42"); 1517 | sb.Append("\x3\x2\x2\x2\x45\x43\x3\x2\x2\x2\x45\x44\x3\x2\x2\x2\x46\x5"); 1518 | sb.Append("\x3\x2\x2\x2GH\a\x3\x2\x2HI\x5\b\x5\x2IJ\x5\n\x6\x2JR\x3\x2"); 1519 | sb.Append("\x2\x2KL\a\x3\x2\x2LM\x5\b\x5\x2MN\x5\n\x6\x2NO\a\x4\x2\x2O"); 1520 | sb.Append("P\x5\n\x6\x2PR\x3\x2\x2\x2QG\x3\x2\x2\x2QK\x3\x2\x2\x2R\a\x3"); 1521 | sb.Append("\x2\x2\x2ST\a\x5\x2\x2TU\x5\x1E\x10\x2UV\a\x6\x2\x2V\t\x3\x2"); 1522 | sb.Append("\x2\x2W[\a\a\x2\x2XZ\x5\x4\x3\x2YX\x3\x2\x2\x2Z]\x3\x2\x2\x2"); 1523 | sb.Append("[Y\x3\x2\x2\x2[\\\x3\x2\x2\x2\\^\x3\x2\x2\x2][\x3\x2\x2\x2^"); 1524 | sb.Append("_\a\b\x2\x2_\v\x3\x2\x2\x2`\x61\x5\x32\x1A\x2\x61\x62\a\t\x2"); 1525 | sb.Append("\x2\x62\r\x3\x2\x2\x2\x63\x64\x5\x1C\xF\x2\x64\x65\a\t\x2\x2"); 1526 | sb.Append("\x65\xF\x3\x2\x2\x2\x66g\a\n\x2\x2gh\a\x5\x2\x2hi\x5\x1E\x10"); 1527 | sb.Append("\x2ij\a\x6\x2\x2jk\a\t\x2\x2k\x11\x3\x2\x2\x2lm\a\v\x2\x2mn"); 1528 | sb.Append("\a\x5\x2\x2no\x5\x1E\x10\x2op\a\x6\x2\x2pq\x5\n\x6\x2q\x13\x3"); 1529 | sb.Append("\x2\x2\x2rs\a\f\x2\x2st\x5\n\x6\x2tu\a\v\x2\x2uv\a\x5\x2\x2"); 1530 | sb.Append("vw\x5\x1E\x10\x2wx\a\x6\x2\x2xy\a\t\x2\x2y\x15\x3\x2\x2\x2z"); 1531 | sb.Append("{\a\r\x2\x2{|\a\x5\x2\x2|}\x5\x18\r\x2}~\a\t\x2\x2~\x7F\x5\x1E"); 1532 | sb.Append("\x10\x2\x7F\x80\a\t\x2\x2\x80\x81\x5\x1A\xE\x2\x81\x82\a\x6"); 1533 | sb.Append("\x2\x2\x82\x83\x5\n\x6\x2\x83\x17\x3\x2\x2\x2\x84\x87\x5\x1C"); 1534 | sb.Append("\xF\x2\x85\x87\x5\x1A\xE\x2\x86\x84\x3\x2\x2\x2\x86\x85\x3\x2"); 1535 | sb.Append("\x2\x2\x87\x19\x3\x2\x2\x2\x88\x8B\x5\x32\x1A\x2\x89\x8B\x5"); 1536 | sb.Append("\x1E\x10\x2\x8A\x88\x3\x2\x2\x2\x8A\x89\x3\x2\x2\x2\x8B\x1B"); 1537 | sb.Append("\x3\x2\x2\x2\x8C\x8D\x5.\x18\x2\x8D\x8E\x5\x30\x19\x2\x8E\x1D"); 1538 | sb.Append("\x3\x2\x2\x2\x8F\x94\x5 \x11\x2\x90\x91\a\xE\x2\x2\x91\x93\x5"); 1539 | sb.Append(" \x11\x2\x92\x90\x3\x2\x2\x2\x93\x96\x3\x2\x2\x2\x94\x92\x3"); 1540 | sb.Append("\x2\x2\x2\x94\x95\x3\x2\x2\x2\x95\x1F\x3\x2\x2\x2\x96\x94\x3"); 1541 | sb.Append("\x2\x2\x2\x97\x9C\x5\"\x12\x2\x98\x99\a\xF\x2\x2\x99\x9B\x5"); 1542 | sb.Append("\"\x12\x2\x9A\x98\x3\x2\x2\x2\x9B\x9E\x3\x2\x2\x2\x9C\x9A\x3"); 1543 | sb.Append("\x2\x2\x2\x9C\x9D\x3\x2\x2\x2\x9D!\x3\x2\x2\x2\x9E\x9C\x3\x2"); 1544 | sb.Append("\x2\x2\x9F\xA2\x5$\x13\x2\xA0\xA1\t\x2\x2\x2\xA1\xA3\x5$\x13"); 1545 | sb.Append("\x2\xA2\xA0\x3\x2\x2\x2\xA2\xA3\x3\x2\x2\x2\xA3#\x3\x2\x2\x2"); 1546 | sb.Append("\xA4\xA9\x5&\x14\x2\xA5\xA6\t\x3\x2\x2\xA6\xA8\x5&\x14\x2\xA7"); 1547 | sb.Append("\xA5\x3\x2\x2\x2\xA8\xAB\x3\x2\x2\x2\xA9\xA7\x3\x2\x2\x2\xA9"); 1548 | sb.Append("\xAA\x3\x2\x2\x2\xAA%\x3\x2\x2\x2\xAB\xA9\x3\x2\x2\x2\xAC\xB1"); 1549 | sb.Append("\x5(\x15\x2\xAD\xAE\t\x4\x2\x2\xAE\xB0\x5(\x15\x2\xAF\xAD\x3"); 1550 | sb.Append("\x2\x2\x2\xB0\xB3\x3\x2\x2\x2\xB1\xAF\x3\x2\x2\x2\xB1\xB2\x3"); 1551 | sb.Append("\x2\x2\x2\xB2\'\x3\x2\x2\x2\xB3\xB1\x3\x2\x2\x2\xB4\xB8\x5*"); 1552 | sb.Append("\x16\x2\xB5\xB6\t\x5\x2\x2\xB6\xB8\x5(\x15\x2\xB7\xB4\x3\x2"); 1553 | sb.Append("\x2\x2\xB7\xB5\x3\x2\x2\x2\xB8)\x3\x2\x2\x2\xB9\xC0\x5,\x17"); 1554 | sb.Append("\x2\xBA\xC0\x5\x34\x1B\x2\xBB\xBC\a\x5\x2\x2\xBC\xBD\x5\x1E"); 1555 | sb.Append("\x10\x2\xBD\xBE\a\x6\x2\x2\xBE\xC0\x3\x2\x2\x2\xBF\xB9\x3\x2"); 1556 | sb.Append("\x2\x2\xBF\xBA\x3\x2\x2\x2\xBF\xBB\x3\x2\x2\x2\xC0+\x3\x2\x2"); 1557 | sb.Append("\x2\xC1\xC2\t\x6\x2\x2\xC2-\x3\x2\x2\x2\xC3\xC4\t\a\x2\x2\xC4"); 1558 | sb.Append("/\x3\x2\x2\x2\xC5\xCA\x5\x32\x1A\x2\xC6\xC7\a!\x2\x2\xC7\xC9"); 1559 | sb.Append("\x5\x32\x1A\x2\xC8\xC6\x3\x2\x2\x2\xC9\xCC\x3\x2\x2\x2\xCA\xC8"); 1560 | sb.Append("\x3\x2\x2\x2\xCA\xCB\x3\x2\x2\x2\xCB\x31\x3\x2\x2\x2\xCC\xCA"); 1561 | sb.Append("\x3\x2\x2\x2\xCD\xCE\a%\x2\x2\xCE\xCF\a\"\x2\x2\xCF\xD0\x5\x1E"); 1562 | sb.Append("\x10\x2\xD0\x33\x3\x2\x2\x2\xD1\xD2\a$\x2\x2\xD2\x35\x3\x2\x2"); 1563 | sb.Append("\x2\x10\x39\x45Q[\x86\x8A\x94\x9C\xA2\xA9\xB1\xB7\xBF\xCA"); 1564 | return sb.ToString(); 1565 | } 1566 | 1567 | public static readonly ATN _ATN = 1568 | new ATNDeserializer().Deserialize(_serializedATN.ToCharArray()); 1569 | } 1570 | -------------------------------------------------------------------------------- /TinyScript/Parser/TinyScriptVisitor.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // ANTLR Version: 4.5.2 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | // Generated from TinyScript.g4 by ANTLR 4.5.2 12 | 13 | // Unreachable code detected 14 | #pragma warning disable 0162 15 | // The variable '...' is assigned but its value is never used 16 | #pragma warning disable 0219 17 | // Missing XML comment for publicly visible type or member '...' 18 | #pragma warning disable 1591 19 | // Ambiguous reference in cref attribute 20 | #pragma warning disable 419 21 | 22 | using Antlr4.Runtime.Misc; 23 | using Antlr4.Runtime.Tree; 24 | using IToken = Antlr4.Runtime.IToken; 25 | 26 | /// 27 | /// This interface defines a complete generic visitor for a parse tree produced 28 | /// by . 29 | /// 30 | /// The return type of the visit operation. 31 | [System.CodeDom.Compiler.GeneratedCode("ANTLR", "4.5.2")] 32 | [System.CLSCompliant(false)] 33 | public interface ITinyScriptVisitor : IParseTreeVisitor { 34 | /// 35 | /// Visit a parse tree produced by . 36 | /// 37 | /// The parse tree. 38 | /// The visitor result. 39 | Result VisitProgram([NotNull] TinyScriptParser.ProgramContext context); 40 | /// 41 | /// Visit a parse tree produced by . 42 | /// 43 | /// The parse tree. 44 | /// The visitor result. 45 | Result VisitStatement([NotNull] TinyScriptParser.StatementContext context); 46 | /// 47 | /// Visit a parse tree produced by . 48 | /// 49 | /// The parse tree. 50 | /// The visitor result. 51 | Result VisitIfStatement([NotNull] TinyScriptParser.IfStatementContext context); 52 | /// 53 | /// Visit a parse tree produced by . 54 | /// 55 | /// The parse tree. 56 | /// The visitor result. 57 | Result VisitQuoteExpr([NotNull] TinyScriptParser.QuoteExprContext context); 58 | /// 59 | /// Visit a parse tree produced by . 60 | /// 61 | /// The parse tree. 62 | /// The visitor result. 63 | Result VisitBlockStatement([NotNull] TinyScriptParser.BlockStatementContext context); 64 | /// 65 | /// Visit a parse tree produced by . 66 | /// 67 | /// The parse tree. 68 | /// The visitor result. 69 | Result VisitAssignStatement([NotNull] TinyScriptParser.AssignStatementContext context); 70 | /// 71 | /// Visit a parse tree produced by . 72 | /// 73 | /// The parse tree. 74 | /// The visitor result. 75 | Result VisitDeclareStatement([NotNull] TinyScriptParser.DeclareStatementContext context); 76 | /// 77 | /// Visit a parse tree produced by . 78 | /// 79 | /// The parse tree. 80 | /// The visitor result. 81 | Result VisitPrintStatement([NotNull] TinyScriptParser.PrintStatementContext context); 82 | /// 83 | /// Visit a parse tree produced by . 84 | /// 85 | /// The parse tree. 86 | /// The visitor result. 87 | Result VisitWhileStatement([NotNull] TinyScriptParser.WhileStatementContext context); 88 | /// 89 | /// Visit a parse tree produced by . 90 | /// 91 | /// The parse tree. 92 | /// The visitor result. 93 | Result VisitDoWhileStatement([NotNull] TinyScriptParser.DoWhileStatementContext context); 94 | /// 95 | /// Visit a parse tree produced by . 96 | /// 97 | /// The parse tree. 98 | /// The visitor result. 99 | Result VisitForStatement([NotNull] TinyScriptParser.ForStatementContext context); 100 | /// 101 | /// Visit a parse tree produced by . 102 | /// 103 | /// The parse tree. 104 | /// The visitor result. 105 | Result VisitCommonExpression([NotNull] TinyScriptParser.CommonExpressionContext context); 106 | /// 107 | /// Visit a parse tree produced by . 108 | /// 109 | /// The parse tree. 110 | /// The visitor result. 111 | Result VisitAssignAbleStatement([NotNull] TinyScriptParser.AssignAbleStatementContext context); 112 | /// 113 | /// Visit a parse tree produced by . 114 | /// 115 | /// The parse tree. 116 | /// The visitor result. 117 | Result VisitDeclareExpression([NotNull] TinyScriptParser.DeclareExpressionContext context); 118 | /// 119 | /// Visit a parse tree produced by . 120 | /// 121 | /// The parse tree. 122 | /// The visitor result. 123 | Result VisitExpression([NotNull] TinyScriptParser.ExpressionContext context); 124 | /// 125 | /// Visit a parse tree produced by . 126 | /// 127 | /// The parse tree. 128 | /// The visitor result. 129 | Result VisitAndAndExpression([NotNull] TinyScriptParser.AndAndExpressionContext context); 130 | /// 131 | /// Visit a parse tree produced by . 132 | /// 133 | /// The parse tree. 134 | /// The visitor result. 135 | Result VisitCmpExpression([NotNull] TinyScriptParser.CmpExpressionContext context); 136 | /// 137 | /// Visit a parse tree produced by . 138 | /// 139 | /// The parse tree. 140 | /// The visitor result. 141 | Result VisitAddExpression([NotNull] TinyScriptParser.AddExpressionContext context); 142 | /// 143 | /// Visit a parse tree produced by . 144 | /// 145 | /// The parse tree. 146 | /// The visitor result. 147 | Result VisitMulExpression([NotNull] TinyScriptParser.MulExpressionContext context); 148 | /// 149 | /// Visit a parse tree produced by . 150 | /// 151 | /// The parse tree. 152 | /// The visitor result. 153 | Result VisitUnaryExpression([NotNull] TinyScriptParser.UnaryExpressionContext context); 154 | /// 155 | /// Visit a parse tree produced by . 156 | /// 157 | /// The parse tree. 158 | /// The visitor result. 159 | Result VisitPrimaryExpression([NotNull] TinyScriptParser.PrimaryExpressionContext context); 160 | /// 161 | /// Visit a parse tree produced by . 162 | /// 163 | /// The parse tree. 164 | /// The visitor result. 165 | Result VisitVariableExpression([NotNull] TinyScriptParser.VariableExpressionContext context); 166 | /// 167 | /// Visit a parse tree produced by . 168 | /// 169 | /// The parse tree. 170 | /// The visitor result. 171 | Result VisitBasicType([NotNull] TinyScriptParser.BasicTypeContext context); 172 | /// 173 | /// Visit a parse tree produced by . 174 | /// 175 | /// The parse tree. 176 | /// The visitor result. 177 | Result VisitDeclarators([NotNull] TinyScriptParser.DeclaratorsContext context); 178 | /// 179 | /// Visit a parse tree produced by . 180 | /// 181 | /// The parse tree. 182 | /// The visitor result. 183 | Result VisitAssign([NotNull] TinyScriptParser.AssignContext context); 184 | /// 185 | /// Visit a parse tree produced by . 186 | /// 187 | /// The parse tree. 188 | /// The visitor result. 189 | Result VisitNumericLiteral([NotNull] TinyScriptParser.NumericLiteralContext context); 190 | } 191 | -------------------------------------------------------------------------------- /TinyScript/ParserRuleContextException.cs: -------------------------------------------------------------------------------- 1 | using Antlr4.Runtime; 2 | using System; 3 | using System.Text; 4 | 5 | namespace TinyScript 6 | { 7 | public class ParserRuleContextException : Exception 8 | { 9 | public ParserRuleContextException(ParserRuleContext ctx, string template, params object[] args) 10 | : base(Format(ctx, template, args)) 11 | { 12 | } 13 | 14 | private static string Format(ParserRuleContext ctx, string template, params object[] args) 15 | { 16 | var sb = new StringBuilder(); 17 | sb.AppendFormat("[{0},{1}] ", ctx.Start.Line, ctx.Start.Column); 18 | sb.AppendFormat(template, args); 19 | return sb.ToString(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /TinyScript/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using TinyScript.Builder; 4 | 5 | namespace TinyScript 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | if(args.Length == 0) 12 | { 13 | ShowHelp(); 14 | return; 15 | } 16 | try 17 | { 18 | using (var s = new FileStream(args[0], FileMode.Open)) 19 | { 20 | if(args.Length == 2) 21 | { 22 | if (args[1].StartsWith("-out:")) 23 | { 24 | var exeName = args[1].Substring(5); 25 | if (!string.IsNullOrEmpty(exeName)) 26 | { 27 | var builder = new IlBuilder(exeName); 28 | var v = new CompilerVisitor(builder); 29 | Run(v, s); 30 | return; 31 | } 32 | } 33 | else if(args[1] == "-jit") 34 | { 35 | var builder = new JitBuilder(); 36 | var v = new CompilerVisitor(builder); 37 | Run(v, s); 38 | builder.GenType.RunMain(); 39 | return; 40 | } 41 | ShowHelp(); 42 | } 43 | else 44 | { 45 | Run(new InterpreterVisitor(new Channel()), s); 46 | } 47 | } 48 | } 49 | catch (ParserRuleContextException ex) 50 | { 51 | Console.WriteLine(ex); 52 | } 53 | } 54 | 55 | private static void Run(TinyScriptBaseVisitor visitor, Stream s) 56 | { 57 | var r = new Runner(visitor); 58 | r.Run(s); 59 | } 60 | 61 | private static void ShowHelp() 62 | { 63 | Console.WriteLine(@"Usage : 64 | ts ScriptFileName [-jit | -out:output.exe] 65 | Example: 66 | ts test.ts 67 | ts test.ts -jit 68 | ts test.ts -out:test.exe"); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /TinyScript/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("ts")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ts")] 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("808ccfa6-f4a7-4295-b4ff-c364c6eaa8e1")] 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/Runner.cs: -------------------------------------------------------------------------------- 1 | using Antlr4.Runtime; 2 | using System.IO; 3 | using System.Text; 4 | 5 | namespace TinyScript 6 | { 7 | public class Runner 8 | { 9 | private readonly TinyScriptBaseVisitor _visitor; 10 | 11 | public Runner(TinyScriptBaseVisitor visitor) 12 | { 13 | _visitor = visitor; 14 | } 15 | 16 | public void Run(string script) 17 | { 18 | var bs = Encoding.UTF8.GetBytes(script); 19 | using (var s = new MemoryStream(bs)) 20 | { 21 | Run(s); 22 | } 23 | } 24 | 25 | public void Run(Stream stream) 26 | { 27 | var ais = new AntlrInputStream(stream); 28 | var lexer = new TinyScriptLexer(ais); 29 | var tokens = new CommonTokenStream(lexer); 30 | var parser = new TinyScriptParser(tokens) {BuildParseTree = true}; 31 | var tree = parser.program(); 32 | _visitor.Visit(tree); 33 | var v = _visitor as ISaveable; 34 | v?.Save(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /TinyScript/Saveable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace TinyScript 7 | { 8 | public interface ISaveable 9 | { 10 | void Save(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /TinyScript/TinyScript.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {808CCFA6-F4A7-4295-B4FF-C364C6EAA8E1} 8 | Exe 9 | Properties 10 | TinyScript 11 | ts 12 | 512 13 | true 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 | ..\Resource\Antlr4.Runtime.dll 44 | 45 | 46 | ..\Resource\nunit.framework.dll 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 82 | --------------------------------------------------------------------------------