1 /********************************************************************************
2 *
3 * Copyright (C) 2008 Fujitsu Services Ltd.
4 *
5 * Author: Nick Battle
6 *
7 * This file is part of VDMJ.
8 *
9 * VDMJ is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * VDMJ is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21 *
22 ******************************************************************************/
23
24 package org.overturetool.vdmj.commands;
25
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 import org.overturetool.vdmj.definitions.ClassDefinition;
30 import org.overturetool.vdmj.definitions.ClassList;
31 import org.overturetool.vdmj.runtime.ClassInterpreter;
32 import org.overturetool.vdmj.runtime.VDMThreadSet;
33
34
35 /***
36 * A class to read and perform class related commands from standard input.
37 */
38
39 public class ClassCommandReader extends CommandReader
40 {
41 /*** A ClassInterpreter version of the interpreter field. */
42 private final ClassInterpreter cinterpreter;
43
44 public ClassCommandReader(ClassInterpreter interpreter, String prompt)
45 {
46 super(interpreter, prompt);
47 cinterpreter = interpreter;
48 }
49
50 @Override
51 protected boolean doDefault(String line) throws Exception
52 {
53 String parts[] = line.split("//s+");
54
55 if (parts.length != 2)
56 {
57 throw new Exception("Usage: default <default class name>");
58 }
59
60 cinterpreter.setDefaultName(parts[1]);
61 println("Default class set to " + cinterpreter.getDefaultName());
62 return true;
63 }
64
65 @Override
66 protected boolean doClasses(String line)
67 {
68 String def = cinterpreter.getDefaultName();
69 ClassList classes = cinterpreter.getClasses();
70
71 for (ClassDefinition c: classes)
72 {
73 if (c.name.name.equals(def))
74 {
75 println(c.name.name + " (default)");
76 }
77 else
78 {
79 println(c.name.name);
80 }
81 }
82
83 return true;
84 }
85
86 @Override
87 protected boolean doCreate(String line) throws Exception
88 {
89 Pattern p = Pattern.compile("^create (//w+)//s*?:=//s*(.+)$");
90 Matcher m = p.matcher(line);
91
92 if (m.matches())
93 {
94 String var = m.group(1);
95 String exp = m.group(2);
96
97 cinterpreter.create(var, exp);
98 }
99 else
100 {
101 throw new Exception("Usage: create <id> := <value>");
102 }
103
104 return true;
105 }
106
107 @Override
108 protected boolean doThreads(String line)
109 {
110 print(VDMThreadSet.getStatus());
111 return true;
112 }
113
114 @Override
115 protected void doHelp(String line)
116 {
117 println("classes - list the loaded class names");
118 println("threads - list active threads");
119 println("default <class> - set the default class name");
120 println("create <id> := <exp> - create a named variable");
121 super.doHelp(line);
122 }
123 }