gdritter repos pico-ml-old / 894a38d
Some AST changes; removed samples Getty Ritter 9 years ago
4 changed file(s) with 122 addition(s) and 48 deletion(s). Collapse all Expand all
+109
-0
ast.c less more
11 #include "ast.h"
2 #include <stdio.h>
23 #include <stdlib.h>
34 #include <string.h>
5
6 /* some slice functions */
47
58 char*
69 str_of_slice(slice_t slice)
1114 str[slice.len] = '\0';
1215 return str;
1316 }
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 }
+13
-12
ast.h less more
1 struct slice {
1 #include <stddef.h>
2
3 typedef struct slice {
24 char *start;
35 size_t len;
46 size_t indentation;
57 } slice_t;
68
79 typedef slice_t ident_t;
10 typedef slice_t type_t;
811
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 {
1021 ident_t name;
1122 type_t *type;
1223 expr_t *expr;
1324 } 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;
2425
2526 char* str_of_slice(slice_t);
2627
+0
-24
samples/monoid.bcp less more
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])
+0
-12
samples/num.bcp less more
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