Skip to content

Commit

Permalink
v1.12
Browse files Browse the repository at this point in the history
-Fixed all cases when 'this.' was not added before members/methods
-Fixed when ! was replaces witn minus instead of ~ in some cases
-Now all method names is correct
-Added deallocation
-Now variable created with .allocate inside of create method is not translated and all this variable mentions are replaced with 'this'
-Fixed a lot of bugs
  • Loading branch information
goshante committed Dec 7, 2019
1 parent f81e241 commit 85c7d68
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Warcraft III Reforged brought us Lua scripting, but still a lot of map makers fo
- Read logs if something goes wrong, this application leaves cjass2lua.log file with all it's actions and possible problems. Everything should be fine if you have no warnings.

### Latest release
- [v1.11](https://github.com/fullmetal-a/cjass2lua/releases/tag/v1.11)
- [v1.12](https://github.com/fullmetal-a/cjass2lua/releases/tag/v1.12)

### Manual
(No TL;DR sections, complicated bullshit or other retarded shit, just exe file with simple interface)
Expand Down
8 changes: 4 additions & 4 deletions cJass2Lua/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,21 @@ namespace Utils

std::string op2lua(const std::string& op, cJass::OperationObject::ConstType prevType)
{
if (op == "&&")
if (op == "!")
return " not ";
else if (op == "&&")
return " and ";
else if (op == "." || op == ":")
return op;
else if (op == "+" && prevType == cJass::OperationObject::ConstType::String)
return " .. ";
else if (op == "||")
return " or ";
else if (op == "!")
return " not ";
else if (op == "!=")
return " ~= ";
else if (op == "++" || op == "--" || op == "+=" || op == "-=" || op == "*=" || op == "/=")
return "";
if (op == "dig_minus")
else if (op == "dig_minus")
return "-";

return " " + op + " ";
Expand Down
57 changes: 42 additions & 15 deletions cJass2Lua/cJassNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,14 @@ namespace cJass
if (mainTopNodeType == Node::Type::Function)
return name;

auto classNode = mainNode->Top();

if (mainNode->Ptr<Method>()->IsStatic())
{
if (mainNode->Ptr<Method>()->GetReplaceThisName() != "" && mainNode->Ptr<Method>()->GetReplaceThisName() == name)
return "this";
return name;

auto classNode = mainNode->Top();
}

if (classNode->Ptr<Class>()->HasNonstaticMember(name))
{
Expand Down Expand Up @@ -870,27 +874,27 @@ namespace cJass
if (_extra == "++")
{
if (_AssignUnary)
_out << _opText << " = " << _opText << " + 1";
_out << GetFullIdName() << " = " << GetFullIdName() << " + 1";
else
{
_out << "(" << _opText << " + 1" << ")";
_out << "(" << GetFullIdName() << " + 1" << ")";
}
}
else if (_extra == "--")
{
if (_AssignUnary)
_out << _opText << " = " << _opText << " - 1";
_out << GetFullIdName() << " = " << GetFullIdName() << " - 1";
else
_out << "(" << _opText << " - 1" << ")";
_out << "(" << GetFullIdName() << " - 1" << ")";
}

}
else
{
if (!_isString)
_out << _opText << " = " << _opText << std::string({ ' ', _extra[0], ' ' });
_out << GetFullIdName() << " = " << GetFullIdName() << std::string({ ' ', _extra[0], ' ' });
else
_out << _opText << " = " << _opText << " .. ";
_out << GetFullIdName() << " = " << GetFullIdName() << " .. ";
for (auto& node : _subnodes)
node->ToLua();
}
Expand All @@ -914,7 +918,7 @@ namespace cJass
break;

case OpType::Call:
_out << _opText << "(";
_out << GetFullIdName() << "(";
nodeCount = CountSubnodes();
if (nodeCount > 0)
{
Expand Down Expand Up @@ -1228,6 +1232,7 @@ namespace cJass
case 'v':
_otype = OpType::VarInitExpression;
_isWrapper = true;
_opText = strings[1];
break;

case 'a':
Expand Down Expand Up @@ -1508,11 +1513,19 @@ namespace cJass
, _type("")
, _vars({})
, _arrSizes({})
, _doNotPrint(false)
{
}

void LocalDeclaration::ToLua()
{
if (_doNotPrint)
{
if (_root)
_root->TryToCreatePrintNotify();
return;
}

_out << OutputInterface::nl;
PrintTabs();
_out << "local ";
Expand Down Expand Up @@ -1611,6 +1624,11 @@ namespace cJass
return (_type != "");
}

void LocalDeclaration::DoNotPrint()
{
_doNotPrint = true;
}

Node* LocalDeclaration::AddVariable(const std::string& name, const std::string& arrSize)
{
appLog(Debug) << "Adding local variable" << name;
Expand All @@ -1621,7 +1639,7 @@ namespace cJass
_vars.push_back(name);
_arrSizes.push_back(arrSize);
auto node = Node::Produce(Node::Type::OperationObject, this, _out);
node->InitData({ "v" });
node->InitData({ "v", name });
AddSubnode(node);
return node->Ptr();
}
Expand Down Expand Up @@ -1791,9 +1809,7 @@ namespace cJass
_out << OutputInterface::nl;

PrintTabs();
_out << "function this:allocate() return this end" << OutputInterface::nl;
PrintTabs();
_out << "function this:deallocate() end" << OutputInterface::nl;
_out << "function this.deallocate() this = nil end" << OutputInterface::nl;

if (CountSubnodes() > 1)
{
Expand Down Expand Up @@ -2007,10 +2023,11 @@ namespace cJass
{
_out << OutputInterface::nl;
PrintTabs(1);
_out << "function " << className << ":" << _name << "(";
_out << "function " << "this." << _name << "(";
}
else
_out << OutputInterface::nl << "function " << "this:" << _name << "(";
_out << "function " << className << ":" << _name << "(";


if (_args.size() == 0)
_out << ")";
Expand Down Expand Up @@ -2056,6 +2073,16 @@ namespace cJass
return _name;
}

void Method::SetThisname(const std::string& name)
{
_thisName = name;
}

std::string Method::GetReplaceThisName() const
{
return _thisName;
}

void Method::InitData(const std::vector<std::string>& strings)
{
size_t size = strings.size();
Expand Down
5 changes: 5 additions & 0 deletions cJass2Lua/cJassNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ namespace cJass
{
private:
std::string _name;
std::string _thisName;
std::string _returnType;
std::vector<std::string> _args;
std::vector<std::string> _strIds;
Expand All @@ -235,6 +236,8 @@ namespace cJass
bool HasStringId(const std::string& strId) const override;
bool IsStatic() const;
std::string GetMethodName() const;
void SetThisname(const std::string& name);
std::string GetReplaceThisName() const;
};


Expand Down Expand Up @@ -351,6 +354,7 @@ namespace cJass
std::string _type;
std::vector<std::string> _vars;
std::vector<std::string> _arrSizes;
bool _doNotPrint;

protected:
virtual void CountAllNodes() override;
Expand All @@ -362,5 +366,6 @@ namespace cJass
size_t GetVarCount() const;
Node* AddVariable(const std::string& name, const std::string& arrSize = 0);
bool hasType() const;
void DoNotPrint();
};
}
33 changes: 32 additions & 1 deletion cJass2Lua/cJassParser2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ namespace cJass
if (word == "native")
return word_t::native;

if (word == "allocate")
return word_t::allocate;

if (word == "do")
return word_t::Do;

Expand Down Expand Up @@ -544,6 +547,7 @@ namespace cJass
bool doNotPutNewLine = false;
bool goBackToComment = false;
bool goBackToExpClose = false;
Node* nd, *nd2;

if (clearStatic)
{
Expand Down Expand Up @@ -1379,6 +1383,9 @@ namespace cJass
getReadyToEndStatement = false;
}

if (_word == "destroy")
_word = _word;

bool endOfLogic = false;
switch (wtype)
{
Expand All @@ -1405,6 +1412,30 @@ namespace cJass
break;
}

case word_t::allocate:
nd = _activeNode;
while (nd != nullptr && nd->GetType() != Node::Type::LocalDeclaration)
nd = nd->Top();

if (nd->GetType() == Node::Type::LocalDeclaration)
{
nd->Ptr<LocalDeclaration>()->DoNotPrint();
nd = nd->Top();
while (nd != nullptr && nd->GetType() != Node::Type::Method)
nd = nd->Top();

if (nd->GetType() == Node::Type::Method)
{
nd2 = _activeNode;
while (nd2 != nullptr && nd2->GetType() != Node::Type::OperationObject && nd2->Ptr<OperationObject>()->GetOpType() != OperationObject::OpType::VarInitExpression)
nd2 = nd2->Top();

if (nd2->GetType() == Node::Type::OperationObject && nd2->Ptr<OperationObject>()->GetOpType() == OperationObject::OpType::VarInitExpression)
nd->Ptr<Method>()->SetThisname(nd2->GetText());
}
}

break;
case word_t::This:
case word_t::id:
if (isLocalTop && !varAddedStack.top())
Expand Down Expand Up @@ -1526,7 +1557,7 @@ namespace cJass
}

std::string oper = _word;
if (wtype_prev == word_t::op || _activeNode->CountSubnodes() == 0 && oper == "-")
if (oper == "-" && (wtype_prev == word_t::op || _activeNode->CountSubnodes() == 0))
oper = "dig_minus";
_addNode(Node::Type::OperationObject, { "o", oper });
}
Expand Down
5 changes: 3 additions & 2 deletions cJass2Lua/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#pragma warning(disable : 4834)


#define APP_VER "1.11"
#define APP_VER "1.12"
#ifdef _WIN64
#define APP_BUILD "x64"
#else
Expand Down Expand Up @@ -108,7 +108,8 @@ namespace cJass
priv,
pub,
Static,
evaluate
evaluate,
allocate
};

enum class ParseSpecialSubject
Expand Down
Binary file modified cJass2Lua/resource.rc
Binary file not shown.

0 comments on commit 85c7d68

Please sign in to comment.