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.