2019-06-23

writing c with indent based syntax similar to coffeescript or python

by combining wisp with sc, which both only depend on guile, it is possible to write c with indent and with less brackets similar to coffeescript or python.

sc is a scheme-like notation for c, similar to an abstract syntax tree. it can be used on its own to write c. wisp, whitespace to lisp, is a notation for scheme expressions that allows for indent in place of round brackets. wisp and sc are both quite complete and schemes basic syntax, which both support, of course too.

how it looks

pre-include "stdio.h"

define (main argc argv) : int int char**
  declare i int
  printf "the number of program arguments passed is %d\n" argc
  for : (set i 0) (< i argc) (set+ i 1)
    printf "argument %d is %s\n" (+ i 1) (array-get argv i)
  return 0

what you need

  • gnu guile, which is usually available in package repositories or already installed
  • wisp.scm, which can be found in the release archives right at the top-level of the wisp folder
  • wisp.scm be installed in guiles module load path - /usr/share/guile/site/wisp.scm should work in many cases
  • the following guile script in a file wisp2lisp somewhere in your $PATH so that it can be used as a command on the command-line
#!/usr/bin/guile
!#
; example usage:
;   cat t.w | wisp2lisp

(import (wisp) (rnrs io ports))
(display (wisp2lisp (get-string-all (current-input-port))))

usage

take a file with contents that use wisp syntax with sc semantics and compile to standard output with the following shell command:

cat myfile.scw | wisp2lisp | sc

write to file:

cat myfile.scw | wisp2lisp | sc > output.c

formatted with clang-format:

cat myfile.scw | wisp2lisp | sc | clang-format

here is a wisp syntax reference, and information about how sc works with syntax reference is found on its respective homepage.

links

motivation

as the author of sc, i used sc in several c projects and found that with the imperative style of c, each line tends to be a command and that with scheme-like syntax that means it starts with a round bracket. this unfortunately appears quite noisy, to the point that it might make things more difficult with all the little commands and declaration and memory-management code overhead in c.

experimental wisp support in sc

sc now comes with a file other/wisp2sc which uses standard input/output and only changes the way associations are made to require less syntax. this makes it possible to use this in declare and all similar forms:

declare
  a 1
  b 2

instead of having to use a dot, since sc on its own currently takes associations as a flat list (key value key value ...)

declare
  . a 1
  . b 2