forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbehavioral_interpreter.js
More file actions
72 lines (59 loc) · 1.39 KB
/
behavioral_interpreter.js
File metadata and controls
72 lines (59 loc) · 1.39 KB
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
const INTERPRETER = {
id: 'interpteter',
name: 'Interpreter',
type: 'behavioral',
hint: 'A way to include language elements in a program',
definition: `Given a language, define a representation for its grammar along with an interpreter that
uses the representation to interpret sentences in the language.`,
when:
'you want to interpret given language and you can represent statements as an abstract syntax trees',
codeES5: `function Sum(left, right) {
this.left = left;
this.right = right;
}
Sum.prototype.pattern = function() {
return this.left.pattern() + this.right.pattern();
};
function Min(left, right) {
this.left = left;
this.right = right;
}
Min.prototype.pattern = function() {
return this.left.pattern() - this.right.pattern();
};
function Num(val) {
this.val = val;
}
Num.prototype.pattern = function() {
return this.val;
};
module.exports = [Num, Min, Sum];`,
codeES6: `class Sum {
constructor(left, right) {
this.left = left;
this.right = right;
}
pattern() {
return this.left.pattern() + this.right.pattern();
}
}
class Min {
constructor(left, right) {
this.left = left;
this.right = right;
}
pattern() {
return this.left.pattern() - this.right.pattern();
}
}
class Num {
constructor(val) {
this.val = val;
}
pattern() {
return this.val;
}
}
export { Num, Min, Sum };`
};
export default INTERPRETER;