Skip to content

Commit 0a66eb1

Browse files
author
E5war5IT
committed
new features
1 parent 8da3129 commit 0a66eb1

File tree

20 files changed

+283
-75
lines changed

20 files changed

+283
-75
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ tests/*
22
tests/node_modules/*
33
node_modules/
44
examples/js-compiler/package-lock.json
5+
!tests/output.bytex

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
```sh
77
npm i llvm.js
88
```
9+
> [!NOTE]
10+
> [URL npm](https://www.npmjs.com/package/llvm.js)
911
1012
## git
1113
```
@@ -94,7 +96,7 @@ codeGen.genSLFunction('print');
9496
codeGen.callFunction('print', '1');
9597
codeGen.codegen('output');
9698

97-
fs.readFileSync('output.bc-asmx').toString('utf8');
99+
fs.readFileSync('output.bytex').toString('utf8');
98100
```
99101

100102
Output:
@@ -115,6 +117,19 @@ main code:
115117
# Documentation
116118

117119
## [@llvm.js/llvm](./documentation/llvm/README.md)
120+
- **Config**
121+
- **set(object: IConfig)**
122+
- **setCommentLine(string: string)**
123+
- **forbiddenSymbol(char: string)**
124+
- **setCommentBlock(string: string)**
125+
- **setCommentBlock(string: string[])**
126+
- **clearCommentLine( )**
127+
- **clearCommentBlock( )**
128+
- **setSupportNumberSnake(bool: boolean)**
129+
- **clearSupportNumberSnake( )**
130+
- **Grammar**
131+
- **verifyGrammar(current: number, ast: Array, grammar: Array, strict: boolean): object**
132+
- **verifyGrammarNoStrict(current: number, ast: Array, grammar: Array): object**
118133

119134
## [@llvm.js/codegen](./documentation/codegen/README.md)
120135

documentation/llvm/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
# llvm.js/llvm
1+
# llvm.js/llvm
2+
3+
`llvm.Config.function()` is a set of functions for configuring the lexer configuration in the project llvm.js . Below is the documentation for each function:
4+
5+
- `setCommentLine(string: string)`: Sets a one-line comment for the lexer. Accepts a string as an argument containing the text of the comment.
6+
7+
- `forbiddenSymbol(char: char | char[])`: Sets forbidden characters to which the lexer will respond with an error. It can take a character or an array of characters as an argument.
8+
9+
- `setCommentBlock(string: string)`: Sets a block comment. If the argument is a string, then the end of the block comment will be determined by the reverse order of characters in the argument. If the argument is an array, then the first element will be used as the beginning of the block comment, and the second element as its end.
10+
11+
- `clearCommentLine()`: Clears data about the presentation of single-line comments.
12+
13+
- `clearCommentBlock()`: Clears data about the presentation of block comments.
14+
15+
- `setSupportNumberSnake(bool: boolean)`: Sets whether to enable or disable support for the underscore character `_` in numbers. Takes the boolean value `true` to enable and `false` to disable support.
16+
17+
- `clearSupportNumberSnake()`: Clears the data and returns the initial value of the underscore character support setting in numbers.

src/llvm/codegen/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class CodeGen {
117117

118118

119119
static codegen(filename = 'output-code') {
120-
let OUTPUT_FILE_PATH = `${filename}.bc-asmx`;
120+
let OUTPUT_FILE_PATH = `${filename}.bytex`;
121121
let outputAST = {};
122122
let output = {};
123123

src/llvm/exceptions/src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ class TokenException extends Exception {
6060
super(message, false);
6161
console.log(`[${token.line}:${token.current}] ${message} ${view ? `"${token.lexem}"` : ''}`);
6262
console.log(`${token.line} | ${token.code}`);
63-
let startIndex = token?.lexem.length > 0 ? token.current == 0 ? token.current : token.current - token.lexem.length : token.current;
64-
console.log(`${' '.repeat(String(token.line).length)} | ${' '.repeat(startIndex)}^${'-'.repeat(token.lexem.length == 1 ? null : token.lexem.length - 1)}`);
63+
console.log(`${' '.repeat(String(token.line).length)} | ${' '.repeat(token.current)}^${'-'.repeat(token.lexem.length == 1 ? null : token.lexem.length - 1)}`);
6564
}
6665
}
6766

src/llvm/llvm/dist/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ export declare interface IConfig {
5050
export declare class Config {
5151
static config: IConfig;
5252
set(object: IConfig): any;
53-
static setCommentLine(strin: string): void;
53+
static setCommentLine(string: string): void;
5454
static forbiddenSymbol(char: string): void;
5555
static setCommentBlock(string: string): void;
5656
static setCommentBlock(string: string[]): void;
5757
static clearCommentLine(): void;
5858
static clearCommentBlock(): void;
59+
static setSupportNumberSnake(bool: boolean): void;
60+
static clearSupportNumberSnake(): void;
5961
}
6062

6163
export * from '@llvm.js/llvm';

src/llvm/llvm/src/config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class Config {
1919

2020
keyword: [],
2121
forbidden: []
22+
},
23+
24+
syntax: {
25+
supportNumberStyleSnakeCase: true
2226
}
2327
}
2428

@@ -40,6 +44,13 @@ class Config {
4044
}
4145
}
4246

47+
static setSupportNumberSnake(bool) {
48+
if (typeof bool === 'boolean') this.config.syntax.supportNumberStyleSnakeCase = bool;
49+
}
50+
51+
static clearSupportNumberSnake() {
52+
this.config.syntax.supportNumberStyleSnakeCase = true;
53+
}
4354

4455
static setCommentLine(string) {
4556
if (typeof string === 'string') this.config.grammar.comment.line = string;

src/llvm/llvm/src/lexer.js

Lines changed: 101 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const Config = require("../src/config");
2-
const Grammar = require("./grammar");
32
const Token = require("./token");
43
const tokenType = require("./token.type");
54

@@ -71,8 +70,8 @@ class Lexer {
7170
}
7271

7372

74-
addTokenType(type, char) {
75-
this.ast.push(new Token(type, char ? char : this.getChar(), this.current, this.line, this.getLine()));
73+
addTokenType(type, char, current) {
74+
this.ast.push(new Token(type, char ? char : this.getChar(), current != undefined ? current : this.current, this.line, this.getLine()));
7675
}
7776

7877

@@ -164,14 +163,15 @@ class Lexer {
164163
else if (['\'', '"'].includes(char)) {
165164
let quote = ['\'', '"'][['\'', '"'].indexOf(char)];
166165
let string = quote;
166+
let currentBuffer = this.current;
167167
this.current++;
168168

169169
while (this.current < this.getLine().length) {
170170
let char_t = this.getLine()[this.current];
171171
string += char_t;
172172

173173
if (char_t == quote) {
174-
this.addTokenType(tokenType.get('STRING'), string);
174+
this.addTokenType(tokenType.get('STRING'), string, currentBuffer);
175175
break;
176176
}
177177

@@ -188,32 +188,103 @@ class Lexer {
188188

189189
else if (/[a-zA-Z]/.test(char)) {
190190
let identifer = '';
191+
let currentBuffer = this.current;
191192

192193
while (this.current < this.getLine().length) {
193-
let char_t = this.getLine()[this.current];
194-
195-
if (/[a-zA-Z]/.test(char_t) || /[0-9]/.test(char_t)) identifer += char_t;
196-
else {
197-
this.addTokenType(tokenType.get('IDENTIFER'), identifer);
194+
const char_t = this.getLine()[this.current];
195+
196+
if (this.current == this.getLine().length - 1) {
197+
if (/[a-zA-Z]/.test(char_t) || /[0-9]/.test(char_t)) {
198+
this.addTokenType(tokenType.get('IDENTIFER'), identifer + char_t, currentBuffer);
199+
this.current++;
200+
break;
201+
} else {
202+
this.addTokenType(tokenType.get('IDENTIFER'), identifer, currentBuffer);
203+
this.scanToken(char_t);
204+
break;
205+
}
206+
}
207+
208+
else if (/[a-zA-Z]/.test(char_t) || /[0-9]/.test(char_t)) {
209+
identifer += char_t;
210+
} else {
211+
this.addTokenType(tokenType.get('IDENTIFER'), identifer, currentBuffer);
198212
this.scanToken(char_t);
199213
break;
200214
}
215+
201216
this.current++;
202217
}
203218

204219
}
205220

206221
else if (/[0-9]/.test(char)) {
207222
let number = '';
223+
let countDot = 0;
208224

209225
while (this.current < this.getLine().length) {
210-
let char_t = this.getLine()[this.current];
211-
if (/[0-9]/.test(char_t)) number += char_t;
212-
else {
213-
this.addTokenType(tokenType.get('NUMBER'), number);
214-
this.scanToken(char_t);
226+
const char_t = this.getLine()[this.current];
227+
228+
if (this.current == this.getLine().length - 1) {
229+
function is() {
230+
if (/[0-9]/.test(char_t)) {
231+
return true;
232+
} else if (char_t == '_') {
233+
let previousToken = this.getLine()[this.current - 1];
234+
let nextToken = this.getLine()[this.current + 1];
235+
236+
if (Config.config.syntax.supportNumberStyleSnakeCase == false) {
237+
return false;
238+
} else {
239+
return [previousToken == '.', nextToken == '.'].includes(true);
240+
}
241+
} else if (char_t == '.') {
242+
return countDot < 1;
243+
}
244+
}
245+
246+
if (is.call(this)) {
247+
this.addTokenType(tokenType.get('NUMBER'), number + char_t, this.current - number.length);
248+
this.current++;
249+
break;
250+
} else {
251+
this.addTokenType(tokenType.get('NUMBER'), number, this.current - number.length);
252+
this.scanToken(char_t);
253+
break;
254+
}
255+
}
256+
257+
else if (/[0-9]/.test(char_t)) {
258+
number += char_t;
259+
} else if (char_t == '_') {
260+
let previousToken = this.getLine()[this.current - 1];
261+
let nextToken = this.getLine()[this.current + 1];
262+
263+
if (Config.config.syntax.supportNumberStyleSnakeCase == false) {
264+
this.addTokenType(tokenType.get('NUMBER'), number, this.current - number.length);
265+
this.scanToken(char_t);
266+
break;
267+
} else if ([previousToken == '.', nextToken == '.'].includes(true)) {
268+
this.addTokenType(tokenType.get('NUMBER'), number, this.current - number.length);
269+
this.scanToken(char_t);
270+
break;
271+
} else {
272+
number += char_t;
273+
}
274+
} else if (char_t == '.') {
275+
if (countDot == 1) {
276+
this.addTokenType(tokenType.get('NUMBER'), number, this.current - number.length);
277+
this.scanToken(char_t);
278+
break;
279+
}
280+
281+
number += char_t;
282+
countDot++;
283+
} else {
284+
this.addTokenType(tokenType.get('NUMBER'), number, this.current - number.length);
215285
break;
216286
}
287+
217288
this.current++;
218289
}
219290
}
@@ -263,6 +334,10 @@ class Lexer {
263334
this.current++;
264335
break;
265336

337+
case '_':
338+
this.addTokenType(tokenType.get('UNDERSCORE'));
339+
this.current++;
340+
break;
266341
case '@':
267342
this.addTokenType(tokenType.get('DOGE'));
268343
this.current++;
@@ -279,10 +354,6 @@ class Lexer {
279354
this.addTokenType(tokenType.get('HASH'));
280355
this.current++;
281356
break;
282-
case '<':
283-
this.addTokenType(tokenType.get('OPEN_ANGLE'));
284-
this.current++;
285-
break;
286357

287358
default:
288359
if (['high', 'middle'].includes(Config.config.language.level)) {
@@ -308,10 +379,20 @@ class Lexer {
308379

309380
else if (char == '>') {
310381
if (this.getNextChar() == '=') {
311-
this.addTokenType(tokenType.get('LESS_EQUAL'), '>=');
382+
this.addTokenType(tokenType.get('GREATER_EQUAL'), '>=');
383+
this.current += 2;
384+
} else {
385+
this.addTokenType(tokenType.get('GREATER'));
386+
this.current++;
387+
}
388+
}
389+
390+
else if (char == '<') {
391+
if (this.getNextChar() == '=') {
392+
this.addTokenType(tokenType.get('LESS_EQUAL'), '<=');
312393
this.current += 2;
313394
} else {
314-
this.addTokenType(tokenType.get('CLOSE_ANGLE'));
395+
this.addTokenType(tokenType.get('LESS'));
315396
this.current++;
316397
}
317398
}

src/llvm/llvm/src/token.type.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ const ListChain = require('../../listchain');
33
let tt = [
44
'number', 'identifer', 'string', 'comment', // 0-9 a-zA-Z "string"
55
'open_paren', 'close_paren', // ()
6-
'open_angle', 'close_angle', // <>
76
'open_brace', 'close_brace', // {}
87
'open_square_bracket', 'close_square_bracket', // []
98
'string_quote', 'string_double_quote', // ' "
10-
'equal', 'bang_equal', 'equal_equal', 'less_equal', // = != == >=
9+
'equal', // =
10+
'bang_equal', 'equal_equal', // != ==
11+
'less_equal', 'greater_equal', // <= >=
1112
'whitespace', // \r \t \n
1213
'minus', 'plus', // - +
1314
'dot', 'comma', // . ,
@@ -22,6 +23,8 @@ let tt = [
2223
'precent', // %
2324
'pipe', 'ampersand', // | &
2425
'doge', // @
26+
'underscore', // _
27+
'less', 'greater', // < >
2528
'plus_equal', 'minus_equal', // += ==
2629
'star_equal', 'caret_equal', // *= ^=
2730
'slash_equal', 'percent_equal', // /= %=

src/llvm/package.json

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
11
{
22
"name": "llvm.js",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"files": [
55
"**/dist/",
66
"**/src/*.js",
77
"**/package.json",
88
"/*.js"
99
],
10-
1110
"exports": {
1211
"./expression": {
1312
"types": "./expression/dist/index.d.ts",
1413
"require": "./expression/src/index.js"
1514
},
16-
1715
"./llvm": {
1816
"types": "./llvm/dist/index.d.ts",
1917
"require": "./llvm/src/index.js"
2018
},
21-
2219
"./exceptions": {
2320
"types": "./exceptions/dist/index.d.ts",
2421
"require": "./exceptions/src/index.js"
2522
},
26-
2723
"./codegen": {
2824
"types": "./codegen/dist/index.d.ts",
2925
"require": "./codegen/src/index.js"
3026
},
31-
3227
"./package.json": "./package.json"
3328
},
34-
3529
"components": [
3630
"expression",
3731
"llvm",
3832
"exceptions",
3933
"codegen"
4034
],
41-
4235
"repository": {
4336
"type": "git",
4437
"url": "https://github.com/llvm-js/llvm-project.git"
4538
},
46-
4739
"typings": "empty.d.ts"
4840
}

0 commit comments

Comments
 (0)