Code cleaning, fixed makefile project errors for CLion
This commit is contained in:
parent
7b59f1f596
commit
00cbc056c8
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,3 +1,3 @@
|
|||||||
.vscode/
|
/.vscode
|
||||||
build/
|
build/
|
||||||
.idea
|
|
||||||
|
|||||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
18
.idea/misc.xml
generated
Normal file
18
.idea/misc.xml
generated
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MakefileSettings">
|
||||||
|
<option name="linkedExternalProjectsSettings">
|
||||||
|
<MakefileProjectSettings>
|
||||||
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
|
<option name="modules">
|
||||||
|
<set>
|
||||||
|
<option value="$PROJECT_DIR$" />
|
||||||
|
</set>
|
||||||
|
</option>
|
||||||
|
<option name="version" value="2" />
|
||||||
|
</MakefileProjectSettings>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="MakefileWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@ -13,6 +13,6 @@ public:
|
|||||||
int test = 10;
|
int test = 10;
|
||||||
std::string print(Expr<std::string>* expr);
|
std::string print(Expr<std::string>* expr);
|
||||||
private:
|
private:
|
||||||
std::string parenthesize(std::string name, std::vector<std::shared_ptr<Expr<std::string> > > exprs);
|
std::string parenthesize(std::string name, std::vector<std::shared_ptr<Expr<std::string>>> exprs);
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -18,11 +18,11 @@ struct Expr{
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
struct BinaryExpr : Expr<T>
|
struct BinaryExpr : Expr<T>
|
||||||
{
|
{
|
||||||
const std::shared_ptr<Expr<T> > left;
|
const std::shared_ptr<Expr<T>> left;
|
||||||
const Token oper;
|
const Token oper;
|
||||||
const std::shared_ptr<Expr<T> > right;
|
const std::shared_ptr<Expr<T>> right;
|
||||||
|
|
||||||
BinaryExpr(std::shared_ptr<Expr<T> > left, Token oper, std::shared_ptr<Expr<T> > right) : left(left), oper(oper), right(right)
|
BinaryExpr(std::shared_ptr<Expr<T>> left, Token oper, std::shared_ptr<Expr<T> > right) : left(left), oper(oper), right(right)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
T accept(Visitor<T>* visitor) override{
|
T accept(Visitor<T>* visitor) override{
|
||||||
@ -32,9 +32,9 @@ struct BinaryExpr : Expr<T>
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
struct GroupingExpr : Expr<T>
|
struct GroupingExpr : Expr<T>
|
||||||
{
|
{
|
||||||
const std::shared_ptr<Expr<T> > expression;
|
const std::shared_ptr<Expr<T>> expression;
|
||||||
|
|
||||||
GroupingExpr(std::shared_ptr<Expr<T> > expression) : expression(expression)
|
GroupingExpr(std::shared_ptr<Expr<T>> expression) : expression(expression)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
T accept(Visitor<T>* visitor) override{
|
T accept(Visitor<T>* visitor) override{
|
||||||
@ -57,9 +57,9 @@ template <typename T>
|
|||||||
struct UnaryExpr : Expr<T>
|
struct UnaryExpr : Expr<T>
|
||||||
{
|
{
|
||||||
const Token oper;
|
const Token oper;
|
||||||
const std::shared_ptr<Expr<T> > right;
|
const std::shared_ptr<Expr<T>> right;
|
||||||
|
|
||||||
UnaryExpr(Token oper, std::shared_ptr<Expr<T> > right) : oper(oper), right(right)
|
UnaryExpr(Token oper, std::shared_ptr<Expr<T>> right) : oper(oper), right(right)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
T accept(Visitor<T>* visitor) override{
|
T accept(Visitor<T>* visitor) override{
|
||||||
|
|||||||
18
headers/TypeWrapper.h
Normal file
18
headers/TypeWrapper.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
struct Object
|
||||||
|
{
|
||||||
|
virtual ~Object(){};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Number : public Object
|
||||||
|
{
|
||||||
|
double value;
|
||||||
|
explicit Number(double value) : value(value) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct String : public Object
|
||||||
|
{
|
||||||
|
std::string value;
|
||||||
|
explicit String(std::string str) : value(str) {}
|
||||||
|
};
|
||||||
@ -6,29 +6,29 @@
|
|||||||
|
|
||||||
std::string ASTPrinter::visitBinaryExpr(BinaryExpr<std::string>* expression){
|
std::string ASTPrinter::visitBinaryExpr(BinaryExpr<std::string>* expression){
|
||||||
std::cout << expression->left << std::endl;
|
std::cout << expression->left << std::endl;
|
||||||
return parenthesize(expression->oper.lexeme, std::vector<std::shared_ptr<Expr<std::string> > >{expression->left, expression->right});
|
return parenthesize(expression->oper.lexeme, std::vector<std::shared_ptr<Expr<std::string>>>{expression->left, expression->right});
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ASTPrinter::visitGroupingExpr(GroupingExpr<std::string>* expression){
|
std::string ASTPrinter::visitGroupingExpr(GroupingExpr<std::string>* expression){
|
||||||
return parenthesize("group", std::vector<std::shared_ptr<Expr<std::string> > >{expression->expression});
|
return parenthesize("group", std::vector<std::shared_ptr<Expr<std::string>>>{expression->expression});
|
||||||
}
|
}
|
||||||
std::string ASTPrinter::visitLiteralExpr(LiteralExpr<std::string>* expression){
|
std::string ASTPrinter::visitLiteralExpr(LiteralExpr<std::string>* expression){
|
||||||
return expression->value;
|
return expression->value;
|
||||||
}
|
}
|
||||||
std::string ASTPrinter::visitUnaryExpr(UnaryExpr<std::string>* expression){
|
std::string ASTPrinter::visitUnaryExpr(UnaryExpr<std::string>* expression){
|
||||||
return parenthesize(expression->oper.lexeme, std::vector<std::shared_ptr<Expr<std::string> > >{expression->right});
|
return parenthesize(expression->oper.lexeme, std::vector<std::shared_ptr<Expr<std::string>>>{expression->right});
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ASTPrinter::print(Expr<std::string> *expr) {
|
std::string ASTPrinter::print(Expr<std::string> *expr) {
|
||||||
return expr->accept(this);
|
return expr->accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ASTPrinter::parenthesize(std::string name, std::vector<std::shared_ptr<Expr<std::string> > > exprs) {
|
std::string ASTPrinter::parenthesize(std::string name, std::vector<std::shared_ptr<Expr<std::string>>> exprs) {
|
||||||
std::string builder;
|
std::string builder;
|
||||||
|
|
||||||
builder += "(" + name;
|
builder += "(" + name;
|
||||||
|
|
||||||
for(std::shared_ptr<Expr<std::string> > expr : exprs)
|
for(std::shared_ptr<Expr<std::string>> expr : exprs)
|
||||||
{
|
{
|
||||||
std::cout << expr << std::endl;
|
std::cout << expr << std::endl;
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include "../headers/Expression.h"
|
#include "../headers/Expression.h"
|
||||||
#include "../headers/Lexer.h"
|
#include "../headers/Lexer.h"
|
||||||
#include "../headers/ASTPrinter.h"
|
#include "../headers/ASTPrinter.h"
|
||||||
|
#include "../headers/TypeWrapper.h"
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
Bob bobLang;
|
Bob bobLang;
|
||||||
@ -15,14 +16,14 @@ int main(){
|
|||||||
|
|
||||||
ASTPrinter printer;
|
ASTPrinter printer;
|
||||||
|
|
||||||
std::shared_ptr<Expr<std::string> > expression = std::make_shared<BinaryExpr<std::string> >(
|
std::shared_ptr<Expr<std::string>> expression = std::make_shared<BinaryExpr<std::string> >(
|
||||||
std::make_shared<UnaryExpr<std::string> >(
|
std::make_shared<UnaryExpr<std::string>>(
|
||||||
Token{MINUS, "-", 1},
|
Token{MINUS, "-", 1},
|
||||||
std::make_shared<LiteralExpr<std::string>>("123", true)
|
std::make_shared<LiteralExpr<std::string>>("123", true)
|
||||||
),
|
),
|
||||||
Token{STAR, "*", 1},
|
Token{STAR, "*", 1},
|
||||||
std::make_shared<GroupingExpr<std::string> >(
|
std::make_shared<GroupingExpr<std::string>>(
|
||||||
std::make_shared<LiteralExpr<std::string> >("45.67", true)
|
std::make_shared<LiteralExpr<std::string>>("45.67", true)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -39,5 +40,15 @@ int main(){
|
|||||||
|
|
||||||
//bobLang.runPrompt();
|
//bobLang.runPrompt();
|
||||||
|
|
||||||
|
std::shared_ptr<Object> object = std::make_shared<String>(String{"Hi"});
|
||||||
|
|
||||||
|
if(auto num = std::dynamic_pointer_cast<Number>(object))
|
||||||
|
{
|
||||||
|
std::cout << num->value << std::endl;
|
||||||
|
}else if(auto str = std::dynamic_pointer_cast<String>(object))
|
||||||
|
{
|
||||||
|
std::cout << str->value << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user