mirror of
https://github.com/soconnor0919/nand2tetris-zed.git
synced 2025-12-12 23:24:45 -05:00
- Fixed HDL highlights query syntax error with #match? predicate - Replaced #match? with #any-of? for exact string matching - Fixed Hack Assembly outline query invalid field name - Improved HDL syntax highlighting with comprehensive patterns - Added HDL bracket matching for all syntax types - Fixed XML scope mismatch from text.xml to source.xml - Enhanced outline queries for better code navigation
194 lines
2.9 KiB
Plaintext
194 lines
2.9 KiB
Plaintext
// Test VM file for syntax highlighting
|
|
// This file demonstrates various VM language features
|
|
|
|
// Stack arithmetic operations
|
|
push constant 7
|
|
push constant 8
|
|
add
|
|
push constant 3
|
|
sub
|
|
neg
|
|
|
|
// Logical operations
|
|
push constant 1
|
|
push constant 0
|
|
eq
|
|
push constant 5
|
|
push constant 3
|
|
gt
|
|
push constant 2
|
|
push constant 2
|
|
lt
|
|
|
|
// Bitwise operations
|
|
push constant 15
|
|
push constant 3
|
|
and
|
|
push constant 12
|
|
push constant 5
|
|
or
|
|
not
|
|
|
|
// Memory access operations
|
|
push argument 0
|
|
push argument 1
|
|
add
|
|
pop local 0
|
|
|
|
push local 0
|
|
push constant 1
|
|
add
|
|
pop local 1
|
|
|
|
push static 0
|
|
push static 1
|
|
add
|
|
pop static 2
|
|
|
|
push this 0
|
|
push that 0
|
|
add
|
|
pop this 1
|
|
|
|
push pointer 0
|
|
push pointer 1
|
|
add
|
|
pop temp 0
|
|
|
|
push temp 0
|
|
push temp 1
|
|
add
|
|
pop temp 2
|
|
|
|
// Program flow commands
|
|
label LOOP_START
|
|
push local 0
|
|
push constant 0
|
|
eq
|
|
if-goto LOOP_END
|
|
|
|
push local 0
|
|
push constant 1
|
|
sub
|
|
pop local 0
|
|
|
|
goto LOOP_START
|
|
|
|
label LOOP_END
|
|
|
|
// Function declaration
|
|
function Math.multiply 2
|
|
push constant 0
|
|
pop local 0 // sum = 0
|
|
push constant 0
|
|
pop local 1 // i = 0
|
|
|
|
label MULTIPLY_LOOP
|
|
push local 1
|
|
push argument 1
|
|
eq
|
|
if-goto MULTIPLY_END
|
|
|
|
push local 0
|
|
push argument 0
|
|
add
|
|
pop local 0
|
|
|
|
push local 1
|
|
push constant 1
|
|
add
|
|
pop local 1
|
|
|
|
goto MULTIPLY_LOOP
|
|
|
|
label MULTIPLY_END
|
|
push local 0
|
|
return
|
|
|
|
// Function call
|
|
push constant 5
|
|
push constant 3
|
|
call Math.multiply 2
|
|
pop temp 0
|
|
|
|
// Another function with local variables
|
|
function Fibonacci.compute 3
|
|
push argument 0
|
|
push constant 2
|
|
lt
|
|
if-goto FIBO_BASE_CASE
|
|
|
|
push argument 0
|
|
push constant 1
|
|
sub
|
|
call Fibonacci.compute 1
|
|
pop local 0
|
|
|
|
push argument 0
|
|
push constant 2
|
|
sub
|
|
call Fibonacci.compute 1
|
|
pop local 1
|
|
|
|
push local 0
|
|
push local 1
|
|
add
|
|
return
|
|
|
|
label FIBO_BASE_CASE
|
|
push argument 0
|
|
return
|
|
|
|
// Main function
|
|
function Main.main 0
|
|
push constant 10
|
|
call Fibonacci.compute 1
|
|
pop temp 0
|
|
return
|
|
|
|
// Complex memory operations
|
|
push constant 8000
|
|
pop pointer 1 // that = 8000
|
|
push constant 0
|
|
pop that 0 // that[0] = 0
|
|
push constant 1
|
|
pop that 1 // that[1] = 1
|
|
|
|
push constant 16384
|
|
pop pointer 1 // that = screen base address
|
|
push constant -1
|
|
pop that 0 // blacken first word
|
|
|
|
// Array processing example
|
|
function Array.sum 3
|
|
push constant 0
|
|
pop local 0 // sum = 0
|
|
push constant 0
|
|
pop local 1 // i = 0
|
|
|
|
label SUM_LOOP
|
|
push local 1
|
|
push argument 1 // array length
|
|
eq
|
|
if-goto SUM_END
|
|
|
|
push argument 0 // array base
|
|
push local 1 // index
|
|
add
|
|
pop pointer 1 // that = array[i] address
|
|
push that 0 // array[i] value
|
|
push local 0 // current sum
|
|
add
|
|
pop local 0 // sum += array[i]
|
|
|
|
push local 1
|
|
push constant 1
|
|
add
|
|
pop local 1 // i++
|
|
|
|
goto SUM_LOOP
|
|
|
|
label SUM_END
|
|
push local 0 // return sum
|
|
return
|