Skip to content

Commit

Permalink
Merge pull request #56 from ComposerFreak/performance-dev
Browse files Browse the repository at this point in the history
Added better variable error/warning handling during compilation.
  • Loading branch information
Rusketh authored Apr 5, 2024
2 parents 0ecd8d6 + 6654e29 commit 20d890c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 28 deletions.
53 changes: 40 additions & 13 deletions lua/expression3/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function COMPILER.Initialize(this, instance, files)
this.__scope.server = true;
this.__scope.client = true;

this.__variables = { };
this.__defined = {};

this.__constructors = {};
Expand Down Expand Up @@ -179,6 +180,7 @@ function COMPILER._Run(this)
result.enviroment = this.__enviroment;
result.directives = this.__directives;
result.hashTable = this.__hashtable;
result.variables = this.__variables;
--result.rootInstruction = this.__root;

result.build = function()
Expand Down Expand Up @@ -382,21 +384,25 @@ function COMPILER.GetOption(this, option, nonDeep)
end
end

function COMPILER.SetVariable(this, name, class, scope, prefix, global)
function COMPILER.SetVariable(this, token, name, class, scope, prefix, global)
if (not scope) then
scope = this.__scopeID;
end

local var = {};
var.name = name;
var.class = class;
var.scope = scope;
var.prefix = prefix;
var.global = global;
local var = {
used = false,
name = name,
token = token,
class = class,
scope = scope,
prefix = prefix,
global = global
};

if not name then debug.Trace(); end

this.__scopeData[scope].memory[name] = var;
this.__variables[#this.__variables + 1] = var;

return class, scope, var;
end
Expand Down Expand Up @@ -484,14 +490,14 @@ function COMPILER.AssignVariable(this, token, declaired, varName, class, scope,
local c, s, var = this:GetVariable(varName, scope, declaired);

if (declaired) then
if (c and c == class) then
this:Throw(token, "Unable to declare variable %s, Variable already exists.", varName);
elseif (c and class ~= "") then
if (c and (c == class or class ~= "")) then
if (var.inport) then this:Throw(token, "Unable to declare variable %s, Variable already exists as Wired input.", varName); end
if (var.outport) then this:Throw(token, "Unable to declare variable %s, Variable already exists as Wired output.", varName); end
if (var.synced) then this:Throw(token, "Unable to declare variable %s, Variable already exists as Synced variable.", varName); end
if (var.global) then this:Throw(token, "Unable to declare variable %s, Variable already exists in global space.", varName); end
this:Throw(token, "Unable to declare variable %s, Variable already exists.", varName);
--elseif (c and class ~= c) then
-- this:Throw(token, "Unable to Initialize variable %s, %s expected got %s.", varName, name(c), name(class));
else
return this:SetVariable(varName, class, scope, prefix, global);
return this:SetVariable(token, varName, class, scope, prefix, global);
end
else
if (not c) then
Expand Down Expand Up @@ -1138,6 +1144,8 @@ function COMPILER.Compile_INC(this, inst, token, data)
this:Throw(token, "Unable to assign variable %s, Variable doesn't exist.", data.variable);
end

info.used = true;

if class ~= "n" then
if data.first then
this:Throw(token, "No such operator ++%s.", name(class));
Expand Down Expand Up @@ -1167,6 +1175,8 @@ function COMPILER.Compile_IND(this, inst, token, data)
this:Throw(token, "Unable to assign variable %s, Variable doesn't exist.", data.variable);
end

info.used = true;

if class ~= "n" then
if data.first then
this:Throw(token, "No such operator --%s.", name(class));
Expand Down Expand Up @@ -1199,7 +1209,13 @@ function COMPILER.Compile_ASS(this, inst, token, data)

for i = 1, tVars do
local var = vars[i].data;

local class, scope, info = this:GetVariable(var);

if (not class) then
this:Throw(token, "Unable to assign variable %s, Variable doesn't exist.", var);
end

classes[var] = class;

if info then
Expand Down Expand Up @@ -1271,6 +1287,7 @@ function COMPILER.Compile_ASS(this, inst, token, data)

for i = 1, tVars do
local var = vars[i].data;

local class, scope, info = this:GetVariable(var);

if (data.class == "f") then
Expand Down Expand Up @@ -2541,6 +2558,8 @@ function COMPILER.Compile_DELTA(this, inst, token, data)
this:Throw(token, "Variable %s does not exist.", var);
end

info.used = true;

if (not info.global) then
this:Throw(token, "Delta operator ($) can not be used on none global variable %s.", var);
end
Expand Down Expand Up @@ -2576,6 +2595,8 @@ function COMPILER.Compile_CHANGED(this, inst, token, data)
this:Throw(token, "Variable %s does not exist.", var);
end

info.used = true;

if (not info.global) then
this:Throw(token, "Changed operator (~) can not be used on none global variable %s.", var);
end
Expand Down Expand Up @@ -2784,6 +2805,8 @@ function COMPILER.Compile_VAR(this, inst, token, data)

local c, s, var = this:GetVariable(data.variable);

var.used = true;

if (var) then
if (var.attribute) then
inst.buffer[#inst.buffer + 1] = "this.";
Expand Down Expand Up @@ -3644,6 +3667,8 @@ function COMPILER.Compile_CALL(this, inst, token, data)
c, s, info = this:GetVariable(expr.data.variable);
-- The var instruction will have already validated this variable.

info.used = true;

if (info and info.signature) then
resultClass = info.resultClass;
resultCount = info.resultCount;
Expand Down Expand Up @@ -4206,6 +4231,8 @@ function COMPILER.Compile_INCLUDE(this, inst, token, file_path)
local Toker = EXPR_TOKENIZER.New();

Toker:Initialize("EXPADV", script);

Toker.__file = file_path;

local ok, res = Toker:Run();

Expand Down
51 changes: 36 additions & 15 deletions lua/expression3/editor/derma/ide.lua
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ function PANEL:DoValidate( Goto, Code, Native )
elseif (status) then
self.btnValidate:SetColor( Color( 50, 255, 50 ) );
self.btnValidate:SetText( "Validation sucessful" );
self:PostValidation(instance);
elseif (instance.state == "internal") then
self:OnValidateError( false, "Internal error (see console)." )
self:Warning(3, Color(255, 255, 255), "Internal error: ", instance.msg)
Expand All @@ -923,6 +924,29 @@ function PANEL:DoValidate( Goto, Code, Native )
self.validator.start();
end

function PANEL:LineCharCallback(line, char, file)
local func;
local location = "";

if (line and char) then
func = function()
timer.Simple(0.1, function()
local inst = self.pnlTabHolder:GetActiveTab( ):GetPanel( );
inst:RequestFocus();
inst:SetCaret( Vector2( line, char ) );
end);
end;

location = string.format("at line %i char %i", line, char);
end

if file then
location = string.format("%s in %s.txt", location, file);
end

return location, func;
end

function PANEL:OnValidateError( Goto, Thrown )
local line, char = 0, 0;
local message = "";
Expand All @@ -947,21 +971,7 @@ function PANEL:OnValidateError( Goto, Thrown )
local location = "";

if ( Goto ) then
func = function()
timer.Simple(0.1, function()
local inst = self.pnlTabHolder:GetActiveTab( ):GetPanel( );
inst:RequestFocus();
inst:SetCaret( Vector2( line, char ) );
end);
end;

if line and char then
location = string.format("at line %i char %i", line, char);
end

if File then
location = string.format("%s in %s.txt", location, Thrown.file);
end
location, func = self:LineCharCallback(line, char, File);
end

if func then func(); end
Expand All @@ -973,6 +983,17 @@ function PANEL:OnValidateError( Goto, Thrown )
self.validator = nil;
end

function PANEL:PostValidation(instance)
for i = 1, #instance.variables do
local var = instance.variables[i];

if (not var.used) then
local location, func = self:LineCharCallback(var.token.line or 0, var.token.char or 0, var.token.file);
self:PrintLine({image = "fugue/exclamation-circle.png", size = 16}, Color(194, 229, 17), "Note", Color(255, 255, 255), ": ", "variable ", var.name, " is not used ", " ", { func, location } );
end
end
end

/*---------------------------------------------------------------------------
Voice stuff
---------------------------------------------------------------------------*/
Expand Down
1 change: 1 addition & 0 deletions lua/expression3/tokenizer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ function TOKENIZER.CreateToken(this, type, name, data, origonal, char, line)
tkn.name = name;
tkn.data = data;

tkn.file = this.__file;
tkn.start = this.__dataStart + this.__offset;
tkn.stop = this.__dataEnd + this.__offset;
tkn.pos = this.__pos;
Expand Down

0 comments on commit 20d890c

Please sign in to comment.