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

Autounravel #505

Draft
wants to merge 48 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
b327863
Merge branch 'rename' into autounravel
charlesstaats Jun 2, 2024
8c6c5ca
Merge branch 'master' into autounravel
charlesstaats Jul 9, 2024
b9a6e6b
Hardcoded autounravel structs named TestAuUn.
charlesstaats Jul 10, 2024
c37fca3
Preliminary autounravel.
charlesstaats Jul 12, 2024
47a6c8e
WIP: autounravel progress
charlesstaats Aug 22, 2024
42ce117
Merge branch 'master' into autounravel
charlesstaats Sep 24, 2024
28725aa
Passing tests for autounravel parametrized type.
charlesstaats Sep 24, 2024
40b2682
Autounravel fields of type added via unravel.
charlesstaats Sep 27, 2024
5dbc739
Delete some commented-out code.
charlesstaats Sep 27, 2024
e810b68
Apply autounravel modifier to unravel declarations.
charlesstaats Sep 27, 2024
38c5644
Test autounravel function.
charlesstaats Sep 27, 2024
65c90bf
Error if autounravel used for type or at top-level.
charlesstaats Sep 29, 2024
5e27fa3
Do not autounravel a type whenever a variable of that type is declared.
charlesstaats Sep 29, 2024
6a08059
First draft autounravel doc.
charlesstaats Oct 1, 2024
825eeda
Merge branch 'master' into autounravel.
johncbowman Oct 1, 2024
4d95214
Remove duplicate error messages.
johncbowman Oct 1, 2024
05cc3ff
Fix behavior when multiple fields autounraveled.
charlesstaats Oct 13, 2024
c407cd8
Add test for builtin operators.
charlesstaats Oct 25, 2024
699627e
Autounravel builtin operators; control autounravel order.
charlesstaats Oct 25, 2024
955a0b4
Error if one autounravel field cannot shadows another.
charlesstaats Nov 8, 2024
d3e10f0
Make alias shadowable to fix error.
charlesstaats Nov 8, 2024
16d05bc
Move functions from .h to .cc file.
charlesstaats Nov 10, 2024
94bac8b
Merge branch 'master' into autounravel
charlesstaats Nov 10, 2024
0d5b222
Test compiler errors for autounravel.
charlesstaats Nov 10, 2024
9e3ff98
Test double-shadowing when only first shadow is allowed.
charlesstaats Nov 10, 2024
2dd68b5
Add comment documenting possibly unneeded line.
charlesstaats Nov 21, 2024
75eccac
Use autounravel for operator init.
charlesstaats Nov 26, 2024
d679251
Autounravel modes; operator init hack.
charlesstaats Nov 26, 2024
02788a6
Add another test case for builtinOps.
charlesstaats Nov 26, 2024
38f9527
Less hacky handling of operator init.
charlesstaats Nov 28, 2024
bbd0a7e
Use C++17 for static analysis tools in vscode.
charlesstaats Nov 30, 2024
ae7db8a
Remove the empty addFunctionOps functions.
charlesstaats Dec 4, 2024
5ab2077
minor code improvements
charlesstaats Dec 5, 2024
fe4aae5
Remove .vscode/c_cpp_properties.json
charlesstaats Dec 14, 2024
f99492a
Merge branch 'master' into autounravel.
johncbowman Dec 14, 2024
a526dfc
Merge branch 'master' into autounravel.
charlesstaats Dec 14, 2024
bbd3243
Update documentation.
johncbowman Dec 14, 2024
0c3610e
Simplify documentation of autounravel.
johncbowman Dec 14, 2024
4601cc9
Merge branch 'autounravel' of https://github.com/vectorgraphics/asymp…
charlesstaats Dec 14, 2024
d94cb96
Make rational.asy use autounravel.
charlesstaats Dec 14, 2024
b4b9d6e
Improve documentation of autounravel.
johncbowman Dec 14, 2024
77ba9cd
Improve documentation of autounravel.
johncbowman Dec 14, 2024
772e8cb
More uniform code in addRecordOps function.
charlesstaats Dec 15, 2024
2735aad
Change applyAutoUnravel bool parameter to enum for readability.
charlesstaats Dec 15, 2024
1b157a7
Merge branch 'autounravel' of https://github.com/vectorgraphics/asymp…
charlesstaats Dec 15, 2024
23e1770
Fix issue with autounravel overrides of builtin record ops.
charlesstaats Dec 25, 2024
0c38493
Merge branch 'master' into autounravel.
johncbowman Dec 30, 2024
1eeafba
Merge branch 'master' into autounravel.
johncbowman Jan 2, 2025
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
8 changes: 4 additions & 4 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: Always
AfterControlStatement: false
AfterEnum: true
AfterFunction: true
AfterNamespace: true
Expand All @@ -35,8 +35,8 @@ BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BinPackArguments: false
ColumnLimit: 120
BinPackArguments: true
ColumnLimit: 80
CompactNamespaces: false
ContinuationIndentWidth: 8
IndentCaseLabels: true
Expand All @@ -49,7 +49,7 @@ ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Left
ReferenceAlignment: Left
ReflowComments: false
ReflowComments: true
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
Expand Down
165 changes: 83 additions & 82 deletions base/rational.asy
Original file line number Diff line number Diff line change
Expand Up @@ -41,107 +41,108 @@ struct rational {
this.q=q;
if(reduce) reduce();
}
}

rational operator cast(int p)
{
return rational(p,false);
}
autounravel rational operator cast(int p)
{
return rational(p,false);
}

rational[] operator cast(int[] a)
{
return sequence(new rational(int i) {return a[i];},a.length);
}
autounravel rational[] operator cast(int[] a)
{
return sequence(new rational(int i) {return a[i];},a.length);
}

rational[][] operator cast(int[][] a)
{
return sequence(new rational[](int i) {return a[i];},a.length);
}
autounravel rational[][] operator cast(int[][] a)
{
return sequence(new rational[](int i) {return a[i];},a.length);
}

real operator ecast(rational r)
{
return r.p/r.q;
}
real operator ecast(rational r)
{
return r.p/r.q;
}

rational operator ecast(real x)
{
int sign=x >= 0.0 ? 1 : -1;
x=abs(x);
int a=floor(x); int b=1;
int c=a+1; int d=1;

while(true) {
int e=a+c;
int f=b+d;
if(f > maxDenominator) break;
if(e/f == x)
return rational(sign*e,f);
else {
if(e/f < x) {
a=e;
b=f;
} else {
c=e;
d=f;
autounravel rational operator ecast(real x)
{
int sign=x >= 0.0 ? 1 : -1;
x=abs(x);
int a=floor(x); int b=1;
int c=a+1; int d=1;

while(true) {
int e=a+c;
int f=b+d;
if(f > maxDenominator) break;
if(e/f == x)
return rational(sign*e,f);
else {
if(e/f < x) {
a=e;
b=f;
} else {
c=e;
d=f;
}
}
}
return abs(a/b-x) < abs(c/d-x) ? rational(sign*a,b) : rational(sign*c,d);
}
return abs(a/b-x) < abs(c/d-x) ? rational(sign*a,b) : rational(sign*c,d);
}

rational operator -(rational r)
{
return rational(-r.p,r.q,false);
}
autounravel rational operator -(rational r)
{
return rational(-r.p,r.q,false);
}

rational operator +(rational r, rational s)
{
return rational(r.p*s.q+s.p*r.q,r.q*s.q);
}
autounravel rational operator +(rational r, rational s)
{
return rational(r.p*s.q+s.p*r.q,r.q*s.q);
}

rational operator -(rational r, rational s)
{
return rational(r.p*s.q-s.p*r.q,r.q*s.q);
}
autounravel rational operator -(rational r, rational s)
{
return rational(r.p*s.q-s.p*r.q,r.q*s.q);
}

rational operator *(rational r, rational s)
{
return rational(r.p*s.p,r.q*s.q);
}
autounravel rational operator *(rational r, rational s)
{
return rational(r.p*s.p,r.q*s.q);
}

rational operator /(rational r, rational s)
{
return rational(r.p*s.q,r.q*s.p);
}
autounravel rational operator /(rational r, rational s)
{
return rational(r.p*s.q,r.q*s.p);
}

bool operator ==(rational r, rational s)
{
return r.p == s.p && r.q == s.q;
}
autounravel bool operator ==(rational r, rational s)
{
return r.p == s.p && r.q == s.q;
}

bool operator !=(rational r, rational s)
{
return r.p != s.p || r.q != s.q;
}
autounravel bool operator !=(rational r, rational s)
{
return r.p != s.p || r.q != s.q;
}

bool operator <(rational r, rational s)
{
return r.p*s.q-s.p*r.q < 0;
}
autounravel bool operator <(rational r, rational s)
{
return r.p*s.q-s.p*r.q < 0;
}

bool operator >(rational r, rational s)
{
return r.p*s.q-s.p*r.q > 0;
}
autounravel bool operator >(rational r, rational s)
{
return r.p*s.q-s.p*r.q > 0;
}

bool operator <=(rational r, rational s)
{
return r.p*s.q-s.p*r.q <= 0;
}
autounravel bool operator <=(rational r, rational s)
{
return r.p*s.q-s.p*r.q <= 0;
}

autounravel bool operator >=(rational r, rational s)
{
return r.p*s.q-s.p*r.q >= 0;
}

bool operator >=(rational r, rational s)
{
return r.p*s.q-s.p*r.q >= 0;
}

bool[] operator ==(rational[] r, rational s)
Expand Down
41 changes: 23 additions & 18 deletions builtin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function *functionFromFormals(ty *result,
return fun;
}

void addFunc(venv &ve, access *a, ty *result, symbol id,
varEntry *addFunc(venv &ve, access *a, ty *result, symbol id,
formal f1=noformal, formal f2=noformal, formal f3=noformal,
formal f4=noformal, formal f5=noformal, formal f6=noformal,
formal f7=noformal, formal f8=noformal, formal f9=noformal,
Expand All @@ -143,10 +143,11 @@ void addFunc(venv &ve, access *a, ty *result, symbol id,
varEntry *ent = new varEntry(fun, a, 0, nullPos);

ve.enter(id, ent);
return ent;
}

// Add a function with one or more default arguments.
void addFunc(venv &ve, bltin f, ty *result, symbol name,
varEntry *addFunc(venv &ve, bltin f, ty *result, symbol name,
formal f1, formal f2, formal f3, formal f4, formal f5, formal f6,
formal f7, formal f8, formal f9, formal fA, formal fB, formal fC,
formal fD, formal fE, formal fF, formal fG, formal fH, formal fI)
Expand All @@ -171,8 +172,8 @@ void addFunc(venv &ve, bltin f, ty *result, symbol name,
#endif

access *a = new bltinAccess(f);
addFunc(ve,a,result,name,f1,f2,f3,f4,f5,f6,f7,f8,f9,
fA,fB,fC,fD,fE,fF,fG,fH,fI);
return addFunc(ve,a,result,name,f1,f2,f3,f4,f5,f6,f7,f8,f9,
fA,fB,fC,fD,fE,fF,fG,fH,fI);
}

void addOpenFunc(venv &ve, bltin f, ty *result, symbol name)
Expand Down Expand Up @@ -659,22 +660,26 @@ void addArrayOps(venv &ve, types::array *t)
}
}

void addRecordOps(venv &ve, record *r)
void addRecordOps(record* r)
{
addFunc(ve, run::boolMemEq, primBoolean(), SYM(alias), formal(r, SYM(a)),
formal(r, SYM(b)));
addFunc(ve, run::boolMemEq, primBoolean(), SYM_EQ, formal(r, SYM(a)),
formal(r, SYM(b)));
addFunc(ve, run::boolMemNeq, primBoolean(), SYM_NEQ, formal(r, SYM(a)),
formal(r, SYM(b)));
assert(r);
trans::venv &ve = r->e.ve;
auto addOp= [&ve](vm::bltin f, ty* result, symbol name, auto&&... formals) {
varEntry* fVar=
addFunc(ve, f, result, name, std::forward<formal>(formals)...);
ve.registerAutoUnravel(name, fVar, AutounravelPriority::OFFER);
};
// alias
addOp(run::boolMemEq, primBoolean(), SYM(alias), formal(r, SYM(a)),
formal(r, SYM(b)));
// operator==
addOp(run::boolMemEq, primBoolean(), SYM_EQ, formal(r, SYM(a)),
formal(r, SYM(b)));
// operator!=
addOp(run::boolMemNeq, primBoolean(), SYM_NEQ, formal(r, SYM(a)),
formal(r, SYM(b)));
}

void addFunctionOps(venv &ve, function *f)
{
// No function ops.
}


void addOperators(venv &ve)
{
addSimpleOperator(ve,binaryOp<string,plus>,primString(),SYM_PLUS);
Expand Down Expand Up @@ -755,7 +760,7 @@ dummyRecord *createDummyRecord(venv &ve, symbol name)
dummyRecord *r=new dummyRecord(name);
vm::vmFrame *f = make_dummyframe(name);
addConstant(ve, f, r, name);
addRecordOps(ve, r);
addRecordOps(r);
return r;
}

Expand Down
23 changes: 11 additions & 12 deletions builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@ void base_venv(venv &);
extern const types::formal noformal;

// Add a function with one or more default arguments.
void addFunc(venv &ve, vm::bltin f, types::ty *result, symbol name,
types::formal f1=noformal, types::formal f2=noformal,
types::formal f3=noformal, types::formal f4=noformal,
types::formal f5=noformal, types::formal f6=noformal,
types::formal f7=noformal, types::formal f8=noformal,
types::formal f9=noformal, types::formal fA=noformal,
types::formal fB=noformal, types::formal fC=noformal,
types::formal fD=noformal, types::formal fE=noformal,
types::formal fF=noformal, types::formal fG=noformal,
types::formal fH=noformal, types::formal fI=noformal);
varEntry *addFunc(venv &ve, vm::bltin f, types::ty *result, symbol name,
types::formal f1=noformal, types::formal f2=noformal,
types::formal f3=noformal, types::formal f4=noformal,
types::formal f5=noformal, types::formal f6=noformal,
types::formal f7=noformal, types::formal f8=noformal,
types::formal f9=noformal, types::formal fA=noformal,
types::formal fB=noformal, types::formal fC=noformal,
types::formal fD=noformal, types::formal fE=noformal,
types::formal fF=noformal, types::formal fG=noformal,
types::formal fH=noformal, types::formal fI=noformal);

// Adds standard functions for a newly added types.
void addArrayOps(venv &ve, types::array *t);
void addRecordOps(venv &ve, types::record *r);
void addFunctionOps(venv &ve, types::function *f);
void addRecordOps(types::record *r);

#ifdef HAVE_LIBGSL
types::record *getGSLModule();
Expand Down
2 changes: 2 additions & 0 deletions camp.l
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ include {adjust(); return INCLUDE; }
quote {adjust(); return QUOTE; }
static {adjust(); makemod(trans::EXPLICIT_STATIC);
return MODIFIER; }
autounravel {adjust(); makemod(trans::AUTOUNRAVEL);
return MODIFIER; }
public {adjust(); makeperm(trans::PUBLIC); return PERM; }
private {adjust(); makeperm(trans::PRIVATE); return PERM; }
restricted {adjust(); makeperm(trans::RESTRICTED); return PERM; }
Expand Down
13 changes: 12 additions & 1 deletion coder.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,13 @@ class coder {
assert(s != DEFAULT_STATIC && s != DEFAULT_DYNAMIC);

/* Non-default static overrules. */
if (sord != EXPLICIT_STATIC)
if (s == AUTOUNRAVEL || sord == AUTOUNRAVEL) {
sord = AUTOUNRAVEL;
} else if (s == EXPLICIT_STATIC || sord == EXPLICIT_STATIC) {
sord = EXPLICIT_STATIC;
} else {
sord = s;
}

sord_stack.push(sord);
}
Expand All @@ -161,6 +166,7 @@ class coder {
switch(sord) {
case DEFAULT_STATIC:
case EXPLICIT_STATIC:
case AUTOUNRAVEL:
return true;
case DEFAULT_DYNAMIC:
case EXPLICIT_DYNAMIC:
Expand All @@ -171,6 +177,11 @@ class coder {
}
}

bool isAutoUnravel()
{
return sord == AUTOUNRAVEL;
}


/* Remove a modifier. */
void popModifier()
Expand Down
Loading