Skip to content

Commit f207650

Browse files
committed
Add log and program.start_time to runtime context
1 parent d12c691 commit f207650

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/language/runtime/keywords.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { either, option, type Option } from '../../adts.js'
2+
import { writeJSON } from '../cli/output.js'
23
import { keywordHandlers as compilerKeywordHandlers } from '../compiling.js'
34
import type { Atom } from '../parsing.js'
45
import {
56
isFunctionNode,
67
makeFunctionNode,
78
makeObjectNode,
9+
serialize,
810
types,
911
type KeywordElaborationResult,
1012
type KeywordModule,
@@ -13,18 +15,20 @@ import {
1315
} from '../semantics.js'
1416
import { lookupPropertyOfObjectNode } from '../semantics/object-node.js'
1517

18+
const unserializableFunction = () =>
19+
either.makeLeft({
20+
kind: 'unserializableValue',
21+
message: 'this function cannot be serialized',
22+
})
23+
1624
const runtimeContext = makeObjectNode({
1725
environment: makeObjectNode({
1826
lookup: makeFunctionNode(
1927
{
2028
parameter: types.string,
2129
return: types.option(types.string),
2230
},
23-
() =>
24-
either.makeLeft({
25-
kind: 'unserializableValue',
26-
message: 'this function cannot be serialized',
27-
}),
31+
unserializableFunction,
2832
option.none,
2933
key => {
3034
if (typeof key !== 'string') {
@@ -53,6 +57,29 @@ const runtimeContext = makeObjectNode({
5357
},
5458
),
5559
}),
60+
log: makeFunctionNode(
61+
{
62+
parameter: types.something,
63+
return: types.object,
64+
},
65+
unserializableFunction,
66+
option.none,
67+
output => {
68+
const serializationResult = serialize(output)
69+
if (either.isLeft(serializationResult)) {
70+
return either.makeLeft({
71+
kind: 'panic',
72+
message: serializationResult.value.message,
73+
})
74+
} else {
75+
writeJSON(process.stderr, serializationResult.value)
76+
return either.makeRight(makeObjectNode({}))
77+
}
78+
},
79+
),
80+
program: makeObjectNode({
81+
start_time: new Date().toISOString(),
82+
}),
5683
})
5784

5885
export const handlers = {

src/language/semantics/type-system/prelude-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,6 @@ export const runtimeContext = makeObjectType('runtime_context', {
9191
environment: makeObjectType('', {
9292
lookup: makeFunctionType('', { parameter: string, return: option(string) }),
9393
}),
94+
log: makeFunctionType('', { parameter: something, return: option(object) }),
95+
program: makeObjectType('', { start_time: string }),
9496
})

0 commit comments

Comments
 (0)