Skip to content

Commit

Permalink
Fix some pointer deletion not running destructors
Browse files Browse the repository at this point in the history
  • Loading branch information
colinator27 committed Aug 14, 2020
1 parent 17deeac commit 25b312d
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 159 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(diannex)
option(LINK_LIBSTD_FS "Link with libstdc++fs" OFF)
option(LINK_LIBCPP_FS "Link with libc++fs" OFF)

add_executable(diannex src/main.cpp src/Lexer.cpp src/Parser.cpp src/Bytecode.cpp src/Binary.cpp src/BinaryWriter.cpp src/Utility.cpp src/Translation.cpp src/libs/miniz/miniz.c)
add_executable(diannex src/main.cpp src/Lexer.cpp src/Parser.cpp src/Bytecode.cpp src/Binary.cpp src/BinaryWriter.cpp src/Utility.cpp src/Translation.cpp src/Context.cpp src/libs/miniz/miniz.c)
target_include_directories(diannex PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
set_property(TARGET diannex PROPERTY CXX_STANDARD 17)

Expand Down
22 changes: 6 additions & 16 deletions include/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <algorithm>

#include "Instruction.h"
#include "Token.h"
#include "Project.h"
#include "ParseResult.h"

namespace diannex
{
Expand All @@ -34,8 +36,8 @@ namespace diannex
std::queue<std::string> queue;
std::string currentFile;
std::unordered_set<std::string> files;
std::vector<std::pair<std::string, std::vector<struct Token>>> tokenList;
std::vector<std::pair<std::string, struct ParseResult*>> parseList;
std::vector<std::pair<std::string, std::vector<Token>>> tokenList;
std::vector<std::pair<std::string, ParseResult*>> parseList;
std::unordered_map<std::string, std::vector<int>> sceneBytecode;
std::unordered_map<std::string, std::vector<int>> functionBytecode;
std::unordered_set<std::string> definitions;
Expand All @@ -50,21 +52,9 @@ namespace diannex
std::vector<TranslationInfo> translationInfo;
bool generatingFunction = false;

~CompileContext()
{
for (auto it = parseList.begin(); it != parseList.end(); it++)
{
delete it->second;
}
}
~CompileContext();

int string(std::string str)
{
int res = std::find(internalStrings.begin(), internalStrings.end(), str) - internalStrings.begin();
if (res == internalStrings.size())
internalStrings.push_back(str);
return res;
}
int string(std::string str);
};
}

Expand Down
128 changes: 1 addition & 127 deletions include/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,136 +10,10 @@
#include "Project.h"
#include "Utility.h"
#include "Context.h"
#include "Token.h"

namespace diannex
{
enum TokenType
{
Identifier, // a-z, A-Z, other language chars, _ (and 0-9 or . after first char)
Number, // 0-9 first chars, optional . followed by more 0-9
Percentage, // %
String, // " followed by content, ending with unescaped " (no backslash preceding it) (also some other escape codes)
MarkedString, // @" and then continue like String
ExcludeString, // !" and then continue like String

GroupKeyword, // These are reserved identifiers, documented in below enum
MainKeyword,
MainSubKeyword,
ModifierKeyword,

OpenParen, // (
CloseParen, // )
OpenCurly, // {
CloseCurly, // }
OpenBrack, // [
CloseBrack, // ]
Semicolon, // ;
Colon, // :
Comma, // ,
Ternary, // ?

VariableStart, // $

Newline, // Used contextually as a semicolon at the end of statements

Equals, // =
Plus, // +
Increment, // ++
PlusEquals, // +=
Minus, // -
Decrement, // --
MinusEquals, // -=
Multiply, // *
Power, // **
MultiplyEquals, // *=
Divide, // /
DivideEquals, // /=
Mod, // %
ModEquals, // %=
Not, // !

CompareEQ, // ==
CompareGT, // >
CompareLT, // <
CompareGTE, // >=
CompareLTE, // <=
CompareNEQ, // !=

LogicalAnd, // &&
LogicalOr, // ||

BitwiseLShift, // <<
BitwiseRShift, // >>
BitwiseAnd, // &
BitwiseAndEquals, // &=
BitwiseOr, // |
BitwiseOrEquals, // |=
BitwiseXor, // ^
BitwiseXorEquals, // ^=
BitwiseNegate, // ~

Directive, // #

MarkedComment, // "//*" or "/**" followed by comment, latter closed with normal "*/"

Error, // If no token matches for some reason OR an error value in the parser
ErrorString, // If there's an error token, but we want to give the token string in the message
ErrorUnenclosedString, // If there's a string with no end
};

enum KeywordType
{
None,

// Group scope (highest level)
Namespace,
Scene,
Def,
Func,

// Main scope (scene/function-scope)
Choice,
Choose,
If,
Else,
While,
For,
Do,
Repeat,
Switch,
Continue,
Break,
Return,
Case,
Default,

// Choice/choose scope
Require,

// Modifiers (in either scope)
Local,
Global,

// Directive keywords
Include,
IfDef,
IfNDef,
EndIf
};

struct Token
{
TokenType type;
uint32_t line;
uint16_t column;
KeywordType keywordType;
std::string content; // unused if KeywordType is known

Token(TokenType type, uint32_t line, uint16_t column);
Token(TokenType type, uint32_t line, uint16_t column, KeywordType keywordType);
Token(TokenType type, uint32_t line, uint16_t column, std::string content);
};

const char* tokenToString(Token t);

class Lexer
Expand Down
20 changes: 20 additions & 0 deletions include/ParseResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef DIANNEX_PARSERESULT_H
#define DIANNEX_PARSERESULT_H

#include <vector>

namespace diannex
{
struct ParseResult
{
class Node* baseNode;
std::vector<struct ParseError> errors;
bool doDelete = true;

~ParseResult();

ParseResult(const ParseResult&) = delete;
};
}

#endif
17 changes: 2 additions & 15 deletions include/Parser.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef DIANNEX_PARSER_H
#define DIANNEX_PARSER_H

#include "Context.h"
#include "Lexer.h"
#include "ParseResult.h"

#include <memory>

Expand Down Expand Up @@ -31,21 +33,6 @@ namespace diannex
const char* info2;
};

struct ParseResult
{
class Node* baseNode;
std::vector<ParseError> errors;
bool doDelete = true;

~ParseResult()
{
if (doDelete)
delete baseNode;
}

ParseResult(const ParseResult&) = delete;
};

class Parser
{
public:
Expand Down
136 changes: 136 additions & 0 deletions include/Token.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#ifndef DIANNEX_TOKEN_H
#define DIANNEX_TOKEN_H

#include <string>

namespace diannex
{
enum TokenType
{
Identifier, // a-z, A-Z, other language chars, _ (and 0-9 or . after first char)
Number, // 0-9 first chars, optional . followed by more 0-9
Percentage, // %
String, // " followed by content, ending with unescaped " (no backslash preceding it) (also some other escape codes)
MarkedString, // @" and then continue like String
ExcludeString, // !" and then continue like String

GroupKeyword, // These are reserved identifiers, documented in below enum
MainKeyword,
MainSubKeyword,
ModifierKeyword,

OpenParen, // (
CloseParen, // )
OpenCurly, // {
CloseCurly, // }
OpenBrack, // [
CloseBrack, // ]
Semicolon, // ;
Colon, // :
Comma, // ,
Ternary, // ?

VariableStart, // $

Newline, // Used contextually as a semicolon at the end of statements

Equals, // =
Plus, // +
Increment, // ++
PlusEquals, // +=
Minus, // -
Decrement, // --
MinusEquals, // -=
Multiply, // *
Power, // **
MultiplyEquals, // *=
Divide, // /
DivideEquals, // /=
Mod, // %
ModEquals, // %=
Not, // !

CompareEQ, // ==
CompareGT, // >
CompareLT, // <
CompareGTE, // >=
CompareLTE, // <=
CompareNEQ, // !=

LogicalAnd, // &&
LogicalOr, // ||

BitwiseLShift, // <<
BitwiseRShift, // >>
BitwiseAnd, // &
BitwiseAndEquals, // &=
BitwiseOr, // |
BitwiseOrEquals, // |=
BitwiseXor, // ^
BitwiseXorEquals, // ^=
BitwiseNegate, // ~

Directive, // #

MarkedComment, // "//*" or "/**" followed by comment, latter closed with normal "*/"

Error, // If no token matches for some reason OR an error value in the parser
ErrorString, // If there's an error token, but we want to give the token string in the message
ErrorUnenclosedString, // If there's a string with no end
};

enum KeywordType
{
None,

// Group scope (highest level)
Namespace,
Scene,
Def,
Func,

// Main scope (scene/function-scope)
Choice,
Choose,
If,
Else,
While,
For,
Do,
Repeat,
Switch,
Continue,
Break,
Return,
Case,
Default,

// Choice/choose scope
Require,

// Modifiers (in either scope)
Local,
Global,

// Directive keywords
Include,
IfDef,
IfNDef,
EndIf
};

struct Token
{
TokenType type;
uint32_t line;
uint16_t column;
KeywordType keywordType;
std::string content; // unused if KeywordType is known

Token(TokenType type, uint32_t line, uint16_t column);
Token(TokenType type, uint32_t line, uint16_t column, KeywordType keywordType);
Token(TokenType type, uint32_t line, uint16_t column, std::string content);
};
}

#endif // DIANNEX_TOKEN_H
Loading

0 comments on commit 25b312d

Please sign in to comment.