newJS, ничего такого я не имел в виду.
Множество всевозможных логических выражений бесконечно, проверками тут обойтись не получится.
«Парсер напиши» означает только одно — написать парсер. Это не сильно сложно, особенно если воспользоваться генераторами парсеров.
Собсно, захотелось всё же потренироваться, так что написал я парсер (с использованием генератора PEG.js). Парсер поддерживает:
- булевы константы: true и yes — истина, false и no — ложь;
- переменные (идентификаторы, состоящие из строчных букв латинского алфавита);
- пробелы (игнорируются);
- скобки для задания приоритета вычислений;
- левоассоциативные операторы & (конъюнкция) и | (дизъюнкция) с одинаковым приоритетом;
- унарный оператор ! (отрицание), обладающий наивысшим приоритетом;
Исходный код
index.htmlHTML5 |
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
| <!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>Вычисление логического выражения</title>
<link data-require="bootstrap-css@3.0.3" data-semver="3.0.3" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script src="https://github.com/dmajda/pegjs/releases/download/v0.8.0/peg-0.8.0.js"></script>
<script data-require="angular.js@1.2.x" src="http://code.angularjs.org/1.2.12/angular.js" data-semver="1.2.12"></script>
<script data-require="jquery@*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<script id="grammar" type="text/template">
{
function BooleanLiteral (value) {
this.evaluate = function () {
return value;
};
}
function Variable (variable) {
variable = variable.join('');
this.evaluate = function (env) {
if (!(variable in env)) {
throw new Error("Undefined variable `" + variable + "'");
}
var value = env[variable];
if ((/^(yes|true)$/).test(value)) return true;
if ((/^(no|false)$/).test(value)) return false;
throw new Error("Variable `" + variable + "' is not a boolean: " + value);
}
}
function Negate (arg) {
this.evaluate = function (env) {
return !arg.evaluate(env);
};
}
var dispatch = {
'&': function (a, b) { return a && b },
'|': function (a, b) { return a || b }
}
function BinOp (op1, o, op2) {
this.evaluate = function (env) {
return dispatch[o](op1.evaluate(env), op2.evaluate(env));
}
}
}
start
= expr
expr
= head:term rest:(op term)* {
var res = head;
rest.forEach(function (elements) {
res = new BinOp(res, elements[0], elements[1]);
});
return res;
}
op
= space o:('&' / '|') space { return o; }
term
= negate arg:(primary) { return new Negate(arg) }
/ primary
primary
= literal
/ variable
/ space '(' space expr:expr space ')' space { return expr; }
variable "variable" // lowercase letters
= space letters:[a-z]+ space { return new Variable(letters); }
literal
= space ('true'/'yes') space { return new BooleanLiteral(true); }
/ space ('false'/'no') space { return new BooleanLiteral(false); }
negate
= space '!' space
space
= ' '*
</script>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<form role="form" class="form-horizontal">
<div class="form-group" ng-class="{'has-error': expr && !isValid(expr) };">
<label class="control-label col-md-2" for="expr">Выражение</label>
<div class="col-md-10">
<input class="form-control" type="text" name="expr" ng-model="expr"
placeholder="Введите логическое выражение…" />
</div>
</div>
<div class="form-group" ng-class="{'has-success': result !== null };">
<label class="control-label col-md-2" for="result">Результат</label>
<div class="col-md-10">
<input type="text" class="form-control" name="result" disabled
value="{{ evaluate(expr, env) }}" />
</div>
</div>
<div class="row">
<label class="control-label col-md-2">Переменные</label>
</div>
<hr />
<div class="form-group" ng-repeat="variable in vars"
ng-class="{'has-error': !isEmptyOrBoolean(env[variable]) };">
<label class="control-label col-md-2">{{ variable }}</label>
<div class="col-md-10">
<div class="input-group">
<input type="text" class="form-control" ng-model="env[variable]"
placeholder="true, false, yes, no" />
<span class="input-group-btn">
<button class="btn btn-danger" ng-click="remove(variable);">
<span class="glyphicon glyphicon-remove"></span>
</button>
</span>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2 col-md-offset-2">Добавить</label>
<div class="col-md-8">
<div class="input-group">
<input type="text" class="form-control" ng-model="addvar"
placeholder="Введите название переменной…" />
<span class="input-group-btn">
<button class="btn btn-success" ng-click="add(addvar);">
<span class="glyphicon glyphicon-plus"></span>
</button>
</span>
</div>
</div>
</div>
<div class="row" ng-show="error">
<div class="col-md-12">
<p class="alert alert-danger">{{ error }}</p>
</div>
</div>
<div class="checkbox">
<label>
<input type="checkbox" ng-model="debug" />
Показать грамматику
</label>
</div>
</form>
</div>
</div>
<div class="row" ng-show="debug">
<div class="col-md-6 col-md-offset-3">
<pre>{{ grammar }}</pre>
</div>
</div>
</div>
</body>
</html> |
|
app.js
Javascript |
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
| var app = angular.module('plunker', []);
app.factory('Grammar', function () {
return $("#grammar").html();
});
app.factory('Parser', ['Grammar', function (Grammar) {
return PEG.buildParser(Grammar);
}]);
app.controller('MainCtrl', function ($scope, Grammar, Parser) {
$scope.env = {};
$scope.debug = false;
$scope.grammar = Grammar;
$scope.vars = ['x', 'y'];
$scope.isValid = function (expr) {
try {
Parser.parse(expr);
return true;
} catch(e) {
return false;
}
};
$scope.isEmptyOrBoolean = function (value) {
return $scope.isEmpty(value) || $scope.isBoolean(value);
};
$scope.isEmpty = function (value) {
if (value === null) return true;
if (value === undefined) return true;
if ((/^\s*$/).test(value)) return true;
return false;
};
$scope.isBoolean = function (value) {
return (/^\s*(true|false|yes|no)\s*$/).test(value);
};
$scope.remove = function (variable) {
delete $scope.env[variable];
$scope.vars.splice($scope.vars.indexOf(variable), 1);
console.log($scope.vars);
};
$scope.add = function () {
var variable = $scope.addvar;
if ($scope.isEmpty(variable)) return;
if (!(/^[a-z]+$/).test(variable)) return;
if ($scope.vars.indexOf(variable) > -1) return;
$scope.vars.push(variable);
$scope.addvar = '';
};
$scope.evaluate = function (expr, env) {
$scope.result = null;
var vars = $scope.vars;
for (var i = 0; i < vars.length; ++i) {
var variable = vars[i]
, value = env[variable];
if (!$scope.isEmptyOrBoolean(value)) {
$scope.error = "Variable `" + variable + "' is not a boolean: " + value;
return null;
}
}
try {
var tree = Parser.parse(expr);
$scope.result = tree.evaluate(env);
$scope.error = null;
return $scope.result;
} catch(e) {
$scope.error = e.message;
return null;
}
};
}); |
|
style.css
CSS |
1
2
3
4
5
6
7
| /* Put your css in here */
pre {
max-height: 600px;
overflow-y: auto;
margin-top: 10px;
} |
|
Демонстрацию можно посмотреть по ссылке:
http://embed.plnkr.co/HFEQ6SWcTYhLWUxOHbSA/preview