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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 | /*
Copyright (C) 2009-2019 Andreas Franz Borchert
Michael Christian Lehn
----------------------------------------------------------------------------
Astl-ULMcalc 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.
Astl-ULMcalc 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/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 std;
using namespace Astl;
using namespace AstlULMcalc;
class SyntaxTreeGeneratorForULMcalc: public SyntaxTreeGenerator {
public:
virtual NodePtr gen(int& argc, char**& argv) {
if (argc == 0) {
throw Exception("no source file given");
}
char* source_name = *argv++; --argc;
ifstream source(source_name);
if (!source) {
std::ostringstream os;
os << "unable to open " << source_name;
throw Exception(os.str());
}
Scanner scanner(source, source_name);
NodePtr root;
parser p(scanner, root);
if (p.parse() != 0) {
std::ostringstream os;
os << "parsing of " << source_name << " failed";
throw Exception(os.str());
}
return root;
}
};
int main(int argc, char** argv) {
try {
SyntaxTreeGeneratorForULMcalc astgen;
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
}
run(argc, argv, astgen, loader, Op::LPAREN);
} 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 -- run Astl scripts for assembler texts for ULM
=head1 SYNOPSIS
B<astl-ulmcalc> F<astl-script>] F<calc-text> [I<args>]
=head1 DESCRIPTION
Astl scripts can be started explicitly using the B<astl-ulmcalc> interpreter
or implicitly using a shebang line in the first line of the script:
#!/usr/bin/env astl-ulmcalc
The construct using F</usr/bin/env> attempts to find F<astl-ulmcalc>
anywhere in the user's path.
The text shall be for the ULM-Calculator
Once the abstract syntax tree is present, the Astl script is
loaded and executed following the execution order defined
in section 12.5 of the Report of the Astl Programming Language.
All arguments behind the text file are put into a list
and bound to the variable I<args> in the I<main> function.
=head1 EXAMPLE
The following example prints a warning message for each
assignment found within a condition:
#!/usr/bin/env astl-ulmcalc
import printer;
=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
*/
|