Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic arrays of non-managed struct and classic arrays of managed struct #2106

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 10 additions & 10 deletions Compiler/script2/cc_compiledscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void AGS::ccCompiledScript::ReplaceLabels()
{
std::vector<CodeLoc> RemainingLabels;

for (size_t idx = 0; idx < Labels.size(); idx++)
for (size_t idx = 0u; idx < Labels.size(); idx++)
{
CodeLoc const loc = Labels[idx];
if (loc >= codesize)
Expand Down Expand Up @@ -182,12 +182,12 @@ int AGS::ccCompiledScript::AddFixup(CodeLoc const where, FixupType const ftype)
return numfixups++;
}

AGS::CodeLoc AGS::ccCompiledScript::AddNewFunction(std::string const &func_name, size_t const num_of_parameters)
AGS::CodeLoc AGS::ccCompiledScript::AddNewFunction(std::string const &func_name, size_t const params_count)
{
FuncProps fp;
fp.Name = func_name;
fp.CodeOffs = codesize;
fp.NumOfParams = num_of_parameters;
fp.ParamsCount = params_count;
Functions.push_back(fp);
return codesize;
}
Expand Down Expand Up @@ -215,12 +215,12 @@ int AGS::ccCompiledScript::FindOrAddImport(std::string const &import_name)
return (ImportIdx[import_name] = numimports++);
}

int AGS::ccCompiledScript::AddExport(std::string const &name, CodeLoc const location, size_t const num_of_arguments)
int AGS::ccCompiledScript::AddExport(std::string const &name, CodeLoc const location, size_t const arguments_count)
{
bool const is_function = (INT_MAX != num_of_arguments);
bool const is_function = (INT_MAX != arguments_count);
// Exported functions get the number of parameters appended
std::string const export_name =
is_function ? name + "$" + std::to_string(num_of_arguments) : name;
is_function ? name + "$" + std::to_string(arguments_count) : name;

if (location >= 0x00ffffff)
{
Expand Down Expand Up @@ -345,17 +345,17 @@ void AGS::Snippet::Paste(ccCompiledScript &scrip)
{
CodeLoc const code_start = scrip.codesize;
// Don't generate additional LINUM directives, that would throw off the fixups and labels
for (size_t idx = 0; idx < Code.size(); idx++)
for (size_t idx = 0u; idx < Code.size(); idx++)
scrip.WriteCode(Code[idx]);
for (size_t idx = 0; idx < Fixups.size(); idx++)
for (size_t idx = 0u; idx < Fixups.size(); idx++)
scrip.AddFixup(code_start + Fixups[idx], FixupTypes[idx]);
for (size_t idx = 0; idx < Labels.size(); idx++)
for (size_t idx = 0u; idx < Labels.size(); idx++)
scrip.Labels.push_back(code_start + Labels[idx]);
}

bool AGS::Snippet::IsEmpty()
{
for (size_t idx = 0; idx < Code.size(); idx++)
for (size_t idx = 0u; idx < Code.size(); idx++)
{
if (SCMD_LINENUM != Code[idx])
return false; // found some content
Expand Down
6 changes: 3 additions & 3 deletions Compiler/script2/cc_compiledscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ccCompiledScript : public ccScript {
struct FuncProps
{
std::string Name;
size_t NumOfParams;
size_t ParamsCount;
CodeLoc CodeOffs;
};
std::vector<FuncProps> Functions = {};
Expand Down Expand Up @@ -73,7 +73,7 @@ class ccCompiledScript : public ccScript {
inline void FixupPrevious(FixupType ftype) { AddFixup(codesize - 1, ftype); };

// Add a function named 'func_name' to the functions repository
CodeLoc AddNewFunction(std::string const &func_name, size_t num_of_parameters);
CodeLoc AddNewFunction(std::string const &func_name, size_t params_count);

inline bool IsImport(std::string const &name) const { return 0 < ImportIdx.count(name); }

Expand All @@ -83,7 +83,7 @@ class ccCompiledScript : public ccScript {
// Add an exported entity to the export repository;
// it has type vartype, resides at location; if it is a function
// Note: This function returns -1 on error
int AddExport(std::string const &name, CodeLoc location, size_t num_of_arguments = INT_MAX);
int AddExport(std::string const &name, CodeLoc location, size_t arguments_count = INT_MAX);

// Start a new section of the code.
ErrorType StartNewSection(std::string const &name);
Expand Down
36 changes: 34 additions & 2 deletions Compiler/script2/cc_internallist.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <vector>
#include <string>
#include <algorithm>
#include <limits>
#include <stdlib.h>
#include "cc_internallist.h"
Expand Down Expand Up @@ -91,6 +89,40 @@ AGS::Symbol AGS::SrcList::GetNext()
return p;
}

void AGS::SrcList::SkipTo(SymbolList const &stoplist)
{
int delimeter_nesting_depth = 0;
for (; !ReachedEOF(); GetNext())
{
// Note that the scanner/tokenizer has already verified
// that all opening symbols get closed and
// that we don't have (...] or similar in the input
Symbol const next_sym = PeekNext();
switch (next_sym)
{
case kKW_OpenBrace:
case kKW_OpenBracket:
case kKW_OpenParenthesis:
++delimeter_nesting_depth;
continue;

case kKW_CloseBrace:
case kKW_CloseBracket:
case kKW_CloseParenthesis:
if (--delimeter_nesting_depth < 0)
return;
continue;

}
if (0 < delimeter_nesting_depth)
continue;

for (auto it = stoplist.begin(); it != stoplist.end(); ++it)
if (next_sym == *it)
return;
}
}

void AGS::SrcList::EatFirstSymbol()
{
if (_len < 1)
Expand Down
32 changes: 26 additions & 6 deletions Compiler/script2/cc_internallist.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
#include <map>
#include <vector>
#include <algorithm>

#include "cs_parser_common.h"
#include "cc_symboltable.h"

namespace AGS
{
// A helper class that contains post-parsed list of sections,
// A helper class that contains post-parsed list of sections
// and a offset-to-section map, useful for finding a symbol's location.
class SectionList
{
// list of sections
std::vector<std::string> _sections;
// starting line offset to section index
std::map<size_t, size_t> _off2sec;

public:
SectionList() = default;
SectionList(const std::vector<std::string> &sections,
Expand Down Expand Up @@ -93,8 +94,8 @@ class SrcList
std::vector<Symbol> &_script;
LineHandler &_lineHandler;
size_t _offset;
size_t _len; // note: _len has the length relative to [0], not relative to [_offset]
size_t &_cursor; // note: _cursor has the position relative to [0], not relative to [_offset]
size_t _len; // Length relative to [0], not relative to [_offset]
size_t &_cursor; // Position relative to [0], not relative to [_offset]

public:
SrcList(std::vector<Symbol> &script, LineHandler &line_handler, size_t &cursor);
Expand All @@ -103,24 +104,43 @@ class SrcList
// but the resulting SrcList starts at [offset] and has the length len.
// No symbols are actually copied.
// NOTE: If you move the cursor of the new list then the cursor of the original list
// moves by the same amount because the cursor variable is shared. This is intentional.
// moves correspondingly because the cursor variable is shared. This is intentional.
SrcList(SrcList const &src_list, size_t offset, size_t len);

inline size_t GetCursor() const { return _cursor - _offset; }
inline void SetCursor(size_t idx) { _cursor = idx + _offset; }

inline size_t Length() const { return _len; };
inline bool ReachedEOF() const { return _cursor - _offset >= _len || _cursor >= _script.size(); }
// Whether the cursor has gone _beyond_ the start when going backwards
inline bool ReachedStartOF() const { return _cursor < _offset; }

// Set the cursor to the start of the list
inline void StartRead() { SetCursor(0); }
// Read the next symbol
Symbol GetNext();
// Look at the symbol that will be read next, but don't read it yet
inline Symbol PeekNext() const { return ReachedEOF() ? kEOF : _script[_cursor]; }
// Look at the symbol that comes next when going backwards when reading
inline Symbol PeekPrev() const { return ReachedStartOF() ? kEOF : _script[GetCursor() - 1u]; }
// Move the cursor back by 1 space.
inline void BackUp() { size_t c = GetCursor(); SetCursor((c > 0u) ? c - 1u : 0u); }

// Skim through the list, ignoring delimited content completely. Stop in the following cases:
// . A symbol in 'stoplist' is encountered
// . A closing symbol is encountered that hasn't been opened.
// Don't consume the symbol that stops the scan.
void SkipTo(SymbolList const &stoplist);
// Skim through the list, ignoring delimited content completely. Stop in the following cases:
// . The symbol 'stopsym' is encountered.
// . A closing symbol is encountered that hasn't been opened.
// Don't consume the symbol that stops the scan.
inline void SkipTo(Symbol stopsym) { SkipTo(SymbolList{ stopsym }); }
// Skim through the list, ignoring delimited content completely.
// Stop whem a closing symbol is encountered that hasn't been opened.
// Don't consume that symbol.
inline void SkipToCloser(void) { SkipTo(SymbolList{}); }

// Get symbol at idx. Moves the cursor, too.
// Note: Can't assign through this operator, this is intentional.
Symbol operator[](size_t idx) { SetCursor(idx); return GetNext(); }
Expand All @@ -133,7 +153,7 @@ class SrcList
void EatLastSymbol();

// Note that when this is a sub-list of an original list, the line numbers and sections
// will still be relative to the original list. This is intentional.
// will still be relative to the original list. This is intentional
// (When the user gets an error, they want to know the "real" line where the error is,
// not a line reference that is relative to an excerpt of their program.)
inline size_t GetLinenoAt(size_t pos) const { return _lineHandler.GetLinenoAt(pos + _offset); }
Expand Down
Loading