From 40aedff45c5122af939ac42e4af13d13c8d3d636 Mon Sep 17 00:00:00 2001 From: Charles Dyason Date: Mon, 10 Jun 2019 12:08:32 +0200 Subject: [PATCH] Added DigitLiteral chars being unique. --- AST/AST.cpp | 3 +++ AST/AST.h | 6 +++--- AST/Digit.cpp | 11 +++++++++++ AST/Digit.h | 19 +++++++++++++++++++ AST/Expression.cpp | 4 ++++ AST/Expression.h | 2 +- AST/Identifier.cpp | 8 +------- AST/Identifier.h | 4 ++-- AST/IdentifierDigit.cpp | 8 ++++++++ AST/IdentifierDigit.h | 17 +++++++++++++++++ AST/IdentifierLetter.cpp | 8 ++++++++ AST/IdentifierLetter.h | 18 ++++++++++++++++++ AST/IntegerLiteral.cpp | 14 ++++++++++++++ AST/IntegerLiteral.h | 22 ++++++++++++++++++++++ AST/Letter.cpp | 11 +++++++++++ AST/Letter.h | 21 +++++++++++++++++++++ AST/Operate.cpp | 4 ++++ AST/Operate.h | 1 + AST/PrimaryExpression.cpp | 4 ++++ AST/PrimaryExpression.h | 2 ++ AST/Terminal.cpp | 4 ++++ AST/Terminal.h | 1 + CMakeLists.txt | 2 +- 23 files changed, 180 insertions(+), 14 deletions(-) create mode 100644 AST/Digit.cpp create mode 100644 AST/Digit.h create mode 100644 AST/IdentifierDigit.cpp create mode 100644 AST/IdentifierDigit.h create mode 100644 AST/IdentifierLetter.cpp create mode 100644 AST/IdentifierLetter.h create mode 100644 AST/IntegerLiteral.cpp create mode 100644 AST/IntegerLiteral.h create mode 100644 AST/Letter.cpp create mode 100644 AST/Letter.h diff --git a/AST/AST.cpp b/AST/AST.cpp index a1c4c70..fe0189c 100644 --- a/AST/AST.cpp +++ b/AST/AST.cpp @@ -4,3 +4,6 @@ #include "AST.h" +std::string AST::toString() { + +} diff --git a/AST/AST.h b/AST/AST.h index 3389807..fff569a 100644 --- a/AST/AST.h +++ b/AST/AST.h @@ -5,11 +5,11 @@ #ifndef COMPILER_AST_H #define COMPILER_AST_H +#include + class AST { public: - virtual void describe() { - - } + virtual std::string toString(); }; #endif //COMPILER_AST_H diff --git a/AST/Digit.cpp b/AST/Digit.cpp new file mode 100644 index 0000000..9884b2e --- /dev/null +++ b/AST/Digit.cpp @@ -0,0 +1,11 @@ +// +// Created by cybex on 2019/06/10. +// + +#include "Digit.h" + +Digit::Digit(const std::string &spelling) : Terminal(spelling) {} + +std::string Digit::toString() { + return Terminal::toString(); +} diff --git a/AST/Digit.h b/AST/Digit.h new file mode 100644 index 0000000..cccc664 --- /dev/null +++ b/AST/Digit.h @@ -0,0 +1,19 @@ +// +// Created by cybex on 2019/06/10. +// + +#ifndef COMPILER_DIGIT_H +#define COMPILER_DIGIT_H + + +#include "Terminal.h" + +// inputs: [0-9] +class Digit: Terminal { +public: + Digit(const std::string &spelling); + std::string toString() override; +}; + + +#endif //COMPILER_DIGIT_H diff --git a/AST/Expression.cpp b/AST/Expression.cpp index 1bee871..d9f8d0c 100644 --- a/AST/Expression.cpp +++ b/AST/Expression.cpp @@ -18,3 +18,7 @@ Expression::~Expression() { delete P2; delete O; } + +std::string Expression::toString() { + return P1->toString() + O->toString() + P2->toString(); +} diff --git a/AST/Expression.h b/AST/Expression.h index a0bd0fb..28b38fe 100644 --- a/AST/Expression.h +++ b/AST/Expression.h @@ -16,7 +16,7 @@ class Expression : AST { public: Expression(PrimaryExpression *p1, PrimaryExpression *p2, Operate *o); explicit Expression(Expression *expression); - + std::string toString() override; virtual ~Expression(); }; diff --git a/AST/Identifier.cpp b/AST/Identifier.cpp index a317538..5141c55 100644 --- a/AST/Identifier.cpp +++ b/AST/Identifier.cpp @@ -2,10 +2,4 @@ // Created by cybex on 2019/05/03. // -#include "Identifier.h" - -Identifier::Identifier(const std::string &spelling) : Terminal(spelling) {} - -Identifier::~Identifier() { - -} +#include "Identifier.h" \ No newline at end of file diff --git a/AST/Identifier.h b/AST/Identifier.h index e155593..65e538c 100644 --- a/AST/Identifier.h +++ b/AST/Identifier.h @@ -10,9 +10,9 @@ class Identifier : public Terminal{ public: - explicit Identifier(const std::string &spelling); + virtual ~Identifier() = 0; - virtual ~Identifier(); + std::string toString() override = 0; }; diff --git a/AST/IdentifierDigit.cpp b/AST/IdentifierDigit.cpp new file mode 100644 index 0000000..a92150d --- /dev/null +++ b/AST/IdentifierDigit.cpp @@ -0,0 +1,8 @@ +// +// Created by cybex on 2019/06/10. +// + +#include "IdentifierDigit.h" + +IdentifierDigit::IdentifierDigit(const std::string &spelling, const std::string &spelling1) : Identifier(spelling), + Digit(spelling1) {} diff --git a/AST/IdentifierDigit.h b/AST/IdentifierDigit.h new file mode 100644 index 0000000..6fc2f3f --- /dev/null +++ b/AST/IdentifierDigit.h @@ -0,0 +1,17 @@ +// +// Created by cybex on 2019/06/10. +// + +#ifndef COMPILER_IDENTIFIERDIGIT_H +#define COMPILER_IDENTIFIERDIGIT_H + +#include "Identifier.h" +#include "Digit.h" + +class IdentifierDigit: public Identifier, public Digit{ +public: + IdentifierDigit(const std::string &spelling, const std::string &spelling1); +}; + + +#endif //COMPILER_IDENTIFIERDIGIT_H diff --git a/AST/IdentifierLetter.cpp b/AST/IdentifierLetter.cpp new file mode 100644 index 0000000..3d15898 --- /dev/null +++ b/AST/IdentifierLetter.cpp @@ -0,0 +1,8 @@ +// +// Created by cybex on 2019/06/10. +// + +#include "IdentifierLetter.h" + +IdentifierLetter::IdentifierLetter(const std::string &letter, const std::string &nextId) : Identifier(nextId), + Letter(letter) {} \ No newline at end of file diff --git a/AST/IdentifierLetter.h b/AST/IdentifierLetter.h new file mode 100644 index 0000000..15fc070 --- /dev/null +++ b/AST/IdentifierLetter.h @@ -0,0 +1,18 @@ +// +// Created by cybex on 2019/06/10. +// + +#ifndef COMPILER_IDENTIFIERLETTER_H +#define COMPILER_IDENTIFIERLETTER_H + + +#include "Identifier.h" +#include "Letter.h" + +class IdentifierLetter: public Identifier, public Letter { +public: + IdentifierLetter(const std::string &letter, const std::string &nextId); +}; + + +#endif //COMPILER_IDENTIFIERLETTER_H diff --git a/AST/IntegerLiteral.cpp b/AST/IntegerLiteral.cpp new file mode 100644 index 0000000..2fbc107 --- /dev/null +++ b/AST/IntegerLiteral.cpp @@ -0,0 +1,14 @@ +// +// Created by cybex on 2019/06/10. +// + +#include "IntegerLiteral.h" +IntegerLiteral::IntegerLiteral(const std::string &spelling, IntegerLiteral *next) : Digit(spelling), next(next) {} + +IntegerLiteral::IntegerLiteral(const std::string &spelling) : Digit(spelling) { + +} + +std::string IntegerLiteral::toString() { + return Digit::toString() + next->toString(); +} diff --git a/AST/IntegerLiteral.h b/AST/IntegerLiteral.h new file mode 100644 index 0000000..eb9e8d5 --- /dev/null +++ b/AST/IntegerLiteral.h @@ -0,0 +1,22 @@ +// +// Created by cybex on 2019/06/10. +// + +#ifndef COMPILER_INTEGERLITERAL_H +#define COMPILER_INTEGERLITERAL_H + + +#include "Digit.h" + +class IntegerLiteral: public Digit{ +public: + IntegerLiteral *next = nullptr; + + IntegerLiteral(const std::string &spelling, IntegerLiteral *next); + explicit IntegerLiteral(const std::string &spelling); + + std::string toString() override; +}; + + +#endif //COMPILER_INTEGERLITERAL_H diff --git a/AST/Letter.cpp b/AST/Letter.cpp new file mode 100644 index 0000000..ed6b8a5 --- /dev/null +++ b/AST/Letter.cpp @@ -0,0 +1,11 @@ +// +// Created by cybex on 2019/06/10. +// + +#include "Letter.h" + +Letter::Letter(const std::string &spelling) : Terminal(spelling) {} + +std::string Letter::toString() { + return Terminal::toString(); +} diff --git a/AST/Letter.h b/AST/Letter.h new file mode 100644 index 0000000..de18644 --- /dev/null +++ b/AST/Letter.h @@ -0,0 +1,21 @@ +// +// Created by cybex on 2019/06/10. +// + +#ifndef COMPILER_LETTER_H +#define COMPILER_LETTER_H + + +#include "Terminal.h" + +// inputs: [a-zA-Z] +class Letter: Terminal { +public: + Letter(const std::string &spelling); + +private: + std::string toString() override; +}; + + +#endif //COMPILER_LETTER_H diff --git a/AST/Operate.cpp b/AST/Operate.cpp index dc60395..0323f39 100644 --- a/AST/Operate.cpp +++ b/AST/Operate.cpp @@ -5,3 +5,7 @@ #include "Operate.h" Operate::Operate(const std::string &spelling) : Terminal(spelling) {} + +std::string Operate::toString() { + return Terminal::toString(); +} diff --git a/AST/Operate.h b/AST/Operate.h index 35db7c6..bfaa5fc 100644 --- a/AST/Operate.h +++ b/AST/Operate.h @@ -11,6 +11,7 @@ class Operate: Terminal { public: explicit Operate(const std::string &spelling); + std::string toString() override; }; diff --git a/AST/PrimaryExpression.cpp b/AST/PrimaryExpression.cpp index 715093d..459c135 100644 --- a/AST/PrimaryExpression.cpp +++ b/AST/PrimaryExpression.cpp @@ -3,3 +3,7 @@ // #include "PrimaryExpression.h" + +std::string PrimaryExpression::toString() { + return AST::toString(); +} diff --git a/AST/PrimaryExpression.h b/AST/PrimaryExpression.h index 4e3acba..b242328 100644 --- a/AST/PrimaryExpression.h +++ b/AST/PrimaryExpression.h @@ -8,6 +8,8 @@ #include "AST.h" class PrimaryExpression: public AST { +public: + std::string toString() override; }; diff --git a/AST/Terminal.cpp b/AST/Terminal.cpp index b6f17d9..b8dd080 100644 --- a/AST/Terminal.cpp +++ b/AST/Terminal.cpp @@ -7,3 +7,7 @@ #include "Terminal.h" Terminal::Terminal(std::string spelling) : spelling(std::move(spelling)) {} + +std::string Terminal::toString() { + return spelling; +} diff --git a/AST/Terminal.h b/AST/Terminal.h index 450e23e..e841b31 100644 --- a/AST/Terminal.h +++ b/AST/Terminal.h @@ -13,6 +13,7 @@ class Terminal: AST { std::string spelling; public: explicit Terminal(std::string spelling); + std::string toString() override; }; diff --git a/CMakeLists.txt b/CMakeLists.txt index 264ebb8..3d4c656 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,4 +59,4 @@ if (LONG EQUAL 1) endif () # Compilation of sources to object to binary -add_executable(Compiler main.cpp AST/Token.cpp AST/Token.h Scanner.cpp Scanner.h Parser.cpp Parser.h AST/AST.h AST/Operate.cpp AST/Operate.h AST/Expression.cpp AST/Expression.h AST/PrimaryExpression_Expression.cpp AST/PrimaryExpression_Expression.h AST/Terminal.cpp AST/Terminal.h AST/Identifier.cpp AST/Identifier.h AST/AST.cpp AST/Program.cpp AST/Program.h AST/Command.cpp AST/Command.h AST/CommandIf.cpp AST/CommandIf.h AST/CommandAssign.cpp AST/CommandAssign.h AST/CommandLet.cpp AST/CommandLet.h AST/Declaration.cpp AST/Declaration.h AST/DeclarationConst.cpp AST/DeclarationConst.h AST/DeclarationVar.cpp AST/DeclarationVar.h AST/TypeDenoter.cpp AST/TypeDenoter.h AST/VarName.cpp AST/VarName.h AST/PrimaryExpressionVar.cpp AST/PrimaryExpressionVar.h AST/PrimaryExpression.cpp AST/PrimaryExpression.h) \ No newline at end of file +add_executable(Compiler main.cpp AST/Token.cpp AST/Token.h Scanner.cpp Scanner.h Parser.cpp Parser.h AST/AST.h AST/Operate.cpp AST/Operate.h AST/Expression.cpp AST/Expression.h AST/PrimaryExpression_Expression.cpp AST/PrimaryExpression_Expression.h AST/Terminal.cpp AST/Terminal.h AST/Identifier.cpp AST/Identifier.h AST/AST.cpp AST/Program.cpp AST/Program.h AST/Command.cpp AST/Command.h AST/CommandIf.cpp AST/CommandIf.h AST/CommandAssign.cpp AST/CommandAssign.h AST/CommandLet.cpp AST/CommandLet.h AST/Declaration.cpp AST/Declaration.h AST/DeclarationConst.cpp AST/DeclarationConst.h AST/DeclarationVar.cpp AST/DeclarationVar.h AST/TypeDenoter.cpp AST/TypeDenoter.h AST/VarName.cpp AST/VarName.h AST/PrimaryExpressionVar.cpp AST/PrimaryExpressionVar.h AST/PrimaryExpression.cpp AST/PrimaryExpression.h AST/Letter.cpp AST/Letter.h AST/IntegerLiteral.cpp AST/IntegerLiteral.h AST/Digit.cpp AST/Digit.h AST/IdentifierLetter.cpp AST/IdentifierLetter.h AST/IdentifierDigit.cpp AST/IdentifierDigit.h) \ No newline at end of file