2022-11-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, and with less brackets, like 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
  • sc
  • wisp

usage

take a file with contents that use wisp syntax with sc semantics and write c code to standard output:

wisp2lisp myfile.scw | sc

write to file:

wisp2lisp myfile.scw | sc > output.c

formatted with clang-format:

wisp2lisp myfile.scw | sc | clang-format

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

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 make one operation, in what is like a list of commands, without much nesting. with scheme-like syntax that would mean most lines would start with a round bracket. this can appear syntactically noisy.

experimental wisp support in sc

the wisp compatibility of sc could be improved, though, as there are some syntactical elements of sc that can be represented with less code in wisp. therefore, sc now comes with a file "other/wisp2sc" that uses standard input/output and currently only changes the way key-value associations are made to make it need less code.

with wisp2sc, declare and similar forms can be used like this:

declare
  a 1
  b 2

instead of having to use a dot on each following line:

declare
  . a 1
  . b 2

this is because sc takes these associations as a flat list (key value key/value ...).

interesting links