天图

· · 个人记录

在代码中,总有一些奇葩,比如以下两种语言

Asciidots & Befunge

下面就来讲讲这些语言

Befunge

一个恶心而有艺术感的语言

你无法相信这是一个随机数生成器代码

 v>>>>>v
  12345
  ^?^
 > ? ?^
  v?v
  6789
  >>>> v
 ^    .<

感觉超级懵逼?

没错啦!

没学过的人当然会懵逼

之前看不懂的代码是天书,这次怎么变天图了?

来来来,莫惊慌

指针?

由于这是个二维代码,就需要代码指针疯狂的走图(利用一个指针将代码扫过一遍运行)。在这里,我们假设有一个沙雕指针po走图,这时,指针符派上了用场

> 让po往右走
< 让po往左走
^ 让po往上走
v 让po往下走
? 让po随机走
_ 在栈中弹出a,若a=0则往右,否则往左
| 同上,但a=0时往下走,否则往上走

当然,整个代码从左上角开始,且po一开始都是往右走的

运算?

当然,加减这些东西是不能少的

假设现在从栈(又是它!)中依次弹出a,b,那么将下面的结果压入栈:

+   a+b
-   b-a
*   a*b
/   b/a(四舍五入到个位数)
%   b%a(不懂取模的出门左转OK?)

比较?

那个,比较是不可少的

! 弹出a,若它不是0压入1,否则压入0
` 依次弹出a,b,若b>a压入1,否则压入0

丢入?

不把东西丢进栈里你还能计算?

"" 中间是字符串,进入了"就按方向依次收集字符并将ASCII压入栈,到另一个"为止
0-9 识别数字,只识别一个就将值压入栈

栈顶?

为栈顶元素奋斗

: 将栈顶元素复制一份丢又到栈顶
\ 交换栈顶的两个元素
$ 弹出栈顶元素后直接不管它

修改?

如何修改代码(惊)?

这里假设代码长这样

0,0   0,1   0,2
1,0   1,1   1,2
2,0   2,1   2,2

之后

p 依次弹出y,x,v后将代码x,y改为字符v
g 依次弹出y,x后压入代码x,y的字符

IO?

输入输出是必须的

. 输出栈顶元素的值并弹出
, 输出栈顶元素所对应的ASCII字符并弹出
& 输入一个整数并压入栈
~ 输入一个字符并将ASCII值压入栈

其他?

#和 (<<这是个空格) 按现在的方向走,不回头~
@ 结束程序吧!程序猿:什么?

例子?

先看看HW

 >25*"!dlrow ,olleH":v
                  v:,_@
                  >  ^

首先,将2,5压入栈后相乘得到'\n'

之后呢,将!dlrow ,olleH装入栈,这时你的栈就有了一个反着的HW

复制H后进入_,此时栈如下

H
---
H
---
e
---
l
---
l
---
o
---
,
---
(空格)
---
w
---
o
---
r
---
l
---
d
---
!
---
\n

之后只要顶上那个家伙不是0,就弹出一个值(_的比较)

之后再弹出一个值并输出ASCII字符

于是这一个字符就被输出和清空了

然后,再将下一个字符复制一遍,重复循环,就可以输出

Hello, world!

Asciidots

又是一个神仙天图

实际上

Asciidots长得和Befunge差不多

/#$<.
*-[+]
\#1/

但

为什么多出了[]?

这个语言相当于一个管道系统,每点水都是一个入水口,到一个地方,这些复杂的水经过处理出去了

开始讲语言吧

在Asciidots中,用.作为“入水口”,&作为“出水口”

每个点一开始有一个方向,ID和值,一开始都是0,这些之后再谈

注释是``后跟着的一些东西

却说我们需要一些“管道”让“水”流,于是便有了

- 左右联通
| 上下联通
/和\ 一个非常像平面镜的东西,例如
/-
|
它是沟通的
同时,还有
 |
-/-
 |
+ 十字路口,不改变方向

练习1

/-&         `` This is where the program ends!
|
\-\ /-\
  | | |
/-/ | \-\
\---/   |
        |
        \-. `` Here's where the program starts

讲讲方向键

<>^v

分别代表四个方向

只要不是逆流,这些水都会变为顺着箭头方向走

.-<-&

这一段代码可以通过(逆向不受影响)

当然,还有括号等

( 只要有水朝左冲过来,就会被影响为往右冲,就像一个蹦床

) 一样的道理

* 只要有水冲进来,就会朝另三个方向冲出去

就像(一个比喻)

 v
<*>
 v

练习2

  /-\ /-& `` End
  | | |
  \-+-v
    | | /-\
(-<-/ | | |
  |   \-<-/
  \-\
    |
    .    `` Start

说说之前所说的ID和数字

ID可以区分每个点,而数字便是这股水的一个信息

一般上,用@变ID,用#变数字

比如

.-@7-#2-&

值有覆盖性,就像

.-#1#2-&

此时水的信息为2

练习3

.-#7-#0-@278-#17-#8-@4-#0-@99-#1-#13-&

当然,与外界的交流是必须的

用$输出

比如,一股水流进了$,

如果接下来的符号是#

就将打印它的值并回车

如果是"或'

就表明接下来是字符串

按方向一个一个输出,到结束后输出回车

当然,你可以用$_取消输出回车

和$a#输出对应的ASCII字符

?是输入

外界将输入一个整数值,这个?将变成那个值

比如

.-#?-&

读入了3就表示

.-#3-&

练习4

.-$"Hey!"-$_"Here we go : "-#?-$#-&

这里有一个非常像变压器的字符

~ 只要在上(下)方等待的数字不为0,那么水平方向上的水将会往下(上)流 在竖直方向的水中,只要在经过~时加上!,效果将恰恰相反

练习5

  /-$"The value is not equal to zero"
  |
.-~-$"The value is equal to zero"
  |
  ?
  #
  |
  .

啊!谈到计算了!

计算是用[]或{}包起来的

比如

 |
[/]-a
 |
 b

假设从右边来的数为a,从下(上)来的数为b,则这里将往上(下)返回b/a

类似的道理,以下是计算一览表,一些返回bool型的将会变为1和0

+ - * / %   基本运算
^ 指数
& 和
o 或
x 异或
> 大于
G 大等于
< 小于
L 小等于
= 相等
! 不等
a-{/}-
   |
   b

{}类似,只是它是计算a/b并水平弹出

练习6

`` Simple subtraction:
``   (3 - 2 = 1)

   #
   $
   |
  [-]-2#-.
   |
   3
   #
   |
   .

.-#?-{+}-$#
      |
.-#?--/

过滤水?

: 删除经过它的值为0的水
; 同理,但过滤的是非0数

如果在它们前面加上@,就表示用id为过滤项

当然,函数什么的也有

用%$定义函数

用一个非正式字符表示

函数以自己的字符开始,以自己的字符(绕了一圈)或走不下去(结束程序)结束

函数就像一个传送带,将水按同样的方向、ID和值进去后按函数最后绕到原字符的方向冲出

练习7

%$A

#  /-)
$  |
\>-A
 \-3#-.

A-\
\-/

当然,还有库支持,这里不允与解释

Libraries
Dots supports libraries! A library is a program that defines a character (usually a letter).

Using Libraries
A library can be imported by starting a line with %!, followed with the file name, followed with a single space and then the character that the library defines.

By default, all copies of the character to lead to the same (singleton) library code. This can cause some unexpected behavior if the library returns an old dot, since that old dot will come out of the char that it came from.

Here’s an example of importing the standard for_in_range library (located in the libs folder) as the character f:

%!for_in_range.dots f
The way to use a library varies. Inputs and outputs of the library are through the alias character.

For the for_in_range library, the inputs are defined as follows:

The dot coming from the left side sets the starting value of the counter
The dot coming from the right sets the end value of the Counter
And the outputs are as follows:

A dot for each number within the range defined by the inputs is output from the top
When the loop is complete (the end value has been reached), a dot is output from the bottom
Here is an example of outputting all the numbers between 1 and 100 to the console, then stopping the program:

%!for_in_range.dots f

         #
         $
         |
.-*-#1---f-\
  \-#100-+-/
         |
         &
Creating Libraries
Each library defined a character that will act as a warp to & from the library.

That can be done like so:

%$X `` X could be replaced with a different character, if so desired
It is recommended that you create warps for different sids of the char. Just look at the example code for the val_to_addr.dots library:

%^X
%$AB

B-X-A


A-*----@{+}-#0-B
  |      |
  \------/
Location of Library Source Files
The source files for libraries are searched for in the following directories (in order):

The directory of the asciidots file being interpreted
The implementation’s dots/libs/ directory
The implementation’s libs/ directory (for backwards compatibility)
Interpretation
Each tick, the dots will travel along the lines until they hit a charter that acts as a function of multiple dots (i.e. an operation character or a ~ character). The dot will stop if it goes on a path that it has already traversed in the same tick

Due to the fact that dots may be moving backwards down a line, if a number or system value (e.g. ?) is seen without a preceding @ or #, it will be ignored, along with any @ or # immediately thereafter

好了,基本上就是这样了

尝试打一下代码吧~