Skip to content
Merged
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
68 changes: 43 additions & 25 deletions interpreters/bas/bas_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Expand Down Expand Up @@ -101,16 +101,17 @@ struct Xref
struct Pc line;
struct LineNumber *next;
} *lines;
struct Xref *l, *r;
struct Xref *l;
struct Xref *r;
};

/****************************************************************************
* Private Functions
****************************************************************************/

static void Xref_add(struct Xref **root,
int (*cmp) (const void *, const void *), const void *key,
struct Pc *line)
int (*cmp) (const void *, const void *),
const void *key, struct Pc *line)
{
int res;
struct LineNumber **tail;
Expand Down Expand Up @@ -153,7 +154,9 @@ static void Xref_destroy(struct Xref *root)
{
if (root)
{
struct LineNumber *cur, *next, *tail;
struct LineNumber *cur;
struct LineNumber *next;
struct LineNumber *tail;

Xref_destroy(root->l);
Xref_destroy(root->r);
Expand All @@ -176,7 +179,8 @@ static void Xref_print(struct Xref *root,
{
if (root)
{
const struct LineNumber *cur, *tail;
const struct LineNumber *cur;
const struct LineNumber *tail;

Xref_print(root->l, print, p, chn);
print(root->key, p, chn);
Expand Down Expand Up @@ -231,7 +235,7 @@ static void printName(const void *k, struct Program *p, int chn)
FS_putChars(chn, (const char *)k);
if (len < 8)
{
FS_putChars(chn, " " + len);
FS_putChars(chn, &(" "[len]));
}
}

Expand Down Expand Up @@ -379,7 +383,8 @@ struct Pc *Program_goLine(struct Program *this, long int line, struct Pc *pc)

for (i = 0; i < this->size; ++i)
{
if (this->code[i]->type == T_INTEGER && line == this->code[i]->u.integer)
if (this->code[i]->type == T_INTEGER &&
line == this->code[i]->u.integer)
{
pc->line = i;
pc->token = this->code[i] + 1;
Expand All @@ -390,13 +395,15 @@ struct Pc *Program_goLine(struct Program *this, long int line, struct Pc *pc)
return (struct Pc *)0;
}

struct Pc *Program_fromLine(struct Program *this, long int line, struct Pc *pc)
struct Pc *Program_fromLine(struct Program *this, long int line,
struct Pc *pc)
{
int i;

for (i = 0; i < this->size; ++i)
{
if (this->code[i]->type == T_INTEGER && this->code[i]->u.integer >= line)
if (this->code[i]->type == T_INTEGER &&
this->code[i]->u.integer >= line)
{
pc->line = i;
pc->token = this->code[i] + 1;
Expand All @@ -413,7 +420,8 @@ struct Pc *Program_toLine(struct Program *this, long int line, struct Pc *pc)

for (i = this->size - 1; i >= 0; --i)
{
if (this->code[i]->type == T_INTEGER && this->code[i]->u.integer <= line)
if (this->code[i]->type == T_INTEGER &&
this->code[i]->u.integer <= line)
{
pc->line = i;
pc->token = this->code[i] + 1;
Expand All @@ -437,7 +445,8 @@ int Program_scopeCheck(struct Program *this, struct Pc *pc, struct Pc *fn)
continue;
}

if (pc->line == scope->begin.line && pc->token <= scope->begin.token)
if (pc->line == scope->begin.line &&
pc->token <= scope->begin.token)
{
continue;
}
Expand Down Expand Up @@ -485,7 +494,8 @@ int Program_scopeCheck(struct Program *this, struct Pc *pc, struct Pc *fn)
return 0;
}

struct Pc *Program_dataLine(struct Program *this, long int line, struct Pc *pc)
struct Pc *Program_dataLine(struct Program *this, long int line,
struct Pc *pc)
{
if ((pc = Program_goLine(this, line, pc)) == (struct Pc *)0)
{
Expand All @@ -507,7 +517,8 @@ struct Pc *Program_dataLine(struct Program *this, long int line, struct Pc *pc)
return pc;
}

struct Pc *Program_imageLine(struct Program *this, long int line, struct Pc *pc)
struct Pc *Program_imageLine(struct Program *this, long int line,
struct Pc *pc)
{
if ((pc = Program_goLine(this, line, pc)) == (struct Pc *)0)
{
Expand Down Expand Up @@ -641,8 +652,8 @@ void Program_PCtoError(struct Program *this, struct Pc *pc, struct Value *v)
{
String_appendPrintf(&s, _(" in line %ld at:\n"),
Program_lineNumber(this, pc));
Token_toString(this->code[pc->line], (struct Token *)0, &s, (int *)0,
-1);
Token_toString(this->code[pc->line], (struct Token *)0, &s,
(int *)0, -1);
Token_toString(this->code[pc->line], pc->token, &s, (int *)0, -1);
String_appendPrintf(&s, "^\n");
}
Expand All @@ -668,7 +679,8 @@ void Program_PCtoError(struct Program *this, struct Pc *pc, struct Value *v)
String_destroy(&s);
}

struct Value *Program_merge(struct Program *this, int dev, struct Value *value)
struct Value *Program_merge(struct Program *this, int dev,
struct Value *value)
{
struct String s;
int l;
Expand All @@ -686,7 +698,8 @@ struct Value *Program_merge(struct Program *this, int dev, struct Value *value)
line = Token_newCode(s.character);
if (line->type == T_INTEGER && line->u.integer > 0)
{
Program_store(this, line, this->numbered ? line->u.integer : 0);
Program_store(this, line,
this->numbered ? line->u.integer : 0);
}
else if (line->type == T_UNNUMBERED)
{
Expand Down Expand Up @@ -732,7 +745,8 @@ int Program_lineNumberWidth(struct Program *this)
}

struct Value *Program_list(struct Program *this, int dev, int watchIntr,
struct Pc *from, struct Pc *to, struct Value *value)
struct Pc *from, struct Pc *to,
struct Value *value)
{
int i, w;
int indent = 0;
Expand Down Expand Up @@ -777,7 +791,7 @@ struct Value *Program_analyse(struct Program *this, struct Pc *pc,
++pc->token;
}

for (;;)
for (; ; )
{
if (pc->token->type == T_GOTO || pc->token->type == T_RESUME ||
pc->token->type == T_RETURN || pc->token->type == T_END ||
Expand Down Expand Up @@ -839,7 +853,7 @@ void Program_renum(struct Program *this, int first, int inc)

for (i = 0; i < this->size; ++i)
{
for (token = this->code[i]; token->type != T_EOL;)
for (token = this->code[i]; token->type != T_EOL; )
{
if (token->type == T_GOTO || token->type == T_GOSUB ||
token->type == T_RESTORE || token->type == T_RESUME ||
Expand Down Expand Up @@ -872,6 +886,7 @@ void Program_renum(struct Program *this, int first, int inc)
}
}
}

for (i = 0; i < this->size; ++i)
{
assert(this->code[i]->type == T_INTEGER ||
Expand Down Expand Up @@ -1032,7 +1047,8 @@ void Program_xref(struct Program *this, int chn)

for (pc.line = 0; pc.line < this->size; ++pc.line)
{
for (pc.token = this->code[pc.line]; pc.token->type != T_EOL; ++pc.token)
for (pc.token = this->code[pc.line]; pc.token->type != T_EOL;
++pc.token)
{
switch (pc.token->type)
{
Expand All @@ -1055,16 +1071,18 @@ void Program_xref(struct Program *this, int chn)
{
case GLOBALVAR:
{
Xref_add(&var, cmpName, &pc.token->u.identifier->name,
&pc);
Xref_add(&var, cmpName,
&pc.token->u.identifier->name, &pc);
break;
}

case USERFUNCTION:
{
Xref_add(&func, cmpName,
&pc.token->u.identifier->name, &pc);
break;
}

default:
break;
}
Expand Down