标签:
1)Folders是一种以文件目录结构来编码的语言,它的编译器是一个C#程序,该C#编译程序通过将目录结构转换为C#代码,再调用C#编译器来实现编译。
2)Folders的详细介绍可以参考这篇资讯:http://www.oschina.net/news/60232/programming-language-folders
3)它翻译自这个页面:http://esoteric.codes/post/109310541818/folders-a-language-with-no-files
4)Folders语言的代码托管地址:https://github.com/rottytooth/Folders
本博客写于2015年3月9日,文中所列举的编译程序源码和内容都以此时间为准
详细的语法规则可言参考(一)中的链接。
Folders可以用来打印文字,它有6个命令和4种数据类型。按文件夹名称升序执行考察各个文件夹,并根据读取的文件夹名称信息,将目录树的内容转换成C#代码。翻阅程序源码,在程序集“Rottytooth.Esolang.Folders.Runtime”下的文件“SpecialSymbols.cs”中,可以找到下面两个代码规则:
1)6个命令和4种数据类型,文件夹命名,对于这个字典来说,命名成键或命名成值都是等价的。用前后两种方式写的文件夹名称都会被识别。比如有一个文件夹名为“Downloads”,那编译时就会按“let”的规则处理,即赋值。
public static readonly Dictionary<string, string> AltCommandNames =
new Dictionary<string, string>()
{
{"New folder", "if"},
{"Temp", "while"},
{"Images", "declare"},
{"Downloads", "let"},
{"Setup", "print"},
{"Logs", "input"},
{"Vaction photos", "int"},
{"Lang", "float"},
{"Img", "string"},
{"User", "double"}
};
这段代码定义了6个命令和4种数据类型的别名,需要注意的是:在网站的介绍页面上,int的别名是“Vacation photos”,但从代码来看,int的别名是“Vaction photos”,单词的字母不一样!这是一个很严重的陷阱,在官方给出例子“99 Bottles of Beer”中,使用的是int这种用法,很显然官方目前并没有发现“vacation”被错写成了“Vaction”。
2)转义字符,前者会在编译时被替换成后者。注意,C语言里的转义字符,除了“\n”为“&lf;”外,其他都是将转义字符中的“\”用“%5C;”代替,如“%5C;t”代表“\t”、“%5C;b”代表“\b”。
private static readonly Dictionary<string, string> SymbolList =
new Dictionary<string, string>()
{
{"%2F;", "/"},
{"%5C;", "\\"},
{"%2A;", "*"},
{"<", "<"},
{">", ">"},
{"%20;", " "},
{"&lf;", "\\n"},
{"%2E;", "."}
};
从github上下载的代码,经过编译后,会生成一个可执行文件“Folders.exe”和一个动态链接库文件“Rottytooth.Esolang.Folders.Runtime.dll”,使用“Folders.exe”可以把指定的目录作为参数,编译出一个新的exe文件。“Hello, World!”是官方提供的最简单的程序示例,下图中的例子,先用tree命令显示了它的目录结构,再用Folders.exe对之进行编译。

编译完毕后,当前界面会出现两个新文件:“Hello World.exe”和“Hello World.pdb”,运行exe文件就可以看到“Hello, World!”字符了。
使用.NET自带的反编译工具ildasm将上面程序反编译成MSIL代码后可以看到,这个程序是一个标准的.NET程序:

被转化成的等价C#代码为:
using System;
using System.IO;
using Rottytooth.Esolang.Folders.Runtime;
public static class Program
{
private static VarManager _varManager;
static Program()
{
_varManager = new VarManager("Hello World");
}
public static void Main(string[] args)
{
Console.Write("Hello".ToString() + ", ".ToString() + "World".ToString() + "!".ToString());
}
}
示例2建立了一个字符串(Img,即string)变量bigguy,并通过连接符“+”把3段文字(Img)拼接到一起并赋值(Downloads,即declare),最后用打印命令(Setup,即print)输出这段文字。目录结构和运行结果如下:

转换后的代码如下:
using System;
using System.IO;
using Rottytooth.Esolang.Folders.Runtime;
public static class Program
{
private static VarManager _varManager;
static Program()
{
_varManager = new VarManager("Long String");
}
public static string bigguy
{
get
{
return (string)_varManager.GetVariable("bigguy");
}
set
{
_varManager.SetVariable("bigguy", value);
}
}
public static void Main(string[] args)
{
bigguy = "Call me Ishmael. Some years ago - never mind how long precisely - having little or no mon";
bigguy = bigguy + "ey in my purse, and nothing particular to interest me on shor";
bigguy = bigguy + "e, I thought I would sail about a little and see the watery part of the world";
Console.Write(bigguy.ToString());
}
}
创立一个执行99次的循环,目录“99 Bottles”的结构如下:

这段代码先声明了一个整型变量bottles赋值为99,然后将它放入到while循环中,每次循环它的值减1,当bottles值不再大于1时退出循环,再进行后面的步骤。
程序转换后的C#代码如下:
using System;
using System.IO;
using Rottytooth.Esolang.Folders.Runtime;
public static class Program
{
private static VarManager _varManager;
static Program()
{
_varManager = new VarManager("99 Bottles");
}
public static int bottles
{
get
{
return (int)_varManager.GetVariable("bottles");
}
set
{
_varManager.SetVariable("bottles", value);
}
}
public static void Main(string[] args)
{
bottles = 99;
Console.Write("99 bottles of beer on the wall, 99 bottles of beer.\n".ToString() + "Take one down, pass it around, ".ToString());
while (bottles > 1)
{
bottles = bottles - 1;
Console.Write(bottles.ToString() + " bottles of beer on the wall.\n\n".ToString() + bottles.ToString() + " bottles of beer on the wall, ".ToString() + bottles.ToString() + " bottles of beer.\nTake one down, pass it around, ".ToString());
}
Console.Write("1 bottle of beer on the wall.\n\n".ToString() + "1 bottle of beer on the wall, 1 bottle of beer.\n".ToString() + "Take one down, pass it around, no more bottles of beer on the wall.".ToString());
}
}
打印九九乘法表,我声明了3个变量:i、j、k,k用来存储每次迭代后i*j的值
目录结构为:
文件夹 PATH 列表 卷序列号为 00000002 BE8F:677A C:\USERS\TSYBIUS\DESKTOP\9-9-TABLE ├─001 declare │ ├─001 i │ └─002 int ├─002 let │ ├─001 i │ └─1 ├─003 declare │ ├─001 j │ └─002 int ├─004 let │ ├─001 j │ └─1 ├─005 declare │ ├─001 k │ └─002 int ├─006 let │ ├─001 k │ └─1 └─007 while ├─001 i < 10 ├─002 let │ ├─001 j │ └─1 ├─003 while │ ├─001 j < i + 1 │ ├─002 let │ │ ├─001 k │ │ └─002 i %2A; j │ ├─003 print │ │ ├─001 i │ │ ├─002 string │ │ │ └─%20;%2A;%20; │ │ ├─003 j │ │ ├─004 string │ │ │ └─%20;=%20; │ │ ├─005 k │ │ └─006 string │ │ └─%5C;t │ └─099 let │ ├─001 j │ └─002 j + 1 ├─004 print │ └─001 string │ └─&lf; └─099 let ├─001 i └─002 i + 1
转换成的C#代码为
using System;
using System.IO;
using Rottytooth.Esolang.Folders.Runtime;
public static class Program
{
private static VarManager _varManager;
static Program()
{
_varManager = new VarManager("9-9-table");
}
public static int i
{
get
{
return (int)_varManager.GetVariable("i");
}
set
{
_varManager.SetVariable("i", value);
}
}
public static int j
{
get
{
return (int)_varManager.GetVariable("j");
}
set
{
_varManager.SetVariable("j", value);
}
}
public static int k
{
get
{
return (int)_varManager.GetVariable("k");
}
set
{
_varManager.SetVariable("k", value);
}
}
public static void Main(string[] args)
{
i = 1;
j = 1;
k = 1;
while (i < 10)
{
j = 1;
while (j < i + 1)
{
k = i * j;
Console.Write(
i.ToString() + " * ".ToString() +
j.ToString() + " = ".ToString() +
k.ToString() + "\t".ToString());
j = j + 1;
}
Console.Write("\n".ToString());
i = i + 1;
}
}
}
运行结果为:

1)最好先用C#代码实现一边想要的效果,将C#代码化减到分支结构仅使用if,循环结构仅使用while
2)创建新文件夹时要有一套自己的编号方式,防止语句顺序出现混乱的情况
END
标签:
原文地址:http://my.oschina.net/Tsybius2014/blog/384518