Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0061860
FIX: Undo caused crash when inserting newline at the end of text
Rasa0 Jun 14, 2018
5f2bf49
Text input cursor only visible when hovering over the text. Text only…
Rasa0 Jun 14, 2018
bec5eff
Added a save index, used in combination with the undo buffer, to mark…
Rasa0 Jun 14, 2018
337ea5d
Added a breakpoint bar, with actual breakpoints, instead of just line…
Rasa0 Jun 15, 2018
e4a6449
FIX: SetText not inserting a line when passed empty string
Rasa0 Jun 16, 2018
fc786b1
Merge branch 'srcMaster'
Rasa0 Jun 16, 2018
42d6cda
Added GetBreakpoints method
Rasa0 Jun 17, 2018
eca6275
Added a statement marker
Rasa0 Jun 20, 2018
1185d11
Setting statement marker now enures that the line is scrolled to if n…
Rasa0 Jun 25, 2018
5318637
Made scroll to line number general, and public
Rasa0 Jun 28, 2018
5ba51c3
Fix: text area held was a static variable
Rasa0 Jul 3, 2018
be0e232
Added MarkDirty method, that forces the file to be dirty untill saved
Rasa0 Jul 11, 2018
72c3b8e
breakpoint callback is now a std::function instead of a c-style funct…
Rasa0 Jul 20, 2018
673874c
Initial Lexer implementation
Rasa0 Jul 24, 2018
f874cac
Fixed some off by one errors.
Rasa0 Jul 24, 2018
508e0ea
Replaced "string start end" to "string" in lexer tokens
Rasa0 Jul 30, 2018
91681d5
Long comment and long strings are now lexed in a single go. Comments …
Rasa0 Jul 31, 2018
43b07b8
Fixed bug causing dot tokens to never be produced
Rasa0 Aug 4, 2018
7fd88e7
Initial Parser implementation
Rasa0 Aug 4, 2018
845094b
Fixed. Forlist tried to consume colon instead of comma. Function stat…
Rasa0 Aug 4, 2018
cbc3730
Fix. Non local functions are now added as a variable too
Rasa0 Aug 5, 2018
78f7d0b
Ignoring *.TMP files in repository
Rasa0 Aug 5, 2018
201e113
Fixed. For loop control variables are now propperly scoped
Rasa0 Aug 5, 2018
2d97705
LuaBlock and LuaFunctionState now properly close all locals scope
Rasa0 Aug 6, 2018
8a096a6
Initial coloring and inspection of lua variables in the editor
Rasa0 Aug 6, 2018
ee3a8d2
Added callbacks for requesting the value of globals, locals and upval…
Rasa0 Aug 6, 2018
a350599
Update README.md
Rasa0 Aug 6, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
*.exe
*.out
*.app
*.TMP
62 changes: 62 additions & 0 deletions LuaBlock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

#include <vector>
#include "LuaVariable.h"
#include "LuaVariableLocation.h"

class LuaBlock
{
bool _breakable;
// All local declarations in the blocks scope
std::vector<LuaLocal> _localDefinitions;
// All locals in the blocks scope
std::vector<LuaLocal> _locals;

public:

LuaBlock(bool breakable)
: _breakable(breakable)
{}

bool IsBreakable() const
{
return _breakable;
}

void AddLocalDefinition(const std::string& name, const LuaVariableLocation& loc, size_t count)
{
_localDefinitions.emplace_back(name, loc, loc._line, count);
_locals.emplace_back(name, loc, loc._line, count);
}

void AddLocal(LuaLocal&& local)
{
_locals.emplace_back(local);
}

size_t GetLocalCount(const std::string& name) const
{
return std::count_if(_localDefinitions.begin(), _localDefinitions.end(),
[name](const LuaLocal& local) { return local._name == name; }
);
}

LuaVariable GetLocal(const std::string& name) const
{
const auto found = std::find_if(_localDefinitions.rbegin(), _localDefinitions.rend(),
[name](const LuaLocal& local) { return local._name == name; }
);

if(found != _localDefinitions.rend())
return *found;

return LuaVariable();
}

const std::vector<LuaLocal>& Close(size_t lastLineDefined)
{
for (auto& local : _locals)
local.SetLastLineDefined(lastLineDefined);
return _locals;
}
};
175 changes: 175 additions & 0 deletions LuaFunctionState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#pragma once

#include <vector>
#include "LuaBlock.h"
#include "LuaVariable.h"
#include <numeric>
#include <any>

class LuaFunctionState
{
size_t _lineDefined;
LuaFunctionState* _prevFunc = nullptr;
bool _isVararg;

// All local declarations in the functions scope
std::vector<LuaLocal> _localDefinitions;
// All locals in the functions scope
std::vector<LuaLocal> _locals;
// All usages of upvalues for this function
std::vector<LuaUpvalue> _upvalues;
// Names of all used upvalues
std::vector<std::string> _upvalueNames;

// Active blocks
std::vector<LuaBlock> _blocks;

// All references to a variable, in the function scope.
std::vector<LuaVariable> _variables;

public:
LuaFunctionState(size_t lineDefined, LuaFunctionState* prevFunc = nullptr)
: _lineDefined(lineDefined), _prevFunc(prevFunc), _isVararg(false)
{}

void SetVararg()
{
_isVararg = true;
}

bool IsVararg() const
{
return _isVararg;
}

bool IsBreakable() const
{
return std::any_of(_blocks.rbegin(), _blocks.rend(),
[](const LuaBlock& block){ return block.IsBreakable(); }
);
}

void OpenBlock(bool breakable)
{
_blocks.emplace_back(breakable);
}

void CloseBlock(size_t lastLineDefined)
{
for (const auto& local : (*_blocks.rbegin()).Close(lastLineDefined))
_variables.emplace_back(local);
_blocks.pop_back();
}

const std::vector<LuaVariable>& Close(size_t endLine)
{
// Close scope of all locals and add them to the variables
for (auto& local : _locals)
{
local.SetLastLineDefined(endLine);
_variables.emplace_back(local);
}
_locals.clear();

// Close scope of all upvalues and add them to the variables
for (auto& upvalue : _upvalues)
{
upvalue.SetLastLineDefined(endLine);
_variables.emplace_back(upvalue);
}
_upvalues.clear();

return _variables;
}

void AddLocal(const std::string& name, const LuaVariableLocation& loc)
{
// TODO Verify std function
auto count = std::count_if(_localDefinitions.begin(), _localDefinitions.end(),
[name](LuaLocal& local) { return local._name == name; }
);

if(_blocks.empty())
{
_localDefinitions.emplace_back(name, loc, loc._line, count);
_locals.emplace_back(name, loc, loc._line, count);
}
else
{
// TODO Verify these std functions
count = std::accumulate(_blocks.begin(), _blocks.end(), count,
[name](size_t acc, const LuaBlock& block) { return acc + block.GetLocalCount(name); }
);

(*_blocks.rbegin()).AddLocalDefinition(name, loc, count);
}
}

// Returns the closest value with this name.
// Turns locals outside the function into upvalues
LuaVariable Variable(const std::string& name, const LuaVariableLocation& loc, bool currentFunction = true)
{
// Check if local in a block
for(auto iter = _blocks.rbegin(); iter != _blocks.rend(); ++iter)
{
auto foundLocal = iter->GetLocal(name);
if(std::holds_alternative<LuaLocal>(foundLocal))
{
auto local = std::get<LuaLocal>(foundLocal);
if (currentFunction)
{
_blocks.rbegin()->AddLocal(LuaLocal(name, loc, local._lineDefined, local._count));
}

return local;
}
}

// Check if local in function scope
const auto foundLocal = std::find_if(_localDefinitions.rbegin(), _localDefinitions.rend(),
[name](const LuaLocal& local) { return local._name == name; }
);
if (foundLocal != _localDefinitions.rend())
{
if (currentFunction)
_locals.emplace_back(LuaLocal(name, loc, foundLocal->_lineDefined, foundLocal->_count));
return *foundLocal;
}

// Check if upvalue
const auto foundUpvalue = std::find_if(_upvalueNames.rbegin(), _upvalueNames.rend(),
[name](const std::string& upvalueName) { return upvalueName == name; }
);
if (foundUpvalue != _upvalueNames.rend())
{
auto upvalue = LuaUpvalue(name, loc, _lineDefined);
if (currentFunction)
_variables.emplace_back(upvalue);
return upvalue;
}

// Not a variable in this scope. Look at previous function
if(_prevFunc)
{
const auto res = _prevFunc->Variable(name, loc, false);

// Upvalue or local in previous function?
if(std::holds_alternative<LuaUpvalue>(res) || std::holds_alternative<LuaLocal>(res))
{
// Make into upvalue for this function
_upvalueNames.push_back(name);

auto upvalue = LuaUpvalue(name, loc, _lineDefined);
if (currentFunction)
_upvalues.push_back(upvalue);
return upvalue;
}
}

// Was a global
auto global = LuaGlobal(name, loc);
if(currentFunction)
_variables.emplace_back(global);
return global;
}
};
Loading