code / nomsu

Lines6.6K Lua5.1K PEG1.3K make117
2 others 83
Markdown60 Bourne Again Shell23
(95 lines)
1 #!/usr/bin/env nomsu -V7.0.0
2 ###
3 This file defines a Read-Evaluate-Print-Loop (REPL) for Nomsu
5 use "consolecolor"
6 use "filesystem"
7 use "commandline"
9 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 external:
12 (help) means:
13 say ("
14 This is the Nomsu v\($(NOMSU VERSION), joined with ".") interactive console.
15 You can type in Nomsu code here and hit 'enter' twice to run it.
16 To exit, type 'exit' or 'quit' and hit enter twice.
17 ")
19 (tutorial) means:
20 (use "tools/tutorial").run_with {.extras = []}
21 exit
23 command line program with $args:
24 say ("
26 \(bright)\(underscore)Welcome to the Nomsu v\($(NOMSU VERSION), joined with ".") interactive console!\
27 ..\(reset color)
28 press 'enter' twice to run a command
29 type 'tutorial' to run the tutorial
31 ")
33 ### Best way I know of to detect the number of return values and only
34 print if it's >0:
35 (say results of (*extra arguments*)) means:
36 $N = (select "#" (*extra arguments*))
37 if ($N == 0): return
38 for $ in (1 to $N):
39 $ret = (select $ (*extra arguments*))
40 if ($ret is "Text"):
41 $ret = (quote $ret)
42 say "\$ret"
44 repeat:
45 say (bright (yellow ">> ")) inline
46 $buff = []
47 repeat:
48 say (bright) inline
49 $line = ($io.read "*L")
50 say (reset color) inline
51 if (($line == "\n") or (not $line)):
52 if (#$buff > 0):
53 ### clear the line
54 if $(COLOR ENABLED):
55 say "\027[1A\027[2K" inline
57 go to (run buffer)
58 $buff, add ($line, with "\t" -> " ")
59 say (dim (yellow ".. ")) inline
60 --- (run buffer) ---
61 if (#$buff == 0): stop
62 $buff = ($buff, joined)
63 spoof file $buff
64 try:
65 $tree = ($buff parsed)
66 ..if it fails with $err:
67 say $err
68 do next
70 unless $tree:
71 do next
73 if ($tree.type == "Comment"):
74 say (dim "Comment:\($tree.1)")
75 do next
77 if ($tree.type != "FileChunks"): $tree = [$tree]
78 for $chunk in $tree:
79 try:
80 $lua = ($chunk as lua)
81 ..if it fails with $err: say $err
83 unless $lua:
84 do next
86 ### TODO: this is a bit hacky, it just defaults variables to global
87 so that stuff mostly works across multiple lines. It would be
88 nicer if local variables actually worked.
89 $lua, remove free vars
90 if (load "return \($lua, text)"):
91 $lua, prepend "return "
93 try:
94 say results of (run $lua)
95 ..if it fails with $err: say $err