forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogramSerializer.py
More file actions
64 lines (47 loc) · 1.47 KB
/
Copy pathprogramSerializer.py
File metadata and controls
64 lines (47 loc) · 1.47 KB
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
#!/usr/bin/env python
# helper for building C program source text
from compilationException import *
class ProgramSerializer(object):
def __init__(self):
self.program = ""
self.eol = "\n"
self.currentIndent = 0
self.INDENT_AMOUNT = 4 # default indent amount
def __str__(self):
return self.program
def increaseIndent(self):
self.currentIndent += self.INDENT_AMOUNT
def decreaseIndent(self):
self.currentIndent -= self.INDENT_AMOUNT
if self.currentIndent < 0:
raise CompilationException(True, "Negative indentation level")
def toString(self):
return self.program
def space(self):
self.append(" ")
def newline(self):
self.program += self.eol
def endOfStatement(self, addNewline):
self.append(";")
if addNewline:
self.newline()
def append(self, string):
self.program += str(string)
def appendFormat(self, format, *args):
string = format.format(*args)
self.append(string)
def appendLine(self, string):
self.append(string)
self.newline()
def emitIndent(self):
self.program += " " * self.currentIndent
def blockStart(self):
self.append("{")
self.newline()
self.increaseIndent()
def blockEnd(self, addNewline):
self.decreaseIndent()
self.emitIndent()
self.append("}")
if addNewline:
self.newline()