Skip to content

Commit c8b1233

Browse files
authored
And / Or as macros. (#24)
* Update license. * and or as macros. Co-authored-by: Bowen Fu <missing>
1 parent c84e4d0 commit c8b1233

File tree

7 files changed

+57
-77
lines changed

7 files changed

+57
-77
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [2021] [Bowen Fu]
189+
Copyright 2021 Bowen Fu
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

core.lisp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,48 @@
6666
)
6767
)
6868

69+
(define and
70+
(macro lst
71+
(define (impl ls)
72+
(if (null? ls)
73+
#t
74+
(if (cons? ls)
75+
(if (null? (cdr ls))
76+
(car ls)
77+
`(if ,(car ls)
78+
,(impl (cdr ls))
79+
#f
80+
)
81+
)
82+
#f
83+
)
84+
)
85+
)
86+
(impl lst)
87+
)
88+
)
89+
90+
(define or
91+
(macro lst
92+
(define (impl ls)
93+
(if (null? ls)
94+
#f
95+
(if (cons? ls)
96+
(if (null? (cdr ls))
97+
(car ls)
98+
`(if ,(car ls)
99+
,(car ls)
100+
,(impl (cdr ls))
101+
)
102+
)
103+
#f
104+
)
105+
)
106+
)
107+
(impl lst)
108+
)
109+
)
110+
69111
(define mycond
70112
(macro clauses
71113
(define (cond-clauses->if lst)

include/lisp/evaluator.h

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -451,42 +451,6 @@ class Sequence final : public Expr
451451
}
452452
};
453453

454-
class And final : public Expr
455-
{
456-
std::vector<ExprPtr> mActions;
457-
public:
458-
And(std::vector<ExprPtr> actions)
459-
: mActions{actions}
460-
{}
461-
ExprPtr eval(std::shared_ptr<Env> const& env) override
462-
{
463-
bool const result = std::all_of(mActions.begin(), mActions.end(), [&env](auto& e){ return isTrue(e->eval(env)); });
464-
return result ? true_() : false_();
465-
}
466-
std::string toString() const override
467-
{
468-
return "And";
469-
}
470-
};
471-
472-
class Or final : public Expr
473-
{
474-
std::vector<ExprPtr> mActions;
475-
public:
476-
Or(std::vector<ExprPtr> actions)
477-
: mActions{actions}
478-
{}
479-
ExprPtr eval(std::shared_ptr<Env> const& env) override
480-
{
481-
bool const result = std::any_of(mActions.begin(), mActions.end(), [&env](auto& e){ return isTrue(e->eval(env)); });
482-
return result ? true_() : false_();
483-
}
484-
std::string toString() const override
485-
{
486-
return "Or";
487-
}
488-
};
489-
490454
template <typename ProcedureT>
491455
class LambdaBase : public Expr
492456
{

include/lisp/parser.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,6 @@ inline std::shared_ptr<Sequence> sequence(ExprPtr const& expr)
9090
return std::make_shared<Sequence>(parseActions(expr));
9191
}
9292

93-
inline ExprPtr and_(ExprPtr const& expr)
94-
{
95-
return std::make_shared<And>(parseActions(expr));
96-
}
97-
98-
inline ExprPtr or_(ExprPtr const& expr)
99-
{
100-
return std::make_shared<Or>(parseActions(expr));
101-
}
102-
10393
inline auto parseAsQuoted(ExprPtr const& expr, std::optional<int32_t> quasiquoteLevel) -> ExprPtr;
10494

10595
inline ExprPtr assertIsLastAndGet(ExprPtr const& expr)
@@ -271,8 +261,6 @@ inline auto tryCons(ExprPtr const& expr) -> ExprPtr
271261
keywordToHandler["if"] = if_;
272262
keywordToHandler["cond"] = cond;
273263
keywordToHandler["begin"] = [](auto cdr){ return std::static_pointer_cast<Expr>(sequence(cdr));};
274-
keywordToHandler["and"] = and_;
275-
keywordToHandler["or"] = or_;
276264
keywordToHandler["quote"] = quote;
277265
keywordToHandler["quasiquote"] = quasiquote;
278266
keywordToHandler["_"] = [car](auto cdr){ return application(car, cdr); };

sample/CMakeLists.txt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ do_test(test_define_proc "(define (x) 2) (x)" 2)
3636
do_test(test_cond1 "(define x #t) (cond (x 1) (else 2))" 1)
3737
do_test(test_cond2 "(define x #f) (cond (x 1) (else 2))" 2)
3838

39-
do_test(test_cond3
40-
"
41-
(define lat? (lambda (l) (cond ((null? l) #t) ((atom? (car l)) (lat? (cdr l))) (else #f)))) (lat? (list 1 2 (list 3)))
42-
"
43-
false)
44-
4539
do_test(test_string1
4640
"
4741
(define member?
@@ -152,4 +146,16 @@ do_test(test_quasiquote_nested "`(1 `,(+ 1 ,(+ 2 3)) 4)" "\\\\(1 \\\\(\\\\'quasi
152146
do_test(test_unquote-splicing-nested "`(1 ```,,@,,@(list (+ 1 2)) 4)" "\\\\(1 \\\\(\\\\'quasiquote \\\\(\\\\'quasiquote \\\\(\\\\'quasiquote \\\\(\\\\'unquote \\\\(\\\\'unquote-splicing \\\\(\\\\'unquote 3\\\\)\\\\)\\\\)\\\\)\\\\)\\\\) 4\\\\)")
153147

154148
do_test(test_unquote-splicing "(let ((x '(1))) `(,@x 1))" "\\\\(1 1\\\\)")
155-
do_test(test_unquote-splicing2 "`(1 2 ,@(list (+ 1 2) (- 5 1)) 5)" "\\\\(1 2 3 4 5\\\\)")
149+
do_test(test_unquote-splicing2 "`(1 2 ,@(list (+ 1 2) (- 5 1)) 5)" "\\\\(1 2 3 4 5\\\\)")
150+
151+
do_test(test_and_1 "(and)" "true")
152+
do_test(test_and_2 "(and 1)" "1")
153+
do_test(test_and_3 "(and 1 \"2\")" "\"2\"")
154+
do_test(test_and_4 "(and 1 \"2\" 'x)" "\'x")
155+
do_test(test_and_5 "(and (or 1 \"2\") 'x)" "\'x")
156+
157+
do_test(test_or_1 "(or)" "false")
158+
do_test(test_or_2 "(or 1)" "1")
159+
do_test(test_or_3 "(or \"2\" 1)" "\"2\"")
160+
do_test(test_or_4 "(or 'x \"2\" 1)" "\'x")
161+
do_test(test_or_5 "(or (and #f 'x) \"2\" 1)" "\'x")

sample/loop.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <filesystem>
66
namespace fs = std::filesystem;
77

8-
#define DEBUG 0
8+
#define DEBUG 1
99

1010
auto consOp = [](std::vector<std::shared_ptr<Expr>> const& args)
1111
{

test/lisp/test.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -512,23 +512,3 @@ TEST(Parser, begin2)
512512
}
513513
EXPECT_TRUE(p.eof());
514514
}
515-
516-
TEST(Parser, andor)
517-
{
518-
std::initializer_list<std::pair<std::string, std::string> > expected = {{"Or", "true"}};
519-
520-
Lexer lex("(or 1 2 (and 3) #t)");
521-
MetaParser p(lex);
522-
523-
auto env = std::make_shared<Env>();
524-
env->defineVariable("#t", true_());
525-
526-
for (auto s : expected)
527-
{
528-
auto me = p.sexpr();
529-
auto e = parse(me);
530-
EXPECT_EQ(e->toString(), s.first);
531-
EXPECT_EQ(e->eval(env)->toString(), s.second);
532-
}
533-
EXPECT_TRUE(p.eof());
534-
}

0 commit comments

Comments
 (0)