Project archive / 09

Language Parser & Matrix Tool

Matrix Calculator with Matlab-like Syntax

I made a matrix calculator, implementing the recursive descent parser and C language. It's able to perform various matrix operations, and I play Corway's game of life with it!

Matrix Calculator with Matlab-like Syntax project cover
Archive
09
Category
Language Parser & Matrix Tool
Result
Native language parser and matrix runtime

System / highlights

What I delivered.

  1. 01

    Implemented a recursive-descent parser for a Matlab-like command syntax.

  2. 02

    Built matrix creation, transformation, and calculation operations in native C.

  3. 03

    Used the matrix engine to run and visualize Conway's Game of Life.

Archive / full note

Project notes.

Originally published Jul 15, 2022

A native C matrix calculator with a Matlab-like language and recursive-descent parser.

Introduction

Matrix Calculator is simple calculator that can perform various matrix operations. It is developed using the C programming language and a recursive descent parser. The calculator provides efficient matrix operations, enabling users to perform various computations. The calculator can perform the following operations:

Codec
1void SET_U_NOT(var *opvar); 2void SET_U_EIGHTCOUNT(var *opvar); 3void SET_B_AND(var *ov1, var *ov2); 4void SET_B_OR(var *ov1, var *ov2); 5void SET_B_GREATER(var *ov1, var *ov2); 6void SET_B_LESS(var *ov1, var *ov2); 7void SET_B_ADD(var *ov1, var *ov2); 8void SET_B_TIMES(var *ov1, var *ov2); 9void SET_B_EQUALS(var *ov1, var *ov2);

CREATE and PRINT

The calculator can create and print matrices, also read matrix from local file. The following Matlab-like code creates a 1 matrix and prints it.

input:

Codetxt
1BEGIN { 2 ONES 1 5 $A 3 READ "test/example/Hblock.arr" $B 4 PRINT "array_A:" 5 PRINT $A 6 PRINT "array_B:" 7 PRINT $B 8}

output:

Codetxt
1array_A: 21 1 1 1 1 3array_B: 40 0 0 0 0 50 1 0 1 0 60 1 1 1 0 70 1 0 1 0 80 0 0 0 0

Overwrite the varibles

input:

Codetxt
1BEGIN { 2 ONES 1 5 $A 3 PRINT "array_A:" 4 PRINT $A 5 READ "test/example/Hblock.arr" $A 6 PRINT "array_A:" 7 PRINT $A 8 ONES 1 1 $A 9 PRINT "also_array_A:" 10 PRINT $A 11}

output:

Codetxt
1array_A: 21 1 1 1 1 3array_A: 40 0 0 0 0 50 1 0 1 0 60 1 1 1 0 70 1 0 1 0 80 0 0 0 0 9Also_array_A: 101

Loop

Nested Loop is also supported. input:

Codetxt
1BEGIN { 2 SET $A := 0 ; 3 LOOP $I 3 { 4 LOOP $J 3 { 5 SET $A := $I $J B-TIMES ; 6 PRINT $A 7 } 8 } 9 PRINT $I 10 PRINT "DONE" 11}

output:

Codetxt
1output: 21 32 43 52 64 76 83 96 109 113 12DONE

Interate loop counter (works like for loop in a assembly way)

input:

Codetxt
1BEGIN { 2 LOOP $I 10 { 3 SET $I := $I 1 B-ADD ; PRINT $I 4 } 5}

output:

Codetxt
1output: 22 34 46 58 610

SET & Conway's game of Life

Via the SET operation, I implement the Conway's game of Life. The following code is the Conway's game of Life.

For SET instruction, I test every <UNARYOP> and <BINARYOP> , also check multiple intake $[A-Z] varibles.

lifegame with 4 iteration example

input:

Codetxt
1BEGIN { 2 READ "test/example/lglider.arr" $A 3 LOOP $I 4 { 4 SET $B := $A U-EIGHTCOUNT ; 5 SET $D := $B 3 B-EQUALS ; 6 SET $C := $B 2 B-EQUALS $D B-OR $A B-AND ; 7 SET $E := $A U-NOT $D B-AND $C B-OR ; 8 9 PRINT $I 10 PRINT $E 11 SET $A := $E ; 12 } 13}

output:

Codetxt
1output: 21 30 0 0 0 0 40 0 1 0 0 50 0 1 0 0 60 0 1 0 0 70 0 0 0 0 82 90 0 0 0 0 100 0 0 0 0 110 1 1 1 0 120 0 0 0 0 130 0 0 0 0 143 150 0 0 0 0 160 0 1 0 0 170 0 1 0 0 180 0 1 0 0 190 0 0 0 0 204 210 0 0 0 0 220 0 0 0 0 230 1 1 1 0 240 0 0 0 0 250 0 0 0 0

set1.nlb:

Codetxt
1BEGIN { 2 READ "test/example/Hblock.arr" $A 3 ONES 5 5 $B 4 SET $A := $A U-EIGHTCOUNT ; 5 SET $B := $B U-NOT ; 6 PRINT "u-eight-of-h-block" 7 PRINT $A 8 PRINT "allzero5x5" 9 PRINT $B 10 11 READ "test/data/random.arr" $A 12 SET $A := $A 3 B-GREATER ; 13 PRINT "random" 14 PRINT $A 15 16 READ "test/data/random.arr" $A 17 SET $A := $A 3 B-LESS ; 18 PRINT "random2" 19 PRINT $A 20 21 READ "test/data/random.arr" $A 22 SET $A := $A 3 B-TIMES ; 23 PRINT "random3" 24 PRINT $A 25}
Codetxt
1output: 2u-eight-of-h-block 31 1 2 1 1 42 2 5 2 2 53 3 6 3 3 62 2 5 2 2 71 1 2 1 1 8allzero5x5 90 0 0 0 0 100 0 0 0 0 110 0 0 0 0 120 0 0 0 0 130 0 0 0 0 14random 151 0 0 1 160 0 0 0 170 0 0 1 181 0 0 0 19random2 200 1 0 0 211 1 1 0 221 1 0 0 230 1 1 1 24random3 2515 0 9 18 263 6 0 9 270 3 9 21 2824 3 3 3

The Project folder

Nlab_folder

Index / methods

Technology & methods.

  • 01C
  • 02Recursive-descent Parser
  • 03Matrix Operations
  • 04Conway's Game of Life