MicroModelicaCCompiler  4.5.3
type_check.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #include "type_check.h"
21 
22 #include <iostream>
23 #include <list>
24 #include <string>
25 
26 #include <ast/ast_builder.hpp>
27 #include "../ast/expression.hpp"
28 #include "symbol_table.hpp"
29 
30 #define T(s) tyEnv->lookup(s)
31 
32 TypeCheck_::TypeCheck_(TypeSymbolTable t, VarSymbolTable v) : tyEnv(t), varEnv(v){};
33 
35 {
36  if (*t1 == T("Integer")) {
37  if (*t2 == T("Integer") or *t2 == T("Real"))
38  return true;
39  else
40  return false;
41  } else if (*t1 == T("Real")) {
42  if (*t2 == T("Integer") or *t2 == T("Real"))
43  return true;
44  else
45  return false;
46  } else {
47  return *t1 == *t2;
48  }
49 }
50 
51 Type TypeCheck_::check_binop(AST_Expression l, AST_Expression r, BinOpType op)
52 {
53  Type t1 = check_expression(l);
54  Type t2 = check_expression(r);
55  switch (op) {
56  case BINOPLOWER:
57  case BINOPLOWEREQ:
58  case BINOPGREATER:
59  case BINOPGREATEREQ:
60  case BINOPCOMPNE:
61  case BINOPCOMPEQ:
62  if (check_equal(t1, t2) and !check_equal(t1, T("Boolean")))
63  return T("Boolean");
64  else
65  throw "Type Error (0)";
66  case BINOPDIV:
67  case BINOPELDIV:
68  case BINOPMULT:
69  case BINOPELMULT:
70  case BINOPADD:
71  case BINOPELADD:
72  case BINOPSUB:
73  case BINOPELSUB:
74  case BINOPEXP:
75  case BINOPELEXP:
76  if (check_equal(t1, t2) and (check_equal(t1, T("Real")) or check_equal(t1, T("Integer"))))
77  return t1;
78  else
79  throw "Type Error (1)";
80  case BINOPAND:
81  case BINOPOR:
82  if (check_equal(t1, t2) and check_equal(t1, T("Boolean"))) return t1;
83  throw "Type Error (2)";
84  }
85  throw "Error! (TypeCheck_::check_binop)";
86 }
87 
89 {
90  Type ct, t1, t2, t;
91  switch (e->expressionType()) {
92  case EXPBINOP: {
93  AST_Expression_BinOp b = e->getAsBinOp();
94  return check_binop(b->left(), b->right(), b->binopType());
95  }
96  case EXPUMINUS: {
97  AST_Expression_UMinus b = e->getAsUMinus();
98  t = check_expression(b->exp());
99  if (check_equal(t, T("Integer")) or check_equal(t, T("Real"))) return t;
100  throw "Type Error (3)";
101  }
102 
103  case EXPOUTPUT: {
104  AST_Expression_Output b = e->getAsOutput();
105  return check_expression(b->expressionList()->front());
106  }
107 
108  case EXPIF: {
109  AST_Expression_If b = e->getAsIf();
110  ct = check_expression(b->condition());
111  t1 = check_expression(b->then());
112  t2 = check_expression(b->else_exp()); // Falta el elseIF
113  if (!check_equal(ct, T("Boolean"))) throw "Type Error (4)";
114  if (!check_equal(t1, t2)) throw "Type Error (5)";
115  return t1;
116  }
117 
118  case EXPCALL: {
119  // AƱadir las funciones en la listaaaa de variables
120  AST_Expression_Call c = e->getAsCall();
121  if (toStr(c->name()) == "sample") return T("Boolean");
122  if (toStr(c->name()) == "pre") return check_expression(c->arguments()->front());
123  return T("Real");
124  }
125 
126  case EXPCOMPREF: {
127  AST_Expression_ComponentReference b = e->getAsComponentReference();
128 
129  VarInfo tt = varEnv->lookup(toStr(b->names()->front()));
130 
131  if (tt == nullptr) {
132  cerr << "Var:" << b->names()->front() << ":";
133  throw "Variable no existe (8)";
134  }
135  if (b->indexes()->front()->size() == 0)
136  return tt->type();
137  else {
138  Type t = tt->type();
139  AST_ExpressionListIterator exit;
140  foreach (exit, b->indexes()->front())
141  if (t->getType() == TYARRAY)
142  t = t->getAsArray()->arrayOf();
143  else
144  throw "Type Error (7)";
145  return t;
146  }
147 
148  break;
149  }
150  case EXPDERIVATIVE:
151  return T("Real");
152  case EXPBOOLEAN:
153  return T("Boolean");
154  case EXPSTRING:
155  return T("String");
156  case EXPREAL:
157  return T("Real");
158  case EXPINTEGER:
159  return T("Integer");
160  case EXPBOOLEANNOT: {
161  AST_Expression_BooleanNot b = e->getAsBooleanNot();
162  t = check_expression(b->exp());
163  if (!check_equal(t, T("Boolean"))) throw "Type Error (6)";
164  return t;
165  }
166  default:
167  throw "No implrementado aun! (check_expression)";
168  }
169 }
EXPINTEGER
@ EXPINTEGER
Definition: ast_types.hpp:180
EXPOUTPUT
@ EXPOUTPUT
Definition: ast_types.hpp:182
TypeCheck_::check_equal
bool check_equal(Type t1, Type t2)
Definition: type_check.cpp:34
toStr
#define toStr(it)
Definition: ast_builder.hpp:53
BINOPEXP
@ BINOPEXP
Definition: ast_types.hpp:154
ast_builder.hpp
BINOPAND
@ BINOPAND
Definition: ast_types.hpp:139
EXPCOMPREF
@ EXPCOMPREF
Definition: ast_types.hpp:165
BINOPCOMPEQ
@ BINOPCOMPEQ
Definition: ast_types.hpp:145
EXPUMINUS
@ EXPUMINUS
Definition: ast_types.hpp:176
TypeCheck_::check_binop
Type check_binop(AST_Expression l, AST_Expression r, BinOpType op)
Definition: type_check.cpp:51
BINOPELEXP
@ BINOPELEXP
Definition: ast_types.hpp:155
BINOPGREATER
@ BINOPGREATER
Definition: ast_types.hpp:142
symbol_table.hpp
BINOPELMULT
@ BINOPELMULT
Definition: ast_types.hpp:153
BINOPELADD
@ BINOPELADD
Definition: ast_types.hpp:147
EXPIF
@ EXPIF
Definition: ast_types.hpp:170
BINOPLOWER
@ BINOPLOWER
Definition: ast_types.hpp:140
TypeCheck_::check_expression
Type check_expression(AST_Expression t)
Definition: type_check.cpp:88
MicroModelica::IR::EQUATION::Type
Type
Definition: equation.hpp:50
BINOPMULT
@ BINOPMULT
Definition: ast_types.hpp:152
BINOPELDIV
@ BINOPELDIV
Definition: ast_types.hpp:151
EXPDERIVATIVE
@ EXPDERIVATIVE
Definition: ast_types.hpp:167
EXPBOOLEAN
@ EXPBOOLEAN
Definition: ast_types.hpp:177
BINOPSUB
@ BINOPSUB
Definition: ast_types.hpp:148
TypeCheck_::TypeCheck_
TypeCheck_(TypeSymbolTable, VarSymbolTable)
Definition: type_check.cpp:32
BINOPOR
@ BINOPOR
Definition: ast_types.hpp:138
BINOPGREATEREQ
@ BINOPGREATEREQ
Definition: ast_types.hpp:143
EXPCALL
@ EXPCALL
Definition: ast_types.hpp:173
TypeCheck_::varEnv
VarSymbolTable varEnv
Definition: type_check.hpp:64
BINOPDIV
@ BINOPDIV
Definition: ast_types.hpp:150
EXPBOOLEANNOT
@ EXPBOOLEANNOT
Definition: ast_types.hpp:181
BINOPCOMPNE
@ BINOPCOMPNE
Definition: ast_types.hpp:144
T
#define T(s)
Definition: type_check.cpp:30
EXPBINOP
@ EXPBINOP
Definition: ast_types.hpp:166
BINOPLOWEREQ
@ BINOPLOWEREQ
Definition: ast_types.hpp:141
BINOPADD
@ BINOPADD
Definition: ast_types.hpp:146
BINOPELSUB
@ BINOPELSUB
Definition: ast_types.hpp:149
BinOpType
BinOpType
Definition: ast_types.hpp:137
EXPREAL
@ EXPREAL
Definition: ast_types.hpp:179
SymbolType::TYARRAY
@ TYARRAY
EXPSTRING
@ EXPSTRING
Definition: ast_types.hpp:178