[m-dev.] diff: fix parsing of Aditi tuples
Simon Taylor
stayl at cs.mu.OZ.AU
Tue Oct 17 12:33:48 AEDT 2000
Estimated hours taken: 0.1
library/io.m:
library/term.m:
Allow integers where floats are expected when
parsing an Aditi tuple.
Index: io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.208
diff -u -u -r1.208 io.m
--- io.m 2000/10/16 01:33:45 1.208
+++ io.m 2000/10/16 02:40:51
@@ -1090,7 +1090,8 @@
% This is the same as io__read_from_string, except that an integer
% is allowed where a character is expected. This is needed by
% extras/aditi/aditi.m because Aditi does not have a builtin
-% character type.
+% character type. This also allows an integer where a float
+% is expected.
:- pred io__read_from_string_with_int_instead_of_char(string, string, int,
io__read_result(T), posn, posn).
@@ -1731,45 +1732,45 @@
io__read(Result) -->
term_io__read_term(ReadResult),
io__get_line_number(LineNumber),
- { IntInsteadOfChar = no },
- { io__process_read_term(IntInsteadOfChar, ReadResult, LineNumber,
+ { IsAditiTuple = no },
+ { io__process_read_term(IsAditiTuple, ReadResult, LineNumber,
Result) }.
io__read_from_string_with_int_instead_of_char(FileName, String, Len, Result,
Posn0, Posn) :-
- IntInsteadOfChar = yes,
- io__read_from_string(IntInsteadOfChar, FileName, String, Len, Result,
+ IsAditiTuple = yes,
+ io__read_from_string(IsAditiTuple, FileName, String, Len, Result,
Posn0, Posn).
io__read_from_string(FileName, String, Len, Result, Posn0, Posn) :-
- IntInsteadOfChar = no,
- io__read_from_string(IntInsteadOfChar, FileName, String, Len,
+ IsAditiTuple = no,
+ io__read_from_string(IsAditiTuple, FileName, String, Len,
Result, Posn0, Posn).
:- pred io__read_from_string(bool, string, string, int, io__read_result(T),
posn, posn).
:- mode io__read_from_string(in, in, in, in, out, in, out) is det.
-io__read_from_string(IntInsteadOfChar, FileName, String, Len,
+io__read_from_string(IsAditiTuple, FileName, String, Len,
Result, Posn0, Posn) :-
parser__read_term_from_string(FileName, String, Len,
Posn0, Posn, ReadResult),
Posn = posn(LineNumber, _, _),
- io__process_read_term(IntInsteadOfChar, ReadResult, LineNumber, Result).
+ io__process_read_term(IsAditiTuple, ReadResult, LineNumber, Result).
:- pred io__process_read_term(bool, read_term, int, io__read_result(T)).
:- mode io__process_read_term(in, in, in, out) is det.
-io__process_read_term(IntInsteadOfChar, ReadResult, LineNumber, Result) :-
+io__process_read_term(IsAditiTuple, ReadResult, LineNumber, Result) :-
(
ReadResult = term(_VarSet, Term),
(
(
- IntInsteadOfChar = yes,
+ IsAditiTuple = yes,
term_to_type_with_int_instead_of_char(Term,
Type)
;
- IntInsteadOfChar = no,
+ IsAditiTuple = no,
term_to_type(Term, Type)
)
->
Index: term.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/term.m,v
retrieving revision 1.94
diff -u -u -r1.94 term.m
--- term.m 2000/10/10 05:36:50 1.94
+++ term.m 2000/10/16 02:26:48
@@ -330,7 +330,8 @@
% This is the same as term_to_type, except that an integer
% is allowed where a character is expected. This is needed by
% extras/aditi/aditi.m because Aditi does not have a builtin
-% character type.
+% character type. This also allows an integer where a float
+% is expected.
:- pred term__term_to_type_with_int_instead_of_char(term(U), T).
:- mode term__term_to_type_with_int_instead_of_char(in, out) is semidet.
@@ -368,18 +369,18 @@
term__try_term_to_type(Term, ok(Val)).
term__term_to_type_with_int_instead_of_char(Term, Val) :-
- IntInsteadOfChar = yes,
- term__try_term_to_type(IntInsteadOfChar, Term, ok(Val)).
+ IsAditiTuple = yes,
+ term__try_term_to_type(IsAditiTuple, Term, ok(Val)).
term__try_term_to_type(Term, Result) :-
- IntInsteadOfChar = no,
- term__try_term_to_type(IntInsteadOfChar, Term, Result).
+ IsAditiTuple = no,
+ term__try_term_to_type(IsAditiTuple, Term, Result).
:- pred term__try_term_to_type(bool, term(U), term_to_type_result(T, U)).
:- mode term__try_term_to_type(in, in, out) is det.
-term__try_term_to_type(IntInsteadOfChar, Term, Result) :-
- term__try_term_to_univ(IntInsteadOfChar, Term,
+term__try_term_to_type(IsAditiTuple, Term, Result) :-
+ term__try_term_to_univ(IsAditiTuple, Term,
type_of(ValTypedVar), UnivResult),
(
UnivResult = ok(Univ),
@@ -394,8 +395,8 @@
:- pred term__try_term_to_univ(bool::in, term(T)::in, type_desc::in,
term_to_type_result(univ, T)::out) is det.
-term__try_term_to_univ(IntInsteadOfChar, Term, Type, Result) :-
- term__try_term_to_univ_2(IntInsteadOfChar, Term, Type, [], Result).
+term__try_term_to_univ(IsAditiTuple, Term, Type, Result) :-
+ term__try_term_to_univ_2(IsAditiTuple, Term, Type, [], Result).
:- pred term__try_term_to_univ_2(bool::in, term(T)::in, type_desc::in,
term_to_type_context::in,
@@ -403,11 +404,11 @@
term__try_term_to_univ_2(_, term__variable(Var), _Type, Context,
error(mode_error(Var, Context))).
-term__try_term_to_univ_2(IntInsteadOfChar, Term, Type, Context, Result) :-
+term__try_term_to_univ_2(IsAditiTuple, Term, Type, Context, Result) :-
Term = term__functor(Functor, ArgTerms, TermContext),
(
type_ctor_and_args(Type, TypeCtor, TypeArgs),
- term__term_to_univ_special_case(IntInsteadOfChar,
+ term__term_to_univ_special_case(IsAditiTuple,
type_ctor_module_name(TypeCtor),
type_ctor_name(TypeCtor),
TypeArgs, Term, Type, Context, SpecialCaseResult)
@@ -417,7 +418,7 @@
Functor = term__atom(FunctorName),
list__length(ArgTerms, Arity),
find_functor(Type, FunctorName, Arity, FunctorNumber, ArgTypes),
- term__term_list_to_univ_list(IntInsteadOfChar, ArgTerms,
+ term__term_list_to_univ_list(IsAditiTuple, ArgTerms,
ArgTypes, Functor, 1, Context, TermContext, ArgsResult)
->
(
@@ -444,14 +445,14 @@
type_desc::in, term_to_type_context::in,
term_to_type_result(univ, T)::out) is semidet.
-term__term_to_univ_special_case(IntInsteadOfChar, "builtin", "character", [],
+term__term_to_univ_special_case(IsAditiTuple, "builtin", "character", [],
Term, _, _, ok(Univ)) :-
(
- IntInsteadOfChar = no,
+ IsAditiTuple = no,
Term = term__functor(term__atom(FunctorName), [], _),
string__first_char(FunctorName, Char, "")
;
- IntInsteadOfChar = yes,
+ IsAditiTuple = yes,
Term = term__functor(term__integer(Int), [], _),
char__to_int(Char, Int)
),
@@ -464,11 +465,17 @@
Term, _, _, ok(Univ)) :-
Term = term__functor(term__string(String), [], _),
type_to_univ(String, Univ).
-term__term_to_univ_special_case(_, "builtin", "float", [],
+term__term_to_univ_special_case(IsAditiTuple, "builtin", "float", [],
Term, _, _, ok(Univ)) :-
- Term = term__functor(term__float(Float), [], _),
- type_to_univ(Float, Univ).
-term__term_to_univ_special_case(IntInsteadOfChar, "array", "array", [ElemType],
+ ( Term = term__functor(term__float(Float), [], _) ->
+ type_to_univ(Float, Univ)
+ ;
+ IsAditiTuple = yes,
+ Term = term__functor(term__integer(Int), [], _),
+ int__to_float(Int, Float),
+ type_to_univ(Float, Univ)
+ ).
+term__term_to_univ_special_case(IsAditiTuple, "array", "array", [ElemType],
Term, _Type, PrevContext, Result) :-
%
% arrays are represented as terms of the form
@@ -484,7 +491,7 @@
ListType = type_of([Elem]),
ArgContext = arg_context(term__atom("array"), 1, TermContext),
NewContext = [ArgContext | PrevContext],
- term__try_term_to_univ_2(IntInsteadOfChar, ArgList, ListType,
+ term__try_term_to_univ_2(IsAditiTuple, ArgList, ListType,
NewContext, ArgResult),
(
ArgResult = ok(ListUniv),
@@ -534,16 +541,16 @@
term_to_type_context::in, term__context::in,
term_to_type_result(list(univ), T)::out) is semidet.
term__term_list_to_univ_list(_, [], [], _, _, _, _, ok([])).
-term__term_list_to_univ_list(IntInsteadOfChar, [ArgTerm|ArgTerms],
+term__term_list_to_univ_list(IsAditiTuple, [ArgTerm|ArgTerms],
[Type|Types], Functor, ArgNum, PrevContext,
TermContext, Result) :-
ArgContext = arg_context(Functor, ArgNum, TermContext),
NewContext = [ArgContext | PrevContext],
- term__try_term_to_univ_2(IntInsteadOfChar, ArgTerm, Type,
+ term__try_term_to_univ_2(IsAditiTuple, ArgTerm, Type,
NewContext, ArgResult),
(
ArgResult = ok(Arg),
- term__term_list_to_univ_list(IntInsteadOfChar, ArgTerms, Types,
+ term__term_list_to_univ_list(IsAditiTuple, ArgTerms, Types,
Functor, ArgNum + 1, PrevContext,
TermContext, RestResult),
(
--------------------------------------------------------------------------
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