From 14c738a40371bcf8e0c0f17f917bae73f096e90e Mon Sep 17 00:00:00 2001 From: Alexandre Bique Date: Sun, 1 Mar 2009 19:43:59 +0000 Subject: [PATCH] fixed cast bool, bool --- check_expr.sh | 0 src/ast/llvm-generator-visitor.cc | 10 ++++++++-- src/ast/llvm-generator-visitor.hh | 1 + src/ast/simplify-visitor.cc | 4 ++++ src/ast/type-checker-visitor.cc | 18 ++++++++++-------- src/ast/type-checker-visitor.hh | 24 ++++++++++++------------ 6 files changed, 35 insertions(+), 22 deletions(-) mode change 100644 => 100755 check_expr.sh diff --git a/check_expr.sh b/check_expr.sh old mode 100644 new mode 100755 diff --git a/src/ast/llvm-generator-visitor.cc b/src/ast/llvm-generator-visitor.cc index cbad282..79c22fd 100644 --- a/src/ast/llvm-generator-visitor.cc +++ b/src/ast/llvm-generator-visitor.cc @@ -90,8 +90,8 @@ namespace ast void \ LLVMGeneratorVisitor::visit(const Type & node) \ { \ - IntegerType * itype = reinterpret_cast(node.type); \ - out_ << Instr" i" << itype->size << " "; \ + out_ << Instr" "; \ + node.type->accept(*this); \ node.left->accept(*this); \ out_ << ", "; \ node.right->accept(*this); \ @@ -186,6 +186,12 @@ namespace ast } void + LLVMGeneratorVisitor::visit(const BoolType & node) + { + out_ << " i1 "; + } + + void LLVMGeneratorVisitor::visit(const IntegerType & node) { out_ << " i" << node.size << " "; diff --git a/src/ast/llvm-generator-visitor.hh b/src/ast/llvm-generator-visitor.hh index 5d46853..83df590 100644 --- a/src/ast/llvm-generator-visitor.hh +++ b/src/ast/llvm-generator-visitor.hh @@ -59,6 +59,7 @@ namespace ast virtual void visit(const If & node); virtual void visit(const Type & type); + virtual void visit(const BoolType & type); virtual void visit(const NumberType & type); virtual void visit(const IntegerType & type); virtual void visit(const FloatType & type); diff --git a/src/ast/simplify-visitor.cc b/src/ast/simplify-visitor.cc index 640dee6..b942b2b 100644 --- a/src/ast/simplify-visitor.cc +++ b/src/ast/simplify-visitor.cc @@ -3,6 +3,7 @@ #include #include "simplify-visitor.hh" +#include "type-checker-visitor.hh" namespace ast { @@ -167,6 +168,9 @@ namespace ast node.exp->accept(*this); node.exp = replacement_; + if (!TypeCheckerVisitor::findCast(node.type, node.exp->type)) + return; + AssignExp * assign = new AssignExp(); assign->value = &node; assign->dest = new Symbol(); diff --git a/src/ast/type-checker-visitor.cc b/src/ast/type-checker-visitor.cc index c3d1160..4058ae8 100644 --- a/src/ast/type-checker-visitor.cc +++ b/src/ast/type-checker-visitor.cc @@ -7,9 +7,10 @@ namespace ast { + TypeCheckerVisitor::castTable_t TypeCheckerVisitor::castTable_; + TypeCheckerVisitor::TypeCheckerVisitor() : BrowseVisitor(), - castTable_(), scope_() { #define ADD_CAST_TABLE_ENTRY(Type1, Type2, Method) \ @@ -23,14 +24,15 @@ namespace ast key = std::make_pair(id2, id1); \ assert_msg(!castTable_.count(key), \ "the same key has been insterted two times"); \ - castTable_[key] = &TypeCheckerVisitor::Method; \ - castTableKey_t key2 = key; \ - assert(castTable_.count(key2) > 0); \ + castTable_[key] = TypeCheckerVisitor::Method; \ } while (0) - ADD_CAST_TABLE_ENTRY(BoolType, BoolType, castIntegerFloat); - ADD_CAST_TABLE_ENTRY(FloatType, IntegerType, castIntegerFloat); - ADD_CAST_TABLE_ENTRY(IntegerType, IntegerType, castIntegerInteger); + if (castTable_.empty()) + { + ADD_CAST_TABLE_ENTRY(BoolType, BoolType, castBoolBool); + ADD_CAST_TABLE_ENTRY(FloatType, IntegerType, castIntegerFloat); + ADD_CAST_TABLE_ENTRY(IntegerType, IntegerType, castIntegerInteger); + } } TypeCheckerVisitor::~TypeCheckerVisitor() @@ -161,7 +163,7 @@ namespace ast key = std::make_pair(type2->nodeType, type1->nodeType); assert(castTable_.count(key) > 0); - CastExp * castExp = (this->*castTable_[key])(type1, type2); + CastExp * castExp = castTable_[key](type1, type2); return castExp; } diff --git a/src/ast/type-checker-visitor.hh b/src/ast/type-checker-visitor.hh index f0b14e7..1e1302c 100644 --- a/src/ast/type-checker-visitor.hh +++ b/src/ast/type-checker-visitor.hh @@ -50,21 +50,21 @@ namespace ast virtual void visit(File & node); virtual void visit(Block & node); - private: - CastExp * findCast(Type * type1, Type * type2); - - CastExp * castBoolBool(Type * type1, Type * type2); - CastExp * castIntegerFloat(Type * type1, Type * type2); - CastExp * castIntegerInteger(Type * type1, Type * type2); - + public: + /// @todo move the cast part in an other place typedef std::pair castTableKey_t; - typedef CastExp * - (TypeCheckerVisitor::*castTableValue_t)(Type * type1, - Type * type2); - typedef std::map castTable_t; + typedef CastExp * (*castTableValue_t)(Type * type1, Type * type2); + typedef std::map castTable_t; typedef BrowseVisitor super_t; - castTable_t castTable_; + static CastExp * findCast(Type * type1, Type * type2); + + private: + static CastExp * castBoolBool(Type * type1, Type * type2); + static CastExp * castIntegerFloat(Type * type1, Type * type2); + static CastExp * castIntegerInteger(Type * type1, Type * type2); + + static castTable_t castTable_; Scope * scope_; }; } -- 2.11.4.GIT