|
From: John M. S. <sk...@oz...> - 2003-06-08 16:36:40
|
There is a new tarball which provides some new features,
primarily a syntax macro processor, which includes
constant folding, and some syntax sugars.
1. SYNTAX MACRO PROCESSOR
--------------------------
Macro directives introduced are:
macro val x = expr;
macro var x = expr;
macro x = expr;
macro fun f(x) = expr;
macro proc g(x,y:fun, h:proc)
{
statements;
}
macro ident x = y;
macro ident x = new;
.... $atom ....
Macro definitions in standard
constructions such as modules and
functions respect scope.
Multiple definitions in the same
scope are allowed, a succeeding
definition hides a preceeding one.
The RHS of val, var, and assignment macros
are evaluated before definition.
The RHS in fun and proc macro definitions
are not evaluated on definition.
Expression macros (fun, val, var) are evaluated
by recursive descent with rescanning.
BEWARE INFINITE RECURSION!
Statement macros (proc) dump their
contents into the current scope on call,
including any macros defined (so that
the scope of the definition is not
respected by macros defined therein).
This is necessary to allow a macro to
package up other macros.
Expression macros only expand in expressions.
The ident macro is used to replace a name
consistently in a scope, and the replacement
is also done in the 'slot' for defining names:
macro ident x = y;
val x = 1; // defines y as 1
The 'new' keyword is replaced on expansion
by a fresh identifier synthesised by the compiler.
Ident macros expand recursively until some
identifier is repeated or there is no expansion
(and therefore expansion always terminates).
macro ident x = x;
is therefore allowed.
The $atom syntax prevents all macro expansion
in the atomic expression. Write $(expr) for
a general expression.
Parameters in all constructions are protected
from expansion, for example:
macro fun x = "Hello";
fun f(x:int):int = {
return x; // this x not replaced
}
There is no macro expansion in type expressions,
patterns, or regular expressions.
Macro replacement obeys the following general order:
1) evaluate the arguments
2) substitute the arguments into the body
3) evaluate the body
AS USUAL BEWARE THAT MACROS ARE DANGEROUS.
2. CONSTANT FOLDING
-------------------
Felix now folds some *plain* integer, string,
ustring, and boolean operations on literals.
It also folds the conditional expression:
if cond then e1 else e2 endif
so that if cond is folded to a constant,
the expression is folded to either e1 or e2,
depending on its value.
Constant folding occurs on all expressions
seen by the macro processor. The conditional
fold is particularly useful since it
guarrantees some ill-typed or unbindable
expressions can be compiled correctly.
For example:
macro fun loadLib (x) =
if windows then LoadLibrary(x)
elif unix then dlopen(x)
else cant_dynlink(x)
endif
;
macro unix = case 2 of 2; // true
macro windows = case 1 of 2; // false
Another important use of constant folding
is the implementation of terminating recursive
macro expressions. For example:
macro fun fact (x) =
if x>1 then x * fact (x-1)
else 1
endif
;
print (fact 10);
works correctly.
3. STRING SUGAR
---------------
Strings can now be composed by juxtaposition.
Any integer is converted to an ISO-10646 code
point represented by UTF-8 for strings
and UCS-4 (identity) for ustrings. For example:
print ("Hello" 32 myName);
is equivalent to
print ("Hello " + myName);
Literals are folded left to right,
however arbitrary expressions can be used,
the shortcut is detected during binding/type checking
and is type dependent (and special cased for
the types "string" and "ustring").
4. IMPROVED ERROR REPORTING
---------------------------
Some errors are now reported with the actual
erroneous program text listed in context, with the
erroneous text highlighted (by underlining
with **** at the moment).
--
John Max Skaller, mailto:sk...@oz...
snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.
voice:61-2-9660-0850
|