Skip to content

Commit 6711657

Browse files
committed
very dirty experimental code
1 parent a96c3e9 commit 6711657

File tree

5 files changed

+290
-0
lines changed

5 files changed

+290
-0
lines changed

BrainSharp.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.24720.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrainSharp", "BrainSharp\BrainSharp.csproj", "{9FF6A7BB-6156-403F-9368-636E84B5A1BE}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{9FF6A7BB-6156-403F-9368-636E84B5A1BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9FF6A7BB-6156-403F-9368-636E84B5A1BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9FF6A7BB-6156-403F-9368-636E84B5A1BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9FF6A7BB-6156-403F-9368-636E84B5A1BE}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

BrainSharp/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>

BrainSharp/BrainSharp.csproj

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{9FF6A7BB-6156-403F-9368-636E84B5A1BE}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>BrainSharp</RootNamespace>
11+
<AssemblyName>BrainSharp</AssemblyName>
12+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<None Include="App.config" />
51+
</ItemGroup>
52+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
53+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
54+
Other similar extension points exist, see Microsoft.Common.targets.
55+
<Target Name="BeforeBuild">
56+
</Target>
57+
<Target Name="AfterBuild">
58+
</Target>
59+
-->
60+
</Project>

BrainSharp/Program.cs

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Reflection;
5+
using System.Reflection.Emit;
6+
7+
namespace BrainSharp
8+
{
9+
internal class Program
10+
{
11+
private static ILGenerator ilg;
12+
private static LocalBuilder v3;
13+
14+
private static Stack<Label> labels;
15+
private static Stack<Label> labels2;
16+
17+
private static void Main(string[] args)
18+
{
19+
labels = new Stack<Label>();
20+
labels2 = new Stack<Label>();
21+
22+
AssemblyName name = new AssemblyName();
23+
name.Name = "Test";
24+
AssemblyBuilder bld = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Save);
25+
26+
ModuleBuilder mb = bld.DefineDynamicModule(name.Name, "Hello.exe");
27+
28+
TypeBuilder tb = mb.DefineType("Program", TypeAttributes.Public | TypeAttributes.Class);
29+
MethodBuilder fb = tb.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { typeof(string[]) });
30+
31+
ilg = fb.GetILGenerator();
32+
33+
ilg.DeclareLocal(typeof(byte[]));
34+
ilg.DeclareLocal(typeof(Int32));
35+
ilg.DeclareLocal(typeof(bool));
36+
v3 = ilg.DeclareLocal(typeof(ConsoleKeyInfo));
37+
38+
ilg.Emit(OpCodes.Ldc_I4, 30000);
39+
ilg.Emit(OpCodes.Newarr, typeof(byte));
40+
ilg.Emit(OpCodes.Stloc_0);
41+
42+
char[] code;
43+
int cp = 0;
44+
45+
if (args.Length == 0)
46+
code = Console.ReadLine().ToCharArray();
47+
else
48+
code = File.ReadAllText(args[0]).ToCharArray();
49+
50+
while (cp < code.Length)
51+
{
52+
char c = code[cp];
53+
54+
switch (c)
55+
{
56+
case '<': EmitMpMinus(); break;
57+
case '>': EmitMpPlus(); break;
58+
case '+': EmitMemPlus(); break;
59+
case '-': EmitMemMinus(); break;
60+
case '.': EmitWrite(); break;
61+
case ',': EmitRead(); break;
62+
case '[': EmitLoopStart(); break;
63+
case ']': EmitLoopEnd(); break;
64+
}
65+
66+
cp++;
67+
}
68+
69+
ilg.Emit(OpCodes.Ret);
70+
71+
Type t = tb.CreateType();
72+
// Set the entrypoint (thereby declaring it an EXE)
73+
bld.SetEntryPoint(fb, PEFileKinds.ConsoleApplication);
74+
75+
// Save it
76+
bld.Save("Hello.exe");
77+
}
78+
79+
private static void EmitLoopStart()
80+
{
81+
var br = ilg.DefineLabel();
82+
ilg.Emit(OpCodes.Br, br);
83+
labels.Push(br);
84+
85+
var br2 = ilg.DefineLabel();
86+
ilg.MarkLabel(br2);
87+
labels2.Push(br2);
88+
}
89+
90+
private static void EmitLoopEnd()
91+
{
92+
var lcc = labels.Pop();
93+
ilg.MarkLabel(lcc);
94+
ilg.Emit(OpCodes.Ldloc_0);
95+
ilg.Emit(OpCodes.Ldloc_1);
96+
ilg.Emit(OpCodes.Ldelem_U1);
97+
ilg.Emit(OpCodes.Ldc_I4_0);
98+
ilg.Emit(OpCodes.Cgt_Un);
99+
ilg.Emit(OpCodes.Stloc_2);
100+
ilg.Emit(OpCodes.Ldloc_2);
101+
ilg.Emit(OpCodes.Brtrue, labels2.Pop());
102+
}
103+
104+
private static void EmitRead()
105+
{
106+
ilg.Emit(OpCodes.Ldloc_0);
107+
ilg.Emit(OpCodes.Ldloc_1);
108+
ilg.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadKey", new Type[] { }));
109+
ilg.Emit(OpCodes.Stloc_3);
110+
ilg.Emit(OpCodes.Ldloca_S, v3);
111+
ilg.Emit(OpCodes.Call, typeof(ConsoleKeyInfo).GetMethod("get_KeyChar"));
112+
ilg.Emit(OpCodes.Conv_U1);
113+
ilg.Emit(OpCodes.Stelem_I1);
114+
}
115+
116+
private static void EmitWrite()
117+
{
118+
ilg.Emit(OpCodes.Ldloc_0);
119+
ilg.Emit(OpCodes.Ldloc_1);
120+
ilg.Emit(OpCodes.Ldelem_U1);
121+
ilg.Emit(OpCodes.Call, typeof(Console).GetMethod("Write", new Type[] { typeof(char) }));
122+
}
123+
124+
private static void EmitMemMinus()
125+
{
126+
ilg.Emit(OpCodes.Ldloc_0);
127+
ilg.Emit(OpCodes.Ldloc_1);
128+
ilg.Emit(OpCodes.Ldelema, typeof(byte));
129+
ilg.Emit(OpCodes.Dup);
130+
ilg.Emit(OpCodes.Ldind_U1);
131+
ilg.Emit(OpCodes.Ldc_I4_1);
132+
ilg.Emit(OpCodes.Sub);
133+
ilg.Emit(OpCodes.Conv_U1);
134+
ilg.Emit(OpCodes.Stind_I1);
135+
}
136+
137+
private static void EmitMemPlus()
138+
{
139+
ilg.Emit(OpCodes.Ldloc_0);
140+
ilg.Emit(OpCodes.Ldloc_1);
141+
ilg.Emit(OpCodes.Ldelema, typeof(byte));
142+
ilg.Emit(OpCodes.Dup);
143+
ilg.Emit(OpCodes.Ldind_U1);
144+
ilg.Emit(OpCodes.Ldc_I4_1);
145+
ilg.Emit(OpCodes.Add);
146+
ilg.Emit(OpCodes.Conv_U1);
147+
ilg.Emit(OpCodes.Stind_I1);
148+
}
149+
150+
private static void EmitMpPlus()
151+
{
152+
ilg.Emit(OpCodes.Ldloc_1);
153+
ilg.Emit(OpCodes.Ldc_I4_1);
154+
ilg.Emit(OpCodes.Add);
155+
ilg.Emit(OpCodes.Stloc_1);
156+
}
157+
158+
private static void EmitMpMinus()
159+
{
160+
ilg.Emit(OpCodes.Ldloc_1);
161+
ilg.Emit(OpCodes.Ldc_I4_1);
162+
ilg.Emit(OpCodes.Sub);
163+
ilg.Emit(OpCodes.Stloc_1);
164+
}
165+
}
166+
}

BrainSharp/Properties/AssemblyInfo.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("BrainSharp")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("BrainSharp")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("9ff6a7bb-6156-403f-9368-636e84b5a1be")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)