treating `list__append' as `list:append'

Fergus Henderson fjh at cs.mu.oz.au
Sun Feb 9 03:29:28 AEDT 1997


Hi,

Who was next on my list of volunteers...?
Oh, that's right, Peter Schachte.

Peter, can you please review the following change?

-----------------------------------------------------------------------------

Add code to treat `__' as an alternative syntax for module qualification.
The code is currently commented out, because we don't yet support
module qualification of data constructors.  It should be re-enabled
as soon as we do.

Also avoid some code duplication.

compiler/prog_io.m:
compiler/prog_io_goal.m:
	Add (commented out) code to handle `__'.

compiler/prog_io_dcg.m:
compiler/type_util.m:
	Avoid code duplication: use sym_name_and_args from prog_io_goal.m
	for parsing possibly qualified terms.

cvs diff: Diffing .
Index: prog_io.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/prog_io.m,v
retrieving revision 1.151
diff -u -r1.151 prog_io.m
--- prog_io.m	1997/01/27 07:45:30	1.151
+++ prog_io.m	1997/02/08 13:42:58
@@ -1784,6 +1784,9 @@
 %		Module:Name
 %			Matches symbols with the specified name exported
 %			by the specified module.
+%
+%	We [will one day] also allow the syntax `Module__Name'
+%	as an alternative for `Module:Name'.
 
 :- pred parse_symbol_name(string, term, maybe1(sym_name)).
 :- mode parse_symbol_name(in, in, out) is det.
@@ -1806,15 +1809,32 @@
 	)
     ;
         ( 
-            Term = term__functor(term__atom(Name2), [], _Context3)
+            Term = term__functor(term__atom(Name), [], _Context3)
         ->
+/********
+Don't allow `__' as an alternative to `:', because
+we don't yet support qualification on constructors (e.g. term__variable).
 	    (
-		DefaultModName = ""
+		string__sub_string_search(Name, "__", LeftLength),
+		LeftLength > 0
 	    ->
-		Result = ok(unqualified(Name2))
+		string__left(Name, LeftLength, Module),
+		string__length(Name, NameLength),
+		RightLength is NameLength - LeftLength - 2,
+		string__right(Name, RightLength, Name2),
+		Result = ok(qualified(Module, Name2))
 	    ;
-		Result = ok(qualified(DefaultModName, Name2))
+********/
+	        (
+		    DefaultModName = ""
+	        ->
+		    Result = ok(unqualified(Name))
+	        ;
+		    Result = ok(qualified(DefaultModName, Name))
+	        )
+/********
 	    )
+********/
         ;
             Result = error("symbol name specifier expected", Term)
         )
Index: prog_io_dcg.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/prog_io_dcg.m,v
retrieving revision 1.1
diff -u -r1.1 prog_io_dcg.m
--- prog_io_dcg.m	1997/01/27 07:45:31	1.1
+++ prog_io_dcg.m	1997/02/06 21:44:20
@@ -60,11 +60,20 @@
 :- mode parse_dcg_goal(in, in, in, in, out, out, out, out) is det.
 
 parse_dcg_goal(Term, VarSet0, N0, Var0, Goal, VarSet, N, Var) :-
+	% first, figure out the context for the goal
 	(
-		Term = term__functor(term__atom(Functor), Args0, Context)
+		Term = term__functor(_, _, Context)
+	;
+		Term = term__variable(_),
+		term__context_init(Context)
+	),
+	% next, parse it
+	(
+		sym_name_and_args(Term, SymName, Args0)
 	->
 		% First check for the special cases:
 		(
+			SymName = unqualified(Functor),
 			parse_dcg_goal_2(Functor, Args0, Context,
 					VarSet0, N0, Var0,
 					Goal1, VarSet1, N1, Var1)
@@ -79,37 +88,18 @@
 			% goal, and append the DCG argument pair to the
 			% non-terminal's argument list.
 			new_dcg_var(VarSet0, N0, VarSet, N, Var),
-			(
-				Functor = ":" ,
-				Args0 = [term__functor(term__atom(ModuleName),
-						[], _), 
-					term__functor(term__atom(PredName),
-						Args_of_pred0, _)]
-			->
-				Pred = qualified(ModuleName, PredName),
-				Args1 = Args_of_pred0
-			;
-				Pred = unqualified(Functor),
-				Args1 = Args0
-			),
-			list__append(Args1,
+			list__append(Args0,
 				[term__variable(Var0), term__variable(Var)],
 				Args),
-			Goal = call(Pred, Args) - Context
+			Goal = call(SymName, Args) - Context
 		)
 	;
 		% A call to a free variable, or to a number or string.
 		% Just translate it into a call to call/3 - the typechecker
 		% will catch calls to numbers and strings.
-		(
-			Term = term__functor(_, _, CallContext)
-		;
-			Term = term__variable(_),
-			term__context_init(CallContext)
-		),
 		new_dcg_var(VarSet0, N0, VarSet, N, Var),
 		Goal = call(unqualified("call"), [Term, term__variable(Var0),
-				term__variable(Var)]) - CallContext
+				term__variable(Var)]) - Context
 	).
 
 	% parse_dcg_goal_2(Functor, Args, Context, VarSet0, N0, Var0,
Index: prog_io_goal.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/prog_io_goal.m,v
retrieving revision 1.1
diff -u -r1.1 prog_io_goal.m
--- prog_io_goal.m	1997/01/27 07:45:32	1.1
+++ prog_io_goal.m	1997/02/08 14:09:46
@@ -65,6 +65,14 @@
 	%	(or if Args is empty, one of
 	%		Name
 	%		Module:Name)
+	%	For backwards compatibility, we allow `__'
+	%	as an alternative to `:'.
+
+	% sym_name_and_args takes a term and returns a sym_name and a list of
+	% argument terms.
+	% It fals if the input is not valid syntax for a QualifiedTerm.
+:- pred sym_name_and_args(term, sym_name, list(term)).
+:- mode sym_name_and_args(in, out, out) is semidet.
 
 	% parse_qualified_term takes a term and an error message,
 	% and returns a sym_name and a list of argument terms.
@@ -76,7 +84,8 @@
 	% parse_qualified_term takes a default module name and a term,
 	% and returns a sym_name and a list of argument terms.
 	% Returns an error on ill-formed input or a module qualifier that
-	% doesn't match the DefaultModName, if DefaultModName is not "".
+	% doesn't match the DefaultModName, if DefaultModName is not ""
+	% and not "mercury_builtin".
 	% parse_qualified_term/3 calls parse_qualified_term/4, and is 
 	% used when no default module name exists.
 
@@ -88,7 +97,7 @@
 :- implementation.
 
 :- import_module hlds_data.
-:- import_module string, std_util.
+:- import_module int, string, std_util.
 
 	% Parse a goal.
 	%
@@ -96,6 +105,13 @@
 	% in either the type-checker or parser anyway.
 
 parse_goal(Term, VarSet0, Goal, VarSet) :-
+	% first, get the goal context
+	(
+		Term = term__functor(_, _, Context)
+	;
+		Term = term__variable(_),
+		term__context_init(Context)
+	),
 	% We just check if it matches the appropriate pattern
 	% for one of the builtins.  If it doesn't match any of the
 	% builtins, then it's just a predicate call.
@@ -110,32 +126,14 @@
 		% it's not a builtin
 		(
 			% check for predicate calls
-			Term = term__functor(term__atom(Name), Terms, Context)
+			sym_name_and_args(Term, SymName, Args)
 		->
 			VarSet = VarSet0,
-			% check for module qualification
-			(
-				Name = ":",
-				Terms = [term__functor(term__atom(ModuleName),
-						[], _), 
-					term__functor(term__atom(PredName),
-						Args, _)]
-			->
-				Goal = call(qualified(ModuleName, 
-					PredName), Args) - Context
-			;
-				Goal = call(unqualified(Name), Terms) - Context
-			)
+			Goal = call(SymName, Args) - Context
 		;
 		% A call to a free variable, or to a number or string.
 		% Just translate it into a call to call/1 - the typechecker
 		% will catch calls to numbers and strings.
-			(
-				Term = term__functor(_, _, Context)
-			;
-				Term = term__variable(_),
-				term__context_init(Context)
-			),
 			Goal = call(unqualified("call"), [Term]) - Context,
 			VarSet = VarSet0
 		)
@@ -321,6 +319,9 @@
 
 %-----------------------------------------------------------------------------%
 
+sym_name_and_args(Term, SymName, Args) :-
+	parse_qualified_term(Term, "", ok(SymName, Args)).
+
 parse_qualified_term(Term, Msg, Result) :-
 	parse_qualified_term("", Term, Msg, Result).
 
@@ -336,7 +337,10 @@
                 ModuleTerm = term__functor(term__atom(Module), [], _Context3)
 	    ->
 		(
-		    ( Module = DefaultModName ; DefaultModName = "")
+		    ( Module = DefaultModName
+		    ; DefaultModName = ""
+		    ; DefaultModName = "mercury_builtin"
+		    )
 		->
 		    Result = ok(qualified(Module, Name), Args)
 		;
@@ -350,14 +354,37 @@
 	)
     ;
         ( 
-            Term = term__functor(term__atom(Name2), Args2, _Context4)
+            Term = term__functor(term__atom(Name), Args, _Context4)
         ->
 	    (
+/**********
+Don't allow `__' as an alternative to `:',
+because we don't yet support qualification of constructors,
+e.g. `term__variable(_)'.
+		string__sub_string_search(Name, "__", LeftLength),
+		LeftLength > 0
+	    ->
+		string__left(Name, LeftLength, Module),
+		string__length(Name, NameLength),
+		RightLength is NameLength - LeftLength - 2,
+		string__right(Name, RightLength, Name2),
+		(
+		    ( Module = DefaultModName
+		    ; DefaultModName = ""
+		    ; DefaultModName = "mercury_builtin"
+		    )
+		->
+		    Result = ok(qualified(Module, Name2), Args)
+		;
+		    Result = error("module qualifier (name before `__') in definition does not match preceding `:- module' declaration", Term)
+		)
+	    ;
+**********/
 		DefaultModName = ""
 	    ->
-            	Result = ok(unqualified(Name2), Args2)
+            	Result = ok(unqualified(Name), Args)
 	    ;
-		Result = ok(qualified(DefaultModName, Name2), Args2)
+		Result = ok(qualified(DefaultModName, Name), Args)
 	    )
         ;
 	    string__append("atom expected in ", Msg, ErrorMsg),
Index: type_util.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/type_util.m,v
retrieving revision 1.37
diff -u -r1.37 type_util.m
--- type_util.m	1997/01/29 00:42:11	1.37
+++ type_util.m	1997/02/06 21:49:02
@@ -154,7 +154,7 @@
 
 :- implementation.
 :- import_module bool, list, term, require, map, std_util.
-:- import_module prog_io, prog_util.
+:- import_module prog_io, prog_io_goal, prog_util.
 
 type_util__type_id_module(_ModuleInfo, qualified(ModuleName, _) -_, ModuleName).
 type_util__type_id_module(_ModuleInfo, unqualified(_) - _, "").
@@ -246,21 +246,11 @@
 	IsEnum = yes.
 
 type_to_type_id(Type, SymName - Arity, Args) :-
-	Type = term__functor(term__atom(Atom), Args0, _),
-	(
-		Atom = ":", Args0 = [ModuleTerm, TypeAndArgsTerm]
-	->
-		ModuleTerm = term__functor(term__atom(ModuleName), [], _),
-		TypeAndArgsTerm = term__functor(term__atom(TypeName), Args1, _),
-		SymName = qualified(ModuleName, TypeName)
-	;
-		SymName = unqualified(Atom),
-		Args1 = Args0
-	),
-
-		% higher order types may have representations where
-		% their arguments don't directly correspond to the
-		% arguments of the term.
+	sym_name_and_args(Type, SymName, Args1),
+
+	% higher order types may have representations where
+	% their arguments don't directly correspond to the
+	% arguments of the term.
 	(
 		type_is_higher_order(Type, _, PredArgTypes) 
 	->
cvs diff: Diffing notes

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the developers mailing list