%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% File : JEVAL_DEMO %% Created by : Joakim Eriksson & Niclas Finne %% Created date : 01-11-15 %% %% $Author: nfi $ %% $Date: 2001/12/11 13:03:28 $ %% $Revision: 1.6 $ %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %% Description: Demo of jeval - single user mode. %% %% Comments: %% Workaround to get window positioned and sized under Unix / X. %% window.show();window.pack();window.setLocation(100,100); %% ( should normally be setBounds() ) :- use_module('../../jeval/jeval', []). start :- start(client). start(Mode) :- jeval:jeval_init(Mode, []), jeval:jeval('window=new JFrame("Jeval Demo");\c pane = window.getContentPane();\c pane.setLayout(new GridLayout(0,1));\c pane.add(new JLabel("Enter expression"));\c expr = new JTextField("5 * 7");\c pane.add(expr);\c result = new JLabel("Result:");\c pane.add(result);\c button = new JButton("Evaluate!");\c pane.add(button);\c window.show();\c window.pack();\c window.setLocation(100, 100);\c jeval.addActionListener("button");\c jeval.addActionListener("expr");'), event_loop. stop:- jeval:jeval('jeval.removeActionListener("button");\c jeval.removeActionListener("expr");\c window.setVisible(false)'), jeval:jeval_deinit. event_loop :- jeval:jeval_result('jeval.getNextEvent(1000)', Result), get_term(Result, Event), handle_event(Event), event_loop. handle_event(nil). handle_event(action_event(_, _, _)) :- jeval:jeval_result('expr.getText()', ExpAtom), eval(ExpAtom, Val), jeval:jeval('result.setText("Result: ' + Val + '")'). eval(ExpAtom, Val) :- get_term(ExpAtom, Expr), catch(Val is Expr, _, fail), !. eval(_, Val) :- Val = 'Syntax error, please type in an arithmetic expression!'. get_term(Result, Term) :- charsio:atom_to_chars(Result, R_S, "."), catch(charsio:read_from_chars(R_S, Term), _, fail).