Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8,061 changes: 3,255 additions & 4,806 deletions package-lock.json

Large diffs are not rendered by default.

32 changes: 24 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "py-slang",
"packageManager": "[email protected]",
"version": "1.0.0",
"description": "",
"main": "build/index.js",
"types": "build/index.d.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"regen": "npm run build && node build/generate.js",
"regen": "npm run build && node dist/generate.js",
"start:dev": "npx nodemon",
"build": "rimraf ./build && tsc",
"start": "npm run build && node build/index.js",
"build": "rollup -c --bundleConfigAsCjs",
"start": "npm run build && node dist/index.js",
"jsdoc": "./scripts/jsdoc.sh",
"test": "jest"
},
"keywords": [
Expand All @@ -22,16 +24,30 @@
},
"license": "Apache-2.0",
"devDependencies": {
"@rollup/plugin-commonjs": "^28.0.3",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^12.1.2",
"@types/fast-levenshtein": "^0.0.4",
"@types/jest": "^29.4.0",
"@types/node": "^18.11.13",
"@types/mathjs": "^9.4.1",
"@types/node": "^18.19.84",
"glob": "^11.0.1",
"jest": "^29.7.0",
"jsdoc": "^4.0.4",
"nodemon": "^2.0.20",
"rimraf": "^3.0.2",
"rollup": "^4.38.0",
"rollup-plugin-modify": "^3.0.0",
"taffydb": "^2.7.3",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"tslib": "^2.8.1",
"typescript": "^5.5.3"
},
"dependencies": {
"@types/estree": "^1.0.0",
"fast-levenshtein": "^3.0.0"
"fast-levenshtein": "^3.0.0",
"mathjs": "^14.4.0"
}
}
25 changes: 25 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';

/**
* @type {import('rollup').RollupOptions}
*/
const config = {
input: 'src/index.ts',
output: {
file: 'dist/index.js',
format: 'umd',
name: 'PySlangRunner',
sourcemap: true
},
plugins: [
resolve(),
commonjs(),
typescript({
tsconfig: './tsconfig.json'
})
]
};

export default config;
21 changes: 21 additions & 0 deletions src/ast-types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {Token} from "./tokenizer";
import {Position} from "estree";

/*The offset is calculated as follows:
Current position is one after real position of end of token: 1
/*
The offset is calculated as follows:
Current position is one after real position of end of token: 1
*/
const MAGIC_OFFSET = 1;

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


/* Searches backwards and forwards till it hits a newline */
function getFullLine(source: string, current: number): string {
let back: number = current;
Expand Down
7 changes: 5 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,16 +499,19 @@ export class Parser {
const startToken = this.peek();
if (this.match(TokenType.TRUE)) return new ExprNS.Literal(startToken, this.previous(), true);
if (this.match(TokenType.FALSE)) return new ExprNS.Literal(startToken, this.previous(), false);

if (this.match(TokenType.NONE)) return new ExprNS.None(startToken, this.previous());
if (this.match(TokenType.STRING)) {
return new ExprNS.Literal(startToken, this.previous(), this.previous().lexeme);
}
if (this.match(TokenType.NUMBER)) {
return new ExprNS.Literal(startToken, this.previous(), Number(this.previous().lexeme));
return new ExprNS.Literal(startToken, this.previous(), Number(this.previous().lexeme.replace(/_/g, "")));
}
if (this.match(TokenType.BIGINT)) {
return new ExprNS.BigIntLiteral(startToken, this.previous(), this.previous().lexeme);
}
if (this.match(TokenType.COMPLEX)) {
return new ExprNS.Complex(startToken, this.previous(), this.previous().lexeme);
}

if (this.match(TokenType.NAME, ...PSEUD_NAMES)) {
return new ExprNS.Variable(startToken, this.previous(), this.previous());
Expand Down
70 changes: 43 additions & 27 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Token } from "./tokenizer";
import { TokenType } from "./tokens";
import { ResolverErrors } from "./errors";

const levenshtein = require('fast-levenshtein');
import levenshtein from 'fast-levenshtein';
// const levenshtein = require('fast-levenshtein');

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

Expand Down Expand Up @@ -149,23 +150,20 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
// The global environment
this.environment = new Environment(source, null, new Map([
// misc library
["get_time", new Token(TokenType.NAME, "get_time", 0, 0, 0)],
["_int", new Token(TokenType.NAME, "_int", 0, 0, 0)],
["_int_from_string", new Token(TokenType.NAME, "_int_from_string", 0, 0, 0)],
["abs", new Token(TokenType.NAME, "abs", 0, 0, 0)],
["char_at", new Token(TokenType.NAME, "char_at", 0, 0, 0)],
["error", new Token(TokenType.NAME, "error", 0, 0, 0)],
["input", new Token(TokenType.NAME, "input", 0, 0, 0)],
["isinstance", new Token(TokenType.NAME, "isinstance", 0, 0, 0)],
["max", new Token(TokenType.NAME, "max", 0, 0, 0)],
["min", new Token(TokenType.NAME, "min", 0, 0, 0)],
["print", new Token(TokenType.NAME, "print", 0, 0, 0)],
["raw_print", new Token(TokenType.NAME, "raw_print", 0, 0, 0)],
["random_random", new Token(TokenType.NAME, "random_random", 0, 0, 0)],
["round", new Token(TokenType.NAME, "round", 0, 0, 0)],
["str", new Token(TokenType.NAME, "str", 0, 0, 0)],
["error", new Token(TokenType.NAME, "error", 0, 0, 0)],
["prompt", new Token(TokenType.NAME, "prompt", 0, 0, 0)],
["is_float", new Token(TokenType.NAME, "is_float", 0, 0, 0)],
["is_int", new Token(TokenType.NAME, "is_int", 0, 0, 0)],
["is_string", new Token(TokenType.NAME, "is_string", 0, 0, 0)],
["is_function", new Token(TokenType.NAME, "is_function", 0, 0, 0)],
["is_boolean", new Token(TokenType.NAME, "is_boolean", 0, 0, 0)],
["parse_int", new Token(TokenType.NAME, "parse_int", 0, 0, 0)],
["char_at", new Token(TokenType.NAME, "char_at", 0, 0, 0)],
["arity", new Token(TokenType.NAME, "arity", 0, 0, 0)],
["None", new Token(TokenType.NAME, "None", 0, 0, 0)],
["NaN", new Token(TokenType.NAME, "NaN", 0, 0, 0)],
["Infinity", new Token(TokenType.NAME, "Infinity", 0, 0, 0)],
["time_time", new Token(TokenType.NAME, "time_time", 0, 0, 0)],

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

// math library
["math_abs", new Token(TokenType.NAME, "math_abs", 0, 0, 0)],
["math_acos", new Token(TokenType.NAME, "math_acos", 0, 0, 0)],
["math_acosh", new Token(TokenType.NAME, "math_acosh", 0, 0, 0)],
["math_asin", new Token(TokenType.NAME, "math_asin", 0, 0, 0)],
Expand All @@ -185,31 +182,46 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
["math_atanh", new Token(TokenType.NAME, "math_atanh", 0, 0, 0)],
["math_cbrt", new Token(TokenType.NAME, "math_cbrt", 0, 0, 0)],
["math_ceil", new Token(TokenType.NAME, "math_ceil", 0, 0, 0)],
["math_clz32", new Token(TokenType.NAME, "math_clz32", 0, 0, 0)],
["math_comb", new Token(TokenType.NAME, "math_comb", 0, 0, 0)],
["math_copysign", new Token(TokenType.NAME, "math_copysign", 0, 0, 0)],
["math_cos", new Token(TokenType.NAME, "math_cos", 0, 0, 0)],
["math_cosh", new Token(TokenType.NAME, "math_cosh", 0, 0, 0)],
["math_degrees", new Token(TokenType.NAME, "math_degrees", 0, 0, 0)],
["math_erf", new Token(TokenType.NAME, "math_erf", 0, 0, 0)],
["math_erfc", new Token(TokenType.NAME, "math_erfc", 0, 0, 0)],
["math_exp", new Token(TokenType.NAME, "math_exp", 0, 0, 0)],
["math_exp2", new Token(TokenType.NAME, "math_exp2", 0, 0, 0)],
["math_expm1", new Token(TokenType.NAME, "math_expm1", 0, 0, 0)],
["math_fabs", new Token(TokenType.NAME, "math_fabs", 0, 0, 0)],
["math_factorial", new Token(TokenType.NAME, "math_factorial", 0, 0, 0)],
["math_floor", new Token(TokenType.NAME, "math_floor", 0, 0, 0)],
["math_fround", new Token(TokenType.NAME, "math_fround", 0, 0, 0)],
["math_hypot", new Token(TokenType.NAME, "math_hypot", 0, 0, 0)],
["math_imul", new Token(TokenType.NAME, "math_imul", 0, 0, 0)],
["math_fma", new Token(TokenType.NAME, "math_fma", 0, 0, 0)],
["math_fmod", new Token(TokenType.NAME, "math_fmod", 0, 0, 0)],
["math_gamma", new Token(TokenType.NAME, "math_gamma", 0, 0, 0)],
["math_gcd", new Token(TokenType.NAME, "math_gcd", 0, 0, 0)],
["math_isfinite", new Token(TokenType.NAME, "math_isfinite", 0, 0, 0)],
["math_isinf", new Token(TokenType.NAME, "math_isinf", 0, 0, 0)],
["math_isnan", new Token(TokenType.NAME, "math_isnan", 0, 0, 0)],
["math_isqrt", new Token(TokenType.NAME, "math_isqrt", 0, 0, 0)],
["math_lcm", new Token(TokenType.NAME, "math_lcm", 0, 0, 0)],
["math_ldexp", new Token(TokenType.NAME, "math_ldexp", 0, 0, 0)],
["math_lgamma", new Token(TokenType.NAME, "math_lgamma", 0, 0, 0)],
["math_log", new Token(TokenType.NAME, "math_log", 0, 0, 0)],
["math_log10", new Token(TokenType.NAME, "math_log10", 0, 0, 0)],
["math_log1p", new Token(TokenType.NAME, "math_log1p", 0, 0, 0)],
["math_log2", new Token(TokenType.NAME, "math_log2", 0, 0, 0)],
["math_log10", new Token(TokenType.NAME, "math_log10", 0, 0, 0)],
["math_max", new Token(TokenType.NAME, "math_max", 0, 0, 0)],
["math_min", new Token(TokenType.NAME, "math_min", 0, 0, 0)],
["math_nextafter", new Token(TokenType.NAME, "math_nextafter", 0, 0, 0)],
["math_perm", new Token(TokenType.NAME, "math_perm", 0, 0, 0)],
["math_pow", new Token(TokenType.NAME, "math_pow", 0, 0, 0)],
["math_random", new Token(TokenType.NAME, "math_random", 0, 0, 0)],
["math_round", new Token(TokenType.NAME, "math_round", 0, 0, 0)],
["math_sign", new Token(TokenType.NAME, "math_sign", 0, 0, 0)],
["math_radians", new Token(TokenType.NAME, "math_radians", 0, 0, 0)],
["math_remainder", new Token(TokenType.NAME, "math_remainder", 0, 0, 0)],
["math_sin", new Token(TokenType.NAME, "math_sin", 0, 0, 0)],
["math_sinh", new Token(TokenType.NAME, "math_sinh", 0, 0, 0)],
["math_sqrt", new Token(TokenType.NAME, "math_sqrt", 0, 0, 0)],
["math_tan", new Token(TokenType.NAME, "math_tan", 0, 0, 0)],
["math_tanh", new Token(TokenType.NAME, "math_tanh", 0, 0, 0)],
["math_trunc", new Token(TokenType.NAME, "math_trunc", 0, 0, 0)],
["math_ulp", new Token(TokenType.NAME, "math_ulp", 0, 0, 0)]
]));
this.functionScope = null;
}
Expand Down Expand Up @@ -442,9 +454,13 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
this.resolve(expr.consequent);
this.resolve(expr.alternative);
}
visitNoneExpr(expr: ExprNS.None): void {
}
visitLiteralExpr(expr: ExprNS.Literal): void {
}
visitBigIntLiteralExpr(expr: ExprNS.BigIntLiteral): void {
}
visitComplexExpr(expr: ExprNS.Complex): void {
}

}
Loading