From 7bc5d05a777045dbd5c9feace7c52c074b146794 Mon Sep 17 00:00:00 2001 From: Aleksander Sadikov Date: Sun, 29 May 2016 19:04:26 +0200 Subject: Denotational semantics, part #2 (mini algol): tests and translations added. --- prolog/problems/algol/algol_if_3/common.py | 42 +++++++++++++++++++++++++++++- prolog/problems/algol/algol_if_3/sl.py | 20 ++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 prolog/problems/algol/algol_if_3/sl.py (limited to 'prolog/problems/algol/algol_if_3') diff --git a/prolog/problems/algol/algol_if_3/common.py b/prolog/problems/algol/algol_if_3/common.py index 9198831..7e27430 100644 --- a/prolog/problems/algol/algol_if_3/common.py +++ b/prolog/problems/algol/algol_if_3/common.py @@ -1,3 +1,10 @@ +from operator import itemgetter +import socket + +import prolog.engine +import prolog.util +from server.hints import Hint, HintPopup + id = 177 number = 20 visible = True @@ -170,5 +177,38 @@ eval(E1 * E2, State, Val) :- !, eval(E1 / E2, State, Val) :- !, eval(E1, State, V1), eval(E2, State, V2), - Val is V1 / V2. + Val is V1 / V2. ''' + +test_cases = [ + ('findall(W, (W = [_], algol_if(_, W, [])), Words), Words == []', + [{}]), + ('algol_if(F, [begin,if,a,<,0,then,a,:=,0-a,else,a,:=,a,end,end], []), \ + apply([a= -2], Out, F), msort(Out, OutSorted), \ + OutSorted == [a=2,printout=[]]', + [{}]), + ('algol_if(F, [begin,if,0,<,a,then,print(a),else,b,:=,a+5,print(b),end,end], []), \ + apply([a= -2,b=0], Out, F), msort(Out, OutSorted), \ + OutSorted == [a= -2,b=3,printout=[3]]', + [{}]), +] + +def test(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + for query, answers in test_cases: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + diff --git a/prolog/problems/algol/algol_if_3/sl.py b/prolog/problems/algol/algol_if_3/sl.py new file mode 100644 index 0000000..a75bcef --- /dev/null +++ b/prolog/problems/algol/algol_if_3/sl.py @@ -0,0 +1,20 @@ +name = 'algol_if/3' +slug = 'interpreter za mini-algol z if stavkom' + +description = '''\ +

Razširi podano gramatiko za jezik mini-algol, da bo le-ta podprla uporabo stavka "if-then-else". +Privzemi, da morata obe veji (then in else) biti prisotni v stavku. Primer:

+
+% if a < b then
+%   print(a)
+% else
+%   print(b)
+% end
+?- _Program = [begin,if,a,<,b,then,print(a),else,print(b),end,end],
+    algol_if(_F, _Program, []),
+    apply([a=3,b=5], Output, _F).
+  Output = [a=3,b=5,printout=[3]].
+
+''' + +hint = {} -- cgit v1.2.1