it's alive
This commit is contained in:
commit
e6583a3b75
1
.cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc
Symbolic link
1
.cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
../../CLAUDE.md
|
||||||
34
.gitignore
vendored
Normal file
34
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
# dependencies (bun install)
|
||||||
|
node_modules
|
||||||
|
|
||||||
|
# output
|
||||||
|
out
|
||||||
|
dist
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# code coverage
|
||||||
|
coverage
|
||||||
|
*.lcov
|
||||||
|
|
||||||
|
# logs
|
||||||
|
logs
|
||||||
|
_.log
|
||||||
|
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||||
|
|
||||||
|
# dotenv environment variable files
|
||||||
|
.env
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
.env.local
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.eslintcache
|
||||||
|
.cache
|
||||||
|
*.tsbuildinfo
|
||||||
|
|
||||||
|
# IntelliJ based IDEs
|
||||||
|
.idea
|
||||||
|
|
||||||
|
# Finder (MacOS) folder config
|
||||||
|
.DS_Store
|
||||||
111
CLAUDE.md
Normal file
111
CLAUDE.md
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
---
|
||||||
|
description: Use Bun instead of Node.js, npm, pnpm, or vite.
|
||||||
|
globs: "*.ts, *.tsx, *.html, *.css, *.js, *.jsx, package.json"
|
||||||
|
alwaysApply: false
|
||||||
|
---
|
||||||
|
|
||||||
|
Default to using Bun instead of Node.js.
|
||||||
|
|
||||||
|
- Use `bun <file>` instead of `node <file>` or `ts-node <file>`
|
||||||
|
- Use `bun test` instead of `jest` or `vitest`
|
||||||
|
- Use `bun build <file.html|file.ts|file.css>` instead of `webpack` or `esbuild`
|
||||||
|
- Use `bun install` instead of `npm install` or `yarn install` or `pnpm install`
|
||||||
|
- Use `bun run <script>` instead of `npm run <script>` or `yarn run <script>` or `pnpm run <script>`
|
||||||
|
- Bun automatically loads .env, so don't use dotenv.
|
||||||
|
|
||||||
|
## APIs
|
||||||
|
|
||||||
|
- `Bun.serve()` supports WebSockets, HTTPS, and routes. Don't use `express`.
|
||||||
|
- `bun:sqlite` for SQLite. Don't use `better-sqlite3`.
|
||||||
|
- `Bun.redis` for Redis. Don't use `ioredis`.
|
||||||
|
- `Bun.sql` for Postgres. Don't use `pg` or `postgres.js`.
|
||||||
|
- `WebSocket` is built-in. Don't use `ws`.
|
||||||
|
- Prefer `Bun.file` over `node:fs`'s readFile/writeFile
|
||||||
|
- Bun.$`ls` instead of execa.
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
Use `bun test` to run tests.
|
||||||
|
|
||||||
|
```ts#index.test.ts
|
||||||
|
import { test, expect } from "bun:test";
|
||||||
|
|
||||||
|
test("hello world", () => {
|
||||||
|
expect(1).toBe(1);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Frontend
|
||||||
|
|
||||||
|
Use HTML imports with `Bun.serve()`. Don't use `vite`. HTML imports fully support React, CSS, Tailwind.
|
||||||
|
|
||||||
|
Server:
|
||||||
|
|
||||||
|
```ts#index.ts
|
||||||
|
import index from "./index.html"
|
||||||
|
|
||||||
|
Bun.serve({
|
||||||
|
routes: {
|
||||||
|
"/": index,
|
||||||
|
"/api/users/:id": {
|
||||||
|
GET: (req) => {
|
||||||
|
return new Response(JSON.stringify({ id: req.params.id }));
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// optional websocket support
|
||||||
|
websocket: {
|
||||||
|
open: (ws) => {
|
||||||
|
ws.send("Hello, world!");
|
||||||
|
},
|
||||||
|
message: (ws, message) => {
|
||||||
|
ws.send(message);
|
||||||
|
},
|
||||||
|
close: (ws) => {
|
||||||
|
// handle close
|
||||||
|
}
|
||||||
|
},
|
||||||
|
development: {
|
||||||
|
hmr: true,
|
||||||
|
console: true,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
HTML files can import .tsx, .jsx or .js files directly and Bun's bundler will transpile & bundle automatically. `<link>` tags can point to stylesheets and Bun's CSS bundler will bundle.
|
||||||
|
|
||||||
|
```html#index.html
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>Hello, world!</h1>
|
||||||
|
<script type="module" src="./frontend.tsx"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
With the following `frontend.tsx`:
|
||||||
|
|
||||||
|
```tsx#frontend.tsx
|
||||||
|
import React from "react";
|
||||||
|
|
||||||
|
// import .css files directly and it works
|
||||||
|
import './index.css';
|
||||||
|
|
||||||
|
import { createRoot } from "react-dom/client";
|
||||||
|
|
||||||
|
const root = createRoot(document.body);
|
||||||
|
|
||||||
|
export default function Frontend() {
|
||||||
|
return <h1>Hello, world!</h1>;
|
||||||
|
}
|
||||||
|
|
||||||
|
root.render(<Frontend />);
|
||||||
|
```
|
||||||
|
|
||||||
|
Then, run index.ts
|
||||||
|
|
||||||
|
```sh
|
||||||
|
bun --hot ./index.ts
|
||||||
|
```
|
||||||
|
|
||||||
|
For more information, read the Bun API docs in `node_modules/bun-types/docs/**.md`.
|
||||||
15
README.md
Normal file
15
README.md
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
# reefvm
|
||||||
|
|
||||||
|
To install dependencies:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bun install
|
||||||
|
```
|
||||||
|
|
||||||
|
To run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bun run index.ts
|
||||||
|
```
|
||||||
|
|
||||||
|
This project was created using `bun init` in bun v1.2.20. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
|
||||||
29
bun.lock
Normal file
29
bun.lock
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
{
|
||||||
|
"lockfileVersion": 1,
|
||||||
|
"workspaces": {
|
||||||
|
"": {
|
||||||
|
"name": "reefvm",
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/bun": "latest",
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"typescript": "^5",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"packages": {
|
||||||
|
"@types/bun": ["@types/bun@1.2.23", "", { "dependencies": { "bun-types": "1.2.23" } }, "sha512-le8ueOY5b6VKYf19xT3McVbXqLqmxzPXHsQT/q9JHgikJ2X22wyTW3g3ohz2ZMnp7dod6aduIiq8A14Xyimm0A=="],
|
||||||
|
|
||||||
|
"@types/node": ["@types/node@24.6.2", "", { "dependencies": { "undici-types": "~7.13.0" } }, "sha512-d2L25Y4j+W3ZlNAeMKcy7yDsK425ibcAOO2t7aPTz6gNMH0z2GThtwENCDc0d/Pw9wgyRqE5Px1wkV7naz8ang=="],
|
||||||
|
|
||||||
|
"@types/react": ["@types/react@19.2.0", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-1LOH8xovvsKsCBq1wnT4ntDUdCJKmnEakhsuoUSy6ExlHCkGP2hqnatagYTgFk6oeL0VU31u7SNjunPN+GchtA=="],
|
||||||
|
|
||||||
|
"bun-types": ["bun-types@1.2.23", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-R9f0hKAZXgFU3mlrA0YpE/fiDvwV0FT9rORApt2aQVWSuJDzZOyB5QLc0N/4HF57CS8IXJ6+L5E4W1bW6NS2Aw=="],
|
||||||
|
|
||||||
|
"csstype": ["csstype@3.1.3", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="],
|
||||||
|
|
||||||
|
"typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
|
||||||
|
|
||||||
|
"undici-types": ["undici-types@7.13.0", "", {}, "sha512-Ov2Rr9Sx+fRgagJ5AX0qvItZG/JKKoBRAVITs1zk7IqZGTJUwgUr7qoYBpWwakpWilTZFM98rG/AFRocu10iIQ=="],
|
||||||
|
}
|
||||||
|
}
|
||||||
11
package.json
Normal file
11
package.json
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"name": "reefvm",
|
||||||
|
"module": "src/index.ts",
|
||||||
|
"type": "module",
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/bun": "latest"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"typescript": "^5"
|
||||||
|
}
|
||||||
|
}
|
||||||
46
src/bytecode.ts
Normal file
46
src/bytecode.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
import { type Value, type FunctionDef, toValue } from "./value"
|
||||||
|
import { OpCode } from "./opcode"
|
||||||
|
|
||||||
|
export type Bytecode = {
|
||||||
|
instructions: Instruction[]
|
||||||
|
constants: Constant[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Instruction = {
|
||||||
|
op: OpCode
|
||||||
|
operand?: number | string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Constant =
|
||||||
|
| Value
|
||||||
|
| FunctionDef
|
||||||
|
|
||||||
|
export function toBytecode(str: string): Bytecode /* throws */ {
|
||||||
|
const lines = str.trim().split("\n")
|
||||||
|
|
||||||
|
const bytecode: Bytecode = {
|
||||||
|
instructions: [],
|
||||||
|
constants: []
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let line of lines) {
|
||||||
|
let [op, operand] = line.trim().split(" ")
|
||||||
|
|
||||||
|
if (operand) {
|
||||||
|
if (/^\d+/.test(operand)) {
|
||||||
|
bytecode.constants.push(toValue(parseFloat(operand)))
|
||||||
|
} else if (/^['"]\w+/.test(operand)) {
|
||||||
|
bytecode.constants.push(toValue(operand.slice(1, operand.length - 1)))
|
||||||
|
} else {
|
||||||
|
throw `Unknown operand: ${operand}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bytecode.instructions.push({
|
||||||
|
op: OpCode[op as keyof typeof OpCode],
|
||||||
|
operand: operand ? bytecode.constants.length - 1 : undefined
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytecode
|
||||||
|
}
|
||||||
7
src/exception.ts
Normal file
7
src/exception.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { Scope } from "./scope"
|
||||||
|
|
||||||
|
export type ExceptionHandler = {
|
||||||
|
catchAddress: number // Where to jump when exception is caught
|
||||||
|
callStackDepth: number // Call stack depth when handler was pushed
|
||||||
|
scope: Scope // Scope to restore when catching
|
||||||
|
}
|
||||||
8
src/frame.ts
Normal file
8
src/frame.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { Scope } from "./scope"
|
||||||
|
|
||||||
|
export type Frame = {
|
||||||
|
returnAddress: number
|
||||||
|
returnScope: Scope
|
||||||
|
isBreakTarget: boolean
|
||||||
|
continueAddress?: number
|
||||||
|
}
|
||||||
8
src/index.ts
Normal file
8
src/index.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
import type { Bytecode } from "./bytecode"
|
||||||
|
import type { Value } from "./value"
|
||||||
|
import { VM } from "./vm"
|
||||||
|
|
||||||
|
export async function run(bytecode: Bytecode): Promise<Value> {
|
||||||
|
const vm = new VM(bytecode)
|
||||||
|
return await vm.run()
|
||||||
|
}
|
||||||
12
src/opcode.ts
Normal file
12
src/opcode.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
export enum OpCode {
|
||||||
|
// stack
|
||||||
|
PUSH, // operand: constant index (number)
|
||||||
|
POP, // operand: none
|
||||||
|
DUP, // operand: none
|
||||||
|
|
||||||
|
// math
|
||||||
|
ADD,
|
||||||
|
SUB,
|
||||||
|
MUL,
|
||||||
|
DIV
|
||||||
|
}
|
||||||
35
src/scope.ts
Normal file
35
src/scope.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
import type { Value } from "./value"
|
||||||
|
|
||||||
|
export class Scope {
|
||||||
|
locals = new Map<string, Value>()
|
||||||
|
parent?: Scope
|
||||||
|
|
||||||
|
constructor(parent?: Scope) {
|
||||||
|
this.parent = parent
|
||||||
|
}
|
||||||
|
|
||||||
|
get(name: string): Value /* throws */ {
|
||||||
|
if (this.locals.has(name))
|
||||||
|
return this.locals.get(name)!
|
||||||
|
|
||||||
|
if (this.parent)
|
||||||
|
return this.parent.get(name)
|
||||||
|
|
||||||
|
throw new Error(`Undefined variable: ${name}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
set(name: string, value: Value) {
|
||||||
|
if (this.locals.has(name))
|
||||||
|
this.locals.set(name, value)
|
||||||
|
else if (this.parent?.has(name))
|
||||||
|
this.parent.set(name, value)
|
||||||
|
else
|
||||||
|
this.locals.set(name, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
has(name: string): boolean {
|
||||||
|
return this.locals.has(name) || this.parent?.has(name) || false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
61
src/value.ts
Normal file
61
src/value.ts
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
import { Scope } from "./scope"
|
||||||
|
|
||||||
|
export type Value =
|
||||||
|
| { type: 'null', value: null }
|
||||||
|
| { type: 'boolean', value: boolean }
|
||||||
|
| { type: 'number', value: number }
|
||||||
|
| { type: 'string', value: string }
|
||||||
|
| { type: 'array', value: Value[] }
|
||||||
|
| { type: 'dict', value: Dict }
|
||||||
|
| {
|
||||||
|
type: 'function',
|
||||||
|
params: string[],
|
||||||
|
defaults: Record<string, Value>,
|
||||||
|
body: number,
|
||||||
|
parentScope: Scope,
|
||||||
|
variadic: boolean,
|
||||||
|
kwargs: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Dict = Map<string, Value>
|
||||||
|
|
||||||
|
export type FunctionDef = {
|
||||||
|
type: 'function_def'
|
||||||
|
params: string[]
|
||||||
|
defaults: Record<string, number>
|
||||||
|
body: number
|
||||||
|
variadic: boolean
|
||||||
|
kwargs: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export function toValue(v: any): Value /* throws */ {
|
||||||
|
if (v === null || v === undefined)
|
||||||
|
return { type: 'null', value: null }
|
||||||
|
|
||||||
|
if (Array.isArray(v))
|
||||||
|
return { type: 'array', value: v.map(toValue) }
|
||||||
|
|
||||||
|
switch (typeof v) {
|
||||||
|
case 'boolean':
|
||||||
|
return { type: 'boolean', value: v }
|
||||||
|
case 'number':
|
||||||
|
return { type: 'number', value: v }
|
||||||
|
case 'string':
|
||||||
|
return { type: 'string', value: v }
|
||||||
|
case 'function':
|
||||||
|
throw "can't toValue() a js function yet"
|
||||||
|
case 'object':
|
||||||
|
const dict: Dict = new Map()
|
||||||
|
|
||||||
|
for (const key in Object.keys(v))
|
||||||
|
dict.set(key, toValue(v[key]))
|
||||||
|
|
||||||
|
return { type: 'dict', value: dict }
|
||||||
|
default:
|
||||||
|
throw `can't toValue this: ${v}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function toNumber(v: Value): number {
|
||||||
|
return v.type === 'number' ? v.value : 0
|
||||||
|
}
|
||||||
84
src/vm.ts
Normal file
84
src/vm.ts
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
import type { Bytecode, Constant, Instruction } from "./bytecode"
|
||||||
|
import type { ExceptionHandler } from "./exception"
|
||||||
|
import type { Frame } from "./frame"
|
||||||
|
import { OpCode } from "./opcode"
|
||||||
|
import { Scope } from "./scope"
|
||||||
|
import { type Value, toValue, toNumber } from "./value"
|
||||||
|
|
||||||
|
export class VM {
|
||||||
|
pc = 0
|
||||||
|
stopped = false
|
||||||
|
stack: Value[] = []
|
||||||
|
callStack: Frame[] = []
|
||||||
|
exceptionHandlers: ExceptionHandler[] = []
|
||||||
|
scope: Scope
|
||||||
|
constants: Constant[] = []
|
||||||
|
instructions: Instruction[] = []
|
||||||
|
|
||||||
|
constructor(bytecode: Bytecode) {
|
||||||
|
this.instructions = bytecode.instructions
|
||||||
|
this.constants = bytecode.constants
|
||||||
|
this.scope = new Scope()
|
||||||
|
}
|
||||||
|
|
||||||
|
async run(): Promise<Value> {
|
||||||
|
this.pc = 0
|
||||||
|
this.stopped = false
|
||||||
|
|
||||||
|
while (!this.stopped && this.pc < this.instructions.length) {
|
||||||
|
const instruction = this.instructions[this.pc]!
|
||||||
|
await this.execute(instruction)
|
||||||
|
this.pc++
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.stack[this.stack.length - 1] || toValue(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
async execute(instruction: Instruction) /* throws */ {
|
||||||
|
switch (instruction.op) {
|
||||||
|
case OpCode.PUSH:
|
||||||
|
const idx = instruction.operand as number
|
||||||
|
const constant = this.constants[idx]
|
||||||
|
|
||||||
|
if (!constant || constant.type === 'function_def')
|
||||||
|
throw new Error(`Invalid constant index: ${idx}`)
|
||||||
|
|
||||||
|
this.stack.push(constant)
|
||||||
|
break
|
||||||
|
|
||||||
|
case OpCode.POP:
|
||||||
|
this.stack.pop()
|
||||||
|
break
|
||||||
|
|
||||||
|
case OpCode.DUP:
|
||||||
|
this.stack.push(this.stack[this.stack.length - 1]!)
|
||||||
|
break
|
||||||
|
|
||||||
|
case OpCode.ADD:
|
||||||
|
this.binaryOp((a, b) => toNumber(a) + toNumber(b))
|
||||||
|
break
|
||||||
|
|
||||||
|
case OpCode.SUB:
|
||||||
|
this.binaryOp((a, b) => toNumber(a) - toNumber(b))
|
||||||
|
break
|
||||||
|
|
||||||
|
case OpCode.MUL:
|
||||||
|
this.binaryOp((a, b) => toNumber(a) * toNumber(b))
|
||||||
|
break
|
||||||
|
|
||||||
|
case OpCode.DIV:
|
||||||
|
this.binaryOp((a, b) => toNumber(a) / toNumber(b))
|
||||||
|
break
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw `Unknown op: ${instruction.op}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
binaryOp(fn: (a: Value, b: Value) => number) {
|
||||||
|
const b = this.stack.pop()!
|
||||||
|
const a = this.stack.pop()!
|
||||||
|
const result = fn(a, b)
|
||||||
|
this.stack.push({ type: 'number', value: result })
|
||||||
|
}
|
||||||
|
}
|
||||||
47
tests/basic.test.ts
Normal file
47
tests/basic.test.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
import { test, expect } from "bun:test"
|
||||||
|
import { run } from "#index"
|
||||||
|
import { toBytecode } from "#bytecode"
|
||||||
|
|
||||||
|
test("adding numbers", async () => {
|
||||||
|
const str = `
|
||||||
|
PUSH 1
|
||||||
|
PUSH 5
|
||||||
|
ADD
|
||||||
|
`
|
||||||
|
expect(await run(toBytecode(str))).toEqual({ type: 'number', value: 6 })
|
||||||
|
|
||||||
|
const str2 = `
|
||||||
|
PUSH 100
|
||||||
|
PUSH 500
|
||||||
|
ADD
|
||||||
|
`
|
||||||
|
expect(await run(toBytecode(str2))).toEqual({ type: 'number', value: 600 })
|
||||||
|
})
|
||||||
|
|
||||||
|
test("subtracting numbers", async () => {
|
||||||
|
const str = `
|
||||||
|
PUSH 5
|
||||||
|
PUSH 2
|
||||||
|
SUB
|
||||||
|
`
|
||||||
|
expect(await run(toBytecode(str))).toEqual({ type: 'number', value: 3 })
|
||||||
|
})
|
||||||
|
|
||||||
|
test("multiplying numbers", async () => {
|
||||||
|
const str = `
|
||||||
|
PUSH 5
|
||||||
|
PUSH 2
|
||||||
|
MUL
|
||||||
|
`
|
||||||
|
expect(await run(toBytecode(str))).toEqual({ type: 'number', value: 10 })
|
||||||
|
})
|
||||||
|
|
||||||
|
test("dividing numbers", async () => {
|
||||||
|
const str = `
|
||||||
|
PUSH 10
|
||||||
|
PUSH 2
|
||||||
|
DIV
|
||||||
|
`
|
||||||
|
expect(await run(toBytecode(str))).toEqual({ type: 'number', value: 5 })
|
||||||
|
})
|
||||||
|
|
||||||
23
tests/bytecode.test.ts
Normal file
23
tests/bytecode.test.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { test, expect } from "bun:test"
|
||||||
|
import { toBytecode } from "#bytecode"
|
||||||
|
import { OpCode } from "#opcode"
|
||||||
|
|
||||||
|
test("string compilation", () => {
|
||||||
|
const str = `
|
||||||
|
PUSH 1
|
||||||
|
PUSH 5
|
||||||
|
ADD
|
||||||
|
`
|
||||||
|
expect(toBytecode(str)).toEqual({
|
||||||
|
instructions: [
|
||||||
|
{ op: OpCode.PUSH, operand: 0 },
|
||||||
|
{ op: OpCode.PUSH, operand: 1 },
|
||||||
|
{ op: OpCode.ADD },
|
||||||
|
],
|
||||||
|
constants: [
|
||||||
|
{ type: 'number', value: 1 },
|
||||||
|
{ type: 'number', value: 5 }
|
||||||
|
]
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
34
tsconfig.json
Normal file
34
tsconfig.json
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
// Environment setup & latest features
|
||||||
|
"lib": [
|
||||||
|
"ESNext"
|
||||||
|
],
|
||||||
|
"target": "ESNext",
|
||||||
|
"module": "Preserve",
|
||||||
|
"moduleDetection": "force",
|
||||||
|
"jsx": "react-jsx",
|
||||||
|
"allowJs": true,
|
||||||
|
// Bundler mode
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"allowImportingTsExtensions": true,
|
||||||
|
"verbatimModuleSyntax": true,
|
||||||
|
"noEmit": true,
|
||||||
|
// Best practices
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
"noUncheckedIndexedAccess": true,
|
||||||
|
"noImplicitOverride": true,
|
||||||
|
// Some stricter flags (disabled by default)
|
||||||
|
"noUnusedLocals": false,
|
||||||
|
"noUnusedParameters": false,
|
||||||
|
"noPropertyAccessFromIndexSignature": false,
|
||||||
|
"baseUrl": ".",
|
||||||
|
"paths": {
|
||||||
|
"#*": [
|
||||||
|
"./src/*"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user