/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static interface BASICCommand
{
}
static interface BASICFunction
{
}
static class BASICSyntaxError
extends java.
lang.
Exception {
BASICSyntaxError
(String[] tokens
) {
super("Syntax Error: " + Arrays.
toString(tokens
)); }
}
static enum BASICVarType
{
INT, DBL, STR
}
static HashMap
<String, BASICCommand
> commands
= new HashMap
<String, BASICCommand
>(); static HashMap
<String, BASICFunction
> functions
= new HashMap
<String, BASICFunction
>(); static HashMap
<String, BASICVarType
> varident
= new HashMap
<String, BASICVarType
>(); static HashMap
<String, Integer
> intvars
= new HashMap
<String, Integer
>(); static HashMap
<String, Double
> dblvars
= new HashMap
<String, Double
>(); static HashMap
<String, String
> strvars
= new HashMap
<String, String
>();
{
// your code goes here
initCommands();
while ((line = in.readLine()) != null)
{
String[] tokens
= line.
split(" "); try
{
if (line.trim().length() > 0)
{
doCommand(tokens, 0);
}
}
catch (BASICSyntaxError ex)
{
System.
out.
println(ex.
getMessage()); }
}
}
static void doCommand
(String[] tokens,
int index
) throws java.
lang.
Exception {
if (index >= tokens.length)
{
return;
}
BASICCommand command;
while ((command = commands.get(tokens[index].toUpperCase())) != null)
{
index = command.excute(tokens, index);
if (index >= tokens.length)
{
return;
}
if (":".equals(tokens[index]) == false)
{
break;
}
index++;
if (index >= tokens.length)
{
return;
}
}
throw new BASICSyntaxError(tokens);
}
static void initCommands()
{
commands.put("LET", new BCLet());
commands.put("PRINT", new BCPrint());
commands.put("REM", new BCRem());
}
static BASICVarType varname
(String var
) {
if (var.matches("^[A-Z]+$"))
{
return BASICVarType.INT;
}
if (var.matches("^[A-Z]+#$"))
{
return BASICVarType.DBL;
}
if (var.matches("^[A-Z]+[$]$"))
{
return BASICVarType.STR;
}
return null;
}
static class ExpreResult
{
int end;
BASICVarType type;
int intret;
double dblret;
}
static ExpreResult expression
(String[] tokens,
int index
) throws java.
lang.
Exception {
if (first.matches("^[0-9]+$"))
{
}
else if (first.matches("^([0-9]+)?[.][0-9]+$"))
{
}
else if (first.matches("^\"[^\"]+\"$"))
{
}
else if ("(".equals(first) == true)
{
}
else
{
first = first.toUpperCase();
BASICFunction function = null;
if ((function = functions.get(first)) != null)
{
function.excute(tokens, index + 1);
}
else if (commands.containsKey(first))
{
throw new BASICSyntaxError(tokens);
}
else
{
BASICVarType type = varname(first);
if (BASICVarType.INT.equals(type))
{
}
else if (BASICVarType.DBL.equals(type))
{
}
}
}
throw new BASICSyntaxError(tokens);
}
static class BCLet implements BASICCommand
{
@Override
public int excute
(String[] args,
int index
) throws java.
lang.
Exception {
int end = index;
if (args.length - index < 4)
{
throw new BASICSyntaxError(args);
}
if ("=".equals(args[index + 2]) == false)
{
throw new BASICSyntaxError(args);
}
String var
= args
[index
+ 1].
toUpperCase(); if (commands.containsKey(var))
{
throw new BASICSyntaxError(args);
}
if (functions.containsKey(var))
{
throw new BASICSyntaxError(args);
}
BASICVarType type = varname(var);
if (BASICVarType.INT.equals(type))
{
System.
out.
println("Integer: " + var
); expression(args, index + 3);
}
else if (BASICVarType.DBL.equals(type))
{
var = var.substring(0, var.length() - 1);
System.
out.
println("Double: " + var
); expression(args, index + 3);
}
else if (BASICVarType.STR.equals(type))
{
var = var.substring(0, var.length() - 1);
System.
out.
println("String: " + var
); expression(args, index + 3);
}
else
{
throw new BASICSyntaxError(args);
}
return args.length;
}
}
static class BCPrint implements BASICCommand
{
@Override
public int excute
(String[] args,
int index
) throws java.
lang.
Exception {
int end = index;
if (args.length - index > 1)
{
expression(args, index + 1);
}
if (";".equals(args[end]) == false)
{
}
return args.length;
}
}
static class BCRem implements BASICCommand
{
@Override
public int excute
(String[] args,
int index
) throws java.
lang.
Exception {
return args.length;
}
}
}