├── .editorconfig ├── .gitignore ├── .idea └── .idea.SrslBytecodeVmAndCodeGenerator │ └── .idea │ ├── .gitignore │ ├── encodings.xml │ ├── indexLayout.xml │ ├── misc.xml │ └── vcs.xml ├── Benchmarks ├── Benchmarks.cs ├── Benchmarks.csproj ├── Benchmarks │ ├── Fibonacci.bite │ └── Prime.bite ├── MethodInvocation.cs ├── Program.cs ├── PropertyAccessBenchmarks.cs ├── README.md ├── TestClass.cs ├── TypeRegistryBenchmarks.cs └── run.cmd ├── Bite.Cli ├── Bite.Cli.csproj ├── Bite.Cli.net60.csproj ├── CommandLine │ ├── CommandLineArgs.cs │ └── OptionAttribute.cs ├── ConsoleEx.cs ├── ILMergeConfig.json ├── Options.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── REPL.cs ├── TestProgram │ ├── CSharpSystemModule.bite │ └── MainModule.bite └── build-all.cmd ├── Bite ├── AntlrGenerated │ ├── AntlrBiteLexer │ │ ├── BITELexer.cs │ │ ├── BITELexer.interp │ │ └── BITELexer.tokens │ └── AntlrBiteParser │ │ ├── BITELexer.cs │ │ ├── BITELexer.interp │ │ ├── BITELexer.tokens │ │ ├── BITEParser.cs │ │ ├── BITEParser.interp │ │ ├── BITEParser.tokens │ │ ├── BITEParserBaseVisitor.cs │ │ └── BITEParserVisitor.cs ├── Ast │ ├── ArgumentsBaseNode.cs │ ├── AssignmentBaseNode.cs │ ├── AssignmentOperatorTypes.cs │ ├── AssignmentTypes.cs │ ├── AstBaseNode.cs │ ├── AstGenerator │ │ └── BiteAstGenerator.cs │ ├── AstNodes.txt │ ├── AstVisitor.cs │ ├── BinaryOperationBaseNode.cs │ ├── BlockStatementBaseNode.cs │ ├── BreakStatementBaseNode.cs │ ├── CallBaseNode.cs │ ├── CallElementEntry.cs │ ├── CallElementTypes.cs │ ├── CallEntry.cs │ ├── CallTypes.cs │ ├── ClassDeclarationBaseNode.cs │ ├── ClassInstanceDeclarationBaseNode.cs │ ├── DebugInfo.cs │ ├── DeclarationBaseNode.cs │ ├── DeclarationsBaseNode.cs │ ├── ExpressionBaseNode.cs │ ├── ExpressionStatementBaseNode.cs │ ├── ForInitializerBaseNode.cs │ ├── ForStatementBaseNode.cs │ ├── FunctionDeclarationBaseNode.cs │ ├── IAstVisitor.cs │ ├── Identifier.cs │ ├── IfStatementBaseNode.cs │ ├── IfStatementEntry.cs │ ├── IfStatementEntryType.cs │ ├── InitializerBaseNode.cs │ ├── LocalVariableDeclarationBaseNode.cs │ ├── LocalVariableInitializerBaseNode.cs │ ├── MemberInitializationNode.cs │ ├── ModifiersBaseNode.cs │ ├── ModuleBaseNode.cs │ ├── ModuleIdentifier.cs │ ├── ParametersBaseNode.cs │ ├── PrimaryBaseNode.cs │ ├── ProgramBaseNode.cs │ ├── ReturnStatementBaseNode.cs │ ├── StatementBaseNode.cs │ ├── StructDeclarationBaseNode.cs │ ├── SyncBlockNode.cs │ ├── TernaryOperationBaseNode.cs │ ├── UnaryPostfixOperation.cs │ ├── UnaryPrefixOperation.cs │ ├── UsingStatementBaseNode.cs │ ├── VariableDeclarationBaseNode.cs │ └── WhileStatementBaseNode.cs ├── Bite.csproj ├── CodeGenerator │ ├── BiteCompilationContext.cs │ ├── BiteProgram.cs │ ├── BiteResult.cs │ ├── CodeGenerator.cs │ └── CompilerException.cs ├── Compiler │ ├── BiteCompiler.cs │ ├── BiteCompilerException.cs │ ├── BiteCompilerSyntaxError.cs │ └── BiteCompilerSyntaxErrorListener.cs ├── Grammar │ ├── BITELexer.g4 │ ├── BITEParser.g4 │ └── compile.bat ├── LICENSE ├── Modules │ ├── Callables │ │ └── System.cs │ ├── Interop.bite │ ├── ModuleLoader.cs │ └── System.bite ├── Runtime │ ├── BiteFunctionCall.cs │ ├── BiteVm.cs │ ├── BiteVmInterpretResult.cs │ ├── BiteVmOpCodes.cs │ ├── BiteVmRuntimeException.cs │ ├── Bytecode │ │ ├── BiteChunkWrapper.cs │ │ ├── Chunk.cs │ │ ├── ChunkDebugHelper.cs │ │ ├── ChunkHelper.cs │ │ └── ConstantValue.cs │ ├── BytecodeList.cs │ ├── BytecodeListStack.cs │ ├── DynamicBiteVariableStack.cs │ ├── FastMemoryStack.cs │ ├── FastPropertyCache.cs │ ├── FieldCache.cs │ ├── Functions │ │ ├── ForeignInterface │ │ │ ├── CSharpEvent.cs │ │ │ ├── CachedProperty.cs │ │ │ ├── DelegateUtility.cs │ │ │ ├── EventWrapper.cs │ │ │ ├── FastFieldInfo.cs │ │ │ ├── FastMethodInfo.cs │ │ │ ├── FastPropertyInfo.cs │ │ │ ├── ForeignLibraryInterfaceVm.cs │ │ │ ├── ICSharpEvent.cs │ │ │ ├── IFastPropertyInfo.cs │ │ │ ├── InteropBase.cs │ │ │ ├── StaticWrapper.cs │ │ │ └── TypeRegistry.cs │ │ ├── IBiteVmCallable.cs │ │ ├── Interop │ │ │ ├── ConstructorInvoker.cs │ │ │ ├── GenericMethodInvoker.cs │ │ │ ├── InteropGetConstructor.cs │ │ │ ├── InteropGetGenericMethod.cs │ │ │ ├── InteropGetMethod.cs │ │ │ ├── InteropGetStaticClass.cs │ │ │ ├── InteropGetStaticMember.cs │ │ │ ├── InteropGetStaticMethod.cs │ │ │ ├── MethodInvoker.cs │ │ │ ├── StaticGenericMethodInvoker.cs │ │ │ └── StaticMethodInvoker.cs │ │ ├── PrintFunctionVm.cs │ │ └── PrintLineFunctionVm.cs │ ├── IntByteStruct.cs │ ├── Memory │ │ ├── BiteMemorySpace.cs │ │ ├── ConstantValueType.cs │ │ ├── DynamicBiteVariable.cs │ │ ├── DynamicVariableExtension.cs │ │ ├── DynamicVariableType.cs │ │ ├── FastClassMemorySpace.cs │ │ ├── FastGlobalMemorySpace.cs │ │ ├── FastMemorySpace.cs │ │ ├── FastModuleMemorySpace.cs │ │ └── ObjectPoolFastMemory.cs │ ├── MethodCache.cs │ ├── Module.cs │ ├── PropertyCache.cs │ ├── StackExtensions.cs │ └── UsingStatementStack.cs └── Symbols │ ├── AccesModifierType.cs │ ├── BaseScope.cs │ ├── BaseSymbol.cs │ ├── BiteClassType.cs │ ├── ClassAndMemberModifiers.cs │ ├── ClassSymbol.cs │ ├── DataAggregateSymbol.cs │ ├── DynamicVariable.cs │ ├── FieldSymbol.cs │ ├── FunctionSymbol.cs │ ├── GlobalScope.cs │ ├── GlobalSymbolTable.cs │ ├── LocalScope.cs │ ├── MemberSymbol.cs │ ├── MethodSymbol.cs │ ├── ModuleBuilder.cs │ ├── ModuleSymbol.cs │ ├── ParameterSymbol.cs │ ├── ParametersSymbol.cs │ ├── Scope.cs │ ├── StringTable.cs │ ├── StructSymbol.cs │ ├── Symbol.cs │ ├── SymbolTable.cs │ ├── SymbolTableBuilder.cs │ ├── SymbolWithScope.cs │ ├── Type.cs │ ├── TypedSymbol.cs │ └── VariableSymbol.cs ├── BiteProgrammingLanguage.sln ├── FSharpTest ├── AssemblyInfo.fs ├── FSharpTest.fsproj ├── Library.fs ├── Script.fsx └── packages.config ├── LICENSE ├── README.md ├── README_DE.md ├── TestApp ├── App.config ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── TestApp.csproj ├── TestClassCSharp.cs ├── TestProgram │ ├── BinaryOpChaining.bite │ ├── BranchingTest.bite │ ├── CSharpEventInvokeExample.bite │ ├── CSharpEventReceiverExample.bite │ ├── ConstructorTest.bite │ ├── DynamicArray.bite │ ├── FibonacciExample.bite │ ├── InheritanceExample.bite │ ├── Interop.bite │ ├── Mandelbrot.bite │ ├── NetLanguageInterfaceExample.bite │ └── PrimeNumber.bite └── packages.config ├── TestAppNet6 ├── Program.cs ├── TestAppNet6.csproj └── TestProgram │ ├── CSharpSystemModule.bite │ └── MainModule.bite ├── TestMandelbrot ├── Mandelbrot.bite ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── TestMandelbrot.csproj ├── UnitTests ├── ExpressionUnitTests.cs ├── ModuleUnitTests.cs ├── StatementUnitTests.cs └── UnitTests.csproj └── WpfThreadTest ├── App.config ├── App.xaml ├── App.xaml.cs ├── GameObject.cs ├── MainWindow.xaml ├── MainWindow.xaml.cs ├── Properties ├── AssemblyInfo.cs ├── Resources.Designer.cs ├── Resources.resx ├── Settings.Designer.cs └── Settings.settings ├── README.md └── WpfThreadTest.csproj /.editorconfig: -------------------------------------------------------------------------------- 1 | [*] 2 | 3 | # Microsoft .NET properties 4 | csharp_space_after_cast = true 5 | csharp_space_between_method_call_parameter_list_parentheses = true 6 | csharp_space_between_method_declaration_parameter_list_parentheses = true 7 | 8 | # ReSharper properties 9 | resharper_align_multiline_calls_chain = true 10 | resharper_blank_lines_after_multiline_statements = 1 11 | resharper_blank_lines_around_single_line_auto_property = 1 12 | resharper_blank_lines_before_case = 1 13 | resharper_blank_lines_before_control_transfer_statements = 1 14 | resharper_blank_lines_before_multiline_statements = 1 15 | resharper_blank_lines_inside_namespace = 1 16 | resharper_csharp_indent_pars = outside 17 | resharper_csharp_space_within_parentheses = true 18 | resharper_csharp_wrap_arguments_style = chop_if_long 19 | resharper_indent_inside_namespace = false 20 | resharper_place_field_attribute_on_same_line = false 21 | resharper_space_before_type_argument_angle = true 22 | resharper_space_between_typecast_parentheses = true 23 | resharper_space_within_foreach_parentheses = true 24 | resharper_space_within_for_parentheses = true 25 | resharper_space_within_if_parentheses = true 26 | resharper_space_within_switch_parentheses = true 27 | resharper_space_within_typeof_parentheses = true 28 | resharper_space_within_type_argument_angles = true 29 | resharper_space_within_while_parentheses = true 30 | resharper_wrap_after_dot_in_method_calls = true 31 | resharper_wrap_chained_binary_expressions = chop_if_long 32 | -------------------------------------------------------------------------------- /.idea/.idea.SrslBytecodeVmAndCodeGenerator/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Rider ignored files 5 | /modules.xml 6 | /projectSettingsUpdater.xml 7 | /contentModel.xml 8 | /.idea.SrslBytecodeVmAndCodeGenerator.iml 9 | # Editor-based HTTP Client requests 10 | /httpRequests/ 11 | # Datasource local storage ignored files 12 | /dataSources/ 13 | /dataSources.local.xml 14 | -------------------------------------------------------------------------------- /.idea/.idea.SrslBytecodeVmAndCodeGenerator/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/.idea.SrslBytecodeVmAndCodeGenerator/.idea/indexLayout.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/.idea.SrslBytecodeVmAndCodeGenerator/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/.idea.SrslBytecodeVmAndCodeGenerator/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Benchmarks/Benchmarks.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.IO; 3 | using BenchmarkDotNet.Attributes; 4 | using Bite.Compiler; 5 | using Bite.Runtime.CodeGen; 6 | 7 | namespace Benchmarks 8 | { 9 | 10 | public class Benchmarks 11 | { 12 | private readonly Dictionary < string, BiteProgram > programs = new Dictionary < string, BiteProgram >(); 13 | 14 | #region Public 15 | 16 | public Benchmarks() 17 | { 18 | IEnumerable < string > files = Directory.EnumerateFiles( 19 | ".\\Benchmarks", 20 | "*.bite", 21 | SearchOption.AllDirectories ); 22 | 23 | foreach ( string file in files ) 24 | { 25 | string name = Path.GetFileNameWithoutExtension( file ); 26 | BiteCompiler compiler = new BiteCompiler(); 27 | programs.Add( name, compiler.Compile( new[] { File.ReadAllText( file ) } ) ); 28 | } 29 | } 30 | 31 | [Benchmark] 32 | public void RunFibonacci() 33 | { 34 | programs["Fibonacci"].Run(); 35 | } 36 | 37 | [Benchmark] 38 | public void RunPrime() 39 | { 40 | programs["Prime"].Run(); 41 | } 42 | 43 | #endregion 44 | } 45 | 46 | public class Bar 47 | { 48 | public int i; 49 | public float f; 50 | public double d; 51 | 52 | public int I { get; set; } 53 | 54 | public float F { get; set; } 55 | 56 | public double D { get; set; } 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Benchmarks/Benchmarks.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net462 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | PreserveNewest 19 | 20 | 21 | PreserveNewest 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Benchmarks/Benchmarks/Fibonacci.bite: -------------------------------------------------------------------------------- 1 | module MainModule; 2 | 3 | import System; 4 | using System; 5 | 6 | function FindFibonacciNumber(n) 7 | { 8 | var count= 2; 9 | var a = 1; 10 | var b = 1; 11 | var c = 1; 12 | if(n == 0) 13 | { 14 | return 0; 15 | } 16 | while(count 0) 24 | { 25 | count++; 26 | } 27 | a++; 28 | } 29 | return (--a); 30 | } 31 | 32 | FindPrimeNumber(64); -------------------------------------------------------------------------------- /Benchmarks/MethodInvocation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using BenchmarkDotNet.Attributes; 4 | using Bite.Runtime.Functions.ForeignInterface; 5 | using Bite.Runtime.Functions.Interop; 6 | using Bite.Runtime.Memory; 7 | 8 | namespace Benchmarks 9 | { 10 | 11 | public class MethodInvocationBenchmarks 12 | { 13 | private readonly TypeRegistry typeRegistry = new TypeRegistry(); 14 | private readonly ForeignLibraryInterfaceVm fli; 15 | private readonly InteropGetMethod interop; 16 | private readonly MethodInvoker m_MethodInvoker; 17 | private readonly TestClass instance; 18 | private readonly MethodInfo method; 19 | 20 | #region Public 21 | 22 | public MethodInvocationBenchmarks() 23 | { 24 | typeRegistry.RegisterType < TestClass >(); 25 | fli = new ForeignLibraryInterfaceVm( typeRegistry ); 26 | interop = new InteropGetMethod( typeRegistry ); 27 | instance = new TestClass(); 28 | 29 | Type type = Type.GetType( "Benchmarks.TestClass, Benchmarks" ); 30 | method = type.GetMethod( "TestMethod", new[] { typeof( string ), typeof( int ), typeof( float ) } ); 31 | 32 | m_MethodInvoker = ( MethodInvoker ) interop.Call( new DynamicBiteVariable[] 33 | { 34 | new DynamicBiteVariable( "TestClass" ), 35 | new DynamicBiteVariable( "TestMethod" ), 36 | new DynamicBiteVariable( "string" ), 37 | new DynamicBiteVariable( "int" ), 38 | new DynamicBiteVariable( "float" ), 39 | } ); 40 | } 41 | 42 | [Benchmark] 43 | public void RunForeignLibraryInterfaceVm() 44 | { 45 | fli.Call( new DynamicBiteVariable[] 46 | { 47 | new DynamicBiteVariable( instance ), 48 | new DynamicBiteVariable( "TestMethod" ), 49 | new DynamicBiteVariable( "Hello" ), 50 | new DynamicBiteVariable( "string" ), 51 | new DynamicBiteVariable( 1 ), 52 | new DynamicBiteVariable( "int" ), 53 | new DynamicBiteVariable( 2f ), 54 | new DynamicBiteVariable( "float" ), 55 | } ); 56 | } 57 | 58 | [Benchmark] 59 | public void RunReflection() 60 | { 61 | method.Invoke( instance, new object[] { "Hello", 1, 2f } ); 62 | } 63 | 64 | [Benchmark] 65 | public void RunInteropGetMethod() 66 | { 67 | m_MethodInvoker.Call( new DynamicBiteVariable[] 68 | { 69 | new DynamicBiteVariable( instance ), 70 | new DynamicBiteVariable( "Hello" ), 71 | new DynamicBiteVariable( 1 ), 72 | new DynamicBiteVariable( 2f ), 73 | } ); 74 | } 75 | 76 | 77 | [Benchmark] 78 | public void RunNative() 79 | { 80 | instance.TestMethod( "Hello", 1, 2f ); 81 | } 82 | 83 | #endregion 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /Benchmarks/Program.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Running; 2 | 3 | namespace Benchmarks 4 | { 5 | 6 | internal class Program 7 | { 8 | #region Private 9 | 10 | private static void Main( string[] args ) 11 | { 12 | //var b = new Benchmarks(); 13 | // BenchmarkRunner.Run < Benchmarks >(); 14 | //BenchmarkRunner.Run < TypeRegistryBenchmarks >(); 15 | //BenchmarkRunner.Run < PropertyAccessBenchmarks >(); 16 | BenchmarkRunner.Run < MethodInvocationBenchmarks >(); 17 | //var b = new MethodInvocationBenchmarks(); 18 | //b.RunForeignLibraryInterfaceVm(); 19 | } 20 | 21 | #endregion 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Benchmarks/PropertyAccessBenchmarks.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using BenchmarkDotNet.Attributes; 3 | using Bite.Compiler; 4 | using Bite.Runtime.CodeGen; 5 | 6 | namespace Benchmarks 7 | { 8 | 9 | public class PropertyAccessBenchmarks 10 | { 11 | private readonly string statements = @"module Main; 12 | 13 | class foo { 14 | var x = 5; 15 | var y = 2; 16 | } 17 | 18 | var a = 1; 19 | var b = new foo(); 20 | 21 | a += 2; 22 | b.x += 2; 23 | b[""y""] += 3; 24 | 25 | bar.i += 1; 26 | bar.f += 2; 27 | bar.d += 3; 28 | 29 | bar.I += 4; 30 | bar.F += 5; 31 | bar.D += 6; 32 | "; 33 | 34 | private readonly BiteProgram program; 35 | 36 | #region Public 37 | 38 | public PropertyAccessBenchmarks() 39 | { 40 | Bar bar = new Bar(); 41 | 42 | BiteCompiler compiler = new BiteCompiler(); 43 | program = compiler.Compile( new[] { statements } ); 44 | } 45 | 46 | [Benchmark] 47 | public void ArithmeticAssignment() 48 | { 49 | program.Run( new Dictionary < string, object > { { "bar", new Bar() } } ); 50 | } 51 | 52 | #endregion 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Benchmarks/README.md: -------------------------------------------------------------------------------- 1 | Call `run.cmd` from the command line console to execute the benchmarks, otherwise if you run it in VS, it will warn that 2 | you have the debugger attached. 3 | -------------------------------------------------------------------------------- /Benchmarks/TestClass.cs: -------------------------------------------------------------------------------- 1 | namespace Benchmarks 2 | { 3 | 4 | public class TestClass 5 | { 6 | #region Public 7 | 8 | public int TestMethod( string arg1, int arg2, float arg3 ) 9 | { 10 | return arg2; 11 | } 12 | 13 | #endregion 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Benchmarks/TypeRegistryBenchmarks.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using BenchmarkDotNet.Attributes; 4 | using Bite.Runtime.Functions.ForeignInterface; 5 | 6 | namespace Benchmarks 7 | { 8 | 9 | public class TypeRegistryBenchmarks 10 | { 11 | private readonly TypeRegistry typeRegistry = new TypeRegistry(); 12 | 13 | #region Public 14 | 15 | public TypeRegistryBenchmarks() 16 | { 17 | typeRegistry.RegisterType < TestClass >(); 18 | } 19 | 20 | [Benchmark] 21 | public void RunCachedGetmethod() 22 | { 23 | typeRegistry.TryResolveType( "TestClass", out Type type ); 24 | 25 | MethodInfo method = typeRegistry.GetMethod( 26 | type, 27 | "TestMethod", 28 | new[] { typeof( string ), typeof( int ), typeof( float ) } ); 29 | 30 | TestClass instance = new TestClass(); 31 | method.Invoke( instance, new object[] { "Hello", 1, 2f } ); 32 | } 33 | 34 | [Benchmark] 35 | public void RunGetMethod() 36 | { 37 | Type type = Type.GetType( "Benchmarks.TestClass, Benchmarks" ); 38 | MethodInfo method = type.GetMethod( "TestMethod", new[] { typeof( string ), typeof( int ), typeof( float ) } ); 39 | TestClass instance = new TestClass(); 40 | method.Invoke( instance, new object[] { "Hello", 1, 2f } ); 41 | } 42 | 43 | [Benchmark] 44 | public void RunNative() 45 | { 46 | TestClass instance = new TestClass(); 47 | instance.TestMethod( "Hello", 1, 2f ); 48 | } 49 | 50 | #endregion 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Benchmarks/run.cmd: -------------------------------------------------------------------------------- 1 | dotnet run -c Release -------------------------------------------------------------------------------- /Bite.Cli/Bite.Cli.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | bitevm 5 | Bite.Cli 6 | Exe 7 | net462 8 | Bite.Cli.Program 9 | AnyCPU 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | False 20 | False 21 | 22 | 23 | 24 | 25 | 26 | PreserveNewest 27 | 28 | 29 | PreserveNewest 30 | 31 | 32 | PreserveNewest 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Bite.Cli/Bite.Cli.net60.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | bitevm 5 | Exe 6 | net6.0 7 | true 8 | true 9 | true 10 | true 11 | true 12 | false 13 | false 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Bite.Cli/CommandLine/OptionAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bite.Cli.CommandLine 4 | { 5 | 6 | public class OptionAttribute : Attribute 7 | { 8 | public char ShortName { get; } 9 | 10 | public string LongName { get; } 11 | 12 | public bool Required { get; } 13 | 14 | public object Defaultvalue { get; } 15 | 16 | public string ShortDescription { get; } 17 | 18 | public string Description { get; } 19 | 20 | #region Public 21 | 22 | public OptionAttribute( 23 | char shortName, 24 | string longName, 25 | object defaultvalue, 26 | string shortDescription, 27 | string description ) 28 | { 29 | ShortName = shortName; 30 | LongName = longName; 31 | Defaultvalue = defaultvalue; 32 | ShortDescription = shortDescription; 33 | Description = description; 34 | } 35 | 36 | public OptionAttribute( 37 | char shortName, 38 | string longName, 39 | bool required, 40 | string shortDescription, 41 | string description ) 42 | { 43 | ShortName = shortName; 44 | LongName = longName; 45 | Required = required; 46 | ShortDescription = shortDescription; 47 | Description = description; 48 | } 49 | 50 | #endregion 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Bite.Cli/ConsoleEx.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | 4 | namespace Bite.Cli 5 | { 6 | 7 | public class ConsoleEx 8 | { 9 | #region Public 10 | 11 | public static string Buffer( bool exitOnEnter, out bool ctrlZPressed ) 12 | { 13 | bool buffering = true; 14 | StringBuilder buffer = new StringBuilder(); 15 | ctrlZPressed = false; 16 | 17 | while ( buffering ) 18 | { 19 | string input = Console.ReadLine(); 20 | 21 | if ( input == null ) 22 | { 23 | ctrlZPressed = true; 24 | 25 | return buffer.ToString(); 26 | } 27 | 28 | buffer.Append( input ); 29 | 30 | if ( exitOnEnter ) 31 | { 32 | buffering = false; 33 | } 34 | else 35 | { 36 | buffer.AppendLine(); 37 | } 38 | } 39 | 40 | return buffer.ToString(); 41 | } 42 | 43 | #endregion 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Bite.Cli/ILMergeConfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "General": { 3 | "InputAssemblies": [ 4 | "Antlr4.Runtime.Standard.dll", 5 | "Bite.dll" 6 | ] 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Bite.Cli/Options.cs: -------------------------------------------------------------------------------- 1 | using Bite.Cli.CommandLine; 2 | 3 | namespace Bite.Cli 4 | { 5 | 6 | public class Options 7 | { 8 | [Option( 'p', "path", ".", "", "The path containing the modules to be loaded" )] 9 | public string Path { get; set; } 10 | 11 | [Option( 'i', "input", false, " [module2.bite] ...", "A list of modules to be loaded" )] 12 | public string[] Modules { get; set; } 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Bite.Cli/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using Bite.Cli.CommandLine; 6 | using Bite.Compiler; 7 | using Bite.Runtime.CodeGen; 8 | 9 | namespace Bite.Cli 10 | { 11 | 12 | internal class Program 13 | { 14 | #region Private 15 | 16 | private static void Main( string[] args ) 17 | { 18 | Console.WriteLine( "Bite Programming Langauge v0.1 (c) 2022\r\n" ); 19 | CommandLineArgs commandLine = new CommandLineArgs( args ); 20 | 21 | commandLine.Parse < Options >( 22 | o => 23 | { 24 | if ( o.Modules != null ) 25 | { 26 | BiteCompiler compiler = new BiteCompiler(); 27 | 28 | BiteProgram program = compiler.Compile( o.Modules.Select( File.ReadAllText ) ); 29 | 30 | program.Run(); 31 | } 32 | else if ( o.Path != null ) 33 | { 34 | IEnumerable < string > files = Directory.EnumerateFiles( 35 | o.Path, 36 | "*.bite", 37 | SearchOption.AllDirectories ); 38 | 39 | BiteCompiler compiler = new BiteCompiler(); 40 | 41 | BiteProgram program = compiler.Compile( files.Select( File.ReadAllText ) ); 42 | 43 | program.Run(); 44 | } 45 | else 46 | { 47 | REPL.Start(); 48 | } 49 | } ); 50 | } 51 | 52 | #endregion 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Bite.Cli/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "Bite.Cli": { 4 | "commandName": "Project", 5 | "commandLineArgs": "", 6 | "1-commandLineArgs": "-i ILMerge\\hello.bite", 7 | "2-commandLineArgs": " -p .\\TestProgram" 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /Bite.Cli/TestProgram/CSharpSystemModule.bite: -------------------------------------------------------------------------------- 1 | module CSharpSystem; 2 | import System; 3 | using System; 4 | 5 | var CSharpInterfaceObject = new NetInterfaceObject(); 6 | 7 | CSharpInterfaceObject.Type = "System.Console"; 8 | 9 | var Console = NetLanguageInterface(CSharpInterfaceObject); 10 | 11 | CSharpInterfaceObject.Type = "System.IO.File"; 12 | 13 | var File = NetLanguageInterface(CSharpInterfaceObject); 14 | 15 | 16 | -------------------------------------------------------------------------------- /Bite.Cli/TestProgram/MainModule.bite: -------------------------------------------------------------------------------- 1 | module MainModule; 2 | 3 | import System; 4 | using System; 5 | 6 | import CSharpSystem; 7 | using CSharpSystem; 8 | 9 | function FindFibonacciNumber(n) 10 | { 11 | var count= 2; 12 | var a = 1; 13 | var b = 1; 14 | var c = 1; 15 | if(n == 0) 16 | { 17 | return 0; 18 | } 19 | while(count>='=36 122 | '||'=37 123 | '&&'=38 124 | '!='=39 125 | '=='=40 126 | '>'=41 127 | '>>'=42 128 | '>='=43 129 | '<'=44 130 | '<<'=45 131 | '<='=46 132 | '-'=47 133 | '--'=48 134 | '+'=49 135 | '++'=50 136 | '/'=51 137 | '*'=52 138 | '!'=53 139 | '.'=54 140 | '?'=55 141 | ':'=56 142 | '->'=57 143 | '%'=58 144 | '~'=59 145 | '&'=60 146 | '^'=61 147 | '|'=62 148 | '('=63 149 | ')'=64 150 | '['=65 151 | ']'=66 152 | ','=67 153 | ';'=68 154 | '$'=69 155 | 'false'=71 156 | 'true'=72 157 | '{'=81 158 | '}'=82 159 | '${'=84 160 | -------------------------------------------------------------------------------- /Bite/AntlrGenerated/AntlrBiteParser/BITELexer.tokens: -------------------------------------------------------------------------------- 1 | DeclareModule=1 2 | DeclareClass=2 3 | DeclareStruct=3 4 | DeclareClassInstance=4 5 | AsKeyword=5 6 | ExternModifier=6 7 | CallableModifier=7 8 | DeclareFunction=8 9 | DeclareVariable=9 10 | DeclareGetter=10 11 | DeclareSetter=11 12 | DeclareForLoop=12 13 | DeclareWhileLoop=13 14 | DeclareStatic=14 15 | DeclareAbstract=15 16 | DeclarePublic=16 17 | DeclarePrivate=17 18 | ControlFlowIf=18 19 | ControlFlowElse=19 20 | FunctionReturn=20 21 | Break=21 22 | NullReference=22 23 | ThisReference=23 24 | UsingDirective=24 25 | ImportDirective=25 26 | StartStatement=26 27 | UseStatement=27 28 | ThreadStatement=28 29 | SyncKeyword=29 30 | AssignOperator=30 31 | PlusAssignOperator=31 32 | MinusAssignOperator=32 33 | MultiplyAssignOperator=33 34 | DivideAssignOperator=34 35 | ModuloAssignOperator=35 36 | BitwiseAndAssignOperator=36 37 | BitwiseOrAssignOperator=37 38 | BitwiseXorAssignOperator=38 39 | BitwiseLeftShiftAssignOperator=39 40 | BitwiseRightShiftAssignOperator=40 41 | LogicalOrOperator=41 42 | LogicalAndOperator=42 43 | UnequalOperator=43 44 | EqualOperator=44 45 | GreaterOperator=45 46 | ShiftRightOperator=46 47 | GreaterEqualOperator=47 48 | SmallerOperator=48 49 | ShiftLeftOperator=49 50 | SmallerEqualOperator=50 51 | MinusOperator=51 52 | MinusMinusOperator=52 53 | PlusOperator=53 54 | PlusPlusOperator=54 55 | DivideOperator=55 56 | MultiplyOperator=56 57 | LogicalNegationOperator=57 58 | DotOperator=58 59 | QuestionMarkOperator=59 60 | ColonOperator=60 61 | ReferenceOperator=61 62 | ModuloOperator=62 63 | ComplimentOperator=63 64 | BitwiseAndOperator=64 65 | BitwiseXorOperator=65 66 | BitwiseOrOperator=66 67 | OpeningRoundBracket=67 68 | ClosingRoundBracket=68 69 | SquareBracketLeft=69 70 | SquareBracketRight=70 71 | CommaSeparator=71 72 | SemicolonSeparator=72 73 | DollarOperator=73 74 | BooleanLiteral=74 75 | False_=75 76 | True_=76 77 | IntegerLiteral=77 78 | FloatingLiteral=78 79 | DecimalLiteral=79 80 | Identifier=80 81 | COMMENT=81 82 | WS=82 83 | LINE_COMMENT=83 84 | DQUOTE=84 85 | CURLY_L=85 86 | CURLY_R=86 87 | TEXT=87 88 | BACKSLASH_PAREN=88 89 | ESCAPE_SEQUENCE=89 90 | 'module'=1 91 | 'class'=2 92 | 'struct'=3 93 | 'new'=4 94 | 'as'=5 95 | 'extern'=6 96 | 'callable'=7 97 | 'function'=8 98 | 'var'=9 99 | 'get'=10 100 | 'set'=11 101 | 'for'=12 102 | 'while'=13 103 | 'static'=14 104 | 'abstract'=15 105 | 'public'=16 106 | 'private'=17 107 | 'if'=18 108 | 'else'=19 109 | 'return'=20 110 | 'break'=21 111 | 'null'=22 112 | 'this'=23 113 | 'using'=24 114 | 'import'=25 115 | 'start'=26 116 | 'use'=27 117 | 'thread'=28 118 | 'sync'=29 119 | '='=30 120 | '+='=31 121 | '-='=32 122 | '*='=33 123 | '/='=34 124 | '%='=35 125 | '&='=36 126 | '|='=37 127 | '^='=38 128 | '<<='=39 129 | '>>='=40 130 | '||'=41 131 | '&&'=42 132 | '!='=43 133 | '=='=44 134 | '>'=45 135 | '>>'=46 136 | '>='=47 137 | '<'=48 138 | '<<'=49 139 | '<='=50 140 | '-'=51 141 | '--'=52 142 | '+'=53 143 | '++'=54 144 | '/'=55 145 | '*'=56 146 | '!'=57 147 | '.'=58 148 | '?'=59 149 | ':'=60 150 | '->'=61 151 | '%'=62 152 | '~'=63 153 | '&'=64 154 | '^'=65 155 | '|'=66 156 | '('=67 157 | ')'=68 158 | '['=69 159 | ']'=70 160 | ','=71 161 | ';'=72 162 | '$'=73 163 | 'false'=75 164 | 'true'=76 165 | '{'=85 166 | '}'=86 167 | '${'=88 168 | -------------------------------------------------------------------------------- /Bite/AntlrGenerated/AntlrBiteParser/BITEParser.tokens: -------------------------------------------------------------------------------- 1 | DeclareModule=1 2 | DeclareClass=2 3 | DeclareStruct=3 4 | DeclareClassInstance=4 5 | AsKeyword=5 6 | ExternModifier=6 7 | CallableModifier=7 8 | DeclareFunction=8 9 | DeclareVariable=9 10 | DeclareGetter=10 11 | DeclareSetter=11 12 | DeclareForLoop=12 13 | DeclareWhileLoop=13 14 | DeclareStatic=14 15 | DeclareAbstract=15 16 | DeclarePublic=16 17 | DeclarePrivate=17 18 | ControlFlowIf=18 19 | ControlFlowElse=19 20 | FunctionReturn=20 21 | Break=21 22 | NullReference=22 23 | ThisReference=23 24 | UsingDirective=24 25 | ImportDirective=25 26 | StartStatement=26 27 | UseStatement=27 28 | ThreadStatement=28 29 | SyncKeyword=29 30 | AssignOperator=30 31 | PlusAssignOperator=31 32 | MinusAssignOperator=32 33 | MultiplyAssignOperator=33 34 | DivideAssignOperator=34 35 | ModuloAssignOperator=35 36 | BitwiseAndAssignOperator=36 37 | BitwiseOrAssignOperator=37 38 | BitwiseXorAssignOperator=38 39 | BitwiseLeftShiftAssignOperator=39 40 | BitwiseRightShiftAssignOperator=40 41 | LogicalOrOperator=41 42 | LogicalAndOperator=42 43 | UnequalOperator=43 44 | EqualOperator=44 45 | GreaterOperator=45 46 | ShiftRightOperator=46 47 | GreaterEqualOperator=47 48 | SmallerOperator=48 49 | ShiftLeftOperator=49 50 | SmallerEqualOperator=50 51 | MinusOperator=51 52 | MinusMinusOperator=52 53 | PlusOperator=53 54 | PlusPlusOperator=54 55 | DivideOperator=55 56 | MultiplyOperator=56 57 | LogicalNegationOperator=57 58 | DotOperator=58 59 | QuestionMarkOperator=59 60 | ColonOperator=60 61 | ReferenceOperator=61 62 | ModuloOperator=62 63 | ComplimentOperator=63 64 | BitwiseAndOperator=64 65 | BitwiseXorOperator=65 66 | BitwiseOrOperator=66 67 | OpeningRoundBracket=67 68 | ClosingRoundBracket=68 69 | SquareBracketLeft=69 70 | SquareBracketRight=70 71 | CommaSeparator=71 72 | SemicolonSeparator=72 73 | DollarOperator=73 74 | BooleanLiteral=74 75 | False_=75 76 | True_=76 77 | IntegerLiteral=77 78 | FloatingLiteral=78 79 | DecimalLiteral=79 80 | Identifier=80 81 | COMMENT=81 82 | WS=82 83 | LINE_COMMENT=83 84 | DQUOTE=84 85 | CURLY_L=85 86 | CURLY_R=86 87 | TEXT=87 88 | BACKSLASH_PAREN=88 89 | ESCAPE_SEQUENCE=89 90 | 'module'=1 91 | 'class'=2 92 | 'struct'=3 93 | 'new'=4 94 | 'as'=5 95 | 'extern'=6 96 | 'callable'=7 97 | 'function'=8 98 | 'var'=9 99 | 'get'=10 100 | 'set'=11 101 | 'for'=12 102 | 'while'=13 103 | 'static'=14 104 | 'abstract'=15 105 | 'public'=16 106 | 'private'=17 107 | 'if'=18 108 | 'else'=19 109 | 'return'=20 110 | 'break'=21 111 | 'null'=22 112 | 'this'=23 113 | 'using'=24 114 | 'import'=25 115 | 'start'=26 116 | 'use'=27 117 | 'thread'=28 118 | 'sync'=29 119 | '='=30 120 | '+='=31 121 | '-='=32 122 | '*='=33 123 | '/='=34 124 | '%='=35 125 | '&='=36 126 | '|='=37 127 | '^='=38 128 | '<<='=39 129 | '>>='=40 130 | '||'=41 131 | '&&'=42 132 | '!='=43 133 | '=='=44 134 | '>'=45 135 | '>>'=46 136 | '>='=47 137 | '<'=48 138 | '<<'=49 139 | '<='=50 140 | '-'=51 141 | '--'=52 142 | '+'=53 143 | '++'=54 144 | '/'=55 145 | '*'=56 146 | '!'=57 147 | '.'=58 148 | '?'=59 149 | ':'=60 150 | '->'=61 151 | '%'=62 152 | '~'=63 153 | '&'=64 154 | '^'=65 155 | '|'=66 156 | '('=67 157 | ')'=68 158 | '['=69 159 | ']'=70 160 | ','=71 161 | ';'=72 162 | '$'=73 163 | 'false'=75 164 | 'true'=76 165 | '{'=85 166 | '}'=86 167 | '${'=88 168 | -------------------------------------------------------------------------------- /Bite/Ast/ArgumentsBaseNode.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public class ArgumentsBaseNode : AstBaseNode 7 | { 8 | public List < ExpressionBaseNode > Expressions; 9 | public List < bool > IsReference; 10 | 11 | #region Public 12 | 13 | public override object Accept( IAstVisitor visitor ) 14 | { 15 | return visitor.Visit( this ); 16 | } 17 | 18 | #endregion 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Bite/Ast/AssignmentBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class AssignmentBaseNode : ExpressionBaseNode 5 | { 6 | public CallBaseNode CallBase; 7 | public AssignmentOperatorTypes OperatorType; 8 | public AssignmentTypes Type; 9 | public BinaryOperationBaseNode Binary; 10 | public TernaryOperationBaseNode Ternary; 11 | public UnaryPostfixOperation UnaryPostfix; 12 | public UnaryPrefixOperation UnaryPrefix; 13 | public PrimaryBaseNode PrimaryBaseNode; 14 | 15 | #region Public 16 | 17 | public override object Accept( IAstVisitor visitor ) 18 | { 19 | return visitor.Visit( this ); 20 | } 21 | 22 | #endregion 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Bite/Ast/AssignmentOperatorTypes.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public enum AssignmentOperatorTypes 5 | { 6 | Default, 7 | Assign, 8 | DivAssign, 9 | MultAssign, 10 | PlusAssign, 11 | MinusAssign, 12 | ModuloAssignOperator, 13 | BitwiseAndAssignOperator, 14 | BitwiseOrAssignOperator, 15 | BitwiseXorAssignOperator, 16 | BitwiseLeftShiftAssignOperator, 17 | BitwiseRightShiftAssignOperator 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Bite/Ast/AssignmentTypes.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public enum AssignmentTypes 5 | { 6 | Default, 7 | Ternary, 8 | Binary, 9 | UnaryPostfix, 10 | UnaryPrefix, 11 | Primary, 12 | Assignment, 13 | Call 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Bite/Ast/AstBaseNode.cs: -------------------------------------------------------------------------------- 1 | using Bite.Symbols; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public abstract class AstBaseNode 7 | { 8 | public Scope AstScopeNode; 9 | public DebugInfo DebugInfoAstNode = new DebugInfo(); 10 | 11 | #region Public 12 | 13 | public abstract object Accept( IAstVisitor visitor ); 14 | 15 | #endregion 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Bite/Ast/AstVisitor.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public abstract class AstVisitor < T > 5 | { 6 | #region Public 7 | 8 | public abstract T Visit( ProgramBaseNode node ); 9 | 10 | public abstract T Visit( ModuleBaseNode node ); 11 | 12 | public abstract T Visit( ModifiersBaseNode node ); 13 | 14 | public abstract T Visit( DeclarationBaseNode node ); 15 | 16 | public abstract T Visit( UsingStatementBaseNode node ); 17 | 18 | public abstract T Visit( DeclarationsBaseNode node ); 19 | 20 | public abstract T Visit( ClassDeclarationBaseNode node ); 21 | 22 | public abstract T Visit( FunctionDeclarationBaseNode node ); 23 | 24 | public abstract T Visit( LocalVariableInitializerBaseNode node ); 25 | 26 | public abstract T Visit( LocalVariableDeclarationBaseNode node ); 27 | 28 | public abstract T Visit( VariableDeclarationBaseNode node ); 29 | 30 | public abstract T Visit( ClassInstanceDeclarationBaseNode node ); 31 | 32 | public abstract T Visit( CallBaseNode node ); 33 | 34 | public abstract T Visit( ArgumentsBaseNode node ); 35 | 36 | public abstract T Visit( ParametersBaseNode node ); 37 | 38 | public abstract T Visit( AssignmentBaseNode node ); 39 | 40 | public abstract T Visit( ExpressionBaseNode node ); 41 | 42 | public abstract T Visit( BlockStatementBaseNode node ); 43 | 44 | public abstract T Visit( SyncBlockNode node ); 45 | 46 | public abstract T Visit( StatementBaseNode node ); 47 | 48 | public abstract T Visit( ExpressionStatementBaseNode node ); 49 | 50 | public abstract T Visit( IfStatementBaseNode node ); 51 | 52 | public abstract T Visit( ForStatementBaseNode node ); 53 | 54 | public abstract T Visit( WhileStatementBaseNode node ); 55 | 56 | public abstract T Visit( ReturnStatementBaseNode node ); 57 | 58 | public abstract T Visit( BreakStatementBaseNode node ); 59 | 60 | public abstract T Visit( InitializerBaseNode node ); 61 | 62 | public abstract T Visit( BinaryOperationBaseNode node ); 63 | 64 | public abstract T Visit( TernaryOperationBaseNode node ); 65 | 66 | public abstract T Visit( PrimaryBaseNode node ); 67 | 68 | public abstract T Visit( StructDeclarationBaseNode node ); 69 | 70 | public abstract T Visit( UnaryPostfixOperation node ); 71 | 72 | public abstract T Visit( UnaryPrefixOperation node ); 73 | 74 | public abstract T Visit( AstBaseNode node ); 75 | 76 | #endregion 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /Bite/Ast/BinaryOperationBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class BinaryOperationBaseNode : ExpressionBaseNode 5 | { 6 | public enum BinaryOperatorType 7 | { 8 | Plus, 9 | Minus, 10 | Modulo, 11 | Mult, 12 | Div, 13 | Equal, 14 | NotEqual, 15 | Less, 16 | LessOrEqual, 17 | Greater, 18 | GreaterOrEqual, 19 | And, 20 | Or, 21 | BitwiseAnd, 22 | BitwiseOr, 23 | BitwiseXor, 24 | ShiftLeft, 25 | ShiftRight 26 | } 27 | 28 | public ExpressionBaseNode LeftOperand; 29 | public BinaryOperatorType? Operator; 30 | public ExpressionBaseNode RightOperand; 31 | 32 | #region Public 33 | 34 | public override object Accept( IAstVisitor visitor ) 35 | { 36 | return visitor.Visit( this ); 37 | } 38 | 39 | #endregion 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Bite/Ast/BlockStatementBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class BlockStatementBaseNode : StatementBaseNode 5 | { 6 | public DeclarationsBaseNode DeclarationsBase; 7 | 8 | #region Public 9 | 10 | public override object Accept( IAstVisitor visitor ) 11 | { 12 | return visitor.Visit( this ); 13 | } 14 | 15 | #endregion 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Bite/Ast/BreakStatementBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class BreakStatementBaseNode : StatementBaseNode 5 | { 6 | #region Public 7 | 8 | public override object Accept( IAstVisitor visitor ) 9 | { 10 | return visitor.Visit( this ); 11 | } 12 | 13 | #endregion 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Bite/Ast/CallBaseNode.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public class CallBaseNode : ExpressionBaseNode 7 | { 8 | public CallTypes CallType; 9 | public PrimaryBaseNode PrimaryBase; 10 | public ArgumentsBaseNode ArgumentsBase; 11 | public List < CallElementEntry > ElementAccess; 12 | public List < CallEntry > CallEntries; 13 | public bool IsFunctionCall; 14 | 15 | #region Public 16 | 17 | public override object Accept( IAstVisitor visitor ) 18 | { 19 | return visitor.Visit( this ); 20 | } 21 | 22 | #endregion 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Bite/Ast/CallElementEntry.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class CallElementEntry 5 | { 6 | public CallElementTypes CallElementType; 7 | public string Identifier = ""; 8 | public CallBaseNode CallBase = null; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Bite/Ast/CallElementTypes.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public enum CallElementTypes 5 | { 6 | StringLiteral, 7 | IntegerLiteral, 8 | Call 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Bite/Ast/CallEntry.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public class CallEntry 7 | { 8 | public PrimaryBaseNode PrimaryBase; 9 | public ArgumentsBaseNode ArgumentsBase; 10 | public List < CallElementEntry > ElementAccess; 11 | public bool IsFunctionCall; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Bite/Ast/CallTypes.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public enum CallTypes 5 | { 6 | Primary, 7 | PrimaryCall 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Bite/Ast/ClassDeclarationBaseNode.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public class ClassDeclarationBaseNode : DeclarationBaseNode 7 | { 8 | public Identifier ClassId; 9 | public List < Identifier > Inheritance; 10 | public ModifiersBaseNode ModifiersBase; 11 | public BlockStatementBaseNode BlockStatementBase; 12 | 13 | #region Public 14 | 15 | public override object Accept( IAstVisitor visitor ) 16 | { 17 | return visitor.Visit( this ); 18 | } 19 | 20 | #endregion 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Bite/Ast/ClassInstanceDeclarationBaseNode.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public class ClassInstanceDeclarationBaseNode : DeclarationBaseNode 7 | { 8 | public ModifiersBaseNode ModifiersBase; 9 | public Identifier InstanceId; 10 | public ArgumentsBaseNode ArgumentsBase; 11 | public Identifier ClassName; 12 | public List < Identifier > ClassPath; 13 | public bool IsVariableRedeclaration; 14 | public List < MemberInitializationNode > Initializers; 15 | 16 | #region Public 17 | 18 | public override object Accept( IAstVisitor visitor ) 19 | { 20 | return visitor.Visit( this ); 21 | } 22 | 23 | #endregion 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Bite/Ast/DebugInfo.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class DebugInfo 5 | { 6 | public int LineNumber; 7 | public int ColumnNumber; 8 | public int LineNumberStart; 9 | public int LineNumberEnd; 10 | public int ColumnNumberStart; 11 | public int ColumnNumberEnd; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Bite/Ast/DeclarationBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public abstract class DeclarationBaseNode : StatementBaseNode 5 | { 6 | #region Public 7 | 8 | public override object Accept( IAstVisitor visitor ) 9 | { 10 | return visitor.Visit( this ); 11 | } 12 | 13 | #endregion 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Bite/Ast/DeclarationsBaseNode.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public class DeclarationsBaseNode : DeclarationBaseNode 7 | { 8 | public List < ClassDeclarationBaseNode > Classes; 9 | public List < StructDeclarationBaseNode > Structs; 10 | public List < ClassInstanceDeclarationBaseNode > ClassInstances; 11 | public List < VariableDeclarationBaseNode > Variables; 12 | public List < FunctionDeclarationBaseNode > Functions; 13 | public List < StatementBaseNode > Statements; 14 | 15 | #region Public 16 | 17 | public DeclarationsBaseNode() 18 | { 19 | Classes = new List < ClassDeclarationBaseNode >(); 20 | Structs = new List < StructDeclarationBaseNode >(); 21 | ClassInstances = new List < ClassInstanceDeclarationBaseNode >(); 22 | Variables = new List < VariableDeclarationBaseNode >(); 23 | Functions = new List < FunctionDeclarationBaseNode >(); 24 | Statements = new List < StatementBaseNode >(); 25 | } 26 | 27 | public override object Accept( IAstVisitor visitor ) 28 | { 29 | return visitor.Visit( this ); 30 | } 31 | 32 | #endregion 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Bite/Ast/ExpressionBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class ExpressionBaseNode : AstBaseNode 5 | { 6 | public AssignmentBaseNode AssignmentBase; 7 | 8 | #region Public 9 | 10 | public override object Accept( IAstVisitor visitor ) 11 | { 12 | return visitor.Visit( this ); 13 | } 14 | 15 | #endregion 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Bite/Ast/ExpressionStatementBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class ExpressionStatementBaseNode : StatementBaseNode 5 | { 6 | public ExpressionBaseNode ExpressionBase; 7 | 8 | #region Public 9 | 10 | public override object Accept( IAstVisitor visitor ) 11 | { 12 | return visitor.Visit( this ); 13 | } 14 | 15 | #endregion 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Bite/Ast/ForInitializerBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class ForInitializerBaseNode : AstBaseNode 5 | { 6 | public LocalVariableInitializerBaseNode LocalVariableInitializerBase { get; set; } 7 | 8 | public ExpressionBaseNode[] Expressions { get; set; } 9 | 10 | #region Public 11 | 12 | public override object Accept( IAstVisitor visitor ) 13 | { 14 | return visitor.Visit( this ); 15 | } 16 | 17 | #endregion 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Bite/Ast/ForStatementBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class ForStatementBaseNode : StatementBaseNode 5 | { 6 | public ForInitializerBaseNode InitializerBase { get; set; } 7 | 8 | public ExpressionBaseNode Condition { get; set; } 9 | 10 | public ExpressionBaseNode[] Iterators { get; set; } 11 | 12 | public StatementBaseNode StatementBase { get; set; } 13 | 14 | #region Public 15 | 16 | public override object Accept( IAstVisitor visitor ) 17 | { 18 | return visitor.Visit( this ); 19 | } 20 | 21 | #endregion 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Bite/Ast/FunctionDeclarationBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class FunctionDeclarationBaseNode : DeclarationBaseNode 5 | { 6 | public Identifier FunctionId; 7 | public Identifier LinkFunctionId; 8 | public ModifiersBaseNode ModifiersBase; 9 | public ParametersBaseNode ParametersBase; 10 | public BlockStatementBaseNode FunctionBlock; 11 | 12 | #region Public 13 | 14 | public override object Accept( IAstVisitor visitor ) 15 | { 16 | return visitor.Visit( this ); 17 | } 18 | 19 | #endregion 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Bite/Ast/IAstVisitor.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public interface IAstVisitor 5 | { 6 | object Visit( AstBaseNode astBaseNode ); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /Bite/Ast/Identifier.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class Identifier : AstBaseNode 5 | { 6 | public string Id; 7 | 8 | #region Public 9 | 10 | public Identifier() 11 | { 12 | } 13 | 14 | public Identifier( string id ) 15 | { 16 | Id = id; 17 | } 18 | 19 | public override object Accept( IAstVisitor visitor ) 20 | { 21 | return visitor.Visit( this ); 22 | } 23 | 24 | #endregion 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Bite/Ast/IfStatementBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class IfStatementBaseNode : StatementBaseNode 5 | { 6 | public ExpressionBaseNode ExpressionBase; 7 | 8 | public StatementBaseNode ThenStatementBase; 9 | 10 | public StatementBaseNode ElseStatementBase; 11 | 12 | #region Public 13 | 14 | public override object Accept( IAstVisitor visitor ) 15 | { 16 | return visitor.Visit( this ); 17 | } 18 | 19 | #endregion 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Bite/Ast/IfStatementEntry.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class IfStatementEntry 5 | { 6 | public IfStatementEntryType IfStatementType; 7 | public ExpressionBaseNode ExpressionBaseElseIf; 8 | public BlockStatementBaseNode ElseBlock; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Bite/Ast/IfStatementEntryType.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public enum IfStatementEntryType 5 | { 6 | Else, 7 | ElseIf 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Bite/Ast/InitializerBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class InitializerBaseNode : AstBaseNode 5 | { 6 | public ExpressionStatementBaseNode Expression; 7 | 8 | #region Public 9 | 10 | public override object Accept( IAstVisitor visitor ) 11 | { 12 | return visitor.Visit( this ); 13 | } 14 | 15 | #endregion 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Bite/Ast/LocalVariableDeclarationBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class LocalVariableDeclarationBaseNode : StatementBaseNode 5 | { 6 | public ModifiersBaseNode ModifiersBase { get; set; } 7 | 8 | public Identifier VarId { get; set; } 9 | 10 | public ExpressionBaseNode ExpressionBase { get; set; } 11 | 12 | #region Public 13 | 14 | public override object Accept( IAstVisitor visitor ) 15 | { 16 | return visitor.Visit( this ); 17 | } 18 | 19 | #endregion 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Bite/Ast/LocalVariableInitializerBaseNode.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public class LocalVariableInitializerBaseNode : StatementBaseNode 7 | { 8 | public IEnumerable < LocalVariableDeclarationBaseNode > VariableDeclarations { get; set; } 9 | 10 | #region Public 11 | 12 | public override object Accept( IAstVisitor visitor ) 13 | { 14 | return visitor.Visit( this ); 15 | } 16 | 17 | #endregion 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Bite/Ast/MemberInitializationNode.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public class MemberInitializationNode : AstBaseNode 7 | { 8 | public Identifier Identifier; 9 | public ExpressionBaseNode Expression; 10 | 11 | #region Public 12 | 13 | public override object Accept( IAstVisitor visitor ) 14 | { 15 | throw new NotImplementedException(); 16 | } 17 | 18 | #endregion 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Bite/Ast/ModifiersBaseNode.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public class ModifiersBaseNode : AstBaseNode 7 | { 8 | public enum ModifierTypes 9 | { 10 | DeclareExtern, 11 | DeclareCallable, 12 | DeclarePrivate, 13 | DeclarePublic, 14 | DeclareAbstract, 15 | DeclareStatic 16 | } 17 | 18 | public List < ModifierTypes > Modifiers; 19 | 20 | #region Public 21 | 22 | public ModifiersBaseNode( string accessMod, string staticAbstractMod ) 23 | { 24 | Modifiers = new List < ModifierTypes >(); 25 | 26 | if ( accessMod != null && accessMod == "public" ) 27 | { 28 | Modifiers.Add( ModifierTypes.DeclarePublic ); 29 | } 30 | else 31 | { 32 | Modifiers.Add( ModifierTypes.DeclarePrivate ); 33 | } 34 | 35 | if ( staticAbstractMod != null && staticAbstractMod == "static" ) 36 | { 37 | Modifiers.Add( ModifierTypes.DeclareStatic ); 38 | } 39 | else if ( staticAbstractMod != null && staticAbstractMod == "abstract" ) 40 | { 41 | Modifiers.Add( ModifierTypes.DeclareAbstract ); 42 | } 43 | } 44 | 45 | public ModifiersBaseNode( string accessMod, string staticAbstractMod, bool isExtern, bool isCallable ) 46 | { 47 | Modifiers = new List < ModifierTypes >(); 48 | 49 | if ( accessMod != null && accessMod == "public" ) 50 | { 51 | Modifiers.Add( ModifierTypes.DeclarePublic ); 52 | } 53 | else 54 | { 55 | Modifiers.Add( ModifierTypes.DeclarePrivate ); 56 | } 57 | 58 | if ( staticAbstractMod != null && staticAbstractMod == "static" ) 59 | { 60 | Modifiers.Add( ModifierTypes.DeclareStatic ); 61 | } 62 | else if ( staticAbstractMod != null && staticAbstractMod == "abstract" ) 63 | { 64 | Modifiers.Add( ModifierTypes.DeclareAbstract ); 65 | } 66 | 67 | if ( isExtern ) 68 | { 69 | Modifiers.Add( ModifierTypes.DeclareExtern ); 70 | } 71 | 72 | if ( isCallable ) 73 | { 74 | Modifiers.Add( ModifierTypes.DeclareCallable ); 75 | } 76 | } 77 | 78 | public override object Accept( IAstVisitor visitor ) 79 | { 80 | return visitor.Visit( this ); 81 | } 82 | 83 | #endregion 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /Bite/Ast/ModuleBaseNode.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Diagnostics; 3 | using System.Linq; 4 | 5 | namespace Bite.Ast 6 | { 7 | 8 | [DebuggerDisplay( "{ModuleIdent.ModuleId.Id}" )] 9 | public class ModuleBaseNode : AstBaseNode 10 | { 11 | public ModuleIdentifier ModuleIdent; 12 | public IEnumerable < ModuleIdentifier > ImportedModules; 13 | public IEnumerable < ModuleIdentifier > UsedModules; 14 | public IEnumerable < StatementBaseNode > Statements; 15 | 16 | #region Public 17 | 18 | public ModuleBaseNode() 19 | { 20 | ModuleIdent = new ModuleIdentifier(); 21 | } 22 | 23 | public override object Accept( IAstVisitor visitor ) 24 | { 25 | return visitor.Visit( this ); 26 | } 27 | 28 | public void AddStatements( IEnumerable < StatementBaseNode > statementNodes ) 29 | { 30 | Statements = statementNodes.ToList(); 31 | } 32 | 33 | #endregion 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Bite/Ast/ModuleIdentifier.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public class ModuleIdentifier : AstBaseNode 7 | { 8 | public Identifier ModuleId; 9 | private readonly List < Identifier > ParentModules; 10 | 11 | #region Public 12 | 13 | public ModuleIdentifier() 14 | { 15 | } 16 | 17 | public ModuleIdentifier( string id ) 18 | { 19 | ModuleId = new Identifier( id ); 20 | ParentModules = new List < Identifier >(); 21 | } 22 | 23 | public ModuleIdentifier( string id, List < string > parentModules ) 24 | { 25 | ModuleId = new Identifier( id ); 26 | ParentModules = new List < Identifier >(); 27 | 28 | foreach ( string parentModule in parentModules ) 29 | { 30 | ParentModules.Add( new Identifier( parentModule ) ); 31 | } 32 | } 33 | 34 | public override object Accept( IAstVisitor visitor ) 35 | { 36 | return visitor.Visit( this ); 37 | } 38 | 39 | public override string ToString() 40 | { 41 | string qualifiedName = ""; 42 | 43 | foreach ( Identifier parentModule in ParentModules ) 44 | { 45 | qualifiedName += parentModule.Id + "."; 46 | } 47 | 48 | qualifiedName += ModuleId.Id; 49 | 50 | return qualifiedName; 51 | } 52 | 53 | #endregion 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Bite/Ast/ParametersBaseNode.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public class ParametersBaseNode : AstBaseNode 7 | { 8 | public List < Identifier > Identifiers; 9 | 10 | #region Public 11 | 12 | public override object Accept( IAstVisitor visitor ) 13 | { 14 | return visitor.Visit( this ); 15 | } 16 | 17 | #endregion 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Bite/Ast/PrimaryBaseNode.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Ast 4 | { 5 | 6 | public class InterpolatedStringPart 7 | { 8 | public string TextBeforeExpression; 9 | public ExpressionBaseNode ExpressionBaseNode; 10 | 11 | #region Public 12 | 13 | public InterpolatedStringPart( string textBeforeExpression, ExpressionBaseNode expressionBaseNode ) 14 | { 15 | TextBeforeExpression = textBeforeExpression; 16 | ExpressionBaseNode = expressionBaseNode; 17 | } 18 | 19 | #endregion 20 | } 21 | 22 | public class InterpolatedString : ExpressionBaseNode 23 | { 24 | public List < InterpolatedStringPart > StringParts; 25 | public string TextAfterLastExpression; 26 | } 27 | 28 | public class ArrayExpressionNode : ExpressionBaseNode 29 | { 30 | public List < ExpressionBaseNode > Expressions; 31 | } 32 | 33 | public class DictionaryInitializerNode : ExpressionBaseNode 34 | { 35 | public Dictionary < Identifier, ExpressionBaseNode > ElementInitializers; 36 | } 37 | 38 | public class PrimaryBaseNode : ExpressionBaseNode 39 | { 40 | public enum PrimaryTypes 41 | { 42 | Default, 43 | Identifier, 44 | BooleanLiteral, 45 | IntegerLiteral, 46 | FloatLiteral, 47 | StringLiteral, 48 | InterpolatedString, 49 | Expression, 50 | NullReference, 51 | ThisReference, 52 | ArrayExpression, 53 | DictionaryExpression 54 | } 55 | 56 | public PrimaryTypes PrimaryType; 57 | public Identifier PrimaryId; 58 | public bool? BooleanLiteral = null; 59 | public int? IntegerLiteral = null; 60 | public double? FloatLiteral = null; 61 | public string StringLiteral; 62 | 63 | public InterpolatedString InterpolatedString; 64 | public AstBaseNode Expression; 65 | 66 | #region Public 67 | 68 | public override object Accept( IAstVisitor visitor ) 69 | { 70 | return visitor.Visit( this ); 71 | } 72 | 73 | #endregion 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /Bite/Ast/ReturnStatementBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class ReturnStatementBaseNode : StatementBaseNode 5 | { 6 | public ExpressionStatementBaseNode ExpressionStatementBase; 7 | 8 | #region Public 9 | 10 | public ReturnStatementBaseNode() 11 | { 12 | ExpressionStatementBase = new ExpressionStatementBaseNode(); 13 | } 14 | 15 | public ReturnStatementBaseNode( ExpressionStatementBaseNode expressionStatementBase ) 16 | { 17 | ExpressionStatementBase = expressionStatementBase; 18 | } 19 | 20 | public override object Accept( IAstVisitor visitor ) 21 | { 22 | return visitor.Visit( this ); 23 | } 24 | 25 | #endregion 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Bite/Ast/StatementBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public abstract class StatementBaseNode : AstBaseNode 5 | { 6 | } 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Bite/Ast/StructDeclarationBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class StructDeclarationBaseNode : DeclarationBaseNode 5 | { 6 | public Identifier StructId; 7 | public ModifiersBaseNode ModifiersBase; 8 | public BlockStatementBaseNode Block; 9 | 10 | #region Public 11 | 12 | public override object Accept( IAstVisitor visitor ) 13 | { 14 | return visitor.Visit( this ); 15 | } 16 | 17 | #endregion 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Bite/Ast/SyncBlockNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class SyncBlockNode : StatementBaseNode 5 | { 6 | public BlockStatementBaseNode Block; 7 | 8 | #region Public 9 | 10 | public override object Accept( IAstVisitor visitor ) 11 | { 12 | return visitor.Visit( this ); 13 | } 14 | 15 | #endregion 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Bite/Ast/TernaryOperationBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class TernaryOperationBaseNode : ExpressionBaseNode 5 | { 6 | public ExpressionBaseNode LeftOperand; 7 | public ExpressionBaseNode MidOperand; 8 | public ExpressionBaseNode RightOperand; 9 | 10 | #region Public 11 | 12 | public override object Accept( IAstVisitor visitor ) 13 | { 14 | return visitor.Visit( this ); 15 | } 16 | 17 | #endregion 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Bite/Ast/UnaryPostfixOperation.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class UnaryPostfixOperation : ExpressionBaseNode 5 | { 6 | public enum UnaryPostfixOperatorType 7 | { 8 | PlusPlus, 9 | MinusMinus 10 | } 11 | 12 | public ExpressionBaseNode Primary; 13 | public UnaryPostfixOperatorType Operator; 14 | 15 | #region Public 16 | 17 | public override object Accept( IAstVisitor visitor ) 18 | { 19 | return visitor.Visit( this ); 20 | } 21 | 22 | #endregion 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Bite/Ast/UnaryPrefixOperation.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class UnaryPrefixOperation : ExpressionBaseNode 5 | { 6 | public enum UnaryPrefixOperatorType 7 | { 8 | Plus, 9 | Compliment, 10 | PlusPlus, 11 | MinusMinus, 12 | LogicalNot, 13 | Negate 14 | } 15 | 16 | public ExpressionBaseNode Primary; 17 | public UnaryPrefixOperatorType Operator; 18 | 19 | #region Public 20 | 21 | public override object Accept( IAstVisitor visitor ) 22 | { 23 | return visitor.Visit( this ); 24 | } 25 | 26 | #endregion 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Bite/Ast/UsingStatementBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class UsingStatementBaseNode : StatementBaseNode 5 | { 6 | public AstBaseNode UsingBaseNode; 7 | public BlockStatementBaseNode UsingBlock; 8 | 9 | #region Public 10 | 11 | public override object Accept( IAstVisitor visitor ) 12 | { 13 | return visitor.Visit( this ); 14 | } 15 | 16 | #endregion 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Bite/Ast/VariableDeclarationBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class VariableDeclarationBaseNode : DeclarationBaseNode 5 | { 6 | public ModifiersBaseNode ModifiersBase; 7 | public Identifier VarId; 8 | public InitializerBaseNode InitializerBase; 9 | 10 | #region Public 11 | 12 | public override object Accept( IAstVisitor visitor ) 13 | { 14 | return visitor.Visit( this ); 15 | } 16 | 17 | #endregion 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Bite/Ast/WhileStatementBaseNode.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Ast 2 | { 3 | 4 | public class WhileStatementBaseNode : StatementBaseNode 5 | { 6 | public ExpressionBaseNode ExpressionBase; 7 | public BlockStatementBaseNode WhileBlock; 8 | 9 | #region Public 10 | 11 | public override object Accept( IAstVisitor visitor ) 12 | { 13 | return visitor.Visit( this ); 14 | } 15 | 16 | #endregion 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Bite/Bite.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net462;netstandard2.0 5 | Bite 6 | Bite 7 | BiteVm 8 | Maximilian Winter 9 | None 10 | Bite 11 | 0.1.5 12 | BiteVm 13 | Copyright (c) Maximilian Winter 2022 14 | https://github.com/Maximilian-Winter/Bite-Programming-Language/wiki 15 | 16 | https://github.com/Maximilian-Winter/Bite-Programming-Language 17 | LICENSE 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Bite/CodeGenerator/BiteCompilationContext.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Bite.Runtime.Bytecode; 3 | using Bite.Symbols; 4 | 5 | namespace Bite.Runtime.CodeGen 6 | { 7 | 8 | public class BiteCompilationContext 9 | { 10 | private readonly Dictionary < string, Chunk > m_CompilingChunks; 11 | private readonly Stack < Chunk > _savedChunks = new Stack < Chunk >(); 12 | private readonly Chunk m_MainChunk; 13 | 14 | internal BaseScope BaseScope { get; } 15 | 16 | internal Chunk CurrentChunk { get; private set; } 17 | 18 | internal Dictionary < string, BinaryChunk > CompiledChunks { get; private set; } 19 | 20 | internal BinaryChunk CompiledMainChunk { get; private set; } 21 | 22 | #region Public 23 | 24 | public BiteCompilationContext( SymbolTable symbolTable ) 25 | { 26 | BaseScope = symbolTable.RootScope; 27 | m_CompilingChunks = new Dictionary < string, Chunk >(); 28 | m_MainChunk = new Chunk(); 29 | CurrentChunk = m_MainChunk; 30 | } 31 | 32 | internal void Build() 33 | { 34 | CompiledChunks = new Dictionary < string, BinaryChunk >(); 35 | 36 | foreach ( KeyValuePair < string, Chunk > compilingChunk in m_CompilingChunks ) 37 | { 38 | CompiledChunks.Add( 39 | compilingChunk.Key, 40 | new BinaryChunk( 41 | compilingChunk.Value.SerializeToBytes(), 42 | compilingChunk.Value.Constants, 43 | compilingChunk.Value.Lines ) ); 44 | } 45 | 46 | CompiledMainChunk = new BinaryChunk( m_MainChunk.SerializeToBytes(), m_MainChunk.Constants, m_MainChunk.Lines ); 47 | } 48 | 49 | internal bool HasChunk( string moduleName ) 50 | { 51 | return m_CompilingChunks.ContainsKey( moduleName ); 52 | } 53 | 54 | internal void NewChunk() 55 | { 56 | CurrentChunk = new Chunk(); 57 | } 58 | 59 | internal void PopChunk() 60 | { 61 | CurrentChunk = _savedChunks.Pop(); 62 | } 63 | 64 | internal void PushChunk() 65 | { 66 | // TODO: this is only ever one depth pushed so maybe we don't need a stack and Push/Pop API? 67 | _savedChunks.Push( CurrentChunk ); 68 | } 69 | 70 | internal void RestoreChunk( string moduleName ) 71 | { 72 | CurrentChunk = m_CompilingChunks[moduleName]; 73 | } 74 | 75 | internal void SaveCurrentChunk( string moduleName ) 76 | { 77 | m_CompilingChunks.Add( moduleName, CurrentChunk ); 78 | } 79 | 80 | internal void SetCurrentChunk( Chunk chunk ) 81 | { 82 | CurrentChunk = chunk; 83 | } 84 | 85 | #endregion 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /Bite/CodeGenerator/BiteProgram.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | using Bite.Modules.Callables; 5 | using Bite.Runtime.Bytecode; 6 | using Bite.Runtime.Functions.ForeignInterface; 7 | using Bite.Symbols; 8 | 9 | namespace Bite.Runtime.CodeGen 10 | { 11 | 12 | /// 13 | /// 14 | public class BiteProgram 15 | { 16 | public TypeRegistry TypeRegistry { get; } = new TypeRegistry(); 17 | 18 | public SymbolTable SymbolTable { get; private set; } 19 | 20 | internal BinaryChunk CompiledMainChunk { get; private set; } 21 | 22 | internal Dictionary < string, BinaryChunk > CompiledChunks { get; private set; } 23 | 24 | #region Public 25 | 26 | internal BiteProgram() 27 | { 28 | } 29 | 30 | internal static BiteProgram Create( 31 | SymbolTable symbolTable, 32 | BinaryChunk compiledMainChunk, 33 | Dictionary < string, BinaryChunk > compiledChunks ) 34 | { 35 | return new BiteProgram 36 | { 37 | SymbolTable = symbolTable, CompiledMainChunk = compiledMainChunk, CompiledChunks = compiledChunks 38 | }; 39 | } 40 | 41 | /// 42 | /// Creates a new and executes the current 43 | /// 44 | /// 45 | public BiteResult Run( Dictionary < string, object > externalObjects = null ) 46 | { 47 | BiteVm biteVm = new BiteVm(); 48 | biteVm.InitVm(); 49 | biteVm.RegisterSystemModuleCallables( TypeRegistry ); 50 | biteVm.RegisterExternalGlobalObjects( externalObjects ); 51 | 52 | BiteVmInterpretResult result = biteVm.Interpret( this, CancellationToken.None ); 53 | 54 | return new BiteResult { InterpretResult = result, ReturnValue = biteVm.ReturnValue }; 55 | } 56 | 57 | /// 58 | /// Creates a new and executes the current 59 | /// 60 | /// 61 | public BiteResult Run( CancellationToken cancellationToken, Dictionary < string, object > externalObjects = null ) 62 | { 63 | BiteVm biteVm = new BiteVm(); 64 | biteVm.InitVm(); 65 | biteVm.RegisterSystemModuleCallables( TypeRegistry ); 66 | biteVm.RegisterExternalGlobalObjects( externalObjects ); 67 | 68 | BiteVmInterpretResult result = biteVm.Interpret( this, cancellationToken ); 69 | 70 | return new BiteResult { InterpretResult = result, ReturnValue = biteVm.ReturnValue }; 71 | } 72 | 73 | /// 74 | /// Creates a new and executes the current 75 | /// 76 | /// 77 | public async Task < BiteResult > RunAsync( 78 | CancellationToken cancellationToken, 79 | Dictionary < string, object > externalObjects = null ) 80 | { 81 | BiteVm biteVm = new BiteVm(); 82 | biteVm.InitVm(); 83 | biteVm.RegisterSystemModuleCallables( TypeRegistry ); 84 | biteVm.RegisterExternalGlobalObjects( externalObjects ); 85 | 86 | BiteVmInterpretResult result = await biteVm.InterpretAsync( this, cancellationToken ); 87 | 88 | return new BiteResult { InterpretResult = result, ReturnValue = biteVm.ReturnValue }; 89 | } 90 | 91 | #endregion 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /Bite/CodeGenerator/BiteResult.cs: -------------------------------------------------------------------------------- 1 | using Bite.Runtime.Memory; 2 | 3 | namespace Bite.Runtime.CodeGen 4 | { 5 | 6 | public class BiteResult 7 | { 8 | public DynamicBiteVariable ReturnValue { get; set; } 9 | 10 | public BiteVmInterpretResult InterpretResult { get; set; } 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Bite/CodeGenerator/CompilerException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Bite.Ast; 3 | 4 | namespace Bite.Runtime.CodeGen 5 | { 6 | 7 | public class CompilerException : Exception 8 | { 9 | public AstBaseNode BaseNode { get; } 10 | 11 | public override string Message => 12 | base.Message + 13 | $" in column {BaseNode.DebugInfoAstNode.ColumnNumber} on line {BaseNode.DebugInfoAstNode.LineNumber}"; 14 | 15 | #region Public 16 | 17 | public CompilerException( string message, AstBaseNode baseNode ) : base( message ) 18 | { 19 | BaseNode = baseNode; 20 | } 21 | 22 | #endregion 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Bite/Compiler/BiteCompilerException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Bite.Compiler 6 | { 7 | 8 | public class BiteCompilerException : ApplicationException 9 | { 10 | public string BiteCompilerExceptionMessage { get; } 11 | 12 | public IReadOnlyCollection < BiteCompilerSyntaxError > SyntaxErrors { get; } 13 | 14 | public override string Message 15 | { 16 | get 17 | { 18 | StringBuilder stringBuilder = new StringBuilder(); 19 | 20 | stringBuilder.Append( BiteCompilerExceptionMessage ); 21 | 22 | stringBuilder.AppendLine(); 23 | 24 | foreach ( BiteCompilerSyntaxError syntaxError in SyntaxErrors ) 25 | { 26 | stringBuilder.AppendLine( syntaxError.Message ); 27 | } 28 | 29 | return stringBuilder.ToString(); 30 | } 31 | } 32 | 33 | #region Public 34 | 35 | public BiteCompilerException( string message, IReadOnlyCollection < BiteCompilerSyntaxError > syntaxErrors ) : 36 | base( message ) 37 | { 38 | BiteCompilerExceptionMessage = message; 39 | SyntaxErrors = syntaxErrors; 40 | } 41 | 42 | #endregion 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Bite/Compiler/BiteCompilerSyntaxError.cs: -------------------------------------------------------------------------------- 1 | using Antlr4.Runtime; 2 | 3 | namespace Bite.Compiler 4 | { 5 | 6 | public readonly struct BiteCompilerSyntaxError 7 | { 8 | public readonly IRecognizer Recognizer; 9 | public readonly IToken OffendingSymbol; 10 | public readonly int Line; 11 | public readonly int CharPositionInLine; 12 | public readonly string Message; 13 | public readonly RecognitionException Exception; 14 | 15 | public BiteCompilerSyntaxError( 16 | IRecognizer recognizer, 17 | IToken offendingSymbol, 18 | int line, 19 | int charPositionInLine, 20 | string message, 21 | RecognitionException exception ) 22 | { 23 | Recognizer = recognizer; 24 | OffendingSymbol = offendingSymbol; 25 | Line = line; 26 | CharPositionInLine = charPositionInLine; 27 | Message = message; 28 | Exception = exception; 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Bite/Compiler/BiteCompilerSyntaxErrorListener.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.IO; 3 | using Antlr4.Runtime; 4 | 5 | namespace Bite.Compiler 6 | { 7 | 8 | public class BiteCompilerSyntaxErrorListener : BaseErrorListener 9 | { 10 | public readonly List < BiteCompilerSyntaxError > Errors = new List < BiteCompilerSyntaxError >(); 11 | 12 | #region Public 13 | 14 | public override void SyntaxError( 15 | TextWriter output, 16 | IRecognizer recognizer, 17 | IToken offendingSymbol, 18 | int line, 19 | int charPositionInLine, 20 | string msg, 21 | RecognitionException e ) 22 | { 23 | Errors.Add( new BiteCompilerSyntaxError( recognizer, offendingSymbol, line, charPositionInLine, msg, e ) ); 24 | } 25 | 26 | #endregion 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Bite/Grammar/BITELexer.g4: -------------------------------------------------------------------------------- 1 | lexer grammar BITELexer; 2 | 3 | 4 | 5 | @members { 6 | int nesting = 0; 7 | } 8 | 9 | DeclareModule: 'module'; 10 | DeclareClass: 'class'; 11 | DeclareStruct: 'struct'; 12 | DeclareClassInstance: 'new'; 13 | AsKeyword: 'as'; 14 | ExternModifier: 'extern'; 15 | CallableModifier: 'callable'; 16 | DeclareFunction: 'function'; 17 | DeclareVariable: 'var'; 18 | DeclareGetter: 'get'; 19 | DeclareSetter: 'set'; 20 | DeclareForLoop: 'for'; 21 | DeclareWhileLoop: 'while'; 22 | DeclareStatic: 'static'; 23 | DeclareAbstract: 'abstract'; 24 | DeclarePublic: 'public'; 25 | DeclarePrivate: 'private'; 26 | ControlFlowIf: 'if'; 27 | ControlFlowElse: 'else'; 28 | FunctionReturn: 'return'; 29 | Break: 'break'; 30 | NullReference: 'null'; 31 | ThisReference: 'this'; 32 | UsingDirective: 'using'; 33 | ImportDirective: 'import'; 34 | StartStatement: 'start'; 35 | UseStatement: 'use'; 36 | ThreadStatement: 'thread'; 37 | SyncKeyword: 'sync'; 38 | 39 | 40 | AssignOperator: '='; 41 | PlusAssignOperator: '+='; 42 | MinusAssignOperator: '-='; 43 | MultiplyAssignOperator: '*='; 44 | DivideAssignOperator: '/='; 45 | ModuloAssignOperator: '%='; 46 | BitwiseAndAssignOperator: '&='; 47 | BitwiseOrAssignOperator: '|='; 48 | BitwiseXorAssignOperator: '^='; 49 | BitwiseLeftShiftAssignOperator: '<<='; 50 | BitwiseRightShiftAssignOperator: '>>='; 51 | 52 | LogicalOrOperator: '||'; 53 | LogicalAndOperator: '&&'; 54 | 55 | UnequalOperator: '!='; 56 | EqualOperator: '=='; 57 | 58 | GreaterOperator: '>'; 59 | ShiftRightOperator: '>>'; 60 | GreaterEqualOperator: '>='; 61 | SmallerOperator: '<'; 62 | ShiftLeftOperator: '<<'; 63 | SmallerEqualOperator: '<='; 64 | 65 | MinusOperator: '-'; 66 | MinusMinusOperator: '--'; 67 | PlusOperator: '+'; 68 | PlusPlusOperator: '++'; 69 | DivideOperator: '/'; 70 | MultiplyOperator: '*'; 71 | 72 | LogicalNegationOperator: '!'; 73 | DotOperator: '.'; 74 | QuestionMarkOperator: '?'; 75 | ColonOperator: ':'; 76 | ReferenceOperator: '->'; 77 | 78 | ModuloOperator: '%'; 79 | 80 | ComplimentOperator: '~'; 81 | 82 | BitwiseAndOperator:'&'; 83 | BitwiseXorOperator:'^'; 84 | BitwiseOrOperator:'|'; 85 | 86 | OpeningRoundBracket: '('; 87 | ClosingRoundBracket: ')'; 88 | 89 | SquareBracketLeft : '['; 90 | SquareBracketRight : ']'; 91 | 92 | CommaSeparator: ','; 93 | SemicolonSeparator: ';'; 94 | 95 | DollarOperator:'$'; 96 | 97 | BooleanLiteral: False_ | True_; 98 | False_: 'false'; 99 | True_: 'true'; 100 | 101 | IntegerLiteral: 102 | DecimalLiteral; 103 | 104 | FloatingLiteral: 105 | Fractionalconstant Exponentpart? 106 | | Digitsequence Exponentpart; 107 | 108 | fragment DIGIT: [0-9]; 109 | 110 | DecimalLiteral: (DIGIT)+; 111 | 112 | fragment NONZERODIGIT: [1-9]; 113 | 114 | 115 | fragment Fractionalconstant: 116 | Digitsequence? '.' Digitsequence 117 | | Digitsequence '.'; 118 | 119 | fragment Exponentpart: 120 | 'e' SIGN? Digitsequence 121 | | 'E' SIGN? Digitsequence; 122 | 123 | fragment SIGN: [+-]; 124 | 125 | fragment Digitsequence: DIGIT ('\''? DIGIT)*; 126 | 127 | 128 | 129 | Identifier: [a-zA-Z_][a-zA-Z0-9_]* ; 130 | 131 | COMMENT: '/*' .*? '*/' -> skip; 132 | WS : [ \r\t\u000C\n]+ -> skip; 133 | 134 | LINE_COMMENT: '//' ~[\r\n]* '\r'? '\n' -> skip; 135 | 136 | DQUOTE: '"' -> pushMode(IN_STRING); 137 | CURLY_L: '{' -> pushMode(DEFAULT_MODE); 138 | CURLY_R: '}' -> popMode; 139 | 140 | mode IN_STRING; 141 | 142 | TEXT: ~[$\\"]+ ; 143 | 144 | BACKSLASH_PAREN: '${' -> pushMode(DEFAULT_MODE); 145 | 146 | ESCAPE_SEQUENCE: '\\' . ; 147 | 148 | DQUOTE_IN_STRING: '"' -> type(DQUOTE), popMode; 149 | 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /Bite/Grammar/compile.bat: -------------------------------------------------------------------------------- 1 | antlr4 -visitor -no-listener -Dlanguage=CSharp -package AntlrBiteParser BITELexer.g4 BITEParser.g4 -o ..\AntlrGenerated\AntlrBiteParser 2 | -------------------------------------------------------------------------------- /Bite/LICENSE: -------------------------------------------------------------------------------- 1 | Bite License 2 | [The BSD License] 3 | Copyright (c) 2022 Maximilian Winter 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 | Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 9 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 10 | 11 | 12 | ANTLR 4 License 13 | [The BSD License] 14 | Copyright (c) 2012 Terence Parr and Sam Harwell 15 | All rights reserved. 16 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 17 | 18 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 19 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 20 | Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 | -------------------------------------------------------------------------------- /Bite/Modules/Callables/System.cs: -------------------------------------------------------------------------------- 1 | using Bite.Runtime; 2 | using Bite.Runtime.Functions; 3 | using Bite.Runtime.Functions.ForeignInterface; 4 | using Bite.Runtime.Functions.Interop; 5 | 6 | namespace Bite.Modules.Callables 7 | { 8 | 9 | public static class SystemModule 10 | { 11 | #region Public 12 | 13 | public static void RegisterSystemModuleCallables( this BiteVm biteVm, TypeRegistry typeRegistry = null ) 14 | { 15 | biteVm.RegisterCallable( "GetConstructor", new InteropGetConstructor( typeRegistry ) ); 16 | biteVm.RegisterCallable( "GetStaticMember", new InteropGetStaticMember( typeRegistry ) ); 17 | biteVm.RegisterCallable( "GetStaticMethod", new InteropGetStaticMethod( typeRegistry ) ); 18 | biteVm.RegisterCallable( "GetMethod", new InteropGetMethod( typeRegistry ) ); 19 | biteVm.RegisterCallable( "GetStaticClass", new InteropGetStaticClass( typeRegistry ) ); 20 | biteVm.RegisterCallable( "GetGenericMethod", new InteropGetGenericMethod( typeRegistry ) ); 21 | biteVm.RegisterCallable( "Print", new PrintFunctionVm() ); 22 | biteVm.RegisterCallable( "PrintLine", new PrintLineFunctionVm() ); 23 | } 24 | 25 | #endregion 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Bite/Modules/Interop.bite: -------------------------------------------------------------------------------- 1 | // Bite Interop Module 2 | 3 | module Interop; 4 | 5 | 6 | // Linked to ForeignLibraryInterfaceVm 7 | 8 | extern callable function GetConstructor ( type, parameterTypes ); 9 | 10 | extern callable function GetStaticClass ( type ); 11 | 12 | extern callable function GetStaticMethod ( type, parameterTypes ); 13 | 14 | extern callable function GetGenericMethod ( method, genericTypes ); 15 | 16 | extern callable function GetStaticMember ( type, name ); 17 | 18 | extern callable function GetMethod ( type, methodName, parameterTypes ); 19 | 20 | // extern callable function GetPropertyOrField ( type, name ); 21 | 22 | -------------------------------------------------------------------------------- /Bite/Modules/ModuleLoader.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | 3 | namespace Bite.Modules 4 | { 5 | 6 | internal class ModuleLoader 7 | { 8 | #region Public 9 | 10 | public static string LoadModule( string moduleName ) 11 | { 12 | using ( Stream stream = 13 | typeof( ModuleLoader ).Assembly.GetManifestResourceStream( $"Bite.Modules.{moduleName}.bite" ) ) 14 | { 15 | StreamReader reader = new StreamReader( stream ); 16 | 17 | return reader.ReadToEnd(); 18 | } 19 | } 20 | 21 | #endregion 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Bite/Modules/System.bite: -------------------------------------------------------------------------------- 1 | // Bite System Module 2 | 3 | module System; 4 | import Interop; 5 | 6 | class Object { 7 | 8 | } 9 | 10 | // Linked to PrintFunctionVm 11 | 12 | extern callable function Print ( object ); 13 | 14 | // Linked to PrintLineFunctionVm 15 | 16 | extern callable function PrintLine ( object ); -------------------------------------------------------------------------------- /Bite/Runtime/BiteFunctionCall.cs: -------------------------------------------------------------------------------- 1 | using Bite.Runtime.Memory; 2 | 3 | namespace Bite.Runtime 4 | { 5 | 6 | public class BiteFunctionCall 7 | { 8 | public string FunctionName; 9 | public BiteChunkWrapper BiteChunkWrapper = null; 10 | public DynamicBiteVariable[] FunctionArguments; 11 | 12 | #region Public 13 | 14 | public BiteFunctionCall( 15 | string functionName, 16 | DynamicBiteVariable[] functionArguments ) 17 | { 18 | FunctionName = functionName; 19 | FunctionArguments = functionArguments; 20 | } 21 | 22 | public BiteFunctionCall( 23 | BiteChunkWrapper fWrapper, 24 | DynamicBiteVariable[] functionArguments ) 25 | { 26 | BiteChunkWrapper = fWrapper; 27 | FunctionArguments = functionArguments; 28 | } 29 | 30 | #endregion 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Bite/Runtime/BiteVmInterpretResult.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Runtime 2 | { 3 | 4 | public enum BiteVmInterpretResult 5 | { 6 | InterpretOk, 7 | InterpretCompileError, 8 | InterpretRuntimeError, 9 | Continue, 10 | Cancelled 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Bite/Runtime/BiteVmOpCodes.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Runtime 2 | { 3 | 4 | public enum BiteVmOpCodes : byte 5 | { 6 | OpNone, 7 | OpPopStack, 8 | OpConstant, 9 | OpAssign, 10 | OpPlusAssign, 11 | OpMinusAssign, 12 | OpMultiplyAssign, 13 | OpDivideAssign, 14 | OpModuloAssign, 15 | OpBitwiseAndAssign, 16 | OpBitwiseOrAssign, 17 | OpBitwiseXorAssign, 18 | OpBitwiseLeftShiftAssign, 19 | OpBitwiseRightShiftAssign, 20 | OpPushNextAssignmentOnStack, 21 | OpAdd, 22 | OpSubtract, 23 | OpMultiply, 24 | OpDivide, 25 | OpModulo, 26 | OpEqual, 27 | OpNotEqual, 28 | OpLess, 29 | OpLessOrEqual, 30 | OpGreater, 31 | OpGreaterEqual, 32 | OpAnd, 33 | OpOr, 34 | OpBitwiseOr, 35 | OpBitwiseAnd, 36 | OpBitwiseXor, 37 | OpBitwiseLeftShift, 38 | OpBitwiseRightShift, 39 | OpPostfixIncrement, 40 | OpPostfixDecrement, 41 | OpPrefixIncrement, 42 | OpPrefixDecrement, 43 | OpNegate, 44 | OpAffirm, 45 | OpCompliment, 46 | OpNot, 47 | OpDefineModule, 48 | OpDefineClass, 49 | OpUsingStatmentHead, 50 | OpUsingStatmentEnd, 51 | OpDefineMethod, 52 | OpDefineCallableMethod, 53 | OpDefineVar, 54 | OpDeclareVar, 55 | OpSetVar, 56 | OpSetVarExternal, 57 | OpGetVar, 58 | OpGetNextVarByRef, 59 | OpGetVarExternal, 60 | OpGetModule, 61 | OpDefineInstance, 62 | OpSetInstance, 63 | OpJumpIfFalse, 64 | OpJump, 65 | OpTernary, 66 | OpEnterBlock, 67 | OpExitBlock, 68 | OpWhileLoop, 69 | OpBindToFunction, 70 | OpGetElement, 71 | OpSetElement, 72 | OpCallFunctionByName, 73 | OpCallFunctionFromStack, 74 | OpSetFunctionParameterName, 75 | OpCallMemberFunction, 76 | OpGetMember, 77 | OpGetMemberWithString, 78 | OpSetMember, 79 | OpSetMemberWithString, 80 | OpKeepLastItemOnStack, 81 | OpBreak, 82 | OpReturn, 83 | OpSwitchContext, 84 | OpReturnContext 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /Bite/Runtime/BiteVmRuntimeException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bite.Runtime 4 | { 5 | 6 | public class BiteVmRuntimeException : ApplicationException 7 | { 8 | public string BiteVmRuntimeExceptionMessage { get; } 9 | 10 | #region Public 11 | 12 | public BiteVmRuntimeException( string message ) : base( message ) 13 | { 14 | BiteVmRuntimeExceptionMessage = message; 15 | } 16 | 17 | #endregion 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Bite/Runtime/Bytecode/BiteChunkWrapper.cs: -------------------------------------------------------------------------------- 1 | using Bite.Runtime.Bytecode; 2 | 3 | namespace Bite.Runtime 4 | { 5 | 6 | public class BiteChunkWrapper 7 | { 8 | public BinaryChunk ChunkToWrap { get; set; } 9 | 10 | #region Public 11 | 12 | public BiteChunkWrapper() 13 | { 14 | ChunkToWrap = null; 15 | } 16 | 17 | public BiteChunkWrapper( BinaryChunk chunkToWrap ) 18 | { 19 | ChunkToWrap = chunkToWrap; 20 | } 21 | 22 | #endregion 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Bite/Runtime/Bytecode/ChunkHelper.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | 3 | namespace Bite.Runtime.Bytecode 4 | { 5 | 6 | public static class ChunkHelper 7 | { 8 | #region Public 9 | 10 | public static int DesserializeOpCodeData( ref byte[] data, ref int instructionPointer ) 11 | { 12 | int result; 13 | 14 | result = data[instructionPointer] | 15 | ( data[instructionPointer + 1] << 8 ) | 16 | ( data[instructionPointer + 2] << 16 ) | 17 | ( data[instructionPointer + 3] << 24 ); 18 | 19 | //result = BitConverter.ToInt32( data, instructionPointer ); 20 | 21 | instructionPointer += 4; 22 | 23 | return result; 24 | } 25 | 26 | public static void FreeChunk( this Chunk chunk ) 27 | { 28 | chunk.Code.Clear(); 29 | chunk.Lines.Clear(); 30 | } 31 | 32 | public static byte[] SerializeToBytes( this Chunk chunk ) 33 | { 34 | using ( MemoryStream m = new MemoryStream() ) 35 | { 36 | using ( BinaryWriter writer = new BinaryWriter( m ) ) 37 | { 38 | foreach ( ByteCode code in chunk.Code ) 39 | { 40 | writer.Write( ( byte ) code.OpCode ); 41 | 42 | if ( code.OpCodeData != null ) 43 | { 44 | foreach ( int data in code.OpCodeData ) 45 | { 46 | writer.Write( data ); 47 | } 48 | } 49 | } 50 | } 51 | 52 | return m.ToArray(); 53 | } 54 | } 55 | 56 | public static int WriteToChunk( this Chunk chunk, BiteVmOpCodes code, int line ) 57 | { 58 | chunk.Code.Add( new ByteCode( code ) ); 59 | chunk.Lines.Add( line ); 60 | 61 | return chunk.Code.Count - 1; 62 | } 63 | 64 | public static int WriteToChunk( this Chunk chunk, ByteCode code, int line ) 65 | { 66 | chunk.Code.Add( code ); 67 | chunk.Lines.Add( line ); 68 | 69 | return chunk.Code.Count - 1; 70 | } 71 | 72 | public static int WriteToChunk( this Chunk chunk, BiteVmOpCodes code, ConstantValue constant, int line ) 73 | { 74 | chunk.Code.Add( new ByteCode( code, chunk.Constants.Count ) ); 75 | chunk.Constants.Add( constant ); 76 | chunk.Lines.Add( line ); 77 | 78 | return chunk.Code.Count - 1; 79 | } 80 | 81 | public static int WriteToChunk( 82 | this Chunk chunk, 83 | BiteVmOpCodes code, 84 | ConstantValue constant, 85 | int opCodeData, 86 | int line ) 87 | { 88 | chunk.Code.Add( new ByteCode( code, chunk.Constants.Count, opCodeData ) ); 89 | chunk.Constants.Add( constant ); 90 | chunk.Lines.Add( line ); 91 | 92 | return chunk.Code.Count - 1; 93 | } 94 | 95 | public static int WriteToChunk( 96 | this Chunk chunk, 97 | BiteVmOpCodes code, 98 | ConstantValue constant, 99 | int opCodeData, 100 | int opCodeData2, 101 | int line ) 102 | { 103 | chunk.Code.Add( new ByteCode( code, chunk.Constants.Count, opCodeData, opCodeData2 ) ); 104 | chunk.Constants.Add( constant ); 105 | chunk.Lines.Add( line ); 106 | 107 | return chunk.Code.Count - 1; 108 | } 109 | 110 | #endregion 111 | } 112 | 113 | } 114 | -------------------------------------------------------------------------------- /Bite/Runtime/Bytecode/ConstantValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | using Bite.Runtime.Memory; 4 | 5 | namespace Bite.Runtime.Bytecode 6 | { 7 | 8 | [StructLayout( LayoutKind.Explicit )] 9 | public struct ConstantValue 10 | { 11 | [FieldOffset( 0 )] 12 | public ConstantValueType ConstantType; 13 | 14 | [FieldOffset( 1 )] 15 | public bool BoolConstantValue; 16 | 17 | [FieldOffset( 1 )] 18 | public int IntegerConstantValue; 19 | 20 | [FieldOffset( 1 )] 21 | public double DoubleConstantValue; 22 | 23 | [FieldOffset( 16 )] 24 | public string StringConstantValue; 25 | 26 | [FieldOffset( 16 )] 27 | public object NullConstantValue; 28 | 29 | public ConstantValue( int value ) : this() 30 | { 31 | IntegerConstantValue = value; 32 | ConstantType = ConstantValueType.Integer; 33 | } 34 | 35 | public ConstantValue( double value ) : this() 36 | { 37 | DoubleConstantValue = value; 38 | ConstantType = ConstantValueType.Double; 39 | } 40 | 41 | public ConstantValue( string value ) : this() 42 | { 43 | StringConstantValue = value; 44 | ConstantType = ConstantValueType.String; 45 | } 46 | 47 | public ConstantValue( bool value ) : this() 48 | { 49 | BoolConstantValue = value; 50 | ConstantType = ConstantValueType.Bool; 51 | } 52 | 53 | public ConstantValue( object value ) : this() 54 | { 55 | if ( value == null ) 56 | { 57 | NullConstantValue = value; 58 | ConstantType = ConstantValueType.Null; 59 | } 60 | } 61 | 62 | public override string ToString() 63 | { 64 | switch ( ConstantType ) 65 | { 66 | case ConstantValueType.Integer: 67 | return $"{ConstantType.ToString()}: {IntegerConstantValue.ToString()}"; 68 | 69 | case ConstantValueType.Double: 70 | return $"{ConstantType.ToString()}: {DoubleConstantValue.ToString()}"; 71 | 72 | case ConstantValueType.String: 73 | return $"{ConstantType.ToString()}: {StringConstantValue}"; 74 | 75 | case ConstantValueType.Bool: 76 | return $"{ConstantType.ToString()}: {BoolConstantValue.ToString()}"; 77 | 78 | case ConstantValueType.Null: 79 | return $"{ConstantType.ToString()}"; 80 | 81 | default: 82 | throw new ArgumentOutOfRangeException(); 83 | } 84 | } 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /Bite/Runtime/BytecodeList.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Bite.Runtime.Bytecode; 3 | 4 | namespace Bite.Runtime 5 | { 6 | 7 | public class BytecodeList 8 | { 9 | public List < ByteCode > ByteCodes = new List < ByteCode >(); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Bite/Runtime/BytecodeListStack.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bite.Runtime 4 | { 5 | 6 | public class BytecodeListStack 7 | { 8 | private readonly BytecodeList[] m_BytecodeLists = new BytecodeList[1024]; 9 | 10 | public int Count { get; set; } = 0; 11 | 12 | #region Public 13 | 14 | public BytecodeList Peek() 15 | { 16 | BytecodeList bytecodeList = m_BytecodeLists[Count - 1]; 17 | 18 | return bytecodeList; 19 | } 20 | 21 | public BytecodeList Peek( int i ) 22 | { 23 | BytecodeList bytecodeList = m_BytecodeLists[i]; 24 | 25 | return bytecodeList; 26 | } 27 | 28 | public BytecodeList Pop() 29 | { 30 | BytecodeList bytecodeList = m_BytecodeLists[--Count]; 31 | 32 | return bytecodeList; 33 | } 34 | 35 | public void Push( BytecodeList dynamicVar ) 36 | { 37 | m_BytecodeLists[Count] = dynamicVar; 38 | 39 | if ( Count >= 1023 ) 40 | { 41 | throw new IndexOutOfRangeException( "Stack Overflow" ); 42 | } 43 | 44 | Count++; 45 | } 46 | 47 | #endregion 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Bite/Runtime/DynamicBiteVariableStack.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Bite.Runtime.Memory; 3 | 4 | namespace Bite.Runtime 5 | { 6 | 7 | public class DynamicBiteVariableStack 8 | { 9 | private DynamicBiteVariable[] m_DynamicVariables = new DynamicBiteVariable[128]; 10 | 11 | public int Count { get; set; } = 0; 12 | 13 | #region Public 14 | 15 | public DynamicBiteVariable Peek() 16 | { 17 | DynamicBiteVariable dynamicVariable = m_DynamicVariables[Count - 1]; 18 | 19 | return dynamicVariable; 20 | } 21 | 22 | public DynamicBiteVariable Peek( int i ) 23 | { 24 | DynamicBiteVariable dynamicVariable = m_DynamicVariables[i]; 25 | 26 | return dynamicVariable; 27 | } 28 | 29 | public DynamicBiteVariable Pop() 30 | { 31 | DynamicBiteVariable dynamicVariable = m_DynamicVariables[--Count]; 32 | 33 | return dynamicVariable; 34 | } 35 | 36 | public void Push( DynamicBiteVariable dynamicVar ) 37 | { 38 | if ( Count >= m_DynamicVariables.Length ) 39 | { 40 | DynamicBiteVariable[] newProperties = new DynamicBiteVariable[m_DynamicVariables.Length * 2]; 41 | Array.Copy( m_DynamicVariables, newProperties, m_DynamicVariables.Length ); 42 | m_DynamicVariables = newProperties; 43 | } 44 | 45 | m_DynamicVariables[Count] = dynamicVar; 46 | 47 | Count++; 48 | } 49 | 50 | #endregion 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Bite/Runtime/FastMemoryStack.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Bite.Runtime.Memory; 3 | 4 | namespace Bite.Runtime 5 | { 6 | 7 | public class FastMemoryStack 8 | { 9 | private FastMemorySpace[] m_FastMemorySpaces = new FastMemorySpace[128]; 10 | 11 | public int Count { get; private set; } = 0; 12 | 13 | #region Public 14 | 15 | public FastMemorySpace Peek() 16 | { 17 | FastMemorySpace fastMemorySpace = m_FastMemorySpaces[Count - 1]; 18 | 19 | return fastMemorySpace; 20 | } 21 | 22 | public FastMemorySpace Pop() 23 | { 24 | FastMemorySpace fastMemorySpace = m_FastMemorySpaces[--Count]; 25 | 26 | return fastMemorySpace; 27 | } 28 | 29 | public void Push( FastMemorySpace fastMemorySpace ) 30 | { 31 | if ( Count >= m_FastMemorySpaces.Length ) 32 | { 33 | FastMemorySpace[] newProperties = new FastMemorySpace[m_FastMemorySpaces.Length * 2]; 34 | Array.Copy( m_FastMemorySpaces, newProperties, m_FastMemorySpaces.Length ); 35 | m_FastMemorySpaces = newProperties; 36 | } 37 | 38 | m_FastMemorySpaces[Count] = fastMemorySpace; 39 | Count++; 40 | } 41 | 42 | #endregion 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Bite/Runtime/FastPropertyCache.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Bite.Runtime.Functions.ForeignInterface; 4 | 5 | namespace Bite.Runtime 6 | { 7 | 8 | public class FastPropertyCache 9 | { 10 | private static Dictionary < string, IFastPropertyInfo > m_PropertyCache = new Dictionary < string, IFastPropertyInfo >(); 11 | 12 | #region Public 13 | 14 | public static void AddPropertyToCache(string propertyName) 15 | { 16 | string key = $"{typeof(T).FullName}.{propertyName}"; 17 | var type = typeof(T); 18 | var pi = type.GetProperty(propertyName); 19 | var fp = new FastPropertyInfo(pi); 20 | 21 | IFastPropertyInfo fastPropertyInfo = new CachedProperty < T >(fp.GetterDelegate, fp.SetterDelegate); 22 | fastPropertyInfo.PropertyType = pi.PropertyType; 23 | m_PropertyCache.Add( key, fastPropertyInfo ); 24 | } 25 | public bool TryGetProperty( Type type, string propertyName, out IFastPropertyInfo propertyInfo ) 26 | { 27 | string key = $"{type.FullName}.{propertyName}"; 28 | 29 | if ( !m_PropertyCache.TryGetValue( key, out propertyInfo ) ) 30 | { 31 | return false; 32 | } 33 | 34 | return true; 35 | } 36 | 37 | #endregion 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Bite/Runtime/FieldCache.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using Bite.Runtime.Functions.ForeignInterface; 5 | 6 | namespace Bite.Runtime 7 | { 8 | 9 | public class FieldCache 10 | { 11 | private static readonly Dictionary < string, FastFieldInfo > m_FieldCache = new Dictionary < string, FastFieldInfo >(); 12 | 13 | #region Public 14 | 15 | public bool TryGetField( Type type, string fieldName, out FastFieldInfo fieldInfo ) 16 | { 17 | string key = $"{type.FullName}.{fieldName}"; 18 | 19 | if ( !m_FieldCache.TryGetValue( key, out fieldInfo ) ) 20 | { 21 | FieldInfo fi = type.GetField( fieldName ); 22 | 23 | if ( fi != null ) 24 | { 25 | FastFieldInfo ffi = new FastFieldInfo( fi ); 26 | m_FieldCache.Add( key, ffi ); 27 | fieldInfo = ffi; 28 | return true; 29 | } 30 | 31 | return false; 32 | } 33 | 34 | return true; 35 | } 36 | 37 | #endregion 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/ForeignInterface/CSharpEvent.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using Bite.Runtime.Memory; 4 | 5 | namespace Bite.Runtime.Functions.ForeignInterface 6 | { 7 | 8 | public class CSharpEvent < V, T, K > : ICSharpEvent 9 | { 10 | private readonly EventWrapper < V, T, K > m_EventWrapper; 11 | 12 | #region Public 13 | 14 | public CSharpEvent( object obj ) 15 | { 16 | m_EventWrapper = new EventWrapper < V, T, K >( obj ); 17 | } 18 | 19 | public bool TryGetEventInfo( string name, out EventInfo eventInfo ) 20 | { 21 | return m_EventWrapper.TryGetEventInfo( name, out eventInfo ); 22 | } 23 | 24 | public object GetEventHolder() 25 | { 26 | return m_EventWrapper.EventHolder; 27 | } 28 | 29 | public void Invoke( string name, DynamicBiteVariable[] m_FunctionArguments ) 30 | { 31 | m_EventWrapper.Invoke( name, m_FunctionArguments ); 32 | } 33 | 34 | public bool TryAddEventHandler( string name, BiteChunkWrapper eventHandlerFunction, BiteVm biteVm ) 35 | { 36 | return m_EventWrapper.TryAddEventHandler( name, eventHandlerFunction, biteVm ); 37 | } 38 | 39 | 40 | 41 | #endregion 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/ForeignInterface/CachedProperty.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bite.Runtime.Functions.ForeignInterface 4 | { 5 | 6 | public class CachedProperty: IFastPropertyInfo 7 | { 8 | private FastPropertyInfo .ReturnValueDelegate GetterDelegate { get; } 9 | 10 | private FastPropertyInfo .UpdateValueDelegate SetterDelegate { get; } 11 | 12 | public CachedProperty(FastPropertyInfo .ReturnValueDelegate returnValueDelegate, FastPropertyInfo .UpdateValueDelegate updateValueDelegate) 13 | { 14 | GetterDelegate = returnValueDelegate; 15 | SetterDelegate = updateValueDelegate; 16 | } 17 | 18 | public Type PropertyType { get; set; } 19 | 20 | public object InvokeGet( object instance, params object[] arguments ) 21 | { 22 | return GetterDelegate((T)instance, arguments); 23 | } 24 | 25 | public void InvokeSet( object instance, object value, params object[] arguments ) 26 | { 27 | SetterDelegate( ( T ) instance, value, arguments ); 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/ForeignInterface/DelegateUtility.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bite.Runtime.Functions.ForeignInterface 4 | { 5 | 6 | public static class DelegateUtility 7 | { 8 | #region Public 9 | 10 | public static T Cast < T >( Delegate source ) where T : class 11 | { 12 | return Cast( source, typeof( T ) ) as T; 13 | } 14 | 15 | public static Delegate Cast( Delegate source, Type type ) 16 | { 17 | if ( source == null ) 18 | { 19 | return null; 20 | } 21 | 22 | Delegate[] delegates = source.GetInvocationList(); 23 | 24 | if ( delegates.Length == 1 ) 25 | { 26 | return Delegate.CreateDelegate( 27 | type, 28 | delegates[0].Target, 29 | delegates[0].Method ); 30 | } 31 | 32 | Delegate[] delegatesDest = new Delegate[delegates.Length]; 33 | 34 | for ( int nDelegate = 0; nDelegate < delegates.Length; nDelegate++ ) 35 | { 36 | delegatesDest[nDelegate] = Delegate.CreateDelegate( 37 | type, 38 | delegates[nDelegate].Target, 39 | delegates[nDelegate].Method ); 40 | } 41 | 42 | return Delegate.Combine( delegatesDest ); 43 | } 44 | 45 | #endregion 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/ForeignInterface/FastFieldInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using System.Reflection; 4 | 5 | namespace Bite.Runtime.Functions.ForeignInterface 6 | { 7 | 8 | public class FastFieldInfo 9 | { 10 | private delegate object ReturnValueDelegate( object instance ); 11 | 12 | private delegate void UpdateValueDelegate( object instance, object value ); 13 | 14 | private UpdateValueDelegate SetDelegate { get; } 15 | 16 | private ReturnValueDelegate Delegate { get; } 17 | 18 | #region Public 19 | 20 | public FastFieldInfo( FieldInfo propertyInfo ) 21 | { 22 | ParameterExpression instanceExpression = Expression.Parameter( typeof( object ), "instance" ); 23 | ParameterExpression valueExpression = Expression.Parameter( typeof( object ), "value" ); 24 | 25 | MemberExpression callExpression = Expression.Field( 26 | !propertyInfo.IsStatic ? propertyInfo.ReflectedType.IsValueType 27 | ? Expression.Unbox(instanceExpression, propertyInfo.ReflectedType) 28 | : Expression.Convert(instanceExpression, propertyInfo.ReflectedType) : null, 29 | propertyInfo ); 30 | 31 | Delegate = Expression.Lambda < ReturnValueDelegate >( 32 | Expression.Convert( callExpression, typeof( object ) ), 33 | instanceExpression ). 34 | Compile(); 35 | 36 | if ( !propertyInfo.ReflectedType.IsEnum ) 37 | { 38 | BinaryExpression assignExpression = Expression.Assign( 39 | callExpression, 40 | Expression.Convert( valueExpression, propertyInfo.FieldType ) ); 41 | 42 | SetDelegate = Expression.Lambda < UpdateValueDelegate >( 43 | assignExpression, 44 | instanceExpression, 45 | valueExpression ). 46 | Compile(); 47 | } 48 | 49 | 50 | FieldType = propertyInfo.FieldType; 51 | } 52 | 53 | public Type FieldType { get; set; } 54 | public object GetField( object instance ) 55 | { 56 | return Delegate( instance ); 57 | } 58 | 59 | public void SetField( object instance, object value ) 60 | { 61 | SetDelegate( instance, value ); 62 | } 63 | 64 | #endregion 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/ForeignInterface/FastMethodInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq.Expressions; 3 | using System.Reflection; 4 | 5 | namespace Bite.Runtime.Functions.ForeignInterface 6 | { 7 | 8 | public class FastMethodInfo 9 | { 10 | private delegate object ReturnValueDelegate( object instance, object[] arguments ); 11 | 12 | private delegate void VoidDelegate( object instance, object[] arguments ); 13 | 14 | private ReturnValueDelegate Delegate { get; } 15 | 16 | #region Public 17 | 18 | public FastMethodInfo( MethodInfo methodInfo ) 19 | { 20 | ParameterExpression instanceExpression = Expression.Parameter( typeof( object ), "instance" ); 21 | ParameterExpression argumentsExpression = Expression.Parameter( typeof( object[] ), "arguments" ); 22 | List < Expression > argumentExpressions = new List < Expression >(); 23 | ParameterInfo[] parameterInfos = methodInfo.GetParameters(); 24 | 25 | for ( int i = 0; i < parameterInfos.Length; ++i ) 26 | { 27 | ParameterInfo parameterInfo = parameterInfos[i]; 28 | 29 | argumentExpressions.Add( 30 | Expression.Convert( 31 | Expression.ArrayIndex( argumentsExpression, Expression.Constant( i ) ), 32 | parameterInfo.ParameterType ) ); 33 | } 34 | 35 | MethodCallExpression callExpression = Expression.Call( 36 | !methodInfo.IsStatic ? Expression.Convert( instanceExpression, methodInfo.ReflectedType ) : null, 37 | methodInfo, 38 | argumentExpressions ); 39 | 40 | if ( callExpression.Type == typeof( void ) ) 41 | { 42 | VoidDelegate voidDelegate = Expression.Lambda < VoidDelegate >( 43 | callExpression, 44 | instanceExpression, 45 | argumentsExpression ). 46 | Compile(); 47 | 48 | Delegate = ( instance, arguments ) => 49 | { 50 | voidDelegate( instance, arguments ); 51 | 52 | return null; 53 | }; 54 | } 55 | else 56 | { 57 | Delegate = Expression.Lambda < ReturnValueDelegate >( 58 | Expression.Convert( callExpression, typeof( object ) ), 59 | instanceExpression, 60 | argumentsExpression ). 61 | Compile(); 62 | } 63 | } 64 | 65 | public object Invoke( object instance, params object[] arguments ) 66 | { 67 | return Delegate( instance, arguments ); 68 | } 69 | 70 | #endregion 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/ForeignInterface/FastPropertyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq.Expressions; 3 | using System.Reflection; 4 | 5 | namespace Bite.Runtime.Functions.ForeignInterface 6 | { 7 | 8 | public class FastPropertyInfo < T > 9 | { 10 | public delegate object ReturnValueDelegate( T instance, object[] arguments ); 11 | 12 | public delegate void UpdateValueDelegate( T instance, object value, object[] arguments ); 13 | 14 | public ReturnValueDelegate GetterDelegate { get; } 15 | 16 | public UpdateValueDelegate SetterDelegate { get; } 17 | 18 | #region Public 19 | 20 | public FastPropertyInfo( PropertyInfo propertyInfo ) 21 | { 22 | ParameterExpression instanceExpression = Expression.Parameter( typeof( T ), "instance" ); 23 | ParameterExpression valueExpression = Expression.Parameter( typeof( object ), "value" ); 24 | ParameterExpression argumentsExpression = Expression.Parameter( typeof( object[] ), "arguments" ); 25 | List < Expression > argumentExpressions = new List < Expression >(); 26 | ParameterInfo[] parameterInfos = propertyInfo.GetIndexParameters(); 27 | 28 | for ( int i = 0; i < parameterInfos.Length; ++i ) 29 | { 30 | ParameterInfo parameterInfo = parameterInfos[i]; 31 | 32 | argumentExpressions.Add( 33 | Expression.Convert( 34 | Expression.ArrayIndex( argumentsExpression, Expression.Constant( i ) ), 35 | parameterInfo.ParameterType ) ); 36 | } 37 | 38 | IndexExpression callExpression = Expression.Property( 39 | instanceExpression, 40 | propertyInfo, 41 | argumentExpressions ); 42 | 43 | BinaryExpression assignExpression = Expression.Assign( 44 | callExpression, 45 | Expression.Convert( valueExpression, propertyInfo.PropertyType ) ); 46 | 47 | GetterDelegate = Expression.Lambda < ReturnValueDelegate >( 48 | Expression.Convert( callExpression, typeof( object ) ), 49 | instanceExpression, 50 | argumentsExpression ). 51 | Compile(); 52 | 53 | SetterDelegate = Expression.Lambda < UpdateValueDelegate >( 54 | assignExpression, 55 | instanceExpression, 56 | valueExpression, 57 | argumentsExpression ). 58 | Compile(); 59 | } 60 | 61 | public object InvokeGet( object instance, params object[] arguments ) 62 | { 63 | return GetterDelegate( ( T ) instance, arguments ); 64 | } 65 | 66 | public void InvokeSet( object instance, object value, params object[] arguments ) 67 | { 68 | SetterDelegate( ( T ) instance, value, arguments ); 69 | } 70 | 71 | #endregion 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/ForeignInterface/ICSharpEvent.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using Bite.Runtime.Memory; 4 | 5 | namespace Bite.Runtime.Functions.ForeignInterface 6 | { 7 | 8 | public interface ICSharpEvent 9 | { 10 | bool TryGetEventInfo( string name, out EventInfo eventInfo ); 11 | object GetEventHolder(); 12 | void Invoke( string name, DynamicBiteVariable[] m_FunctionArguments ); 13 | bool TryAddEventHandler( string name, BiteChunkWrapper eventHandlerFunction, BiteVm biteVm ); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/ForeignInterface/IFastPropertyInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bite.Runtime.Functions.ForeignInterface 4 | { 5 | 6 | public interface IFastPropertyInfo 7 | { 8 | Type PropertyType { get; set; } 9 | object InvokeGet( object instance, params object[] arguments ); 10 | 11 | void InvokeSet( object instance, object value, params object[] arguments ); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/ForeignInterface/InteropBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bite.Runtime.Functions.ForeignInterface 4 | { 5 | 6 | public abstract class InteropBase 7 | { 8 | protected readonly TypeRegistry m_TypeRegistry; 9 | 10 | protected InteropBase() 11 | { 12 | m_TypeRegistry = new TypeRegistry(); 13 | } 14 | 15 | protected InteropBase( TypeRegistry typeRegistry ) 16 | { 17 | m_TypeRegistry = typeRegistry; 18 | } 19 | 20 | 21 | public Type ResolveType( string name ) 22 | { 23 | if (m_TypeRegistry == null || !m_TypeRegistry.TryResolveType( name, out Type type )) 24 | { 25 | type = Type.GetType( name ); 26 | } 27 | 28 | return type; 29 | } 30 | 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /Bite/Runtime/Functions/ForeignInterface/StaticWrapper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | 5 | namespace Bite.Runtime.Functions.ForeignInterface 6 | { 7 | 8 | public class StaticWrapper 9 | { 10 | private readonly Dictionary < string, FastMethodInfo > CachedStaticMethods = 11 | new Dictionary < string, FastMethodInfo >(); 12 | 13 | public Type StaticWrapperType { get; } 14 | 15 | #region Public 16 | 17 | public StaticWrapper( Type type ) 18 | { 19 | StaticWrapperType = type; 20 | } 21 | 22 | public object InvokeMember( string name, object[] args, Type[] argsTypes ) 23 | { 24 | if ( !CachedStaticMethods.ContainsKey( name ) ) 25 | { 26 | MethodInfo method = StaticWrapperType.GetMethod( 27 | name, 28 | BindingFlags.Static | BindingFlags.Public, 29 | null, 30 | argsTypes, 31 | null ); 32 | 33 | FastMethodInfo fastMethodInfo = new FastMethodInfo( method ); 34 | 35 | CachedStaticMethods.Add( name, fastMethodInfo ); 36 | 37 | return fastMethodInfo.Invoke( null, args ); 38 | } 39 | 40 | return CachedStaticMethods[name].Invoke( null, args ); 41 | } 42 | 43 | #endregion 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/IBiteVmCallable.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Bite.Runtime.Memory; 3 | 4 | namespace Bite.Runtime.Functions 5 | { 6 | 7 | public interface IBiteVmCallable 8 | { 9 | object Call( DynamicBiteVariable[] arguments ); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/Interop/ConstructorInvoker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Bite.Runtime.Memory; 4 | 5 | namespace Bite.Runtime.Functions.Interop 6 | { 7 | 8 | public class ConstructorInvoker : IBiteVmCallable 9 | { 10 | private ConstructorInfo m_ConstructorInfo; 11 | private Type[] m_ArgTypes; 12 | 13 | 14 | public ConstructorInvoker( ConstructorInfo constructorInfo ) 15 | { 16 | m_ConstructorInfo = constructorInfo; 17 | ParameterInfo[] parameterInfos = m_ConstructorInfo.GetParameters(); 18 | m_ArgTypes = new Type[parameterInfos.Length]; 19 | 20 | for ( var i = 0; i < parameterInfos.Length; i++ ) 21 | { 22 | m_ArgTypes[i] = parameterInfos[i].ParameterType; 23 | } 24 | } 25 | 26 | public object Call( DynamicBiteVariable[] arguments ) 27 | { 28 | object[] constructorArgs = new object[arguments.Length]; 29 | 30 | for ( int i = 0; i < arguments.Length; i++ ) 31 | { 32 | constructorArgs[i] = Convert.ChangeType( 33 | arguments[i].ToObject(), 34 | m_ArgTypes[i] ); 35 | 36 | } 37 | 38 | object classObject = m_ConstructorInfo.Invoke( constructorArgs ); 39 | 40 | return classObject; 41 | } 42 | 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /Bite/Runtime/Functions/Interop/GenericMethodInvoker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Bite.Runtime.Functions.ForeignInterface; 4 | using Bite.Runtime.Memory; 5 | 6 | namespace Bite.Runtime.Functions.Interop 7 | { 8 | 9 | public class GenericMethodInvoker : IBiteVmCallable 10 | { 11 | private Type[] m_ArgTypes; 12 | 13 | private MethodInfo m_OriginalMethodInfo; 14 | 15 | public GenericMethodInvoker( MethodInfo methodInfo ) 16 | { 17 | OriginalMethodInfo = methodInfo; 18 | 19 | ParameterInfo[] parameterInfos = methodInfo.GetParameters(); 20 | m_ArgTypes = new Type[parameterInfos.Length]; 21 | 22 | for (var i = 0; i < parameterInfos.Length; i++) 23 | { 24 | m_ArgTypes[i] = parameterInfos[i].ParameterType; 25 | } 26 | } 27 | 28 | public MethodInfo OriginalMethodInfo 29 | { 30 | get => m_OriginalMethodInfo; 31 | private set => m_OriginalMethodInfo = value; 32 | } 33 | 34 | public object Call( DynamicBiteVariable[] arguments ) 35 | { 36 | object[] constructorArgs = new object[arguments.Length - 1]; 37 | 38 | for (int i = 1; i < arguments.Length; i++) 39 | { 40 | constructorArgs[i - 1] = Convert.ChangeType( 41 | arguments[i].ToObject(), 42 | m_ArgTypes[i - 1] ); 43 | 44 | } 45 | 46 | object value = OriginalMethodInfo.Invoke( arguments[0].ToObject(), constructorArgs ); 47 | 48 | return value; 49 | } 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/Interop/InteropGetConstructor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Bite.Runtime.Functions.ForeignInterface; 4 | using Bite.Runtime.Memory; 5 | 6 | namespace Bite.Runtime.Functions.Interop 7 | { 8 | 9 | public class InteropGetConstructor : InteropBase, IBiteVmCallable 10 | { 11 | 12 | #region Public 13 | 14 | public InteropGetConstructor() 15 | { 16 | } 17 | 18 | public InteropGetConstructor( TypeRegistry typeRegistry ) : base( typeRegistry ) 19 | { 20 | } 21 | 22 | public object Call( DynamicBiteVariable[] arguments ) 23 | { 24 | Type type = ResolveType( arguments[0].StringData ); 25 | 26 | if ( type == null ) 27 | { 28 | throw new BiteVmRuntimeException( 29 | $"Runtime Error: Type: {arguments[0].StringData} not registered as a type!" ); 30 | } 31 | 32 | Type[] constructorArgTypes = new Type[arguments.Length - 1]; 33 | 34 | int counter = 0; 35 | 36 | for ( int i = 1; i < arguments.Length; i++ ) 37 | { 38 | if ( arguments[i].DynamicType == DynamicVariableType.String ) 39 | { 40 | Type argType = ResolveType( arguments[i].StringData ); 41 | 42 | if ( argType == null ) 43 | { 44 | throw new BiteVmRuntimeException( 45 | $"Runtime Error: Type: {arguments[i].StringData} not registered as a type!" ); 46 | } 47 | 48 | constructorArgTypes[counter] = argType; 49 | 50 | } 51 | else 52 | { 53 | throw new BiteVmRuntimeException( "Expected string" ); 54 | } 55 | 56 | counter++; 57 | } 58 | 59 | ConstructorInfo constructorInfo = m_TypeRegistry.GetConstructor( type, constructorArgTypes ); 60 | 61 | return new ConstructorInvoker( constructorInfo ); 62 | } 63 | 64 | 65 | #endregion 66 | } 67 | 68 | } -------------------------------------------------------------------------------- /Bite/Runtime/Functions/Interop/InteropGetGenericMethod.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Bite.Runtime.Functions.ForeignInterface; 4 | using Bite.Runtime.Memory; 5 | 6 | namespace Bite.Runtime.Functions.Interop 7 | { 8 | 9 | public class InteropGetGenericMethod : InteropBase, IBiteVmCallable 10 | { 11 | public InteropGetGenericMethod() 12 | { 13 | } 14 | 15 | public InteropGetGenericMethod( TypeRegistry typeRegistry ) : base( typeRegistry ) 16 | { 17 | } 18 | 19 | public object Call( DynamicBiteVariable[] arguments ) 20 | { 21 | if ( arguments[0].ObjectData is MethodInfo methodInvoker ) 22 | { 23 | Type[] methodArgTypes = new Type[arguments.Length - 1]; 24 | 25 | int counter = 0; 26 | 27 | for (int i = 1; i < arguments.Length; i++) 28 | { 29 | if (arguments[i].DynamicType == DynamicVariableType.String) 30 | { 31 | Type argType = ResolveType( arguments[i].StringData ); 32 | 33 | if (argType == null) 34 | { 35 | throw new BiteVmRuntimeException( 36 | $"Runtime Error: Type: {arguments[i].StringData} not registered as a type!" ); 37 | } 38 | 39 | methodArgTypes[counter] = argType; 40 | 41 | } 42 | else 43 | { 44 | throw new BiteVmRuntimeException( "Expected string" ); 45 | } 46 | 47 | counter++; 48 | } 49 | 50 | MethodInfo genericMethod = methodInvoker.MakeGenericMethod( methodArgTypes ); 51 | 52 | GenericMethodInvoker genericMethodInvoker = new GenericMethodInvoker( genericMethod ); 53 | 54 | return genericMethodInvoker; 55 | } 56 | 57 | return null; 58 | } 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/Interop/InteropGetMethod.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Bite.Runtime.Functions.ForeignInterface; 4 | using Bite.Runtime.Memory; 5 | 6 | namespace Bite.Runtime.Functions.Interop 7 | { 8 | 9 | public class InteropGetMethod : InteropBase, IBiteVmCallable 10 | { 11 | 12 | #region Public 13 | 14 | public InteropGetMethod() 15 | { 16 | } 17 | 18 | public InteropGetMethod( TypeRegistry typeRegistry ) : base( typeRegistry ) 19 | { 20 | } 21 | 22 | public object Call( DynamicBiteVariable[] arguments ) 23 | { 24 | Type type = ResolveType( arguments[0].StringData ); 25 | 26 | if (type == null) 27 | { 28 | throw new BiteVmRuntimeException( 29 | $"Runtime Error: Type: {arguments[0].StringData} not registered as a type!" ); 30 | } 31 | 32 | Type[] methodArgTypes = new Type[arguments.Length - 2]; 33 | 34 | int counter = 0; 35 | 36 | for (int i = 2; i < arguments.Length; i++) 37 | { 38 | if (arguments[i].DynamicType == DynamicVariableType.String) 39 | { 40 | Type argType = ResolveType( arguments[i].StringData ); 41 | 42 | if (argType == null) 43 | { 44 | throw new BiteVmRuntimeException( 45 | $"Runtime Error: Type: {arguments[i].StringData} not registered as a type!" ); 46 | } 47 | 48 | methodArgTypes[counter] = argType; 49 | 50 | } 51 | else 52 | { 53 | throw new BiteVmRuntimeException( "Expected string" ); 54 | } 55 | 56 | counter++; 57 | } 58 | 59 | MethodInfo methodInfo = m_TypeRegistry.GetMethod( type, arguments[1].StringData, methodArgTypes ); 60 | 61 | if ( methodInfo.IsGenericMethod ) 62 | { 63 | return methodInfo; 64 | } 65 | else 66 | { 67 | return new MethodInvoker( methodInfo ); 68 | } 69 | 70 | } 71 | 72 | 73 | #endregion 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /Bite/Runtime/Functions/Interop/InteropGetStaticClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Bite.Runtime.Functions.ForeignInterface; 3 | using Bite.Runtime.Memory; 4 | 5 | namespace Bite.Runtime.Functions.Interop 6 | { 7 | 8 | public class InteropGetStaticClass : InteropBase, IBiteVmCallable 9 | { 10 | public InteropGetStaticClass() 11 | { 12 | } 13 | 14 | public InteropGetStaticClass( TypeRegistry typeRegistry ) : base( typeRegistry ) 15 | { 16 | } 17 | 18 | public object Call( DynamicBiteVariable[] arguments ) 19 | { 20 | Type type = ResolveType( arguments[0].StringData ); 21 | 22 | if ( type == null ) 23 | { 24 | throw new BiteVmRuntimeException( 25 | $"Runtime Error: Type: {arguments[0].StringData} not registered as a type!" ); 26 | } 27 | 28 | StaticWrapper wrapper = new StaticWrapper( type ); 29 | 30 | return wrapper; 31 | } 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /Bite/Runtime/Functions/Interop/InteropGetStaticMember.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Bite.Runtime.Functions.ForeignInterface; 4 | using Bite.Runtime.Memory; 5 | 6 | namespace Bite.Runtime.Functions.Interop 7 | { 8 | 9 | public class InteropGetStaticMember : InteropBase, IBiteVmCallable 10 | { 11 | public InteropGetStaticMember() 12 | { 13 | } 14 | 15 | public InteropGetStaticMember( TypeRegistry typeRegistry ) : base( typeRegistry ) 16 | { 17 | } 18 | 19 | public object Call( DynamicBiteVariable[] arguments ) 20 | { 21 | Type type = ResolveType( arguments[0].StringData ); 22 | 23 | if ( type == null ) 24 | { 25 | throw new BiteVmRuntimeException( 26 | $"Runtime Error: Type: {arguments[0].StringData} not registered as a type!" ); 27 | } 28 | 29 | MemberInfo[] memberInfo = 30 | type.GetMember( arguments[1].StringData, BindingFlags.Public | BindingFlags.Static ); 31 | 32 | if ( memberInfo.Length > 0 ) 33 | { 34 | object obj = GetValue( memberInfo[0], null ); 35 | 36 | return obj; 37 | } 38 | 39 | throw new BiteVmRuntimeException( 40 | $"Runtime Error: member {arguments[0].StringData} not found on type {arguments[0].StringData}" ); 41 | } 42 | 43 | private static object GetValue( MemberInfo memberInfo, object forObject ) 44 | { 45 | switch ( memberInfo.MemberType ) 46 | { 47 | case MemberTypes.Field: 48 | return ( ( FieldInfo ) memberInfo ).GetValue( forObject ); 49 | 50 | case MemberTypes.Property: 51 | return ( ( PropertyInfo ) memberInfo ).GetValue( forObject ); 52 | 53 | default: 54 | throw new NotImplementedException(); 55 | } 56 | } 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /Bite/Runtime/Functions/Interop/InteropGetStaticMethod.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Bite.Runtime.Functions.ForeignInterface; 4 | using Bite.Runtime.Memory; 5 | 6 | namespace Bite.Runtime.Functions.Interop 7 | { 8 | 9 | public class InteropGetStaticMethod : InteropBase, IBiteVmCallable 10 | { 11 | public InteropGetStaticMethod() 12 | { 13 | } 14 | 15 | public InteropGetStaticMethod( TypeRegistry typeRegistry ) : base( typeRegistry ) 16 | { 17 | } 18 | 19 | public object Call( DynamicBiteVariable[] arguments ) 20 | { 21 | Type type = ResolveType( arguments[0].StringData ); 22 | 23 | if ( type == null ) 24 | { 25 | throw new BiteVmRuntimeException( 26 | $"Runtime Error: Type: {arguments[0].StringData} not registered as a type!" ); 27 | } 28 | 29 | Type[] methodArgTypes = new Type[arguments.Length - 2]; 30 | 31 | int counter = 0; 32 | 33 | for (int i = 2; i < arguments.Length; i++) 34 | { 35 | if (arguments[i].DynamicType == DynamicVariableType.String) 36 | { 37 | Type argType = ResolveType( arguments[i].StringData ); 38 | 39 | if (argType == null) 40 | { 41 | throw new BiteVmRuntimeException( 42 | $"Runtime Error: Type: {arguments[i].StringData} not registered as a type!" ); 43 | } 44 | 45 | methodArgTypes[counter] = argType; 46 | 47 | } 48 | else 49 | { 50 | throw new BiteVmRuntimeException( "Expected string" ); 51 | } 52 | 53 | counter++; 54 | } 55 | 56 | MethodInfo methodInfo = m_TypeRegistry.GetMethod( type, arguments[1].StringData, methodArgTypes ); 57 | 58 | StaticMethodInvoker staticMethodInvoker = new StaticMethodInvoker( methodInfo ); 59 | 60 | return staticMethodInvoker; 61 | } 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/Interop/MethodInvoker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Bite.Runtime.Functions.ForeignInterface; 4 | using Bite.Runtime.Memory; 5 | 6 | namespace Bite.Runtime.Functions.Interop 7 | { 8 | 9 | public class MethodInvoker : IBiteVmCallable 10 | { 11 | private Type[] m_ArgTypes; 12 | private readonly FastMethodInfo m_FastMethodInfo; 13 | 14 | private MethodInfo m_OriginalMethodInfo; 15 | 16 | public MethodInvoker( MethodInfo methodInfo ) 17 | { 18 | OriginalMethodInfo = methodInfo; 19 | m_FastMethodInfo = new FastMethodInfo( methodInfo ); 20 | 21 | ParameterInfo[] parameterInfos = methodInfo.GetParameters(); 22 | m_ArgTypes = new Type[parameterInfos.Length]; 23 | 24 | for (var i = 0; i < parameterInfos.Length; i++) 25 | { 26 | m_ArgTypes[i] = parameterInfos[i].ParameterType; 27 | } 28 | } 29 | 30 | public MethodInfo OriginalMethodInfo 31 | { 32 | get => m_OriginalMethodInfo; 33 | private set => m_OriginalMethodInfo = value; 34 | } 35 | 36 | public object Call( DynamicBiteVariable[] arguments ) 37 | { 38 | object[] constructorArgs = new object[arguments.Length - 1]; 39 | 40 | for (int i = 1; i < arguments.Length; i++) 41 | { 42 | constructorArgs[i - 1] = Convert.ChangeType( 43 | arguments[i].ToObject(), 44 | m_ArgTypes[i - 1] ); 45 | 46 | } 47 | 48 | object value = m_FastMethodInfo.Invoke( arguments[0].ToObject(), constructorArgs ); 49 | 50 | return value; 51 | } 52 | 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /Bite/Runtime/Functions/Interop/StaticGenericMethodInvoker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Bite.Runtime.Functions.ForeignInterface; 4 | using Bite.Runtime.Memory; 5 | 6 | namespace Bite.Runtime.Functions.Interop 7 | { 8 | 9 | public class StaticGenericMethodInvoker : IBiteVmCallable 10 | { 11 | private Type[] m_ArgTypes; 12 | 13 | private MethodInfo m_OriginalMethodInfo; 14 | 15 | public StaticGenericMethodInvoker( MethodInfo methodInfo ) 16 | { 17 | OriginalMethodInfo = methodInfo; 18 | 19 | ParameterInfo[] parameterInfos = methodInfo.GetParameters(); 20 | m_ArgTypes = new Type[parameterInfos.Length]; 21 | 22 | for (var i = 0; i < parameterInfos.Length; i++) 23 | { 24 | m_ArgTypes[i] = parameterInfos[i].ParameterType; 25 | } 26 | } 27 | 28 | public MethodInfo OriginalMethodInfo 29 | { 30 | get => m_OriginalMethodInfo; 31 | private set => m_OriginalMethodInfo = value; 32 | } 33 | 34 | public object Call( DynamicBiteVariable[] arguments ) 35 | { 36 | object[] constructorArgs = new object[arguments.Length - 1]; 37 | 38 | for (int i = 0; i < arguments.Length; i++) 39 | { 40 | constructorArgs[i] = Convert.ChangeType( 41 | arguments[i].ToObject(), 42 | m_ArgTypes[i] ); 43 | 44 | } 45 | 46 | object value = OriginalMethodInfo.Invoke( null, constructorArgs ); 47 | 48 | return value; 49 | } 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/Interop/StaticMethodInvoker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Bite.Runtime.Functions.ForeignInterface; 4 | using Bite.Runtime.Memory; 5 | 6 | namespace Bite.Runtime.Functions.Interop 7 | { 8 | 9 | public class StaticMethodInvoker : IBiteVmCallable 10 | { 11 | private Type[] m_ArgTypes; 12 | private readonly FastMethodInfo m_FastMethodInfo; 13 | 14 | private MethodInfo m_OriginalMethodInfo; 15 | public StaticMethodInvoker( MethodInfo methodInfo ) 16 | { 17 | OriginalMethodInfo = methodInfo; 18 | m_FastMethodInfo = new FastMethodInfo( methodInfo ); 19 | 20 | ParameterInfo[] parameterInfos = methodInfo.GetParameters(); 21 | m_ArgTypes = new Type[parameterInfos.Length]; 22 | 23 | for (var i = 0; i < parameterInfos.Length; i++) 24 | { 25 | m_ArgTypes[i] = parameterInfos[i].ParameterType; 26 | } 27 | } 28 | 29 | public MethodInfo OriginalMethodInfo 30 | { 31 | get => m_OriginalMethodInfo; 32 | private set => m_OriginalMethodInfo = value; 33 | } 34 | 35 | public object Call( DynamicBiteVariable[] arguments ) 36 | { 37 | object[] constructorArgs = new object[arguments.Length]; 38 | 39 | for (int i = 0; i < arguments.Length; i++) 40 | { 41 | constructorArgs[i] = Convert.ChangeType( 42 | arguments[i].ToObject(), 43 | m_ArgTypes[i] ); 44 | 45 | } 46 | 47 | object value = m_FastMethodInfo.Invoke( null, constructorArgs ); 48 | 49 | return value; 50 | } 51 | 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/PrintFunctionVm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Bite.Runtime.Memory; 4 | 5 | namespace Bite.Runtime.Functions 6 | { 7 | 8 | public class PrintFunctionVm : IBiteVmCallable 9 | { 10 | #region Public 11 | 12 | public object Call( DynamicBiteVariable[] arguments ) 13 | { 14 | if ( arguments[0].DynamicType != DynamicVariableType.Null ) 15 | { 16 | int arraySize = arguments.Length; 17 | 18 | for ( int i = 0; i < arraySize; i++ ) 19 | { 20 | Console.Write( arguments[i].ToString() ); 21 | } 22 | } 23 | else 24 | { 25 | Console.WriteLine( "Error: Passed Null Reference to Function!" ); 26 | } 27 | 28 | return null; 29 | } 30 | 31 | #endregion 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Bite/Runtime/Functions/PrintLineFunctionVm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Bite.Runtime.Memory; 4 | 5 | namespace Bite.Runtime.Functions 6 | { 7 | 8 | public class PrintLineFunctionVm : IBiteVmCallable 9 | { 10 | #region Public 11 | 12 | public object Call( DynamicBiteVariable[] arguments ) 13 | { 14 | int argumentsCount = arguments.Length; 15 | 16 | for ( int i = 0; i < argumentsCount; i++ ) 17 | { 18 | Console.WriteLine( arguments[i].ToString() ); 19 | } 20 | 21 | return null; 22 | } 23 | 24 | #endregion 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Bite/Runtime/IntByteStruct.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.InteropServices; 2 | 3 | namespace Bite.Runtime 4 | { 5 | 6 | [StructLayout( LayoutKind.Explicit )] 7 | internal struct IntByteStruct 8 | { 9 | [FieldOffset( 0 )] 10 | public byte byte0; 11 | 12 | [FieldOffset( 1 )] 13 | public byte byte1; 14 | 15 | [FieldOffset( 2 )] 16 | public byte byte2; 17 | 18 | [FieldOffset( 3 )] 19 | public byte byte3; 20 | 21 | [FieldOffset( 0 )] 22 | public int integer; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Bite/Runtime/Memory/BiteMemorySpace.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Runtime.Memory 4 | { 5 | 6 | public class BiteMemorySpace 7 | { 8 | private Dictionary < string, DynamicBiteVariable > m_FunctionByName = 9 | new Dictionary < string, DynamicBiteVariable >(); 10 | 11 | private Dictionary < string, DynamicBiteVariable > m_ClassInstanceByName = 12 | new Dictionary < string, DynamicBiteVariable >(); 13 | 14 | 15 | private DynamicBiteVariable[] m_Properties; 16 | 17 | public BiteMemorySpace(int size) 18 | { 19 | m_Properties = new DynamicBiteVariable[size]; 20 | } 21 | 22 | public void DefineVariable( int i, DynamicBiteVariable dynamicBiteVariable ) 23 | { 24 | 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Bite/Runtime/Memory/ConstantValueType.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Runtime.Memory 2 | { 3 | 4 | public enum ConstantValueType : byte 5 | { 6 | Integer, 7 | Double, 8 | String, 9 | Null, 10 | Bool 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Bite/Runtime/Memory/DynamicVariableType.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Runtime.Memory 2 | { 3 | 4 | public enum DynamicVariableType : uint 5 | { 6 | Null = 0xfff80001, // Use NaN boxing 7 | True = 0xfff80002, 8 | False = 0xfff80003, 9 | String = 0xfff80004, 10 | Array = 0xfff80005, 11 | Object = 0xfff80006 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Bite/Runtime/Memory/FastClassMemorySpace.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Bite.Runtime.Bytecode; 3 | 4 | namespace Bite.Runtime.Memory 5 | { 6 | 7 | public class FastClassMemorySpace : FastMemorySpace 8 | { 9 | #region Public 10 | 11 | public FastClassMemorySpace( 12 | string name, 13 | FastMemorySpace enclosingSpace, 14 | int stackCount, 15 | BinaryChunk callerChunk, 16 | int callerInstructionPointer, 17 | int callerLineNumberPointer, 18 | int memberCount ) : base( 19 | $"$class_{name}", 20 | enclosingSpace, 21 | stackCount, 22 | callerChunk, 23 | callerInstructionPointer, 24 | callerLineNumberPointer, 25 | memberCount ) 26 | { 27 | } 28 | 29 | #endregion 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Bite/Runtime/Memory/FastGlobalMemorySpace.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Bite.Runtime.Bytecode; 3 | 4 | namespace Bite.Runtime.Memory 5 | { 6 | 7 | public class FastGlobalMemorySpace : FastMemorySpace 8 | { 9 | private readonly List < FastMemorySpace > m_Modules = new List < FastMemorySpace >(); 10 | 11 | #region Public 12 | 13 | public FastGlobalMemorySpace( int memberCount ) : base( "Global", null, 0, new BinaryChunk(), 0, 0, memberCount ) 14 | { 15 | } 16 | 17 | //public List Modules 18 | //{ 19 | // get => m_Modules; 20 | // set => m_Modules = value; 21 | //} 22 | 23 | public void AddModule( FastMemorySpace memorySpace ) 24 | { 25 | m_Modules.Add( memorySpace ); 26 | } 27 | 28 | public override DynamicBiteVariable Get( string idStr, bool calledFromGlobalMemorySpace = false ) 29 | { 30 | foreach ( FastMemorySpace fastMemorySpace in m_Modules ) 31 | { 32 | if ( fastMemorySpace.Exist( idStr, true ) ) 33 | { 34 | return fastMemorySpace.Get( idStr, true ); 35 | } 36 | } 37 | 38 | return DynamicVariableExtension.ToDynamicVariable(); 39 | } 40 | 41 | public FastMemorySpace GetModule( int index ) 42 | { 43 | return m_Modules[index]; 44 | } 45 | 46 | public FastMemorySpace GetModule( string moduleName ) 47 | { 48 | for ( int i = 0; i < m_Modules.Count; i++ ) 49 | { 50 | if ( m_Modules[i].Name == moduleName ) 51 | { 52 | return m_Modules[i]; 53 | } 54 | } 55 | 56 | return null; 57 | } 58 | 59 | #endregion 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Bite/Runtime/Memory/FastModuleMemorySpace.cs: -------------------------------------------------------------------------------- 1 | using Bite.Runtime.Bytecode; 2 | 3 | namespace Bite.Runtime.Memory 4 | { 5 | 6 | public class FastModuleMemorySpace : FastMemorySpace 7 | { 8 | #region Public 9 | 10 | public FastModuleMemorySpace( 11 | string name, 12 | FastMemorySpace enclosingSpace, 13 | int stackCount, 14 | BinaryChunk callerChunk, 15 | int callerInstructionPointer, 16 | int callerLineNumberPointer, 17 | int memberCount ) : base( name, enclosingSpace, stackCount, callerChunk, callerInstructionPointer, callerLineNumberPointer, memberCount ) 18 | { 19 | } 20 | 21 | #endregion 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Bite/Runtime/Memory/ObjectPoolFastMemory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bite.Runtime.Memory 4 | { 5 | 6 | public class ObjectPoolFastMemory 7 | { 8 | private FastMemorySpace[] m_FastCallMemorySpaces = new FastMemorySpace[128]; 9 | private int m_FastMemorySpacePointer = 0; 10 | 11 | #region Public 12 | 13 | public ObjectPoolFastMemory() 14 | { 15 | for ( int i = 0; i < 128; i++ ) 16 | { 17 | m_FastCallMemorySpaces[i] = new FastMemorySpace( $"$objectpool_{i}", null, 0, null, 0, 0, 0 ); 18 | } 19 | } 20 | 21 | public FastMemorySpace Get() 22 | { 23 | if ( m_FastMemorySpacePointer >= m_FastCallMemorySpaces.Length ) 24 | { 25 | FastMemorySpace[] newProperties = new FastMemorySpace[m_FastCallMemorySpaces.Length * 2]; 26 | Array.Copy( m_FastCallMemorySpaces, newProperties, m_FastCallMemorySpaces.Length ); 27 | 28 | for ( int i = m_FastMemorySpacePointer; i < newProperties.Length; i++ ) 29 | { 30 | newProperties[i] = new FastMemorySpace( $"$objectpool_{i}", null, 0, null, 0, 0,0 ); 31 | } 32 | 33 | m_FastCallMemorySpaces = newProperties; 34 | } 35 | 36 | FastMemorySpace fastMemorySpace = m_FastCallMemorySpaces[m_FastMemorySpacePointer]; 37 | fastMemorySpace.Properties = Array.Empty < DynamicBiteVariable >(); 38 | fastMemorySpace.CurrentMemoryPointer = 0; 39 | fastMemorySpace.NamesToProperties.Clear(); 40 | fastMemorySpace.CallerChunk = null; 41 | fastMemorySpace.CallerIntructionPointer = 0; 42 | fastMemorySpace.CallerLineNumberPointer = 0; 43 | fastMemorySpace.StackCountAtBegin = 0; 44 | fastMemorySpace.IsRunningCallback = false; 45 | 46 | m_FastMemorySpacePointer++; 47 | 48 | return fastMemorySpace; 49 | } 50 | 51 | public void Return( FastMemorySpace item ) 52 | { 53 | if ( item == m_FastCallMemorySpaces[m_FastMemorySpacePointer - 1] ) 54 | { 55 | m_FastMemorySpacePointer--; 56 | } 57 | } 58 | 59 | #endregion 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Bite/Runtime/MethodCache.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using Bite.Runtime.Functions.ForeignInterface; 5 | 6 | namespace Bite.Runtime 7 | { 8 | 9 | public class MethodCache 10 | { 11 | private static readonly Dictionary < string, FastMethodInfo > m_MethodCache = 12 | new Dictionary < string, FastMethodInfo >(); 13 | 14 | #region Public 15 | 16 | public bool TryGetMethod( 17 | Type type, 18 | Type[] functionArgumentTypes, 19 | string methodName, 20 | out FastMethodInfo fastMethodInfo ) 21 | { 22 | string key = $"{type.FullName}.{methodName}"; 23 | 24 | for ( int i = 0; i < functionArgumentTypes.Length; i++ ) 25 | { 26 | key += "." + functionArgumentTypes[i].Name; 27 | } 28 | 29 | if ( !m_MethodCache.TryGetValue( key, out fastMethodInfo ) ) 30 | { 31 | MethodInfo methodInfo = type.GetMethod( methodName, functionArgumentTypes ); 32 | 33 | if ( methodInfo != null ) 34 | { 35 | fastMethodInfo = new FastMethodInfo( methodInfo ); 36 | m_MethodCache.Add( key, fastMethodInfo ); 37 | 38 | return true; 39 | } 40 | 41 | return false; 42 | } 43 | 44 | return true; 45 | } 46 | 47 | #endregion 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Bite/Runtime/Module.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Runtime 4 | { 5 | 6 | public class Module 7 | { 8 | public bool MainModule { get; set; } 9 | 10 | public string Name { get; set; } 11 | 12 | public IReadOnlyCollection < string > Imports { get; set; } 13 | 14 | public string Code { get; set; } 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Bite/Runtime/PropertyCache.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | 5 | namespace Bite.Runtime 6 | { 7 | 8 | public class PropertyCache 9 | { 10 | private static readonly Dictionary < string, PropertyInfo > m_PropertyCache = 11 | new Dictionary < string, PropertyInfo >(); 12 | 13 | #region Public 14 | 15 | public bool TryGetProperty( Type type, string propertyName, out PropertyInfo propertyInfo ) 16 | { 17 | string key = $"{type.FullName}.{propertyName}"; 18 | 19 | if ( !m_PropertyCache.TryGetValue( key, out propertyInfo ) ) 20 | { 21 | propertyInfo = type.GetProperty( propertyName ); 22 | 23 | if ( propertyInfo != null ) 24 | { 25 | m_PropertyCache.Add( key, propertyInfo ); 26 | 27 | return true; 28 | } 29 | 30 | return false; 31 | } 32 | 33 | return true; 34 | } 35 | 36 | #endregion 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Bite/Runtime/StackExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Bite.Runtime.Memory; 3 | 4 | namespace Bite.Runtime 5 | { 6 | 7 | public static class StackExtensions 8 | { 9 | #region Public 10 | 11 | /// 12 | /// Retrieves the current value on the stack based on the specified type 13 | /// 14 | /// 15 | /// 16 | /// 17 | public static object PopDataByType( this DynamicBiteVariableStack vmStack, Type type ) 18 | { 19 | object data; 20 | 21 | DynamicBiteVariable currentStack = vmStack.Peek(); 22 | 23 | // Cast the data based on the recieving propertyType 24 | if ( type == typeof( double ) && currentStack.IsNumeric() ) 25 | { 26 | data = vmStack.Pop().NumberData; 27 | } 28 | else if ( type == typeof( float ) && currentStack.IsNumeric() ) 29 | { 30 | data = ( float ) vmStack.Pop().NumberData; 31 | } 32 | else if ( type == typeof( int ) && currentStack.IsNumeric() ) 33 | { 34 | data = ( int ) vmStack.Pop().NumberData; 35 | } 36 | else if ( type == typeof( string ) && currentStack.DynamicType == DynamicVariableType.String ) 37 | { 38 | data = vmStack.Pop().StringData; 39 | } 40 | else if ( type == typeof( bool ) && currentStack.IsBoolean() ) 41 | { 42 | data = currentStack.DynamicType == DynamicVariableType.True; 43 | } 44 | else 45 | { 46 | data = vmStack.Pop().ObjectData; 47 | } 48 | 49 | return data; 50 | } 51 | 52 | #endregion 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Bite/Runtime/UsingStatementStack.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bite.Runtime 4 | { 5 | 6 | public class UsingStatementStack 7 | { 8 | private object[] m_UsedObjects = new object[128]; 9 | 10 | public int Count { get; set; } = 0; 11 | 12 | #region Public 13 | 14 | public void Pop() 15 | { 16 | object usedObject = m_UsedObjects[--Count]; 17 | ( ( IDisposable ) usedObject ).Dispose(); 18 | } 19 | 20 | public void Push( object usedObject ) 21 | { 22 | if ( Count >= m_UsedObjects.Length ) 23 | { 24 | object[] newProperties = new object[m_UsedObjects.Length * 2]; 25 | Array.Copy( m_UsedObjects, newProperties, m_UsedObjects.Length ); 26 | m_UsedObjects = newProperties; 27 | } 28 | 29 | m_UsedObjects[Count] = usedObject; 30 | 31 | if ( Count >= 1023 ) 32 | { 33 | throw new IndexOutOfRangeException( "Using Statement Overflow" ); 34 | } 35 | 36 | Count++; 37 | } 38 | 39 | #endregion 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Bite/Symbols/AccesModifierType.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public enum AccesModifierType 5 | { 6 | Private = 0, 7 | Public, 8 | None 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Bite/Symbols/BaseSymbol.cs: -------------------------------------------------------------------------------- 1 | using Bite.Ast; 2 | 3 | namespace Bite.Symbols 4 | { 5 | 6 | public abstract class BaseSymbol : Symbol 7 | { 8 | public virtual string Name => name; 9 | 10 | public virtual Scope SymbolScope 11 | { 12 | get => scope; 13 | set => scope = value; 14 | } 15 | 16 | public virtual Type Type 17 | { 18 | get => type; 19 | set => type = value; 20 | } 21 | 22 | public virtual AstBaseNode DefBaseNode 23 | { 24 | set => m_DefBaseNode = value; 25 | get => m_DefBaseNode; 26 | } 27 | 28 | public virtual int InsertionOrderNumber 29 | { 30 | get => lexicalOrder; 31 | set => lexicalOrder = value; 32 | } 33 | 34 | #region Public 35 | 36 | public BaseSymbol( string name ) 37 | { 38 | this.name = name; 39 | } 40 | 41 | public override bool Equals( object obj ) 42 | { 43 | if ( !( obj is Symbol ) ) 44 | { 45 | return false; 46 | } 47 | 48 | if ( obj == this ) 49 | { 50 | return true; 51 | } 52 | 53 | return name.Equals( ( ( Symbol ) obj ).Name ); 54 | } 55 | 56 | /*public override int GetHashCode() 57 | { 58 | return name.GetHashCode(); 59 | }*/ 60 | 61 | public override string ToString() 62 | { 63 | string s = ""; 64 | 65 | if ( scope != null ) 66 | { 67 | s = scope.Name + "."; 68 | } 69 | 70 | if ( type != null ) 71 | { 72 | string ts = type.ToString(); 73 | 74 | if ( type is SymbolWithScope ) 75 | { 76 | ts = ( ( SymbolWithScope ) type ).getFullyQualifiedName( "." ); 77 | } 78 | 79 | return '<' + s + Name + ":" + ts + '>'; 80 | } 81 | 82 | return s + Name; 83 | } 84 | 85 | #endregion 86 | 87 | protected internal int lexicalOrder; 88 | 89 | protected internal AstBaseNode m_DefBaseNode; 90 | protected internal readonly string name; 91 | protected internal Scope scope; 92 | protected internal Type type; 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /Bite/Symbols/BiteClassType.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Symbols 4 | { 5 | 6 | public class BiteClassType : Type 7 | { 8 | private static readonly List < string > s_BiteClassTypes = new List < string >(); 9 | private static int s_ClassTypeIndex = 0; 10 | 11 | public string Name { get; } 12 | 13 | public int TypeIndex => s_ClassTypeIndex; 14 | 15 | #region Public 16 | 17 | public BiteClassType( string typeName ) 18 | { 19 | Name = typeName; 20 | 21 | if ( s_BiteClassTypes.Contains( typeName ) ) 22 | { 23 | s_ClassTypeIndex = s_BiteClassTypes.FindIndex( s => s == typeName ); 24 | } 25 | else 26 | { 27 | s_ClassTypeIndex = s_BiteClassTypes.Count; 28 | s_BiteClassTypes.Add( typeName ); 29 | } 30 | } 31 | 32 | public override string ToString() 33 | { 34 | return $" Type: {Name}"; 35 | } 36 | 37 | #endregion 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Bite/Symbols/ClassAndMemberModifiers.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public enum ClassAndMemberModifiers 5 | { 6 | Abstract, 7 | Static, 8 | None 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Bite/Symbols/DynamicVariable.cs: -------------------------------------------------------------------------------- 1 | using Bite.Ast; 2 | 3 | namespace Bite.Symbols 4 | { 5 | 6 | public class DynamicVariable : SymbolWithScope, TypedSymbol 7 | { 8 | public AstBaseNode DefinitionBaseNode = null; 9 | 10 | public ClassAndMemberModifiers ClassAndMemberModifiers => m_ClassAndMemberModifier; 11 | 12 | public AccesModifierType AccesModifier => m_AccessModifier; 13 | 14 | public int TypeIndex => 0; 15 | 16 | public Type Type { get; set; } 17 | 18 | #region Public 19 | 20 | public DynamicVariable( 21 | string name, 22 | AccesModifierType accesModifierType, 23 | ClassAndMemberModifiers classAndMemberModifiers ) : base( name ) 24 | { 25 | m_AccessModifier = accesModifierType; 26 | m_ClassAndMemberModifier = classAndMemberModifiers; 27 | } 28 | 29 | #endregion 30 | 31 | protected internal AccesModifierType m_AccessModifier; 32 | protected internal ClassAndMemberModifiers m_ClassAndMemberModifier; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Bite/Symbols/FieldSymbol.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public class FieldSymbol : DynamicVariable, MemberSymbol 5 | { 6 | public virtual int SlotNumber => slot; 7 | 8 | #region Public 9 | 10 | public FieldSymbol( 11 | string name, 12 | AccesModifierType accesModifierType, 13 | ClassAndMemberModifiers classAndMemberModifiers ) : base( name, accesModifierType, classAndMemberModifiers ) 14 | { 15 | } 16 | 17 | #endregion 18 | 19 | protected internal int slot; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Bite/Symbols/FunctionSymbol.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Bite.Ast; 3 | 4 | namespace Bite.Symbols 5 | { 6 | 7 | public class FunctionSymbol : SymbolWithScope, TypedSymbol 8 | { 9 | public string LinkName; 10 | 11 | public virtual FunctionDeclarationBaseNode DefBaseNode 12 | { 13 | set => m_DefBaseNode = value; 14 | get => m_DefBaseNode; 15 | } 16 | 17 | public virtual Type Type 18 | { 19 | get => retType; 20 | set => retType = value; 21 | } 22 | 23 | public virtual int NumberOfParameters 24 | { 25 | get 26 | { 27 | int i = 0; 28 | 29 | foreach ( KeyValuePair < string, Symbol > sym in symbols ) 30 | { 31 | if ( sym.Value is ParameterSymbol ) 32 | { 33 | i++; 34 | } 35 | } 36 | 37 | return i; 38 | } 39 | } 40 | 41 | public ClassAndMemberModifiers ClassAndMemberModifiers => m_ClassAndMemberModifier; 42 | 43 | public AccesModifierType AccesModifier => m_AccessModifier; 44 | 45 | public bool IsExtern => m_IsExtern; 46 | 47 | public bool IsCallable => m_IsCallable; 48 | 49 | #region Public 50 | 51 | public FunctionSymbol( 52 | string name, 53 | AccesModifierType accesModifierType, 54 | ClassAndMemberModifiers classAndMemberModifiers, 55 | bool isExtern, 56 | bool isCallable ) : base( name ) 57 | { 58 | m_AccessModifier = accesModifierType; 59 | m_ClassAndMemberModifier = classAndMemberModifiers; 60 | m_IsExtern = isExtern; 61 | m_IsCallable = isCallable; 62 | } 63 | 64 | public override string ToString() 65 | { 66 | return name + ":" + base.ToString(); 67 | } 68 | 69 | #endregion 70 | 71 | protected internal AccesModifierType m_AccessModifier; 72 | protected internal ClassAndMemberModifiers m_ClassAndMemberModifier; 73 | 74 | protected internal FunctionDeclarationBaseNode m_DefBaseNode; 75 | protected internal bool m_IsCallable; 76 | protected internal bool m_IsExtern; 77 | protected internal Type retType; 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /Bite/Symbols/GlobalScope.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public class GlobalScope : BaseScope 5 | { 6 | public override string Name => "global"; 7 | 8 | #region Public 9 | 10 | public GlobalScope() : base( null ) 11 | { 12 | } 13 | 14 | public void DefineModule( ModuleSymbol moduleSymbol ) 15 | { 16 | moduleSymbol.EnclosingScope = this; 17 | define( moduleSymbol ); 18 | } 19 | 20 | public void DefineVariable( VariableSymbol variableSymbol ) 21 | { 22 | define( variableSymbol ); 23 | } 24 | 25 | #endregion 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Bite/Symbols/GlobalSymbolTable.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Symbols 4 | { 5 | 6 | public class GlobalSymbolTable 7 | { 8 | /*private static Dictionary < Symbol, int > GlobalSymbols = new Dictionary < Symbol, int >(); 9 | private static int m_Counter = 0; 10 | 11 | public static void Define( Symbol symbol ) 12 | { 13 | GlobalSymbols.Add( symbol, m_Counter ); 14 | m_Counter++; 15 | } 16 | 17 | public static int GetIndex( Symbol symbol ) 18 | { 19 | return GlobalSymbols[symbol]; 20 | }*/ 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Bite/Symbols/LocalScope.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public class LocalScope : BaseScope 5 | { 6 | public override string Name => "local"; 7 | 8 | #region Public 9 | 10 | public LocalScope( Scope enclosingScope ) : base( enclosingScope ) 11 | { 12 | } 13 | 14 | #endregion 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Bite/Symbols/MemberSymbol.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public interface MemberSymbol : Symbol 5 | { 6 | AccesModifierType AccesModifier { get; } 7 | 8 | ClassAndMemberModifiers ClassAndMemberModifiers { get; } 9 | 10 | int SlotNumber { get; } 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Bite/Symbols/MethodSymbol.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public class MethodSymbol : FunctionSymbol, MemberSymbol 5 | { 6 | public bool IsConstructor; 7 | 8 | public virtual int SlotNumber => slot; 9 | 10 | #region Public 11 | 12 | // TODO: Allow extern on methods? 13 | public MethodSymbol( 14 | string name, 15 | AccesModifierType accessModifier, 16 | ClassAndMemberModifiers classAndMemberModifiers ) : base( 17 | name, 18 | accessModifier, 19 | classAndMemberModifiers, 20 | false, 21 | false ) 22 | { 23 | } 24 | 25 | #endregion 26 | 27 | protected internal int slot = -1; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Bite/Symbols/ModuleBuilder.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using Bite.Ast; 4 | 5 | namespace Bite.Symbols 6 | { 7 | 8 | /// 9 | /// Contains helper methods for building module members 10 | /// 11 | public abstract class ModuleBuilder 12 | { 13 | #region Protected 14 | 15 | protected FunctionSymbol CreateFunction( 16 | string name, 17 | AccesModifierType accesModifier, 18 | ClassAndMemberModifiers memberModifiers, 19 | bool isExtern, 20 | bool isCallable, 21 | string type, 22 | IReadOnlyCollection < string > parameters ) 23 | { 24 | FunctionSymbol functionSymbol = new FunctionSymbol( 25 | name, 26 | accesModifier, 27 | memberModifiers, 28 | isExtern, 29 | isCallable 30 | ) 31 | { 32 | Type = new BiteClassType( type ), 33 | m_DefBaseNode = new FunctionDeclarationBaseNode 34 | { 35 | FunctionId = new Identifier( name ), 36 | ParametersBase = new ParametersBaseNode 37 | { 38 | Identifiers = parameters.Select( p => new Identifier( p ) ).ToList() 39 | } 40 | } 41 | }; 42 | 43 | foreach ( string parameter in parameters ) 44 | { 45 | functionSymbol.define( new ParameterSymbol( parameter ) ); 46 | } 47 | 48 | return functionSymbol; 49 | } 50 | 51 | protected FieldSymbol CreatePublicField( string name, string type ) 52 | { 53 | return new FieldSymbol( 54 | name, 55 | AccesModifierType.Public, 56 | ClassAndMemberModifiers.None ) { Type = new BiteClassType( type ), DefinitionBaseNode = null }; 57 | } 58 | 59 | protected FieldSymbol CreatePublicField( string name, string type, AstBaseNode declaringType ) 60 | { 61 | return new FieldSymbol( 62 | name, 63 | AccesModifierType.Public, 64 | ClassAndMemberModifiers.None ) { Type = new BiteClassType( type ), DefinitionBaseNode = declaringType }; 65 | } 66 | 67 | #endregion 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /Bite/Symbols/ParameterSymbol.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public class ParameterSymbol : DynamicVariable 5 | { 6 | #region Public 7 | 8 | public ParameterSymbol( string name ) : base( name, AccesModifierType.None, ClassAndMemberModifiers.None ) 9 | { 10 | } 11 | 12 | #endregion 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Bite/Symbols/ParametersSymbol.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Symbols 4 | { 5 | 6 | public class ParametersSymbol : BaseSymbol 7 | { 8 | public List < ParameterSymbol > ParameterSymbols; 9 | 10 | #region Public 11 | 12 | public ParametersSymbol( string name ) : base( name ) 13 | { 14 | } 15 | 16 | #endregion 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Bite/Symbols/Scope.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Symbols 4 | { 5 | 6 | public interface Scope 7 | { 8 | IList < Symbol > AllSymbols { get; } 9 | 10 | void define( Symbol sym ); 11 | 12 | IList < Scope > EnclosingPathToRoot { get; } 13 | 14 | Scope EnclosingScope { get; set; } 15 | 16 | string Name { get; } 17 | 18 | void nest( Scope scope ); 19 | 20 | Symbol resolve( string name, out int moduleId, ref int depth, bool throwErrorWhenNotFound = true ); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Bite/Symbols/StringTable.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Bite.Symbols 4 | { 5 | 6 | public class StringTable 7 | { 8 | public virtual int NumberOfStrings => index + 1; 9 | 10 | #region Public 11 | 12 | public virtual int add( string s ) 13 | { 14 | if ( table.ContainsKey( s ) ) 15 | { 16 | return table[s]; 17 | } 18 | 19 | index++; 20 | table.Add( s, index ); 21 | strings.Add( s ); 22 | 23 | return index; 24 | } 25 | 26 | public virtual string get( int i ) 27 | { 28 | if ( i < size() && i >= 0 ) 29 | { 30 | return strings[i]; 31 | } 32 | 33 | return null; 34 | } 35 | 36 | public virtual int size() 37 | { 38 | return table.Count; 39 | } 40 | 41 | public virtual string[] toArray() 42 | { 43 | return ( ( List < string > ) strings ).ToArray(); 44 | } 45 | 46 | public virtual IList < string > toList() 47 | { 48 | return strings; 49 | } 50 | 51 | public override string ToString() 52 | { 53 | return table.ToString(); 54 | } 55 | 56 | #endregion 57 | 58 | protected internal int index = -1; 59 | protected internal IList < string > strings = new List < string >(); 60 | protected internal IDictionary < string, int > table = new Dictionary < string, int >(); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /Bite/Symbols/StructSymbol.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public class StructSymbol : DataAggregateSymbol 5 | { 6 | #region Public 7 | 8 | public StructSymbol( string name, AccesModifierType accesModifierType, ClassAndMemberModifiers structModifiers ) : 9 | base( name, accesModifierType, structModifiers ) 10 | { 11 | } 12 | 13 | #endregion 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Bite/Symbols/Symbol.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public interface Symbol 5 | { 6 | bool Equals( object o ); 7 | 8 | //int GetHashCode(); 9 | 10 | int InsertionOrderNumber { get; set; } 11 | 12 | string Name { get; } 13 | 14 | Scope SymbolScope { get; set; } 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Bite/Symbols/SymbolTable.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public class SymbolTable 5 | { 6 | public GlobalScope RootScope { get; } 7 | 8 | internal Scope CurrentScope { get; private set; } 9 | 10 | #region Public 11 | 12 | public SymbolTable() 13 | { 14 | RootScope = new GlobalScope(); 15 | CurrentScope = RootScope; 16 | } 17 | 18 | /// 19 | /// Defines the specified module on the Global scope 20 | /// 21 | /// 22 | public SymbolTable WithModule( ModuleSymbol module ) 23 | { 24 | RootScope.DefineModule( module ); 25 | 26 | return this; 27 | } 28 | 29 | internal void PopScope() 30 | { 31 | CurrentScope = CurrentScope.EnclosingScope; 32 | } 33 | 34 | internal void PushScope( Scope s ) 35 | { 36 | CurrentScope = s; 37 | } 38 | 39 | #endregion 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Bite/Symbols/SymbolWithScope.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Diagnostics; 3 | using System.Text; 4 | 5 | namespace Bite.Symbols 6 | { 7 | 8 | [DebuggerDisplay( "{Name}" )] 9 | public abstract class SymbolWithScope : BaseScope, Symbol, Scope 10 | { 11 | public override string Name => name; 12 | 13 | public virtual Scope SymbolScope 14 | { 15 | get => enclosingScope; 16 | set => EnclosingScope = value; 17 | } 18 | 19 | public bool IsExternal { get; } 20 | 21 | public override Scope EnclosingScope => enclosingScope; 22 | 23 | public virtual string QualifiedName => enclosingScope.Name + "." + name; 24 | 25 | public virtual int InsertionOrderNumber 26 | { 27 | get => index; 28 | set => index = value; 29 | } 30 | 31 | public override int NumberOfSymbols => symbols.Count; 32 | 33 | #region Public 34 | 35 | public SymbolWithScope( string name ) 36 | { 37 | this.name = name; 38 | } 39 | 40 | public override bool Equals( object obj ) 41 | { 42 | if ( !( obj is Symbol ) ) 43 | { 44 | return false; 45 | } 46 | 47 | if ( obj == this ) 48 | { 49 | return true; 50 | } 51 | 52 | return name.Equals( ( ( Symbol ) obj ).Name ); 53 | } 54 | 55 | public virtual string getFullyQualifiedName( string scopePathSeparator ) 56 | { 57 | List < Scope > path = new List < Scope >( EnclosingPathToRoot ); 58 | path.Reverse(); 59 | 60 | if ( path.Count == 0 ) 61 | { 62 | return ""; 63 | } 64 | 65 | StringBuilder buf = new StringBuilder(); 66 | buf.Append( path[0].Name ); 67 | 68 | for ( int i = 1; i < path.Count; i++ ) 69 | { 70 | Scope s = path[i]; 71 | buf.Append( scopePathSeparator ); 72 | buf.Append( s.Name ); 73 | } 74 | 75 | return buf.ToString(); 76 | } 77 | 78 | /*public override int GetHashCode() 79 | { 80 | return name.GetHashCode(); 81 | }*/ 82 | 83 | public virtual string getQualifiedName( string scopePathSeparator ) 84 | { 85 | return enclosingScope.Name + scopePathSeparator + name; 86 | } 87 | 88 | #endregion 89 | 90 | protected internal int index; 91 | protected internal readonly string name; 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /Bite/Symbols/Type.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public interface Type 5 | { 6 | string Name { get; } 7 | 8 | int TypeIndex { get; } 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Bite/Symbols/TypedSymbol.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public interface TypedSymbol 5 | { 6 | Type Type { get; set; } 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /Bite/Symbols/VariableSymbol.cs: -------------------------------------------------------------------------------- 1 | namespace Bite.Symbols 2 | { 3 | 4 | public class VariableSymbol : BaseSymbol, TypedSymbol 5 | { 6 | public ClassAndMemberModifiers ClassAndMemberModifiers => m_ClassAndMemberModifier; 7 | 8 | public AccesModifierType AccesModifier => m_AccessModifier; 9 | 10 | #region Public 11 | 12 | public VariableSymbol( 13 | string name, 14 | AccesModifierType accesModifierType, 15 | ClassAndMemberModifiers classAndMemberModifiers ) : base( name ) 16 | { 17 | m_AccessModifier = accesModifierType; 18 | m_ClassAndMemberModifier = classAndMemberModifiers; 19 | } 20 | 21 | #endregion 22 | 23 | protected internal AccesModifierType m_AccessModifier; 24 | protected internal ClassAndMemberModifiers m_ClassAndMemberModifier; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /FSharpTest/AssemblyInfo.fs: -------------------------------------------------------------------------------- 1 | namespace FSharpTest.AssemblyInfo 2 | 3 | open System.Reflection 4 | open System.Runtime.CompilerServices 5 | open System.Runtime.InteropServices 6 | 7 | // General Information about an assembly is controlled through the following 8 | // set of attributes. Change these attribute values to modify the information 9 | // associated with an assembly. 10 | [] 11 | [] 12 | [] 13 | [] 14 | [] 15 | [] 16 | [] 17 | [] 18 | 19 | // Setting ComVisible to false makes the types in this assembly not visible 20 | // to COM components. If you need to access a type in this assembly from 21 | // COM, set the ComVisible attribute to true on that type. 22 | [] 23 | 24 | // The following GUID is for the ID of the typelib if this project is exposed to COM 25 | [] 26 | 27 | // Version information for an assembly consists of the following four values: 28 | // 29 | // Major Version 30 | // Minor Version 31 | // Build Number 32 | // Revision 33 | // 34 | // You can specify all the values or you can default the Build and Revision Numbers 35 | // by using the '*' as shown below: 36 | // [] 37 | [] 38 | [] 39 | 40 | do 41 | () -------------------------------------------------------------------------------- /FSharpTest/FSharpTest.fsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | Debug 7 | AnyCPU 8 | {69E5A329-0D7E-4C21-A44D-6B2DA04D3A98} 9 | Library 10 | FSharpTest 11 | FSharpTest 12 | v4.6.2 13 | true 14 | 15 | 16 | true 17 | portable 18 | false 19 | false 20 | bin\$(Configuration)\ 21 | DEBUG;TRACE 22 | 3 23 | --warnon:1182 24 | 25 | 26 | pdbonly 27 | true 28 | true 29 | bin\$(Configuration)\ 30 | TRACE 31 | 3 32 | --warnon:1182 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | ..\packages\FSharp.Core.4.5.2\lib\net45\FSharp.Core.dll 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /FSharpTest/Library.fs: -------------------------------------------------------------------------------- 1 | namespace FSharpTest 2 | 3 | type Line = class 4 | val X1 : float 5 | val Y1 : float 6 | val X2 : float 7 | val Y2 : float 8 | 9 | new (x1, y1, x2, y2) as this = 10 | { X1 = x1; Y1 = y1; X2 = x2; Y2 = y2;} 11 | then 12 | printfn " Creating Line: {(%g, %g), (%g, %g)}\nLength: %g" 13 | this.X1 this.Y1 this.X2 this.Y2 this.Length 14 | 15 | member x.Length = 16 | let sqr x = x * x 17 | sqrt(sqr(x.X1 - x.X2) + sqr(x.Y1 - x.Y2) ) 18 | end 19 | -------------------------------------------------------------------------------- /FSharpTest/Script.fsx: -------------------------------------------------------------------------------- 1 | // Learn more about F# at http://fsharp.org. See the 'F# Tutorial' project 2 | // for more guidance on F# programming. 3 | 4 | #load "Library.fs" 5 | open FSharpTest 6 | 7 | // Define your library scripting code here 8 | 9 | -------------------------------------------------------------------------------- /FSharpTest/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Bite License 2 | [The BSD License] 3 | Copyright (c) 2022 Maximilian Winter 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 | Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 9 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 10 | 11 | 12 | ANTLR 4 License 13 | [The BSD License] 14 | Copyright (c) 2012 Terence Parr and Sam Harwell 15 | All rights reserved. 16 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 17 | 18 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 19 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 20 | Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 | -------------------------------------------------------------------------------- /TestApp/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle( "TestApp" )] 8 | [assembly: AssemblyDescription( "" )] 9 | [assembly: AssemblyConfiguration( "" )] 10 | [assembly: AssemblyCompany( "" )] 11 | [assembly: AssemblyProduct( "TestApp" )] 12 | [assembly: AssemblyCopyright( "Copyright © 2022" )] 13 | [assembly: AssemblyTrademark( "" )] 14 | [assembly: AssemblyCulture( "" )] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible( false )] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid( "9577fef2-3d0b-4cc3-9ff6-0fd56f564534" )] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion( "1.0.0.0" )] 35 | [assembly: AssemblyFileVersion( "1.0.0.0" )] 36 | -------------------------------------------------------------------------------- /TestApp/TestClassCSharp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TestApp 4 | { 5 | 6 | public class Foo 7 | { 8 | public int i = 5; 9 | } 10 | 11 | public class TestClassCSharp 12 | { 13 | public static int t = 45; 14 | public double i = 5; 15 | 16 | public Foo testfield { get; set; } = new Foo(); 17 | 18 | #region Public 19 | 20 | public TestClassCSharp() 21 | { 22 | i = 0; 23 | } 24 | 25 | public TestClassCSharp( int n ) 26 | { 27 | i = n; 28 | } 29 | 30 | public TestClassCSharp( int n, double x, int y, float z ) 31 | { 32 | i = n * x * y * z; 33 | } 34 | 35 | public void PrintVar() 36 | { 37 | Console.WriteLine( i ); 38 | } 39 | 40 | #endregion 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /TestApp/TestProgram/BinaryOpChaining.bite: -------------------------------------------------------------------------------- 1 | module HelloWorld; 2 | 3 | import System; 4 | using System; 5 | 6 | var a = 1 * 2 + 3 * 4; 7 | var b = 2 * 5 - 4 / 2 + 6; 8 | var c = 2 * 5 - (4 / 2 + 6); 9 | var d = 2 * 5 - 4 / (2 + 6); 10 | 11 | PrintLine(a); 12 | PrintLine(b); 13 | PrintLine(c); 14 | PrintLine(d); 15 | -------------------------------------------------------------------------------- /TestApp/TestProgram/BranchingTest.bite: -------------------------------------------------------------------------------- 1 | module HelloWorld; 2 | 3 | import System; 4 | using System; 5 | 6 | var a = "foo"; 7 | 8 | var b = "bar"; 9 | 10 | var c = 0; 11 | 12 | if( a == "test") 13 | { 14 | c = 2; 15 | } 16 | else if( b == "bar1") 17 | { 18 | c = 4; 19 | } 20 | else if( b == "bar2") 21 | { 22 | c = 8; 23 | } 24 | else if( b == "bar5") 25 | { 26 | c = 16; 27 | } 28 | else if( b == "bar") 29 | { 30 | c = 32; 31 | } 32 | else if( b == "bar4") 33 | { 34 | c = 64; 35 | } 36 | else 37 | { 38 | c = 42; 39 | } 40 | PrintLine(c); 41 | -------------------------------------------------------------------------------- /TestApp/TestProgram/CSharpEventInvokeExample.bite: -------------------------------------------------------------------------------- 1 | module HelloWorld; 2 | 3 | import System; 4 | using System; 5 | 6 | class TestSender 7 | { 8 | 9 | } 10 | var valueSetByCallback = "Hallo"; 11 | var sender = new TestSender(); 12 | var counter = 0; 13 | 14 | var SampleEventArgs = NetLanguageInterface("SampleEventArgs", true, "", "string"); 15 | 16 | while(true) 17 | { 18 | SampleEventArgs.Text = "Hallo: ${counter}"; 19 | EventObject.OnSampleEvent.Invoke(sender, SampleEventArgs); 20 | counter++; 21 | } 22 | -------------------------------------------------------------------------------- /TestApp/TestProgram/CSharpEventReceiverExample.bite: -------------------------------------------------------------------------------- 1 | module HelloWorld; 2 | 3 | import System; 4 | using System; 5 | 6 | var valueSetByCallback = "Hallo"; 7 | 8 | function CallBack(n, s) 9 | { 10 | valueSetByCallback = s.Text; 11 | } 12 | 13 | PrintLine(valueSetByCallback); 14 | EventObject.OnSampleEvent += CallBack; 15 | 16 | var oldValue = valueSetByCallback; 17 | while(true) 18 | { 19 | if(oldValue != valueSetByCallback) 20 | { 21 | PrintLine("New Value From Callback: ${valueSetByCallback}"); 22 | } 23 | } -------------------------------------------------------------------------------- /TestApp/TestProgram/ConstructorTest.bite: -------------------------------------------------------------------------------- 1 | module HelloWorld; 2 | 3 | import System; 4 | using System; 5 | 6 | class Foo 7 | { 8 | var x = 5; 9 | } 10 | 11 | class TestClass : Foo 12 | { 13 | function TestClass(n) 14 | { 15 | x = n; 16 | } 17 | } 18 | 19 | function TestFunction(n) 20 | { 21 | n.x = 10; 22 | } 23 | 24 | var t = new TestClass(500); 25 | PrintLine(t.x); 26 | 27 | var a = new TestClass(150); 28 | 29 | PrintLine(a.x); 30 | 31 | TestFunction(a); 32 | 33 | PrintLine(a.x); -------------------------------------------------------------------------------- /TestApp/TestProgram/DynamicArray.bite: -------------------------------------------------------------------------------- 1 | module HelloWorld; 2 | 3 | import System; 4 | using System; 5 | 6 | var c = new Object(); 7 | var f = 0; 8 | for(var x = 0; x < 10; x++) 9 | { 10 | for(var y = 0; y < 10; y++) 11 | { 12 | for(var z = 0; z < 10; z++) 13 | { 14 | c[x][y][z] = "Number: " + f; 15 | f++; 16 | } 17 | } 18 | } 19 | 20 | for(var x = 0; x < 10; x++) 21 | { 22 | for(var y = 0; y < 10; y++) 23 | { 24 | for(var z = 0; z < 10; z++) 25 | { 26 | PrintLine(c[x][y][z]); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /TestApp/TestProgram/FibonacciExample.bite: -------------------------------------------------------------------------------- 1 | module HelloWorld; 2 | 3 | import System; 4 | using System; 5 | 6 | function FindFibonacciNumber(n) 7 | { 8 | var count= 2; 9 | var a = 1; 10 | var b = 1; 11 | var c = 1; 12 | if(n == 0) 13 | { 14 | return 0; 15 | } 16 | while(count= maxiter ) 64 | { 65 | setPixel( img, i, j, colorBlack ); 66 | } 67 | else 68 | { 69 | var intensity = mathSqrt(k / maxiter); 70 | setPixel( img, i, j, GetWhiteColorByIntensity(intensity)); 71 | } 72 | } 73 | } 74 | 75 | img.Save( "file.png", "string", imgFormat, "ImageFormat" ); 76 | -------------------------------------------------------------------------------- /TestApp/TestProgram/NetLanguageInterfaceExample.bite: -------------------------------------------------------------------------------- 1 | module HelloWorld; 2 | 3 | import System; 4 | using System; 5 | 6 | 7 | // Create variable that holds static class. In this case it is System.Console. 8 | var Console = NetLanguageInterface("Console"); 9 | 10 | // Use the variable declared before and call the WriteLine method. 11 | Console.WriteLine("Hello World!"); 12 | 13 | 14 | // Create variable that holds an instance of TestClassCSharp through use of the default constructor. 15 | var TestCSharp1 = NetLanguageInterface("TestClassCSharp", true); 16 | 17 | // Access the field 'i' from the TestClassCSharp instance. 18 | Console.WriteLine(TestCSharp1.i); 19 | 20 | 21 | // Create variable that holds an instance of TestClassCSharp, it passes an argument of type int to the constructor. 22 | var TestCSharp2 = NetLanguageInterface("TestClassCSharp", true, 42, "int"); 23 | 24 | // Access the field 'i' from the TestClassCSharp instance. 25 | Console.WriteLine(TestCSharp2.i); 26 | 27 | 28 | // Create variable that holds an instance of TestClassCSharp, it passes 4 arguments to the constructor int, double, int and float. 29 | var TestCSharp3 = NetLanguageInterface("TestClassCSharp", true, 42, "int", 42.24, "double", 42, "int", 42.42, "float"); 30 | 31 | // Access the field 'i' from the TestClassCSharp instance. 32 | Console.WriteLine(TestCSharp3.i); 33 | 34 | 35 | // Create variable that holds an instance of Line from a F# Library, it passes 4 arguments to the constructor all of them floats. 36 | var FSharpLine = NetLanguageInterface("Line", true, 1, "float", 2, "float", 3, "float", 4, "float"); 37 | 38 | // Access the field 'Y2' from the Line instance. 39 | Console.WriteLine(FSharpLine.Y2); 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /TestApp/TestProgram/PrimeNumber.bite: -------------------------------------------------------------------------------- 1 | module HelloWorld; 2 | 3 | import System; 4 | using System; 5 | 6 | function FindPrimeNumber(n) 7 | { 8 | var count=0; 9 | var a = 2; 10 | while(count 0) 24 | { 25 | count++; 26 | } 27 | a++; 28 | } 29 | return (--a); 30 | } 31 | 32 | PrintLine(FindPrimeNumber(2)); 33 | PrintLine(FindPrimeNumber(4)); 34 | PrintLine(FindPrimeNumber(8)); 35 | PrintLine(FindPrimeNumber(16)); 36 | PrintLine(FindPrimeNumber(32)); 37 | PrintLine(FindPrimeNumber(64)); -------------------------------------------------------------------------------- /TestApp/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /TestAppNet6/TestAppNet6.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | PreserveNewest 19 | 20 | 21 | PreserveNewest 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /TestAppNet6/TestProgram/CSharpSystemModule.bite: -------------------------------------------------------------------------------- 1 | module CSharpSystem; 2 | import System; 3 | using System; 4 | 5 | var CSharpInterfaceObject = new CSharpInterface(); 6 | 7 | // Type.GetType requires an Assembly Qualified Name if it is not in mscorlib 8 | CSharpInterfaceObject.Type = "System.Console, System.Console"; 9 | 10 | var Console = CSharpInterfaceCall(CSharpInterfaceObject); 11 | 12 | // Type.GetType requires an Assembly Qualified Name if it is not in mscorlib 13 | CSharpInterfaceObject.Type = "System.IO.File, System.IO.FileSystem"; 14 | 15 | var File = CSharpInterfaceCall(CSharpInterfaceObject); 16 | 17 | 18 | -------------------------------------------------------------------------------- /TestAppNet6/TestProgram/MainModule.bite: -------------------------------------------------------------------------------- 1 | module MainModule; 2 | 3 | import System; 4 | using System; 5 | 6 | import CSharpSystem; 7 | using CSharpSystem; 8 | 9 | function FindFibonacciNumber(n) 10 | { 11 | var count= 2; 12 | var a = 1; 13 | var b = 1; 14 | var c = 1; 15 | if(n == 0) 16 | { 17 | return 0; 18 | } 19 | while(count= maxiter ) 54 | { 55 | img.SetPixel( i, "int", j, "int", colorBlack, "Color" ); 56 | } 57 | else 58 | { 59 | var intensity = math.Sqrt(k / maxiter, "double"); 60 | img.SetPixel( i, "int", j, "int", GetWhiteColorByIntensity(intensity), "Color" ); 61 | } 62 | } 63 | } 64 | 65 | img.Save( "file.png", "string", imgFormat, "ImageFormat" ); -------------------------------------------------------------------------------- /TestMandelbrot/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle( "TestMandelbrot" )] 8 | [assembly: AssemblyDescription( "" )] 9 | [assembly: AssemblyConfiguration( "" )] 10 | [assembly: AssemblyCompany( "" )] 11 | [assembly: AssemblyProduct( "TestMandelbrot" )] 12 | [assembly: AssemblyCopyright( "Copyright © 2022" )] 13 | [assembly: AssemblyTrademark( "" )] 14 | [assembly: AssemblyCulture( "" )] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible( false )] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid( "46A37F39-770C-42DB-888A-CDB8EB674B3B" )] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion( "1.0.0.0" )] 35 | [assembly: AssemblyFileVersion( "1.0.0.0" )] 36 | -------------------------------------------------------------------------------- /TestMandelbrot/TestMandelbrot.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {46A37F39-770C-42DB-888A-CDB8EB674B3B} 8 | Exe 9 | Properties 10 | TestMandelbrot 11 | TestMandelbrot 12 | v4.8 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | false 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | {2b941484-fb5e-4b3f-af7d-bf61238d6f16} 50 | Bite 51 | 52 | 53 | 54 | 55 | Always 56 | 57 | 58 | 59 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /UnitTests/UnitTests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net462 5 | false 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | runtime; build; native; contentfiles; analyzers; buildtransitive 17 | all 18 | 19 | 20 | runtime; build; native; contentfiles; analyzers; buildtransitive 21 | all 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /WpfThreadTest/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /WpfThreadTest/App.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /WpfThreadTest/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | 3 | namespace WpfThreadTest 4 | { 5 | 6 | /// 7 | /// Interaction logic for App.xaml 8 | /// 9 | public partial class App : Application 10 | { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /WpfThreadTest/GameObject.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | using System.Windows.Controls; 3 | 4 | namespace WpfThreadTest 5 | { 6 | 7 | public class GameObject 8 | { 9 | private readonly UIElement m_Element; 10 | 11 | public float X { get; set; } 12 | 13 | public float Y { get; set; } 14 | 15 | public float dX { get; set; } 16 | 17 | public float dY { get; set; } 18 | 19 | #region Public 20 | 21 | public GameObject( UIElement element ) 22 | { 23 | m_Element = element; 24 | dX = 0.01f; 25 | dY = 0.01f; 26 | } 27 | 28 | public void Move() 29 | { 30 | // This code needs to be executed on the UI thread, where the element was created 31 | Canvas.SetLeft( m_Element, X ); 32 | Canvas.SetTop( m_Element, Y ); 33 | } 34 | 35 | #endregion 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /WpfThreadTest/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /WpfThreadTest/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | using System.Windows; 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( "WpfThreadTest" )] 9 | [assembly: AssemblyDescription( "" )] 10 | [assembly: AssemblyConfiguration( "" )] 11 | [assembly: AssemblyCompany( "" )] 12 | [assembly: AssemblyProduct( "WpfThreadTest" )] 13 | [assembly: AssemblyCopyright( "Copyright © 2022" )] 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 | //In order to begin building localizable applications, set 23 | //CultureYouAreCodingWith in your .csproj file 24 | //inside a . For example, if you are using US english 25 | //in your source files, set the to en-US. Then uncomment 26 | //the NeutralResourceLanguage attribute below. Update the "en-US" in 27 | //the line below to match the UICulture setting in the project file. 28 | 29 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 30 | 31 | [assembly: ThemeInfo( 32 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 33 | //(used if a resource is not found in the page, 34 | // or application resource dictionaries) 35 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 36 | //(used if a resource is not found in the page, 37 | // app, or any theme specific resource dictionaries) 38 | )] 39 | 40 | // Version information for an assembly consists of the following four values: 41 | // 42 | // Major Version 43 | // Minor Version 44 | // Build Number 45 | // Revision 46 | // 47 | // You can specify all the values or you can default the Build and Revision Numbers 48 | // by using the '*' as shown below: 49 | // [assembly: AssemblyVersion("1.0.*")] 50 | [assembly: AssemblyVersion( "1.0.0.0" )] 51 | [assembly: AssemblyFileVersion( "1.0.0.0" )] 52 | -------------------------------------------------------------------------------- /WpfThreadTest/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace WpfThreadTest.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// Returns the cached ResourceManager instance used by this class. 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("WpfThreadTest.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// Overrides the current thread's CurrentUICulture property for all 51 | /// resource lookups using this strongly typed resource class. 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /WpfThreadTest/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // Dieser Code wurde von einem Tool generiert. 4 | // Laufzeitversion:4.0.30319.42000 5 | // 6 | // Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn 7 | // der Code erneut generiert wird. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace WpfThreadTest.Properties { 12 | 13 | 14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] 16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { 17 | 18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 19 | 20 | public static Settings Default { 21 | get { 22 | return defaultInstance; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /WpfThreadTest/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | --------------------------------------------------------------------------------