-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogo3d.py
More file actions
44 lines (40 loc) · 1.29 KB
/
logo3d.py
File metadata and controls
44 lines (40 loc) · 1.29 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
import os
import sys
from antlr4 import *
from logo3dLexer import logo3dLexer
from logo3dParser import logo3dParser
from visitor import Visitor
# In order to start the program, we write a new line with a call to
# a procediment specified by the user in the arguments, if not is
# given, by default, the program calls to to the main
if len(sys.argv) == 2:
file_object = open(sys.argv[1], 'a')
file_object.write('\nmain()')
file_object.close()
else:
call = sys.argv[2] + '(' + sys.argv[3]
for i in range(4, len(sys.argv)):
call = call + ','
call = call + sys.argv[i]
call = call + ')'
file_object = open(sys.argv[1], 'a')
file_object.write('\n')
file_object.write(call)
file_object.close()
input_stream = FileStream(sys.argv[1])
lexer = logo3dLexer(input_stream)
token_stream = CommonTokenStream(lexer)
parser = logo3dParser(token_stream)
tree = parser.root()
visitor = Visitor()
visitor.visit(tree)
# Removes the last line from file (the call that we added in the beginning)
with open(sys.argv[1], "r+", encoding="utf-8") as file:
file.seek(0, os.SEEK_END)
pos = file.tell() - 1
while pos > 0 and file.read(1) != "\n":
pos -= 1
file.seek(pos, os.SEEK_SET)
if pos > 0:
file.seek(pos, os.SEEK_SET)
file.truncate()