├── .gitignore ├── LICENSE ├── README.md ├── UwuScript.sln ├── UwuScript ├── App.config ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── Token.cs ├── TokenTemplate.cs ├── TokenType.cs ├── TokenizationException.cs ├── Tokenizer.cs ├── TuringMachine.cs ├── UwuScript.csproj └── examples │ └── hello.uwu └── assets ├── logo.png └── logo.svg /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | *.VC.VC.opendb 85 | 86 | # Visual Studio profiler 87 | *.psess 88 | *.vsp 89 | *.vspx 90 | *.sap 91 | 92 | # TFS 2012 Local Workspace 93 | $tf/ 94 | 95 | # Guidance Automation Toolkit 96 | *.gpState 97 | 98 | # ReSharper is a .NET coding add-in 99 | _ReSharper*/ 100 | *.[Rr]e[Ss]harper 101 | *.DotSettings.user 102 | 103 | # JustCode is a .NET coding add-in 104 | .JustCode 105 | 106 | # TeamCity is a build add-in 107 | _TeamCity* 108 | 109 | # DotCover is a Code Coverage Tool 110 | *.dotCover 111 | 112 | # NCrunch 113 | _NCrunch_* 114 | .*crunch*.local.xml 115 | nCrunchTemp_* 116 | 117 | # MightyMoose 118 | *.mm.* 119 | AutoTest.Net/ 120 | 121 | # Web workbench (sass) 122 | .sass-cache/ 123 | 124 | # Installshield output folder 125 | [Ee]xpress/ 126 | 127 | # DocProject is a documentation generator add-in 128 | DocProject/buildhelp/ 129 | DocProject/Help/*.HxT 130 | DocProject/Help/*.HxC 131 | DocProject/Help/*.hhc 132 | DocProject/Help/*.hhk 133 | DocProject/Help/*.hhp 134 | DocProject/Help/Html2 135 | DocProject/Help/html 136 | 137 | # Click-Once directory 138 | publish/ 139 | 140 | # Publish Web Output 141 | *.[Pp]ublish.xml 142 | *.azurePubxml 143 | # TODO: Comment the next line if you want to checkin your web deploy settings 144 | # but database connection strings (with potential passwords) will be unencrypted 145 | *.pubxml 146 | *.publishproj 147 | 148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 149 | # checkin your Azure Web App publish settings, but sensitive information contained 150 | # in these scripts will be unencrypted 151 | PublishScripts/ 152 | 153 | # NuGet Packages 154 | *.nupkg 155 | # The packages folder can be ignored because of Package Restore 156 | **/packages/* 157 | # except build/, which is used as an MSBuild target. 158 | !**/packages/build/ 159 | # Uncomment if necessary however generally it will be regenerated when needed 160 | #!**/packages/repositories.config 161 | # NuGet v3's project.json files produces more ignoreable files 162 | *.nuget.props 163 | *.nuget.targets 164 | 165 | # Microsoft Azure Build Output 166 | csx/ 167 | *.build.csdef 168 | 169 | # Microsoft Azure Emulator 170 | ecf/ 171 | rcf/ 172 | 173 | # Windows Store app package directories and files 174 | AppPackages/ 175 | BundleArtifacts/ 176 | Package.StoreAssociation.xml 177 | _pkginfo.txt 178 | 179 | # Visual Studio cache files 180 | # files ending in .cache can be ignored 181 | *.[Cc]ache 182 | # but keep track of directories ending in .cache 183 | !*.[Cc]ache/ 184 | 185 | # Others 186 | ClientBin/ 187 | ~$* 188 | *~ 189 | *.dbmdl 190 | *.dbproj.schemaview 191 | *.pfx 192 | *.publishsettings 193 | node_modules/ 194 | orleans.codegen.cs 195 | 196 | # Since there are multiple workflows, uncomment next line to ignore bower_components 197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 198 | #bower_components/ 199 | 200 | # RIA/Silverlight projects 201 | Generated_Code/ 202 | 203 | # Backup & report files from converting an old project file 204 | # to a newer Visual Studio version. Backup files are not needed, 205 | # because we have git ;-) 206 | _UpgradeReport_Files/ 207 | Backup*/ 208 | UpgradeLog*.XML 209 | UpgradeLog*.htm 210 | 211 | # SQL Server files 212 | *.mdf 213 | *.ldf 214 | 215 | # Business Intelligence projects 216 | *.rdl.data 217 | *.bim.layout 218 | *.bim_*.settings 219 | 220 | # Microsoft Fakes 221 | FakesAssemblies/ 222 | 223 | # GhostDoc plugin setting file 224 | *.GhostDoc.xml 225 | 226 | # Node.js Tools for Visual Studio 227 | .ntvs_analysis.dat 228 | 229 | # Visual Studio 6 build log 230 | *.plg 231 | 232 | # Visual Studio 6 workspace options file 233 | *.opt 234 | 235 | # Visual Studio LightSwitch build output 236 | **/*.HTMLClient/GeneratedArtifacts 237 | **/*.DesktopClient/GeneratedArtifacts 238 | **/*.DesktopClient/ModelManifest.xml 239 | **/*.Server/GeneratedArtifacts 240 | **/*.Server/ModelManifest.xml 241 | _Pvt_Extensions 242 | 243 | # Paket dependency manager 244 | .paket/paket.exe 245 | paket-files/ 246 | 247 | # FAKE - F# Make 248 | .fake/ 249 | 250 | # JetBrains Rider 251 | .idea/ 252 | *.sln.iml 253 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Saul Johnson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UwuScript 2 | World's first uwu-oriented language. 3 | 4 | ![Logo](https://github.com/lambdacasserole/uwuscript/raw/master/assets/logo.png) 5 | 6 | Saw [a tweet](https://twitter.com/UwyBBQ/status/757959990359523328) from [Uwy](https://github.com/Uwy) and thought it'd be a tasty little meme morsel to actually build with a tokenizer I have lying around. It's basically [Brainfuck](https://en.wikipedia.org/wiki/Brainfuck) but with a special token for single-line comments. If you're looking for the plain old Brainfuck interpreter I wrote in C#, it's called [ByteRibbon](https://github.com/lambdacasserole/byteribbon). 7 | 8 | Here's a hello world program in UwuScript: 9 | 10 | ``` 11 | uwu uwu uwu uwu uwu uwu uwu uwu uwu uwu ow< >w> uwu uwu uwu uwu uwu uwu uwu >w> *w* Comment! 12 | uwu uwu uwu uwu uwu uwu uwu uwu uwu uwu >w> uwu uwu uwu >w> uwu wo >w> uwu uwu owo >w> uwu owo uwu uwu uwu uwu uwu uwu uwu owo owo uwu uwu 14 | uwu owo >w> uwu uwu owo w> owo uwu uwu uwu owo nwn nwn nwn nwn nwn nwn owo nwn nwn nwn 16 | nwn nwn nwn nwn nwn owo >w> uwu owo >w> owo *w* Hello world! 17 | ``` 18 | 19 | The following table is straight from [Wikipedia's table of Brainfuck commands](https://en.wikipedia.org/wiki/Brainfuck#Commands), but modified to show each command's UwuScript equivalent: 20 | 21 | | UwuScript | Brainfuck | Meaning | 22 | |-------------|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 23 | | `>w>` | `>` | Increment the data pointer (to point to the next cell to the right). | 24 | | `wo` | `]` | If the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command. | 31 | | `*w*` | `N/A` | Denotes a single-line comment. | 32 | 33 | You're essentially manipulating a [Turing machine](https://en.wikipedia.org/wiki/Turing_machine) to produce output. Anything written in Brainfuck is obviously trivial to port over. 34 | 35 | ## Usage 36 | Compile the project in Visual Studio, run it like this: 37 | 38 | ``` 39 | UwuScript.exe "examples/hello.uwu" 40 | ``` 41 | 42 | ## Limitations 43 | Largely untested and very unoptimized, the Turing machine underlying the language is a very hasty port from the PHP version in [my ancient Brainfony repository](https://github.com/lambdacasserole/brainfony). It's gonna be buggy and slow. 44 | 45 | Also, random inline text will break the tokenizer, that's why the single-line comment token was added. It's easy to change this so you can stick non-instructions wherever you like (like I believe Urban Muller's [original Brainfuck implementation](https://gist.github.com/rdebath/0ca09ec0fdcf3f82478f) does) by changing the regular expressions that the tokenizer is initialized with. 46 | 47 | ## Contributing 48 | If you find a bug and would really like to contribute for whatever reason, feel free to fork and submit a pull request. 49 | -------------------------------------------------------------------------------- /UwuScript.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UwuScript", "UwuScript\UwuScript.csproj", "{FE4335FE-D922-4659-830D-96A825F9CF33}" 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 | {FE4335FE-D922-4659-830D-96A825F9CF33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {FE4335FE-D922-4659-830D-96A825F9CF33}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {FE4335FE-D922-4659-830D-96A825F9CF33}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {FE4335FE-D922-4659-830D-96A825F9CF33}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /UwuScript/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /UwuScript/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | 4 | namespace UwuScript 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | // Configure tokenizer. 11 | var tokenizer = new Tokenizer(); 12 | tokenizer.Add(@">w>", TokenType.MoveRight); 13 | tokenizer.Add(@"wo", TokenType.EndLoop); 20 | tokenizer.Add(@"\s+?", TokenType.Whitespace); 21 | tokenizer.Add(@"\*w\*.+?\n", TokenType.Comment); 22 | 23 | // Check file exists. 24 | var filepath = args[0]; 25 | if (!File.Exists(filepath)) 26 | { 27 | Console.WriteLine("Error opening input file."); 28 | return; 29 | } 30 | 31 | // Tokenize source. 32 | var tokens = tokenizer.Tokenize(File.ReadAllText(filepath) + "\r\n"); 33 | 34 | // Execute program. 35 | var machine = new TuringMachine(tokens); 36 | machine.Run(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /UwuScript/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("UwuScript")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("UwuScript")] 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("fe4335fe-d922-4659-830d-96a825f9cf33")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /UwuScript/Token.cs: -------------------------------------------------------------------------------- 1 | namespace UwuScript 2 | { 3 | /// 4 | /// A token created by the class. 5 | /// 6 | public class Token 7 | { 8 | /// 9 | /// Gets the token type. 10 | /// 11 | public TokenType Type { get; } 12 | 13 | /// 14 | /// Gets the sequence of characters in the token. 15 | /// 16 | public string Sequence { get; } 17 | 18 | /// 19 | /// Gets the column position of the token in the source. 20 | /// 21 | public int Column { get; } 22 | 23 | /// 24 | /// Gets the line position of the token in the source. 25 | /// 26 | public int Line { get; } 27 | 28 | /// 29 | /// Initializes a new instance of a token. 30 | /// 31 | /// The token type. 32 | /// The sequence of characters in the token. 33 | /// The column position of the token in the source. 34 | /// The line position of the token in the source. 35 | public Token(TokenType type, string sequence, int column = -1, int line = -1) 36 | { 37 | Type = type; 38 | Sequence = sequence; 39 | Column = column; 40 | Line = line; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /UwuScript/TokenTemplate.cs: -------------------------------------------------------------------------------- 1 | using System.Text.RegularExpressions; 2 | 3 | namespace UwuScript 4 | { 5 | /// 6 | /// Represents a regular expression template of a token type. 7 | /// 8 | public class TokenTemplate 9 | { 10 | /// 11 | /// Gets the regular expression that matches a token of this type. 12 | /// 13 | public Regex Pattern { get; private set; } 14 | 15 | /// 16 | /// Gets the token type that maps to the template pattern. 17 | /// 18 | public TokenType Type { get; private set; } 19 | 20 | /// 21 | /// Initializes a new instance of a regular expression template of a token type. 22 | /// 23 | /// The regular expression that matches a token of this type. 24 | /// The token type that maps to the template pattern. 25 | public TokenTemplate(Regex pattern, TokenType type) 26 | { 27 | Pattern = pattern; 28 | Type = type; 29 | } 30 | 31 | /// 32 | /// Initializes a new instance of a regular expression template of a token type. 33 | /// 34 | /// The regular expression string that matches a token of this type. 35 | /// The token type that maps to the template pattern. 36 | public TokenTemplate(string pattern, TokenType type) 37 | : this(new Regex(pattern, RegexOptions.Multiline), type) 38 | { 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /UwuScript/TokenType.cs: -------------------------------------------------------------------------------- 1 | namespace UwuScript 2 | { 3 | /// 4 | /// An enumeration of token types. 5 | /// 6 | public enum TokenType 7 | { 8 | None, 9 | MoveRight, 10 | MoveLeft, 11 | Increment, 12 | Decrement, 13 | StartLoop, 14 | EndLoop, 15 | Input, 16 | Output, 17 | Whitespace, 18 | Comment 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /UwuScript/TokenizationException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace UwuScript 4 | { 5 | /// 6 | /// Represents an error encountered during source tokenization. 7 | /// 8 | public class TokenizationException : Exception 9 | { 10 | /// 11 | /// Gets the line position in the source at which the tokenizer encountered an error. 12 | /// 13 | public int Line { get; private set; } 14 | 15 | /// 16 | /// Gets the column position in the source at which the tokenizer encountered an error. 17 | /// 18 | public int Column { get; private set; } 19 | 20 | /// 21 | /// Initializes a new instance of an error encountered during source tokenization. 22 | /// 23 | /// The message to show. 24 | /// The line position in the source at which the tokenizer encountered an error. 25 | /// The column position in the source at which the tokenizer encountered an error. 26 | public TokenizationException(string message, int line, int column) 27 | : base(message) 28 | { 29 | Line = line; 30 | Column = column; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /UwuScript/Tokenizer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Text.RegularExpressions; 4 | 5 | namespace UwuScript 6 | { 7 | /// 8 | /// An implementation of a string tokenizer. 9 | /// 10 | public class Tokenizer 11 | { 12 | private readonly IList _tokenTemplates; 13 | 14 | /// 15 | /// Initializes a new instance of a string tokenizer. 16 | /// 17 | public Tokenizer() 18 | { 19 | // Load token templates. 20 | _tokenTemplates = new List(); 21 | } 22 | 23 | /// 24 | /// Adds a token template to the tokenizer. 25 | /// 26 | /// The template to add. 27 | public void Add(TokenTemplate template) 28 | { 29 | _tokenTemplates.Add(template); 30 | } 31 | 32 | /// 33 | /// Maps a regular expression to a token type in the tokenizer. 34 | /// 35 | /// The regular expression. 36 | /// The token type. 37 | public void Add(Regex pattern, TokenType type) 38 | { 39 | Add(new TokenTemplate(pattern, type)); 40 | } 41 | 42 | /// 43 | /// Maps a regular expression string to a token type in the tokenizer. 44 | /// 45 | /// The regular expression string. 46 | /// The token type. 47 | public void Add(string pattern, TokenType type) 48 | { 49 | Add(new TokenTemplate(pattern, type)); 50 | } 51 | 52 | /// 53 | /// Gets the current line position in the source. 54 | /// 55 | /// The complete source. 56 | /// The source remaining to tokenize. 57 | /// 58 | private static int GetLinePosition(string source, string remaining) 59 | { 60 | return source.Count(c => c == '\n') - remaining.Count(c => c == '\n') + 1; 61 | } 62 | 63 | /// 64 | /// Gets the current column position in the source. 65 | /// 66 | /// The complete source. 67 | /// The source remaining to tokenize. 68 | /// 69 | private static int GetColumnPosition(string source, string remaining) 70 | { 71 | var processed = source.Substring(0, source.Length - remaining.Length); 72 | return processed.Length - processed.LastIndexOf('\n'); 73 | } 74 | 75 | /// 76 | /// Transforms source code into a list of tokens. 77 | /// 78 | /// The source code to transform. 79 | /// 80 | public IList Tokenize(string source) 81 | { 82 | var tokens = new List(); 83 | 84 | // Tokenize input. 85 | var remaining = source; 86 | while (!string.IsNullOrEmpty(remaining)) 87 | { 88 | // Try to match each template against start of input. 89 | var matches = false; 90 | foreach (var tokenTemplate in _tokenTemplates) 91 | { 92 | var match = tokenTemplate.Pattern.Match(remaining); 93 | if (match.Success && match.Index == 0) 94 | { 95 | // Add token of matching type. 96 | tokens.Add(new Token(tokenTemplate.Type, match.Value, 97 | GetColumnPosition(source, remaining), GetLinePosition(source, remaining))); 98 | 99 | // Trim string from beginning of source. 100 | remaining = tokenTemplate.Pattern.Replace(remaining, string.Empty, 1); 101 | matches = true; 102 | break; 103 | } 104 | } 105 | 106 | // Unexpected character encountered. 107 | if (!matches) 108 | { 109 | var line = GetLinePosition(source, remaining); 110 | var column = GetColumnPosition(source, remaining); 111 | var character = remaining.First(); 112 | throw new TokenizationException($"Unexpected character '{character}' at line {line} column" + 113 | $" {column}.", line, column); 114 | } 115 | } 116 | 117 | return tokens; 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /UwuScript/TuringMachine.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace UwuScript 7 | { 8 | /// 9 | /// Represents a Turing machine. 10 | /// 11 | public class TuringMachine 12 | { 13 | /// 14 | /// The program this interpreter will execute. 15 | /// 16 | private IList program; 17 | 18 | /// 19 | /// The length of the program to execute. 20 | /// 21 | private int length; 22 | 23 | /// 24 | /// The program instruction pointer. 25 | /// 26 | private int programPointer; 27 | 28 | /// 29 | /// The interpreter's memory storage. 30 | /// 31 | private byte[] memory; 32 | 33 | /// 34 | /// The program memory pointer. 35 | /// 36 | private int memoryPointer; 37 | 38 | /// 39 | /// Initialises a new instance of a Turing machine set to execute the specified program. 40 | /// 41 | /// The list of instruction tokens to execute. 42 | public TuringMachine(IList program) 43 | { 44 | // Initialise program and instruction pointer. 45 | this.program = program; 46 | length = this.program.Count; 47 | 48 | // Initialize memory. 49 | memoryPointer = 0; 50 | memory = new byte[32768]; // 32 kilobytes will do. 51 | } 52 | 53 | /// 54 | /// Jumps the program pointer to a matching character. 55 | /// 56 | /// Whether or not to jump the pointer forward. 57 | /// The tokens and their jump values. 58 | private void Jump(bool forward, Dictionary tokens) 59 | { 60 | int l = 1; 61 | while (l > 0) { 62 | programPointer += forward ? 1 : -1; 63 | if (tokens.ContainsKey(program[programPointer].Type)) 64 | { 65 | l += tokens[program[programPointer].Type]; 66 | } 67 | } 68 | } 69 | 70 | /// 71 | /// Runs the loaded program. 72 | /// 73 | public void Run() 74 | { 75 | // Loop through instructions. 76 | while (programPointer < length) { 77 | switch (program[programPointer].Type) { 78 | case TokenType.MoveRight: 79 | // Move to next memory cell. 80 | memoryPointer++; 81 | break; 82 | case TokenType.MoveLeft: 83 | // Move to previous memory cell. 84 | memoryPointer--; 85 | break; 86 | case TokenType.Increment: 87 | // Increment current memory cell. 88 | memory[memoryPointer]++; 89 | break; 90 | case TokenType.Decrement: 91 | // Decrement current memory cell. 92 | memory[memoryPointer]--; 93 | break; 94 | case TokenType.StartLoop: 95 | // If current memory cell is 0, jump past the matching ']' symbol. 96 | Dictionary startJumpValues = new Dictionary 97 | { 98 | { TokenType.StartLoop, 1 }, 99 | { TokenType.EndLoop, -1 } 100 | }; 101 | if (memory[memoryPointer] == 0) { 102 | Jump(true, startJumpValues); 103 | } 104 | break; 105 | case TokenType.EndLoop: 106 | // If current memory cell is not 0, jump back to the matching '[' symbol. 107 | Dictionary endJumpValues = new Dictionary 108 | { 109 | { TokenType.StartLoop, -1 }, 110 | { TokenType.EndLoop, 1 } 111 | }; 112 | if (memory[memoryPointer] != 0) { 113 | Jump(false, endJumpValues); 114 | } 115 | break; 116 | case TokenType.Output: 117 | // Output character. 118 | Console.Write(Encoding.ASCII.GetChars(new byte[] { memory[memoryPointer] })); 119 | break; 120 | case TokenType.Input: 121 | // Read character from input. 122 | memory[memoryPointer] = (byte) Console.Read(); 123 | break; 124 | default: 125 | // Do nothing. 126 | break; 127 | } 128 | programPointer++; 129 | } 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /UwuScript/UwuScript.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {FE4335FE-D922-4659-830D-96A825F9CF33} 8 | Exe 9 | Properties 10 | UwuScript 11 | UwuScript 12 | v4.5.2 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 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | PreserveNewest 59 | 60 | 61 | 62 | 69 | -------------------------------------------------------------------------------- /UwuScript/examples/hello.uwu: -------------------------------------------------------------------------------- 1 | uwu uwu uwu uwu uwu uwu uwu uwu uwu uwu ow< >w> uwu uwu uwu uwu uwu uwu uwu >w> *w* Comment! 2 | uwu uwu uwu uwu uwu uwu uwu uwu uwu uwu >w> uwu uwu uwu >w> uwu wo >w> uwu uwu owo >w> uwu owo uwu uwu uwu uwu uwu uwu uwu owo owo uwu uwu 4 | uwu owo >w> uwu uwu owo w> owo uwu uwu uwu owo nwn nwn nwn nwn nwn nwn owo nwn nwn nwn 6 | nwn nwn nwn nwn nwn owo >w> uwu owo >w> owo *w* Hello world! 7 | -------------------------------------------------------------------------------- /assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lambdacasserole/uwuscript/a96f4448c768a0ef7eb5c2214236297f1b2509e1/assets/logo.png -------------------------------------------------------------------------------- /assets/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 24 | 46 | 48 | 49 | 51 | image/svg+xml 52 | 54 | 55 | 56 | 57 | 58 | 63 | 68 | 73 | 78 | 83 | 88 | 93 | 98 | 103 | 108 | 114 | 120 | 121 | 122 | --------------------------------------------------------------------------------