From 80bb477cc4ea5226ae760726730b3e09d21559de Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 5 Nov 2003 15:34:12 +0000 Subject: [PATCH] * Default function arguments. --- src/fix-ng/eval.cc | 13 ++++++++----- src/fix-ng/fix-expr.cc | 5 ++++- src/fix-ng/fix.sdf | 8 +++++--- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/fix-ng/eval.cc b/src/fix-ng/eval.cc index 90b7ff29f..770802f32 100644 --- a/src/fix-ng/eval.cc +++ b/src/fix-ng/eval.cc @@ -27,10 +27,13 @@ static Expr substArgs(Expr body, ATermList formals, Expr arg) /* Get the formal arguments. */ while (!ATisEmpty(formals)) { ATerm t = ATgetFirst(formals); - char * s; - if (!ATmatch(t, "", &s)) - abort(); /* can't happen */ - subs.set(t, undefined); + Expr name, def; + debug(printTerm(t)); + if (ATmatch(t, "NoDefFormal()", &name)) + subs.set(name, undefined); + else if (ATmatch(t, "DefFormal(, )", &name, &def)) + subs.set(name, def); + else abort(); /* can't happen */ formals = ATgetNext(formals); } @@ -44,7 +47,7 @@ static Expr substArgs(Expr body, ATermList formals, Expr arg) Expr key = ATgetFirst(keys); Expr cur = subs.get(key); if (!cur) - throw badTerm(format("argument `%1%' not declared") + throw badTerm(format("function has no formal argument `%1%'") % aterm2String(key), arg); subs.set(key, args.get(key)); } diff --git a/src/fix-ng/fix-expr.cc b/src/fix-ng/fix-expr.cc index 814e186b4..6e73b2934 100644 --- a/src/fix-ng/fix-expr.cc +++ b/src/fix-ng/fix-expr.cc @@ -171,7 +171,10 @@ Expr substitute(const ATermMap & subs, Expr e) ATermMap subs2(subs); ATermList fs = formals; while (!ATisEmpty(fs)) { - if (!ATmatch(ATgetFirst(fs), "", &s)) abort(); + Expr def; + if (!ATmatch(ATgetFirst(fs), "NoDefFormal()", &s) && + !ATmatch(ATgetFirst(fs), "DefFormal(, )", &s)) + abort(); subs2.remove(s); fs = ATgetNext(fs); } diff --git a/src/fix-ng/fix.sdf b/src/fix-ng/fix.sdf index 8e9f0fa72..9dc04d937 100644 --- a/src/fix-ng/fix.sdf +++ b/src/fix-ng/fix.sdf @@ -17,7 +17,7 @@ imports Fix-Exprs Fix-Layout module Fix-Exprs imports Fix-Lexicals URI exports - sorts Expr Bind + sorts Expr Bind Formal context-free syntax Id -> Expr {cons("Var")} @@ -34,7 +34,9 @@ exports Expr Expr -> Expr {cons("Call"), left} - "{" {Id ","}* "}" ":" Expr -> Expr {cons("Function"), right} + "{" {Formal ","}* "}" ":" Expr -> Expr {cons("Function"), right} + Id -> Formal {cons("NoDefFormal")} + Id "?" Expr -> Formal {cons("DefFormal")} "rec" "{" Binds "}" -> Expr {cons("Rec")} "let" "{" Binds "}" -> Expr {cons("LetRec")} @@ -62,7 +64,7 @@ exports Expr "." Id -> Expr > Expr ExprList -> ExprList > Expr Expr -> Expr - > "{" {Id ","}* "}" ":" Expr -> Expr + > "{" {Formal ","}* "}" ":" Expr -> Expr %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%