aboutsummaryrefslogtreecommitdiffstats
path: root/error.cpp
blob: 40032b0c99ba070eb06a85f867aec9f4a0633dd8 (plain) (blame)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// error.cpp
// Revision 7-feb-2005

#include "error.h"
#include "sysvar.h"
#include "util.h"

#include <sstream>
#include <algorithm>

namespace sysvar= blassic::sysvar;


namespace {

struct errcode {
	const BlErrNo err;
	const char * const str;
	errcode (BlErrNo nerr, const char * nstr) :
		err (nerr),
		str (nstr)
	{ }
};

// Can't declare const or Borland can't expand find_if. Why?
errcode table []= {
	errcode (ErrNoError,            "No error"),
	errcode (ErrSyntax,             "Syntax horror"),
	errcode (ErrMismatch,           "Type mismatch"),
	errcode (ErrGosubWithoutReturn, "GOSUB without RETURN"),
	errcode (ErrReturnWithoutGosub, "RETURN without GOSUB"),
	errcode (ErrNextWithoutFor,     "NEXT without FOR"),
	errcode (ErrNotImplemented,     "Not implemented"),
	errcode (ErrDivZero,            "Division by zero"),
	errcode (ErrDataExhausted,      "Data exhausted"),
	errcode (ErrInvalidCommand,     "Invalid command"),
	errcode (ErrPolite,             "Programmer is too polite"),
	errcode (ErrBadSubscript,       "Bad Subscript"),
	errcode (ErrOutMemory,          "Out of memory"),
	errcode (ErrAlreadyDim,         "Array already dimensioned"),
	errcode (ErrNoContinue,         "Cannot CONTinue"),
	errcode (ErrFileNumber,         "Bad file number"),
	errcode (ErrFileMode,           "Bad file mode"),
	errcode (ErrFileAlreadyOpen,    "File already open"),
	errcode (ErrFileRead,           "Error reading file"),
	errcode (ErrFileWrite,          "Error writing file"),
	errcode (ErrUntilWithoutRepeat, "UNTIL without REPEAT"),
	errcode (ErrWendWithoutWhile,   "WEND without WHILE"),
	errcode (ErrWhileWithoutWend,   "WHILE without WEND"),
	errcode (ErrBlassicInternal,    "Internal Blassic error"),
	errcode (ErrNoDynamicLibrary,   "Dynamic library not found"),
	errcode (ErrNoDynamicSymbol,    "Symbol not found in dynamic library"),
	errcode (ErrCannotResume,       "Cannot RESUME"),
	errcode (ErrNoLabel,            "Label does not exist"),
	errcode (ErrMisplacedLocal,     "LOCAL out of subroutine"),
	errcode (ErrFieldOverflow,      "FIELD overflow"),
	errcode (ErrFileNotFound,       "File not found"),
	errcode (ErrLineExhausted,      "Line numbers exhausted"),
	errcode (ErrFunctionNoDefined,  "User function undefined"),
	errcode (ErrIncompleteDef,      "User function incomplete"),
	errcode (ErrInvalidDirect,      "Invalid direct command"),
	errcode (ErrBadRecord,          "Bad record number"),
	errcode (ErrFunctionCall,       "Illegal function call"),
	errcode (ErrSocket,             "Socket error"),
	errcode (ErrRenameFile,         "Rename file error"),
	errcode (ErrOperatingSystem,    "Operating system error"),
	errcode (ErrPastEof,            "Input past EOF"),
	errcode (ErrNoGraphics,         "Graphics mode required"),
	errcode (ErrImproperArgument,   "Improper argument"),
	errcode (ErrDomain,             "Domain error"),
	errcode (ErrRange,              "Result out of range"),
	errcode (ErrLineNotExist,       "Line does not exist"),
	errcode (ErrFnRecursion,        "FN recursion too deep"),
	errcode (ErrOverflow,           "Overflow"),
	errcode (ErrRegexp,             "Bad regular expression"),
	errcode (ErrDynamicUnsupported, "Dymanic link not supported"),
	errcode (ErrRepeatWithoutUntil, "REPEAT without UNTIL"),
	errcode (ErrUnexpectedFnEnd,    "FN END outside DEF FN"),
	errcode (ErrNoFnEnd,            "FN without FN END"),
	errcode (ErrDuplicateLabel,     "Duplicate LABEL"),
	errcode (ErrNoTeDejo,           "That won't work"),
};

errcode * table_end= table + util::dim_array (table);

class is_err {
public:
	is_err (BlErrNo err) : err (err)
	{ }
	bool operator () (const errcode & ec) const
	{ return ec.err == err; }
private:
	BlErrNo err;
};

} // namespace

//***********************************************
//		class BlError
//***********************************************

BlError::BlError () :
	err (ErrNoError)
{ }

BlError::BlError (BlErrNo nerr) :
	err (nerr)
{ }

BlError::BlError (BlErrNo nerr, ProgramPos npos) :
	err (nerr),
	pos (npos)
{ }

void BlError::clear ()
{
	err= ErrNoError;
	pos= ProgramPos ();
}

void BlError::set (BlErrNo nerr, ProgramPos npos)
{
	err= nerr;
	pos= npos;
}

void BlError::seterr (BlErrNo nerr)
{
	err= nerr;
}

BlErrNo BlError::geterr () const
{
	return err;
}

ProgramPos BlError::getpos () const
{
	return pos;
}

std::ostream & operator << (std::ostream & os, const BlError & bl)
{
	os << ErrStr (bl.err);
	const BlLineNumber line= bl.getpos ().getnum ();
	if (line != LineDirectCommand)
		os << " in " << line;
	//os << '\n';
	return os;
}

//***********************************************
//		class BlBreakInPos
//***********************************************

BlBreakInPos::BlBreakInPos (ProgramPos pos) :
	pos (pos)
{ }

ProgramPos BlBreakInPos::getpos () const
{
	return pos;
}

std::ostream & operator << (std::ostream & os, const BlBreakInPos & bbip)
{
	os << "**BREAK**";
	BlLineNumber line= bbip.getpos ().getnum ();
	if (line != LineDirectCommand)
		os << " in " << line;
	return os;
}

//***********************************************
//		Other functions
//***********************************************

std::string ErrStr (BlErrNo err)
{
	const errcode * perr= std::find_if (table, table_end, is_err (err) );
	if (perr != table_end)
		return perr->str;
	std::ostringstream strbuf;
	strbuf << "Error " << err;
	return strbuf.str ();
}

bool showdebuginfo ()
{
	//return sysvar::getFlags1 () & sysvar::ShowDebugInfo;
	return sysvar::hasFlags1 (sysvar::ShowDebugInfo);
}

// Fin de error.cpp
Un proyecto texto-plano.xyz