Update to modern standard i guess

This commit is contained in:
Michael Fabian 'Xaymar' Dirks
2024-06-06 13:37:13 +02:00
parent ade728ec31
commit 4e2dd8e30b
29 changed files with 828 additions and 378 deletions
+22
View File
@@ -0,0 +1,22 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#include "arithmetic.hpp"
BlitzLLVM::AST::ArithmeticExpression::ArithmeticExpression(Operator op, std::unique_ptr<Expression> left, std::unique_ptr<Expression> right)
: m_operator(op), m_left(std::move(left)), m_right(std::move(right)) {}
BlitzLLVM::AST::ArithmeticExpression::~ArithmeticExpression() {}
+43
View File
@@ -0,0 +1,43 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "ast.hpp"
#include "value.hpp"
namespace BlitzLLVM {
namespace AST {
enum class Operator : int8_t {
Add, /*+*/
Subtract, /*-*/
Multiply, /***/
Divide, /*/*/
Invert, /*~*/
Power, /*^*/
Equal, /*=*/
};
class ArithmeticExpression : public Expression {
public:
ArithmeticExpression(Operator op, std::unique_ptr<Expression> left, std::unique_ptr<Expression> right);
virtual ~ArithmeticExpression();
private:
Operator m_operator;
std::unique_ptr<Expression> m_left, m_right;
};
}
}
+17
View File
@@ -0,0 +1,17 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#include "ast.hpp"
+26
View File
@@ -0,0 +1,26 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#pragma once
namespace BlitzLLVM {
namespace AST {
class Expression {
public:
virtual ~Expression() {};
};
}
}
View File
View File
+42
View File
@@ -0,0 +1,42 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#include "function.hpp"
BlitzLLVM::AST::ScopeExpression::ScopeExpression() {}
BlitzLLVM::AST::ScopeExpression::~ScopeExpression() {}
void BlitzLLVM::AST::ScopeExpression::AddExpression(std::unique_ptr<Expression> ex) {
m_expressions.push_back(std::move(ex));
}
BlitzLLVM::AST::FunctionExpression::FunctionExpression(ValueType returnType,
std::string& m_name,
std::list<std::unique_ptr<VariableExpression>> parameters,
std::unique_ptr<ScopeExpression> scope)
: m_returnType(returnType), m_name(m_name), m_parameters(std::move(parameters)), m_content(std::move(scope)) {
}
BlitzLLVM::AST::FunctionExpression::~FunctionExpression() {}
BlitzLLVM::AST::CallExpression::CallExpression(std::string& name, std::list<std::unique_ptr<VariableExpression>> arguments)
: m_name(name), m_arguments(std::move(arguments)) {
}
BlitzLLVM::AST::CallExpression::~CallExpression() {}
+63
View File
@@ -0,0 +1,63 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "ast.hpp"
#include "value.hpp"
#include <list>
#include <memory>
#include <string>
namespace BlitzLLVM {
namespace AST {
class ScopeExpression : public Expression {
public:
ScopeExpression();
virtual ~ScopeExpression();
void AddExpression(std::unique_ptr<Expression> ex);
private:
std::list<std::unique_ptr<Expression>> m_expressions;
};
class FunctionExpression : public ScopeExpression {
public:
FunctionExpression(ValueType returnType,
std::string& m_name,
std::list<std::unique_ptr<VariableExpression>> parameters,
std::unique_ptr<ScopeExpression> scope);
virtual ~FunctionExpression();
private:
ValueType m_returnType;
std::string m_name;
std::list<std::unique_ptr<VariableExpression>> m_parameters;
std::unique_ptr<ScopeExpression> m_content;
};
class CallExpression : public Expression {
public:
CallExpression(std::string& name, std::list<std::unique_ptr<VariableExpression>> arguments);
virtual ~CallExpression();
private:
std::string m_name;
std::list<std::unique_ptr<VariableExpression>> m_arguments;
};
}
}
+59
View File
@@ -0,0 +1,59 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#include "value.hpp"
BlitzLLVM::AST::VariableExpression::VariableExpression(std::string& name, ValueType type /*= ValueType::Number*/)
: m_name(name), m_type(type) {}
BlitzLLVM::AST::VariableExpression::~VariableExpression() {}
BlitzLLVM::AST::ValueType BlitzLLVM::AST::VariableExpression::GetType() {
return m_type;
}
BlitzLLVM::AST::NumberExpression::NumberExpression(int32_t value) : value(value) {}
BlitzLLVM::AST::NumberExpression::~NumberExpression() {}
BlitzLLVM::AST::ValueType BlitzLLVM::AST::NumberExpression::GetType() {
return ValueType::Number;
}
BlitzLLVM::AST::DecimalExpression::DecimalExpression(float_t value) : value(value) {}
BlitzLLVM::AST::DecimalExpression::~DecimalExpression() {}
BlitzLLVM::AST::ValueType BlitzLLVM::AST::DecimalExpression::GetType() {
return ValueType::Decimal;
}
BlitzLLVM::AST::StringExpression::StringExpression(std::string value) : value(value) {}
BlitzLLVM::AST::StringExpression::~StringExpression() {}
BlitzLLVM::AST::ValueType BlitzLLVM::AST::StringExpression::GetType() {
return ValueType::String;
}
BlitzLLVM::AST::ConstExpression::ConstExpression(std::string& name, std::unique_ptr<ValueExpression> value)
: m_name(name), m_value(std::move(value)) {}
BlitzLLVM::AST::ConstExpression::~ConstExpression() {}
BlitzLLVM::AST::ValueType BlitzLLVM::AST::ConstExpression::GetType() {
return m_value->GetType();
}
+98
View File
@@ -0,0 +1,98 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "ast.hpp"
#include "lexer.hpp"
#include <inttypes.h>
#include <float.h>
#include <math.h>
#include <memory>
#include <string>
namespace BlitzLLVM {
namespace AST {
enum class ValueType : int8_t {
Unknown,
Number,
Decimal,
String,
Type,
};
class ValueExpression : public Expression {
public:
virtual ValueType GetType() = 0;
};
class ConstExpression : public ValueExpression {
public:
ConstExpression(std::string& name, std::unique_ptr<ValueExpression> value);
virtual ~ConstExpression();
virtual ValueType GetType() override;
private:
std::string m_name;
std::unique_ptr<ValueExpression> m_value;
};
class VariableExpression : public ValueExpression {
public:
VariableExpression(std::string& name, ValueType type = ValueType::Number);
virtual ~VariableExpression();
virtual ValueType GetType() override;
private:
std::string m_name;
ValueType m_type;
};
class NumberExpression : public ValueExpression {
public:
NumberExpression(int32_t value);
virtual ~NumberExpression();
virtual ValueType GetType() override;
private:
int32_t value;
};
class DecimalExpression : public ValueExpression {
public:
DecimalExpression(float_t value);
virtual ~DecimalExpression();
virtual ValueType GetType() override;
private:
float_t value;
};
class StringExpression : public ValueExpression {
public:
StringExpression(std::string value);
virtual ~StringExpression();
virtual ValueType GetType() override;
private:
std::string value;
};
}
}
+41
View File
@@ -0,0 +1,41 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#include "compiler.hpp"
#include "parser.hpp"
#include "lexer.hpp"
#include <fstream>
#include <iostream>
BlitzLLVM::Compiler::Compiler() {}
BlitzLLVM::Compiler::~Compiler() {}
bool BlitzLLVM::Compiler::Compile(std::string in, std::string out) {
/*std::ifstream infile;
infile.open(in);
if (infile.bad() || !infile.good() || infile.eof()) {
std::cerr << "Failed to open file: " << in << std::endl;
return false;
}*/
Parser psr = Parser(in);
if (!psr.Parse()) {
}
return true;
}
+30
View File
@@ -0,0 +1,30 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <string>
namespace BlitzLLVM {
class Compiler {
public:
Compiler();
~Compiler();
bool Compile(std::string in, std::string out);
private:
};
}
+296
View File
@@ -0,0 +1,296 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#include "lexer.hpp"
#include <codecvt>
#include <boost/algorithm/string/predicate.hpp>
std::pair<char, BlitzLLVM::Lexer::Token> g_symbolCharacters[] = {
//{ '\"', BlitzLLVM::Lexer::Token::TokenDoubleQuote }, // Has special meaning.
{ '+', BlitzLLVM::Lexer::Token::TokenPlus },
{ '-', BlitzLLVM::Lexer::Token::TokenMinus },
{ '/', BlitzLLVM::Lexer::Token::TokenSlashForward },
{ '\\', BlitzLLVM::Lexer::Token::TokenSlashBackward },
{ '*', BlitzLLVM::Lexer::Token::TokenMultiply },
{ '=', BlitzLLVM::Lexer::Token::TokenEqual },
{ '#', BlitzLLVM::Lexer::Token::TokenOctothorp },
{ '%', BlitzLLVM::Lexer::Token::TokenPercent },
{ '$', BlitzLLVM::Lexer::Token::TokenDollar },
{ '(', BlitzLLVM::Lexer::Token::TokenRoundBracketOpen },
{ ')', BlitzLLVM::Lexer::Token::TokenRoundBracketClose },
{ '[', BlitzLLVM::Lexer::Token::TokenSquareBracketOpen },
{ ']', BlitzLLVM::Lexer::Token::TokenSquareBracketClose },
{ '<', BlitzLLVM::Lexer::Token::TokenAngleBracketOpen },
{ '>', BlitzLLVM::Lexer::Token::TokenAngleBracketClose },
//{ '.', BlitzLLVM::Lexer::Token::TokenDot }, // Special meaning.
{ ':', BlitzLLVM::Lexer::Token::TokenColon },
{ ',', BlitzLLVM::Lexer::Token::TokenComma },
//{ ';', BlitzLLVM::Lexer::Token::TokenSemicolon },
{ '^', BlitzLLVM::Lexer::Token::TokenCaret },
{ '~', BlitzLLVM::Lexer::Token::TokenBitNot },
};
BlitzLLVM::Lexer::Lexer() {}
BlitzLLVM::Lexer::~Lexer() {}
std::pair<BlitzLLVM::Lexer::Token, std::string> BlitzLLVM::Lexer::GetCurrentToken() {
return std::make_pair(m_currentToken, m_currentText);
}
std::pair<BlitzLLVM::Lexer::Token, std::string> BlitzLLVM::Lexer::GetNextToken(std::shared_ptr<std::istream> fs) {
std::string buf;
Token tkn = Token::TokenEOF;
bool haveResult = false;
// Allow "overriding" the next retrieved Token.
if (m_overrideToken != Token::TokenUnknown) {
buf = m_overrideText;
tkn = m_overrideToken;
m_overrideToken = Token::TokenUnknown;
haveResult = true;
}
bool m_isTextMode = false;
bool m_isNumberMode = false;
bool m_isStringMode = false;
bool m_isCommentMode = false;
bool m_numberModeHasDecimal = false;
while (((fs->eof() == false) && (fs->good())) && !haveResult) {
char chr = fs->get();
if (chr == '\r' || chr == '\n') {
if (tkn != Token::TokenEOF) {
m_overrideToken = Token::TokenNewLine;
m_overrideText = "";
} else {
tkn = Token::TokenNewLine;
buf = "";
}
m_isStringMode = false;
m_isNumberMode = false;
m_isTextMode = false;
m_isCommentMode = false;
break;
} else if (m_isStringMode) {
if (chr == '\"') {
m_overrideToken = Token::TokenDoubleQuote;
m_overrideText = chr;
m_isStringMode = false;
tkn = Token::TokenQuotedText;
break;
} else if (iscntrl(chr) || !isprint(chr)) {
fs->putback(chr);
m_isStringMode = false;
break;
} else {
buf += chr;
}
} else if (m_isTextMode) {
if (isalnum(chr) || (chr == '_')) {
buf += chr;
} else {
fs->putback(chr);
m_isTextMode = false;
break;
}
} else if (m_isNumberMode) {
if (isdigit(chr)) {
buf += chr;
} else if (chr == '.') {
if (m_numberModeHasDecimal == false) {
m_numberModeHasDecimal = true;
tkn = Token::TokenDecimal;
buf += chr;
} else {
fs->putback(chr);
m_isNumberMode = false;
break;
}
} else {
fs->putback(chr);
m_isNumberMode = false;
break;
}
} else if (m_isCommentMode) {
buf += chr;
tkn = Token::TokenComment;
} else {
// Whitespace
if (isspace(chr))
continue;
// Control Code
if (iscntrl(chr)) {
tkn = Token::TokenUnknown;
buf = chr;
}
// Special handling for + and -, due to numbers and decimals.
if (chr == '+' || chr == '-') {
char chr2 = fs->get();
if (isdigit(chr2)) {
m_isNumberMode = true;
m_numberModeHasDecimal = false;
tkn = Token::TokenNumber;
buf = chr + chr2;
break;
} else if (chr2 == '.') {
m_isNumberMode = true;
m_numberModeHasDecimal = true;
tkn = Token::TokenDecimal;
buf = chr + "0" + chr2;
break;
} else {
fs->putback(chr2);
}
}
// Symbol
for (auto v : g_symbolCharacters) {
if (v.first == chr) {
tkn = v.second;
buf = v.first;
break;
}
}
if (tkn != Token::TokenEOF) {
haveResult = true;
break;
}
// Strings, Text, Numbers
if (chr == ';') {
m_isCommentMode = true;
tkn = Token::TokenSemicolon;
buf = chr;
break;
} else if (chr == '\"') {
m_isStringMode = true;
tkn = Token::TokenDoubleQuote;
buf = chr;
break;
} else if (isalpha(chr)) {
m_isTextMode = true;
tkn = Token::TokenText;
buf = chr;
} else if (isdigit(chr)) {
m_isNumberMode = true;
m_numberModeHasDecimal = false;
tkn = Token::TokenNumber;
buf = chr;
} else if (chr == '.') {
m_isNumberMode = true;
m_numberModeHasDecimal = true;
tkn = Token::TokenDecimal;
buf = "0" + chr;
} else {
tkn = Token::TokenUnknown;
buf = chr;
break;
}
}
}
// Convert from Text into native Token.
if (tkn == Token::TokenText)
tkn = ConvertTextToToken(tkn, buf);
return std::make_pair(tkn, buf);
}
BlitzLLVM::Lexer::Token BlitzLLVM::Lexer::ConvertTextToToken(Token in, std::string text) {
static std::pair<const char*, Token> l_textToTokenList[] = {
// Binary
{ "not", Token::TokenNot },
{ "and", Token::TokenAnd },
{ "or", Token::TokenOr },
{ "xor", Token::TokenXor },
{ "shl", Token::TokenShl },
{ "shr", Token::TokenShr },
{ "sal", Token::TokenSal },
{ "sar", Token::TokenSar },
{ "false", Token::TokenFalse },
{ "true", Token::TokenTrue },
// Conversion
{ "float", Token::TokenFloat },
{ "string", Token::TokenString },
{ "hex", Token::TokenHex },
{ "int", Token::TokenInt },
// Control
{ "if", Token::TokenIf },
{ "then", Token::TokenThen },
{ "elseIf", Token::TokenElseIf },
{ "else", Token::TokenElse },
{ "endIf", Token::TokenEndIf },
{ "select", Token::TokenSelect },
{ "case", Token::TokenCase },
{ "default", Token::TokenDefault },
{ "goto", Token::TokenGoto },
{ "gosub", Token::TokenGosub },
{ "return", Token::TokenReturn },
{ "function", Token::TokenFunction },
{ "end", Token::TokenEnd },
{ "stop", Token::TokenStop },
// Loop
{ "for", Token::TokenFor },
{ "to", Token::TokenTo },
{ "next", Token::TokenNext },
{ "while", Token::TokenWhile },
{ "wend", Token::TokenWend },
{ "repeat", Token::TokenRepeat },
{ "until", Token::TokenUntil },
{ "forever", Token::TokenForever },
{ "exit", Token::TokenExit },
// Math
{ "abs", Token::TokenAbs },
{ "sign", Token::TokenSign },
{ "cos", Token::TokenCos },
{ "sin", Token::TokenSin },
{ "tan", Token::TokenTan },
{ "acos", Token::TokenACos },
{ "asin", Token::TokenASin },
{ "atan", Token::TokenATan },
{ "atan2", Token::TokenATan2 },
{ "log", Token::TokenLog },
{ "log10", Token::TokenLog10 },
{ "ceil", Token::TokenCeil },
{ "floor", Token::TokenFloor },
{ "mod", Token::TokenMod },
{ "pi", Token::TokenPi },
{ "exp", Token::TokenExp },
{ "sqr", Token::TokenSqr },
// Variables
{ "const", Token::TokenConst },
{ "global", Token::TokenGlobal },
{ "local", Token::TokenLocal },
// Includes
{ "include", Token::TokenInclude },
};
for (auto v : l_textToTokenList) {
if (boost::iequals(text, v.first)) {
return v.second;
}
}
return in;
}
+127
View File
@@ -0,0 +1,127 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <inttypes.h>
#include <istream>
#include <memory>
#include <string>
#include <utility>
namespace BlitzLLVM {
class Lexer {
public:
enum class Token : uint64_t {
TokenUnknown,
TokenEOF,
TokenNewLine,
// Symbols
TokenPlus,
TokenMinus,
TokenSlashForward,
TokenSlashBackward,
TokenMultiply,
TokenEqual,
TokenOctothorp,
TokenPercent,
TokenDollar,
TokenRoundBracketOpen,
TokenRoundBracketClose,
TokenSquareBracketOpen,
TokenSquareBracketClose,
TokenAngleBracketOpen,
TokenAngleBracketClose,
TokenDot,
TokenColon,
TokenComma,
TokenSemicolon,
TokenCaret,
TokenBitNot /*~*/,
// String Delimiter
TokenDoubleQuote,
// Types
TokenText,
TokenNumber,
TokenDecimal,
TokenQuotedText, // Text encapsulated by TokenDoubleQuote
TokenComment,
// Binary
TokenNot,
TokenAnd, TokenOr, TokenXor,
TokenShl, TokenShr,
TokenSal, TokenSar,
TokenFalse, TokenTrue,
// Conversion
TokenFloat,
TokenString, TokenHex,
TokenInt,
// Control
TokenIf, TokenThen, TokenElseIf, TokenElse, TokenEndIf,
TokenSelect, TokenCase, TokenDefault, // End Select = TokenEnd, TokenSelect.
TokenGoto, TokenGosub,
TokenReturn,
TokenFunction, // End Function = TokenEnd, TokenFunction.
TokenEnd,
TokenStop /* DEBUGGER! Ignore in Release mode. */,
// Loop
TokenFor, TokenTo, TokenNext,
TokenWhile, TokenWend,
TokenRepeat, TokenUntil, TokenForever,
TokenExit,
// Math
TokenAbs, TokenSign /*Sgn*/,
TokenCos, TokenSin, TokenTan,
TokenACos, TokenASin, TokenATan, TokenATan2,
TokenLog, TokenLog10,
TokenCeil, TokenFloor,
TokenMod,
TokenPi,
TokenExp, TokenSqr,
// Variables
TokenConst,
TokenGlobal,
TokenLocal,
// Including files.
TokenInclude,
};
public:
Lexer();
~Lexer();
std::pair<Token, std::string> GetCurrentToken();
std::pair<Token, std::string> GetNextToken(std::shared_ptr<std::istream> fs);
private:
BlitzLLVM::Lexer::Token ConvertTextToToken(Token in, std::string text);
private:
Token m_currentToken = Token::TokenUnknown;
std::string m_currentText = "";
Token m_overrideToken = Token::TokenUnknown;
std::string m_overrideText = "";
};
}
+110
View File
@@ -0,0 +1,110 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#include <iostream>
#include "boost/program_options.hpp"
#include "compiler.hpp"
#include "version.h"
#define LICENSE "Copyright (C) 2017 Michael Fabian Dirks\n\
This program comes with ABSOLUTELY NO WARRANTY, for details launch with `--warranty`.\n\
This is free software, and you are welcome to redistribute it under certain conditions."
#define WARRANTY "\
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.EXCEPT WHEN OTHERWISE STATED IN \
WRITING THE COPYRIGHT HOLDERS AND / OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, \
EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR \
A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM \
PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION."
int main(int argc, char** argv) {
std::string optInput;
bool optQuiet, optVerbose;
#pragma region Define Program Options
boost::program_options::options_description opts_help("Generic");
opts_help.add_options()
("help,h", "Show this help message.")
("warranty", "Show warranty information.")
("quiet,q", boost::program_options::value<bool>(&optQuiet)->default_value(false), "Use quieter logging.")
("verbose,v", boost::program_options::value<bool>(&optVerbose)->default_value(false), "Use verbose logging (overrides quiet).")
;
boost::program_options::options_description opts_param("Parameters");
opts_param.add_options()
("input,i", boost::program_options::value<std::string>(&optInput), "Input .bb file.")
;
boost::program_options::options_description opts;
opts.add(opts_help).add(opts_param);
boost::program_options::positional_options_description opts_pos;
opts_pos.add("input", -1);
#pragma endregion Define Program Options
#pragma region Convert ArgC/ArgV to Program Options
boost::program_options::variables_map vm;
{
auto clp = boost::program_options::command_line_parser(argc, argv);
boost::program_options::store(clp.options(opts).positional(opts_pos).run(), vm);
boost::program_options::notify(vm);
}
#pragma endregion Convert ArgC/ArgV to Program Options
#pragma region Header, Warranty, Help
// Header
if (!optQuiet || optVerbose) {
std::cout
<< "BlitzLLVM Code Compiler"
<< " v" << VERSION_MAJOR
<< "." << VERSION_MINOR
<< "." << VERSION_PATCH
<< " " << LICENSE
<< '\n' << std::endl;
}
// Warranty
if (vm.count("warranty")) {
std::cout << '\n' << WARRANTY << '\n' << std::endl;
#ifdef _DEBUG
std::cin.get();
#endif
return 1;
}
// Help
if (vm.empty() || vm.count("help")) {
std::cout
<< "Usage: cc [options] <file.bb>" << '\n'
<< opts
<< std::endl;
#ifdef _DEBUG
std::cin.get();
#endif
return 1;
}
#pragma endregion Header, Warranty, Help
#pragma region Process Input
BlitzLLVM::Compiler comp;
comp.Compile(optInput, optInput + ".exe");
#pragma endregion Process Input
#ifdef _DEBUG
std::cin.get();
#endif
return 0;
}
+125
View File
@@ -0,0 +1,125 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#include "parser.hpp"
#include "ast/function.hpp"
#include <iostream>
#include <vector>
#include <stdarg.h>
BlitzLLVM::Parser::Parser(std::string file) {
// Try and load the file
std::shared_ptr<std::ifstream> instream = std::make_shared<std::ifstream>(file);
if (instream->bad() || !instream->good()) {
throw std::ios_base::failure("Failed to open file.");
}
m_files.push(std::make_pair(file, instream));
}
BlitzLLVM::Parser::~Parser() {
while (m_files.size() > 0) {
std::shared_ptr<std::ifstream> file = std::dynamic_pointer_cast<std::ifstream>(m_files.top().second);
file->close();
m_files.pop();
}
}
std::unique_ptr<BlitzLLVM::AST::Expression> BlitzLLVM::Parser::Parse() {
std::unique_ptr<AST::ScopeExpression> scope = std::make_unique<AST::ScopeExpression>();
std::unique_ptr<AST::Expression> expr;
while ((expr = std::move(ParseExpression())) != nullptr) {
scope->AddExpression(std::move(expr));
}
return std::move(scope);
}
void BlitzLLVM::Parser::LogMessage(const char* msg, ...) {
std::vector<char> buf(65535);
va_list val;
va_start(val, msg);
int rval = vsnprintf(buf.data(), buf.size(), msg, val);
va_end(val);
std::cout << buf.data() << '\n';
}
void BlitzLLVM::Parser::LogError(const char* msg, ...) {
std::vector<char> buf(65535);
va_list val;
va_start(val, msg);
int rval = vsnprintf(buf.data(), buf.size(), msg, val);
va_end(val);
std::cerr << buf.data() << '\n';
}
std::pair<BlitzLLVM::Lexer::Token, std::string> BlitzLLVM::Parser::GetNextToken() {
return m_lexer.GetNextToken(m_files.top().second);
}
std::unique_ptr<BlitzLLVM::AST::Expression> BlitzLLVM::Parser::ParseExpression() {
while (true) {
auto tkn = GetNextToken();
switch (tkn.first) {
case BlitzLLVM::Lexer::Token::TokenNewLine:
case BlitzLLVM::Lexer::Token::TokenComment:
// Skip Comments, since we don't really need them for the AST.
continue;
case BlitzLLVM::Lexer::Token::TokenPlus:
case BlitzLLVM::Lexer::Token::TokenMinus:
default: // End Of File / Unknown
case BlitzLLVM::Lexer::Token::TokenUnknown:
case BlitzLLVM::Lexer::Token::TokenEOF:
return nullptr;
break;
}
}
}
std::unique_ptr<BlitzLLVM::AST::NumberExpression> BlitzLLVM::Parser::ParseNumber(BlitzLLVM::Lexer::Token token, std::string value) {
if (token != Lexer::Token::TokenNumber) {
LogError("Unexpected Token during parsing, expected number.");
return nullptr;
}
char* endptr = const_cast<char*>(value.c_str() + value.size());
int32_t parsed = strtol(value.c_str(), &endptr, 10);
if (errno == ERANGE) {
LogError("Number out of range.");
return nullptr;
}
return std::make_unique<BlitzLLVM::AST::NumberExpression>(parsed);
}
std::unique_ptr<BlitzLLVM::AST::DecimalExpression> BlitzLLVM::Parser::ParseDecimal(BlitzLLVM::Lexer::Token token, std::string value) {
if (token != Lexer::Token::TokenNumber) {
LogError("Unexpected Token during parsing, expected number.");
return nullptr;
}
char* endptr = const_cast<char*>(value.c_str() + value.size());
float_t parsed = strtof(value.c_str(), &endptr);
if (errno == ERANGE) {
LogError("Number out of range.");
return nullptr;
}
return std::make_unique<BlitzLLVM::AST::DecimalExpression>(parsed);
}
+52
View File
@@ -0,0 +1,52 @@
// Code Compiler for BlitzLLVM
// Copyright(C) 2017 Michael Fabian Dirks
//
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "lexer.hpp"
#include "ast/ast.hpp"
#include "ast/value.hpp"
#include <fstream>
#include <map>
#include <memory>
#include <string>
#include <stack>
namespace BlitzLLVM {
class Parser {
public:
Parser(std::string file);
~Parser();
std::unique_ptr<AST::Expression> Parse();
protected:
void LogMessage(const char* msg, ...);
void LogError(const char* msg, ...);
private:
std::pair<BlitzLLVM::Lexer::Token, std::string> GetNextToken();
private:
std::unique_ptr<AST::Expression> ParseExpression();
std::unique_ptr<AST::NumberExpression> ParseNumber(BlitzLLVM::Lexer::Token token, std::string value);
std::unique_ptr<AST::DecimalExpression> ParseDecimal(BlitzLLVM::Lexer::Token token, std::string value);
private:
Lexer m_lexer;
std::stack<std::pair<std::string, std::shared_ptr<std::istream>>> m_files;
};
}