Package com.csse3200.game.ui.terminal
Class Shell
java.lang.Object
com.csse3200.game.ui.terminal.Shell
Shell: A simple, single-file, dependency-free scripting language interpreter
written in vanilla Java.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
An interface for abstracting read/write operations, allowing the Shell to work with different input/output sources, such as a standard console or a network socket.static class
Represents a numerical range that can be iterated over. -
Field Summary
FieldsModifier and TypeFieldDescriptioncom.csse3200.game.ui.terminal.Environment
The execution environment, holding global variables and the call stack.static final Class
<?> A reference to the Range class for internal use.static final Class
<?> A reference to the ReturnValue class for internal use.static final Class
<?> A reference to the ShellMap class -
Constructor Summary
ConstructorsConstructorDescriptionShell
(Shell.Console console) Constructs a new Shell with a given console and a new default environment.Shell
(Shell.Console console, com.csse3200.game.ui.terminal.Environment env) Constructs a new Shell with a specified console and a pre-existing environment. -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
Performs a logical AND operation.void
close()
Cleanup the resources used by the shell.Evaluates a given string of source code.boolean
Returns true if a given object exists in the current scopeImplements a for-each loop construct that iterates over various iterable types.Gets a value from the global environment scope.ifElse
(Object condition, com.csse3200.game.ui.terminal.EvaluableFunction ifFunction, com.csse3200.game.ui.terminal.EvaluableFunction elseFunction) Implements an if-else construct.Implements an if-then construct.boolean
Returns true if the give object is actually a class typestatic boolean
Determines the "truthiness" of an object, similar to languages like JavaScript or Python.static void
The main function.static Object
Performs a logical NOT operation.static Object
Performs a logical OR operation.void
run()
Runs the shell's read-evaluation loop.Sets a value in the global environment scope.toString()
Provides a string representation of the Shell instance, including its environment.tryCatch
(com.csse3200.game.ui.terminal.EvaluableFunction tryBlock, com.csse3200.game.ui.terminal.EvaluableFunction catchBlock) Implements a try-catch construct for error handling.whileLoop
(com.csse3200.game.ui.terminal.EvaluableFunction condition, com.csse3200.game.ui.terminal.EvaluableFunction function) Implements a while loop construct.
-
Field Details
-
RangeClass
A reference to the Range class for internal use. -
ShellMapClass
A reference to the ShellMap class -
ReturnValueClass
A reference to the ReturnValue class for internal use. -
env
public com.csse3200.game.ui.terminal.Environment envThe execution environment, holding global variables and the call stack.
-
-
Constructor Details
-
Shell
Constructs a new Shell with a given console and a new default environment.- Parameters:
console
- The console interface for I/O.
-
Shell
Constructs a new Shell with a specified console and a pre-existing environment.- Parameters:
console
- The console interface for I/O.env
- The execution environment to use.
-
-
Method Details
-
run
public void run()Runs the shell's read-evaluation loop. -
close
public void close()Cleanup the resources used by the shell. -
eval
Evaluates a given string of source code.- Parameters:
source
- The source code to evaluate.- Returns:
- The result of the last evaluated statement.
-
main
The main function. This is for testing purposes only. -
toString
Provides a string representation of the Shell instance, including its environment. -
and
Performs a logical AND operation.- Parameters:
l
- The left-hand side operand.r
- The right-hand side operand.- Returns:
- The result of the logical AND.
-
or
Performs a logical OR operation.- Parameters:
l
- The left-hand side operand.r
- The right-hand side operand.- Returns:
- The result of the logical OR.
-
not
Performs a logical NOT operation.- Parameters:
x
- The operand.- Returns:
- The result of the logical NOT.
-
isTruthy
Determines the "truthiness" of an object, similar to languages like JavaScript or Python. Used to coerce objects to booleans.- Parameters:
obj
- The object to evaluate.- Returns:
- false if the object is null, a zero number, an empty string/collection, or Boolean false. Returns true otherwise.
-
ifThen
Implements an if-then construct. Executes the function if the condition is truthy.- Parameters:
condition
- The condition to check.function
- The function to execute if the condition is true.- Returns:
- The result of the function, or null if the condition was false.
-
ifElse
public Object ifElse(Object condition, com.csse3200.game.ui.terminal.EvaluableFunction ifFunction, com.csse3200.game.ui.terminal.EvaluableFunction elseFunction) Implements an if-else construct.- Parameters:
condition
- The condition to check.ifFunction
- The function to execute if the condition is true.elseFunction
- The function to execute if the condition is false.- Returns:
- The result of the executed function.
-
forEach
Implements a for-each loop construct that iterates over various iterable types.- Parameters:
obj
- The object to iterate over (can be an Iterator, Collection, Array, or Map).function
- The function to execute for each item.- Returns:
- null after the loop completes.
- Throws:
com.csse3200.game.ui.terminal.ShellException
- if the object is not iterable.
-
whileLoop
public Object whileLoop(com.csse3200.game.ui.terminal.EvaluableFunction condition, com.csse3200.game.ui.terminal.EvaluableFunction function) Implements a while loop construct.- Parameters:
condition
- The condition to evaluate before each iteration.function
- The function to execute in the loop body.- Returns:
- The result of the last executed statement in the loop.
-
tryCatch
public Object tryCatch(com.csse3200.game.ui.terminal.EvaluableFunction tryBlock, com.csse3200.game.ui.terminal.EvaluableFunction catchBlock) Implements a try-catch construct for error handling.- Parameters:
tryBlock
- The function containing code that might throw an exception.catchBlock
- The function to execute if a ShellException is caught.- Returns:
- The result of the try block, or the result of the catch block if an exception occurred.
-
setGlobal
Sets a value in the global environment scope.- Parameters:
name
- The name of the global variable.value
- The value to set.- Returns:
- The value that was set.
-
getGlobal
Gets a value from the global environment scope.- Parameters:
name
- The name of the global variable.- Returns:
- The value of the global variable.
-
isClass
Returns true if the give object is actually a class type- Parameters:
obj
- the object to be tested- Returns:
- true if obj is a class, false otherwise
-
exists
Returns true if a given object exists in the current scope- Parameters:
name
- the name to look for in the environment- Returns:
- true if the object exists
-