001    package org.jdmp.core.script.jdmp;
002    
003    import java.io.PushbackReader;
004    import java.io.StringReader;
005    
006    import org.jdmp.core.module.Module;
007    import org.jdmp.core.script.Result;
008    import org.jdmp.core.script.jdmp.lexer.Lexer;
009    import org.jdmp.core.script.jdmp.node.Start;
010    import org.jdmp.core.script.jdmp.parser.Parser;
011    
012    public class Interpreter {
013    
014            private Module module = null;
015    
016            public Interpreter(Module module) {
017                    this.module = module;
018            }
019    
020            public Result execute(String s) {
021                    try {
022                            if (!s.endsWith(";")) {
023                                    s = s + ";";
024                            }
025    
026                            Translation translation = new Translation(module);
027                            StringReader sr = new StringReader(s);
028                            Parser p = new Parser(new Lexer(new PushbackReader(sr, 1024)));
029    
030                            Start tree = p.parse();
031                            tree.apply(translation);
032    
033                            return translation.getResult();
034    
035                    } catch (Exception e) {
036                            e.printStackTrace();
037                            if (e.getCause() != null) {
038                                    return new Result("Error", e.getCause());
039                            } else {
040                                    return new Result("Unparseable expression", e);
041                            }
042                    }
043            }
044    
045    }