Introducing Gypsum

Published on 2014-07-06
Tagged: gypsum

View All Posts

Today, I'd like to introduce a fun side-project I've been working on for a while. Gypsum is a new programming language, and CodeSwitch is a new virtual machine which executes Gypsum bytecode.

def main =
  print("Hello, world!\n")

Gypsum is a compiled, statically-typed, object-oriented language. Once the compiler is more complete, it will be functional as well. The compiler is very much "under construction" right now. Gypsum's syntax is inspired by Python and Scala. Whitespace is significant, so semicolons and braces are not needed (though they can still be used when extra control is desired).

Here's an example program which computes a factorial:

def factorial(n: i64) =
  var p = 1
  while (n > 0)
    p *= n
    n -= 1
  p

def main =
  var n = 5
  var fac = factorial(n)
  print(n.to-string + "! is " + fac.to-string + "\n")

Here's another example which approximates the value of pi by numerically measuring the area of a quarter-circle of radius 2.

def integrate-circle =
  var begin = 0.
  var end = 2.
  var dx = 1e-3
  var sum = 0.
  var x = begin
  while (x < end)
    var yl = sqrt(4. - x * x)
    x += dx
    var yh = sqrt(4. - x * x)
    var y = .5 * (yl + yh)
    sum += y * dx
  sum

def sqrt(n: f64) =
  if (n < -1e4)
    throw Exception
  var x = 1.
  var i = 0
  while (i < 10)
    x = .5 * (x + n/x)
    i += 1
  x

def abs(n: f64) = if (n < 0.) -1.0 * n else n

def main =
  var pi = integrate-circle
  print("The value of \u03c0 is approximately " + pi.to-string + "\n")

To try out Gypsum, clone my GitHub repository. The examples directory has several sample programs with some commentary on how to use the language.

Gypsum development is at a really early stage right now. It's not suitable for writing anything more than toy programs yet. I'm actively working on it though, and I'm looking forward to writing more about it soon. I've learned a ton about language design already, and I think there's a lot more potential for experimentation.