On this page:
2.1 Expressions
expr: lit
expr: binop
2.2 Declarations
decl: type: def
decl: type: tagged
2.3 Types
type: ref
type: struct
type: union
type: enum
type: array
type: pointer
type: function
2.4 Utility Functions
type?
expr?
decl?
2.5 S-Expression Syntax
typedef
struct
union
enum
array
pointer
2.6 Parsing
parse-decl
parse-decls
2.7 C Reader
Version: 4.1.4.2

2 C Syntax

This library provides data types representing C abstract syntax, a C parser, and macros for constructing C abstract syntax with a convenient parenthesized syntax. It can be required via:

 (require (planet dherman/c:1:0/syntax))

2.1 Expressions

(struct expr:lit (type value))
  type : symbol?
  value : any

(struct expr:binop (op left right))
  op : symbol?
  left : expr?
  right : expr?

2.2 Declarations

(struct decl:type:def (root defs))
  root : type?
  defs : (listof (cons symbol? #f))

(struct decl:type:tagged (type))
  type : (or/c type:struct? type:union? type:enum?)

2.3 Types

(struct type:ref (name))
  name : symbol?

(struct type:struct (tag fields))
  tag : (optional symbol?)
  fields : (listof (cons symbol? type?))

(struct type:union (tag variants))
  tag : (optional symbol?)
  variants : (listof (cons symbol? type?))

(struct type:enum (tag variants))
  tag : (optional symbol?)
  variants : list?

(struct type:array (type size))
  type : type?
  size : expr?

(struct type:pointer (type))
  type : type?

(struct type:function (return args))
  return : type?
  args : (listof type?)

2.4 Utility Functions

(type? x)  boolean?
  x : any

Determines whether x is a type.

(expr? x)  boolean?
  x : any

Determines whether x is an expression.

(decl? x)  boolean?
  x : any

Determines whether x is a declaration.

2.5 S-Expression Syntax

(typedef type alias-id)
 
type = struct
  | type-id
(struct tag-id)
(struct tag-id (struct-field ...))
(struct (struct-field ...))
 
struct-field = [type field-id]
  | field-id
(union tag-id)
(union tag-id (union-variant ...))
(union (union-variant ...))
 
union-variant = [type variant-id]
  | variant-id
(enum tag-id)
(enum tag-id (enum-variant ...))
(enum (enum-variant ...))
 
enum-variant = [variant-id expr]
  | variant-id

(array type size)  type:array?
  type : type?
  size : expr?

(pointer type)  type:pointer?
  type : type?

2.6 Parsing

(parse-decl in)  decl?
  in : (or/c input-port? string? path?)

Parses a C declaration.

(parse-decls in)  (listof decl?)
  in : (or/c input-port? string? path?)

Parses a sequence of C declarations.

2.7 C Reader

The module

 (require (planet dherman/c:1:0/reader))

provides a reader for C syntax. Using this reader, it is possible to embed C syntax directly into Scheme programs. For example:

  (define decls
    #reader (planet dherman/c/reader) {
      typedef int INT;
      struct tm {
          INT tm_sec;
          INT tm_min;
          INT tm_hour;
          INT tm_mday;
          INT tm_mon;
          INT tm_year;
          INT tm_wday;
          INT tm_yday;
          INT tm_isdst;
      };
    })

The C reader produces a quoted literal list of declarations.