summaryrefslogtreecommitdiff
path: root/prolog/problems/algol/algol_for_3
diff options
context:
space:
mode:
authorAleksander Sadikov <aleksander.sadikov@fri.uni-lj.si>2016-05-29 19:04:26 +0200
committerAleksander Sadikov <aleksander.sadikov@fri.uni-lj.si>2016-05-29 19:04:26 +0200
commit7bc5d05a777045dbd5c9feace7c52c074b146794 (patch)
tree8cdddfe9e34b101b44a8966e18e2634ec18e0281 /prolog/problems/algol/algol_for_3
parent8204263c09d77c2b4a8648aa9cad1bba97a9a5f5 (diff)
Denotational semantics, part #2 (mini algol): tests and translations added.
Diffstat (limited to 'prolog/problems/algol/algol_for_3')
-rw-r--r--prolog/problems/algol/algol_for_3/common.py42
-rw-r--r--prolog/problems/algol/algol_for_3/sl.py18
2 files changed, 59 insertions, 1 deletions
diff --git a/prolog/problems/algol/algol_for_3/common.py b/prolog/problems/algol/algol_for_3/common.py
index b48610b..38d5d45 100644
--- a/prolog/problems/algol/algol_for_3/common.py
+++ b/prolog/problems/algol/algol_for_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 = 178
number = 30
visible = True
@@ -177,5 +184,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_for(_, W, [])), Words), Words == []',
+ [{}]),
+ ('algol_for(F, [begin,for,a,:=,0,&,a,<,9,&,a,:=,a+1,do,begin,print(a),a,:=,a+1,end,end], []), \
+ apply([a=0], Out, F), msort(Out, OutSorted), \
+ OutSorted == [a=10,printout=[0,2,4,6,8]]',
+ [{}]),
+ ('algol_for(F, [begin,for,a,:=,1,&,a,<,32,&,a,:=,a*2,do,begin,print(a),end,end], []), \
+ apply([a=0], Out, F), msort(Out, OutSorted), \
+ OutSorted == [a=32,printout=[1,2,4,8,16]]',
+ [{}]),
+]
+
+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_for_3/sl.py b/prolog/problems/algol/algol_for_3/sl.py
new file mode 100644
index 0000000..17d91fc
--- /dev/null
+++ b/prolog/problems/algol/algol_for_3/sl.py
@@ -0,0 +1,18 @@
+name = 'algol_for/3'
+slug = 'interpreter za mini-algol s for stavkom'
+
+description = '''\
+<p>Razširi podano gramatiko za jezik mini-algol, da bo le-ta podprla uporabo stavka "for". Primer:</p>
+<pre>
+% for a := 0 & a < 5 & a := a + 1 do
+% begin
+% print(a)
+% end
+?- _Program = [begin,for,a,:=,0,&,a,<,5,&,a,:=,a+1,do,begin,print(a),end,end],
+ algol_for(_F, _Program, []),
+ apply([a=2], Output, _F).
+ Output = [a=5,printout=[0,1,2,3,4]].
+</pre>
+'''
+
+hint = {}