Fix HDL and Hack Assembly syntax highlighting and queries

- 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
This commit is contained in:
2025-09-11 11:24:24 -04:00
commit c231dbfd27
133 changed files with 2792 additions and 0 deletions

20
examples/And.hdl Normal file
View File

@@ -0,0 +1,20 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/01/And.hdl
/**
* And gate:
* out = 1 if (a == 1 and b == 1)
* 0 otherwise
*/
CHIP And {
IN a, b;
OUT out;
PARTS:
// Put your code here:
Nand(a=a, b=b, out=nandOut);
Not(in=nandOut, out=out);
}

View File

@@ -0,0 +1,107 @@
// Enhanced Hack Assembly Test File
// Demonstrates all the improved highlighting features
// Test A-instructions with various symbol types
@R0 // Virtual register
@R15 // Virtual register
@SP // Stack pointer
@LCL // Local pointer
@ARG // Argument pointer
@THIS // This pointer
@THAT // That pointer
@SCREEN // Screen memory map
@KBD // Keyboard memory map
@userVar // User-defined variable
@LOOP // User-defined label
// Test constants (including negative)
@0 // Zero
@1 // One
@-1 // Negative one
@32767 // Max positive
@-32768 // Min negative
@42 // Random positive
@-42 // Random negative
// Test C-instructions with all destination combinations
D=A // D register
A=M // A register
M=D // M register
MD=A // M and D
AM=D // A and M
AD=M // A and D
AMD=A // All three
// Test computation field operations
D=0 // Zero
D=1 // One
D=-1 // Negative one
D=D // D register
D=!D // Not D
D=-D // Negate D
D=D+1 // Increment D
D=D-1 // Decrement D
D=A // A register
D=!A // Not A
D=-A // Negate A
D=A+1 // Increment A
D=A-1 // Decrement A
D=M // M register
D=!M // Not M
D=-M // Negate M
D=M+1 // Increment M
D=M-1 // Decrement M
D=D+A // D plus A
D=D-A // D minus A
D=A-D // A minus D
D=D&A // D and A
D=D|A // D or A
D=D+M // D plus M
D=D-M // D minus M
D=M-D // M minus D
D=D&M // D and M
D=D|M // D or M
// Test jump conditions
D;JGT // Jump if greater than
D;JEQ // Jump if equal
D;JGE // Jump if greater or equal
D;JLT // Jump if less than
D;JNE // Jump if not equal
D;JLE // Jump if less or equal
D;JMP // Unconditional jump
// Test label declarations
(MAIN_LOOP)
(END_PROGRAM)
(ERROR_HANDLER)
// Test complex program structure
@MAIN_LOOP
D;JGT
@END_PROGRAM
0;JMP
(MAIN_LOOP)
@SCREEN
D=A
@pointer
M=D
@KBD
D=M
@END_PROGRAM
D;JEQ
@pointer
A=M
M=-1
@pointer
M=M+1
@MAIN_LOOP
0;JMP
(END_PROGRAM)
0;JMP

33
examples/EnhancedHDL.hdl Normal file
View File

@@ -0,0 +1,33 @@
/**
* Enhanced HDL Test File
* Demonstrates all the new highlighting features
*/
CHIP EnhancedTest {
// Array notation support
IN a[16], b[8], c;
OUT out[16], flag, result[4];
PARTS:
// Boolean literals in connections
Mux16(a=a, b=false, sel=c, out=temp);
// Bit range operations
Add16(a=a[0..7], b=b, out=out[0..7]);
Add16(a=a[8..15], b=false, out=out[8..15]);
// Complex array operations
Not16(in=a, out=notA);
And16(a=notA, b=true, out=result);
// Single bit operations
And(a=a[0], b=b[0], out=flag);
// Mixed operations with ranges
Mux4Way16(a=a[0..3], b=a[4..7], c=a[8..11], d=a[12..15],
sel=c, out=out[0..3]);
}

129
examples/Math.jack Normal file
View File

@@ -0,0 +1,129 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/12/Math.jack
/**
* A library of commonly used mathematical functions.
* Note: Jack compilers implement multiplication and division using OS method calls.
*/
class Math {
static Array twoToThe;
/** Initializes the library. */
function void init() {
var int i;
let twoToThe = Array.new(16);
let twoToThe[0] = 1;
let i = 1;
while (i < 16) {
let twoToThe[i] = twoToThe[i-1] + twoToThe[i-1];
let i = i + 1;
}
return;
}
/** Returns the absolute value of x. */
function int abs(int x) {
if (x < 0) {
return -x;
}
else {
return x;
}
}
/** Returns the product of x and y.
* When a Jack compiler detects the multiplication operator '*' in the
* program's code, it handles it by invoking this method. In other words,
* the Jack expressions x*y and multiply(x,y) return the same value.
*/
function int multiply(int x, int y) {
var int sum, shiftedX, i;
let sum = 0;
let shiftedX = x;
let i = 0;
while (i < 16) {
if (~((y & twoToThe[i]) = 0)) {
let sum = sum + shiftedX;
}
let shiftedX = shiftedX + shiftedX;
let i = i + 1;
}
return sum;
}
/** Returns the integer part of x/y.
* When a Jack compiler detects the multiplication operator '/' in the
* program's code, it handles it by invoking this method. In other words,
* the Jack expressions x/y and divide(x,y) return the same value.
*/
function int divide(int x, int y) {
var int neg_x, neg_y;
var int q;
var int result;
let neg_x = x < 0;
let neg_y = y < 0;
let x = Math.abs(x);
let y = Math.abs(y);
if (y > x) {
return 0;
}
let q = Math.divide(x, y + y);
if ((x - ((q + q) * y)) < y) {
let result = q + q;
}
else {
let result = q + q + 1;
}
if (neg_x = neg_y) {
return result;
}
else {
return -result;
}
}
/** Returns the integer part of the square root of x. */
function int sqrt(int x) {
var int y, j;
var int approx;
var int approx_squared;
let y = 0;
let j = 7; // (16 / 2) - 1
while (~(j < 0)) {
let approx = y + twoToThe[j];
let approx_squared = approx * approx;
if (~(approx_squared > x) & (approx_squared > 0)) {
let y = approx;
}
let j = j - 1;
}
return y;
}
/** Returns the greater number. */
function int max(int a, int b) {
if (a > b) {
return a;
}
else {
return b;
}
}
/** Returns the smaller number. */
function int min(int a, int b) {
if (a < b) {
return a;
}
else {
return b;
}
}
}

106
examples/test.asm Normal file
View File

@@ -0,0 +1,106 @@
// Test Hack Assembly file for syntax highlighting
// This file demonstrates various Hack Assembly features
// Simple A-instruction with constant
@17
D=A
// A-instruction with symbol
@sum
M=D
// C-instruction with all parts: dest=comp;jump
@i
D=M
@LOOP
D;JGT
// Label declaration
(LOOP)
// Various computation operations
@sum
D=M
@i
D=D+M
@sum
M=D
// Memory operations
@R0
D=M
@temp
M=D
// Predefined symbols
@SP
M=M+1
@LCL
D=M
@ARG
M=D
// Screen and keyboard
@SCREEN
D=A
@KBD
M=M+1
// Complex computations
D=D+1
D=D-1
D=!D
D=-D
A=D+A
M=D&A
D=D|M
// Jump conditions
@END
0;JMP
@value
D=M
@POSITIVE
D;JGT
@ZERO
D;JEQ
@NEGATIVE
D;JLT
(POSITIVE)
// Positive value handling
@1
D=A
@result
M=D
@END
0;JMP
(ZERO)
// Zero value handling
@0
D=A
@result
M=D
@END
0;JMP
(NEGATIVE)
// Negative value handling
@-1
D=A
@result
M=D
(END)
// Infinite loop
@END
0;JMP
// Variable declarations (will be resolved by assembler)
@counter
@total
@average

5
examples/test.cmp Normal file
View File

@@ -0,0 +1,5 @@
| a | b | out |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |

97
examples/test.tst Normal file
View File

@@ -0,0 +1,97 @@
// Test script file for syntax highlighting
// This file demonstrates various test script features
load CPU.hdl,
output-file CPU.out,
compare-to CPU.cmp,
// Set up output format
output-list time%S1.3.1 inM%D0.6.0 instruction%B0.16.0 reset%B2.1.2 outM%D1.6.0 writeM%B3.1.2 addressM%D1.5.1 pc%D0.5.0 DRegister[]%D1.7.1;
// Test A-instruction: @12345
set instruction %B0011000000111001,
tick, output, tock, output;
// Test C-instruction: D=A
set instruction %B1110110000010000,
tick, output, tock, output;
// Test memory access
set instruction %B0101101110100000, // @23456
tick, output, tock, output;
set instruction %B1110000111110000, // AD=A-D
tick, output, tock, output;
// Test RAM operations
set RAM[0] 256,
set RAM[1] 0,
set RAM[2] 100,
repeat 10 {
ticktock;
output;
}
// Test conditional operations
set instruction %B0000001111101011, // @1003
tick, output, tock, output;
set instruction %B1110001100001000, // M=D
tick, output, tock, output;
// Test jump operations
set instruction %B0000000000001110, // @14
tick, output, tock, output;
set instruction %B1110001100000100, // D;jlt
tick, output, tock, output;
// While loop test
while RAM[0] > 0 {
set RAM[0] RAM[0] - 1,
ticktock;
output;
}
// Reset test
set reset 1;
tick, output, tock, output;
set instruction %B0111111111111111, // @32767
set reset 0;
tick, output, tock, output;
// Binary instruction tests
set instruction %B1111110111100000, // A=M+1
tick, output, tock, output;
set instruction %B1110001100101000, // AM=D
tick, output, tock, output;
// Test all jump conditions
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
// Final output
output;
echo "Test completed successfully";

193
examples/test.vm Normal file
View File

@@ -0,0 +1,193 @@
// 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