Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: munificent/bantam
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: stefanboca/bantam-cpp
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 3 commits
  • 68 files changed
  • 1 contributor

Commits on Sep 14, 2020

  1. translate to c++

    stefanboca committed Sep 14, 2020
    Copy the full SHA
    d8db021 View commit details
  2. make it work

    stefanboca committed Sep 14, 2020
    Copy the full SHA
    0097eb1 View commit details
  3. update README.md

    stefanboca committed Sep 14, 2020
    Copy the full SHA
    0d35853 View commit details
Showing with 893 additions and 865 deletions.
  1. +0 −6 .classpath
  2. +4 −0 .gitignore
  3. +0 −17 .project
  4. +25 −0 CMakeLists.txt
  5. +1 −1 README.md
  6. +17 −0 include/BantamParser.h
  7. +11 −0 include/Expressions.h
  8. +22 −0 include/Lexer.h
  9. +8 −0 include/ParseException.h
  10. +13 −0 include/Parselets.h
  11. +46 −0 include/Parser.h
  12. +14 −0 include/Precedence.h
  13. +21 −0 include/Token.h
  14. +61 −0 include/TokenType.h
  15. +18 −0 include/expressions/AssignExpression.h
  16. +23 −0 include/expressions/CallExpression.h
  17. +18 −0 include/expressions/ConditionalExpression.h
  18. +17 −0 include/expressions/Expression.h
  19. +19 −0 include/expressions/NameExpression.h
  20. +19 −0 include/expressions/OperatorExpression.h
  21. +15 −0 include/expressions/PostfixExpression.h
  22. +17 −0 include/expressions/PrefixExpression.h
  23. +16 −0 include/parselets/AssignParselet.h
  24. +16 −0 include/parselets/BinaryOperatorParselet.h
  25. +16 −0 include/parselets/CallParselet.h
  26. +14 −0 include/parselets/ConditionalParselet.h
  27. +12 −0 include/parselets/GroupParselet.h
  28. +16 −0 include/parselets/InfixParselet.h
  29. +13 −0 include/parselets/NameParselet.h
  30. +17 −0 include/parselets/PostfixOperatorParselet.h
  31. +15 −0 include/parselets/PrefixOperatorParselet.h
  32. +15 −0 include/parselets/PrefixParselet.h
  33. +42 −0 src/BantamParser.cpp
  34. +44 −0 src/Lexer.cpp
  35. +78 −0 src/Parser.cpp
  36. +0 −69 src/com/stuffwithstuff/bantam/BantamParser.java
  37. +0 −76 src/com/stuffwithstuff/bantam/Lexer.java
  38. +0 −85 src/com/stuffwithstuff/bantam/Main.java
  39. +0 −8 src/com/stuffwithstuff/bantam/ParseException.java
  40. +0 −99 src/com/stuffwithstuff/bantam/Parser.java
  41. +0 −19 src/com/stuffwithstuff/bantam/Precedence.java
  42. +0 −20 src/com/stuffwithstuff/bantam/Token.java
  43. +0 −42 src/com/stuffwithstuff/bantam/TokenType.java
  44. +0 −20 src/com/stuffwithstuff/bantam/expressions/AssignExpression.java
  45. +0 −26 src/com/stuffwithstuff/bantam/expressions/CallExpression.java
  46. +0 −27 src/com/stuffwithstuff/bantam/expressions/ConditionalExpression.java
  47. +0 −11 src/com/stuffwithstuff/bantam/expressions/Expression.java
  48. +0 −18 src/com/stuffwithstuff/bantam/expressions/NameExpression.java
  49. +0 −26 src/com/stuffwithstuff/bantam/expressions/OperatorExpression.java
  50. +0 −22 src/com/stuffwithstuff/bantam/expressions/PostfixExpression.java
  51. +0 −22 src/com/stuffwithstuff/bantam/expressions/PrefixExpression.java
  52. +0 −28 src/com/stuffwithstuff/bantam/parselets/AssignParselet.java
  53. +0 −36 src/com/stuffwithstuff/bantam/parselets/BinaryOperatorParselet.java
  54. +0 −35 src/com/stuffwithstuff/bantam/parselets/CallParselet.java
  55. +0 −25 src/com/stuffwithstuff/bantam/parselets/ConditionalParselet.java
  56. +0 −17 src/com/stuffwithstuff/bantam/parselets/GroupParselet.java
  57. +0 −18 src/com/stuffwithstuff/bantam/parselets/InfixParselet.java
  58. +0 −15 src/com/stuffwithstuff/bantam/parselets/NameParselet.java
  59. +0 −26 src/com/stuffwithstuff/bantam/parselets/PostfixOperatorParselet.java
  60. +0 −32 src/com/stuffwithstuff/bantam/parselets/PrefixOperatorParselet.java
  61. +0 −19 src/com/stuffwithstuff/bantam/parselets/PrefixParselet.java
  62. +120 −0 src/main.cpp
  63. +13 −0 src/parselets/AssignParselet.cpp
  64. +14 −0 src/parselets/BinaryOperatorParselet.cpp
  65. +14 −0 src/parselets/CallParselet.cpp
  66. +11 −0 src/parselets/ConditionalParselet.cpp
  67. +7 −0 src/parselets/GroupParselet.cpp
  68. +11 −0 src/parselets/PrefixOperatorParselet.cpp
6 changes: 0 additions & 6 deletions .classpath

This file was deleted.

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# Ignore output files
bin/
build/

#IDE folders
.vscode/
17 changes: 0 additions & 17 deletions .project

This file was deleted.

25 changes: 25 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.16.0)
project(new_project VERSION 0.1.0)

# include(CTest)
# enable_testing()

add_executable(main
src/main.cpp
src/Lexer.cpp
src/Parser.cpp
src/BantamParser.cpp

src/parselets/AssignParselet.cpp
src/parselets/BinaryOperatorParselet.cpp
src/parselets/CallParselet.cpp
src/parselets/ConditionalParselet.cpp
src/parselets/GroupParselet.cpp
src/parselets/PrefixOperatorParselet.cpp
)
set_target_properties(main PROPERTIES CXX_STANDARD 20)
target_include_directories(main PRIVATE ${CMAKE_SOURCE_DIR}/include)

# set(CPACK_PROJECT_NAME ${PROJECT_NAME})
# set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
# include(CPack)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This is a tiny little Java app to demonstrate Pratt parsing. For a full explanation, see [this blog post](http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/).
This is a tiny little ~~Java~~ C++ app to demonstrate Pratt parsing. For a full explanation, see [this blog post](http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/).
17 changes: 17 additions & 0 deletions include/BantamParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "Lexer.h"
#include "Parser.h"
#include "TokenType.h"
#include "Precedence.h"

class BantamParser : public Parser {
public:
BantamParser(LexerSP lexer);

void postfix(TokenType token, int precedence);
void prefix(TokenType token, int precedence);

void infixLeft(TokenType token, int precedence);
void infixRight(TokenType token, int precedence);
};
11 changes: 11 additions & 0 deletions include/Expressions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "expressions/Expression.h"

#include "expressions/AssignExpression.h"
#include "expressions/CallExpression.h"
#include "expressions/ConditionalExpression.h"
#include "expressions/NameExpression.h"
#include "expressions/OperatorExpression.h"
#include "expressions/PostfixExpression.h"
#include "expressions/PrefixExpression.h"
22 changes: 22 additions & 0 deletions include/Lexer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "TokenType.h"
#include "Token.h"

#include <string>
#include <map>
#include <memory>

class Lexer {
public:
Lexer(std::string text);

Token next();

private:
std::map<char, TokenType> mPunctuators = std::map<char, TokenType>();
std::string mText;
int mIndex = 0;
};

typedef std::shared_ptr<Lexer> LexerSP;
8 changes: 8 additions & 0 deletions include/ParseException.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <stdexcept>

class ParseException : public std::runtime_error {
public:
ParseException(std::string msg = "") : std::runtime_error(msg) {}
};
13 changes: 13 additions & 0 deletions include/Parselets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "parselets/InfixParselet.h"
#include "parselets/PrefixParselet.h"

#include "parselets/AssignParselet.h"
#include "parselets/BinaryOperatorParselet.h"
#include "parselets/CallParselet.h"
#include "parselets/ConditionalParselet.h"
#include "parselets/GroupParselet.h"
#include "parselets/NameParselet.h"
#include "parselets/PostfixOperatorParselet.h"
#include "parselets/PrefixOperatorParselet.h"
46 changes: 46 additions & 0 deletions include/Parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include "TokenType.h"
#include "Lexer.h"
#include "Token.h"
#include "Expressions.h"
#include "ParseException.h"
#include "parselets/PrefixParselet.h"
#include "parselets/InfixParselet.h"

#include <vector>
#include <map>
#include <memory>

class PrefixParselet;
class InfixParselet;

typedef std::shared_ptr<PrefixParselet> PrefixParseletSP;
typedef std::shared_ptr<InfixParselet> InfixParseletSP;

class Parser : public std::enable_shared_from_this<Parser> {
public:
Parser() = delete;
Parser(LexerSP tokens);

void registerParselet(TokenType token, PrefixParseletSP parselet);
void registerParselet(TokenType token, InfixParseletSP parselet);

ExpressionSP parseExpression(int precedence);
ExpressionSP parseExpression();

bool match(TokenType expected);
Token consume(TokenType expected);
Token consume();

private:
Token lookAhead(int distance);
int getPrecedence();

LexerSP mTokens;
std::vector<Token> mRead;
std::map<TokenType, PrefixParseletSP> mPrefixParselets;
std::map<TokenType, InfixParseletSP> mInfixParselets;
};

typedef std::shared_ptr<Parser> ParserSP;
14 changes: 14 additions & 0 deletions include/Precedence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

class Precedence {
public:
// Ordered in increasing Precedence
static const int ASSIGNMENT = 1;
static const int CONDITIONAL = 2;
static const int SUM = 3;
static const int PRODUCT = 4;
static const int EXPONENT = 5;
static const int PREFIX = 6;
static const int POSTFIX = 7;
static const int CALL = 8;
};
21 changes: 21 additions & 0 deletions include/Token.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "TokenType.h"
#include <string>

class Token {
public:
Token(TokenType type, std::string text) {
mType = type;
mText = text;
}

TokenType getType() { return mType; }
std::string& getText() { return mText; }

std::string toString() { return mText; }

private:
TokenType mType;
std::string mText;
};
61 changes: 61 additions & 0 deletions include/TokenType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include <string>

enum class TokenType {
LEFT_PAREN,
RIGHT_PAREN,
COMMA,
ASSIGN,
PLUS,
MINUS,
ASTERISK,
SLASH,
CARET,
TILDE,
BANG,
QUESTION,
COLON,
NAME,
END
};

static inline std::string toString(TokenType tokenType) {
switch (tokenType) {
case TokenType::LEFT_PAREN: return "LEFT_PAREN";
case TokenType::RIGHT_PAREN: return "RIGHT_PAREN";
case TokenType::COMMA: return "COMMA";
case TokenType::ASSIGN: return "ASSIGN";
case TokenType::PLUS: return "PLUS";
case TokenType::MINUS: return "MINUS";
case TokenType::ASTERISK: return "ASTERISK";
case TokenType::SLASH: return "SLASH";
case TokenType::CARET: return "CARET";
case TokenType::TILDE: return "TILDE";
case TokenType::BANG: return "BANG";
case TokenType::QUESTION: return "QUESTION";
case TokenType::COLON: return "COLON";
case TokenType::NAME: return "NAME";
case TokenType::END: return "END";
}
return "";
}

static inline char punctuator(TokenType tokenType) {
switch (tokenType) {
case TokenType::LEFT_PAREN: return '(';
case TokenType::RIGHT_PAREN: return ')';
case TokenType::COMMA: return ',';
case TokenType::ASSIGN: return '=';
case TokenType::PLUS: return '+';
case TokenType::MINUS: return '-';
case TokenType::ASTERISK: return '*';
case TokenType::SLASH: return '/';
case TokenType::CARET: return '^';
case TokenType::TILDE: return '~';
case TokenType::BANG: return '!';
case TokenType::QUESTION: return '?';
case TokenType::COLON: return ':';
default: return 0;
}
}
18 changes: 18 additions & 0 deletions include/expressions/AssignExpression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "Expression.h"

class AssignExpression : public Expression {
public:
AssignExpression(std::string name, ExpressionSP right) : mName(name), mRight(right) {}

virtual void print(std::ostream& out) const override {
out << "(" << mName << " = ";
out << *mRight;
out << ")";
}

private:
std::string mName;
ExpressionSP mRight;
};
23 changes: 23 additions & 0 deletions include/expressions/CallExpression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "Expression.h"

#include <vector>

class CallExpression : public Expression {
public:
CallExpression(ExpressionSP function, std::vector<ExpressionSP> args) : mFunction(function), mArgs(args) {}

virtual void print(std::ostream& out) const override {
out << *mFunction << "(";
for (int i = 0; i < mArgs.size(); i++) {
out << *(mArgs.at(i));
if (i < mArgs.size() - 1) { out << ", "; }
}
out << ")";
}

private:
ExpressionSP mFunction;
std::vector<ExpressionSP> mArgs;
};
18 changes: 18 additions & 0 deletions include/expressions/ConditionalExpression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "Expression.h"

class ConditionalExpression : public Expression {
public:
ConditionalExpression(ExpressionSP condition, ExpressionSP thenArm, ExpressionSP elseArm) :
mCondition(condition), mThenArm(thenArm), mElseArm(elseArm) {}

virtual void print(std::ostream& out) const override {
out << "(" << *mCondition << " ? " << *mThenArm << " : " << *mElseArm << ")";
}

private:
ExpressionSP mCondition;
ExpressionSP mThenArm;
ExpressionSP mElseArm;
};
17 changes: 17 additions & 0 deletions include/expressions/Expression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <string>
#include <memory>
#include <ostream>

class Expression {
public:
virtual void print(std::ostream& out) const = 0;
};

typedef std::shared_ptr<Expression> ExpressionSP;

static inline std::ostream& operator<<(std::ostream& out, const Expression& expr) {
expr.print(out);
return out;
}
19 changes: 19 additions & 0 deletions include/expressions/NameExpression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "Expression.h"

#include <memory>

class NameExpression : public Expression {
public:
NameExpression(std::string name) : mName(name) {}

std::string getName() { return mName; }

virtual void print(std::ostream& out) const override { out << mName; }

private:
std::string mName;
};

typedef std::shared_ptr<NameExpression> NameExpressionSP;
Loading