diff --git a/.gitignore b/.gitignore index 096eb1d..f5c757a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -.vscode/ +/.vscode build/ -.idea + diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -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 diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..53624c9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,18 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/headers/ASTPrinter.h b/headers/ASTPrinter.h index fbcda39..5fff0b2 100644 --- a/headers/ASTPrinter.h +++ b/headers/ASTPrinter.h @@ -13,6 +13,6 @@ public: int test = 10; std::string print(Expr* expr); private: - std::string parenthesize(std::string name, std::vector > > exprs); + std::string parenthesize(std::string name, std::vector>> exprs); }; \ No newline at end of file diff --git a/headers/Expression.h b/headers/Expression.h index b2ae208..9e00c8c 100644 --- a/headers/Expression.h +++ b/headers/Expression.h @@ -18,11 +18,11 @@ struct Expr{ template struct BinaryExpr : Expr { - const std::shared_ptr > left; + const std::shared_ptr> left; const Token oper; - const std::shared_ptr > right; + const std::shared_ptr> right; - BinaryExpr(std::shared_ptr > left, Token oper, std::shared_ptr > right) : left(left), oper(oper), right(right) + BinaryExpr(std::shared_ptr> left, Token oper, std::shared_ptr > right) : left(left), oper(oper), right(right) { } T accept(Visitor* visitor) override{ @@ -32,9 +32,9 @@ struct BinaryExpr : Expr template struct GroupingExpr : Expr { - const std::shared_ptr > expression; + const std::shared_ptr> expression; - GroupingExpr(std::shared_ptr > expression) : expression(expression) + GroupingExpr(std::shared_ptr> expression) : expression(expression) { } T accept(Visitor* visitor) override{ @@ -57,9 +57,9 @@ template struct UnaryExpr : Expr { const Token oper; - const std::shared_ptr > right; + const std::shared_ptr> right; - UnaryExpr(Token oper, std::shared_ptr > right) : oper(oper), right(right) + UnaryExpr(Token oper, std::shared_ptr> right) : oper(oper), right(right) { } T accept(Visitor* visitor) override{ diff --git a/headers/TypeWrapper.h b/headers/TypeWrapper.h new file mode 100644 index 0000000..8145cbc --- /dev/null +++ b/headers/TypeWrapper.h @@ -0,0 +1,18 @@ +#pragma once +#include +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) {} +}; \ No newline at end of file diff --git a/source/ASTPrinter.cpp b/source/ASTPrinter.cpp index 3d7e6dd..9a99dc7 100644 --- a/source/ASTPrinter.cpp +++ b/source/ASTPrinter.cpp @@ -6,29 +6,29 @@ std::string ASTPrinter::visitBinaryExpr(BinaryExpr* expression){ std::cout << expression->left << std::endl; - return parenthesize(expression->oper.lexeme, std::vector > >{expression->left, expression->right}); + return parenthesize(expression->oper.lexeme, std::vector>>{expression->left, expression->right}); } std::string ASTPrinter::visitGroupingExpr(GroupingExpr* expression){ - return parenthesize("group", std::vector > >{expression->expression}); + return parenthesize("group", std::vector>>{expression->expression}); } std::string ASTPrinter::visitLiteralExpr(LiteralExpr* expression){ return expression->value; } std::string ASTPrinter::visitUnaryExpr(UnaryExpr* expression){ - return parenthesize(expression->oper.lexeme, std::vector > >{expression->right}); + return parenthesize(expression->oper.lexeme, std::vector>>{expression->right}); } std::string ASTPrinter::print(Expr *expr) { return expr->accept(this); } -std::string ASTPrinter::parenthesize(std::string name, std::vector > > exprs) { +std::string ASTPrinter::parenthesize(std::string name, std::vector>> exprs) { std::string builder; builder += "(" + name; - for(std::shared_ptr > expr : exprs) + for(std::shared_ptr> expr : exprs) { std::cout << expr << std::endl; diff --git a/source/main.cpp b/source/main.cpp index 0592045..6af5ec5 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -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 > expression = std::make_shared >( - std::make_shared >( + std::shared_ptr> expression = std::make_shared >( + std::make_shared>( Token{MINUS, "-", 1}, std::make_shared>("123", true) ), Token{STAR, "*", 1}, - std::make_shared >( - std::make_shared >("45.67", true) + std::make_shared>( + std::make_shared>("45.67", true) ) ); @@ -39,5 +40,15 @@ int main(){ //bobLang.runPrompt(); + std::shared_ptr object = std::make_shared(String{"Hi"}); + + if(auto num = std::dynamic_pointer_cast(object)) + { + std::cout << num->value << std::endl; + }else if(auto str = std::dynamic_pointer_cast(object)) + { + std::cout << str->value << std::endl; + } + return 0; }