-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizerTest.js
59 lines (49 loc) · 1.24 KB
/
tokenizerTest.js
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
function testInput() {
return "(subtract 4 2)";
}
function expectedResult() {
return [
{ type: 'paren', value: '(' },
{ type: 'name', value: 'substract' },
{ type: 'number', value: '4' },
{ type: 'number', value: '2' },
{ type: 'paren', value: ')' },
];
}
function Lexer(program){
this.program = program
}
// don't patch built-in types on real applications
// might break things!
// prototypes
Array.prototype.first = function() {
return this[0]
}
Array.prototype.last = function() {
return this[this.length -1]
}
// 'this' führt hier dazu, dass 'program' nicht in der function übergeben werden muss
// besser. objektorientierung
Lexer.prototype.createTokens = function() {
console.log(this.program)
var arr = this.program.split("")
console.log(arr)
console.log(arr.first())
console.log(arr.last())
if(arr.first() === "(" && arr.last() === ")")
return [
{ type: 'paren', value: '(' },
{ type: 'paren', value: ')' },
];
else
throw new Error("Lexer Error!!!!")
}
function main(){
var lexer = new Lexer(testInput())
var tokens = lexer.createTokens()
console.log(tokens)
// var output = lex(testInput());
// console.log(output);
// console.log(lex("Something Else"))
}
main();