Ton

Форк
0
/
func-main.cpp 
129 строк · 4.6 Кб
1
/*
2
    This file is part of TON Blockchain source code.
3

4
    TON Blockchain is free software; you can redistribute it and/or
5
    modify it under the terms of the GNU General Public License
6
    as published by the Free Software Foundation; either version 2
7
    of the License, or (at your option) any later version.
8

9
    TON Blockchain is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    GNU General Public License for more details.
13

14
    You should have received a copy of the GNU General Public License
15
    along with TON Blockchain.  If not, see <http://www.gnu.org/licenses/>.
16

17
    In addition, as a special exception, the copyright holders give permission
18
    to link the code of portions of this program with the OpenSSL library.
19
    You must obey the GNU General Public License in all respects for all
20
    of the code used other than OpenSSL. If you modify file(s) with this
21
    exception, you may extend this exception to your version of the file(s),
22
    but you are not obligated to do so. If you do not wish to do so, delete this
23
    exception statement from your version. If you delete this exception statement
24
    from all source files in the program, then also delete it here.
25

26
    Copyright 2017-2020 Telegram Systems LLP
27
*/
28
#include "func.h"
29
#include "parser/srcread.h"
30
#include "parser/lexer.h"
31
#include "parser/symtable.h"
32
#include <getopt.h>
33
#include <fstream>
34
#include "git.h"
35

36
void usage(const char* progname) {
37
  std::cerr
38
      << "usage: " << progname
39
      << " [-vIAPSR][-O<level>][-i<indent-spc>][-o<output-filename>][-W<boc-filename>] {<func-source-filename> ...}\n"
40
         "\tGenerates Fift TVM assembler code from a funC source\n"
41
         "-I\tEnables interactive mode (parse stdin)\n"
42
         "-o<fift-output-filename>\tWrites generated code into specified file instead of stdout\n"
43
         "-v\tIncreases verbosity level (extra information output into stderr)\n"
44
         "-i<indent>\tSets indentation for the output code (in two-space units)\n"
45
         "-A\tPrefix code with `\"Asm.fif\" include` preamble\n"
46
         "-O<level>\tSets optimization level (2 by default)\n"
47
         "-P\tEnvelope code into PROGRAM{ ... }END>c\n"
48
         "-S\tInclude stack layout comments in the output code\n"
49
         "-R\tInclude operation rewrite comments in the output code\n"
50
         "-W<output-boc-file>\tInclude Fift code to serialize and save generated code into specified BoC file. Enables "
51
         "-A and -P.\n"
52
         "\t-s\tOutput semantic version of FunC and exit\n"
53
         "\t-V<version>\tShow func build information\n";
54
  std::exit(2);
55
}
56

57
int main(int argc, char* const argv[]) {
58
  int i;
59
  std::string output_filename;
60
  while ((i = getopt(argc, argv, "Ahi:Io:O:PRsSvW:V")) != -1) {
61
    switch (i) {
62
      case 'A':
63
        funC::asm_preamble = true;
64
        break;
65
      case 'I':
66
        funC::interactive = true;
67
        break;
68
      case 'i':
69
        funC::indent = std::max(0, atoi(optarg));
70
        break;
71
      case 'o':
72
        output_filename = optarg;
73
        break;
74
      case 'O':
75
        funC::opt_level = std::max(0, atoi(optarg));
76
        break;
77
      case 'P':
78
        funC::program_envelope = true;
79
        break;
80
      case 'R':
81
        funC::op_rewrite_comments = true;
82
        break;
83
      case 'S':
84
        funC::stack_layout_comments = true;
85
        break;
86
      case 'v':
87
        ++funC::verbosity;
88
        break;
89
      case 'W':
90
        funC::boc_output_filename = optarg;
91
        funC::asm_preamble = funC::program_envelope = true;
92
        break;
93
      case 's':
94
        std::cout << funC::func_version << "\n";
95
        std::exit(0);
96
        break;
97
      case 'V':
98
        std::cout << "FunC semantic version: v" << funC::func_version << "\n";
99
        std::cout << "Build information: [ Commit: " << GitMetadata::CommitSHA1() << ", Date: " << GitMetadata::CommitDate() << "]\n";
100
        std::exit(0);
101
        break;
102
      case 'h':
103
      default:
104
        usage(argv[0]);
105
    }
106
  }
107

108
  std::ostream *outs = &std::cout;
109

110
  std::unique_ptr<std::fstream> fs;
111
  if (!output_filename.empty()) {
112
    fs = std::make_unique<std::fstream>(output_filename, std::fstream::trunc | std::fstream::out);
113
    if (!fs->is_open()) {
114
      std::cerr << "failed to create output file " << output_filename << '\n';
115
      return 2;
116
    }
117
    outs = fs.get();
118
  }
119

120
  std::vector<std::string> sources;
121

122
  while (optind < argc) {
123
    sources.push_back(std::string(argv[optind++]));
124
  }
125

126
  funC::read_callback = funC::fs_read_callback;
127

128
  return funC::func_proceed(sources, *outs, std::cerr);
129
}
130

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.