-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample.d
142 lines (127 loc) · 3.16 KB
/
sample.d
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
142
import orelang.Transpiler,
orelang.Engine,
orelang.Value;
import std.stdio;
void main() {
/**
* sum of 1 to 10
*/
enum codeINE = `
(new Engine).eval(
new Value([new Value("step"),
new Value([new Value("set"), new Value("i"), new Value(10.0)]),
new Value([new Value("set"), new Value("sum"), new Value(0.0)]),
new Value([new Value("until"), new Value([new Value("="), new Value([new Value("get"), new Value("i")]), new Value(0.0)]), new Value([
new Value("step"),
new Value([new Value("set"), new Value("sum"), new Value([new Value("+"), new Value([new Value("get"), new Value("sum")]), new Value([new Value("get"), new Value("i")])])]),
new Value([new Value("set"), new Value("i"), new Value([new Value("+"), new Value([new Value("get"), new Value("i")]), new Value(-1.0)])])
])]),
new Value([new Value("get"), new Value("sum")])
])
)`;
Value x = mixin(codeINE);
writeln("A sample code which written in Internal Expression, sum of 1 to 10)");
write("Code: ");
writeln(codeINE);
writeln("result -> ", x.getNumeric);
writeln("\n");
/**
* S Expression
*/
// loop 10 times and square 10
string code1 = `
(step
(def square (x)
(step
(set y (* x x))
(set z (* x x))
(* y z)))
(set x 1)
(while (< x 11)
(step
(println x)
(set x (+ x 1))))
(println (square 10)))
`;
// sum 1 to 10
string code2 = `
(step
(set sum 0)
(set i 1)
(while (<= i 10)
(step
(set sum (+ sum i))
(set i (+ i 1))))
(println sum))
`;
string code3 = `
(step
(def fun (x) (* x 40))
(set f fun)
(println (f (fun 10))))
`;
string code4 = `
(step
(set x (lambda (y) (* y y)))
(println (x 500))
(println ((lambda (z) (* z 40)) 10)))
`;
string code5 = `
(step
(print '(1 2 3 4 5 6 789))
(print (map (lambda (x) (* x x)) '(1 2 3 4 5))))
`;
string code6 = `
(step
(set arr (set-idx '(1 2 3 4 5) 2 100))
(println (map (lambda (x) (* x 10)) arr)))
`;
string code7 = `
(step
(set x 10)
(println x))
`;
string factor = `
(step
(def factor (x)
(if (<= x 1)
1
(* x (factor (- x 1)))))
(println (factor 4))
(println (factor 5)))
`;
string fib = `
(step
(def fib (n) (step
(if (= n 0) 0
(if (= n 1) 1
(+ (fib (- n 1)) (fib (- n 2)))))))
(def-var i 0)
(while (< i 10) (step
(println (fib i))
(set i (+ i 1)))))
`;
string fold = "(println (fold + 0 (map (lambda (x) (* x x)) (seq 10))))";
string[] codes = [
code1,
code2,
code3,
code4,
code5,
code6,
code7,
factor,
fib,
fold
];
size_t idx;
foreach (code; codes) {
Engine engine = new Engine();
writeln("Sample code", idx++, " :");
writeln("CODE----------------------------------------");
writeln(code);
writeln("OUTPUTS--------------------------------------");
engine.eval(Transpiler.transpile(code));
writeln("\n");
}
}