1 | #include <cstdio>
|
---|
2 | #include <cstdarg>
|
---|
3 | #include "messages.h"
|
---|
4 | #include "Symbol.h"
|
---|
5 | #include "types.h"
|
---|
6 |
|
---|
7 | using namespace std;
|
---|
8 |
|
---|
9 | Messages::Messages()
|
---|
10 | /* Default settings */
|
---|
11 | : show_warnings(true), warnings(0), suppressed(0), errors(0)
|
---|
12 | {}
|
---|
13 |
|
---|
14 | void Messages::EnableWarnings()
|
---|
15 | {
|
---|
16 | show_warnings = true;
|
---|
17 | }
|
---|
18 |
|
---|
19 | void Messages::DisableWarnings()
|
---|
20 | {
|
---|
21 | show_warnings = false;
|
---|
22 | }
|
---|
23 |
|
---|
24 | void Messages::Warning(int lineno, const char *msg, ...)
|
---|
25 | {
|
---|
26 | if (show_warnings) {
|
---|
27 | ++warnings;
|
---|
28 | fprintf(stderr, "Warning: %d: ", lineno);
|
---|
29 | va_list args;
|
---|
30 | va_start(args, msg);
|
---|
31 | vfprintf(stderr, msg, args);
|
---|
32 | va_end(args);
|
---|
33 | } else {
|
---|
34 | ++suppressed;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | void Messages::Error(int lineno, const char *msg, ...)
|
---|
39 | {
|
---|
40 | ++errors;
|
---|
41 | fprintf(stderr, "Error: %d: ", lineno);
|
---|
42 | va_list args;
|
---|
43 | va_start(args, msg);
|
---|
44 | vfprintf(stderr, msg, args);
|
---|
45 | va_end(args);
|
---|
46 | }
|
---|
47 |
|
---|
48 | void Messages::CoercionWarning(int lineno, ReturnType to, ReturnType from)
|
---|
49 | {
|
---|
50 | Warning(lineno, "Type coerced from %s to %s.\n",
|
---|
51 | ReturnTypeToString(from), ReturnTypeToString(to));
|
---|
52 | }
|
---|
53 |
|
---|
54 | void Messages::ReturnValueError(int lineno, const string& id, ReturnType type)
|
---|
55 | {
|
---|
56 | Error(lineno, "Return value of type %s from function %s discarded.\n",
|
---|
57 | ReturnTypeToString(type), id.c_str());
|
---|
58 | }
|
---|
59 |
|
---|
60 | void Messages::TypeError(int lineno, ReturnType expected, ReturnType received)
|
---|
61 | {
|
---|
62 | Error(lineno, "Expected type %s, got type %s.\n",
|
---|
63 | ReturnTypeToString(expected), ReturnTypeToString(received));
|
---|
64 | }
|
---|
65 |
|
---|
66 | void Messages::UndeclaredIdError(int lineno, const string& id)
|
---|
67 | {
|
---|
68 | Error(lineno, "Identifier \'%s\' undeclared.\n", id.c_str());
|
---|
69 | }
|
---|
70 |
|
---|
71 | void Messages::DeclaredIdError(int lineno, const string& id, int prev_lineno)
|
---|
72 | {
|
---|
73 | Error(lineno, "Identifier \'%s\' was already previously declared on line %d.\n",
|
---|
74 | id.c_str(), prev_lineno);
|
---|
75 | }
|
---|
76 |
|
---|
77 | void Messages::LValueError(int lineno, const string& id)
|
---|
78 | {
|
---|
79 | Error(lineno, "Identifier \'%s\' is not a lvalue.\n", id.c_str());
|
---|
80 | }
|
---|
81 |
|
---|
82 | void Messages::RValueError(int lineno, const string& id, SymbolType symbol_type)
|
---|
83 | {
|
---|
84 | Error(lineno, "Identifier \'%s\' is a %s and can't be used here.\n",
|
---|
85 | id.c_str(), SymbolTypeToString(symbol_type));
|
---|
86 | }
|
---|
87 |
|
---|
88 | void Messages::CallError(int lineno, const string& id, SymbolType symbol_type)
|
---|
89 | {
|
---|
90 | Error(lineno, "Identifier \'%s\' is a %s, was expecting a function or procedure.\n",
|
---|
91 | id.c_str(), SymbolTypeToString(symbol_type));
|
---|
92 | }
|
---|
93 |
|
---|
94 | void Messages::ParamCountError(int lineno, int paramcount, Symbol *symbol)
|
---|
95 | {
|
---|
96 | Error(lineno, "%s parameters in call to %s.\n",
|
---|
97 | ((paramcount > symbol->GetParameterCount())? "Too many" : "Too few"),
|
---|
98 | SignatureString(symbol).c_str());
|
---|
99 | }
|
---|
100 |
|
---|
101 | void Messages::ShowNumbers() const
|
---|
102 | {
|
---|
103 | fprintf(stderr, "%d warning%s(%d suppressed) and %d error%s.\n",
|
---|
104 | warnings, ((warnings == 1) ? "" : "s"), suppressed,
|
---|
105 | errors, ((errors == 1) ? "" : "s"));
|
---|
106 | }
|
---|
107 |
|
---|
108 | int Messages::GetErrors() const
|
---|
109 | {
|
---|
110 | return errors;
|
---|
111 | }
|
---|
112 |
|
---|
113 | int Messages::GetWarnings() const
|
---|
114 | {
|
---|
115 | return warnings + suppressed;
|
---|
116 | }
|
---|
117 |
|
---|
118 | string Messages::SignatureString(Symbol *symbol)
|
---|
119 | {
|
---|
120 | string signature = "";
|
---|
121 | signature += SymbolTypeToString(symbol->GetSymbolType());
|
---|
122 | signature += " ";
|
---|
123 | signature += symbol->GetName();
|
---|
124 | signature += "(";
|
---|
125 | const int parametercount = symbol->GetParameterCount();
|
---|
126 | for (int i = 0; i < parametercount; ++i) {
|
---|
127 | Symbol *param = symbol->GetParameter(i);
|
---|
128 | signature += param->GetName();
|
---|
129 | signature += ": ";
|
---|
130 | signature += ReturnTypeToString(param->GetReturnType());
|
---|
131 | signature += ((i == parametercount) ? "): " : ", ");
|
---|
132 | }
|
---|
133 | signature += ReturnTypeToString(symbol->GetReturnType());
|
---|
134 | return signature;
|
---|
135 | }
|
---|