• Stars
    star
    211
  • Rank 186,867 (Top 4 %)
  • Language
    C
  • Created almost 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

sxc is an 'S-Expression C' transpiler for generating C code using macros written in Common Lisp

THIS REPOSORY IS OLD!!!

PLEASE GO TO https://github.com/BusFactor1Inc/sxc for updates.

Be sure to 'Star' if you want further notes on updates.

Burton Samograd BusFactor1Inc -- 2019

--

EXAMPLE:

$ ./sxcc.sh hello.sxc
$ ./a.out            
Hello, World!
$ make fire # TODO: make fire come out
$ C-c # Control-C to get your terminal back

sxc - S-Expression C

sxc is an s-expression based language source transpiler for C. By providing an S-expression based syntax, sxc will attempt to add Lisp-like macro and code generation capabilities to the C language (WIP).

Motivation

Lisp as a language uses lists as data structures for it's code represenation and is a language designed to work with lists. This combination has allowed for the creation of 'macros', or programs that take an input program, analyze it and then transform or generate arbitrary code from it. This gives great power to the programmer to create new abstraction and control structures that are not available in the original language.

sxc makes C a programmable language, just like a Lisp, with a the mental programming model of C.

Features

- transpilation from .sxc to .c sources
- macros
- source line gdb debugging of original sources (currently broken)

Building

Install sbcl from http://sbcl.org.

Type make. Output will be in ./sxc.

Running

Use the wrapper scripts ./sxc.sh and ./sxcc.sh to compile and build an executable from one or more .sxc files:

   ./sxcc.sh main.sxc lib.sxc

The result goes into ./a.out.

Examples

The following is a basic "Hello World" program:

;;
;; hello.sxc
;;

(#include <stdio.h>)

(int main ((int argc) (char (** argv)))
     (printf "Hello, World!\n")

     (return 0))

This can be compiled and run using the following:

./sxcc.sh -o hello hello.sxc && ./hello

Future Design

A more complex example is adding a new control structure to the language, like a 'string switch', which is like a standard C switch but works with strings as arguments:

;;
;; sswitch - string switch
;;
;; a switch statement that works with strings
;;
(macro sswitch (val &body cases)
       (labels ((genswitch (cases)
		  (let* ((case (car cases))
			 (cmd (first case))
			 (val (second case))
			 (statements (cddr case))
			 condition)
		    (when case
		      (unless (listp val)
			(setf val (list val)))
		      (mapcar (lambda (v)
				(setf condition (append conditiion `((strcmp ,v ,val) &&))))
			      val)
		      (setf condition (append condition '(1)))
		      `(if ,condition
			   ,@statements
			   (else
			    ,@(genswitch (cdr cases))))))))
       (genswitch cases))
		     

(int main ((int argc) (char (** argv)))
     (sswitch ([] argv 1)
	      (case ("a" "c")
		(printf "The value is \"a\" or \"c\"\n"))
	      (case "d"
		(goto e-label))
	      (case "b"
		(printf "The value is \"b\"\n"))
	      (case "e"
		(: e-label)
		(printf "The value is \"d\" or \"e\"\n"))
	      (default 
		  (printf "The value is neither \"a\", \"b\", \"c\", \"d\", or \"e\"\n")))
     (return 0))

Syntax

SEE ./tests/ FOR MORE SYNTAX EXAMPLES.

--

The basic syntax of sxc follows that of the Lisp family of languages.

The first argument to an s-expression is the function or keyword to be called or used, followed by it's arguments.

There are a some exceptions for convenience:

(?++ ...) - post increment (++? ...) - pre increment

(?-- ...) - post decrement (--? ...) - pre decrement

Utilities

A wrapper script 'sxcc' is provided to simplify building and compilation of program sources into an executable.

TODO:

You can find some things to translate in to SXC in the to-translate/ directory.

-- Burton Samograd [email protected] 2018

More Repositories

1

emacs-jit

Emacs with an Emacs Lisp JIT (Just In Time) compiler using libjit.
Emacs Lisp
142
star
2

sigil

A Parenscript to Javascript command line compiler and interpreter.
Common Lisp
26
star
3

rsa

The RSA Encryption Algorithm written in Common Lisp
Common Lisp
20
star
4

px2

PX2 is a Javascript Front End Framework designed to replace Backbone written in Parenscript.
JavaScript
20
star
5

anser

A header only Boolean Propagator Network framework for the omni-directional computation of Integer mathematical functions and computation graph analysis.
C++
14
star
6

med

med - A fork of the emacs-like text editor for the Mezzano operating system
Common Lisp
13
star
7

autograph

Autograph is a CSS generation language that uses s-expressions for styling rules and allows for the use of variables and functions written in Common Lisp.
Common Lisp
7
star
8

typed-cl

Easier type declerations for for Common Lisp.
Common Lisp
4
star
9

cl-active-variables

Variables with read and write callbacks in Common Lisp.
Common Lisp
3
star
10

cl-magic

parse and decode 'magic bytes' in files
C
1
star
11

prototype

A starting point 'boilerplate' for a prototype Common Lisp web application.
Common Lisp
1
star
12

tracker

Tracker is the start of a music tracker interface written with PX2 in Parenscript.
CSS
1
star
13

woc

Word Occurance - a poor man's google for the filesystem
C
1
star
14

sb-directory

A suite of directory walking and sizing functions in Common Lisp using the sb-posix routines of SBCL.
Common Lisp
1
star
15

hackernews-reader

A web based Hacker News reader using a node.js server.
JavaScript
1
star
16

xp32

XP32 was my university graduation thesis (4th year) project, a graphics demo for DOS.
Emacs Lisp
1
star
17

Quill

The Quill Common Lisp Language (QCL) with Clarity
1
star