Skip to content

Commit e068cad

Browse files
committed
[Init]初始化完成膜蛤脚本解释器
0 parents  commit e068cad

6 files changed

Lines changed: 379 additions & 0 deletions

File tree

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# MoHaScript
2+
### 简介
3+
这是一个暴力膜蛤脚本语言的实现
4+
> - 换皮[Brainfxxk](https://github.com/topics/brainfuck),对每个操作进行蛤化翻译
5+
> - 使用Golang实现膜蛤大法,以其作为脚本的解释器
6+
7+
### 帮助
8+
可使用 -h 命令行参数对MoHaScript的解释器参数说明进行打印,效果如下:
9+
~~~
10+
Usage of moha:
11+
-f string
12+
放膜蛤脚本的路径是坠吼的!
13+
-h 长者的指点
14+
~~~
15+
可使用 -f <FILE_PATH> 命令,对膜蛤脚本的路径进行指定。
16+
17+
### 字符标识
18+
仅支持八个命令:
19+
20+
| 标识 | 含义 |
21+
| :---: | :---: |
22+
|| 观察长者的时候,你的时间流逝会变快,所以你的⌚的"指针",会前移 |
23+
| 👓️ | 当你戴上👓时,会受到万民敬仰,你本体的时间会变慢,所以你的"指针",会后移 |
24+
|| 不要说话,➕1s |
25+
| 📹️ | 香港记者的摄像机,Too young,too simple,会让你-1s |
26+
| 🎸 | 当你弹起夏威夷吉他,长者的教导就会输入你的内心,你可以得到输入 |
27+
| 🤓 | 看到这个表情,就如同见到了长者,这个表情在疯狂"输出"|
28+
| 🧙 | 膜法师开始不断的吟唱咒语,开启了膜蛤新世界的大门 |
29+
| 🐶 | 咒语的内容总是以🐶为标志,没有人知道为什么 |
30+
31+
### 例子
32+
[输出膜法师的Slogan](demo/slogan.moha)
33+
34+
35+
### 下载
36+
请在release中下载最新版本或编译安装

demo/slogan.moha

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
🧙📹️🐶 ⌚ 🧙📹️🐶 ⌚ 🧙📹️🐶
2+
👓️👓️ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕
3+
🧙
4+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕
5+
🧙
6+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕ 👓️📹️
7+
🐶
8+
👓️📹️
9+
🐶
10+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕
11+
🧙
12+
⌚ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 📹️📹️📹️📹️📹️📹️📹️ 👓️📹️
13+
🐶
14+
⌚🤓
15+
16+
👓️👓️ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕
17+
🧙
18+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕
19+
🧙
20+
⌚ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 📹️📹️📹️ 👓️📹️
21+
🐶
22+
👓️📹️
23+
🐶
24+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕
25+
🧙
26+
⌚ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 📹️📹️📹️📹️📹️📹️📹️ 👓️📹️
27+
🐶
28+
⌚ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 📹️📹️📹️📹️🤓
29+
30+
👓️ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕
31+
🧙
32+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕ 👓️📹️
33+
🐶
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+
👓️📹️
59+
🐶
60+
⌚ ➕➕➕➕➕➕➕➕
61+
🧙
62+
⌚ ➕➕➕➕➕➕➕➕ 👓️📹️
63+
🐶
64+
⌚ ➕➕➕🤓
65+
66+
👓️👓️ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕
67+
🧙
68+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕
69+
🧙
70+
⌚ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 👓️📹️
71+
🐶
72+
👓️📹️
73+
🐶
74+
⌚ ➕➕➕➕➕➕➕➕➕➕
75+
🧙
76+
⌚ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 👓️📹️
77+
🐶
78+
⌚ ➕➕🤓
79+
80+
// 转至 index 3,4 号Memory,输出逗号与空格
81+
⌚ 🧙📹️🐶 ➕➕➕➕➕➕
82+
⌚ 🧙📹️🐶 👓️ 🧙⌚ ➕➕➕➕➕➕➕ 👓️📹️🐶
83+
⌚ ➕➕🤓 🧙📹️🐶 👓️ ➕➕➕➕➕
84+
🧙⌚ ➕➕➕➕ 👓️📹️🐶 ⌚🤓
85+
👓️👓️
86+
87+
👓️👓️ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕
88+
🧙
89+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕
90+
🧙
91+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕ 👓️📹️
92+
🐶
93+
👓️📹️
94+
🐶
95+
⌚ ➕➕➕➕➕➕➕➕➕➕
96+
🧙
97+
⌚ ➕➕➕➕➕➕➕➕➕➕ 👓️📹️
98+
🐶
99+
⌚ ➕➕➕➕➕➕➕➕➕➕🤓
100+
101+
👓️👓️ ➕➕➕➕➕➕➕➕➕➕ ➕
102+
🧙
103+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕
104+
🧙
105+
⌚ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 📹️📹️ 👓️📹️
106+
🐶
107+
👓️📹️
108+
🐶
109+
⌚⌚ ➕➕➕➕➕➕➕➕➕➕🤓
110+
111+
👓️👓️ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕
112+
🧙
113+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕
114+
🧙
115+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕ 👓️📹️
116+
🐶
117+
👓️📹️
118+
🐶
119+
⌚ ➕➕➕➕➕➕➕
120+
🧙
121+
⌚ ➕➕➕➕➕➕➕➕ 👓️📹️
122+
🐶
123+
⌚🤓
124+
125+
➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕🤓
126+
127+
👓️👓️ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕
128+
🧙
129+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕
130+
🧙
131+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕ 👓️📹️
132+
🐶
133+
👓️📹️
134+
🐶
135+
⌚ ➕➕➕➕➕
136+
🧙
137+
⌚ ➕➕➕➕➕➕➕➕ 👓️📹️
138+
🐶
139+
⌚🤓
140+
141+
👓️ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕
142+
🧙
143+
⌚ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 📹️📹️📹️📹️📹️📹️📹️📹️ 👓️📹️
144+
🐶
145+
⌚🤓
146+
147+
👓️👓️ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕
148+
🧙
149+
⌚ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕➕➕➕➕➕ ➕➕➕➕➕
150+
🧙
151+
⌚ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 📹️📹️📹️📹️📹️📹️📹️📹️📹️📹️ 📹️📹️📹️📹️📹️📹️ 👓️📹️
152+
🐶
153+
👓️📹️
154+
🐶
155+
⌚ ➕➕➕➕➕➕➕➕➕➕
156+
🧙
157+
⌚ ➕➕➕➕➕➕ 👓️📹️
158+
🐶
159+
⌚ 📹️📹️🤓
160+
161+
// 转至 index 3,4 号Memory,输出问号
162+
⌚ 🧙📹️🐶 ⌚ 🧙📹️🐶 👓 ➕➕➕➕➕➕➕➕
163+
🧙
164+
⌚ ➕➕➕➕➕➕➕➕ 👓️📹️
165+
🐶
166+
⌚ ️📹️🤓

interpreter.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
func StartInterpreter (script string) {
4+
// 数据域
5+
memory := NewMemory()
6+
cmdList := []rune(string(script))
7+
var loopMark []int
8+
9+
for cmdIndex := 0; cmdIndex < len(cmdList); cmdIndex ++ {
10+
cmdItem := cmdList[cmdIndex]
11+
12+
if runeEquals(cmdItem, "⌚") {
13+
memory.ToRight()
14+
} else if runeEquals(cmdItem, "👓️") {
15+
memory.ToLeft()
16+
} else if runeEquals(cmdItem, "➕") {
17+
memory.Add()
18+
} else if runeEquals(cmdItem, "📹️") {
19+
memory.Minus()
20+
} else if runeEquals(cmdItem, "🎸") {
21+
memory.ReadNum()
22+
} else if runeEquals(cmdItem, "🤓") {
23+
memory.PrintChar()
24+
} else if runeEquals(cmdItem, "🧙") {
25+
if memory.Get() != 0 {
26+
loopMark = append(loopMark, cmdIndex)
27+
} else {
28+
nextCloseLoopIndex := findNextLoopClose(cmdList, cmdIndex, "🐶")
29+
if nextCloseLoopIndex < 0 {
30+
panic("[Error] 做膜法师🧙的人,必然需要言之有🐶啊,你缺🐶了知道吗?")
31+
} else {
32+
cmdIndex = nextCloseLoopIndex
33+
}
34+
}
35+
} else if runeEquals(cmdItem, "🐶") {
36+
if memory.Get() != 0 {
37+
cmdIndex = loopMark[len(loopMark) - 1]
38+
} else {
39+
loopMark = loopMark[:len(loopMark) - 1]
40+
}
41+
}
42+
}
43+
}
44+
45+
func findNextLoopClose(cmdList []rune, pCurrent int, closeMark string) int {
46+
for i := pCurrent; i < len(cmdList); i++ {
47+
if runeEquals(cmdList[i], closeMark) {
48+
return i
49+
}
50+
}
51+
return -1
52+
}
53+
54+
func runeEquals (runeChar rune, runeStr string) bool {
55+
return runeChar == []rune(runeStr)[0]
56+
}

main.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"io/ioutil"
6+
"os"
7+
"strings"
8+
)
9+
10+
func main () {
11+
// ⌚👓️➕📹🤓️🎸🧙🐶
12+
script, err := ioutil.ReadFile(parseConf())
13+
strScript := strings.ReplaceAll(string(script), "\n", "")
14+
strScript = strings.ReplaceAll(strScript, " ", "")
15+
16+
if err != nil {
17+
panic("[Error] 长者对你的脚本路径很不满意, 你的生命减少了一秒钟……")
18+
}
19+
20+
StartInterpreter(string(script))
21+
}
22+
23+
func parseConf () string {
24+
pHelpFlag := flag.Bool("h", false, "长者的指点")
25+
pFilePath := flag.String("f", "", "放膜蛤脚本的路径是坠吼的!")
26+
flag.Parse()
27+
28+
if *pHelpFlag {
29+
flag.Usage()
30+
os.Exit(0)
31+
}
32+
33+
return *pFilePath
34+
}

memory.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
type Memory struct {
12+
data []int64
13+
p int
14+
}
15+
16+
func NewMemory () Memory {
17+
return Memory{
18+
data: make([]int64, 10),
19+
p: 0,
20+
}
21+
}
22+
23+
func (memory *Memory) Get () int64 {
24+
return memory.data[memory.p]
25+
}
26+
27+
func (memory *Memory) Add () {
28+
memory.data[memory.p] ++
29+
}
30+
31+
func (memory *Memory) Minus () {
32+
memory.data[memory.p] --
33+
}
34+
35+
// 自动扩充右边界
36+
func (memory *Memory) ToRight () {
37+
memory.p ++
38+
if memory.p >= len(memory.data) {
39+
memory.data = append(memory.data, int64(0))
40+
}
41+
}
42+
43+
// 左边触底,到达最右端
44+
func (memory *Memory) ToLeft () {
45+
if memory.p == 0 {
46+
memory.p = len(memory.data) - 1
47+
} else {
48+
memory.p --
49+
}
50+
}
51+
52+
func (memory *Memory) PrintChar () {
53+
fmt.Print(string(rune(memory.data[memory.p])))
54+
}
55+
56+
func (memory *Memory) ReadNum () {
57+
reader := bufio.NewReader(os.Stdin)
58+
input, err := reader.ReadString('\n')
59+
60+
fmt.Print(input)
61+
62+
if err != nil {
63+
panic("[Error] 蛤???你的输入Too young,too simple...")
64+
}
65+
66+
inputNum, err := strconv.Atoi(strings.Trim(input, "\n"))
67+
if err != nil {
68+
panic("[Error] 蛤???你的输入Sometimes naïve")
69+
}
70+
71+
memory.data[memory.p] = int64(inputNum)
72+
}

unit_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestOutput(t *testing.T) {
9+
int64Array := []int64{ 33503, 21033, 22269, 23478, 29983, 27515, 20197, 44, 20, 23682, 22240, 31096, 31119, 36991, 36235, 20043, 63 }
10+
var runeArray []rune
11+
for i := 0; i < len(int64Array); i++ {
12+
runeArray = append(runeArray, rune(int64Array[i]))
13+
}
14+
fmt.Println(string(runeArray))
15+
}

0 commit comments

Comments
 (0)