Skip to content

Commit 0c6953c

Browse files
authored
Fix local binding and sequence compilation. (#41)
* clean up. * Fix local var. * fix seq. Co-authored-by: Bowen Fu <missing>
1 parent 5ac89a1 commit 0c6953c

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

core.lisp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,3 @@
237237
)
238238

239239
(define (even num) (= (% num 2) 0))
240-
241-
(define (my-cons car cdr) (lambda (dispatch) (if (= dispatch 'my-car) car cdr)))

include/lisp/compiler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ class SymbolTable
8282
{
8383
return mOrigFreeVars;
8484
}
85+
auto nbDefinitions() const
86+
{
87+
return mNbDefinitions;
88+
}
8589
};
8690

8791
class Compiler

src/compiler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,8 @@ void Compiler::compile(ExprPtr const& expr)
150150
for (auto const& e : seqPtr->mActions)
151151
{
152152
compile(e);
153-
instructions().push_back(kPOP);
153+
// todo, some actions push stack, some do not. Those pushing stack should pop their values.
154154
}
155-
instructions().resize(instructions().size() - 1);
156155
return;
157156
}
158157
if (auto lambdaPtr = dynamic_cast<LambdaBase<CompoundProcedure> const*>(exprPtr))
@@ -174,13 +173,14 @@ void Compiler::compile(ExprPtr const& expr)
174173
instructions().push_back(kRET);
175174
auto funcInstructions = std::get<0>(mFuncStack.top());
176175
auto const freeVars = symbolTable().freeVariables();
176+
auto const nbLocals = symbolTable().nbDefinitions() - args.size();
177177
mFuncStack.pop();
178178
for (auto f : freeVars)
179179
{
180180
emitVar(f);
181181
}
182182
auto const index = mCode.constantPool.size();
183-
auto const funcSym = FunctionSymbol{lambdaPtr->mName, args.size(), variadic, /* nbLocals= */ 0, funcInstructions};
183+
auto const funcSym = FunctionSymbol{lambdaPtr->mName, args.size(), variadic, nbLocals, funcInstructions};
184184
mCode.constantPool.push_back(funcSym);
185185
instructions().push_back(kCLOSURE);
186186
emitIndex(index);

test/lisp/testCompiler.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ TEST(Compiler, rest)
312312
EXPECT_EQ(output, "(2 . (3 . nil))\n");
313313
}
314314

315-
TEST(Compiler, localBinding)
315+
TEST(Compiler, freeVars)
316316
{
317317
std::string const source = " (define (my-cons car cdr) (lambda (dispatch) (if (= dispatch 'my-car) car cdr))) ((my-cons 1 2) 'my-cdr)";
318318
auto code = sourceToBytecode(source);
@@ -323,3 +323,15 @@ TEST(Compiler, localBinding)
323323
std::string output = testing::internal::GetCapturedStdout();
324324
EXPECT_EQ(output, "2\n");
325325
}
326+
327+
TEST(Compiler, localBinding)
328+
{
329+
std::string const source = " (define (my-cons car cdr) (define x car) (define y cdr) (lambda (dispatch) (if (= dispatch 'my-car) x y))) ((my-cons 1 2) 'my-car)";
330+
auto code = sourceToBytecode(source);
331+
code.instructions.push_back(kPRINT);
332+
VM vm{code};
333+
testing::internal::CaptureStdout();
334+
vm.run();
335+
std::string output = testing::internal::GetCapturedStdout();
336+
EXPECT_EQ(output, "1\n");
337+
}

0 commit comments

Comments
 (0)