-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNIDE-examples.mc
143 lines (79 loc) · 2.24 KB
/
NIDE-examples.mc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
`{Hello World}
expptr f(expptr exp){return exp;}
f(`{a})
int x[10];
for(int i = 0; i < 10; i++)x[i] = i;
for(int i = 0; i < 10; i++)fprintf(stdout,"%d",x[i]);
int_exp(x[5])
{int sum = 0; for(int i = 0; i < 10; i++)sum += x[i]; return int_exp(sum);}
typedef struct myexpstruct{
char * label;
struct myexpstruct * car;
struct myexpstruct * cdr;
} myexpstruct, *myexp;
myexp mycons(char * s, myexp x, myexp y){
myexp cell = malloc(sizeof(myexpstruct));
cell->label = s;
cell->car = x;
cell->cdr = y;
return cell;}
expptr myexp_exp(myexp x){
if(x == NULL) return string_atom("nil");
return `{${string_atom(x->label)} ${myexp_exp(x->car)} ${myexp_exp(x->cdr)}};
}
myexp_exp(mycons("foo",mycons("bar",NULL,NULL),NULL))
int y[0] = 2;
y[0] += 1;
int_exp(y[0])
expptr friend[0] = `{Bob Givan};
int height[0] = 6;
`{My friend ${friend[0]} is ${int_exp(height[0])} feet tall.}
expptr g(expptr x){return x;}
g(`{a})
expptr g(expptr x){return `{$x $x};}
g(`{a})
expptr bar(int i);
expptr foo(int i){
if(i == 0){return `{foo};}
return bar(--i);}
expptr bar(int i){
if(i == 0){return `{bar};}
return foo(--i);}
foo(1)
expptr bar(int i){
if(i == 0){return `{bar2};}
return foo(--i);}
foo(1)
umacro{mydolist($x, $L){$body}}{
expptr rest = gensym("rest");
return `{for(explist $rest = $L;
!atomp($rest);
$rest = cdr($rest);)
{expptr $x = car($rest); $body}};}
macroexpand(`{mydolist(item,list){f(item);}})
macroexpand(`{ucase{`{a;b};{\$a;\$any}:{return a;}}})
ucase{`{a;b};{$a;$any}:{return a;}}
int numeralp(expptr x){
if(!atomp(x))return 0;
char * s= atom_string(x);
for(int i = 0; s[i] != '\0'; i++){
if(s[i] < '0' || s[i] > '9')return 0;}
return 1;
}
int value(expptr e){
ucase{e;
{$x+$y}:{return value(x)+value(y);}
{$x*$y}:{return value(x)*value(y);}
{($x)}:{return value(x);}
{$z}.(numeralp(z)):{return atoi(atom_string(z));}}
return 0;
}
int_exp(value(`{(5+2)*10}))
ucase{x;{?any}:{return x;)}
ucase{x;{?any}:(return x;)}
ucase{x;{?any}:{return x;}}
ucase{NULL;{$x}:{return car(x);}}
int_exp(value(`{x}))
{expptr x = NULL; return x->arg1;}
ucase{`{a};{$x}:{return x;}}
ucase{`{a};{$x}:{breakpt("foo");return x;}}