Some AST changes; removed samples
Getty Ritter
9 years ago
1 | 1 |
#include "ast.h"
|
| 2 |
#include <stdio.h>
|
2 | 3 |
#include <stdlib.h>
|
3 | 4 |
#include <string.h>
|
| 5 |
|
| 6 |
/* some slice functions */
|
4 | 7 |
|
5 | 8 |
char*
|
6 | 9 |
str_of_slice(slice_t slice)
|
|
11 | 14 |
str[slice.len] = '\0';
|
12 | 15 |
return str;
|
13 | 16 |
}
|
| 17 |
|
| 18 |
slice_t*
|
| 19 |
mk_slice(char* c)
|
| 20 |
{
|
| 21 |
slice_t* s = malloc(sizeof(slice_t));
|
| 22 |
s->start = c;
|
| 23 |
s->len = strlen(c);
|
| 24 |
s->indentation = 0;
|
| 25 |
return s;
|
| 26 |
}
|
| 27 |
|
| 28 |
void
|
| 29 |
print_slice(slice_t* s)
|
| 30 |
{
|
| 31 |
int i;
|
| 32 |
for (i = 0; i < s->len; i++)
|
| 33 |
printf("%c", s->start[i]);
|
| 34 |
}
|
| 35 |
|
| 36 |
/* expr helpers */
|
| 37 |
|
| 38 |
void
|
| 39 |
expr_print(expr_t* e)
|
| 40 |
{
|
| 41 |
(e->print)(e->payload);
|
| 42 |
}
|
| 43 |
|
| 44 |
/* app */
|
| 45 |
|
| 46 |
void
|
| 47 |
print_app(expr_t **payload)
|
| 48 |
{
|
| 49 |
printf("(apply ");
|
| 50 |
expr_print(payload[0]);
|
| 51 |
printf(" ");
|
| 52 |
expr_print(payload[1]);
|
| 53 |
printf(")");
|
| 54 |
}
|
| 55 |
|
| 56 |
expr_t*
|
| 57 |
mk_app(expr_t* f, expr_t* x)
|
| 58 |
{
|
| 59 |
expr_t* e = malloc(sizeof(void*) * 4);
|
| 60 |
e->print = (callback)&print_app;
|
| 61 |
e->eval = NULL;
|
| 62 |
{
|
| 63 |
void** v = (void**) e;
|
| 64 |
v[2] = (void*) f;
|
| 65 |
v[3] = (void*) x;
|
| 66 |
}
|
| 67 |
return e;
|
| 68 |
}
|
| 69 |
|
| 70 |
/* lam */
|
| 71 |
|
| 72 |
/* let */
|
| 73 |
|
| 74 |
/* var */
|
| 75 |
|
| 76 |
void
|
| 77 |
print_var(slice_t **s)
|
| 78 |
{
|
| 79 |
printf("(var \"");
|
| 80 |
print_slice(s[0]);
|
| 81 |
printf("\")");
|
| 82 |
}
|
| 83 |
|
| 84 |
expr_t*
|
| 85 |
mk_var(ident_t* i)
|
| 86 |
{
|
| 87 |
void** v = malloc(sizeof(void*) * 3);
|
| 88 |
v[0] = (void*)&print_var;
|
| 89 |
v[1] = NULL;
|
| 90 |
v[2] = i;
|
| 91 |
return (expr_t*) v;
|
| 92 |
}
|
| 93 |
|
| 94 |
/* annot */
|
| 95 |
|
| 96 |
/* default */
|
| 97 |
|
| 98 |
void
|
| 99 |
print_default(void* payload)
|
| 100 |
{
|
| 101 |
printf("nil");
|
| 102 |
}
|
| 103 |
|
| 104 |
expr_t*
|
| 105 |
mk_default(void)
|
| 106 |
{
|
| 107 |
expr_t* e = malloc(sizeof(expr_t));
|
| 108 |
e->print = &print_default;
|
| 109 |
e->eval = NULL;
|
| 110 |
return e;
|
| 111 |
}
|
| 112 |
|
| 113 |
/* for testing */
|
| 114 |
|
| 115 |
int
|
| 116 |
main(int argc, char* argv[])
|
| 117 |
{
|
| 118 |
expr_t* e = mk_app(mk_default(), mk_var(mk_slice("foo")));
|
| 119 |
expr_print(e);
|
| 120 |
printf("\n");
|
| 121 |
return 0;
|
| 122 |
}
|
1 | |
struct slice {
|
| 1 |
#include <stddef.h>
|
| 2 |
|
| 3 |
typedef struct slice {
|
2 | 4 |
char *start;
|
3 | 5 |
size_t len;
|
4 | 6 |
size_t indentation;
|
5 | 7 |
} slice_t;
|
6 | 8 |
|
7 | 9 |
typedef slice_t ident_t;
|
| 10 |
typedef slice_t type_t;
|
8 | 11 |
|
9 | |
struct decl {
|
| 12 |
typedef void (*callback)(void*);
|
| 13 |
|
| 14 |
typedef struct expr {
|
| 15 |
callback print;
|
| 16 |
callback eval;
|
| 17 |
char payload[0];
|
| 18 |
} expr_t;
|
| 19 |
|
| 20 |
typedef struct decl {
|
10 | 21 |
ident_t name;
|
11 | 22 |
type_t *type;
|
12 | 23 |
expr_t *expr;
|
13 | 24 |
} decl_t;
|
14 | |
|
15 | |
struct expr {
|
16 | |
enum {
|
17 | |
EXPR_APP,
|
18 | |
EXPR_LAM,
|
19 | |
EXPR_LET,
|
20 | |
EXPR_VAR,
|
21 | |
EXPR_LIT
|
22 | |
} tag;
|
23 | |
} expr_t;
|
24 | 25 |
|
25 | 26 |
char* str_of_slice(slice_t);
|
26 | 27 |
|
1 | |
-- this is just a normal type definition
|
2 | |
record Monoid m =
|
3 | |
{ .mempty m
|
4 | |
, .mappend (m -> m -> m)
|
5 | |
}
|
6 | |
|
7 | |
implicit monoidIntAdd : Monoid Int =
|
8 | |
{ .mempty = 0
|
9 | |
, .mappend = (+)
|
10 | |
}
|
11 | |
|
12 | |
monoidIntMul : Monoid Int =
|
13 | |
{ .mempty = 1
|
14 | |
, .mappend = (*)
|
15 | |
}
|
16 | |
|
17 | |
mconcat (implicit m : Monoid m) (lst : List m) : m =
|
18 | |
case lst of
|
19 | |
Cons x xs -> m.mappend x xs
|
20 | |
Nil -> m.mempty
|
21 | |
|
22 | |
main = print (mconcat monoidIntAdd [1,2,3])
|
23 | |
; print (mconcat monoidIntMul [1,2,3])
|
24 | |
; print (mconcat [1,2,3])
|
1 | |
record Add x =
|
2 | |
{ .zero x
|
3 | |
, .add (x -> x -> x)
|
4 | |
}
|
5 | |
|
6 | |
record Mul x =
|
7 | |
{ .one x
|
8 | |
, .mul (x -> x -> x)
|
9 | |
}
|
10 | |
|
11 | |
sum (implicit add : Add a) = foldl add.zero add.add
|
12 | |
prod (implicit mul : Mul a) = foldl mul.one mul.mul
|