Update
This commit is contained in:
parent
174e16d4b8
commit
dc397a794e
|
@ -1,6 +1,5 @@
|
|||
|
||||
*.gch
|
||||
|
||||
.idea/
|
||||
cmake-build-debug/
|
||||
|
||||
# Windows image file caches
|
||||
Thumbs.db
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -3,31 +3,34 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef BLANG_ASSIGNSTATEMENT_H
|
||||
#define BLANG_ASSIGNSTATEMENT_H
|
||||
|
||||
#include "Statement.hpp"
|
||||
|
||||
class AssignStatement: public Statement
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //BLANG_ASSIGNSTATEMENT_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<ExprIdentifier, Expr*> _subExprs;
|
||||
};
|
||||
|
||||
|
||||
#endif //BLANG_BINARYOPERATOREXPR_H
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef CALLEXPR_H
|
||||
#define CALLEXPR_H
|
||||
|
||||
/*!Represents a function call */
|
||||
class CallExpr: public Expr
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //CALLEXPR_H
|
|
@ -1,27 +1,38 @@
|
|||
#ifndef IFSTATEMENT_HPP
|
||||
#define IFSTATEMENT_HPP
|
||||
#ifndef COMPOUNDSTATEMENT_HPP
|
||||
#define COMPOUNDSTATEMENT_HPP
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "Statement.hpp"
|
||||
|
||||
class IfStatement: public Statement{
|
||||
class CompoundStatement : public Statement
|
||||
{
|
||||
|
||||
public:
|
||||
typedef std::vector<Statement*>::iterator body_iterator;
|
||||
typedef std::vector<Statement*>::const_iterator const_body_iterator;
|
||||
typedef std::vector<Statement*>::reverse_iterator reverse_body_iterator;
|
||||
typedef std::vector<Statement*>::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<Statement*> _body;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -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<StmtIdentifier, Statement*> _subStmts;
|
||||
SourceLocation _whileLoc;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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<StmtIdentifier, Statement*> _subStmts;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -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;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -1,38 +1,48 @@
|
|||
#ifndef IFSTATEMENT_HPP
|
||||
#define IFSTATEMENT_HPP
|
||||
|
||||
#include <map>
|
||||
|
||||
#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<StmtIdentifier, Statement*> _subStmts;
|
||||
|
||||
SourceLocation _IfLoc;
|
||||
SourceLocation _ElseLoc;
|
||||
SourceLocation _IfLoc;
|
||||
SourceLocation _ElseLoc;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -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
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef BLANG_NUMBERLITERALEXPR_H
|
||||
#define BLANG_NUMBERLITERALEXPR_H
|
||||
|
||||
|
||||
class NumberLiteralExpr
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //BLANG_NUMBERLITERALEXPR_H
|
|
@ -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
|
|
@ -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
|
|
@ -2,7 +2,7 @@
|
|||
#define WHILESTATEMENT_HPP
|
||||
|
||||
#include "Statement.hpp"
|
||||
#include "ExprStatement.hpp"
|
||||
#include "Expr.hpp"
|
||||
#include "SourceLocation.hpp"
|
||||
|
||||
|
||||
|
|
|
@ -1 +1,62 @@
|
|||
#include "SourceLocation.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#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;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
#include "../../include/Statement/AssignStatement.hpp"
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
//
|
||||
// Created by yoann on 24/11/16.
|
||||
//
|
||||
|
||||
#include "../../include/Statement/CallExpr.h"
|
|
@ -1 +1,56 @@
|
|||
#include "Statement/CompoundStatement.hpp"
|
||||
#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();
|
||||
}
|
|
@ -1,41 +1,60 @@
|
|||
#include "Statement/DoStatement.hpp"
|
||||
|
||||
ExprStatement* DoStatement::getCond(){
|
||||
return reinterpret_cast<ExprStatement*>(_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<Expr *>(_subStmts[COND]);
|
||||
}
|
||||
|
||||
void DoStatement::setCond(ExprStatement* e){
|
||||
_subExprs[COND] = reinterpret_cast<Statement *>(e);
|
||||
const Expr *DoStatement::getCond() const
|
||||
{
|
||||
return reinterpret_cast<Expr *>(_subStmts[COND]);
|
||||
}
|
||||
|
||||
Statement* DoStatement::getBody(){
|
||||
return _subExprs[BODY];
|
||||
void DoStatement::setCond(Expr *e)
|
||||
{
|
||||
_subStmts[COND] = reinterpret_cast<Statement *>(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;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
#include "Statement/Expr.hpp"
|
|
@ -1 +0,0 @@
|
|||
#include "Statement/ExprStatement.hpp"
|
|
@ -1 +1,84 @@
|
|||
#include "Statement/ForStatement.hpp"
|
||||
#include <Statement/Expr.hpp>
|
||||
#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<AssignStatement *>(_subStmts[INIT]);
|
||||
}
|
||||
|
||||
const AssignStatement *ForStatement::getInit() const
|
||||
{
|
||||
return reinterpret_cast<AssignStatement *>(_subStmts[INIT]);
|
||||
}
|
||||
|
||||
void ForStatement::setInit(AssignStatement *init)
|
||||
{
|
||||
_subStmts[INIT] = init;
|
||||
}
|
||||
|
||||
Expr *ForStatement::getCond()
|
||||
{
|
||||
return reinterpret_cast<Expr *>(_subStmts[COND]);
|
||||
}
|
||||
|
||||
const Expr *ForStatement::getCond() const
|
||||
{
|
||||
return reinterpret_cast<Expr *>(_subStmts[COND]);
|
||||
}
|
||||
|
||||
void ForStatement::setCond(Expr *cond)
|
||||
{
|
||||
_subStmts[COND] = cond;
|
||||
}
|
||||
|
||||
Expr *ForStatement::getInc()
|
||||
{
|
||||
return reinterpret_cast<Expr *>(_subStmts[INC]);
|
||||
}
|
||||
|
||||
const Expr *ForStatement::getInc() const
|
||||
{
|
||||
return reinterpret_cast<Expr *>(_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;
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -1,53 +1,79 @@
|
|||
#include "Statement/IfStatement.hpp"
|
||||
|
||||
ExprStatement* IfStatement::getCond(){
|
||||
return reinterpret_cast<ExprStatement*>(_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<Expr *>(_subStmts[COND]);
|
||||
}
|
||||
|
||||
void IfStatement::setCond(ExprStatement* e){
|
||||
_subExprs[COND] = reinterpret_cast<Statement *>(e);
|
||||
const Expr *IfStatement::getCond() const
|
||||
{
|
||||
return reinterpret_cast<Expr *>(_subStmts[COND]);
|
||||
}
|
||||
|
||||
Statement* IfStatement::getThen(){
|
||||
return _subExprs[THEN];
|
||||
void IfStatement::setCond(Expr *e)
|
||||
{
|
||||
_subStmts[COND] = reinterpret_cast<Statement *>(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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
#include "Statement/NumberLiteralExpr.h"
|
|
@ -1 +0,0 @@
|
|||
#include "Statement/OperatorStatement.hpp"
|
|
@ -0,0 +1 @@
|
|||
#include "Statement/StringLiteralExpr.h"
|
|
@ -0,0 +1,59 @@
|
|||
%{
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#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); }
|
||||
<strEnv>"\"" { BEGIN(INITIAL); return(Token::Token_StringLiteral); }
|
||||
<strEnv>"\n" { cerr << "multi-line strings not allowed" << endl; ++nbLines; }
|
||||
<strEnv>"\\\n" { val+='\n'; ++nbLines; } // line cut by \
|
||||
<strEnv>"\\". { val+=yytext[1]; }
|
||||
<strEnv><<EOF>> { BEGIN(INITIAL); return(STRING); }
|
||||
<strEnv>. { 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);
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue