Skip to content

Commit bd87748

Browse files
WangYuyang1013WangdahaiWangdahai
authored
Feature translator (#49)
* Implement translator feature * Implement translator feature * Implement translator feature * add commend on double to string --------- Co-authored-by: Wangdahai <wangdahai@192.168.1.137> Co-authored-by: Wangdahai <wangdahai@WangdahaideMacBook-Air.local>
1 parent 9a3eceb commit bd87748

12 files changed

+3920
-4888
lines changed

package-lock.json

+3,255-4,806
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"name": "py-slang",
3+
"packageManager": "yarn@1.22.22",
34
"version": "1.0.0",
45
"description": "",
5-
"main": "build/index.js",
6-
"types": "build/index.d.ts",
6+
"main": "dist/index.js",
7+
"types": "dist/index.d.ts",
78
"scripts": {
8-
"regen": "npm run build && node build/generate.js",
9+
"regen": "npm run build && node dist/generate.js",
910
"start:dev": "npx nodemon",
10-
"build": "rimraf ./build && tsc",
11-
"start": "npm run build && node build/index.js",
11+
"build": "rollup -c --bundleConfigAsCjs",
12+
"start": "npm run build && node dist/index.js",
13+
"jsdoc": "./scripts/jsdoc.sh",
1214
"test": "jest"
1315
},
1416
"keywords": [
@@ -22,16 +24,30 @@
2224
},
2325
"license": "Apache-2.0",
2426
"devDependencies": {
27+
"@rollup/plugin-commonjs": "^28.0.3",
28+
"@rollup/plugin-node-resolve": "^16.0.1",
29+
"@rollup/plugin-terser": "^0.4.4",
30+
"@rollup/plugin-typescript": "^12.1.2",
31+
"@types/fast-levenshtein": "^0.0.4",
2532
"@types/jest": "^29.4.0",
26-
"@types/node": "^18.11.13",
33+
"@types/mathjs": "^9.4.1",
34+
"@types/node": "^18.19.84",
35+
"glob": "^11.0.1",
36+
"jest": "^29.7.0",
37+
"jsdoc": "^4.0.4",
2738
"nodemon": "^2.0.20",
2839
"rimraf": "^3.0.2",
40+
"rollup": "^4.38.0",
41+
"rollup-plugin-modify": "^3.0.0",
42+
"taffydb": "^2.7.3",
2943
"ts-jest": "^29.0.5",
3044
"ts-node": "^10.9.1",
31-
"typescript": "^4.9.4"
45+
"tslib": "^2.8.1",
46+
"typescript": "^5.5.3"
3247
},
3348
"dependencies": {
3449
"@types/estree": "^1.0.0",
35-
"fast-levenshtein": "^3.0.0"
50+
"fast-levenshtein": "^3.0.0",
51+
"mathjs": "^14.4.0"
3652
}
3753
}

rollup.config.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import resolve from '@rollup/plugin-node-resolve';
2+
import commonjs from '@rollup/plugin-commonjs';
3+
import typescript from '@rollup/plugin-typescript';
4+
5+
/**
6+
* @type {import('rollup').RollupOptions}
7+
*/
8+
const config = {
9+
input: 'src/index.ts',
10+
output: {
11+
file: 'dist/index.js',
12+
format: 'umd',
13+
name: 'PySlangRunner',
14+
sourcemap: true
15+
},
16+
plugins: [
17+
resolve(),
18+
commonjs(),
19+
typescript({
20+
tsconfig: './tsconfig.json'
21+
})
22+
]
23+
};
24+
25+
export default config;

src/ast-types.ts

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/errors.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {Token} from "./tokenizer";
22
import {Position} from "estree";
33

4-
/*The offset is calculated as follows:
5-
Current position is one after real position of end of token: 1
4+
/*
5+
The offset is calculated as follows:
6+
Current position is one after real position of end of token: 1
67
*/
78
const MAGIC_OFFSET = 1;
89

@@ -13,7 +14,6 @@ function escape(unsafe: string): string {
1314
return unsafe.replace(SPECIAL_CHARS, "\\$&");
1415
}
1516

16-
1717
/* Searches backwards and forwards till it hits a newline */
1818
function getFullLine(source: string, current: number): string {
1919
let back: number = current;

src/parser.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -499,16 +499,19 @@ export class Parser {
499499
const startToken = this.peek();
500500
if (this.match(TokenType.TRUE)) return new ExprNS.Literal(startToken, this.previous(), true);
501501
if (this.match(TokenType.FALSE)) return new ExprNS.Literal(startToken, this.previous(), false);
502-
502+
if (this.match(TokenType.NONE)) return new ExprNS.None(startToken, this.previous());
503503
if (this.match(TokenType.STRING)) {
504504
return new ExprNS.Literal(startToken, this.previous(), this.previous().lexeme);
505505
}
506506
if (this.match(TokenType.NUMBER)) {
507-
return new ExprNS.Literal(startToken, this.previous(), Number(this.previous().lexeme));
507+
return new ExprNS.Literal(startToken, this.previous(), Number(this.previous().lexeme.replace(/_/g, "")));
508508
}
509509
if (this.match(TokenType.BIGINT)) {
510510
return new ExprNS.BigIntLiteral(startToken, this.previous(), this.previous().lexeme);
511511
}
512+
if (this.match(TokenType.COMPLEX)) {
513+
return new ExprNS.Complex(startToken, this.previous(), this.previous().lexeme);
514+
}
512515

513516
if (this.match(TokenType.NAME, ...PSEUD_NAMES)) {
514517
return new ExprNS.Variable(startToken, this.previous(), this.previous());

src/resolver.ts

+43-27
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { Token } from "./tokenizer";
55
import { TokenType } from "./tokens";
66
import { ResolverErrors } from "./errors";
77

8-
const levenshtein = require('fast-levenshtein');
8+
import levenshtein from 'fast-levenshtein';
9+
// const levenshtein = require('fast-levenshtein');
910

1011
const RedefineableTokenSentinel = new Token(TokenType.AT, "", 0, 0, 0);
1112

@@ -149,23 +150,20 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
149150
// The global environment
150151
this.environment = new Environment(source, null, new Map([
151152
// misc library
152-
["get_time", new Token(TokenType.NAME, "get_time", 0, 0, 0)],
153+
["_int", new Token(TokenType.NAME, "_int", 0, 0, 0)],
154+
["_int_from_string", new Token(TokenType.NAME, "_int_from_string", 0, 0, 0)],
155+
["abs", new Token(TokenType.NAME, "abs", 0, 0, 0)],
156+
["char_at", new Token(TokenType.NAME, "char_at", 0, 0, 0)],
157+
["error", new Token(TokenType.NAME, "error", 0, 0, 0)],
158+
["input", new Token(TokenType.NAME, "input", 0, 0, 0)],
159+
["isinstance", new Token(TokenType.NAME, "isinstance", 0, 0, 0)],
160+
["max", new Token(TokenType.NAME, "max", 0, 0, 0)],
161+
["min", new Token(TokenType.NAME, "min", 0, 0, 0)],
153162
["print", new Token(TokenType.NAME, "print", 0, 0, 0)],
154-
["raw_print", new Token(TokenType.NAME, "raw_print", 0, 0, 0)],
163+
["random_random", new Token(TokenType.NAME, "random_random", 0, 0, 0)],
164+
["round", new Token(TokenType.NAME, "round", 0, 0, 0)],
155165
["str", new Token(TokenType.NAME, "str", 0, 0, 0)],
156-
["error", new Token(TokenType.NAME, "error", 0, 0, 0)],
157-
["prompt", new Token(TokenType.NAME, "prompt", 0, 0, 0)],
158-
["is_float", new Token(TokenType.NAME, "is_float", 0, 0, 0)],
159-
["is_int", new Token(TokenType.NAME, "is_int", 0, 0, 0)],
160-
["is_string", new Token(TokenType.NAME, "is_string", 0, 0, 0)],
161-
["is_function", new Token(TokenType.NAME, "is_function", 0, 0, 0)],
162-
["is_boolean", new Token(TokenType.NAME, "is_boolean", 0, 0, 0)],
163-
["parse_int", new Token(TokenType.NAME, "parse_int", 0, 0, 0)],
164-
["char_at", new Token(TokenType.NAME, "char_at", 0, 0, 0)],
165-
["arity", new Token(TokenType.NAME, "arity", 0, 0, 0)],
166-
["None", new Token(TokenType.NAME, "None", 0, 0, 0)],
167-
["NaN", new Token(TokenType.NAME, "NaN", 0, 0, 0)],
168-
["Infinity", new Token(TokenType.NAME, "Infinity", 0, 0, 0)],
166+
["time_time", new Token(TokenType.NAME, "time_time", 0, 0, 0)],
169167

170168
// math constants
171169
["math_pi", new Token(TokenType.NAME, "math_pi", 0, 0, 0)],
@@ -175,7 +173,6 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
175173
["math_tau", new Token(TokenType.NAME, "math_tau", 0, 0, 0)],
176174

177175
// math library
178-
["math_abs", new Token(TokenType.NAME, "math_abs", 0, 0, 0)],
179176
["math_acos", new Token(TokenType.NAME, "math_acos", 0, 0, 0)],
180177
["math_acosh", new Token(TokenType.NAME, "math_acosh", 0, 0, 0)],
181178
["math_asin", new Token(TokenType.NAME, "math_asin", 0, 0, 0)],
@@ -185,31 +182,46 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
185182
["math_atanh", new Token(TokenType.NAME, "math_atanh", 0, 0, 0)],
186183
["math_cbrt", new Token(TokenType.NAME, "math_cbrt", 0, 0, 0)],
187184
["math_ceil", new Token(TokenType.NAME, "math_ceil", 0, 0, 0)],
188-
["math_clz32", new Token(TokenType.NAME, "math_clz32", 0, 0, 0)],
185+
["math_comb", new Token(TokenType.NAME, "math_comb", 0, 0, 0)],
186+
["math_copysign", new Token(TokenType.NAME, "math_copysign", 0, 0, 0)],
189187
["math_cos", new Token(TokenType.NAME, "math_cos", 0, 0, 0)],
190188
["math_cosh", new Token(TokenType.NAME, "math_cosh", 0, 0, 0)],
189+
["math_degrees", new Token(TokenType.NAME, "math_degrees", 0, 0, 0)],
190+
["math_erf", new Token(TokenType.NAME, "math_erf", 0, 0, 0)],
191+
["math_erfc", new Token(TokenType.NAME, "math_erfc", 0, 0, 0)],
191192
["math_exp", new Token(TokenType.NAME, "math_exp", 0, 0, 0)],
193+
["math_exp2", new Token(TokenType.NAME, "math_exp2", 0, 0, 0)],
192194
["math_expm1", new Token(TokenType.NAME, "math_expm1", 0, 0, 0)],
195+
["math_fabs", new Token(TokenType.NAME, "math_fabs", 0, 0, 0)],
196+
["math_factorial", new Token(TokenType.NAME, "math_factorial", 0, 0, 0)],
193197
["math_floor", new Token(TokenType.NAME, "math_floor", 0, 0, 0)],
194-
["math_fround", new Token(TokenType.NAME, "math_fround", 0, 0, 0)],
195-
["math_hypot", new Token(TokenType.NAME, "math_hypot", 0, 0, 0)],
196-
["math_imul", new Token(TokenType.NAME, "math_imul", 0, 0, 0)],
198+
["math_fma", new Token(TokenType.NAME, "math_fma", 0, 0, 0)],
199+
["math_fmod", new Token(TokenType.NAME, "math_fmod", 0, 0, 0)],
200+
["math_gamma", new Token(TokenType.NAME, "math_gamma", 0, 0, 0)],
201+
["math_gcd", new Token(TokenType.NAME, "math_gcd", 0, 0, 0)],
202+
["math_isfinite", new Token(TokenType.NAME, "math_isfinite", 0, 0, 0)],
203+
["math_isinf", new Token(TokenType.NAME, "math_isinf", 0, 0, 0)],
204+
["math_isnan", new Token(TokenType.NAME, "math_isnan", 0, 0, 0)],
205+
["math_isqrt", new Token(TokenType.NAME, "math_isqrt", 0, 0, 0)],
206+
["math_lcm", new Token(TokenType.NAME, "math_lcm", 0, 0, 0)],
207+
["math_ldexp", new Token(TokenType.NAME, "math_ldexp", 0, 0, 0)],
208+
["math_lgamma", new Token(TokenType.NAME, "math_lgamma", 0, 0, 0)],
197209
["math_log", new Token(TokenType.NAME, "math_log", 0, 0, 0)],
210+
["math_log10", new Token(TokenType.NAME, "math_log10", 0, 0, 0)],
198211
["math_log1p", new Token(TokenType.NAME, "math_log1p", 0, 0, 0)],
199212
["math_log2", new Token(TokenType.NAME, "math_log2", 0, 0, 0)],
200-
["math_log10", new Token(TokenType.NAME, "math_log10", 0, 0, 0)],
201-
["math_max", new Token(TokenType.NAME, "math_max", 0, 0, 0)],
202-
["math_min", new Token(TokenType.NAME, "math_min", 0, 0, 0)],
213+
["math_nextafter", new Token(TokenType.NAME, "math_nextafter", 0, 0, 0)],
214+
["math_perm", new Token(TokenType.NAME, "math_perm", 0, 0, 0)],
203215
["math_pow", new Token(TokenType.NAME, "math_pow", 0, 0, 0)],
204-
["math_random", new Token(TokenType.NAME, "math_random", 0, 0, 0)],
205-
["math_round", new Token(TokenType.NAME, "math_round", 0, 0, 0)],
206-
["math_sign", new Token(TokenType.NAME, "math_sign", 0, 0, 0)],
216+
["math_radians", new Token(TokenType.NAME, "math_radians", 0, 0, 0)],
217+
["math_remainder", new Token(TokenType.NAME, "math_remainder", 0, 0, 0)],
207218
["math_sin", new Token(TokenType.NAME, "math_sin", 0, 0, 0)],
208219
["math_sinh", new Token(TokenType.NAME, "math_sinh", 0, 0, 0)],
209220
["math_sqrt", new Token(TokenType.NAME, "math_sqrt", 0, 0, 0)],
210221
["math_tan", new Token(TokenType.NAME, "math_tan", 0, 0, 0)],
211222
["math_tanh", new Token(TokenType.NAME, "math_tanh", 0, 0, 0)],
212223
["math_trunc", new Token(TokenType.NAME, "math_trunc", 0, 0, 0)],
224+
["math_ulp", new Token(TokenType.NAME, "math_ulp", 0, 0, 0)]
213225
]));
214226
this.functionScope = null;
215227
}
@@ -442,9 +454,13 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
442454
this.resolve(expr.consequent);
443455
this.resolve(expr.alternative);
444456
}
457+
visitNoneExpr(expr: ExprNS.None): void {
458+
}
445459
visitLiteralExpr(expr: ExprNS.Literal): void {
446460
}
447461
visitBigIntLiteralExpr(expr: ExprNS.BigIntLiteral): void {
448462
}
463+
visitComplexExpr(expr: ExprNS.Complex): void {
464+
}
449465

450466
}

0 commit comments

Comments
 (0)