Skip to content

Commit

Permalink
misc(zscript): internal label errors now prevent compile
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyV99 authored and connorjclark committed Dec 23, 2023
1 parent e1b10e2 commit 1b0d63b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/parser/BuildVisitors.h
Expand Up @@ -5,6 +5,7 @@
#include "ByteCode.h"
#include "ZScript.h"
#include "Scope.h"
#include "base/headers.h"
#include <algorithm>
#include <vector>
#include <set>
Expand Down Expand Up @@ -185,17 +186,24 @@ namespace ZScript
class SetLabels : public ArgumentVisitor
{
public:
bool err = false;
void caseLabel(LabelArgument &host, void *param)
{
std::map<int32_t, int32_t> *labels = (std::map<int32_t, int32_t> *)param;
int32_t lineno = (*labels)[host.getID()];

if(lineno==0)
host.setLineNo(check(host.getID(), *(map<int32_t, int32_t> *)param, &err));
}
static int check(int lbl, map<int32_t, int32_t> const& labels, bool* error = nullptr)
{
auto it = labels.find(lbl);

if (it == labels.end())
{
zconsole_error("Internal error: couldn't find function label %d", host.getID());
zconsole_error("Internal error: couldn't find label %d", lbl);
if (error)
*error = true;
return 0;
}
host.setLineNo(lineno);

return it->second;
}
};
class MergeLabels : public ArgumentVisitor
Expand Down
1 change: 1 addition & 0 deletions src/parser/Compiler.h
Expand Up @@ -194,6 +194,7 @@ namespace ZScript
std::pair<std::string,std::string> parts, Scope* scope);

static int32_t const recursionLimit = 30;
static bool assemble_err;
private:
static bool valid_include(ASTImportDecl& decl, std::string& ret_fname);
static std::string prepareFilename(std::string const& filename);
Expand Down
6 changes: 6 additions & 0 deletions src/parser/ScriptParser.cpp
Expand Up @@ -82,6 +82,7 @@ void ScriptParser::initialize()
fid = 0;
gid = 1;
lid = 0;
assemble_err = false;
CompileError::initialize();
CompileOption::initialize();
includePaths.clear();
Expand Down Expand Up @@ -159,7 +160,9 @@ static unique_ptr<ScriptsData> _compile_helper(string const& filename)
zconsole_info("%s", "Pass 6: Assembling");
zconsole_idle();

ScriptParser::assemble_err = false;
ScriptParser::assemble(id.get());
if (ScriptParser::assemble_err) return nullptr;

unique_ptr<ScriptsData> result(new ScriptsData(program));
if(!ignore_asserts && casserts.size()) return nullptr;
Expand Down Expand Up @@ -205,6 +208,7 @@ int32_t ScriptParser::vid = 0;
int32_t ScriptParser::fid = 0;
int32_t ScriptParser::gid = 1;
int32_t ScriptParser::lid = 0;
bool ScriptParser::assemble_err = false;

string ScriptParser::prepareFilename(string const& filename)
{
Expand Down Expand Up @@ -1013,6 +1017,8 @@ vector<shared_ptr<Opcode>> ScriptParser::assembleOne(
// Now fill in those labels
SetLabels setlabel;
setlabel.execute(rval, &linenos);
if (setlabel.err)
assemble_err = true;

return rval;
}
Expand Down

0 comments on commit 1b0d63b

Please sign in to comment.