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_3/common.py | 36 ++++++++++++++++++++++++++++++++- prolog/problems/algol/algol_3/sl.py | 32 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 prolog/problems/algol/algol_3/sl.py (limited to 'prolog/problems/algol/algol_3') diff --git a/prolog/problems/algol/algol_3/common.py b/prolog/problems/algol/algol_3/common.py index ff38469..e997bde 100644 --- a/prolog/problems/algol/algol_3/common.py +++ b/prolog/problems/algol/algol_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 = 176 number = 10 visible = True @@ -163,5 +170,32 @@ 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(_, W, [])), Words), Words == []', + [{}]), + ('algol(F, [begin,a,:=,a+2,end], []), apply([a=3], Out, F), msort(Out, OutSorted), OutSorted == [a=5,printout=[]]', + [{}]), +] + +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_3/sl.py b/prolog/problems/algol/algol_3/sl.py new file mode 100644 index 0000000..6fa9b0b --- /dev/null +++ b/prolog/problems/algol/algol_3/sl.py @@ -0,0 +1,32 @@ +name = 'algol/3' +slug = 'interpreter za mini-algol' + +description = '''\ +

DCG za mini-algol.

+
+% uporabi (apply) funkcijo nad začetnim stanjem
+?- apply([a=2], Out, fun(_In, Out, eval(a+3, _In, Out))).
+  Out = 5.
+
+% a := a+b
+% b := a-b
+% a := a-b
+?- _Program = [begin,a,:=,a+b,b,:=,a-b,a,:=,a-b,end],
+    algol(_F, _Program, []),
+    apply([a=3,b=5], Output, _F).
+  Output = [a=5,b=3,printout=[]].
+
+% a := 0
+% while a < 10 do
+% begin
+%   print(a)
+%   a := a+1
+% end
+?- _Program = [begin,a,:=,0,while,a,<,10,do,begin,print(a),a,:=,a+1,end,end],
+    algol(_F, _Program, []),
+    apply([a=3], Output, _F).
+  Output = [a=10,printout=[0,1,2,3,4,5,6,7,8,9]].
+
+''' + +hint = {} -- cgit v1.2.1