16
\$\begingroup\$

In honor of Star Wars day, I've put together this small Python program I'm calling JediScript. JediScript is essentially a scrapped-down version of BrainFuck without input or looping. Here are the commands in JediScript.

  • SlashWithSaber: Move forward on the tape.
  • ParryBladeWithSaber: Move backward on the tape.
  • StabWithSaber: Increment a cell.
  • BlockBladeWithSaber: Decrement cell.
  • UseForceWithHands: Output the current cell.

Each command is semicolon ; separated, like so: StabWithSaber;UseForceWithHands. Here's an example input. This will output the character p.

StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;StabWithSaber;UseForceWithHands;

This is something that I threw together in about 20 minutes, so it's not the greatest, but I'd still appreciate a review.

#!/usr/bin/env python
"""
JediScript, BrainFuck for Star Wars.
May the 4th be with you.
"""


DEFAULT_SPLIT = ";"


"""
Variables below this are environment
variables for the user to modify.
"""
data_tape = [0 for _ in range(256)]
tape_pos  = 0


def increment_cell():
    global data_tape
    global tape_pos
    data_tape[tape_pos] += 1 if data_tape[tape_pos] <= 127 else 0


def decrement_cell():
    global data_tape
    global tape_pos
    data_tape[tape_pos] -= 1 if data_tape[tape_pos] >= 0 else 0


def move_forward():
    global tape_pos
    tape_pos += 1 if tape_pos <= len(data_tape) - 1 else 0


def move_backward():
    global tape_pos
    tape_pos -= 1 if tape_pos >= 0 else 0


def output_cell():
    print chr(data_tape[tape_pos])


"""
Dictionary contains tokens that reference
their relevant functions.
"""
TOKENS = {
    "SlashWithSaber": move_forward,
    "ParryBladeWithSaber": move_backward,
    "StabWithSaber": increment_cell,
    "BlockBladeWithSaber": decrement_cell,
    "UseForceWithHands": output_cell,
}


def execute_commands(tokenized_string):
    """
    Executes commands from the tokenized string.
    tokenized_string - The tokenized string
    """
    for token in tokenized_string:
        if token in TOKENS:
            TOKENS[token]()


def tokenize_input(string):
    """
    Tokenize a string into it's the form [ token, ... ]
    string - The string to tokenize.
    """
    string = string.replace(" ", "")
    string = string.split(DEFAULT_SPLIT)
    return string


def get_user_input(prompt):
    """
    Get input from the user.
    prompt - The prompt to be used.
    """
    while True:
        string = raw_input(prompt)
        string = tokenize_input(string)
        execute_commands(string)


if __name__ == "__main__":
    get_user_input("JediScript $ ")
\$\endgroup\$
2
  • 3
    \$\begingroup\$ About your language: I guess new-line separated is easier to read. \$\endgroup\$ May 4, 2015 at 19:05
  • \$\begingroup\$ I am not sure if I am missing something, but what does the spherical notes do? Other than error? \$\endgroup\$
    – user95591
    May 11, 2016 at 14:40

1 Answer 1

11
\$\begingroup\$
  • Why do you allow each cell of the tape to hold numbers from -1 to 128? seems like an odd range.
  • in move_backward() why do you allow the tape to reach position -1?
  • in move_forward() why do you allow the tape's position to be beyond the end of the tape?
  • In general you should be using exclusive comparisons (without the =) as you'll make fewer mistakes.
\$\endgroup\$
4
  • 4
    \$\begingroup\$ I don't understand why using exclusive comparisons would make fewer mistakes ? \$\endgroup\$
    – Marc-Andre
    May 4, 2015 at 17:16
  • 4
    \$\begingroup\$ I don't either, but there's actually research on it. Coders who use an exclusive comparison for the top-end of the loop or the range make fewer off-by-one errors than coders who use the "-or-equal-to" comparators. \$\endgroup\$
    – Snowbody
    May 4, 2015 at 17:33
  • 6
    \$\begingroup\$ I would be interested in the link to that research! \$\endgroup\$
    – Marc-Andre
    May 4, 2015 at 17:36
  • 1
    \$\begingroup\$ Searched a while and all I came up with was cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html I'll keep looking \$\endgroup\$
    – Snowbody
    May 4, 2015 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.