2023-08-21

writing c with indent-based syntax similar to coffeescript or python

part of c programming

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

sc is a scheme-like notation for c. it can be used on its own to write c.

wisp, whitespace to lisp, is a notation for scheme expressions that allows indentation in-place of round brackets.

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
  • sc

usage examples

given a file with contents in wisp syntax with sc semantics, the following shell examples demonstrate usage via standard standard input/output.

display c code

wisp2lisp myfile.scw | sc

write to file

wisp2lisp myfile.scw | sc > output.c

display formatted c using clang-format

wisp2lisp myfile.scw | sc | clang-format

motivation

as the author of sc, i use sc in several c projects and found that with the imperative style of c, most lines tend to be simple operations, in what is like a column of commands. translated to scheme-like syntax, many lines start with a round bracket and have a nesting scope that ends on the same line, which can appear syntactically noisy.

interesting links