Skip to content

Commit

Permalink
fix: negative number check
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderSerio committed Jun 24, 2024
1 parent 7dbf8f4 commit 05c30de
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 118 deletions.
5 changes: 5 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set -e

cd doc

npm run build
2 changes: 1 addition & 1 deletion doc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"dev": "vite",
"build": "vue-tsc && vite build",
"build": "vue-tsc --skipLibCheck && vite build",
"preview": "vite preview"
},
"dependencies": {
Expand Down
6 changes: 3 additions & 3 deletions doc/src/components/navbar/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
</template>

<script setup lang="ts">
import { useRoute, useRouter } from "vue-router";
const router = useRouter();
const route = useRoute();
// import { useRoute, useRouter } from "vue-router";
// const router = useRouter();
// const route = useRoute();
</script>

<style scoped>
Expand Down
2 changes: 0 additions & 2 deletions doc/src/documents/sectionList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { jsx } from "vue/jsx-runtime";

const sectionList = [
{
title: `快速开始`,
Expand Down
3 changes: 0 additions & 3 deletions doc/src/pages/home/Home.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<script setup lang="ts">
import { ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
const linkTo = (path: string) => {
router.push(path);
};
const count = ref(0);
</script>

<template>
Expand Down
8 changes: 4 additions & 4 deletions doc/src/pages/playground/Playground.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import * as monaco from "monaco-editor";
// import * as monaco from "monaco-editor";
import MonacoEditor from "../../components/monacoEditor/MonacoEditor.vue";
import nighta from "../../util/interpreter.js";
import { onMounted, ref } from "vue";
Expand All @@ -9,7 +9,7 @@ const value = ref<string | Array<any>>('(say "Hello World")');
const result = ref<string | Array<any>>("还没有执行代码");
const isValid = ref(true);
const editorMounted = (editor: monaco.editor.IStandaloneCodeEditor) => {
const editorMounted = (/*editor: monaco.editor.IStandaloneCodeEditor*/) => {
// console.log("editor 实例加载完成", editor);
// console.log("nighta 解释器加载完成", nighta.parse);
};
Expand All @@ -19,10 +19,10 @@ const runCode = () => {
//TODO: 防抖节流
console.error();
const exp = nighta.parse(value.value);
const res = nighta.eval(exp);
nighta.eval(exp);
isValid.value = true;
result.value = window.codeOutputList.map(safeJsonStringify);
localStorage.setItem("nighta-playground-code-cache", value.value);
localStorage.setItem("nighta-playground-code-cache", value.value as string);
} catch (err) {
console.error();
isValid.value = false;
Expand Down
2 changes: 1 addition & 1 deletion doc/src/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Section from "../components/section/Section.vue"
import Playground from "../pages/playground/Playground.vue"
import sectionList from "../documents/sectionList";

const routes = [
const routes: any = [
{ path: "/", meta: { KeepAlive: true }, component: Home },
{ path: "/main", meta: { KeepAlive: true }, component: Main, redirect: `/main/${sectionList[0].title}`, children: [{ path: ':section', component: Section }] },
{ path: "/playground", meta: { KeepAlive: true }, component: Playground }
Expand Down
2 changes: 1 addition & 1 deletion doc/src/util/interpreter.js

Large diffs are not rendered by default.

49 changes: 38 additions & 11 deletions doc/src/util/safeJsonStringify.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
const safeJsonStringify = (value: any) => {
let cache: Array<any> | null = [];
let isFirstRecord = true;

const str = JSON.stringify(value, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (cache?.indexOf(value) !== -1) {
return;
const str = JSON.stringify(
value,
(key, value) => {
if (typeof value === "object" && value !== null) {
if (cache?.indexOf(value) !== -1) {
return;
}
cache.push(value);
if (!Object.keys(value).length) {
return () => { };
}
}
cache.push(value);
}
return value;
});

if (key === "record") {
if (isFirstRecord) {
isFirstRecord = false;
} else {
return () => { };
}
}

if (key === "parent") {
return () => { };
} else if (key === "env") {
return () => { };
}

if (value instanceof Array) {
return () => { };
}

return value;
},
4
);

cache = null;

return str;
}
return (str as any)?.replaceAll('{}', '[Object]') ?? str;
};

export default safeJsonStringify;
export default safeJsonStringify;
2 changes: 1 addition & 1 deletion doc/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"jsx": "preserve",

/* Linting */
"strict": true,
"strict": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
Expand Down
2 changes: 1 addition & 1 deletion doc/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"strict": true
"strict": false
},
"include": ["vite.config.ts"]
}
1 change: 1 addition & 0 deletions doc/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
base: '/Nighta/',
plugins: [vue()],
optimizeDeps: {
include: [
Expand Down
2 changes: 1 addition & 1 deletion lib/esm/index.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/umd/index.umd.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"doc": "cd doc && npm run dev",
"gen": "cd src/parser && powershell -ExecutionPolicy Bypass -File gen.ps1",
"build": "rollup -c && copy lib\\esm\\index.esm.js doc\\src\\util\\interpreter.js",
"build:all": "npm run gen && npm run test && npm run build"
"build:all": "npm run gen && npm run test && npm run build",
"preview": "cd doc && npm run build && npm run preview",
"publish:doc": "git subtree push --prefix dist origin gh-pages"
},
"keywords": [],
"author": "",
Expand Down
28 changes: 18 additions & 10 deletions src/Nighta.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class Nighta {
// console.log(env);
// console.log('======================================');
if (this._isNumber(exp)) {
if (+exp < 0) {
console.log('???', +exp === -1);
return +exp;
}
return exp;
}

if (this._isString(exp)) {
} else if (this._isString(exp)) {
return exp.slice(1, -1);
}

if (this._isUndefined(exp)) {
} else if (this._isUndefined(exp)) {
return undefined;
}

Expand Down Expand Up @@ -220,7 +220,7 @@ class Nighta {
}
}

throw `Invalid Syntax: ${JSON.stringify(exp)}`;
throw `Invalid Syntax: ${JSON.stringify(exp)}\n\n${JSON.stringify(env)}`;
}

_callUserDefinedFunction(fn, args, env) {
Expand Down Expand Up @@ -257,6 +257,9 @@ class Nighta {
}

_isNumber(exp) {
if (typeof exp === 'string') {
return /^-\d+$/.test(exp);
}
return typeof exp === 'number';
}

Expand Down Expand Up @@ -292,21 +295,25 @@ const nighta = new Nighta(new Environment({
'||': (v1, v2) => v1 || v2,
'&': (v1, v2) => v1 & v2,
'|': (v1, v2) => v1 | v2,
integer: (num) => Math.floor(num),
say: (...args) => {
let res;
if (args.length > 1) {
res = args.reduce((prev, cur) => prev + cur, '');
console.log(res);
} else {
res = args[0];
// if (args[0].record) {
// res = args[0].record;
// } else {
res = args?.[0];
// }
console.dir(res, { depth: 3 });
}
return res;
},
}));

const List = nighta.eval(nighta.parse(`
(class List null {
const List = nighta.eval(nighta.parse(`(class List null {
(fun constructor (len) {
(self["len"] = len)
(var i 0)
Expand Down Expand Up @@ -355,6 +362,7 @@ const List = nighta.eval(nighta.parse(`
})
})
`), nighta.global);

nighta.global.define("List", List);


Expand Down
2 changes: 1 addition & 1 deletion src/environment/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Environment {
return this;
}
if (this.parent === null) {
throw new ReferenceError(`Variable ${name} is not defined.`);
throw new ReferenceError(`Variable or property ${name} is not defined.`);
}

return this.parent.resolve(name);
Expand Down
7 changes: 2 additions & 5 deletions src/test/class-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ module.exports = (nighta) => {
)
(fun sum () {
(var res
(self["x"] + self["y"])
)
(var res (x + y) )
(var msg "The res is ")
(say msg res)
})
Expand Down Expand Up @@ -152,8 +150,7 @@ module.exports = (nighta) => {

parser.parseTest(`
{
(var list (new List 1000))
(say list)
(var list (new List 100))
}
`);
};
17 changes: 13 additions & 4 deletions src/test/math-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ module.exports = (nighta) => {
// assert.strictEqual(nighta.eval(['-', ['-', 10, -1], 10]), 1);
// assert.strictEqual(nighta.eval(['/', 4, 10]), 0.4);
// assert.strictEqual(nighta.eval(['/', ['/', 10, 2], 10]), 0.5);
parser.parseTest(
`
{
(var i -1)
(say i)
}
`,
);
parser.parseTest(
`
(block
Expand All @@ -31,11 +39,12 @@ module.exports = (nighta) => {
`
{
(var + (
fun (v1 v2) {
fun (v1 v2) {
(say v1 " say: [Hello] to " v2)
})
)
("You" + "World")}
})
)
("You" + "World")
}
`
);
};
Loading

0 comments on commit 05c30de

Please sign in to comment.