Useage is as:
To create the object: Emo emo = new Emo(); emo.SourceFile = @"C:\yourfilename.emo"; To syntax check: Listerrors; bool wereThereErrors = emo.SyntaxCheck(out errors); To emulate: emo.Emulate();
Please forgive the formatting, you should be able to copy and paste the source below get it out.
EMO language emulator and syntax checker Copyright (C) 2010 Sean Fife This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
public class Emo { public string SourceFile { get; set; } public Emo() { } ~Emo() { } public void Emulate() { StreamReader source = new StreamReader(SourceFile); string allCode = source.ReadToEnd(); source.Close(); string[] lines = allCode.Split('\n'); for (int i = 0; i < lines.Length; i++) { lines[i] = lines[i].Split('~')[0].Trim(); } //memory byte[] memory = new byte[256]; byte register = new byte(); byte workingLocation = new byte(); int currentMemoryPtr = 0; int loopIndex = 0; int[] loopBegin = new int[32]; int[] loopEnd = new int[32]; bool ptrOrValue = false; bool justLooped = false; Stackstack = new Stack (); for (int i = 0; i < lines.Length; i++) { int len = lines[i].Length; for (int j = 0; j < len; j++) { switch (lines[i][j]) { case ';': workingLocation = memory[currentMemoryPtr]; ptrOrValue = true; break; case ':': workingLocation = register; ptrOrValue = false; break; case ')': register = workingLocation; break; case '}': register = memory[currentMemoryPtr]; break; case '{': memory[currentMemoryPtr] = register; break; case '(': memory[currentMemoryPtr] = workingLocation; break; case '^': if (ptrOrValue) { currentMemoryPtr++; } else { workingLocation++; } break; case '-': if (ptrOrValue) { currentMemoryPtr--; } else { workingLocation--; } break; case 'o': if (ptrOrValue) { currentMemoryPtr = currentMemoryPtr << 1; } else { workingLocation = (byte)((int)workingLocation << 1); } break; case 'c': if (ptrOrValue) { currentMemoryPtr = currentMemoryPtr >> 1; } else { workingLocation = (byte)((int)workingLocation >> 1); } break; case '=': //wait for keyboard input ConsoleKeyInfo info = Console.ReadKey(); workingLocation = (byte)char.ConvertToUtf32(info.KeyChar.ToString(), 0); break; case '|'://no op mouth break; case '@': Console.Write(char.ConvertFromUtf32(workingLocation)); break; case '<': if (justLooped) { justLooped = false; } else { loopIndex++; loopBegin[loopIndex] = i; } break; case '>': loopEnd[loopIndex] = i; if (memory[currentMemoryPtr] == 0) { loopIndex--; } else { i = loopBegin[loopIndex] - 1; justLooped = true; continue; } break; case 'P': stack.Push(workingLocation); break; case 'X': workingLocation = stack.Pop(); break; } } } } public bool SyntaxCheck(out List errors) { errors = new List (); StreamReader source = new StreamReader(SourceFile); string allCode = source.ReadToEnd(); source.Close(); string[] lines = allCode.Split('\n'); bool isOk = true; int i = 0; int nestedLoopCount = 0; int errorCount = 0; foreach (string l in lines) { string line = l.Split('~')[0].Trim(); if (line.Length == 0) continue; int len = line.Length - 1; if (line[0] != ':' && line[0] != ';' && line[0] != '<' && line[0] != '=') { isOk = false; errors.Add(string.Format("Line({0}): Line must begin with :, ;, =, or <", i)); errorCount++; } if (line[len] != ')' && line[len] != '(' && line[len] != '>' && line[len] != '|' && line[len] != '@' && line[len] != '}' && line[len] != '{') { isOk = false; errorCount++; errors.Add(string.Format("Line({0}): Line must end with (, ), |, @, {, }, or >", i)); } if (line.IndexOf(';') < 0 && line.IndexOf(':') < 0 && line.IndexOf('=') < 0) { errorCount++; isOk = false; errors.Add(string.Format("Line({0}): Line must contain :, ;, or =", i)); } if (line.IndexOf(')')< 0 && line.IndexOf('(') < 0 && line.IndexOf('|') < 0 && line.IndexOf('@') < 0 && line[len] != '}' && line[len] != '{') { errorCount++; isOk = false; errors.Add(string.Format("Line({0}): Line must contain (, ), @, }, {, or |", i)); } if (line.IndexOf('<') >= 0 && line.IndexOf('>') >= 0) { errors.Add(string.Format("Line({0}): Warning - Loop begins and ends on same line, beware infinite looping", i)); } if (line.IndexOf('<') >= 0) { nestedLoopCount++; } if (line.IndexOf('>') >= 0) { nestedLoopCount--; } i++; } if (nestedLoopCount != 0) { isOk = false; errorCount++; errors.Add(string.Format("Incorrect nested loops, missing opening or closing bracket", i)); } if (errorCount > 0) { errors.Add(""); errors.Add(""); errors.Add(string.Format("{0} Total Errors", errorCount)); } else { errors.Add(string.Format("No Errors", errorCount)); } return isOk; } }