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

Fix a number of bugs in SyntaxTree and SyntaxTreeCode #5165

Merged
merged 5 commits into from
Oct 27, 2022
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
2 changes: 1 addition & 1 deletion src/code.c
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ void CodeCharExpr (
Expr litr; /* literal expression, result */

/* allocate the character expression */
litr = NewExpr( EXPR_CHAR, sizeof(UChar) );
litr = NewExpr(EXPR_CHAR, sizeof(UInt));
WRITE_EXPR(litr, 0, chr);

/* push the literal expression */
Expand Down
9 changes: 8 additions & 1 deletion src/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,14 @@ EXPORT_INLINE Int TNUM_EXPR(Expr expr)
*/
#define CONST_ADDR_EXPR(expr) CONST_ADDR_STAT(expr)

#define READ_EXPR(expr, idx) (CONST_ADDR_EXPR(expr)[idx])
EXPORT_INLINE Stat READ_EXPR(Expr expr, Int idx)
{
GAP_ASSERT(!IS_REF_LVAR(expr));
GAP_ASSERT(!IS_INTEXPR(expr));
GAP_ASSERT(idx >= 0);
GAP_ASSERT(idx < SIZE_EXPR(expr) / sizeof(Expr));
return CONST_ADDR_EXPR(expr)[idx];
}

/****************************************************************************
**
Expand Down
101 changes: 61 additions & 40 deletions src/syntaxtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ static Obj SyntaxTreeDefaultCompiler(Obj result, Expr expr)
int i;
UInt1 tnum;
CompilerT comp;
BOOL didvariadic = 0;
Int offset = 0;

// TODO: GAP_ASSERT tnum range
tnum = TNUM_EXPR(expr);
Expand All @@ -194,29 +196,36 @@ static Obj SyntaxTreeDefaultCompiler(Obj result, Expr expr)
Obj compiled;

if (comp.args[i].argcomp) {
Expr subexpr = READ_EXPR(expr, i);
Expr subexpr = READ_EXPR(expr, i + offset);
compiled = comp.args[i].argcomp(subexpr);
}
else {
// special case: the last argument may have zero as decompiler,
// meaning that all remaining slots of the statement should be
// decompiled into a single list
const UInt offset = comp.arity - 1;
GAP_ASSERT(i == offset);

// compile the complete rest into one statement
// special case: one argument may have zero as decompiler,
// which expresses that this slot is "variadic", and
// is decompiled into a single list
fingolfin marked this conversation as resolved.
Show resolved Hide resolved
GAP_ASSERT(!didvariadic);
didvariadic = 1;
// Avoid warning about used variable when not in debug mode
(void)didvariadic;

// compile the variadic part into a single list
const UInt nr = SIZE_EXPR(expr) / sizeof(expr);
compiled = NEW_PLIST(T_PLIST, nr - offset);
for (; i < nr; i++) {
Expr subexpr = READ_EXPR(expr, i);
const UInt varlength = nr - (comp.arity - 1);
compiled = NEW_PLIST(T_PLIST, nr - (comp.arity - 1));
for (; offset < varlength; offset++) {
Expr subexpr = READ_EXPR(expr, i + offset);
// handle 0 to properly deal with EXPR_LIST
Obj obj = subexpr ? SyntaxTreeCompiler(subexpr) : 0;
PushPlist(compiled, obj);
}
// offset gets set one bigger final time around loop
offset--;
}

AssPRec(result, rnam, compiled);
}

GAP_ASSERT(i + offset == SIZE_EXPR(expr) / sizeof(expr));
return result;
}

Expand All @@ -242,38 +251,50 @@ static Expr SyntaxTreeDefaultCoder(Obj node)
UInt slots = comp.arity;
UInt arity = comp.arity;

UInt isvararg = comp.arity > 0 && comp.args[comp.arity - 1].argcomp == 0;
Obj vararglist;
Int varargloc = -1;
for (UInt i = 0; i < comp.arity; ++i) {
if (comp.args[i].argcomp == 0) {
GAP_ASSERT(varargloc == -1);
varargloc = i;
}
}

if (isvararg) {
arity--;
vararglist = ElmRecST(tnum, node, comp.args[arity].argname);
slots = arity + LEN_LIST(vararglist);
Obj vararglist = 0;

if (varargloc != -1) {
vararglist = ElmRecST(tnum, node, comp.args[varargloc].argname);
slots = arity - 1 + LEN_LIST(vararglist);
}

// reserve space for the statement or expressions
Expr expr = NewStatOrExpr(tnum, slots * sizeof(Expr), 0);

UInt i;
Int i;
UInt offset = 0;

for (i = 0; i < arity; i++) {
Obj subast = ElmRecST(tnum, node, comp.args[i].argname);
WRITE_EXPR(expr, i, comp.args[i].argcode(subast));
}

if (isvararg) {
for (i = arity; i < slots; i++) {
Obj elem = ELM0_LIST(vararglist, i - arity + 1);
// Deal with empty entries in list expressions
if (elem == 0) {
WRITE_EXPR(expr, i, 0);
}
else if (comp.args[arity].isStat) {
WRITE_EXPR(expr, i, SyntaxTreeDefaultStatCoder(elem));
}
else {
WRITE_EXPR(expr, i, SyntaxTreeDefaultExprCoder(elem));
if (i == varargloc) {
for (; offset < LEN_LIST(vararglist); offset++) {
Obj elem = ELM0_LIST(vararglist, offset + 1);
// Deal with empty entries in list expressions
if (elem == 0) {
WRITE_EXPR(expr, i + offset, 0);
}
else if (comp.args[i].isStat) {
WRITE_EXPR(expr, i + offset,
SyntaxTreeDefaultStatCoder(elem));
}
else {
WRITE_EXPR(expr, i + offset,
SyntaxTreeDefaultExprCoder(elem));
}
}
// offset gets set one bigger final time around loop
offset--;
}
else {
Obj subast = ElmRecST(tnum, node, comp.args[i].argname);
WRITE_EXPR(expr, i + offset, comp.args[i].argcode(subast));
}
}

Expand Down Expand Up @@ -564,7 +585,7 @@ static Expr SyntaxTreeCodeChar(Obj node)
RequirePlainRec("SyntaxTreeCodeChar", node);
Obj chr = ElmRecST(EXPR_CHAR, node, "value");
Char currchar = CHAR_VALUE(chr);
Expr lit = NewStatOrExpr(EXPR_CHAR, sizeof(UChar), 0);
Expr lit = NewStatOrExpr(EXPR_CHAR, sizeof(UInt), 0);
WRITE_EXPR(lit, 0, currchar);
return lit;
}
Expand Down Expand Up @@ -834,7 +855,7 @@ static const CompilerT Compilers[] = {
COMPILER(EXPR_RANGE, SyntaxTreeRangeExpr, SyntaxTreeCodeRangeExpr),
COMPILER(EXPR_STRING, SyntaxTreeEvalCompiler, SyntaxTreeCodeValue),
COMPILER(EXPR_REC, SyntaxTreeRecExpr, SyntaxTreeCodeRecExpr),
COMPILER_(EXPR_REC_TILDE),
COMPILER(EXPR_REC_TILDE, SyntaxTreeRecExpr, SyntaxTreeCodeRecExpr),

COMPILER(
EXPR_FLOAT_EAGER, SyntaxTreeFloatEager, SyntaxTreeCodeFloatEager),
Expand All @@ -859,13 +880,13 @@ static const CompilerT Compilers[] = {
COMPILER_(EXPR_ELMS_LIST, ARG_EXPR_("list"), ARG_EXPR_("poss")),
COMPILER_(EXPR_ELM_LIST_LEV,
ARG_EXPR_("lists"),
ARG_EXPR_("pos"),
ARG_EXPR_("level")),
ARGS_EXPR("pos"),
ARG_EXPR("level", ObjInt_UInt, UInt_ObjInt)),
COMPILER_(EXPR_ELMS_LIST_LEV,
ARG_EXPR_("lists"),
ARG_EXPR_("poss"),
ARG_EXPR_("level")),
COMPILER_(EXPR_ISB_LIST, ARG_EXPR_("list"), ARG_EXPR_("pos")),
ARG_EXPR("level", ObjInt_UInt, UInt_ObjInt)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, that makes sense. Nice catch!

COMPILER_(EXPR_ISB_LIST, ARG_EXPR_("list"), ARGS_EXPR("pos")),
COMPILER_(EXPR_ELM_REC_NAME,
ARG_EXPR_("record"),
ARG_EXPR("name", SyntaxTreeRNam, RNamObj)),
Expand Down
Loading