DEV Community

Nevergarden
Nevergarden

Posted on

Shiro Branch

So I've been writing my own programming language for fun I decided to write a devlog series on what I'll be doing.

In my language I decided to omit if else and switch or any conditional statement and instead use a singular statement called branch.

It uses the same concept in CS Branching in a way that it will be much closer to it's mathematical implementation and result of this statement can be evaluated.

An example of a mathematical equation:

Math Equation f of x for x less than 2 be 12 for x equal 2 be 13 for x bigger than 2 be 100

fx = branch x {
 < 2 => { return 12; }
 == 2 => { return 13; }
 > 2 => { return 100; }
}
Enter fullscreen mode Exit fullscreen mode

Another feature is that the inner block expressions will be evaluated:

var x := 4;
var m = 10;
var fx := branch x {
  < 10 => { m = 12; return 5; }
  >= 10 => { return 2; }
}
Enter fullscreen mode Exit fullscreen mode

will result in x = 4, m = 12 and fx = 5.
(variable declaration for mutable and immutable variables are not finalized yet but var name = expr is mutable variable declaration and var name := expr is immutable variable declaration for now.)

For this statement I decided to add branch => otherwise tokens to lexer.

using otherwise

otherwise is for any other condition in a mathematical function.

var y := branch {
  x == 1 => { return 1; }
  x == 2 => { return -1; }
  otherwise => { return 0; }
}
Enter fullscreen mode Exit fullscreen mode

so while evaluating y if x is any number except 1 or 2 y value would be 0.

Top comments (0)