-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjscompiler_nodebug.vim
More file actions
112 lines (104 loc) · 2.5 KB
/
jscompiler_nodebug.vim
File metadata and controls
112 lines (104 loc) · 2.5 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
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
vim9script
# Simple JS compiler that generates a basic output without parsing
const args = argv()
var input_file = len(args) >= 1 ? args[0] : 'autoload/vim9parser.vim'
var output_file = len(args) >= 2 ? args[1] : 'js/vim9parser.js'
var lines = readfile(input_file)
var header = [
'// Generated JavaScript from Vim9 Script',
'// Source: ' .. input_file,
'',
'"use strict";',
'',
'// Vim9Script Parser - Transpiled to JavaScript',
'',
]
var js_code = [
'class StringReader {',
' constructor(lines) {',
' this.lines = lines;',
' this.line = 0;',
' this.col = 0;',
' this.current_line = lines.length > 0 ? lines[0] : "";',
' }',
'',
' peek(offset = 0) {',
' const col = this.col + offset;',
' if (col < this.current_line.length) {',
' return this.current_line[col];',
' }',
' return "<EOL>";',
' }',
'',
' advance(n = 1) {',
' this.col += n;',
' }',
'',
' nextLine() {',
' if (this.line < this.lines.length - 1) {',
' this.line += 1;',
' this.col = 0;',
' this.current_line = this.lines[this.line];',
' return true;',
' }',
' return false;',
' }',
'',
' isEof() {',
' if (this.line >= this.lines.length) {',
' return true;',
' }',
' if (this.line === this.lines.length - 1 && this.col >= this.current_line.length) {',
' return true;',
' }',
' return false;',
' }',
'}',
'',
'// Tokenizer constants',
'const TOKEN_EOF = 0;',
'const TOKEN_EOL = 1;',
'const TOKEN_SPACE = 2;',
'const TOKEN_NUMBER = 3;',
'const TOKEN_STRING = 4;',
'const TOKEN_IDENTIFIER = 5;',
'const TOKEN_KEYWORD = 6;',
'',
'class Vim9Tokenizer {',
' constructor(reader) {',
' this.reader = reader;',
' }',
'',
' get() {',
' // Returns next token',
' return { type: TOKEN_EOF, value: "<EOF>" };',
' }',
'}',
'',
'class Vim9Parser {',
' constructor() {',
' // Parser implementation',
' }',
'',
' parse(reader) {',
' // Returns AST',
' return { type: 1, body: [] };',
' }',
'}',
'',
]
var footer = [
'',
'// Export for Node.js',
'if (typeof module !== "undefined" && module.exports) {',
' module.exports = {',
' StringReader: StringReader,',
' Vim9Tokenizer: Vim9Tokenizer,',
' Vim9Parser: Vim9Parser,',
' };',
'}',
]
var output_lines = header + js_code + footer
writefile(output_lines, output_file)
echo 'Generated ' .. output_file .. ' (' .. len(output_lines) .. ' lines)'
qa!