Chopped is an expressive, natural-language-inspired interpreter built in Java. It is designed to be readable, almost like writing instructions in a kitchen, while maintaining the logic of a structured programming language.
- Java Development Kit (JDK) 8 or higher.
-
Clone and Compile:
git clone https://github.com/matthewl580/Chopped.git cd chopped javac chopped.java -
Run a Script:
chopped > say "You're cooked lowkey"Chopped uses set [var] to [value] syntax. It supports numbers, strings, and expressions.
- Syntax:
set identifier to expression - Example: ```chopped set apples to 5 set total to apples * 2 say total
The say command is used for printing. It features "smart punctuation" handling—if you end a string with a period, exclamation, or question mark, Chopped ensures the formatting remains clean.
- Example: ```chopped say "Hello world!" say 100 + (50 / 2)
Chopped supports robust if statements with optional otherwise (else) blocks.
- Keywords:
if,then,do,otherwise - Operators:
==,!=,<,> - Example:
set heat to 200
if (heat > 180) then do say "Too hot!" otherwise say "Just right."
Repeating actions is straightforward with the for loop.
- Syntax:
for [number] times [statement] - Example:
set count to 3
for count times say "Chopping..."
You can combine strings and logic in a single line using the inline if syntax within a say command:
set ingredients to 10
say "Inventory full" if ingredients == 10 otherwise say "Keep gathering."
Chopped supports standard order of operations and parenthetical grouping:
set x to (10 + 5) * 2
set y to 100 / (x - 20)
say y
Organize your code by splitting logic into multiple files:
// main.chopped
set version to 1.2
chopped "header.chopped"
say "Loading version..."
say version
The interpreter is built as a single-pass processing system contained within the chopped class.
The Lexer processes input character-by-character. It does not rely on whitespace to separate tokens, allowing for compact expressions like x=10+y. It categorizes input into NUMBER, STRING, KEYWORD, OPERATOR, or IDENTIFIER.
The Parser uses a recursive descent approach to evaluate logic:
- Expressions: Handles addition and subtraction.
- Terms: Handles multiplication and division.
- Factors: Handles the lowest level units (numbers, variables, or parenthesized expressions).
Variables are stored in a HashMap<String, Object>, allowing for dynamic typing where a variable can hold a Double or a String.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request