@junoscript/juno 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Modules/async.ts +81 -0
- package/Modules/juno-node-fs.ts +105 -0
- package/Modules/juno-test.jn +156 -0
- package/dist/CLI/build-juno.d.ts +5 -0
- package/dist/CLI/build-juno.js +58 -0
- package/dist/CLI/generate-juno-types.d.ts +1 -0
- package/dist/CLI/generate-juno-types.js +51 -0
- package/dist/CLI/junode.d.ts +2 -0
- package/dist/CLI/junode.js +32 -0
- package/dist/Lexer/lexer.d.ts +42 -0
- package/dist/Lexer/lexer.js +300 -0
- package/dist/Lexer/parser.d.ts +395 -0
- package/dist/Lexer/parser.js +1881 -0
- package/dist/Modules/async.d.ts +33 -0
- package/dist/Modules/async.js +76 -0
- package/dist/Modules/index.d.ts +195 -0
- package/dist/Modules/index.js +31 -0
- package/dist/Modules/juno-node-fs.d.ts +52 -0
- package/dist/Modules/juno-node-fs.js +129 -0
- package/dist/Modules/juno-node.d.ts +74 -0
- package/dist/Modules/juno-node.js +56 -0
- package/dist/Modules/juno-test.d.ts +141 -0
- package/dist/Modules/juno-test.js +134 -0
- package/dist/Modules/juno-types.d.ts +35 -0
- package/dist/Modules/juno-types.js +2 -0
- package/dist/Modules/juno.d.ts +21 -0
- package/dist/Modules/juno.js +76 -0
- package/dist/Modules/types.d.ts +56 -0
- package/dist/Modules/types.js +87 -0
- package/dist/Runtime/interpreter.d.ts +64 -0
- package/dist/Runtime/interpreter.js +2533 -0
- package/dist/Runtime/resolve-import.d.ts +2 -0
- package/dist/Runtime/resolve-import.js +30 -0
- package/dist/vscode-junoscript/src/language.d.ts +86 -0
- package/dist/vscode-junoscript/src/language.js +4206 -0
- package/package.json +19 -4
- package/types/array.jn +158 -0
- package/types/boolean.jn +18 -0
- package/types/error.jn +28 -0
- package/types/error.ts +9 -0
- package/types/index.jn +11 -0
- package/types/iterable.jn +211 -0
- package/types/map.jn +44 -0
- package/types/map.ts +132 -0
- package/types/number.ts +52 -0
- package/types/object.jn +94 -0
- package/types/object.ts +49 -0
- package/types/queue.jn +46 -0
- package/types/random.jn +25 -0
- package/types/random.ts +1 -0
- package/types/range.jn +27 -0
- package/types/set.jn +139 -0
- package/types/set.ts +209 -0
- package/types/string.jn +33 -0
- package/types/string.ts +1 -0
- package/types/tuple.jn +11 -0
- package/README.md +0 -3
package/Modules/async.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/** JavaScript asynchronous primitives exposed to JunoScript. */
|
|
2
|
+
export type TimerHandle = object
|
|
3
|
+
export type ImmediateHandle = object
|
|
4
|
+
export type AsyncCallback = (...args: unknown[]) => void
|
|
5
|
+
export type FetchInput = string | URL | Request
|
|
6
|
+
export type FetchOptions = RequestInit
|
|
7
|
+
export type FetchResponse = Response
|
|
8
|
+
export type RejectedResult<E = unknown> = { ok: false; error: E }
|
|
9
|
+
|
|
10
|
+
const PromiseFactory = function<T>(
|
|
11
|
+
executor: (resolve: (value: T | PromiseLike<T> | RejectedResult) => void) => void
|
|
12
|
+
): Promise<T> {
|
|
13
|
+
if (arguments.length !== 1) throw new TypeError('Promise() accepts exactly one executor callback')
|
|
14
|
+
return new globalThis.Promise<T>((resolve, reject) => executor(value => {
|
|
15
|
+
if (isRejectedResult(value)) reject(value.error)
|
|
16
|
+
else resolve(value)
|
|
17
|
+
}))
|
|
18
|
+
} as unknown as PromiseConstructor
|
|
19
|
+
|
|
20
|
+
Object.setPrototypeOf(PromiseFactory, globalThis.Promise)
|
|
21
|
+
for (const name of ['all', 'allSettled', 'any', 'race', 'reject', 'resolve', 'withResolvers'] as const) {
|
|
22
|
+
const member = (globalThis.Promise as unknown as Record<string, Function | undefined>)[name]
|
|
23
|
+
if (!member) continue
|
|
24
|
+
const value = name === 'resolve'
|
|
25
|
+
? <T>(input: T | PromiseLike<T>): Promise<Awaited<T>> => isRejectedResult(input)
|
|
26
|
+
? globalThis.Promise.reject(input.error)
|
|
27
|
+
: globalThis.Promise.resolve(input) as Promise<Awaited<T>>
|
|
28
|
+
: name === 'withResolvers'
|
|
29
|
+
? <T>() => {
|
|
30
|
+
const controls = (globalThis.Promise as unknown as {
|
|
31
|
+
withResolvers<U>(): { promise: Promise<U>; resolve(value: U | PromiseLike<U>): void; reject(reason?: unknown): void }
|
|
32
|
+
}).withResolvers<T>()
|
|
33
|
+
return {
|
|
34
|
+
promise: controls.promise,
|
|
35
|
+
resolve: (input: T | PromiseLike<T> | RejectedResult): void => {
|
|
36
|
+
if (isRejectedResult(input)) controls.reject(input.error)
|
|
37
|
+
else controls.resolve(input)
|
|
38
|
+
},
|
|
39
|
+
reject: controls.reject
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
: member.bind(globalThis.Promise)
|
|
43
|
+
Object.defineProperty(PromiseFactory, name, { value, enumerable: true })
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isRejectedResult(value: unknown): value is { ok: false; error: unknown } {
|
|
47
|
+
return value !== null && typeof value === 'object' && 'ok' in value && value.ok === false && 'error' in value
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { PromiseFactory as Promise }
|
|
51
|
+
|
|
52
|
+
export function setTimeout<T, TArgs extends any[]>(callback: (...args: TArgs) => T, delay: number, ...args: TArgs): T {
|
|
53
|
+
return globalThis.setTimeout(callback, delay, ...args) as unknown as T
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function setInterval<T, TArgs extends any[]>(callback: (...args: TArgs) => T, delay?: number, ...args: TArgs): TimerHandle {
|
|
57
|
+
return globalThis.setInterval(callback, delay ?? 0, ...args)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function clearTimeout(handle: TimerHandle): void { globalThis.clearTimeout(handle as NodeJS.Timeout) }
|
|
61
|
+
export function clearInterval(handle: TimerHandle): void { globalThis.clearInterval(handle as NodeJS.Timeout) }
|
|
62
|
+
|
|
63
|
+
export function setImmediate(callback: AsyncCallback, ...args: unknown[]): ImmediateHandle {
|
|
64
|
+
return globalThis.setImmediate(callback, ...args)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function clearImmediate(handle: ImmediateHandle): void { globalThis.clearImmediate(handle as NodeJS.Immediate) }
|
|
68
|
+
export function queueMicrotask(callback: AsyncCallback): void { globalThis.queueMicrotask(callback) }
|
|
69
|
+
export function fetch(input: FetchInput, init?: FetchOptions): Promise<FetchResponse> { return globalThis.fetch(input, init) }
|
|
70
|
+
|
|
71
|
+
export default {
|
|
72
|
+
Promise: PromiseFactory,
|
|
73
|
+
setTimeout,
|
|
74
|
+
setInterval,
|
|
75
|
+
clearTimeout,
|
|
76
|
+
clearInterval,
|
|
77
|
+
setImmediate,
|
|
78
|
+
clearImmediate,
|
|
79
|
+
queueMicrotask,
|
|
80
|
+
fetch
|
|
81
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import * as nodeFs from 'node:fs'
|
|
2
|
+
import * as promises from 'node:fs/promises'
|
|
3
|
+
|
|
4
|
+
/** A serializable subset of Node's fs.Stats. */
|
|
5
|
+
export type FileStat = {
|
|
6
|
+
size: number
|
|
7
|
+
createdAt: number
|
|
8
|
+
modifiedAt: number
|
|
9
|
+
isFile: boolean
|
|
10
|
+
isDirectory: boolean
|
|
11
|
+
isSymbolicLink: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function readFileSync(path: string, encoding = 'utf8'): string {
|
|
15
|
+
return nodeFs.readFileSync(path, { encoding: encoding as BufferEncoding })
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function writeFileSync(path: string, data: string, encoding = 'utf8'): void {
|
|
19
|
+
nodeFs.writeFileSync(path, data, { encoding: encoding as BufferEncoding })
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function appendFileSync(path: string, data: string, encoding = 'utf8'): void {
|
|
23
|
+
nodeFs.appendFileSync(path, data, { encoding: encoding as BufferEncoding })
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function existsSync(path: string): boolean { return nodeFs.existsSync(path) }
|
|
27
|
+
|
|
28
|
+
export function mkdirSync(path: string, recursive = false): void {
|
|
29
|
+
nodeFs.mkdirSync(path, { recursive })
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function readdirSync(path: string): string[] { return nodeFs.readdirSync(path) }
|
|
33
|
+
|
|
34
|
+
export function removeSync(path: string, recursive = false, force = false): void {
|
|
35
|
+
nodeFs.rmSync(path, { recursive, force })
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function renameSync(oldPath: string, newPath: string): void { nodeFs.renameSync(oldPath, newPath) }
|
|
39
|
+
export function copyFileSync(source: string, destination: string): void { nodeFs.copyFileSync(source, destination) }
|
|
40
|
+
|
|
41
|
+
export function statSync(path: string): FileStat {
|
|
42
|
+
return fileStat(nodeFs.statSync(path))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function fileStat(value: nodeFs.Stats): FileStat {
|
|
46
|
+
return {
|
|
47
|
+
size: value.size,
|
|
48
|
+
createdAt: value.birthtimeMs,
|
|
49
|
+
modifiedAt: value.mtimeMs,
|
|
50
|
+
isFile: value.isFile(),
|
|
51
|
+
isDirectory: value.isDirectory(),
|
|
52
|
+
isSymbolicLink: value.isSymbolicLink()
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function readFile(path: string, encoding = 'utf8'): Promise<string> {
|
|
57
|
+
return promises.readFile(path, { encoding: encoding as BufferEncoding })
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function writeFile(path: string, data: string, encoding = 'utf8'): Promise<null> {
|
|
61
|
+
await promises.writeFile(path, data, { encoding: encoding as BufferEncoding })
|
|
62
|
+
return null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function appendFile(path: string, data: string, encoding = 'utf8'): Promise<null> {
|
|
66
|
+
await promises.appendFile(path, data, { encoding: encoding as BufferEncoding })
|
|
67
|
+
return null
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function exists(path: string): Promise<boolean> {
|
|
71
|
+
return promises.access(path).then(() => true, () => false)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function mkdir(path: string, recursive = false): Promise<null> {
|
|
75
|
+
await promises.mkdir(path, { recursive })
|
|
76
|
+
return null
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function readdir(path: string): Promise<string[]> {
|
|
80
|
+
return promises.readdir(path)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function remove(path: string, recursive = false, force = false): Promise<null> {
|
|
84
|
+
await promises.rm(path, { recursive, force })
|
|
85
|
+
return null
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function rename(oldPath: string, newPath: string): Promise<null> {
|
|
89
|
+
await promises.rename(oldPath, newPath)
|
|
90
|
+
return null
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export async function copyFile(source: string, destination: string): Promise<null> {
|
|
94
|
+
await promises.copyFile(source, destination)
|
|
95
|
+
return null
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function stat(path: string): Promise<FileStat> {
|
|
99
|
+
return fileStat(await promises.stat(path))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export default {
|
|
103
|
+
readFile, writeFile, appendFile, exists, mkdir, readdir, remove, rename, copyFile, stat,
|
|
104
|
+
readFileSync, writeFileSync, appendFileSync, existsSync, mkdirSync, readdirSync, removeSync, renameSync, copyFileSync, statSync
|
|
105
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# Type declarations for the native juno:test runtime module.
|
|
2
|
+
type Matchers = {
|
|
3
|
+
that(actual: any): JestMatchers
|
|
4
|
+
toBe<T>(actual: T, expected: T): void
|
|
5
|
+
toBeEqual<T>(actual: T, expected: T): void
|
|
6
|
+
toEqual<T>(actual: T, expected: T): void
|
|
7
|
+
toStrictEqual<T>(actual: T, expected: T): void
|
|
8
|
+
toBeTrue(actual: boolean): void
|
|
9
|
+
toBeTruthy(actual: any): void
|
|
10
|
+
toBeFalse(actual: boolean): void
|
|
11
|
+
toBeFalsy(actual: any): void
|
|
12
|
+
toBeDefined<T>(actual: T): void
|
|
13
|
+
toBeUndefined(actual: any): void
|
|
14
|
+
toBeNull<T>(actual: T): void
|
|
15
|
+
toBeNaN(actual: number): void
|
|
16
|
+
toBeCloseTo(actual: number, expected: number, precision: number = null): void
|
|
17
|
+
toBeGreaterThan(actual: number, expected: number): void
|
|
18
|
+
toBeGreaterThanOrEqual(actual: number, expected: number): void
|
|
19
|
+
toBeLessThan(actual: number, expected: number): void
|
|
20
|
+
toBeLessThanOrEqual(actual: number, expected: number): void
|
|
21
|
+
toContain(actual: any, expected: any): void
|
|
22
|
+
toContainEqual(actual: any, expected: any): void
|
|
23
|
+
toHaveLength(actual: any, expected: number): void
|
|
24
|
+
toHaveProperty(actual: object, path: string, expected: any = null): void
|
|
25
|
+
toMatch(actual: string, expected: string | RegEx): void
|
|
26
|
+
toMatchObject(actual: object, expected: object): void
|
|
27
|
+
toThrow(actual: (any) -> any, expected: any = null): void
|
|
28
|
+
toBeInstanceOf(actual: any, expected: any): void
|
|
29
|
+
toHaveBeenCalled(actual: (any) -> any): void
|
|
30
|
+
toHaveBeenCalledTimes(actual: (any) -> any, count: number): void
|
|
31
|
+
toHaveBeenCalledWith(actual: (any) -> any, ...args: any[]): void
|
|
32
|
+
toHaveBeenLastCalledWith(actual: (any) -> any, ...args: any[]): void
|
|
33
|
+
toHaveBeenNthCalledWith(actual: (any) -> any, nth: number, ...args: any[]): void
|
|
34
|
+
toHaveReturned(actual: (any) -> any): void
|
|
35
|
+
toHaveReturnedTimes(actual: (any) -> any, count: number): void
|
|
36
|
+
toHaveReturnedWith(actual: (any) -> any, value: any): void
|
|
37
|
+
toHaveLastReturnedWith(actual: (any) -> any, value: any): void
|
|
38
|
+
toHaveNthReturnedWith(actual: (any) -> any, nth: number, value: any): void
|
|
39
|
+
any(constructorValue: any): any
|
|
40
|
+
anything(): any
|
|
41
|
+
arrayContaining(value: any[]): any
|
|
42
|
+
arrayOf(value: any): any
|
|
43
|
+
closeTo(value: number, precision: number = null): any
|
|
44
|
+
objectContaining(value: object): any
|
|
45
|
+
stringContaining(value: string): any
|
|
46
|
+
stringMatching(value: string | RegEx): any
|
|
47
|
+
assertions(count: number): void
|
|
48
|
+
hasAssertions(): void
|
|
49
|
+
extend(matchers: object): void
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type JestMatchers = {
|
|
53
|
+
readonly not: JestMatchers
|
|
54
|
+
toBe(expected: any): void
|
|
55
|
+
toEqual(expected: any): void
|
|
56
|
+
toStrictEqual(expected: any): void
|
|
57
|
+
toBeTruthy(): void
|
|
58
|
+
toBeFalsy(): void
|
|
59
|
+
toBeDefined(): void
|
|
60
|
+
toBeUndefined(): void
|
|
61
|
+
toBeNull(): void
|
|
62
|
+
toBeNaN(): void
|
|
63
|
+
toBeCloseTo(expected: number, precision: number = null): void
|
|
64
|
+
toContain(expected: any): void
|
|
65
|
+
toContainEqual(expected: any): void
|
|
66
|
+
toHaveLength(expected: number): void
|
|
67
|
+
toHaveProperty(path: string, expected: any = null): void
|
|
68
|
+
toMatch(expected: string | RegEx): void
|
|
69
|
+
toMatchObject(expected: object): void
|
|
70
|
+
toThrow(expected: any = null): void
|
|
71
|
+
toHaveBeenCalled(): void
|
|
72
|
+
toHaveBeenCalledTimes(count: number): void
|
|
73
|
+
toHaveBeenCalledWith(...args: any[]): void
|
|
74
|
+
toHaveReturnedWith(value: any): void
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type MockFunction = {
|
|
78
|
+
fun (...args: any[]): any
|
|
79
|
+
readonly mock: any
|
|
80
|
+
mockClear(): MockFunction
|
|
81
|
+
mockReset(): MockFunction
|
|
82
|
+
mockRestore(): void
|
|
83
|
+
mockName(name: string): MockFunction
|
|
84
|
+
getMockName(): string
|
|
85
|
+
getMockImplementation(): ((any) -> any) | null
|
|
86
|
+
mockImplementation(implementation: (any) -> any): MockFunction
|
|
87
|
+
mockImplementationOnce(implementation: (any) -> any): MockFunction
|
|
88
|
+
mockReturnThis(): MockFunction
|
|
89
|
+
mockReturnValue(value: any): MockFunction
|
|
90
|
+
mockReturnValueOnce(value: any): MockFunction
|
|
91
|
+
mockResolvedValue(value: any): MockFunction
|
|
92
|
+
mockResolvedValueOnce(value: any): MockFunction
|
|
93
|
+
mockRejectedValue(value: any): MockFunction
|
|
94
|
+
mockRejectedValueOnce(value: any): MockFunction
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
type TestFunction = {
|
|
98
|
+
fun (name: string, body: () -> any): any
|
|
99
|
+
only(name: string, body: () -> any): any
|
|
100
|
+
skip(name: string, body: () -> any): void
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export const expect : Matchers & { not: Matchers } = null
|
|
104
|
+
|
|
105
|
+
export fun Mock(implementation: (any) -> any = null): MockFunction =
|
|
106
|
+
return null
|
|
107
|
+
|
|
108
|
+
export fun spyOn(target: object, method: string): MockFunction =
|
|
109
|
+
return null
|
|
110
|
+
|
|
111
|
+
export fun replaceProperty(target: object, property: string, value: any): any =
|
|
112
|
+
return null
|
|
113
|
+
|
|
114
|
+
export fun isMockFunction(value: any): boolean =
|
|
115
|
+
return false
|
|
116
|
+
|
|
117
|
+
export fun clearAllMocks(): void =
|
|
118
|
+
return null
|
|
119
|
+
export fun resetAllMocks(): void =
|
|
120
|
+
return null
|
|
121
|
+
export fun restoreAllMocks(): void =
|
|
122
|
+
return null
|
|
123
|
+
|
|
124
|
+
export fun describe(name: string, body: () -> any): any =
|
|
125
|
+
return null
|
|
126
|
+
|
|
127
|
+
export const test: TestFunction = null
|
|
128
|
+
export const it: TestFunction = null
|
|
129
|
+
|
|
130
|
+
export fun beforeEach(callback: () -> any): void =
|
|
131
|
+
return null
|
|
132
|
+
export fun afterEach(callback: () -> any): void =
|
|
133
|
+
return null
|
|
134
|
+
|
|
135
|
+
export fun useFakeTimers(): void =
|
|
136
|
+
return null
|
|
137
|
+
export fun useRealTimers(): void =
|
|
138
|
+
return null
|
|
139
|
+
export fun clearAllTimers(): void =
|
|
140
|
+
return null
|
|
141
|
+
export fun runAllTimers(): void =
|
|
142
|
+
return null
|
|
143
|
+
export fun runAllTimersAsync(): Promise<void> =
|
|
144
|
+
return null
|
|
145
|
+
export fun runOnlyPendingTimers(): void =
|
|
146
|
+
return null
|
|
147
|
+
export fun advanceTimersByTime(milliseconds: number): void =
|
|
148
|
+
return null
|
|
149
|
+
export fun advanceTimersByTimeAsync(milliseconds: number): Promise<void> =
|
|
150
|
+
return null
|
|
151
|
+
export fun advanceTimersToNextTimer(steps: number = null): void =
|
|
152
|
+
return null
|
|
153
|
+
export fun setSystemTime(time: number): void =
|
|
154
|
+
return null
|
|
155
|
+
export fun getTimerCount(): number =
|
|
156
|
+
return 0
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildJunoFile = buildJunoFile;
|
|
4
|
+
const node_fs_1 = require("node:fs");
|
|
5
|
+
const node_path_1 = require("node:path");
|
|
6
|
+
const parser_1 = require("../Lexer/parser");
|
|
7
|
+
const language_1 = require("../vscode-junoscript/src/language");
|
|
8
|
+
function modulePath(fromDirectory, target) {
|
|
9
|
+
const path = (0, node_path_1.relative)(fromDirectory, target).replace(/\\/g, '/');
|
|
10
|
+
return path.startsWith('.') ? path : `./${path}`;
|
|
11
|
+
}
|
|
12
|
+
function exportedNames(statements) {
|
|
13
|
+
const values = new Set();
|
|
14
|
+
const types = new Set();
|
|
15
|
+
let defaultName;
|
|
16
|
+
for (const statement of statements) {
|
|
17
|
+
if (statement.kind !== 'Export')
|
|
18
|
+
continue;
|
|
19
|
+
const declaration = statement.declaration;
|
|
20
|
+
const names = declaration.kind === 'Destructure'
|
|
21
|
+
? declaration.bindings.map(binding => binding.name)
|
|
22
|
+
: declaration.kind === 'Import'
|
|
23
|
+
? [...(declaration.defaultImport ? [declaration.defaultImport] : []), ...declaration.namedImports]
|
|
24
|
+
: 'name' in declaration ? [declaration.name] : [];
|
|
25
|
+
for (const name of names) {
|
|
26
|
+
values.add(name);
|
|
27
|
+
if (declaration.kind === 'TypeDeclaration')
|
|
28
|
+
types.add(name);
|
|
29
|
+
if (statement.default)
|
|
30
|
+
defaultName = name;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return { values: [...values], types: [...types], defaultName };
|
|
34
|
+
}
|
|
35
|
+
function buildJunoFile(file, options = {}) {
|
|
36
|
+
const input = (0, node_path_1.resolve)(file);
|
|
37
|
+
const target = options.target ?? (options.outFile && (0, node_path_1.extname)(options.outFile).toLowerCase() === '.ts' ? 'ts' : 'js');
|
|
38
|
+
const output = (0, node_path_1.resolve)(options.outFile ?? input.replace(/\.jn$/i, target === 'ts' ? '.ts' : '.js'));
|
|
39
|
+
if (input === output)
|
|
40
|
+
throw new Error('Build output must be different from the input JN file');
|
|
41
|
+
const source = (0, node_fs_1.readFileSync)(input, 'utf8');
|
|
42
|
+
const diagnostics = (0, language_1.analyze)(source, input)?.diagnostics ?? [];
|
|
43
|
+
if (diagnostics.length) {
|
|
44
|
+
const details = diagnostics.map(item => `${input}:${item.line}:${item.column} - ${item.message}`).join('\n');
|
|
45
|
+
throw new Error(`JunoScript diagnostics prevented build:\n${details}`);
|
|
46
|
+
}
|
|
47
|
+
const statements = (0, parser_1.parse)(source, { filePath: input });
|
|
48
|
+
const exports = exportedNames(statements);
|
|
49
|
+
const runtime = (0, node_path_1.resolve)(__dirname, '../Runtime/interpreter.js');
|
|
50
|
+
const runtimeImport = modulePath((0, node_path_1.dirname)(output), runtime);
|
|
51
|
+
const sourceReference = modulePath((0, node_path_1.dirname)(output), input);
|
|
52
|
+
const banner = '// Generated by junode --build. Edit the .jn source instead.\n';
|
|
53
|
+
const generated = target === 'js'
|
|
54
|
+
? `${banner}"use strict";\nconst path = require("node:path");\nconst { runBuiltModule } = require(${JSON.stringify(runtimeImport)});\nconst __junoSource = ${JSON.stringify(source)};\nconst __junoFilename = path.resolve(__dirname, ${JSON.stringify(sourceReference)});\nmodule.exports = runBuiltModule(__junoSource, __junoFilename);\n`
|
|
55
|
+
: `${banner}import * as path from 'node:path'\nconst { runBuiltModule } = require(${JSON.stringify(runtimeImport)}) as typeof import(${JSON.stringify(runtimeImport)})\n\nconst __junoSource = ${JSON.stringify(source)}\nconst __junoFilename = path.resolve(__dirname, ${JSON.stringify(sourceReference)})\nconst __junoModule = runBuiltModule(__junoSource, __junoFilename) as Record<string, any>\n\n${exports.values.filter(name => name !== exports.defaultName).map(name => `export const ${name} = __junoModule[${JSON.stringify(name)}]`).join('\n')}${exports.types.length ? `\n${exports.types.map(name => `export type ${name} = any`).join('\n')}` : ''}${exports.defaultName ? `\nexport default __junoModule[${JSON.stringify(exports.defaultName)}]\n` : '\n'}`;
|
|
56
|
+
(0, node_fs_1.writeFileSync)(output, generated);
|
|
57
|
+
return output;
|
|
58
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const node_fs_1 = require("node:fs");
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
const lexer_1 = require("../Lexer/lexer");
|
|
6
|
+
const parser_1 = require("../Lexer/parser");
|
|
7
|
+
const root = (0, node_path_1.resolve)(__dirname, '../..');
|
|
8
|
+
const source = (0, node_fs_1.readFileSync)((0, node_path_1.resolve)(root, 'types/iterable.jn'), 'utf8');
|
|
9
|
+
const declaration = new parser_1.Parser((0, lexer_1.Lexer)(source).tokenize()).parse().find(statement => statement.kind === 'Export' && statement.declaration.kind === 'TypeDeclaration' && statement.declaration.name === 'Iterable');
|
|
10
|
+
if (!declaration || declaration.kind !== 'Export' || declaration.declaration.kind !== 'TypeDeclaration') {
|
|
11
|
+
throw new Error("types/iterable.jn must export type 'Iterable'");
|
|
12
|
+
}
|
|
13
|
+
function printType(type, generics) {
|
|
14
|
+
switch (type.kind) {
|
|
15
|
+
case 'NamedType':
|
|
16
|
+
return type.name === 'any' ? 'unknown' : type.name;
|
|
17
|
+
case 'LiteralType': return typeof type.value === 'string' ? JSON.stringify(type.value) : String(type.value);
|
|
18
|
+
case 'GenericType': return `${type.name}<${type.args.map(argument => printType(argument, generics)).join(', ')}>`;
|
|
19
|
+
case 'ArrayType': return `Array<${printType(type.element, generics)}>`;
|
|
20
|
+
case 'IndexedAccessType': return `${printType(type.object, generics)}[${printType(type.index, generics)}]`;
|
|
21
|
+
case 'TypeOfType': return 'unknown';
|
|
22
|
+
case 'ConditionalType': return `${printType(type.check, generics)} extends ${printType(type.target, generics)} ? ${printType(type.consequent, generics)} : ${printType(type.alternate, generics)}`;
|
|
23
|
+
case 'UnionType': return `(${type.types.map(part => printType(part, generics)).join(' | ')})`;
|
|
24
|
+
case 'IntersectionType': return `(${type.types.map(part => printType(part, generics)).join(' & ')})`;
|
|
25
|
+
case 'FunctionType': {
|
|
26
|
+
const parameters = type.params.map((param, index) => `${type.paramNames?.[index] ?? `arg${index}`}: ${printType(param, generics)}`);
|
|
27
|
+
if (type.rest)
|
|
28
|
+
parameters.push(`...${type.restName ?? 'args'}: ${printType(type.rest, generics)}`);
|
|
29
|
+
const genericParameters = type.genericParameters?.map(({ name, constraint, defaultType }) => `${name}${constraint ? ` extends ${printType(constraint, generics)}` : ''}${defaultType ? ` = ${printType(defaultType, generics)}` : ''}`);
|
|
30
|
+
return `${genericParameters ? `<${genericParameters.join(', ')}>` : ''}(${parameters.join(', ')}) => ${printType(type.result, generics)}`;
|
|
31
|
+
}
|
|
32
|
+
case 'ObjectType': return `{ ${type.members.map(member => {
|
|
33
|
+
const prefix = `${member.readonly ? 'readonly ' : ''}${member.name}${member.optional ? '?' : ''}`;
|
|
34
|
+
if (member.accessor === 'get')
|
|
35
|
+
return `get ${member.name}(): ${printType(member.type, generics)}`;
|
|
36
|
+
if (member.accessor === 'set') {
|
|
37
|
+
const params = (member.params ?? []).map(param => `${param.name}: ${param.type ? printType(param.type, generics) : 'unknown'}`).join(', ');
|
|
38
|
+
return `set ${member.name}(${params})`;
|
|
39
|
+
}
|
|
40
|
+
if (!member.method)
|
|
41
|
+
return `${prefix}: ${printType(member.type, generics)}`;
|
|
42
|
+
const params = (member.params ?? []).map((param, index) => `${param.name || `arg${index}`}${param.defaultValue ? '?' : ''}: ${param.type ? printType(param.type, generics) : 'unknown'}`).join(', ');
|
|
43
|
+
return `${prefix}(${params}): ${printType(member.type, generics)}`;
|
|
44
|
+
}).join('; ')} }`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const genericNames = declaration.declaration.generics;
|
|
48
|
+
const genericText = genericNames.length ? `<${genericNames.join(', ')}>` : '';
|
|
49
|
+
const typeText = printType(declaration.declaration.value, new Set(genericNames));
|
|
50
|
+
const output = `/** Generated from types/iterable.jn. Edit the JunoScript source instead. */\nexport type Iterable${genericText} = ${typeText}\n`;
|
|
51
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.resolve)(root, 'Modules/juno-types.ts'), output);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const interpreter_js_1 = require("../Runtime/interpreter.js");
|
|
5
|
+
const build_juno_js_1 = require("./build-juno.js");
|
|
6
|
+
const args = process.argv.slice(2);
|
|
7
|
+
const build = args[0] === '--build';
|
|
8
|
+
const file = args[build ? 1 : 0];
|
|
9
|
+
if (!file || file === '--help' || file === '-h') {
|
|
10
|
+
console.log('Usage:\n junode <file.jn> [args...]\n junode --build <file.jn> [--out <file.js|file.ts>] [--target js|ts]');
|
|
11
|
+
process.exit(file ? 0 : 2);
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
if (build) {
|
|
15
|
+
const outIndex = args.indexOf('--out');
|
|
16
|
+
const targetIndex = args.indexOf('--target');
|
|
17
|
+
const target = targetIndex >= 0 ? args[targetIndex + 1] : undefined;
|
|
18
|
+
if (target !== undefined && target !== 'js' && target !== 'ts')
|
|
19
|
+
throw new Error("--target must be 'js' or 'ts'");
|
|
20
|
+
const output = (0, build_juno_js_1.buildJunoFile)(file, {
|
|
21
|
+
outFile: outIndex >= 0 ? args[outIndex + 1] : undefined,
|
|
22
|
+
target: target
|
|
23
|
+
});
|
|
24
|
+
console.log(output);
|
|
25
|
+
}
|
|
26
|
+
else
|
|
27
|
+
(0, interpreter_js_1.runFile)(file);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
console.error(error instanceof Error ? error.stack ?? error.message : error);
|
|
31
|
+
process.exitCode = 1;
|
|
32
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type TokenType = 'Number' | 'BigInt' | 'String' | 'RegEx' | 'Identifier' | 'Keyword' | 'Newline' | 'Indent' | 'Dedent' | 'EOF' | 'Plus' | 'Minus' | 'Multiply' | 'Divide' | 'Percent' | 'Equal' | 'EqualEqual' | 'NotEqual' | 'PlusEqual' | 'MinusEqual' | 'MultiplyEqual' | 'DivideEqual' | 'PercentEqual' | 'PipeEqual' | 'AmpersandEqual' | 'Increment' | 'Decrement' | 'Less' | 'LessEqual' | 'Greater' | 'GreaterEqual' | 'Nullish' | 'Arrow' | 'FatArrow' | 'Colon' | 'Comma' | 'Dot' | 'Ellipsis' | 'Question' | 'Pipe' | 'Ampersand' | 'Bang' | 'LeftParen' | 'RightParen' | 'LeftBracket' | 'RightBracket' | 'LeftBrace' | 'RightBrace';
|
|
2
|
+
export type Token = {
|
|
3
|
+
type: 'Number';
|
|
4
|
+
value: number;
|
|
5
|
+
line: number;
|
|
6
|
+
column: number;
|
|
7
|
+
} | {
|
|
8
|
+
type: 'BigInt';
|
|
9
|
+
value: bigint;
|
|
10
|
+
line: number;
|
|
11
|
+
column: number;
|
|
12
|
+
} | {
|
|
13
|
+
type: 'String';
|
|
14
|
+
value: string;
|
|
15
|
+
line: number;
|
|
16
|
+
column: number;
|
|
17
|
+
} | {
|
|
18
|
+
type: 'RegEx';
|
|
19
|
+
value: {
|
|
20
|
+
source: string;
|
|
21
|
+
flags: string;
|
|
22
|
+
};
|
|
23
|
+
line: number;
|
|
24
|
+
column: number;
|
|
25
|
+
} | {
|
|
26
|
+
type: 'Identifier' | 'Keyword';
|
|
27
|
+
value: string;
|
|
28
|
+
line: number;
|
|
29
|
+
column: number;
|
|
30
|
+
} | {
|
|
31
|
+
type: Exclude<TokenType, 'Number' | 'BigInt' | 'String' | 'RegEx' | 'Identifier' | 'Keyword'>;
|
|
32
|
+
line: number;
|
|
33
|
+
column: number;
|
|
34
|
+
};
|
|
35
|
+
export type Lexer = {
|
|
36
|
+
pos: number;
|
|
37
|
+
source: string;
|
|
38
|
+
tokenize(): Token[];
|
|
39
|
+
readonly current: string;
|
|
40
|
+
readonly isAtEnd: boolean;
|
|
41
|
+
};
|
|
42
|
+
export declare function Lexer(source: string): Lexer;
|