Class Shell

java.lang.Object
com.csse3200.game.ui.terminal.Shell

public class Shell extends Object
Shell: A simple, single-file, dependency-free scripting language interpreter written in vanilla Java.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static 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

    Fields
    Modifier and Type
    Field
    Description
    com.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

    Constructors
    Constructor
    Description
    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 Type
    Method
    Description
    static Object
    and(Object l, Object r)
    Performs a logical AND operation.
    void
    Cleanup the resources used by the shell.
    eval(String source)
    Evaluates a given string of source code.
    boolean
    exists(String name)
    Returns true if a given object exists in the current scope
    forEach(Object obj, com.csse3200.game.ui.terminal.EvaluableFunction function)
    Implements 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.
    ifThen(Object condition, com.csse3200.game.ui.terminal.EvaluableFunction function)
    Implements an if-then construct.
    boolean
    Returns true if the give object is actually a class type
    static boolean
    Determines the "truthiness" of an object, similar to languages like JavaScript or Python.
    static void
    main(String[] args)
    The main function.
    static Object
    Performs a logical NOT operation.
    static Object
    or(Object l, Object r)
    Performs a logical OR operation.
    void
    run()
    Runs the shell's read-evaluation loop.
    setGlobal(String name, Object value)
    Sets a value in the global environment scope.
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • RangeClass

      public static final Class<?> RangeClass
      A reference to the Range class for internal use.
    • ShellMapClass

      public static final Class<?> ShellMapClass
      A reference to the ShellMap class
    • ReturnValueClass

      public static final Class<?> ReturnValueClass
      A reference to the ReturnValue class for internal use.
    • env

      public com.csse3200.game.ui.terminal.Environment env
      The execution environment, holding global variables and the call stack.
  • Constructor Details

    • Shell

      public Shell(Shell.Console console)
      Constructs a new Shell with a given console and a new default environment.
      Parameters:
      console - The console interface for I/O.
    • Shell

      public Shell(Shell.Console console, com.csse3200.game.ui.terminal.Environment env)
      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

      public Object eval(String source)
      Evaluates a given string of source code.
      Parameters:
      source - The source code to evaluate.
      Returns:
      The result of the last evaluated statement.
    • main

      public static void main(String[] args)
      The main function. This is for testing purposes only.
    • toString

      public String toString()
      Provides a string representation of the Shell instance, including its environment.
      Overrides:
      toString in class Object
      Returns:
      A string representation of the shell.
    • and

      public static Object and(Object l, Object r)
      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

      public static Object or(Object l, Object r)
      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

      public static Object not(Object x)
      Performs a logical NOT operation.
      Parameters:
      x - The operand.
      Returns:
      The result of the logical NOT.
    • isTruthy

      public static boolean isTruthy(Object obj)
      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

      public Object ifThen(Object condition, com.csse3200.game.ui.terminal.EvaluableFunction function)
      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

      public Object forEach(Object obj, com.csse3200.game.ui.terminal.EvaluableFunction function)
      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

      public Object setGlobal(String name, Object value)
      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

      public Object getGlobal(String name)
      Gets a value from the global environment scope.
      Parameters:
      name - The name of the global variable.
      Returns:
      The value of the global variable.
    • isClass

      public boolean isClass(Object obj)
      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

      public boolean exists(String name)
      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