Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript 实现 Brainscrambler 解释器 #24

Open
sunhengzhe opened this issue May 31, 2018 · 0 comments
Open

javascript 实现 Brainscrambler 解释器 #24

sunhengzhe opened this issue May 31, 2018 · 0 comments

Comments

@sunhengzhe
Copy link
Member

sunhengzhe commented May 31, 2018

Brainscrambler 是一种 EPL(Esoteric programming language), 而你的任务是给 Brainscrambler 设计一个解释器。解释器提供一个 read 方法用来解释语言。

有兴趣可以阅读 那些奇奇怪怪的编程语言

指令

Brainscrambler 有 11 种指令:

  • + 使 current number 加一
  • - 使 current number 减一
  • < 将 current number 移动到左边的栈
  • > 将 current number 移动到右边的栈
  • * 往当前栈中压入一个 0
  • ^ 从当前栈中弹出一个值并丢弃
  • # 循环到下一个栈
  • , 输入一个数字并将该数字压入到当前栈
  • . 输出 current number
  • [ 当 current number 大于 0 时循环此处
  • ] 循环结束

重要概念

  • 所有的栈有一个初始化的值 0
  • 从当前栈弹出时,如果栈为空,则 current number 为 undefined
  • 每次 read 函数被调用时,输出结果应被重置,但栈维持状态
  • 当 . 或 < 或 > 命令被执行但 current number 为 undefined 时,不影响输出结果
  • 当 + 或 - 命令被执行但 current number 为 undefined 时,将 current number 置为 0
  • 循环语句在每次循环结束时判断循环条件
  • Brainscrabler 的内存维护三个栈:A,B 和 C。“当前栈” 从 A 开始,使用 # 命令可以移动到 B,然后移动到 C,然后移动到 A,以此类推

输入

read 方法提供一个参数作为输入,输入的类型为字符串,包含可能出现的命令。

输出

输出为字符串,包含所有被输出的数字。

示例

input: *+.

output: '1'

Test

Test.describe('Fixed Tests', _ => {
  var inter = new Interpreter();
  Test.it("Addition Subtraction Tests", _S => {
    Test.assertEquals(inter.read('*.'),'0','Current number on stack A should be outputted and be 0');
    Test.assertEquals(inter.read('*++.'),'2','');
    Test.assertEquals(inter.read('*+-.'),'0','');
    Test.assertEquals(inter.read('*+*.'),'0','');
    Test.assertEquals(inter.read('*+++-+.'),'3','');
  });
  
  Test.it("Rotation Tests", _S => {
    Test.assertEquals(inter.read('####*.'),'0','');
    Test.assertEquals(inter.read('*.#*+.#*++.#.'),'0120','');
    Test.assertEquals(inter.read('*++>#.'),'2','');
    Test.assertEquals(inter.read('*+++#.'),'3','');
    Test.assertEquals(inter.read('***++-+>#>#.'),'2','');
  });
  
  Test.it("Loop Tests", _A => {
    Test.assertEquals(inter.read(',5[.-]'),'54321','');
    Test.assertEquals(inter.read(',9[.-]'),'987654321','');
    Test.assertEquals(inter.read(',10>*#[-##.+#]'),'0123456789','');
    Test.assertEquals(inter.read(',4>*++#[-##.++#]'),'2468','');
    Test.assertEquals(inter.read(',9[.--]'),'97531','');
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant