Code cleaning, fixed makefile project errors for CLion

This commit is contained in:
Bobby Lucero 2023-05-26 21:05:03 -04:00
parent 7b59f1f596
commit 00cbc056c8
9 changed files with 80 additions and 19 deletions

4
.gitignore vendored
View File

@ -1,3 +1,3 @@
.vscode/
/.vscode
build/
.idea

8
.idea/.gitignore generated vendored Normal file
View 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
View 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
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -13,6 +13,6 @@ public:
int test = 10;
std::string print(Expr<std::string>* expr);
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);
};

View File

@ -18,11 +18,11 @@ struct Expr{
template <typename T>
struct BinaryExpr : Expr<T>
{
const std::shared_ptr<Expr<T> > left;
const std::shared_ptr<Expr<T>> left;
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{
@ -32,9 +32,9 @@ struct BinaryExpr : Expr<T>
template <typename 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{
@ -57,9 +57,9 @@ template <typename T>
struct UnaryExpr : Expr<T>
{
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{

18
headers/TypeWrapper.h Normal file
View 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) {}
};

View File

@ -6,29 +6,29 @@
std::string ASTPrinter::visitBinaryExpr(BinaryExpr<std::string>* expression){
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){
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){
return expression->value;
}
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) {
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;
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;

View File

@ -7,6 +7,7 @@
#include "../headers/Expression.h"
#include "../headers/Lexer.h"
#include "../headers/ASTPrinter.h"
#include "../headers/TypeWrapper.h"
int main(){
Bob bobLang;
@ -15,14 +16,14 @@ int main(){
ASTPrinter printer;
std::shared_ptr<Expr<std::string> > expression = std::make_shared<BinaryExpr<std::string> >(
std::make_shared<UnaryExpr<std::string> >(
std::shared_ptr<Expr<std::string>> expression = std::make_shared<BinaryExpr<std::string> >(
std::make_shared<UnaryExpr<std::string>>(
Token{MINUS, "-", 1},
std::make_shared<LiteralExpr<std::string>>("123", true)
),
Token{STAR, "*", 1},
std::make_shared<GroupingExpr<std::string> >(
std::make_shared<LiteralExpr<std::string> >("45.67", true)
std::make_shared<GroupingExpr<std::string>>(
std::make_shared<LiteralExpr<std::string>>("45.67", true)
)
);
@ -39,5 +40,15 @@ int main(){
//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;
}