septima-lang 0.0.1 → 0.0.4
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/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +2 -2
- package/dist/src/parser.d.ts +2 -0
- package/dist/src/parser.js +73 -6
- package/dist/src/runtime.d.ts +2 -3
- package/dist/src/runtime.js +7 -4
- package/dist/src/scanner.d.ts +2 -1
- package/dist/src/scanner.js +16 -4
- package/dist/src/septima.d.ts +32 -0
- package/dist/src/septima.js +79 -0
- package/dist/tests/parser.spec.js +7 -30
- package/dist/tests/{cdl.spec.d.ts → septima.spec.d.ts} +0 -0
- package/dist/tests/septima.spec.js +765 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/jest-output.json +1 -1
- package/package.json +1 -1
- package/src/a.js +66 -0
- package/src/index.ts +1 -1
- package/src/parser.ts +81 -6
- package/src/runtime.ts +7 -3
- package/src/scanner.ts +16 -4
- package/src/septima.ts +92 -0
- package/tests/parser.spec.ts +6 -6
- package/tests/{cdl.spec.ts → septima.spec.ts} +83 -10
- package/dist/src/cdl.d.ts +0 -33
- package/dist/src/cdl.js +0 -63
- package/dist/tests/cdl.spec.js +0 -692
- package/src/cdl.ts +0 -78
package/src/cdl.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { Parser } from './parser'
|
|
2
|
-
import { Result, ResultSink, ResultSinkImpl } from './result'
|
|
3
|
-
import { Runtime, Verbosity } from './runtime'
|
|
4
|
-
import { Scanner } from './scanner'
|
|
5
|
-
import { shouldNeverHappen } from './should-never-happen'
|
|
6
|
-
import { SourceCode } from './source-code'
|
|
7
|
-
|
|
8
|
-
interface Options {
|
|
9
|
-
/**
|
|
10
|
-
* A callback function to be invoked when the CDL program evaluated to `sink`. Allows the caller to determine which
|
|
11
|
-
* value will be returned in that case. For instance, passing `() => undefined` will translate a `sink` value to
|
|
12
|
-
* `undefined`. The default behavior is to throw an error.
|
|
13
|
-
*/
|
|
14
|
-
onSink?: (res: ResultSink) => unknown
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class Cdl {
|
|
18
|
-
private readonly scanner
|
|
19
|
-
private readonly sourceCode
|
|
20
|
-
private readonly parser
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Runs a CDL program and returns the value it evaluates to. If it evaluates to `sink`, returns the value computed
|
|
24
|
-
* by `options.onSink()` - if present, or throws an error - otherwise.
|
|
25
|
-
*
|
|
26
|
-
* This method is the simplest way to evaluate a CDL program. One can also use `.compute()` to get a higher degree
|
|
27
|
-
* of details about the result.
|
|
28
|
-
*
|
|
29
|
-
* @param input the source code of the CDL program
|
|
30
|
-
* @param options
|
|
31
|
-
* @returns the value that `input` evaluates to
|
|
32
|
-
*/
|
|
33
|
-
static run(input: string, options?: Options): unknown {
|
|
34
|
-
const onSink =
|
|
35
|
-
options?.onSink ??
|
|
36
|
-
((r: ResultSink) => {
|
|
37
|
-
throw new Error(r.message)
|
|
38
|
-
})
|
|
39
|
-
const res = new Cdl(input).compute()
|
|
40
|
-
if (res.tag === 'ok') {
|
|
41
|
-
return res.value
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (res.tag === 'sink') {
|
|
45
|
-
return onSink(res)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
shouldNeverHappen(res)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
constructor(readonly input: string) {
|
|
52
|
-
this.sourceCode = new SourceCode(this.input)
|
|
53
|
-
this.scanner = new Scanner(this.sourceCode)
|
|
54
|
-
this.parser = new Parser(this.scanner)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
compute(verbosity: Verbosity = 'quiet'): Result {
|
|
58
|
-
const ast = parse(this.parser)
|
|
59
|
-
const runtime = new Runtime(ast, verbosity, this.parser)
|
|
60
|
-
const c = runtime.compute()
|
|
61
|
-
|
|
62
|
-
if (c.value) {
|
|
63
|
-
if (!c.value.isSink()) {
|
|
64
|
-
return { value: c.value.export(), tag: 'ok' }
|
|
65
|
-
}
|
|
66
|
-
return new ResultSinkImpl(c.value, this.sourceCode)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const runtimeErrorMessage = `${c.errorMessage} when evaluating:\n${this.sourceCode.formatTrace(c.expressionTrace)}`
|
|
70
|
-
throw new Error(runtimeErrorMessage)
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function parse(arg: string | Parser) {
|
|
75
|
-
const parser = typeof arg === 'string' ? new Parser(new Scanner(new SourceCode(arg))) : arg
|
|
76
|
-
const ast = parser.parse()
|
|
77
|
-
return ast
|
|
78
|
-
}
|