Initial release.
[robmyers:dogecode.git] / bin / dcc
1 #!/bin/env python3
2 # -*- mode: python -*-
3
4 # dcc - Compile brainfuck code to a csv file specifying Dogeparty sends
5 # Copyright (C) 2014 Rob Myers rob@robmyers.org
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or 
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 from optparse import OptionParser
21 import sys
22
23 import dogecode.translation
24
25 def getopts():
26     usage = "usage: %prog [options] source_file"
27     parser = OptionParser(usage=usage)
28     parser.add_option("-n", "--nocomment",
29                       action="store_false", dest="strip", default=True,
30                       help="don't skip the first [] as a comment")
31     parser.add_option("-d", "--dontrun",
32                       action="store_false", dest="run", default=True,
33                       help="don't run the code locally to check it executes")
34     parser.add_option("-o", "--outfile", dest="outfilename", default="-",
35                       help="write compiled csv to OUTFILENAME", metavar="FILE")
36     opts, args = parser.parse_args()
37     if(len(args) != 1):
38         parser.error("must supply source_file")
39     return opts, args
40
41 def main():
42     (opts, args) = getopts()
43     outfile = sys.stdout
44     if opts.outfilename != "-":
45         try:
46             outfile = open(opts.outfilename, 'w')
47         except:
48             print("Couldn't open file {} .".format(opts.outfilename))
49             sys.exit(2)
50     strip_comment = opts.strip
51     run_code_locally = opts.run
52     source_filename = args[0]
53     try:
54         source_code = open(source_filename).read()
55     except:
56         print("Couldn't read file {} .".format(source_filename))
57         sys.exit(2)
58     #TODO: check account strings are valid
59     if run_code_locally:
60         #TODO: run the code locally
61         pass
62     dogecode.translation.source_to_tokens_csv(source_code,
63                                               outfile,
64                                               strip_comment)
65
66 if __name__ == "__main__":
67     main()