From ddb2d1d89d8fb741072f9cb822472504a6733ff1 Mon Sep 17 00:00:00 2001 From: planeer Date: Thu, 25 Apr 2019 11:21:57 +0200 Subject: [PATCH] DmsObject put in DmsGame and serialization --- dmc/src/DmsFields/DmsFloatField.h | 2 +- dmc/src/DmsFields/DmsIntField.h | 2 +- dmc/src/DmsFields/DmsStringField.h | 2 +- dmc/src/DmsGame.cpp | 21 ++++++++++ dmc/src/DmsGame.h | 19 +++++++++ dmc/src/DmsObject.h | 2 - dmc/src/Parser.h | 62 ++++++++++++++++++++++++++++++ dmc/src/main.cpp | 2 + 8 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 dmc/src/DmsGame.cpp create mode 100644 dmc/src/DmsGame.h diff --git a/dmc/src/DmsFields/DmsFloatField.h b/dmc/src/DmsFields/DmsFloatField.h index 1be626f..a972ece 100644 --- a/dmc/src/DmsFields/DmsFloatField.h +++ b/dmc/src/DmsFields/DmsFloatField.h @@ -26,7 +26,7 @@ class DmsFloatField: public DmsField { void set_value(float value_); float get_value(); - std::string serialize() { return ""; }; + std::string serialize() { return name + " -> " + std::to_string(value) + "\n"; }; std::string compile() { return ""; }; private: diff --git a/dmc/src/DmsFields/DmsIntField.h b/dmc/src/DmsFields/DmsIntField.h index 973a210..453756e 100644 --- a/dmc/src/DmsFields/DmsIntField.h +++ b/dmc/src/DmsFields/DmsIntField.h @@ -26,7 +26,7 @@ class DmsIntField: public DmsField { void set_value(int value_); int get_value(); - std::string serialize() { return ""; }; + std::string serialize() { return name + " -> " + std::to_string(value) + "\n"; }; std::string compile() { return ""; }; private: diff --git a/dmc/src/DmsFields/DmsStringField.h b/dmc/src/DmsFields/DmsStringField.h index 4eb009d..0bd9d91 100644 --- a/dmc/src/DmsFields/DmsStringField.h +++ b/dmc/src/DmsFields/DmsStringField.h @@ -26,7 +26,7 @@ class DmsStringField: public DmsField { void set_value(std::string value_); std::string get_value(); - std::string serialize() { return ""; }; + std::string serialize() { return name + " -> " + value + "\n"; }; std::string compile() { return ""; }; private: diff --git a/dmc/src/DmsGame.cpp b/dmc/src/DmsGame.cpp new file mode 100644 index 0000000..7d7d0df --- /dev/null +++ b/dmc/src/DmsGame.cpp @@ -0,0 +1,21 @@ +#include "DmsGame.h" + +DmsGame::DmsGame() { + constants = new DmsObject(); + players = new DmsObject(); + enemies = new DmsObject(); + encounters = new DmsObject(); + scenarios = new DmsObject(); +} + + +DmsGame::~DmsGame() { +} + +std::string DmsGame::serialize() { + return "\nCONSTANTS: \n" + constants->serialize() + + "\nPLAYERS: \n" + players->serialize() + + "\nENEMIES: \n" + enemies->serialize() + + "\nENCOUNTERS: \n" + encounters->serialize() + + "\nSCENARIOS: \n" + scenarios->serialize(); +} diff --git a/dmc/src/DmsGame.h b/dmc/src/DmsGame.h new file mode 100644 index 0000000..a71087a --- /dev/null +++ b/dmc/src/DmsGame.h @@ -0,0 +1,19 @@ +#pragma once + +#include "DmsObject.h" + +class DmsGame { +public: + DmsGame(); + ~DmsGame(); + + // TODO - Choose serialize or compile or both and choose type str/byte[]... + std::string serialize(); + std::string compile(); + + DmsObject *constants; + DmsObject *players; + DmsObject *enemies; + DmsObject *encounters; + DmsObject *scenarios; +}; diff --git a/dmc/src/DmsObject.h b/dmc/src/DmsObject.h index 0637db3..afa7a02 100644 --- a/dmc/src/DmsObject.h +++ b/dmc/src/DmsObject.h @@ -10,8 +10,6 @@ class DmsObject { // TODO - Choose serialize or compile or both and choose type str/byte[]... std::string serialize() { return field_scope.serialize(); }; std::string compile() { return field_scope.compile(); }; - -protected: DmsFieldScope field_scope; }; diff --git a/dmc/src/Parser.h b/dmc/src/Parser.h index e660b2e..462437f 100644 --- a/dmc/src/Parser.h +++ b/dmc/src/Parser.h @@ -3,6 +3,7 @@ #include #include "Scanner.h" +#include "DmsGame.h" class Parser { public: @@ -37,6 +38,8 @@ class Parser { return result == Error; } + DmsGame *game = new DmsGame(); + protected: // BNF Grammar // @@ -83,9 +86,15 @@ class Parser { bool CONSTANT() { if (token.type() == Token::Identifier) { + + std::string lexem = token.lexem(); + string_E = ""; + token = scanner.next_token(); if (!E()) return Error; + game->constants->field_scope.set_field_value(lexem, string_E); + if (token.type() == Token::Identifier) { return CONSTANT(); } else { @@ -97,6 +106,8 @@ class Parser { bool PLAYERS() { if (token.lexem() == "PLAYERS:") { + current = game->players; + token = scanner.next_token(); return PLAYER(); } @@ -105,6 +116,8 @@ class Parser { bool PLAYER() { if (token.type() == Token::Identifier) { + string_OBJECT = token.lexem() + "_"; + token = scanner.next_token(); if (!STATS()) return Error; @@ -119,6 +132,8 @@ class Parser { bool ENEMIES() { if (token.lexem() == "ENEMIES:") { + current = game->enemies; + token = scanner.next_token(); return ENEMY(); } @@ -127,6 +142,8 @@ class Parser { bool ENEMY() { if (token.type() == Token::Identifier) { + string_OBJECT = token.lexem() + "_"; + token = scanner.next_token(); if (!STATS()) return Error; @@ -150,9 +167,15 @@ class Parser { bool STAT() { if (token.lexem() == "has") { token = scanner.next_token(); + + string_E = ""; + if (!E()) return Error; if (token.type() == Token::Identifier) { + + current->field_scope.set_field_value(string_OBJECT + token.lexem(), string_E); + token = scanner.next_token(); return Ok; } @@ -162,6 +185,8 @@ class Parser { bool ENCOUNTERS() { if (token.lexem() == "ENCOUNTERS:") { + current = game->encounters; + token = scanner.next_token(); return HAPPENINGS(); } @@ -170,6 +195,8 @@ class Parser { bool SCENARIOS() { if (token.lexem() == "SCENARIOS:") { + current = game->scenarios; + token = scanner.next_token(); return HAPPENINGS(); } @@ -178,11 +205,17 @@ class Parser { bool HAPPENINGS() { if (token.type() == Token::Identifier) { + + std::string lexem = token.lexem(); token = scanner.next_token(); if (token.lexem() == "has") { token = scanner.next_token(); + + string_THING = ""; if (!THING()) return Error; + current->field_scope.set_field_value(lexem, string_THING); + if (token.type() == Token::Identifier) { return HAPPENINGS(); } else { @@ -195,10 +228,14 @@ class Parser { bool THING() { if (token.type() == Token::Identifier) { + string_THING += token.lexem(); + token = scanner.next_token(); if (!OCCURRENCES()) return Error; if (token.lexem() == "+") { + string_THING += token.lexem(); + token = scanner.next_token(); return THING(); } else { @@ -211,9 +248,13 @@ class Parser { bool OCCURRENCES() { if (token.lexem() == "*") { + string_THING += token.lexem(); + token = scanner.next_token(); if (token.type() == Token::Float) { + string_THING += token.lexem(); + token = scanner.next_token(); return Ok; } else { @@ -240,6 +281,9 @@ class Parser { bool EE() { if (token.lexem() == "+" || token.lexem() == "-") { + + string_E += token.lexem(); + token = scanner.next_token(); return T() && EE(); } @@ -252,6 +296,9 @@ class Parser { bool TT() { if (token.lexem() == "*" || token.lexem() == "/" || token.lexem() == "^" || token.lexem() == "%") { + + string_E += token.lexem(); + token = scanner.next_token(); return F() && TT(); } @@ -260,16 +307,25 @@ class Parser { bool F() { if (token.lexem() == "(") { + + string_E += token.lexem(); + token = scanner.next_token(); if (!E()) return Error; if (token.lexem() == ")") { + + string_E += token.lexem(); + token = scanner.next_token(); return Ok; } else { return Error; } } else if (token.type() == Token::Float || token.type() == Token::Identifier) { + + string_E += token.lexem(); + token = scanner.next_token(); return Ok; } @@ -285,4 +341,10 @@ class Parser { Scanner scanner; Token token; bool result; + + std::string string_E = ""; + std::string string_STAT = ""; + std::string string_OBJECT = ""; + std::string string_THING = ""; + DmsObject *current; }; diff --git a/dmc/src/main.cpp b/dmc/src/main.cpp index 16f268a..f1f2ea1 100644 --- a/dmc/src/main.cpp +++ b/dmc/src/main.cpp @@ -39,6 +39,8 @@ int main(int argc, char *argv[]) { bool res = parser.parse(); std::cout << (res ? "Successful!" : "Failed!") << std::endl; + std::cout << parser.game->serialize() << std::endl; + // Cleanup in_f.close();