polserver/pol-core/bscript/compiler/file/SourceFileIdentifier.cpp
turleypol 7090c64840
Short Circuit Optimizer (#797)
* first working short curcuit for && and or
creates jumps after each expressions to skip the following

* fixed valuestack when short circuit jmp does not jump

* use specialized instructions for short circuit && and ||

1: lhs
2: logical jump if false/true goto 4 <- if jmp do logical convert
3: rhs
4: logical convert
5: rest

logical convert is needed since "normal" && || operations convert isTrue
to BLong

added BObjectRef BObjectImp set specialization to remove noise

* fixed converted objimp when jump on false values

* first version of short circuit warning
should be moved to analyzer
added whitelist of module functions which have no sideeffect to reduce
the number of warnings

* moved warning visitor to analyzer and added it as extra compile step
fixed sourceline print and cache the content

* missing include, unused member

* included the correct header

* ecompile.cfg to activate and warn
ecompile cmdline arg to activate it
run all tests also with it active
fixed that only the most right side statement was checked

* ecompile cmdline

* increase ecompile version
cleanup

* compilation error

* missing header

* allow -S- to deactivate shortcircuit like the other params do

* extended whitelist functions

* revert fileformat version increase
fixed escript test cmake

* use the correct arg

* escript testoutput can now be different if shortcircuit is active

* docs

* additional test

* addressed comments
2025-07-28 21:48:34 +02:00

37 lines
1 KiB
C++

#include "SourceFileIdentifier.h"
#include "clib/filecont.h"
#include "clib/strutil.h"
namespace Pol::Bscript::Compiler
{
SourceFileIdentifier::SourceFileIdentifier( unsigned index, std::string pathname )
: index( index ), pathname( std::move( pathname ) ), _lines()
{
}
const std::vector<std::string>& SourceFileIdentifier::getLines() const
{
if ( !_lines.empty() )
return _lines;
Clib::FileContents cont{ pathname.c_str(), true };
std::string content{ cont.contents() };
Clib::sanitizeUnicodeWithIso( &content );
std::string::size_type pos = 0;
std::string::size_type prev = 0;
while ( ( pos = content.find_first_of( "\n\r", prev ) ) != std::string::npos )
{
auto strpos = pos;
if ( content[pos] == '\r' && pos + 1 < content.size() && content[pos + 1] == '\n' )
++pos; // windows lineendings..
_lines.push_back( content.substr( prev, strpos - prev ) );
prev = pos + 1;
}
if ( prev < content.length() )
_lines.push_back( content.substr( prev ) );
return _lines;
}
} // namespace Pol::Bscript::Compiler