Modified:
/trunk/include/ast.h
/trunk/include/astvisitor.h
/trunk/src/astvisitor.cc
/trunk/src/parser.y
=======================================
--- /trunk/include/ast.h Tue May 17 17:53:24 2011
+++ /trunk/include/ast.h Wed May 18 16:09:13 2011
@@ -29,7 +29,7 @@
#define CLEVER_AST_H
#include <vector>
-#include <iostream> //debug
+#include <set>
#include "value.h"
#include "refcounted.h"
#include "astvisitor.h"
@@ -899,49 +899,43 @@
~ClassStmtList() {
{
std::list<MethodDeclaration*>::iterator
- it = m_methods_decl.begin(), end =
m_methods_decl.end();
+ it = m_methods_decl.begin(), end = m_methods_decl.end();
while (it != end) {
- (*it)->delRef();
- ++it;
+ (*it)->delRef();
+ ++it;
}
}
-
+
{
std::list<AttributeDeclaration*>::iterator
- it = m_attrib_decl.begin(), end = m_attrib_decl.end();
+ it = m_attrib_decl.begin(), end = m_attrib_decl.end();
while (it != end) {
- (*it)->delRef();
- ++it;
+ (*it)->delRef();
+ ++it;
}
}
}
void addMethod(ASTNode* method) throw() {
- MethodDeclaration* m =
static_cast<MethodDeclaration*>(method);
+ MethodDeclaration* m = static_cast<MethodDeclaration*>(method);
m_methods_decl.push_back(m);
- m->addRef();
-
- std::cout << "Method: " <<
- m->get_name()->get_value()->get_name()->str() << std::endl;
+ m->addRef();
}
void addAttribute(ASTNode* attribute) throw() {
AttributeDeclaration* attr =
static_cast<AttributeDeclaration*>(attribute);
m_attrib_decl.push_back(attr);
attr->addRef();
-
- std::cout << "Attribute: " <<
- attr->get_variable()->get_value()->get_name()->str() << std::endl;
}
std::list<MethodDeclaration*>& get_methods_decl() throw() {
- return m_methods_decl;
+ return m_methods_decl;
}
std::list<AttributeDeclaration*>& get_attrib_decl() throw() {
- return m_attrib_decl;
+ return m_attrib_decl;
}
private:
std::list<MethodDeclaration*> m_methods_decl;
@@ -950,6 +944,66 @@
DISALLOW_COPY_AND_ASSIGN(ClassStmtList);
};
+class ClassDeclaration : public ASTNode {
+public:
+ ClassDeclaration(ASTNode* name, ASTNode* body)
+ : m_name(name), m_body(body) {
+ m_name->addRef();
+ m_body->addRef();
+ }
+
+ ~ClassDeclaration() {
+ m_name->delRef();
+ m_body->delRef();
+ }
+
+ int check() {
+ ClassStmtList* list = static_cast<ClassStmtList*>(m_body);
+ std::list<AttributeDeclaration*>& attribs = list->get_attrib_decl();
+ std::list<AttributeDeclaration*>::const_iterator it;
+
+ std::set<std::string> s;
+
+ for (it = attribs.begin(); it != attribs.end(); ++it) {
+ if (s.find((*it)->get_variable()->get_value()->get_name()->str()) ==
s.end()) {
+ s.insert((*it)->get_variable()->get_value()->get_name()->str());
+ }
+ else {
+ return 1;
+ }
+ }
+
+ s.clear();
+
+ std::list<MethodDeclaration*>& methods = list->get_methods_decl();
+ std::list<MethodDeclaration*>::const_iterator it2;
+
+ for (it2 = methods.begin(); it2 != methods.end(); ++it2) {
+ if (s.find((*it2)->get_name()->get_value()->get_name()->str()) ==
s.end()) {
+ s.insert((*it2)->get_name()->get_value()->get_name()->str());
+ }
+ else {
+ return 2;
+ }
+ }
+
+ return 0;
+ }
+
+ void accept(ASTVisitor& visitor) throw() {
+ visitor.visit(this);
+ }
+
+ const CString* get_class_name() const {
+ return m_name->get_value()->get_name();
+ }
+
+private:
+ ASTNode* m_name;
+ ASTNode* m_body;
+ DISALLOW_COPY_AND_ASSIGN(ClassDeclaration);
+};
+
}} // clever::ast
#endif // CLEVER_AST_H
=======================================
--- /trunk/include/astvisitor.h Tue May 17 17:14:59 2011
+++ /trunk/include/astvisitor.h Wed May 18 16:09:13 2011
@@ -52,8 +52,7 @@
class BlockNode;
class ArgumentList;
class FuncDeclaration;
-class MethodDeclaration;
-class AttributeDeclaration;
+class ClassDeclaration;
class ReturnStmt;
#define AST_VISITOR(type, exprtype) void type::visit(exprtype* expr)
throw()
@@ -87,8 +86,7 @@
AST_VISITOR_DECL_VIRTUAL(AssignExpr);
AST_VISITOR_DECL_VIRTUAL(ImportStmt);
AST_VISITOR_DECL_VIRTUAL(FuncDeclaration);
- AST_VISITOR_DECL_VIRTUAL(MethodDeclaration);
- AST_VISITOR_DECL_VIRTUAL(AttributeDeclaration);
+ AST_VISITOR_DECL_VIRTUAL(ClassDeclaration);
AST_VISITOR_DECL_VIRTUAL(ReturnStmt);
private:
DISALLOW_COPY_AND_ASSIGN(ASTVisitor);
@@ -146,8 +144,7 @@
AST_VISITOR_DECL(AssignExpr);
AST_VISITOR_DECL(ImportStmt);
AST_VISITOR_DECL(FuncDeclaration);
- AST_VISITOR_DECL(MethodDeclaration);
- AST_VISITOR_DECL(AttributeDeclaration);
+ AST_VISITOR_DECL(ClassDeclaration);
AST_VISITOR_DECL(ReturnStmt);
private:
=======================================
--- /trunk/src/astvisitor.cc Tue May 17 17:16:21 2011
+++ /trunk/src/astvisitor.cc Wed May 18 16:09:13 2011
@@ -29,6 +29,7 @@
#include "astvisitor.h"
#include "typetable.h"
#include "compiler.h"
+#include <iostream> //debug
namespace clever { namespace ast {
@@ -635,17 +636,21 @@
}
/**
- * Generates opcodes for class attributes
+ * Generates opcodes for class declaration
*/
-AST_VISITOR(CodeGenVisitor, AttributeDeclaration) {
- // @TODO
-}
-
-/**
- * Generates opcodes for class methods
- */
-AST_VISITOR(CodeGenVisitor, MethodDeclaration) {
- // @TODO
+AST_VISITOR(CodeGenVisitor, ClassDeclaration) {
+ int check = expr->check();
+ const CString* class_name = expr->get_class_name();
+
+ if (check == 0) {
+
+ }
+ else if (check == 1) {
+ Compiler::errorf(expr->get_location(), "Attribute redefinition in
class %s", class_name->str().c_str());
+ }
+ else {
+ Compiler::errorf(expr->get_location(), "Method redefinition in
class %s", class_name->str().c_str());
+ }
}
}} // clever::ast
=======================================
--- /trunk/src/parser.y Tue May 17 18:39:22 2011
+++ /trunk/src/parser.y Wed May 18 16:09:13 2011
@@ -156,7 +156,7 @@
;
statements:
- expr ';' { tree.top()->add($1); }
+ expr ';' { tree.top()->add($1); }
| variable_declaration ';' { tree.top()->add($1); }
| func_declaration { tree.top()->add($1); }
| if_expr { tree.top()->add($1); }
@@ -190,7 +190,7 @@
;
class_declaration:
- CLASS TYPE '{' class_stmt '}' { $$ = $4; delete $2; }
+ CLASS TYPE '{' class_stmt '}' { $$ = new ast::ClassDeclaration($2, $4); }
;
access_modifier: