Skip to content

Commit f90ae42

Browse files
committed
Initial creation
0 parents  commit f90ae42

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*~
2+
*.o
3+
test

README.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Simple scheme
2+
3+
Scheme is known as maxwell of computer science.
4+
I always want to understand how it works.
5+
Finally, I find some time and write a simple scheme interpreter with C++.
6+
7+
This interpreter supports special operator:
8+
define, lambda, if, cond, let, quote, assign.
9+
10+
It also supports big number and tail recursion.
11+
12+
Most SICP exercise can be written with it.
13+
See the example directory in source files.
14+
15+
16+
17+
18+
19+

sscheme.cc

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
2+
void Repl(char* name, FILE* file)
3+
{
4+
Environment env;
5+
6+
// repl(read evaluate print loop)
7+
for(;;) {
8+
if (!name)
9+
printf(">");
10+
Exp* exp = Exp::Read(file);
11+
if (exp == NULL)
12+
break;
13+
Value val = exp->Eval(&env);
14+
if (!name)
15+
val->Print();
16+
delete exp;
17+
}
18+
}
19+
20+
// opaque expression
21+
class Exp
22+
{
23+
public:
24+
25+
virtual Value Eval(Environment*) = 0;
26+
virtual ~Exp() { }
27+
28+
static Exp* Read();
29+
30+
private:
31+
// Disallow explict/copy/assignment constructor
32+
Exp();
33+
Exp(const Exp&);
34+
void operator=(const Exp&);
35+
};
36+
37+
class SelfEvalExp: public Exp
38+
{
39+
public:
40+
SelfEvalExp(Value value): value_(value) { }
41+
virtual Value Eval(Environment*) { return value_; }
42+
43+
private:
44+
Value value_;
45+
};
46+
47+
class VariableExp: public Exp
48+
{
49+
public:
50+
VariableExp(Variable var): var_(var) { }
51+
virtual Value Eval(Environment*);
52+
private:
53+
Variable var_;
54+
};
55+
56+
Value VariableExp::Eval(Environment* env)
57+
{
58+
assert(env);
59+
return env->Lookup(var_);
60+
}
61+
62+
class QuoteExp: public Exp
63+
{
64+
public:
65+
QuoteExp(Exp* exp): quote_text_(exp) { }
66+
virtual Value Eval(Environment*) {return Value(text_);}
67+
private:
68+
Exp* text_;
69+
};
70+
71+
class LambdaExp: public Exp
72+
{
73+
public:
74+
LambdaExp(Arguments plist, Code code; Environment*);
75+
virtual Value Eval(Environment*);
76+
private:
77+
Arguments plist_;
78+
Code code_;
79+
Environment* env_;
80+
};
81+
82+
class IfExp: public Exp
83+
{
84+
public:
85+
virtual Value Eval(Environment*);
86+
};
87+
88+
class CondExp: public Exp
89+
{
90+
public:
91+
virtual Value Eval(Environment*);
92+
};
93+
94+
class AssignExp: public Exp
95+
{
96+
public:
97+
virutal Value Eval(Environment*);
98+
};
99+
100+
class DefineExp: public Exp
101+
{
102+
public:
103+
virtual Value Eval(Environment*);
104+
};
105+
106+
class BeginExp: public Exp
107+
{
108+
public:
109+
virtual Value Eval(Environment*);
110+
};
111+
112+
class AppExp: public Exp
113+
{
114+
public:
115+
virtual Value Eval(Environment*);
116+
};
117+
118+
119+
int main(int argc, char* argv[])
120+
{
121+
122+
for(int i = 1; i < argc; i++) {
123+
FILE* file = fopen(argv[i],"r");
124+
if (!file) {
125+
fprintf(stderr,"%s fail to open %s(%s)\n", argv[0], argv[i], strerr(errno));
126+
return EXIT_FAILURE;
127+
} else {
128+
Repl(argv[i],file);
129+
fclose(file);
130+
}
131+
}
132+
if (argc == 1) repl(NULL,stdin);
133+
134+
return EXIT_SUCCESS;
135+
}

0 commit comments

Comments
 (0)