-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_powershell.py
105 lines (70 loc) · 2.97 KB
/
compile_powershell.py
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
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
#! /usr/bin/python
# Written by Folkert van Heusden
# Released in the public domain
# www.vanheusden.com
import sys
from compile_base import CompileBase
class CompileToPowerShell(CompileBase):
def header(self):
print('Brainfuck to PowerShell compiler.', file=sys.stderr)
def genindent(self, level):
return ' ' * (level * 4)
def get_name():
return ('powershell', 'PowerShell')
def invokeFunction(self, funcNr):
print('%sf%d;' % (self.genindent(self.lindentlevel), funcNr))
def addToDataPtr(self, n, dot, position):
ind = self.genindent(self.lindentlevel)
print('%s$global:data_ptr += %d;' % (ind, n))
def subFromDataPtr(self, n, dot, position):
ind = self.genindent(self.lindentlevel)
print('%s$global:data_ptr -= %d;' % (ind, n))
def addToData(self, n, dot, position):
ind = self.genindent(self.lindentlevel)
print('%s$global:data_mem[$global:data_ptr] += %d;' % (ind, n))
print('%s$global:data_mem[$global:data_ptr] = $global:data_mem[$global:data_ptr] -band 255;' % ind)
def subFromData(self, n, dot, position):
ind = self.genindent(self.lindentlevel)
print('%s$global:data_mem[$global:data_ptr] -= %d;' % (ind, n))
print('%s$global:data_mem[$global:data_ptr] = $global:data_mem[$global:data_ptr] -band 255;' % ind)
def emitCharacter(self, n, dot):
ind = self.genindent(self.lindentlevel)
for i in range(0, n):
print('%sWrite-Host ([char]::ConvertFromUtf32($global:data_mem[$global:data_ptr])) -NoNewLine;' % ind)
def startLoop(self, n, position):
for j in range(0, n):
print('%swhile($global:data_mem[$global:data_ptr] -gt 0) {' % self.genindent(self.lindentlevel))
self.lindentlevel += 1
def finishLoop(self, n, dot, position):
for j in range(0, n):
self.lindentlevel -= 1
print('%s}' % self.genindent(self.lindentlevel))
def addComment(self, s):
print('# %s' % s)
def multilineCommentStart(self):
print('#')
def multilineCommentLine(self, s):
print('# %s' % s)
def multilineCommentEnd(self):
print('#')
def emitProgramBootstrap(self, file):
for i in self.copyrightNotice:
print('# %s' % i)
print('')
print(f'# This is a translation of "{file}".')
print('')
print('$global:data_mem = [int[]]::new(65536);')
print('$global:data_ptr = 0;')
print('')
def emitFunctions(self):
for blkLoop in range(0, len(self.blocks)):
print('function f%d {' % blkLoop)
self.lindentlevel += 1
ind = self.genindent(self.lindentlevel)
self.translate(self.blocks[blkLoop][0], self.blocks[blkLoop][1])
self.lindentlevel -= 1
print('}')
def emitMainFunction(self):
self.lindentlevel += 1
self.translate(0, len(self.allCode))
self.lindentlevel -= 1