This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculadora.m
229 lines (196 loc) · 6.94 KB
/
calculadora.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
:- module calculadora.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is cc_multi.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module exception.
:- import_module int.
:- import_module list.
:- import_module map.
:- import_module maybe.
:- import_module ops.
:- import_module pair.
:- import_module parser.
:- import_module require.
:- import_module string.
:- import_module term.
:- import_module term_io.
:- import_module univ.
:- import_module varset.
%-----------------------------------------------------------------------------%
:- type calc_info == map(string, int).
main(!IO) :-
main_2(map.init, !IO).
:- pred main_2(calc_info::in, io::di, io::uo) is cc_multi.
main_2(CalcInfo0, !IO) :-
io.write_string("calculadora> ", !IO),
io.flush_output(!IO),
parser.read_term_with_op_table(calculator_op_table, Res, !IO),
(
Res = error(Msg, _Line),
io.write_string(Msg, !IO),
io.nl(!IO),
main(!IO)
;
Res = eof,
io.write_string("EOF\n", !IO)
;
Res = term(VarSet, Term),
( if
Term = term.functor(term.atom("="),
[term.variable(Var, _Context), ExprTerm0], _)
then
ExprTerm = ExprTerm0,
varset.lookup_name(VarSet, Var, VarName),
SetVar = yes(VarName)
else
ExprTerm = Term,
SetVar = no
),
try(
( pred(Num0::out) is det :-
Num0 = eval_expr(CalcInfo0, VarSet, ExprTerm)
), EvalResult),
(
EvalResult = succeeded(Num),
io.write_int(Num, !IO),
io.nl(!IO),
( SetVar = yes(VarToSet) ->
map.set(VarToSet, Num, CalcInfo0, CalcInfo)
;
CalcInfo = CalcInfo0
)
;
EvalResult = exception(Exception),
CalcInfo = CalcInfo0,
( univ_to_type(Exception, EvalError) ->
report_eval_error(EvalError, !IO)
;
rethrow(EvalResult)
)
),
% Recursively call ourself for the next term(s).
main_2(CalcInfo, !IO)
).
:- pred report_eval_error(eval_error::in, io::di, io::uo) is det.
report_eval_error(unknown_operator(Name, Arity), !IO) :-
io.write_string("operador desconocido `", !IO),
io.write_string(Name, !IO),
io.write_string("/", !IO),
io.write_int(Arity, !IO),
io.write_string("'.\n", !IO).
report_eval_error(unknown_variable(Name), !IO) :-
io.write_string("variable desconocida`", !IO),
io.write_string(Name, !IO),
io.write_string("'.\n", !IO).
report_eval_error(unexpected_const(Const), !IO) :-
io.write_string("inesperado", !IO),
(
Const = term.int(int),
io.write_string(" int `", !IO),
io.write_int(int, !IO),
io.write_string("'", !IO)
;
Const = term.string(String),
io.write_string(" string """, !IO),
io.write_string(String, !IO),
io.write_string("""", !IO)
;
( Const = term.int(_, _, _, _)
; Const = term.atom(_)
; Const = term.implementation_defined(_)
),
error("error")
),
io.nl(!IO).
:- func eval_expr(calc_info, varset, term) = int.
eval_expr(CalcInfo, VarSet, term.variable(Var, _Context)) = Res :-
varset.lookup_name(VarSet, Var, VarName),
( if map.search(CalcInfo, VarName, Res0) then
Res = Res0
else
throw(unknown_variable(VarName))
).
eval_expr(CalcInfo, VarSet, term.functor(term.atom(Op), Args, _)) = Res :-
( if
(
Args = [Arg1],
Res0 = eval_unop(Op, eval_expr(CalcInfo, VarSet, Arg1))
;
Args = [Arg1, Arg2],
Res0 = eval_binop(Op,
eval_expr(CalcInfo, VarSet, Arg1),
eval_expr(CalcInfo, VarSet, Arg2))
)
then
Res = Res0
else
throw(unknown_operator(Op, list.length(Args)))
).
eval_expr(_, _, Term) = int :-
Term = term.functor(Const, _, Context),
Const = term.int(_, _, _, _),
( if term_to_int(Term, int0) then
int = int0
else
throw(unexpected_const(Const) - Context)
).
eval_expr(_, _, term.functor(term.int(int), _, Context)) =
throw(unexpected_const(term.int(int)) - Context).
eval_expr(_, _, term.functor(term.string(String), _, Context)) =
throw(unexpected_const(term.string(String)) - Context).
eval_expr(_, _, term.functor(ImplDefConst, _, Context)) = _ :-
ImplDefConst = term.implementation_defined(_),
throw(unexpected_const(ImplDefConst) - Context).
:- func eval_unop(string, int) = int is semidet.
eval_unop("-", Num) = -Num.
eval_unop("+", Num) = Num.
:- func eval_binop(string, int, int) = int is semidet.
eval_binop("-", Num1, Num2) = Num1 - Num2.
eval_binop("+", Num1, Num2) = Num1 + Num2.
eval_binop("*", Num1, Num2) = Num1 * Num2.
eval_binop("//", Num1, Num2) = Num1 // Num2.
:- type eval_error
---> unknown_operator(
string, % name
int % arity
)
; unknown_variable(string)
; unexpected_const(term.const).
:- type calculator_op_table ---> calculator_op_table.
:- pred calculadora.ops_table(string::in, op_info::out, list(op_info)::out)
is semidet.
calculadora.ops_table("//", op_info(infix(y, x), 400), []).
calculadora.ops_table("*", op_info(infix(y, x), 400), []).
calculadora.ops_table("+", op_info(infix(y, x), 500),
[op_info(prefix(x), 500)]).
calculadora.ops_table("-", op_info(infix(y, x), 500),
[op_info(prefix(x), 200)]).
calculadora.ops_table("=", op_info(infix(x, x), 700), []).
:- instance ops.op_table(calculator_op_table) where [
( ops.lookup_infix_op(_, Op, Priority, LeftAssoc, RightAssoc) :-
calculadora.ops_table(Op, Info, _),
Info = op_info(infix(LeftAssoc, RightAssoc), Priority)
),
ops.lookup_operator_term(_, _, _, _) :- fail,
( ops.lookup_prefix_op(_, Op, Priority, LeftAssoc) :-
calculadora.ops_table(Op, _, OtherInfo),
OtherInfo = [op_info(prefix(LeftAssoc), Priority)]
),
ops.lookup_postfix_op(_, _, _, _) :- fail,
ops.lookup_binary_prefix_op(_, _, _, _, _) :- fail,
ops.lookup_op(Table, Op) :- ops.lookup_infix_op(Table, Op, _, _, _),
ops.lookup_op(Table, Op) :- ops.lookup_prefix_op(Table, Op, _, _),
ops.lookup_op(Table, Op) :-
ops.lookup_binary_prefix_op(Table, Op, _, _, _),
ops.lookup_op(Table, Op) :- ops.lookup_postfix_op(Table, Op, _, _),
ops.lookup_op_infos(_, Op, OpInfo, OtherInfos) :-
calculadora.ops_table(Op, OpInfo, OtherInfos),
ops.max_priority(_) = 700,
ops.arg_priority(Table) = ops.max_priority(Table) + 1
].
%-----------------------------------------------------------------------------%
:- end_module calculadora.
%-----------------------------------------------------------------------------%