From dc397a794eae89e3f0800fb770cd3444a143c114 Mon Sep 17 00:00:00 2001 From: Intelligide Date: Thu, 24 Nov 2016 12:15:08 +0100 Subject: [PATCH] Update --- .gitignore | 5 +- CMakeLists.txt | 5 +- src/CMakeLists.txt | 14 ++-- src/include/SourceLocation.hpp | 37 ++++----- src/include/Statement/AssignStatement.hpp | 12 +++ src/include/Statement/BinaryOperatorExpr.h | 38 +++++++++ src/include/Statement/CallExpr.h | 11 +++ src/include/Statement/CompoundStatement.hpp | 39 ++++++---- src/include/Statement/DoStatement.hpp | 37 +++++---- src/include/Statement/Expr.hpp | 28 +++++++ src/include/Statement/ExprStatement.hpp | 27 ------- src/include/Statement/ForStatement.hpp | 36 +++++++-- src/include/Statement/GotoStatement.hpp | 23 +++--- src/include/Statement/IfStatement.hpp | 50 +++++++----- src/include/Statement/LabelStatement.hpp | 14 ++-- src/include/Statement/NumberLiteralExpr.h | 11 +++ src/include/Statement/OperationKinds.def | 39 ++++++++++ src/include/Statement/OperatorStatement.hpp | 0 src/include/Statement/StringLiteralExpr.h | 24 ++++++ src/include/Statement/WhileStatement.hpp | 2 +- src/lib/SourceLocation.cpp | 63 ++++++++++++++- src/lib/Statement/AssignStatement.cpp | 1 + src/lib/Statement/BinaryOperatorExpr.cpp | 52 +++++++++++++ src/lib/Statement/CallExpr.cpp | 5 ++ src/lib/Statement/CompoundStatement.cpp | 57 +++++++++++++- src/lib/Statement/DoStatement.cpp | 59 +++++++++----- src/lib/Statement/Expr.cpp | 1 + src/lib/Statement/ExprStatement.cpp | 1 - src/lib/Statement/ForStatement.cpp | 85 ++++++++++++++++++++- src/lib/Statement/GotoStatement.cpp | 47 +++++++----- src/lib/Statement/IfStatement.cpp | 78 ++++++++++++------- src/lib/Statement/LabelStatement.cpp | 17 ++++- src/lib/Statement/NumberLiteralExpr.cpp | 1 + src/lib/Statement/OperatorStatement.cpp | 1 - src/lib/Statement/StringLiteralExpr.cpp | 1 + src/lib/lexer.l | 59 ++++++++++++++ src/lib/parser.y | 61 +++++++++++++++ 37 files changed, 843 insertions(+), 198 deletions(-) create mode 100644 src/include/Statement/AssignStatement.hpp create mode 100644 src/include/Statement/BinaryOperatorExpr.h create mode 100644 src/include/Statement/CallExpr.h create mode 100644 src/include/Statement/Expr.hpp delete mode 100644 src/include/Statement/ExprStatement.hpp create mode 100644 src/include/Statement/NumberLiteralExpr.h create mode 100644 src/include/Statement/OperationKinds.def delete mode 100644 src/include/Statement/OperatorStatement.hpp create mode 100644 src/include/Statement/StringLiteralExpr.h create mode 100644 src/lib/Statement/AssignStatement.cpp create mode 100644 src/lib/Statement/BinaryOperatorExpr.cpp create mode 100644 src/lib/Statement/CallExpr.cpp create mode 100644 src/lib/Statement/Expr.cpp delete mode 100644 src/lib/Statement/ExprStatement.cpp create mode 100644 src/lib/Statement/NumberLiteralExpr.cpp delete mode 100644 src/lib/Statement/OperatorStatement.cpp create mode 100644 src/lib/Statement/StringLiteralExpr.cpp create mode 100644 src/lib/lexer.l create mode 100644 src/lib/parser.y diff --git a/.gitignore b/.gitignore index ac4138a..3850f7a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ - -*.gch - +.idea/ +cmake-build-debug/ # Windows image file caches Thumbs.db diff --git a/CMakeLists.txt b/CMakeLists.txt index c1b20c7..b77818e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,13 +24,16 @@ endif() set(CMAKE_CXX_FLAGS "-std=c++11") +find_package(BISON REQUIRED) +find_package(FLEX REQUIRED) + add_subdirectory(utils) add_subdirectory(tools) add_subdirectory(src) option(BUILD_UNIT_TESTS "Build unit tests" OFF) if(BUILD_UNIT_TESTS) - add_subdirectory(unittests) + add_subdirectory(tests) endif() option(BUILD_DOC "Build documentation" OFF) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6d3fd6d..f9fac89 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,10 @@ set(Blang_VERSION 1.0.0) set(Blang_RELEASE 1) configure_file(include/version.h.in include/version.h @ONLY) +BISON_TARGET(BlangParser parser.y lib/parser_gen.cpp) +FLEX_TARGET(BlangScanner lexer.l lib/lexer_gen.cpp) +ADD_FLEX_BISON_DEPENDENCY(BlangScanner BlangParser) + file( GLOB_RECURSE sources_files @@ -28,12 +32,12 @@ add_library( ) target_link_libraries( - Blang - libblang + Blang + libblang ) target_link_libraries( - libblang - libpreblang - blangutils + libblang + libpreblang + blangutils ) diff --git a/src/include/SourceLocation.hpp b/src/include/SourceLocation.hpp index 1f178c9..010c24b 100644 --- a/src/include/SourceLocation.hpp +++ b/src/include/SourceLocation.hpp @@ -3,31 +3,34 @@ #include -class SourceLocation{ +class SourceLocation +{ public: + SourceLocation(); + SourceLocation(std::string file, int line, int column, int offset); - bool isValid() const; - void print() const; - std::string printToString() const; + bool isValid() const; - std::string file(); - void setFile(std::string); + void print() const; + std::string printToString() const; - int lineLocation(); - void setLineLocation(int); + std::string file() const; + void setFile(std::string); - int columnLocation(); - void setColumnLocation(int); + int lineLocation() const; + void setLineLocation(int); - int offset(); - void setOffset(int); + int columnLocation() const; + void setColumnLocation(int); + + int offset() const; + void setOffset(int); private: - std::string _file; - int _offset; - - int _column; - int _line; + std::string _file; + int _offset; + int _column; + int _line; }; diff --git a/src/include/Statement/AssignStatement.hpp b/src/include/Statement/AssignStatement.hpp new file mode 100644 index 0000000..f0b9edb --- /dev/null +++ b/src/include/Statement/AssignStatement.hpp @@ -0,0 +1,12 @@ +#ifndef BLANG_ASSIGNSTATEMENT_H +#define BLANG_ASSIGNSTATEMENT_H + +#include "Statement.hpp" + +class AssignStatement: public Statement +{ + +}; + + +#endif //BLANG_ASSIGNSTATEMENT_H diff --git a/src/include/Statement/BinaryOperatorExpr.h b/src/include/Statement/BinaryOperatorExpr.h new file mode 100644 index 0000000..fb7d07b --- /dev/null +++ b/src/include/Statement/BinaryOperatorExpr.h @@ -0,0 +1,38 @@ +#ifndef BLANG_BINARYOPERATOREXPR_H +#define BLANG_BINARYOPERATOREXPR_H + +#include "Expr.hpp" + +class BinaryOperatorExpr: public Expr +{ +public: + enum Opcode { +#define BINARY_OPERATION(Name, Spelling) BO_##Name, +#include "OperationKinds.def" + }; + + BinaryOperatorExpr(Expr *lhs, Expr *rhs, Opcode opc); + + Expr *getLHS () const; + void setLHS (Expr *E); + + Expr *getRHS () const; + void setRHS (Expr *E); + + Opcode getOpcode() const; + void setOpcode (Opcode O); + std::string getOpcodeStr() const; + + static std::string getOpcodeStr(Opcode opc) const; + +private: + Opcode _opc; + enum ExprIdentifier + { + LHS, RHS + }; + std::map _subExprs; +}; + + +#endif //BLANG_BINARYOPERATOREXPR_H diff --git a/src/include/Statement/CallExpr.h b/src/include/Statement/CallExpr.h new file mode 100644 index 0000000..0975ed7 --- /dev/null +++ b/src/include/Statement/CallExpr.h @@ -0,0 +1,11 @@ +#ifndef CALLEXPR_H +#define CALLEXPR_H + +/*!Represents a function call */ +class CallExpr: public Expr +{ + +}; + + +#endif //CALLEXPR_H diff --git a/src/include/Statement/CompoundStatement.hpp b/src/include/Statement/CompoundStatement.hpp index ae5b984..82ee92d 100644 --- a/src/include/Statement/CompoundStatement.hpp +++ b/src/include/Statement/CompoundStatement.hpp @@ -1,27 +1,38 @@ -#ifndef IFSTATEMENT_HPP -#define IFSTATEMENT_HPP +#ifndef COMPOUNDSTATEMENT_HPP +#define COMPOUNDSTATEMENT_HPP + +#include #include "Statement.hpp" -class IfStatement: public Statement{ +class CompoundStatement : public Statement +{ public: + typedef std::vector::iterator body_iterator; + typedef std::vector::const_iterator const_body_iterator; + typedef std::vector::reverse_iterator reverse_body_iterator; + typedef std::vector::const_reverse_iterator const_reverse_body_iterator; - void getCond(); - void getCond() const; - void setCond(); + unsigned int size () const; + bool empty() const; - void getThen(); - void getThen() const; - void setThen(); + void setLastStatement(Statement *S); - void getElse(); - void getElse() const; - void setElse(); + const_body_iterator body_begin() const; + body_iterator body_begin(); - void getIfLocation(); - void getThenLocation(); + const_body_iterator body_end() const; + body_iterator body_end(); + const_reverse_body_iterator body_rbegin() const; + reverse_body_iterator body_rbegin(); + + const_reverse_body_iterator body_rend() const; + reverse_body_iterator body_rend(); + +private: + std::vector _body; }; #endif \ No newline at end of file diff --git a/src/include/Statement/DoStatement.hpp b/src/include/Statement/DoStatement.hpp index 8a7bbd3..16c7de6 100644 --- a/src/include/Statement/DoStatement.hpp +++ b/src/include/Statement/DoStatement.hpp @@ -2,33 +2,38 @@ #define DOSTATEMENT_HPP #include "Statement.hpp" -#include "ExprStatement.hpp" +#include "Expr.hpp" #include "SourceLocation.hpp" -class DoStatement: public Statement{ +class DoStatement : public Statement +{ public: + DoStatement(Statement *body, Expr *cond, SourceLocation DL, SourceLocation WL); - ExprStatement* getCond(); - const ExprStatement* getCond() const; - void setCond(ExprStatement*); + Expr *getCond(); + const Expr *getCond() const; + void setCond(Expr *); - Statement* getBody(); - const Statement* getBody() const; - void setBody(Statement*); + Statement *getBody(); + const Statement *getBody() const; + void setBody(Statement *); - SourceLocation getDoLocation() const; - void setDoLocation(SourceLocation); + SourceLocation getDoLocation() const; + void setDoLocation(SourceLocation); - SourceLocation getWhileLocation() const; - void setWhileLocation(SourceLocation); + SourceLocation getWhileLocation() const; + void setWhileLocation(SourceLocation); private: - SourceLocation _DoLoc; - enum { BODY, COND, END_EXPR }; - Statement* _subExprs[END_EXPR]; - SourceLocation _WhileLoc; + SourceLocation _doLoc; + enum StmtIdentifier + { + BODY, COND + }; + std::map _subStmts; + SourceLocation _whileLoc; }; diff --git a/src/include/Statement/Expr.hpp b/src/include/Statement/Expr.hpp new file mode 100644 index 0000000..82e1f64 --- /dev/null +++ b/src/include/Statement/Expr.hpp @@ -0,0 +1,28 @@ +#ifndef EXPRSTATEMENT_HPP +#define EXPRSTATEMENT_HPP + +#include "Statement.hpp" + +class Expr : public Statement +{ + +public: + + void getCond(); + void getCond() const; + void setCond(); + + void getThen(); + void getThen() const; + void setThen(); + + void getElse(); + void getElse() const; + void setElse(); + + void getIfLocation(); + void getThenLocation(); + +}; + +#endif \ No newline at end of file diff --git a/src/include/Statement/ExprStatement.hpp b/src/include/Statement/ExprStatement.hpp deleted file mode 100644 index 1528a56..0000000 --- a/src/include/Statement/ExprStatement.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef EXPRSTATEMENT_HPP -#define EXPRSTATEMENT_HPP - -#include "Statement.hpp" - -class ExprStatement: public Statement{ - -public: - - void getCond(); - void getCond() const; - void setCond(); - - void getThen(); - void getThen() const; - void setThen(); - - void getElse(); - void getElse() const; - void setElse(); - - void getIfLocation(); - void getThenLocation(); - -}; - -#endif \ No newline at end of file diff --git a/src/include/Statement/ForStatement.hpp b/src/include/Statement/ForStatement.hpp index f13c73c..a0fdfc5 100644 --- a/src/include/Statement/ForStatement.hpp +++ b/src/include/Statement/ForStatement.hpp @@ -3,17 +3,41 @@ #include "Statement.hpp" #include "SourceLocation.hpp" +#include "AssignStatement.hpp" -class ForStatement: public Statement{ +class ForStatement : public Statement +{ public: - Statement* getBody(); - const Statement* getBody() const; - void setBody(Statement*); + ForStatement(SourceLocation forLocation, AssignStatement* init, Expr* cond, Statement* body, Expr* inc = NULL); - SourceLocation getForLocation() const; - void setForLocation(SourceLocation); + AssignStatement *getInit(); + const AssignStatement *getInit() const; + void setInit(AssignStatement *); + Expr *getCond(); + const Expr *getCond() const; + void setCond(Expr *); + + Expr *getInc(); + const Expr *getInc() const; + void setInc(Expr *); + + Statement *getBody(); + const Statement *getBody() const; + void setBody(Statement *); + + SourceLocation getForLocation() const; + void setForLocation(SourceLocation); + +private: + SourceLocation _forLocation; + + enum StmtIdentifier + { + INIT, BODY, COND, INC + }; + std::map _subStmts; }; #endif \ No newline at end of file diff --git a/src/include/Statement/GotoStatement.hpp b/src/include/Statement/GotoStatement.hpp index 729fbfb..2b3e744 100644 --- a/src/include/Statement/GotoStatement.hpp +++ b/src/include/Statement/GotoStatement.hpp @@ -6,21 +6,24 @@ #include "SourceLocation.hpp" -class GotoStatement: public Statement{ +class GotoStatement : public Statement +{ public: - LabelStatement* getLabel(); - const LabelStatement* getLabel() const; - void setLabel(LabelStatement*); + GotoStatement(LabelDecl* label, SourceLocation gl, SourceLocation ll); - SourceLocation getGotoLocation() const; - void setGotoLocation(SourceLocation); + LabelDecl *getLabel(); + const LabelDecl *getLabel() const; + void setLabel(LabelDecl *); - SourceLocation getLabelLocation() const; - void setLabelLocation(SourceLocation); + SourceLocation getGotoLocation() const; + void setGotoLocation(SourceLocation); + + SourceLocation getLabelLocation() const; + void setLabelLocation(SourceLocation); private: - SourceLocation _GotoLoc; - LabelStatement* _Label; + SourceLocation _gotoLoc; + LabelDecl *_label; }; diff --git a/src/include/Statement/IfStatement.hpp b/src/include/Statement/IfStatement.hpp index fe74687..99537a1 100644 --- a/src/include/Statement/IfStatement.hpp +++ b/src/include/Statement/IfStatement.hpp @@ -1,38 +1,48 @@ #ifndef IFSTATEMENT_HPP #define IFSTATEMENT_HPP +#include + #include "Statement.hpp" -#include "ExprStatement.hpp" +#include "Expr.hpp" #include "SourceLocation.hpp" - -class IfStatement: public Statement{ +class IfStatement : public Statement +{ public: + IfStatement(SourceLocation IfLocation, Expr* cond, Statement* then, SourceLocation EL = SourceLocation(), Statement* elses); - ExprStatement* getCond(); - const ExprStatement* getCond() const; - void setCond(ExprStatement*); + ExprStatement* getCond(); + const ExprStatement *getCond() const; + void setCond(ExprStatement *); - Statement* getThen(); - const Statement* getThen() const; - void setThen(Statement*); + Statement *getThen(); + const Statement *getThen() const; + void setThen(Statement *); - Statement* getElse(); - const Statement* getElse() const; - void setElse(Statement*); + Statement *getElse(); + const Statement *getElse() const; + void setElse(Statement *); - SourceLocation getIfLocation() const; - void setIfLocation(SourceLocation); - SourceLocation getThenLocation() const; - void setThenLocation(SourceLocation); + SourceLocation getIfLocation() const; + void setIfLocation(SourceLocation); + + SourceLocation getThenLocation() const; + void setThenLocation(SourceLocation); + + SourceLocation getElseLocation() const; + void setElseLocation(SourceLocation); private: - enum { VAR, COND, THEN, ELSE, END_EXPR }; - Statement* _subExprs[END_EXPR]; + enum StmtIdentifier + { + COND, THEN, ELSE + }; + std::map _subStmts; - SourceLocation _IfLoc; - SourceLocation _ElseLoc; + SourceLocation _IfLoc; + SourceLocation _ElseLoc; }; diff --git a/src/include/Statement/LabelStatement.hpp b/src/include/Statement/LabelStatement.hpp index d7c5f38..e1e1d8c 100644 --- a/src/include/Statement/LabelStatement.hpp +++ b/src/include/Statement/LabelStatement.hpp @@ -2,20 +2,22 @@ #define LABELSTATEMENT_HPP #include "Statement.hpp" -#include "ExprStatement.hpp" +#include "Expr.hpp" #include "SourceLocation.hpp" -class LabelStatement: public Statement{ +class LabelStatement : public Statement +{ public: + LabelStatement(SourceLocation ll, LabelDecl* decl); - SourceLocation getLabelLocation() const; - void setLabelLocation(SourceLocation); + SourceLocation getLabelLocation() const; + void setLabelLocation(SourceLocation); private: - SourceLocation _LabelLoc; - + SourceLocation _labelLoc; + LabelDecl* _decl; }; #endif \ No newline at end of file diff --git a/src/include/Statement/NumberLiteralExpr.h b/src/include/Statement/NumberLiteralExpr.h new file mode 100644 index 0000000..a4b2cdd --- /dev/null +++ b/src/include/Statement/NumberLiteralExpr.h @@ -0,0 +1,11 @@ +#ifndef BLANG_NUMBERLITERALEXPR_H +#define BLANG_NUMBERLITERALEXPR_H + + +class NumberLiteralExpr +{ + +}; + + +#endif //BLANG_NUMBERLITERALEXPR_H diff --git a/src/include/Statement/OperationKinds.def b/src/include/Statement/OperationKinds.def new file mode 100644 index 0000000..cfc0dfd --- /dev/null +++ b/src/include/Statement/OperationKinds.def @@ -0,0 +1,39 @@ +//===--- OperationKinds.def - Operations Database ---------------*- C++ -*-===// +// +// This file enumerates the different kinds of operations that can be +// performed by various expressions. +// +//===----------------------------------------------------------------------===// +// +/// @file OperationKinds.def +/// +/// Macros had one or two arguments: +/// +/// Name: The name of the operation. Name (prefixed with CK_, UO_ or BO_) will +/// be the name of the corresponding enumerator (see OperationsKinds.h). +/// +/// Spelling: A string that provides a canonical spelling for the operation. + +#ifndef BINARY_OPERATION +# define BINARY_OPERATION(Name, Spelling) +#endif + +//===- Binary Operations -------------------------------------------------===// +// Operators listed in order of precedence. +// Note that additions to this should also update the StmtVisitor class. + +BINARY_OPERATION(Mul, "*") +BINARY_OPERATION(Div, "/") +BINARY_OPERATION(Add, "+") +BINARY_OPERATION(Sub, "-") +// Relational operators. +BINARY_OPERATION(LT, "<") +BINARY_OPERATION(GT, ">") +BINARY_OPERATION(LE, "<=") +BINARY_OPERATION(GE, ">=") +// Equality operators. +BINARY_OPERATION(EQ, "=") +BINARY_OPERATION(NE, "!=") + +#undef CAST_OPERATION +#undef BINARY_OPERATION diff --git a/src/include/Statement/OperatorStatement.hpp b/src/include/Statement/OperatorStatement.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/include/Statement/StringLiteralExpr.h b/src/include/Statement/StringLiteralExpr.h new file mode 100644 index 0000000..c307110 --- /dev/null +++ b/src/include/Statement/StringLiteralExpr.h @@ -0,0 +1,24 @@ +#ifndef BLANG_STRINGLITERALEXPR_H +#define BLANG_STRINGLITERALEXPR_H + + +class StringLiteralExpr +{ +public: + StringLiteralExpr(std::string str, SourceLocation loc); + + unsigned int getLength () const; + + void setString(std::string str); + std::string getString() const; + + SourceLocation getLocStart() const; + SourceLocation getLocEnd() const; + +private: + std::string _str; + SourceLocation _loc; +}; + + +#endif //BLANG_STRINGLITERALEXPR_H diff --git a/src/include/Statement/WhileStatement.hpp b/src/include/Statement/WhileStatement.hpp index 6c10fb1..29fe561 100644 --- a/src/include/Statement/WhileStatement.hpp +++ b/src/include/Statement/WhileStatement.hpp @@ -2,7 +2,7 @@ #define WHILESTATEMENT_HPP #include "Statement.hpp" -#include "ExprStatement.hpp" +#include "Expr.hpp" #include "SourceLocation.hpp" diff --git a/src/lib/SourceLocation.cpp b/src/lib/SourceLocation.cpp index 0461eae..0c5a84e 100644 --- a/src/lib/SourceLocation.cpp +++ b/src/lib/SourceLocation.cpp @@ -1 +1,62 @@ -#include "SourceLocation.hpp" \ No newline at end of file +#include + +#include "SourceLocation.hpp" + +SourceLocation::SourceLocation(std::string file, int line, int column, int offset): + _file(file), + _line(line), + _column(column), + _offset(offset) +{ + +} + +void SourceLocation::print() const +{ + std::cout << printToString(); +} + +std::string SourceLocation::printToString() const +{ + return file() + ":" + std::to_string(lineLocation()) +":" + std::to_string(columnLocation()); +} + +std::string SourceLocation::file() const +{ + return _file; +} + +void SourceLocation::setFile(std::string file) +{ + _file = file; +} + +int SourceLocation::lineLocation() const +{ + return _line; +} + +void SourceLocation::setLineLocation(int l) +{ + _line = l; +} + +int SourceLocation::columnLocation() const +{ + return _column; +} + +void SourceLocation::setColumnLocation(int c) +{ + _column = c; +} + +int SourceLocation::offset() const +{ + return _offset; +} + +void SourceLocation::setOffset(int offset) +{ + _offset = offset; +} \ No newline at end of file diff --git a/src/lib/Statement/AssignStatement.cpp b/src/lib/Statement/AssignStatement.cpp new file mode 100644 index 0000000..d5918b7 --- /dev/null +++ b/src/lib/Statement/AssignStatement.cpp @@ -0,0 +1 @@ +#include "../../include/Statement/AssignStatement.hpp" diff --git a/src/lib/Statement/BinaryOperatorExpr.cpp b/src/lib/Statement/BinaryOperatorExpr.cpp new file mode 100644 index 0000000..e0f4a61 --- /dev/null +++ b/src/lib/Statement/BinaryOperatorExpr.cpp @@ -0,0 +1,52 @@ +#include "Statement/BinaryOperatorExpr.h" + +static std::string BinaryOperatorExpr::getOpcodeStr(BinaryOperatorExpr::Opcode opc) const +{ + switch (opc) { +#define BINARY_OPERATION(Name, Spelling) case BO_##Name: return Spelling; + +#include "OperationKinds.def" + } +} + +BinaryOperatorExpr::BinaryOperatorExpr(Expr *lhs, Expr *rhs, BinaryOperatorExpr::Opcode opc): + _opc(opc) +{ + _subExprs[LHS]= lhs; + _subExprs[RHS]= rhs; +} + +Expr *BinaryOperatorExpr::getLHS() const +{ +} + +void BinaryOperatorExpr::setLHS(Expr *E) +{ + _subExprs[LHS] = E; +} + +Expr *BinaryOperatorExpr::getRHS() const +{ + return _subExprs[RHS]; +} + +void BinaryOperatorExpr::setRHS(Expr *E) +{ + _subExprs[RHS] = E; +} + +BinaryOperatorExpr::Opcode BinaryOperatorExpr::getOpcode() const +{ + return _opc; +} + +void BinaryOperatorExpr::setOpcode(BinaryOperatorExpr::Opcode O) +{ + _opc = O; +} + +std::string BinaryOperatorExpr::getOpcodeStr() const +{ + return getOpcodeStr(_opc); +} + diff --git a/src/lib/Statement/CallExpr.cpp b/src/lib/Statement/CallExpr.cpp new file mode 100644 index 0000000..6074cc6 --- /dev/null +++ b/src/lib/Statement/CallExpr.cpp @@ -0,0 +1,5 @@ +// +// Created by yoann on 24/11/16. +// + +#include "../../include/Statement/CallExpr.h" diff --git a/src/lib/Statement/CompoundStatement.cpp b/src/lib/Statement/CompoundStatement.cpp index 150caf2..2f80cd9 100644 --- a/src/lib/Statement/CompoundStatement.cpp +++ b/src/lib/Statement/CompoundStatement.cpp @@ -1 +1,56 @@ -#include "Statement/CompoundStatement.hpp" \ No newline at end of file +#include "Statement/CompoundStatement.hpp" + +unsigned int CompoundStatement::size() const +{ + return _body.size(); +} + +bool CompoundStatement::empty() const +{ + return _body.empty(); +} + +void CompoundStatement::setLastStatement(Statement *S) +{ + _body.push_back(S); +} + +const_body_iterator CompoundStatement::body_begin() const +{ + return _body.begin(); +} + +body_iterator CompoundStatement::body_begin() +{ + return _body.begin(); +} + +const_body_iterator CompoundStatement::body_end() const +{ + return _body.end(); +} + +body_iterator CompoundStatement::body_end() +{ + return _body.end(); +} + +const_reverse_body_iterator CompoundStatement::body_rbegin() const +{ + return _body.rbegin(); +} + +reverse_body_iterator CompoundStatement::body_rbegin() +{ + return _body.rbegin(); +} + +const_reverse_body_iterator CompoundStatement::body_rend() const +{ + return _body.rend(); +} + +reverse_body_iterator CompoundStatement::body_rend() +{ + return _body.rend(); +} \ No newline at end of file diff --git a/src/lib/Statement/DoStatement.cpp b/src/lib/Statement/DoStatement.cpp index 5062132..52f6b83 100644 --- a/src/lib/Statement/DoStatement.cpp +++ b/src/lib/Statement/DoStatement.cpp @@ -1,41 +1,60 @@ #include "Statement/DoStatement.hpp" -ExprStatement* DoStatement::getCond(){ - return reinterpret_cast(_subExprs[COND]); +DoStatement::DoStatement(Statement *body, Expr *cond, SourceLocation DL, SourceLocation WL): + _doLoc(DL), + _whileLoc(WL) +{ + _subStmts[BODY] = body; + _subStmts[COND] = cond; } -const ExprStatement* DoStatement::getCond() const{ - return getCond(); + +Expr *DoStatement::getCond() +{ + return reinterpret_cast(_subStmts[COND]); } -void DoStatement::setCond(ExprStatement* e){ - _subExprs[COND] = reinterpret_cast(e); +const Expr *DoStatement::getCond() const +{ + return reinterpret_cast(_subStmts[COND]); } -Statement* DoStatement::getBody(){ - return _subExprs[BODY]; +void DoStatement::setCond(Expr *e) +{ + _subStmts[COND] = reinterpret_cast(e); } -const Statement* DoStatement::getBody() const{ - return getBody(); +Statement *DoStatement::getBody() +{ + return _subStmts[BODY]; } -void DoStatement::setBody(Statement* s){ - _subExprs[BODY] = s; +const Statement *DoStatement::getBody() const +{ + return _subStmts[BODY]; } -SourceLocation DoStatement::getDoLocation() const{ - return _DoLoc; +void DoStatement::setBody(Statement *s) +{ + _subStmts[BODY] = s; } -void DoStatement::setDoLocation(SourceLocation loc){ - _DoLoc = loc; +SourceLocation DoStatement::getDoLocation() const +{ + return _doLoc; } -SourceLocation DoStatement::getWhileLocation() const{ - return _WhileLoc; +void DoStatement::setDoLocation(SourceLocation loc) +{ + _doLoc = loc; } -void DoStatement::setWhileLocation(SourceLocation loc){ - _WhileLoc = loc; +SourceLocation DoStatement::getWhileLocation() const +{ + return _whileLoc; +} + +void DoStatement::setWhileLocation(SourceLocation loc) +{ + _whileLoc = loc; } \ No newline at end of file diff --git a/src/lib/Statement/Expr.cpp b/src/lib/Statement/Expr.cpp new file mode 100644 index 0000000..54d7667 --- /dev/null +++ b/src/lib/Statement/Expr.cpp @@ -0,0 +1 @@ +#include "Statement/Expr.hpp" \ No newline at end of file diff --git a/src/lib/Statement/ExprStatement.cpp b/src/lib/Statement/ExprStatement.cpp deleted file mode 100644 index 28bc22f..0000000 --- a/src/lib/Statement/ExprStatement.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "Statement/ExprStatement.hpp" \ No newline at end of file diff --git a/src/lib/Statement/ForStatement.cpp b/src/lib/Statement/ForStatement.cpp index 36acb0f..bd2e18a 100644 --- a/src/lib/Statement/ForStatement.cpp +++ b/src/lib/Statement/ForStatement.cpp @@ -1 +1,84 @@ -#include "Statement/ForStatement.hpp" \ No newline at end of file +#include +#include "Statement/ForStatement.hpp" + +ForStatement::ForStatement(SourceLocation forLocation, AssignStatement *init, Expr *cond, Statement *body, + Expr *inc): + _forLocation(forLocation) +{ + _subStmts[INIT] = init; + _subStmts[COND] = cond; + _subStmts[BODY] = body; + if(inc) + _subStmts[INC] = inc; + +} + +AssignStatement *ForStatement::getInit() +{ + return reinterpret_cast(_subStmts[INIT]); +} + +const AssignStatement *ForStatement::getInit() const +{ + return reinterpret_cast(_subStmts[INIT]); +} + +void ForStatement::setInit(AssignStatement *init) +{ + _subStmts[INIT] = init; +} + +Expr *ForStatement::getCond() +{ + return reinterpret_cast(_subStmts[COND]); +} + +const Expr *ForStatement::getCond() const +{ + return reinterpret_cast(_subStmts[COND]); +} + +void ForStatement::setCond(Expr *cond) +{ + _subStmts[COND] = cond; +} + +Expr *ForStatement::getInc() +{ + return reinterpret_cast(_subStmts[INC]); +} + +const Expr *ForStatement::getInc() const +{ + return reinterpret_cast(_subStmts[INC]); +} + +void ForStatement::setInc(Expr *inc) +{ + _subStmts[INC] = inc; +} + +Statement *ForStatement::getBody() +{ + return _subStmts[BODY]; +} + +const Statement *ForStatement::getBody() const +{ + return _subStmts[BODY]; +} + +void ForStatement::setBody(Statement *body) +{ + _subStmts[BODY] = body; +} + +SourceLocation ForStatement::getForLocation() const +{ + return _forLocation; +} + +void ForStatement::setForLocation(SourceLocation loc) +{ + _forLocation = loc; +} \ No newline at end of file diff --git a/src/lib/Statement/GotoStatement.cpp b/src/lib/Statement/GotoStatement.cpp index c86a3fc..1acaaf6 100644 --- a/src/lib/Statement/GotoStatement.cpp +++ b/src/lib/Statement/GotoStatement.cpp @@ -1,32 +1,45 @@ #include "Statement/GotoStatement.hpp" -LabelStatement* GotoStatement::getLabel(){ - return _Label; +GotoStatement::GotoStatement(LabelDecl* label, SourceLocation gl, SourceLocation ll): + _label(label), + _gotoLoc(gl) +{ + } -const LabelStatement* GotoStatement::getLabel() const{ - return getLabel(); +LabelDecl *GotoStatement::getLabel() +{ + return _label; } - -void GotoStatement::setLabel(LabelStatement* lbl){ - _Label = lbl; +const LabelDecl *GotoStatement::getLabel() const +{ + return _label; } -SourceLocation GotoStatement::getGotoLocation() const{ - return _GotoLoc; +void GotoStatement::setLabel(LabelDecl * lbl) +{ + _label = lbl; } -void GotoStatement::setGotoLocation(SourceLocation loc){ - _GotoLoc = loc; +SourceLocation GotoStatement::getGotoLocation() const +{ + return _gotoLoc; } -SourceLocation GotoStatement::getLabelLocation() const{ - if(_Label) - return _Label->getLabelLocation(); +void GotoStatement::setGotoLocation(SourceLocation loc) +{ + _gotoLoc = loc; } -void GotoStatement::setLabelLocation(SourceLocation loc){ - if(_Label) - return _Label->setLabelLocation(loc); +SourceLocation GotoStatement::getLabelLocation() const +{ + if (_Label) + return _Label->getLabelLocation(); +} + +void GotoStatement::setLabelLocation(SourceLocation loc) +{ + if (_Label) + return _Label->setLabelLocation(loc); } \ No newline at end of file diff --git a/src/lib/Statement/IfStatement.cpp b/src/lib/Statement/IfStatement.cpp index 4492ef8..238031d 100644 --- a/src/lib/Statement/IfStatement.cpp +++ b/src/lib/Statement/IfStatement.cpp @@ -1,53 +1,79 @@ #include "Statement/IfStatement.hpp" -ExprStatement* IfStatement::getCond(){ - return reinterpret_cast(_subExprs[COND]); +IfStatement::IfStatement(SourceLocation IfLocation, Expr* cond, Statement* then, SourceLocation ElseLocation = SourceLocation(), Statement* elses = NULL): + _IfLoc(IfLocation), + _ElseLoc(ElseLocation) +{ + _subStmts[COND] = cond; + _subStmts[THEN] = then; + + if(elses) + _subStmts[ELSE] = elses; } -const ExprStatement* IfStatement::getCond() const{ - return getCond(); + +Expr* IfStatement::getCond() +{ + return reinterpret_cast(_subStmts[COND]); } -void IfStatement::setCond(ExprStatement* e){ - _subExprs[COND] = reinterpret_cast(e); +const Expr *IfStatement::getCond() const +{ + return reinterpret_cast(_subStmts[COND]); } -Statement* IfStatement::getThen(){ - return _subExprs[THEN]; +void IfStatement::setCond(Expr *e) +{ + _subStmts[COND] = reinterpret_cast(e); } -const Statement* IfStatement::getThen() const{ - return getThen(); +Statement *IfStatement::getThen() +{ + return _subStmts[THEN]; } -void IfStatement::setThen(Statement* s){ - _subExprs[THEN] = s; +const Statement *IfStatement::getThen() const +{ + SourceLocation getThenLocation() const; + void setThenLocation(SourceLocation); } -Statement* IfStatement::getElse(){ - return _subExprs[ELSE]; +void IfStatement::setThen(Statement *s) +{ + _subStmts[THEN] = s; } -const Statement* IfStatement::getElse() const{ - return getElse(); +Statement *IfStatement::getElse() +{ + return _subStmts[ELSE]; } -void IfStatement::setElse(Statement* s){ - _subExprs[ELSE] = s; +const Statement *IfStatement::getElse() const +{ + return _subStmts[ELSE]; } -SourceLocation IfStatement::getIfLocation() const{ - return _IfLoc; +void IfStatement::setElse(Statement *s) +{ + _subStmts[ELSE] = s; } -void IfStatement::setIfLocation(SourceLocation loc){ - _IfLoc = loc; +SourceLocation IfStatement::getIfLocation() const +{ + return _IfLoc; } -SourceLocation IfStatement::getThenLocation() const{ - return _ElseLoc; +void IfStatement::setIfLocation(SourceLocation loc) +{ + _IfLoc = loc; } -void IfStatement::setThenLocation(SourceLocation loc){ - _ElseLoc = loc; +SourceLocation IfStatement::getThenLocation() const +{ + return _ElseLoc; +} + +void IfStatement::setThenLocation(SourceLocation loc) +{ + _ElseLoc = loc; } diff --git a/src/lib/Statement/LabelStatement.cpp b/src/lib/Statement/LabelStatement.cpp index 1004a2d..ccaff82 100644 --- a/src/lib/Statement/LabelStatement.cpp +++ b/src/lib/Statement/LabelStatement.cpp @@ -1,9 +1,18 @@ #include "Statement/LabelStatement.hpp" -SourceLocation LabelStatement::getLabelLocation() const{ - return _LabelLoc; +LabelStatement::LabelStatement(SourceLocation ll, LabelDecl* decl): + _decl(decl), + _labelLoc(ll) +{ + } -void LabelStatement::setLabelLocation(SourceLocation loc){ - _LabelLoc = loc; +SourceLocation LabelStatement::getLabelLocation() const +{ + return _labelLoc; +} + +void LabelStatement::setLabelLocation(SourceLocation loc) +{ + _labelLoc = loc; } diff --git a/src/lib/Statement/NumberLiteralExpr.cpp b/src/lib/Statement/NumberLiteralExpr.cpp new file mode 100644 index 0000000..dfe54b0 --- /dev/null +++ b/src/lib/Statement/NumberLiteralExpr.cpp @@ -0,0 +1 @@ +#include "Statement/NumberLiteralExpr.h" diff --git a/src/lib/Statement/OperatorStatement.cpp b/src/lib/Statement/OperatorStatement.cpp deleted file mode 100644 index 483a3bb..0000000 --- a/src/lib/Statement/OperatorStatement.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "Statement/OperatorStatement.hpp" \ No newline at end of file diff --git a/src/lib/Statement/StringLiteralExpr.cpp b/src/lib/Statement/StringLiteralExpr.cpp new file mode 100644 index 0000000..150e94e --- /dev/null +++ b/src/lib/Statement/StringLiteralExpr.cpp @@ -0,0 +1 @@ +#include "Statement/StringLiteralExpr.h" diff --git a/src/lib/lexer.l b/src/lib/lexer.l new file mode 100644 index 0000000..a2da5de --- /dev/null +++ b/src/lib/lexer.l @@ -0,0 +1,59 @@ +%{ +#include +#include + +#include "Token.hpp" + +using namespace std; + +string val; +int nbLines = 0; +int nbChars = 0; +%} + +%option noyywrap + +%x strEnv + +integer [0-9]+ +real [0-9]+\.[0-9]*|\.[0-9]+ +value {integer}|{real} +operator "+"|"-"|"/"|"*"|"=>"|"<="|">="|"="|"->" +keyword "+"|"-"|"/"|"*"|"=>"|"<="|">="|"="|"->" +ident [a-zA-Z_][0-9a-zA-Z_]* + +%% + +{value} { val=yytext; return(Token::Token_NumberLiteral); } +{operator} { val=yytext; return(Token::Token_Operator); } +{keyword} { val=yytext; return(Token::Token_Keyword); } +{ident} { val=yytext; return(Token::Token_Identifier); } +"\"" { val.clear(); BEGIN(strEnv); } +"\"" { BEGIN(INITIAL); return(Token::Token_StringLiteral); } +"\n" { cerr << "multi-line strings not allowed" << endl; ++nbLines; } +"\\\n" { val+='\n'; ++nbLines; } // line cut by \ +"\\". { val+=yytext[1]; } +<> { BEGIN(INITIAL); return(STRING); } +. { val+=yytext[0]; } +[ \t]+ { /* nothing to be done */ } +"\n" { ++nbLines; } +. { val=yytext; return(Token::Token_Unknown); } + +%% +int main(int argc,char ** argv) +{ + int token; + if(argc>1) + yyin=fopen(argv[1],"r"); // check result !!! + TODO + do { + token=yylex(); + switch(token) { + case STRING: cerr << "STRING[" << val << "]" << endl; break; + case INTEGER: cerr << "INTEGER[" << val << "]" << endl; break; + } + } while(token); + + cerr << nbLines << " lines" << endl; + return(0); +} \ No newline at end of file diff --git a/src/lib/parser.y b/src/lib/parser.y new file mode 100644 index 0000000..9e08efc --- /dev/null +++ b/src/lib/parser.y @@ -0,0 +1,61 @@ +#include +#include +using namespace std; +int yylex(void); // defini dans progL.cpp, utilise par yyparse() +void yyerror(const char * msg); // defini plus loin, utilise par yyparse() +int lineNumber; // notre compteur de lignes +extern FILE * yyin; // defini dans progL.cpp, utilise par main() + +%} + +%token START END // les lexemes que doit fournir yylex() +%token ASSIGN SEMICOLON +%token IDENT REAL INTEGER +%start program // l'axiome de notre grammaire + +%% + +program : stmtList +; +stmtList : stmtList stmt { CompoundStatement* stmt = $1; stmt.setLastStatement($2); $$ = stmt; } + | stmt { CompoundStatement* stmt = new CompoundStatement(); stmt.setLastStatement($1); $$ = stmt; } +; +stmt : assign + | ifStmt + | WHILE expr EOL stmtList EOL WHILEEND {} + | DO EOL stmtList EOL LPWHILE expr {} + | FOR assignStmt TO expr EOL stmtList EOL NEXT { $$ = new ForStatement($2, $4, $6);} + | FOR assignStmt TO expr STEP expr EOL stmtList EOL NEXT { $$ = new ForStatement($2, $4, $8, $6);} + | GOTO IDENT + | LABEL IDENT + | BREAK + | RETURN + | STOP + | expr +; +expr : IDENT { cerr << "integer expr" << endl; } + | NUMBER { cerr << "real expr" << endl; } + | expr OPERATOR expr { cerr << "ident expr" << endl; } +; +assignStmt : expr ASSIGN IDENT +; +ifStmt : IF expr EOL THEN EOL stmtList EOL IFEND { $$ = new IfStatement($2, $6); } + | IF expr EOL THEN EOL stmtList EOL ELSE EOL stmtList EOL IFEND { $$ = new IfStatement($2, $6, $10); } + | expr => assign { $$ = new IfStatement($2, $3); } + +%% + +void yyerror(const char * msg) +{ + cerr << "line " << lineNumber << ": " << msg << endl; +} + +int main(int argc,char ** argv) +{ + if(argc>1) + yyin=fopen(argv[1],"r"); // check result !!! + lineNumber=1; + if(!yyparse()) + cerr << "Success" << endl; + return(0); +} \ No newline at end of file