Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
kleene.h File Reference

In admiration of Stephen Cole Kleene. More...

#include <stddef.h>
#include <stdint.h>
#include "serene/rt/errors.h"
Include dependency graph for kleene.h:

Go to the source code of this file.

Data Structures

struct  srn_kleene_arm_t
 One arm of a case. More...
struct  srn_kleene_local_t
 SRN_KL_VAR: a reference to a bound local by name. More...
struct  srn_kleene_global_t
 SRN_KL_GLOBAL: a reference to a top-level definition by name. More...
struct  srn_kleene_fn_t
 SRN_KL_FN: a closure. More...
struct  srn_kleene_app_t
 SRN_KL_APP and SRN_KL_PAPP: fn applied to args. More...
struct  srn_kleene_prim_t
 SRN_KL_PRIM: primitive op over atom args. More...
struct  srn_kleene_ctor_t
 SRN_KL_CTOR: build a value with constructor tag from atom fields. More...
struct  srn_kleene_let_t
 SRN_KL_LET: evaluate rhs, bind it to name, then evaluate body. More...
struct  srn_kleene_case_t
 SRN_KL_CASE: match scrutinee against arms. More...
struct  srn_kleene_label_t
 SRN_KL_LABEL: a label name taking params, whose body runs when jumped to, in scope of cont. More...
struct  srn_kleene_jump_t
 SRN_KL_JUMP: hand args to the label named label. More...
struct  srn_kleene_node_t
 A node of the Kleene tree. tag selects the active union member. More...
struct  srn_kleene_def_t
 A single top-level definition, (def name body). More...
struct  srn_kleene_module_t
 A parsed Kleene module, the unit the reader returns and the builder consumes. More...

Typedefs

typedef struct srn_mm_t srn_mm_t
typedef struct srn_context_t srn_context_t
typedef struct srn_value_t srn_value_t
typedef struct srn_metadata_t srn_metadata_t
typedef struct srn_sexp_tree_t srn_sexp_tree_t
typedef enum srn_kleene_node_tag_t srn_kleene_node_tag_t
typedef struct srn_kleene_node_t srn_kleene_node_t
typedef struct srn_kleene_arm_t srn_kleene_arm_t
 One arm of a case.
typedef struct srn_kleene_local_t srn_kleene_local_t
 SRN_KL_VAR: a reference to a bound local by name.
typedef struct srn_kleene_global_t srn_kleene_global_t
 SRN_KL_GLOBAL: a reference to a top-level definition by name.
typedef struct srn_kleene_fn_t srn_kleene_fn_t
 SRN_KL_FN: a closure.
typedef struct srn_kleene_app_t srn_kleene_app_t
 SRN_KL_APP and SRN_KL_PAPP: fn applied to args.
typedef struct srn_kleene_prim_t srn_kleene_prim_t
 SRN_KL_PRIM: primitive op over atom args.
typedef struct srn_kleene_ctor_t srn_kleene_ctor_t
 SRN_KL_CTOR: build a value with constructor tag from atom fields.
typedef struct srn_kleene_let_t srn_kleene_let_t
 SRN_KL_LET: evaluate rhs, bind it to name, then evaluate body.
typedef struct srn_kleene_case_t srn_kleene_case_t
 SRN_KL_CASE: match scrutinee against arms.
typedef struct srn_kleene_label_t srn_kleene_label_t
 SRN_KL_LABEL: a label name taking params, whose body runs when jumped to, in scope of cont.
typedef struct srn_kleene_jump_t srn_kleene_jump_t
 SRN_KL_JUMP: hand args to the label named label.

Enumerations

enum  srn_kleene_node_tag_t : size_t {
  SRN_KL_VAR , SRN_KL_LIT , SRN_KL_GLOBAL , SRN_KL_FN ,
  SRN_KL_APP , SRN_KL_PAPP , SRN_KL_PRIM , SRN_KL_CTOR ,
  SRN_KL_LET , SRN_KL_CASE , SRN_KL_LABEL , SRN_KL_JUMP
}

Functions

srn_error_t * srn_kleene_read (srn_context_t *ctx, const char *src, srn_kleene_module_t *out)
 Parse the Kleene source in src (of length len) into *out.
size_t srn_kleene_print (const srn_kleene_module_t *module, char *buf, size_t cap)
 Write module back out as Kleene text into the buffer buf of size cap.

Detailed Description

In admiration of Stephen Cole Kleene.

The first layer of IR of the compiler. Any source level language should elaborate to this IR.

The tree is deliberately small and already in A-normal form. Every operand is an atom, a variable, a literal, or a global reference, and anything compound is named by an enclosing let. The reader checks that shape and rejects input that breaks it. Nodes carry no source level types.

Definition in file kleene.h.

Typedef Documentation

◆ srn_context_t

typedef struct srn_context_t srn_context_t

Definition at line 43 of file kleene.h.

◆ srn_kleene_app_t

typedef struct srn_kleene_app_t srn_kleene_app_t

SRN_KL_APP and SRN_KL_PAPP: fn applied to args.

Each arg is an atom node (VAR, LIT, or GLOBAL).

◆ srn_kleene_arm_t

typedef struct srn_kleene_arm_t srn_kleene_arm_t

One arm of a case.

Matches a constructor tag and binds that constructor's fields to fresh locals before running the body.

◆ srn_kleene_case_t

typedef struct srn_kleene_case_t srn_kleene_case_t

SRN_KL_CASE: match scrutinee against arms.

The reader guarantees the tags are distinct.

◆ srn_kleene_ctor_t

typedef struct srn_kleene_ctor_t srn_kleene_ctor_t

SRN_KL_CTOR: build a value with constructor tag from atom fields.

◆ srn_kleene_fn_t

typedef struct srn_kleene_fn_t srn_kleene_fn_t

SRN_KL_FN: a closure.

Free variables are captured implicitly by the lowering, the source lists only the parameters.

◆ srn_kleene_global_t

typedef struct srn_kleene_global_t srn_kleene_global_t

SRN_KL_GLOBAL: a reference to a top-level definition by name.

◆ srn_kleene_jump_t

typedef struct srn_kleene_jump_t srn_kleene_jump_t

SRN_KL_JUMP: hand args to the label named label.

◆ srn_kleene_label_t

typedef struct srn_kleene_label_t srn_kleene_label_t

SRN_KL_LABEL: a label name taking params, whose body runs when jumped to, in scope of cont.

◆ srn_kleene_let_t

typedef struct srn_kleene_let_t srn_kleene_let_t

SRN_KL_LET: evaluate rhs, bind it to name, then evaluate body.

◆ srn_kleene_local_t

typedef struct srn_kleene_local_t srn_kleene_local_t

SRN_KL_VAR: a reference to a bound local by name.

◆ srn_kleene_node_t

typedef struct srn_kleene_node_t srn_kleene_node_t

Definition at line 75 of file kleene.h.

◆ srn_kleene_node_tag_t

◆ srn_kleene_prim_t

typedef struct srn_kleene_prim_t srn_kleene_prim_t

SRN_KL_PRIM: primitive op over atom args.

◆ srn_metadata_t

typedef struct srn_metadata_t srn_metadata_t

Definition at line 45 of file kleene.h.

◆ srn_mm_t

typedef struct srn_mm_t srn_mm_t

Definition at line 42 of file kleene.h.

◆ srn_sexp_tree_t

typedef struct srn_sexp_tree_t srn_sexp_tree_t

Definition at line 46 of file kleene.h.

◆ srn_value_t

typedef struct srn_value_t srn_value_t

Definition at line 44 of file kleene.h.

Enumeration Type Documentation

◆ srn_kleene_node_tag_t

enum srn_kleene_node_tag_t : size_t
Enumerator
SRN_KL_VAR 

local variable reference

SRN_KL_LIT 

literal (int, string, char)

SRN_KL_GLOBAL 

top level name reference

SRN_KL_FN 

closure: params, body

SRN_KL_APP 

function application

SRN_KL_PAPP 

partial application

SRN_KL_PRIM 

primitive op on atoms

SRN_KL_CTOR 

constructor: integer tag + atom fields

SRN_KL_LET 

bind one expression, then continue

SRN_KL_CASE 

branch on a scrutinee's constructor tag

SRN_KL_LABEL 

define a label that can jump to

SRN_KL_JUMP 

jump to a label with atom args

Definition at line 48 of file kleene.h.

48 : size_t {
49 /// local variable reference
51 /// literal (int, string, char)
53 /// top level name reference
55 /// closure: params, body
57 /// function application
59 /// partial application
61 /// primitive op on atoms
63 /// constructor: integer tag + atom fields
65 /// bind one expression, then continue
67 /// branch on a scrutinee's constructor tag
69 /// define a label that can jump to
71 /// jump to a label with atom args
srn_kleene_node_tag_t
Definition kleene.h:48
@ SRN_KL_GLOBAL
top level name reference
Definition kleene.h:54
@ SRN_KL_CTOR
constructor: integer tag + atom fields
Definition kleene.h:64
@ SRN_KL_LIT
literal (int, string, char)
Definition kleene.h:52
@ SRN_KL_VAR
local variable reference
Definition kleene.h:50
@ SRN_KL_PRIM
primitive op on atoms
Definition kleene.h:62
@ SRN_KL_FN
closure: params, body
Definition kleene.h:56
@ SRN_KL_LABEL
define a label that can jump to
Definition kleene.h:70
@ SRN_KL_APP
function application
Definition kleene.h:58
@ SRN_KL_JUMP
jump to a label with atom args
Definition kleene.h:72
@ SRN_KL_CASE
branch on a scrutinee's constructor tag
Definition kleene.h:68
@ SRN_KL_LET
bind one expression, then continue
Definition kleene.h:66
@ SRN_KL_PAPP
partial application
Definition kleene.h:60

Function Documentation

◆ srn_kleene_print()

size_t srn_kleene_print ( const srn_kleene_module_t * module,
char * buf,
size_t cap )

Write module back out as Kleene text into the buffer buf of size cap.

Returns the number of bytes the full text needs, not counting a terminating NUL. When that exceeds cap the buffer holds a truncated result, call again with a large enough buffer. Reading then printing a module yields equivalent text, which makes the format usable as a golden-test artifact.

◆ srn_kleene_read()

srn_error_t * srn_kleene_read ( srn_context_t * ctx,
const char * src,
srn_kleene_module_t * out )

Parse the Kleene source in src (of length len) into *out.

Nodes are allocated from mm and remain valid for that arena's lifetime. On success returns NULL and writes the module to *out. On a syntax or shape error returns an error built on ctx, describing the offending offset, and leaves *out untouched.