Create all in one method Brainmess
authorMichael Welch <michaelgwelch@gmail.com>
Wed, 14 Mar 2012 00:55:20 +0000 (19:55 -0500)
committerMichael Welch <michaelgwelch@gmail.com>
Wed, 14 Mar 2012 00:55:20 +0000 (19:55 -0500)
csharp/BrainmessShort/BrainmessShort.csproj [new file with mode: 0644]
csharp/BrainmessShort/BrainmessShort.sln [new file with mode: 0644]
csharp/BrainmessShort/Main.cs [new file with mode: 0644]

diff --git a/csharp/BrainmessShort/BrainmessShort.csproj b/csharp/BrainmessShort/BrainmessShort.csproj
new file mode 100644 (file)
index 0000000..56d608a
--- /dev/null
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+    <ProductVersion>9.0.21022</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{D1C9257C-3C4A-4091-8CA6-38803D0D1679}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>BrainmessShort</RootNamespace>
+    <AssemblyName>BrainmessShort</AssemblyName>
+    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug</OutputPath>
+    <DefineConstants>DEBUG;</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <PlatformTarget>x86</PlatformTarget>
+    <Externalconsole>true</Externalconsole>
+    <Commandlineparameters>../../../../scripts/hello.bm</Commandlineparameters>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+    <DebugType>none</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Release</OutputPath>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <PlatformTarget>x86</PlatformTarget>
+    <Externalconsole>true</Externalconsole>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Main.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+</Project>
\ No newline at end of file
diff --git a/csharp/BrainmessShort/BrainmessShort.sln b/csharp/BrainmessShort/BrainmessShort.sln
new file mode 100644 (file)
index 0000000..635921e
--- /dev/null
@@ -0,0 +1,20 @@
+\feff\r
+Microsoft Visual Studio Solution File, Format Version 10.00\r
+# Visual Studio 2008\r
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrainmessShort", "BrainmessShort.csproj", "{D1C9257C-3C4A-4091-8CA6-38803D0D1679}"\r
+EndProject\r
+Global\r
+       GlobalSection(SolutionConfigurationPlatforms) = preSolution\r
+               Debug|x86 = Debug|x86\r
+               Release|x86 = Release|x86\r
+       EndGlobalSection\r
+       GlobalSection(ProjectConfigurationPlatforms) = postSolution\r
+               {D1C9257C-3C4A-4091-8CA6-38803D0D1679}.Debug|x86.ActiveCfg = Debug|x86\r
+               {D1C9257C-3C4A-4091-8CA6-38803D0D1679}.Debug|x86.Build.0 = Debug|x86\r
+               {D1C9257C-3C4A-4091-8CA6-38803D0D1679}.Release|x86.ActiveCfg = Release|x86\r
+               {D1C9257C-3C4A-4091-8CA6-38803D0D1679}.Release|x86.Build.0 = Release|x86\r
+       EndGlobalSection\r
+       GlobalSection(MonoDevelopProperties) = preSolution\r
+               StartupItem = BrainmessShort.csproj\r
+       EndGlobalSection\r
+EndGlobal\r
diff --git a/csharp/BrainmessShort/Main.cs b/csharp/BrainmessShort/Main.cs
new file mode 100644 (file)
index 0000000..f9643f6
--- /dev/null
@@ -0,0 +1,77 @@
+using System;
+using System.IO;
+
+namespace BrainmessShort
+{
+   public class Brainmess
+   {
+        public static void Main(string[] args)
+        {       
+            var reader = File.OpenText(args[0]);
+            Run(reader.ReadToEnd());
+            reader.Close();
+        }
+        
+        public static void Run(string program) 
+        {
+            int pc = 0;
+            int[] tape = new int[5000];
+            int tc = 2500;
+            int nestLevel;
+            while(pc < program.Length)
+            {
+                char instruction = program[pc];
+                pc++;
+                switch(instruction)
+                {
+                case '>': 
+                    tc++;
+                    break;
+                case '<':
+                    tc--;
+                    break;
+                case '+':
+                    tape[tc]++;
+                    break;
+                case '-':
+                    tape[tc]--;
+                    break;
+                case '.':
+                    Console.Write((char)tape[tc]);
+                    break;
+                case ',':
+                    tape[tc] = Console.Read();
+                    break;
+                case '[':
+                    if (tape[tc] == 0)
+                    {
+                        nestLevel = 1;
+                        while(nestLevel > 0)
+                        {
+                            instruction = program[pc];
+                            if (instruction == '[') nestLevel++;
+                            else if (instruction == ']') nestLevel--;
+                            pc++;
+                        }
+                    }
+                    break;
+                case ']':
+                    if (tape[tc] != 0)
+                    {
+                        pc -= 2;
+                        nestLevel = 1;
+                        while(nestLevel > 0)
+                        {
+                            instruction = program[pc];
+                            if (instruction == '[') nestLevel--;
+                            else if (instruction == ']') nestLevel++;
+                            pc--;
+                        }
+                        pc++;
+                    }
+                    break;
+                }
+            }
+        }
+    }
+}