-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
60 lines (53 loc) · 1.76 KB
/
Copy pathcli.ts
File metadata and controls
60 lines (53 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Import necessary modules from the libraries
import { Args, Command } from '@effect/cli'
import { Terminal } from '@effect/platform'
import { BunContext, BunRuntime } from '@effect/platform-bun'
import { Console, Effect, Layer, Pretty } from 'effect'
import { PROMPT } from './repl/constants'
import { Parser } from './parser'
import { Eval } from './evaluator'
import { createEnvironment } from './object/environment'
import { objSchema } from '@/schemas/objs/union'
// Define the top-level command
const topLevel = Command.make('hello-world', {}, () =>
Console.log('Hello friend, welcome to the Monkey programming language'),
)
// Define a text argument
const text = Args.text({ name: 'text' })
// Create a command that logs the provided text argument to the console
// this takes text I want to return tokens on input
const echoCommand = Command.make('echo', { text }, ({ text }) =>
Console.log(text),
)
const replCommand = Command.make(
'repl',
{},
() =>
Effect.gen(function* () {
const terminal = yield* Terminal.Terminal
const env = createEnvironment()
while (true) {
yield* terminal.display(`${PROMPT}`)
const input = yield* terminal.readLine
const parser = yield* Parser
yield* parser.init(input)
const program = yield* parser.parseProgram
const evaluation = yield* Eval(program)(env, undefined)
yield* Console.log(Pretty.make(objSchema)(evaluation))
}
}),
// Console.log(input),
)
const command = topLevel.pipe(
Command.withSubcommands([echoCommand, replCommand]),
)
// Set up the CLI application
const cli = Command.run(command, {
name: 'Hello World CLI',
version: 'v1.0.0',
})
// Prepare and run the CLI application
cli(process.argv).pipe(
Effect.provide(Layer.mergeAll(Parser.Default, BunContext.layer)),
BunRuntime.runMain,
)