Skip to content

Commit

Permalink
DmsObject put in DmsGame and serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
planeer committed Apr 25, 2019
1 parent f658b9c commit ddb2d1d
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dmc/src/DmsFields/DmsFloatField.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion dmc/src/DmsFields/DmsIntField.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion dmc/src/DmsFields/DmsStringField.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions dmc/src/DmsGame.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
19 changes: 19 additions & 0 deletions dmc/src/DmsGame.h
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 0 additions & 2 deletions dmc/src/DmsObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

62 changes: 62 additions & 0 deletions dmc/src/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <istream>

#include "Scanner.h"
#include "DmsGame.h"

class Parser {
public:
Expand Down Expand Up @@ -37,6 +38,8 @@ class Parser {
return result == Error;
}

DmsGame *game = new DmsGame();

protected:
// BNF Grammar
//
Expand Down Expand Up @@ -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 {
Expand All @@ -97,6 +106,8 @@ class Parser {

bool PLAYERS() {
if (token.lexem() == "PLAYERS:") {
current = game->players;

token = scanner.next_token();
return PLAYER();
}
Expand All @@ -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;

Expand All @@ -119,6 +132,8 @@ class Parser {

bool ENEMIES() {
if (token.lexem() == "ENEMIES:") {
current = game->enemies;

token = scanner.next_token();
return ENEMY();
}
Expand All @@ -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;

Expand All @@ -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;
}
Expand All @@ -162,6 +185,8 @@ class Parser {

bool ENCOUNTERS() {
if (token.lexem() == "ENCOUNTERS:") {
current = game->encounters;

token = scanner.next_token();
return HAPPENINGS();
}
Expand All @@ -170,6 +195,8 @@ class Parser {

bool SCENARIOS() {
if (token.lexem() == "SCENARIOS:") {
current = game->scenarios;

token = scanner.next_token();
return HAPPENINGS();
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -240,6 +281,9 @@ class Parser {

bool EE() {
if (token.lexem() == "+" || token.lexem() == "-") {

string_E += token.lexem();

token = scanner.next_token();
return T() && EE();
}
Expand All @@ -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();
}
Expand All @@ -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;
}
Expand All @@ -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;
};
2 changes: 2 additions & 0 deletions dmc/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit ddb2d1d

Please sign in to comment.