ThomasScript source code
Because ThomasScript isn’t finished yet and I really am not worried about licensing it because it takes too much time (for my liking) I have decided to just put it here, on my blog. Because it doesn’t have a license I have decided to invent the following:
- If there are compiling errors, tell me
- If there are running errors, tell me
- If you computer (or anything else) is damaged because of the source code don’t tell me – it can be your fault for downloading it
import java.io.*;
import javax.swing.*;
import java.util.*;
class ThomasScript
{
public String[] variables = new String[10000];
public String[] values = new String[10000];
public int next = 0;
public Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("Welcome to ThomasScript");
System.out.println("Recognized request from: " + args[0]);
File source = new File(args[0]);
if (source.exists())
{
System.out.println("Can confirm that the source exists");
try
{
FileReader fr = new FileReader(args[0]);
String code = "";
BufferedReader buffer = new BufferedReader(fr);
String line = "";
String[] lines = new String[100000]; //CHANGE NUMBER TO NUMBER OF LINES IN FILE!!
int nextone = 0;
while ((line = buffer.readLine()) != null)
{
lines[nextone] = line;
nextone++;
}
ThomasScript ts = new ThomasScript();
ts.runCode(lines);
}
catch (IOException e)
{
System.out.println("Sorry, some sort of error occured.");
}
}
else System.out.println("Sorry, file doesn't exist");
}
void runCode(String[] lines)
{
ThomasScript ts = new ThomasScript();
System.out.println("Running code");
System.out.println("-------------------------------------");
System.out.println("");
int max = 0;
//Identifies the max number, temporary code only
for (int a = 0; a < lines.length; a++)
{
if (lines[a] == null)
{
max = a;
break;
}
}
int a = 0;
while (true)
{
if (lines[a] == null) break;
String[] components = lines[a].trim().split(" ");
String parts = "";
for (int b = 1; b < components.length; b++) parts += components[b] + " ";
if (components[0].equals("write"))
{
String[] args = getActualStringValues(parts);
for (int b = 0; b < args.length; b++)
{
if (args[b] == null) break;
System.out.print(args[b]);
}
System.out.print("\n");
}
else if (components[0].equals("msg"))
{
String[] args = getActualStringValues(parts);
String fullmessage = "";
for (int b = 0; b < args.length; b++)
{
if (args[b] == null) break;
fullmessage += args[b];
}
JOptionPane.showMessageDialog(null, fullmessage);
System.out.print("\n");
}
else if (components[0].equals("dim"))
{
for (int b = 1; b < components.length; b++)
{
variables[next] = components[b];
values[next] = "";
next++;
}
}
else if (components[0].equals("set"))
{
String toGive = "";
for (int b = 3; b < components.length; b++) toGive += components[b] + " ";
toGive = toGive.trim();
try
{
int index = 0;
for (int b = 0; b < variables.length; b++)
{
if (variables[b] != null)
{
if (variables[b].equals(components[1]))
{
index = b;
break;
}
}
}
if (components[2].equals("="))
{
String[] valueOut = getActualStringValues(toGive);
if (valueOut[0] == null) valueOut[0] = toGive;
values[index] = valueOut[0];
}
else if (components[2].equals("+"))
{
if (toGive.charAt(0) == '"')
{
String[] valueOut = getActualStringValues(toGive);
values[index] += valueOut[0];
}
else if (toGive.charAt(0) == '$')
{
if (couldBeInt(getVariableValue(toGive)) && couldBeInt(getVariableValue(components[1])))
{
int result = (Integer.parseInt(getVariableValue(components[1])) + Integer.parseInt(getVariableValue(toGive)));
values[index] = "" + result;
}
else
{
values[index] += getVariableValue(toGive);
}
}
else if (couldBeInt(toGive))
{
int result = (Integer.parseInt(getVariableValue(components[1])) + Integer.parseInt(toGive));
values[index] = "" + result;
}
}
else if (components[2].equals("-"))
{
if (toGive.charAt(0) == '"')
{
String[] valueOut = getActualStringValues(toGive);
values[index] += valueOut[0];
}
else if (toGive.charAt(0) == '$')
{
if (couldBeInt(getVariableValue(toGive)) && couldBeInt(getVariableValue(components[1])))
{
int result = (Integer.parseInt(getVariableValue(components[1])) - Integer.parseInt(getVariableValue(toGive)));
values[index] = "" + result;
}
else
{
values[index] += getVariableValue(toGive);
}
}
else if (couldBeInt(toGive))
{
int result = (Integer.parseInt(getVariableValue(components[1])) - Integer.parseInt(toGive));
values[index] = "" + result;
}
}
else if (components[2].equals("*"))
{
if (toGive.charAt(0) == '"')
{
String[] valueOut = getActualStringValues(toGive);
values[index] += valueOut[0];
}
else if (toGive.charAt(0) == '$')
{
if (couldBeInt(getVariableValue(toGive)) && couldBeInt(getVariableValue(components[1])))
{
int result = (Integer.parseInt(getVariableValue(components[1])) * Integer.parseInt(getVariableValue(toGive)));
values[index] = "" + result;
}
else
{
values[index] += getVariableValue(toGive);
}
}
else if (couldBeInt(toGive))
{
int result = (Integer.parseInt(getVariableValue(components[1])) * Integer.parseInt(toGive));
values[index] = "" + result;
}
}
else if (components[2].equals("/"))
{
if (toGive.charAt(0) == '"')
{
String[] valueOut = getActualStringValues(toGive);
values[index] += valueOut[0];
}
else if (toGive.charAt(0) == '$')
{
if (couldBeInt(getVariableValue(toGive)) && couldBeInt(getVariableValue(components[1])))
{
int result = (Integer.parseInt(getVariableValue(components[1])) / Integer.parseInt(getVariableValue(toGive)));
values[index] = "" + result;
}
else
{
values[index] += getVariableValue(toGive);
}
}
else if (couldBeInt(toGive))
{
int result = (Integer.parseInt(getVariableValue(components[1])) / Integer.parseInt(toGive));
values[index] = "" + result;
}
}
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
else if (components[0].equals("get"))
{
int index = -1;
for (int i = 0; i < variables.length; i++)
{
if (variables[i].equals(components[1]))
{
index = i;
break;
}
}
if (index == -1) System.out.println("Variable not declared");
else
{
String prompt = "";
for (int i = 2; i < components.length; i++) prompt += components[i] + " ";
String[] prompts = getActualStringValues(prompt);
prompt = "";
for (int i = 0; i < prompts.length; i++)
{
if (prompts[i] == null) break;
prompt += prompts[i];
}
System.out.println(prompt);
String input = in.nextLine();
values[index] = input;
}
}
else if (components[0].equals("getnum"))
{
int index = -1;
for (int i = 0; i < variables.length; i++)
{
if (variables[i].equals(components[1]))
{
index = i;
break;
}
}
if (index == -1) System.out.println("Variable not declared");
else
{
String prompt = "";
for (int i = 2; i < components.length; i++) prompt += components[i] + " ";
String[] prompts = getActualStringValues(prompt);
prompt = "";
for (int i = 0; i < prompts.length; i++)
{
if (prompts[i] == null) break;
prompt += prompts[i];
}
System.out.println(prompt);
int input = 0;
input = in.nextInt();
values[index] = "" + input;
}
}
else if (components[0].equals("goto"))
{
if (couldBeInt(components[1]))
{
a = Integer.parseInt(components[1]) - 2;
}
}
else if (components[0].equals("exit") || components[0].equals("quit")) break;
else if (components[0].equals("if"))
{
String $var1 = getActualStringValues(components[1])[0];
String $var2 = getActualStringValues(components[3])[0];
int var1 = 0, var2 = 0;
boolean var1true = false, var2true = false;
if (couldBeInt($var1))
{
var1 = Integer.parseInt($var1);
var1true = true;
}
if (couldBeInt($var2))
{
var2 = Integer.parseInt($var2);
var2true = true;
}
if (components[2].equals("="))
{
if ($var1.equals($var2) && couldBeInt(components[4])) a = Integer.parseInt(components[4]) - 2;
}
else if (var1true && var2true)
{
if (components[2].equals(">"))
{
if (var1 > var2 && couldBeInt(components[4])) a = Integer.parseInt(components[4]) - 2;
}
else if (components[2].equals("<"))
{
if (var1 < var2 && couldBeInt(components[4])) a = Integer.parseInt(components[4]) - 2;
}
else if (components[2].equals(">="))
{
if (var1 >= var2 && couldBeInt(components[4])) a = Integer.parseInt(components[4]) - 2;
}
else if (components[2].equals("<="))
{
if (var1 <= var2 && couldBeInt(components[4])) a = Integer.parseInt(components[4]) - 2;
}
else if (components[2].equals("!="))
{
if (var1 != var2 && couldBeInt(components[4])) a = Integer.parseInt(components[4]) - 2;
}
}
}
else System.out.println("Unrecognized command on line " + (a+1));
a++;
}
System.out.println("\n-------------------------------------\nRun complete");
}
String[] getActualStringValues(String parts)
{
parts = parts.trim();
String[] returnOfTheString = new String[parts.length()];
boolean inString = false;
boolean inVariable = false;
int position = -1;
boolean started = false;
for (int a = 0; a < parts.length(); a++)
{
char c = parts.charAt(a);
if (c == '"' && !inString)
{
position++;
inString = true;
}
else if (c == '"' && inString)
{
inString = false;
}
else if (inString)
{
if (returnOfTheString[position] == null) returnOfTheString[position] = "";
returnOfTheString[position] += c;
}
else if (c == '$' && !inVariable)
{
position++;
inVariable = true;
returnOfTheString[position] = "$";
}
else if ((c == ' ' || a == parts.length()) && inVariable)
{
returnOfTheString[position] = getVariableValue(returnOfTheString[position]);
inVariable = false;
}
else if (inVariable)
{
returnOfTheString[position] += c;
}
else
{
}
}
if (inVariable)
{
returnOfTheString[position] = getVariableValue(returnOfTheString[position]);
}
return returnOfTheString;
}
public String getVariableValue(String name)
{
int index = 0;
for (int a = 0; a < variables.length; a++)
{
if (variables[a] != null)
{
if (variables[a].equals(name))
{
index = a;
break;
}
}
}
return values[index];
}
boolean couldBeInt(String value)
{
value = value.trim();
for (int a = 0; a < value.length(); a++)
{
char c = value.charAt(a);
if (c != '0' && c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9' && c != '.' && c != '-')
{
return false;
}
}
return true;
}
}