Created
August 5, 2009 04:19
-
-
Save ricochet1k/162502 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This is a basic interpreter of the language Swap. | |
* Link: http://esolangs.org/wiki/Swap | |
* Author: revcompgeek | |
*/ | |
module swap; | |
import tango.io.Console; | |
import tango.io.Stdout; | |
import tango.io.device.File; | |
void main(char[][] args){ | |
if(args.length < 2){ | |
Stdout("Swap language interpreter").newline; | |
Stdout("http://esolangs.org/wiki/Swap").newline; | |
Stdout.format("Usage: {} filename", args[0]).newline; | |
return; | |
} | |
char[] data = cast(char[])File.get(args[1]); | |
uint locate(char[] str, uint i=0){ | |
while(i < data.length-str.length+1 && data[i..i+str.length] != str) | |
i += (data[i] == '\\')? 2:1; | |
if(i >= data.length-str.length+1) return data.length; | |
return i; | |
} | |
char[] consume(){ | |
char[] txt = ""; | |
uint i = 0; | |
while(i < data.length){ | |
if(data[i] == '\\'){ | |
txt ~= data[0..i] ~ data[i+1]; | |
data = data[i+2..$]; | |
i=0; continue; | |
} | |
if(data[i] == '~'){ | |
txt ~= data[0..i]; | |
data = data[i+1..$]; | |
return txt; | |
} | |
i++; | |
} | |
txt ~= data; | |
data = ""; | |
return txt; | |
} | |
while(data.length){ | |
Stdout(consume()).flush; | |
if(!data.length) continue; | |
char[] string1 = consume(); | |
char[] string2 = consume(); | |
//Char input | |
if(string1.length == 0 && string2.length == 0){ | |
char[] text = consume(); | |
char[] c = new char[1]; | |
Cin.input.read(c); | |
//Scan for text and replace with c | |
uint i = locate(text,0); | |
while(i < data.length){ | |
data = data[0..i] ~ c ~ data[i+text.length..$]; | |
i = locate(text,i + c.length); | |
} | |
continue; | |
} | |
// Swap construct | |
if(string1 == string2){ | |
uint code1start = 0; | |
uint code1end = locate(string1,code1start); | |
if(code1end == data.length) continue; // no string1 found | |
uint code2start = code1end + string1.length; | |
uint code2end = locate(string1,code2start); | |
if(code2end == data.length) { //1|2 -> 2|1 | |
data = data[code2start..code2end] ~ string1 ~ data[code1start..code1end]; | |
continue; | |
} | |
uint code3start = code2end + string1.length; | |
uint code3end = locate(string1,code3start); | |
if(code3end == data.length) { //1|2|3 -> 3|2|1 | |
data = data[code3start..code3end] ~ string1 ~ data[code2start..code2end] ~ string1 ~ data[code1start..code1end]; | |
continue; | |
} | |
uint code4start = code3end + string1.length; | |
uint code4end = locate(string1,code4start); | |
if(code4end == data.length || true) { | |
//1|2|3|4 -> 1|3|2|4 | |
data = data[code1start..code1end] ~ string1 ~ data[code3start..code3end] ~ string1 ~ data[code2start..code2end] ~ string1 ~ data[code4start..$]; | |
continue; | |
} | |
} | |
//Swap occurences of string1 and string2 in data | |
uint i = 0; | |
while(i < data.length){ | |
if(string1.length && i + string1.length < data.length+1 && data[i..i+string1.length] == string1) | |
data = data[0..i] ~ string2 ~ data[i+string1.length..$]; | |
else if(string2.length && i + string2.length < data.length+1 && data[i..i+string2.length] == string2) | |
data = data[0..i] ~ string1 ~ data[i+string2.length..$]; | |
i++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment