fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static interface BASICCommand
  11. {
  12. int excute(String[] args, int index) throws java.lang.Exception;
  13. }
  14.  
  15. static interface BASICFunction
  16. {
  17. ExpreResult excute(String[] args, int index) throws java.lang.Exception;
  18. }
  19.  
  20. static class BASICSyntaxError extends java.lang.Exception
  21. {
  22. BASICSyntaxError(String[] tokens)
  23. {
  24. super("Syntax Error: " + Arrays.toString(tokens));
  25. }
  26. }
  27.  
  28. static enum BASICVarType
  29. {
  30. INT, DBL, STR
  31. }
  32.  
  33. static HashMap<String, BASICCommand> commands = new HashMap<String, BASICCommand>();
  34. static HashMap<String, BASICFunction> functions = new HashMap<String, BASICFunction>();
  35. static HashMap<String, BASICVarType> varident = new HashMap<String, BASICVarType>();
  36. static HashMap<String, Integer> intvars = new HashMap<String, Integer>();
  37. static HashMap<String, Double> dblvars = new HashMap<String, Double>();
  38. static HashMap<String, String> strvars = new HashMap<String, String>();
  39.  
  40. public static void main (String[] args) throws java.lang.Exception
  41. {
  42. // your code goes here
  43. String line;
  44. initCommands();
  45. while ((line = in.readLine()) != null)
  46. {
  47. String[] tokens = line.split(" ");
  48. try
  49. {
  50. if (line.trim().length() > 0)
  51. {
  52. doCommand(tokens, 0);
  53. }
  54. }
  55. catch (BASICSyntaxError ex)
  56. {
  57. System.out.println(ex.getMessage());
  58. }
  59. }
  60. }
  61.  
  62. static void doCommand(String[] tokens, int index) throws java.lang.Exception
  63. {
  64. if (index >= tokens.length)
  65. {
  66. return;
  67. }
  68. BASICCommand command;
  69. while ((command = commands.get(tokens[index].toUpperCase())) != null)
  70. {
  71. index = command.excute(tokens, index);
  72. if (index >= tokens.length)
  73. {
  74. return;
  75. }
  76. if (":".equals(tokens[index]) == false)
  77. {
  78. break;
  79. }
  80. index++;
  81. if (index >= tokens.length)
  82. {
  83. return;
  84. }
  85. }
  86. throw new BASICSyntaxError(tokens);
  87. }
  88.  
  89. static void initCommands()
  90. {
  91. commands.put("LET", new BCLet());
  92. commands.put("PRINT", new BCPrint());
  93. commands.put("REM", new BCRem());
  94. }
  95.  
  96. static BASICVarType varname(String var)
  97. {
  98. if (var.matches("^[A-Z]+$"))
  99. {
  100. return BASICVarType.INT;
  101. }
  102. if (var.matches("^[A-Z]+#$"))
  103. {
  104. return BASICVarType.DBL;
  105. }
  106. if (var.matches("^[A-Z]+[$]$"))
  107. {
  108. return BASICVarType.STR;
  109. }
  110. return null;
  111. }
  112.  
  113. static class ExpreResult
  114. {
  115. int end;
  116. BASICVarType type;
  117. String strret;
  118. int intret;
  119. double dblret;
  120. }
  121.  
  122. static ExpreResult expression(String[] tokens, int index) throws java.lang.Exception
  123. {
  124. String first = tokens[index];
  125. if (first.matches("^[0-9]+$"))
  126. {
  127. System.out.println("int");
  128. }
  129. else if (first.matches("^([0-9]+)?[.][0-9]+$"))
  130. {
  131. System.out.println("dbl");
  132. }
  133. else if (first.matches("^\"[^\"]+\"$"))
  134. {
  135. System.out.println("str");
  136. }
  137. else if ("(".equals(first) == true)
  138. {
  139. System.out.println("(");
  140. }
  141. else
  142. {
  143. first = first.toUpperCase();
  144. BASICFunction function = null;
  145. if ((function = functions.get(first)) != null)
  146. {
  147. function.excute(tokens, index + 1);
  148. }
  149. else if (commands.containsKey(first))
  150. {
  151. throw new BASICSyntaxError(tokens);
  152. }
  153. else
  154. {
  155. BASICVarType type = varname(first);
  156. if (BASICVarType.INT.equals(type))
  157. {
  158.  
  159. }
  160. else if (BASICVarType.DBL.equals(type))
  161. {
  162.  
  163. }
  164. }
  165. }
  166. throw new BASICSyntaxError(tokens);
  167. }
  168.  
  169. static class BCLet implements BASICCommand
  170. {
  171. @Override public int excute(String[] args, int index) throws java.lang.Exception
  172. {
  173. int end = index;
  174. if (args.length - index < 4)
  175. {
  176. throw new BASICSyntaxError(args);
  177. }
  178. if ("=".equals(args[index + 2]) == false)
  179. {
  180. throw new BASICSyntaxError(args);
  181. }
  182. String var = args[index + 1].toUpperCase();
  183. if (commands.containsKey(var))
  184. {
  185. throw new BASICSyntaxError(args);
  186. }
  187. if (functions.containsKey(var))
  188. {
  189. throw new BASICSyntaxError(args);
  190. }
  191. BASICVarType type = varname(var);
  192. if (BASICVarType.INT.equals(type))
  193. {
  194. System.out.println("Integer: " + var);
  195. expression(args, index + 3);
  196. }
  197. else if (BASICVarType.DBL.equals(type))
  198. {
  199. var = var.substring(0, var.length() - 1);
  200. System.out.println("Double: " + var);
  201. expression(args, index + 3);
  202. }
  203. else if (BASICVarType.STR.equals(type))
  204. {
  205. var = var.substring(0, var.length() - 1);
  206. System.out.println("String: " + var);
  207. expression(args, index + 3);
  208. }
  209. else
  210. {
  211. throw new BASICSyntaxError(args);
  212. }
  213. return args.length;
  214. }
  215. }
  216.  
  217. static class BCPrint implements BASICCommand
  218. {
  219. @Override public int excute(String[] args, int index) throws java.lang.Exception
  220. {
  221. String msg = "";
  222. int end = index;
  223. if (args.length - index > 1)
  224. {
  225. expression(args, index + 1);
  226. }
  227. System.out.print(msg);
  228. if (";".equals(args[end]) == false)
  229. {
  230. System.out.println();
  231. }
  232. return args.length;
  233. }
  234. }
  235.  
  236. static class BCRem implements BASICCommand
  237. {
  238. @Override public int excute(String[] args, int index) throws java.lang.Exception
  239. {
  240. return args.length;
  241. }
  242. }
  243. }
Success #stdin #stdout 0.08s 380160KB
stdin
REM 半角スペースを空けることが前提
REM 例
REM LET ABC# = 2.0 * COS ( 60 / PI# ) * SIN ( 60 / PI# )

LET A = 10
LET B# = 12.3
LET C$ = "100"

PRINT A
PRINT B#
PRINT C$
stdout
Integer: A
int
Syntax Error: [LET, A, =, 10]
Double: B
dbl
Syntax Error: [LET, B#, =, 12.3]
String: C
str
Syntax Error: [LET, C$, =, "100"]
Syntax Error: [PRINT, A]
Syntax Error: [PRINT, B#]
Syntax Error: [PRINT, C$]