summaryrefslogtreecommitdiff
path: root/prolog/common.py
blob: 1c83feeda8f5c062b5b1d8c3388b67a24918817d (plain)
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
# coding=utf-8

import operator
import prolog.engine
from server.hints import Hint, HintPopup

id = 1

hint_type = {
    'no_hint': Hint('no_hint'),
    'program_already_correct': Hint('program_already_correct'),
    'system_error': Hint('system_error'),
    'test_results': Hint('test_results'),
    'syntax_error': Hint('syntax_error'),
    'monkey_main': Hint('monkey_main'),
    'monkey_change': HintPopup('monkey_change', style='change'),
    'monkey_insert': HintPopup('monkey_insert', style='insert'),
    'monkey_remove': HintPopup('monkey_remove', style='remove'),
}

def hint(program, solved_problems):
    # Check program for syntax errors.
    engine_id, output = prolog.engine.create(code=program)
    if engine_id is not None:
        prolog.engine.destroy(engine_id)
    if 'error' in map(operator.itemgetter(0), output):
        errors_msg = '\n'.join(['{}: {}'.format(m_type, m_text) for m_type, m_text in output])
        return [{'id': 'syntax_error', 'args': {'messages': errors_msg}}]
    return []