A Simple Math Language
The following tokens can be expressions
- Number -
start with an optional + or minus follow by one or more digits followed by an optional . and one or more digits
- Identifier - is a letter followed by zero or more letters, digits or underscore characters
If E is an expression then so is
- ( E ) returns E
- - E returns the negation of E
- + E returns E
- NOT E returns the logical negation of E
- E ' returns the matrix transpose of E
If E1 and E2 are expressions then so is
- E1 + E2 returns the sum of E1 and E2
- E1 - E2 returns E1 minus E2
- E1 * E2 returns E1 times E2
- E1 / E2 returns E1 divided by E2
- E1 .+ E2 returns the element wise addition of E1 and E2
- E1 .- E2 returns the element wise substraction of E2 from E1
- E1 .* E2 returns the element wise multiplication of E1 times E2
- E1 ./ E2 returns the element wise division of E1 by E2
- E1 < E2 returns true if E1 is less than E2, false otherwise
- E1 > E2 returns true if E1 is greater than E2, false otherwise
- E1 <= E2 returns true if E1 is less than or equal to E2, false otherwise
- E1 >= E2 returns true if E1 is greater than or equal to E2, false otherwise
- E1 == E2 returns true if E1 is equal to E2, false otherwise
- E1 != E2 returns false if E1 is equal to E2, true otherwise
If E11 E12 ... E1N, E21 E22 ... E2N ... are expressions then so is
- [ E11 E12 ... E1N ; E11 E12 ... E1N ; ... ] returns a matrix with E11 at position (1,1), E12 at position (1,2), etc.
All of these operators are left associatitive. The unary operators have highest precedence, followed by the element-wise operators (the ones starting with .), the arithmetic operators (+, -, *, /) and then the comparison operators.
If Identifier is a matrix variable, then
- Identifier(Expr) is an expression that produces the Expr element of Identifier
- Identifier(Expr1,Expr2) is an expression that produces the Expr1,Expr2 element of Identifier
- and so on
ones and zeros are functions that return matrices of the size of their number of arguments, ones(Expr) and zeros(Expr) return one dimensional matrices (vectors) of size Expr, ones of 1s and zeros of 0s, ones(Expr1,Expr2) and zeros(Expr1,Expr2) return two dimensional matrices of size Expr1,Expr2 and so on.
A statement S make take one of the following forms:
- Identifier = Expression ; sets the variable Identifier to the value Expression
- MatrixReference = Expression ; sets the matrix value MatrixReference to Expression
- print("string") ;
print('string') ; prints the string string
- print(Expression) ; prints the value Expression as ascii
- println ; prints a newline
- if Expression then S1 else S2 ; an if-else statement
- if Expression then S1 ; an if statement
- for Identifier = Expr to Expr do S rof a for statement
- while Expression do S elihw a while statement
- { S1 S2 ... SN } a sequence of statements