Simple Math Programming Language (SMPL) for the Web

Overview

Simple Math Programming Language (SMPL) for the Web

SMPL is a math-oriented programming language that can be interpreted in the browser. Its primary use is for the mathe:buddy app.

SMPL's syntax is basically a subset of JavaScript, but extended with intrinsic mathematical data types (e.g. matrices) and operator overloading for these types. SMPL uses MathJS (and later on e.g. TensorFlow) for core calculations.

Many concepts (and also parts of the source code) are taken from the Simple E-Learning Language SELL.

Compared to SELL, SMPL is Turing Complete: It allows e.g. loops and conditions.

SMPL is easily extendible. Refer to the Developer Notes below.

Notes related to the "mathe:buddy" project

We will also support writing code for random questions in Python, Octave, Maxima and SageMath.

SMPL has the following advantage:

  • No server is required. Code is executed at client-side. There is no need to install anything
  • Simple syntax with intrinsic data types for math
  • No need to import math libraries explicitly

Example

Copy the following code to https://npm.runkit.com/ and run it!

var SMPL = require('@mathebuddy/mathebuddy-smpl');

let src = `
// create two (3x3)-matrices A and B, with random
// (integral) entries in range [-5,5] without zero.
let A:B = randZ<3,3>(-5,5);
let C = A * B;
`;

let variables = SMPL.interpret(src);

for (let v of variables) {
  console.log(v.id + ' = ' + v.value.toString());
}

Installation

npm install @mathebuddy/mathebuddy-smpl

Language Definition

SMPL is (mostly) an imperative, typed language. Its base syntax is derived from JavaScript.

Programs

Data Types

boolean (BOOL), integer (INT), rational (RATIONAL), real (REAL), term (TERM), matrix (MATRIX), vector (VECTOR), set (SET), complex (COMPLEX).

Declarations

Declarations are initiated with keyword let, followed by an identifier and finally assigned expression by =. The expression in the right-hand side is mandatory to derive the data type.

Example

let x = 5;
let y = 7, z = 9.1011;
let u = rand(5);
let v = zeros<2,3>();
let a : b = rand<3,3>(-2, 2);
let c :/ d :/ e = rand<3,3>(-2, 2);
  • Variables x and u are integral. The value for u is randomly chosen from set {0,1,2,3,4,5}.
  • Variable z is a real valued.
  • Variables x and z are declared together (without any semantical relation). The notation is equivalent to let y=7; let y=9.1011;
  • Variables v, a, b, c and d are matrices. v is a zero matrix with two rows and three columns.
  • Matrices a and b consist of randomly chosen, integral elements in range [-2,2].
  • The colon separator : evaluates the right-hand side to each of the variables: let a = rand<3,3>(-2, 2); let b = rand<3,3>(-2, 2);. Accordingly, elements for a and b are drawn individually.
  • Separator :/ guarantees that no pair of matrices c, d and e is equal.

Expressions

List of operators, ordered by increasing precedence:

Operator Description
|| Logical Or
&& Logical And
==,!= Equal, Unequal
<, <=,>,>= Less than, Less or equal, Greater than, Greater or equal
+, - Addition, Subtraction
*, / Multiplication, Division
^ Potency
++, -- Postfix Incrementation, Decrementation

Base data types are evaluated at compile-time. Properties like e.g. matrix dimensions are evaluated at runtime. Thus, a RuntimeError is thrown if e.g. two matrices with a different number of rows are added.

Example

let x = 1.23 * sin(y) + exp(u + 2*w);
let C = A * B^T;
let d = det(C);
  • The examples assumes, that variables y, u, w, A, B, C have been declared before usage.

Conditions

Example

let s = 0;
if (x > 0) {
  s=1;
}
else if (x < 0) {
  s = -1;
}
else {
  x=0;
}

Loops

Example

while (x > 0) {
  // body
}

Example

do {
  // body
} while (x > 0);

Example

for (let i = 0; i < 5; i++) {
  // body
}

Functions

A function consists of a header and a body:

  • The header declared the name of the function, its parameter names and types and the return type.
  • The body is represented by a list of statements and returns a result that is compatible to the return type.

Example

function f(x: INT, y: INT): INT {
  return x + y;
}

let y = f(3, 4);

Grammar

The following formal grammar (denoted in EBNF) is currently implemented.

program = { statement };
statement = declaration | if | for | do | while | switch | function | return | break | continue | expression EOS;
declaration = "let" ID { ":" ID } "=" expr { "," ID { ":" ID } "=" expr } EOS;
expression = or { ("="|"+="|"-="|"/=") or };
or = and { "||" and };
and = equal { "&&" equal };
equal = relational [ ("=="|"!=") relational ];
relational = add [ ("<="|">="|"<"|">") add ];
add = mul { ("+"|"-") mul };
mul = unary { ("*"|"/") unary };
unary = unaryExpression [ unaryPostfix ];
unaryExpression = INT | IMAG | REAL | "(" expr ")" | ID | "-" unary;
unaryPostfix = "++" | "--" | [ "<" [ unary { "," unary } ] ">" ] "(" [ expr { "," expr } ] ")" | "[" expr "]";
for = "for" "(" expression ";" expression ";" expression ")" block;
if = "if" "(" expression ")" block [ "else" block ];
block = statement | "{" { statement } "}";
do = "do" block "while" "(" expr ")" EOS;
while = "while" "(" expr ")" block;
switch = "switch" "(" expr ")" "{" { "case" INT ":" { statement } } "default" ":" { statement } "}";
function = "function" ID "(" [ ID { "," ID } ] ")" block;
return = "return" [ expr ] EOS;
break = "break" EOS;
continue = "continue" EOS;

Developer Notes

File src/prototypes.ts declares function prototypes. These must be implemented for the interpreter in file src/interpret.ts.

Example: Create support for a function abs(..) for real and complex numbers.

  • Insert the following lines into const functions in file src/prototypes.ts:

    abs(x:REAL):REAL -> _absReal;
    abs(x:COMPLEX):REAL -> _absComplex;
    

Since the function is overloaded with two different types (and JavaScript does not support function overloading), each must be mapped onto a different function ID (_absReal and _absComplex).

  • Add two private methods to class SMPL_Interpreter in file src/interpret.ts:

    private _absReal(x: number): number {
      return Math.abs(x);
      // direct implementation: return x < 0 ? -x : x;
    }
    
    private _absComplex(x: Complex): number {
      const x_mathjs = x.toMathjs();
      return mathjs.abs(x_mathjs) as number;
      // direct implementation: return Math.sqrt(x.real*x.real + x.imag*x.imag);
    }

The first method uses Vanilla JavaScript function Math.abs(..) to calculate the absolute values of a real number.

The second method uses MathJS to calculate the absolute value of a complex number. Classes Complex, Matrix, ... provide type conversion methods.

Any third party math library written (or compiled to) JavaScript may be included, but increases the download size of the library (currently less than 200 kiB).

You might also like...

"Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: Make simple calculations. Read a random math-related quote.

Math Magician "Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: Make simple calculations.

Jun 27, 2022

Math magicians is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: Make simple calculations. Read a random math-related quote.

Math magicians is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: Make simple calculations. Read a random math-related quote.

react-math-magicians React Math magicians is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: - - Make simpl

May 27, 2022

"Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to make simple calculations and read random math-related quotes. Its built using react

Math Magician "Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to make simple calculations a

Feb 23, 2022

Module 03 project: Math magicians is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to Make simple calculations and Read a random math-related quote.

Math-magicians Math magicians is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to Make simple calculations an

Sep 26, 2022

Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: Make simple calculations and Read a random math-related quote.

Capstone project / FilmTube This is the final project of the moduel 2. we build a series page using an API to display all the series on the main page

Aug 23, 2022

"Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: Make simple calculations. Read a random math-related quote.

ONLINE MATH CALCULATOR USING REACT "Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: Make

Aug 24, 2022

"Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to make simple calculations and read a random math-related quote.

Math magicians "Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: Make simple calculations

Aug 26, 2022

Simple JSON parse/stringify for the Wren programming language

wren-json Simple JSON parse/stringify for the Wren programming language. Parses strict json and relaxed json Comments Unquoted keys Trailing commas St

May 18, 2022

The interpretation implementation implemented programming language built for fun. I'm currently boring in full stack web development. So, I crafted this one LoL. 👻

The interpretation implementation implemented programming language built for fun. I'm currently boring in full stack web development. So, I crafted this one LoL.  👻

What's Wuttyi? Everything is expression 👻 I just developed this tiny programming language because of boring in higher level programming construct. Mo

Dec 13, 2022
Releases(0.0.3a)
Owner
mathe:buddy / TH Köln
Prof. Dr. Heiko Knospe (project manager), Patricia Maria Graf B.Sc (content & gamification), Andreas Schwenk M.Sc (software & gamification)
mathe:buddy / TH Köln
Cookbook Method is the process of learning a programming language by building up a repository of small programs that implement specific programming concepts.

CookBook - Hacktoberfest Find the book you want to read next! PRESENTED BY What is CookBook? A cookbook in the programming context is collection of ti

GDSC-NITH 16 Nov 17, 2022
Write "hello world" in your native language, code "hello world" in your favorite programming language!

Hello World, All languages! ?? ?? Write "hello world" in your native language, code "hello world" in your favorite language! #hacktoberfest2022 How to

Carolina Calixto 6 Dec 13, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
"Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: Make simple calculations. Read a random math-related quote.

math-magicians A Single Page App (SPA) that allows users to Make simple calculations and read a random math-related quote. "Math magicians" is a websi

Temitope Ogunleye 3 Feb 21, 2022
Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: Make simple calculations. Read a random math-related quote.

Math Magicians. Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to: Make simple calculations

Mithlesh kumar 5 Mar 29, 2022
Math magicians is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to make simple calculations and read a random math-related quote. Build with React.js

Math Magicians Math magicians is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to make simple calculations an

Kyrillos Hany 9 Mar 23, 2022
It is a simple Math Sprint Game in which the user must agree or disagree with the answers to math problems in the quickest time possible.

MATH-SPRINT-GAME The user must agree or disagree with the results of math equations in this Math Sprint Game. The amount of time the player spends mak

AMEY THAKUR 8 Aug 1, 2022