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/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
- }