[m-dev.] for review: transformation of x `fun` y to fun(x,y)

Peter Ross petdr at cs.mu.OZ.AU
Fri Jul 9 17:42:25 AEST 1999


Hi,

This is for Fergus to review.

Pete.

PS. The bootcheck is still running, but I can't imagine that it will
    have too many problems since placing a ` in my source gave a syntax
    error before this change.

===================================================================


Estimated hours taken: 5

Allow the term (x `fun` y) to be transformed to fun(x,y).

library/lexer.m:
    Handle the backquote token.

library/parser.m:
    Do the term transformation as the program is parsed.

doc/reference_manual.texi:
    Document the new behaviour.


Index: lexer.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/lexer.m,v
retrieving revision 1.31
diff -u -r1.31 lexer.m
--- lexer.m	1998/11/05 06:48:19	1.31
+++ lexer.m	1999/07/09 07:22:33
@@ -313,6 +313,9 @@
 			lexer__get_slash(Token, Context)
 		; { Char = ('#') } ->
 			lexer__get_source_line_number([], Token, Context)
+		; { Char = ('`') } ->
+			lexer__get_context(Context),
+			{ Token = name("`") }
 		; { lexer__graphic_token_char(Char) } ->
 			lexer__get_context(Context),
 			lexer__get_graphic([Char], Token)
@@ -364,6 +367,9 @@
 			=(Posn1),
 			lexer__string_get_source_line_number(String, Len,
 				Posn1, Token, Context)
+		; { Char = ('`') } ->
+			lexer__string_get_context(Posn0, Context),
+			{ Token = name("`") }
 		; { lexer__graphic_token_char(Char) } ->
 			lexer__string_get_graphic(String, Len, Posn0,
 				Token, Context)
@@ -423,6 +429,9 @@
 			lexer__get_slash(Token, Context)
 		; { Char = ('#') } ->
 			lexer__get_source_line_number([], Token, Context)
+		; { Char = ('`') } ->
+			lexer__get_context(Context),
+			{ Token = name("`") }
 		; { lexer__graphic_token_char(Char) } ->
 			lexer__get_context(Context),
 			lexer__get_graphic([Char], Token)
@@ -471,6 +480,9 @@
 			=(Posn1),
 			lexer__string_get_source_line_number(String, Len,
 				Posn1, Token, Context)
+		; { Char = ('`') } ->
+			lexer__string_get_context(Posn0, Context),
+			{ Token = name("`") }
 		; { lexer__graphic_token_char(Char) } ->
 			lexer__string_get_graphic(String, Len, Posn0,
 				Token, Context)
Index: parser.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/parser.m,v
retrieving revision 1.32
diff -u -r1.32 parser.m
--- parser.m	1998/11/20 04:10:31	1.32
+++ parser.m	1999/07/09 06:37:30
@@ -359,13 +359,28 @@
 		% infix op
 		parser__get_token(Token, Context),
 		{ Token = comma, IsArg = no ->
-			Op = ","
+			Op0 = ","
 		;
-			Token = name(Op)
+			Token = name(Op0)
 		},
-		parser__get_ops_table(OpTable),
-		{ ops__lookup_infix_op(OpTable, Op,
-				OpPriority, LeftAssoc, RightAssoc) },
+		(
+				% A token surrounded by backquotes is a
+				% prefix token being using in an
+				% infix manner.
+			{ Op0 = "`" }
+		->
+			parser__get_token(name(Op), _),
+			parser__get_token(name("`"), _),
+
+			{ OpPriority = 100 },
+			{ LeftAssoc = y },
+			{ RightAssoc = x }
+		;
+			{ Op = Op0 },
+			parser__get_ops_table(OpTable),
+			{ ops__lookup_infix_op(OpTable, Op,
+					OpPriority, LeftAssoc, RightAssoc) }
+		),
 		{ OpPriority =< MaxPriority },
 		{ parser__check_priority(LeftAssoc, OpPriority, LeftPriority) }
 	->
Index: doc/reference_manual.texi
===================================================================
RCS file: /home/staff/zs/imp/mercury/doc/reference_manual.texi,v
retrieving revision 1.142
diff -u -r1.142 reference_manual.texi
--- reference_manual.texi	1999/07/08 08:48:05	1.142
+++ reference_manual.texi	1999/07/09 07:02:41
@@ -171,8 +171,8 @@
 @section Tokens
 
 Tokens in Mercury are the same as in ISO Prolog.
-The only difference is the @samp{#@var{line}} token, which
-is used as a line number directive (see below).
+The only differences are the @samp{#@var{line}} token, which
+is used as a line number directive (see below) and the @samp{`} token.
 
 The different tokens are as follows.  Tokens may be separated by
 whitespace or line number directives.
@@ -294,8 +294,9 @@
 @section Terms
 
 Syntactically, terms in Mercury are exactly the same as in ISO Prolog,
-except that as an extension we permit higher-order terms, as described
-below.
+except that as extensions we permit higher-order terms and the
+introduction of infix operators by the use of grave accents (backquotes)
+, as described below.
 However, the meaning of some terms in Mercury is different to that
 in Prolog.  @xref{Data-terms}.
 
@@ -309,6 +310,13 @@
 a sequence of argument terms separated by commas, and a close
 parenthesis.  Compound terms may also be specified using
 operator notation, as in Prolog.
+
+Operators can also be formed by enclosing an identifier between grave
+accents (backquotes).  Any variable or constructor may
+be used as an operator in this way.  If @code{fun} is an identifier,
+then a term of the form @code{fun(x,y)} is equivalent to 
+code{x `fun` y}.  The operator is treated as having the highest precedence
+possible and is left associative.
 
 A higher-order term is a variable followed without any intervening
 whitespace by an open parenthesis (i.e. an open_ct token),

----
 +----------------------------------------------------------------------+
 | Peter Ross      M Sci/Eng Melbourne Uni                              |
 | petdr at cs.mu.oz.au  WWW: www.cs.mu.oz.au/~petdr/ ph: +61 3 9344 9158  |
 +----------------------------------------------------------------------+
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list