* Default function arguments.

This commit is contained in:
Eelco Dolstra 2003-11-05 15:34:12 +00:00
parent 0690c1c9c0
commit 80bb477cc4
3 changed files with 17 additions and 9 deletions

View file

@ -27,10 +27,13 @@ static Expr substArgs(Expr body, ATermList formals, Expr arg)
/* Get the formal arguments. */ /* Get the formal arguments. */
while (!ATisEmpty(formals)) { while (!ATisEmpty(formals)) {
ATerm t = ATgetFirst(formals); ATerm t = ATgetFirst(formals);
char * s; Expr name, def;
if (!ATmatch(t, "<str>", &s)) debug(printTerm(t));
abort(); /* can't happen */ if (ATmatch(t, "NoDefFormal(<term>)", &name))
subs.set(t, undefined); subs.set(name, undefined);
else if (ATmatch(t, "DefFormal(<term>, <term>)", &name, &def))
subs.set(name, def);
else abort(); /* can't happen */
formals = ATgetNext(formals); formals = ATgetNext(formals);
} }
@ -44,7 +47,7 @@ static Expr substArgs(Expr body, ATermList formals, Expr arg)
Expr key = ATgetFirst(keys); Expr key = ATgetFirst(keys);
Expr cur = subs.get(key); Expr cur = subs.get(key);
if (!cur) if (!cur)
throw badTerm(format("argument `%1%' not declared") throw badTerm(format("function has no formal argument `%1%'")
% aterm2String(key), arg); % aterm2String(key), arg);
subs.set(key, args.get(key)); subs.set(key, args.get(key));
} }

View file

@ -171,7 +171,10 @@ Expr substitute(const ATermMap & subs, Expr e)
ATermMap subs2(subs); ATermMap subs2(subs);
ATermList fs = formals; ATermList fs = formals;
while (!ATisEmpty(fs)) { while (!ATisEmpty(fs)) {
if (!ATmatch(ATgetFirst(fs), "<str>", &s)) abort(); Expr def;
if (!ATmatch(ATgetFirst(fs), "NoDefFormal(<str>)", &s) &&
!ATmatch(ATgetFirst(fs), "DefFormal(<str>, <term>)", &s))
abort();
subs2.remove(s); subs2.remove(s);
fs = ATgetNext(fs); fs = ATgetNext(fs);
} }

View file

@ -17,7 +17,7 @@ imports Fix-Exprs Fix-Layout
module Fix-Exprs module Fix-Exprs
imports Fix-Lexicals URI imports Fix-Lexicals URI
exports exports
sorts Expr Bind sorts Expr Bind Formal
context-free syntax context-free syntax
Id -> Expr {cons("Var")} Id -> Expr {cons("Var")}
@ -34,7 +34,9 @@ exports
Expr Expr -> Expr {cons("Call"), left} 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")} "rec" "{" Binds "}" -> Expr {cons("Rec")}
"let" "{" Binds "}" -> Expr {cons("LetRec")} "let" "{" Binds "}" -> Expr {cons("LetRec")}
@ -62,7 +64,7 @@ exports
Expr "." Id -> Expr Expr "." Id -> Expr
> Expr ExprList -> ExprList > Expr ExprList -> ExprList
> Expr Expr -> Expr > Expr Expr -> Expr
> "{" {Id ","}* "}" ":" Expr -> Expr > "{" {Formal ","}* "}" ":" Expr -> Expr
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%