A toy programming language and compiler built on top of the Hack Computer
LavaScript translates .ls files into Hack assembly,
assembles them into binary, and runs them on a custom CPU emulator
that operates on top of the Hack ALU.
Inspired by the legendary Nand2Tetris course,
extended with ✨ new features ✨ such as assignments, conditions, loops,
character printing, and even a quirky Maybe random condition.
- 📝 Variables with automatic memory allocation
- ➕ Arithmetic operators:
+,-,*,/ - 🔀 Logical operators:
&,| - 🔎 Comparisons:
==,!=,>,<,>=,<= - 🔁 Control flow:
whileloops (supportsTrue,False, andMaybefor random branching)ifconditionalsfor (init; condition; increment)loops
- 🖨️ Printing:
print(x)→ print numberprintc('A')orprintc(x)→ print ASCII character
- 🔡 Character literals automatically converted to ASCII codes
- ⚙️ Compiler → Hack assembly
- 🛠️ Assembler → machine code
- 💻 CPU emulator executes the machine code with extended print opcodes:
01→ print number (decimal)10→ print ASCII character11→ no print (default, backward compatible)
Clone the repository, then run:
./melt examples/example.ls
to compile and execute a LavaScript program.
Development
Add new syntax → parser.py
Implement new operations → compiler.py
Update opcode translation → assembler.py
Extend CPU behavior → cpu.py
Roadmap
<p>Functions and subroutines</p> <p>Strings and arrays</p> <p>Additional operators (like % and logical not)</p> <p>File I/O</p>
License
This project is just for learning and fun. Do whatever you want.