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:

void SET_U_NOT(var *opvar); void SET_U_EIGHTCOUNT(var *opvar); void SET_B_AND(var *ov1, var *ov2); void SET_B_OR(var *ov1, var *ov2); void SET_B_GREATER(var *ov1, var *ov2); void SET_B_LESS(var *ov1, var *ov2); void SET_B_ADD(var *ov1, var *ov2); void SET_B_TIMES(var *ov1, var *ov2); void 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:

BEGIN { ONES 1 5 $A READ "test/example/Hblock.arr" $B PRINT "array_A:" PRINT $A PRINT "array_B:" PRINT $B }

output:

array_A: 1 1 1 1 1 array_B: 0 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 0 0 0 0 0 0

Overwrite the varibles

input:

BEGIN { ONES 1 5 $A PRINT "array_A:" PRINT $A READ "test/example/Hblock.arr" $A PRINT "array_A:" PRINT $A ONES 1 1 $A PRINT "also_array_A:" PRINT $A }

output:

array_A: 1 1 1 1 1 array_A: 0 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 0 0 0 0 0 0 Also_array_A: 1

Loop

Nested Loop is also supported. input:

BEGIN { SET $A := 0 ; LOOP $I 3 { LOOP $J 3 { SET $A := $I $J B-TIMES ; PRINT $A } } PRINT $I PRINT "DONE" }

output:

output: 1 2 3 2 4 6 3 6 9 3 DONE

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

input:

BEGIN { LOOP $I 10 { SET $I := $I 1 B-ADD ; PRINT $I } }

output:

output: 2 4 6 8 10

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:

BEGIN { READ "test/example/lglider.arr" $A LOOP $I 4 { SET $B := $A U-EIGHTCOUNT ; SET $D := $B 3 B-EQUALS ; SET $C := $B 2 B-EQUALS $D B-OR $A B-AND ; SET $E := $A U-NOT $D B-AND $C B-OR ; PRINT $I PRINT $E SET $A := $E ; } }

output:

output: 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0

set1.nlb:

BEGIN { READ "test/example/Hblock.arr" $A ONES 5 5 $B SET $A := $A U-EIGHTCOUNT ; SET $B := $B U-NOT ; PRINT "u-eight-of-h-block" PRINT $A PRINT "allzero5x5" PRINT $B READ "test/data/random.arr" $A SET $A := $A 3 B-GREATER ; PRINT "random" PRINT $A READ "test/data/random.arr" $A SET $A := $A 3 B-LESS ; PRINT "random2" PRINT $A READ "test/data/random.arr" $A SET $A := $A 3 B-TIMES ; PRINT "random3" PRINT $A }
output: u-eight-of-h-block 1 1 2 1 1 2 2 5 2 2 3 3 6 3 3 2 2 5 2 2 1 1 2 1 1 allzero5x5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 random 1 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 random2 0 1 0 0 1 1 1 0 1 1 0 0 0 1 1 1 random3 15 0 9 18 3 6 0 9 0 3 9 21 24 3 3 3

The Project folder

Nlab_folder

Index / methods

Technology & methods.

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