Skip to content

Commit fd3576b

Browse files
committed
added greyscript version
1 parent e39cef8 commit fd3576b

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed

main.gs

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
//
2+
// Version 1.0
3+
//
4+
5+
// Variables generales
6+
argv = params
7+
ascii = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", " ", "!", "''", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", ""]
8+
9+
// Comprueba que el input sea el correcto
10+
11+
if (argv.len < 1 or argv.len > 2) then
12+
print("\n\nUsage: brainfk [path/to/file.bf] [args]\n")
13+
exit(null)
14+
else if argv[0] == "-h" or argv[0] == "--help" then
15+
print("\n\nRead the README.md for more info.\n")
16+
exit(null)
17+
end if
18+
19+
// Estimacion del inicio del bucle
20+
loop = function(src, pos)
21+
endLoop = 0
22+
while 1
23+
pos = pos - 1
24+
item = src[pos]
25+
if item == "]" then
26+
endLoop = endLoop + 1
27+
else if item == "[" and endLoop != 0 then
28+
endLoop = endLoop - 1
29+
else if item == "[" and endLoop == 0 then
30+
break
31+
end if
32+
end while
33+
return pos
34+
end function
35+
36+
// Busca errores, como bucles incompletos
37+
checkErrors = function(src, argv)
38+
lpStat = 0
39+
clStat = 0
40+
getInp = 0
41+
err = 0
42+
for i in src
43+
if i == "[" then
44+
lpStat = lpStat + 1
45+
else if i == "]" then
46+
lpStat = lpStat - 1
47+
else if i == ">" then
48+
clStat = clStat + 1
49+
else if i == "<" then
50+
clStat = clStat - 1
51+
else if i == "," then
52+
getInp = getInp + 1
53+
end if
54+
end for
55+
56+
if lpStat != 0 then
57+
err = err + 1
58+
end if
59+
60+
if clStat < 0 then
61+
err = err + 2
62+
end if
63+
64+
if err != 0 then
65+
print("Code returned ", err)
66+
exit(null)
67+
end if
68+
end function
69+
70+
// Calcula el numero de celdas necesario para que
71+
// el programa funcione con suficiente espacio + 2
72+
getCells = function(src)
73+
num = 0
74+
cells = 0
75+
for i in source
76+
if i == ">" then
77+
num = num + 1
78+
else if i == "<" then
79+
num = num - 1
80+
end if
81+
if num > cells then
82+
cells = num
83+
end if
84+
end for
85+
return cells + 2
86+
end function
87+
88+
// Le das un numero y, con suerte, devolvera su valor ascii
89+
chr = function(num, ascii)
90+
return ascii[num]
91+
end function
92+
93+
// Le das un ascii y, con suerte, devolvera su valor numerico
94+
ord = function(char, ascii)
95+
for i in range(ascii.len - 1)
96+
if ascii[i] == char then
97+
break
98+
end if
99+
end for
100+
return i
101+
end function
102+
103+
// Hace seguir las reglas de brainfuck de forma estricta
104+
bRules = function(cells, cPos)
105+
if cells[cPos] > 255 then
106+
cells[cPos] = 0
107+
else if cells[cPos] < 0 then
108+
cells[cPos] = 255
109+
end if
110+
return cells[cPos]
111+
end function
112+
113+
// Obtiene el codigo fuente del archivo
114+
file = get_shell.host_computer.File(argv[0])
115+
if file == null then
116+
print("\n\nFile not found\n")
117+
exit(null)
118+
else
119+
source = file.get_content
120+
print("\nSource code:\n" + source)
121+
end if
122+
123+
// Obtiene el input y se lo mete a 'stdin'
124+
if argv.len == 2 then
125+
stdin = argv[1]
126+
print("\nArguments: " + stdin)
127+
end if
128+
129+
// Final de output del codigo fuente / argumentos
130+
print("\n\n")
131+
132+
// Busca errores en el codigo
133+
checkErrors(source, argv)
134+
135+
// Agrega el espacio necesario a 'cells' para funcionar
136+
cells = []
137+
for i in range(getCells(source) + 2)
138+
cells = cells + [0]
139+
end for
140+
141+
// Variables del interpretador
142+
prgPos = 0
143+
cellPos = 0
144+
pArg = 0
145+
146+
// Esto es especifico para GreyScript,
147+
// ya que no se puede hacer print("stuff", end="")
148+
stdout = ""
149+
150+
// Interpretador
151+
while prgPos != source.len
152+
153+
obj = source[prgPos]
154+
155+
cells[cellPos] = bRules(cells, cellPos)
156+
157+
if obj == ">" then
158+
cellPos = cellPos + 1
159+
160+
else if obj == "<" then
161+
cellPos = cellPos - 1
162+
163+
else if obj == "+" then
164+
cells[cellPos] = cells[cellPos] + 1
165+
166+
else if obj == "-" then
167+
cells[cellPos] = cells[cellPos] - 1
168+
169+
else if obj == "." then
170+
stdout = stdout + chr(cells[cellPos], ascii)
171+
172+
else if obj == "," then
173+
cells[cellPos] = ord(stdin[pArg], ascii)
174+
pArg = pArg + 1
175+
176+
else if obj == "]" then
177+
if cells[cellPos] != 0 then
178+
prgPos = loop(source, prgPos)
179+
end if
180+
181+
end if
182+
183+
prgPos = prgPos + 1
184+
end while
185+
186+
// Gracias miniscript
187+
print(stdout)
188+
189+
// Final
190+
print("\n\nCode returned 0")
191+
exit(null)

0 commit comments

Comments
 (0)