Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit51a3116

Browse files
committed
Add support for logical AND / OR operations
1 parentd5e1f65 commit51a3116

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

‎evaluator.js‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,55 @@ function analyze_sequence(stmts) {
330330

331331
}
332332

333+
/* BOOLEAN OPERATIONS (&& and ||) */
334+
335+
functionis_boolean_operation(stmt){
336+
returnis_tagged_list(stmt,"boolean_operation");
337+
}
338+
339+
functionname_of_boolean_op(stmt){
340+
returnhead(tail(head(tail(stmt))));
341+
}
342+
343+
functionboolean_op_left_arg(stmt){
344+
returnlist_ref(head(tail(tail(stmt))),0);
345+
}
346+
347+
functionboolean_op_right_arg(stmt){
348+
returnlist_ref(head(tail(tail(stmt))),1);
349+
}
350+
351+
functionanalyze_boolean_op(stmt){
352+
constleft_hand_expr_func=analyze(boolean_op_left_arg(stmt));
353+
constright_hand_expr_func=analyze(boolean_op_right_arg(stmt));
354+
355+
returnname_of_boolean_op(stmt)==="&&"
356+
?analyze_logical_and(left_hand_expr_func,right_hand_expr_func)
357+
:analyze_logical_or(left_hand_expr_func,right_hand_expr_func);
358+
}
359+
360+
functionanalyze_logical_and(left_hand_expr_func,right_hand_expr_func){
361+
return(env,succeed,fail)=>{
362+
left_hand_expr_func(env,
363+
(val,fail2)=>{
364+
val ?right_hand_expr_func(env,succeed,fail2)
365+
:succeed(false,fail2);
366+
},
367+
fail);
368+
};
369+
}
370+
371+
functionanalyze_logical_or(left_hand_expr_func,right_hand_expr_func){
372+
return(env,succeed,fail)=>{
373+
left_hand_expr_func(env,
374+
(val,fail2)=>{
375+
val ?succeed(true,fail2)
376+
:right_hand_expr_func(env,succeed,fail2);
377+
},
378+
fail);
379+
};
380+
}
381+
333382
/* FUNCTION APPLICATION */
334383

335384
// The core of our evaluator is formed by the
@@ -750,6 +799,8 @@ function analyze(stmt) {
750799
?analyze_require(stmt)
751800
:is_application(stmt)
752801
?analyze_application(stmt)
802+
:is_boolean_operation(stmt)
803+
?analyze_boolean_op(stmt)
753804
:error(stmt,"Unknown statement type in analyze");
754805
}
755806

‎test.js‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ function test_subfunction() {
3030
assert_equal(120,final_result);
3131
}
3232

33+
functiontest_binary_boolean_operations(){
34+
parse_and_eval("true && (false || true) && (true && false);");
35+
assert_equal(false,final_result);
36+
}
37+
38+
functiontest_shortcircuiting(){
39+
parse_and_eval("function foo() { return foo(); }\
40+
true || foo();"
41+
);
42+
43+
assert_equal(true,final_result);
44+
parse_and_eval("function foo() { return foo(); }\
45+
false && foo();"
46+
);
47+
48+
assert_equal(false,final_result);
49+
}
50+
3351
/* 1.2 Test Non-Deterministic Functionality */
3452

3553
/**
@@ -64,14 +82,18 @@ function test_nondet_require() {
6482
return low > high ? amb() : amb(low, int_between(low + 1, high)); \
6583
} \
6684
function is_divisible_by_3(x) { return (x % 3) === 0;} \
67-
let integer = int_between(4, 10); \
68-
require(is_divisible_by_3(integer)); \
85+
function is_even(x) { return (x % 2) === 0;} \
86+
function is_one(x) { return x === 1;}\
87+
let integer = int_between(1, 12); \
88+
require(is_one(integer) || (is_even(integer) && is_divisible_by_3(integer))); \
6989
integer;"
7090
);
7191

92+
assert_equal(1,final_result);
93+
try_again();
7294
assert_equal(6,final_result);
7395
try_again();
74-
assert_equal(9,final_result);
96+
assert_equal(12,final_result);
7597
try_again();
7698
assert_equal(null,final_result);
7799
}
@@ -117,7 +139,8 @@ run(
117139
test_assignment,
118140
test_conditional_expressions,
119141
test_function,
120-
test_subfunction,
142+
test_subfunction,
143+
test_shortcircuiting,
121144
test_nondet_empty,
122145
test_nondet_infinite,
123146
test_nondet_require,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp