source: liacs/coco/assignment2/messages.cc@ 235

Last change on this file since 235 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

File size: 3.6 KB
Line 
1#include <cstdio>
2#include <cstdarg>
3#include "messages.h"
4#include "Symbol.h"
5#include "types.h"
6
7using namespace std;
8
9Messages::Messages()
10 /* Default settings */
11 : show_warnings(true), warnings(0), suppressed(0), errors(0)
12{}
13
14void Messages::EnableWarnings()
15{
16 show_warnings = true;
17}
18
19void Messages::DisableWarnings()
20{
21 show_warnings = false;
22}
23
24void 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
38void 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
48void 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
54void 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
60void 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
66void Messages::UndeclaredIdError(int lineno, const string& id)
67{
68 Error(lineno, "Identifier \'%s\' undeclared.\n", id.c_str());
69}
70
71void 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
77void Messages::LValueError(int lineno, const string& id)
78{
79 Error(lineno, "Identifier \'%s\' is not a lvalue.\n", id.c_str());
80}
81
82void 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
88void 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
94void 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
101void 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
108int Messages::GetErrors() const
109{
110 return errors;
111}
112
113int Messages::GetWarnings() const
114{
115 return warnings + suppressed;
116}
117
118string 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}
Note: See TracBrowser for help on using the repository browser.