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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
   Copyright (C) 2019 Andreas Franz Borchert
                      Michael Christian Lehn
   ----------------------------------------------------------------------------
   The Astl Library is free software; you can redistribute it
   and/or modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either version
   2 of the License, or (at your option) any later version.

   The Astl Library is distributed in the hope that it will be
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <cassert>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <memory>
#include <astl/builtin-parse.hpp>
#include <astl/generator.hpp>
#include <astl/loader.hpp>
#include <astl/run.hpp>
#include "location.hpp"
#include "operators.hpp"
#include "parser.hpp"
#include "scanner.hpp"
#include "yytname.hpp"

using namespace Astl;
using namespace AstlULMcalc;

AttributePtr builtin_parse(BindingsPtr bindings, AttributePtr args) {
   return builtin_parse(bindings, args,
      [](NodePtr& root, InputStreamPtr istream) -> bool {
         Scanner scanner(istream->get(), istream->get_name());
         parser p(scanner, root);
         return p.parse() == 0;
      });
}

int main(int argc, char** argv) {
   try {
      Loader loader;
      char* path = getenv("ASTL_ULMCALC_PATH");
      if (path) {
         char* dir = path;
         for (char* cp = path; *cp; ++cp) {
            if (*cp == ':') {
               if (dir) {
                  * cp = 0;
                  loader.add_library(dir);
                  dir = 0;
               }
            } else if (!dir) {
               dir = cp;
            }
         }
         if (dir) loader.add_library(dir);
      } else {
         // default path
#ifdef ASTL_ULMCALC_LIBDIR
         loader.add_library(ASTL_ULMCALC_LIBDIR);
#else
         loader.add_library("/usr/local/share/astl/ulmcalc");
         loader.add_library("/usr/share/astl/ulmcalc");
#endif
      }

      BuiltinFunctions bfs;
      bfs.add("parse", builtin_parse);
      bfs.add("run_attribution_rules", builtin_run_attribution_rules);
      bfs.add("run_state_machines", builtin_run_state_machines);
      auto bindings = std::make_shared<Bindings>();
      bfs.insert(bindings);

      run(argc, argv, loader, Op::LPAREN, bindings);
   } catch (Exception& e) {
      std::cerr << e.what() << std::endl;
      std::exit(1);
   } catch (std::exception& e) {
      std::cerr << e.what() << std::endl;
      std::exit(1);
   }
}

/*

=head1 NAME

astl-ulmcalc-free -- run free-standing Astl scripts for assembler texts for ULMcalc

=head1 SYNOPSIS

B<astl-ulmcalc-free> F<astl-script> [I<args>]

=head1 DESCRIPTION

Astl scripts can be started explicitly using the B<astl-ulmcalc-free> interpreter
or implicitly using a shebang line in the first line of the script:

   #!/usr/bin/env astl-ulmcalc-free

The construct using F</usr/bin/env> attempts to find F<astl-ulmcalc-free>
anywhere in the user's path.

The text shall be for the ULM Calculator

B<astl-ulmcalc-free> does not load or parse itself an assembler source
file. Instead, I<root> is set to null and it is up to the I<main>
function of the script to load and parse source files.

=head1 ENVIRONMENT

The environment variable I<ASTL_ULMCALC_PATH> can be used to specify
a colon-separated list of directories where library modules
are looked for. By default, F</usr/local/share/astl/ulmcalc> and
F</usr/share/astl/ulmcalc> are used. The script itself can add more
directories to the library search path using the Astl
library clause (see section 12.1 in the I<Report of the
Astl Programming Language>).

=head1 AUTHOR

Andreas F. Borchert

=cut

*/