-
Notifications
You must be signed in to change notification settings - Fork 0
/
py2beam.py
executable file
·110 lines (97 loc) · 3.85 KB
/
py2beam.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
106
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys, os, shutil
import getopt
from subprocess import call
try:
from py2beam.config import *
from py2beam.frontend.scanner import PyScanner
from py2beam.frontend.parser import PyParser
from py2beam.code_generator.composer import Composer
except:
from src.config import *
from src.frontend.scanner import PyScanner
from src.frontend.parser import PyParser
from src.code_generator.composer import Composer
help_message = '''\
Usage: py2beam.py [options] <source file>
Options:
-o, --output= <file> Place the output into <file>
-e, --execute Executes the compiled beam
-v Output messages about what the compiler is doing
-h, --help Display this information
'''
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
if argv is None:
argv = sys.argv
execute = False
verbose = False
output = None
try:
try:
opts, args = getopt.getopt(argv[1:], "ieho:v", ["help", "output=", "execute", "initialize"])
except getopt.error, msg:
raise Usage(msg)
# option processing
for option, value in opts:
if option == "-v":
verbose = True
if option in ("-h", "--help"):
raise Usage(help_message)
if option in ("-o", "--output"):
output = value
if option in ("-e", "--execute"):
execute = True
if option in ("-i", "--initialize"):
scanner = PyScanner()
parser = PyParser(lexer=scanner)
print "initialized for first use..."
sys.exit(0)
if args == []:
raise Usage(help_message)
for arg in args:
out_path, file_ = os.path.split(arg)
filename, ext = os.path.splitext(file_)
if output:
out_path, out_file = os.path.split(output)
filename, out_ext = os.path.splitext(out_file)
if filename == "":
filename = os.path.splitext(file_)[0]
code = ""
with open(arg, 'r') as source:
for l in source:
code += l
if len(code) > 0:
beam_name = filename
scanner = PyScanner()
# print code
parser = PyParser(lexer=scanner)
print "compiling %s..." % file_
tree = parser.parse(code)
build_dir = os.path.join(out_path, filename)
if not os.path.exists(build_dir) or not os.path.isdir(build_dir):
os.makedirs(build_dir)
for lib in ("base", "common", "builtins"):
# if not os.path.exists(os.path.join(build_dir, "%s.beam" % lib)):
# print "inserting %s lib..." % lib
shutil.copyfile(os.path.join(TARGET, SRC_DIR, CG_DIR, "%s.beam" % lib), os.path.join(build_dir, "%s.beam" % lib))
composer = Composer(build_dir, beam_name, tree, verbose)
composer.generate()
composer.write()
if execute:
print 79 * "#"
print 33*" " + "NOW EXECUTING"
print 79 * "#"
# erl -pa ./ -run prova module -run init stop -noshell
call(["erl", "-pa", build_dir, "-run", beam_name, "module", "-run", "init", "stop", "-noshell"])
print 79 * "#"
except Usage, err:
# print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
print >> sys.stderr, str(err.msg)
# print >> sys.stderr, "\t for help use --help"
return 2
if __name__ == "__main__":
sys.exit(main())