-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
101 lines (76 loc) · 2.26 KB
/
README
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
==== fcl compiler ====
This compiler is written entirely in Scheme.
- Scheme-native primitive scanner
- recursive-descent LL(1) parser
- table-driven semantic checker
- stack-based code generator
=== Usage ===
To load the program into MIT/GNU Scheme and compile your file from fcl to C, run this command:
> echo y | scheme --load Compiler.scm --eval '(begin (compile "myfile.fcl" "myfile.c" fcl-to-c) (exit))'
After generating the code, the program will immediately close.
To include the compiler tools in other Scheme programs, simply use the (load ...) expression.
=== The fcl language ===
Example code:
fac(n) | n>0 : 1 = n*fac(n-1);
The definition of a function consists of:
- the function name
- an optional parameter list
- an optional guard expression
- if a guard is given, an optional terminal value
- the function body expression
= Parameters =
A function with no parameters could simply be written like this:
x = 5;
x is a function of the value 5.
= Guard =
A function can be guarded against a boolean expression.
There are four boolean operators: ~ (equal), ! (not equal), > (greater than) and < (less than).
lessThanFive(n) | n<5 = 1;
The terminal value will be emitted if the guard expression is false.
isEven(n) | n!1 : 0 = isOdd(n-1);
isOdd(n) | n!1 : 1 = isEven(n-1);
x = isEven(5);
The above program should run as follows:
=> x
=> isEven(5)
=> isOdd(4)
=> isEven(3)
=> isOdd(2)
=> isEven(1)
<= isEven(1) [0]
...
<= x [0]
= Function body =
A function body has an integer value, which is computed from the body expression.
There are four integer operators: +, -, * and /.
A function definition ends with a semicolon.
== Sample program ==
fac(n) | n > 0 : 1 = n*fac(n-1);
fib(n) | n > 2 : 1 = fib(n-2)+fib(n-1);
gcd(a,b) | a!b : a = gcd1(a,b);
gcd1(a,b) | a>b : gcd2(a,b) = gcd(a-b,b);
gcd2(a,b) = gcd(a,b-a);
== Sample C output ==
int fac(int n) {
if (n>0)
return n*fac(n-1);
else return 1;
}
int fib(int n) {
if (n>2)
return fib(n-2)+fib(n-1);
else return 1;
}
int gcd(int a,int b) {
if (a!=b)
return gcd1(a,b);
else return a;
}
int gcd1(int a,int b) {
if (a>b)
return gcd(a-b,b);
else return gcd2(a,b);
}
int gcd2(int a,int b) {
return gcd(a,b-a);
}