-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtutor.py
executable file
·222 lines (211 loc) · 7.05 KB
/
tutor.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python3
from charcoal import Run
from random import choice, randint
from ast import literal_eval
import sys
import argparse
print("""\
Welcome to Charcoal tutor!
If you don't like copy-pasting commands, use -g for grave mode or \
-v for verbose mode.
If you've done this before, \
use -l to specify the last level you've completed.""")
parser = argparse.ArgumentParser(description="Helps learn Charcoal.")
parser.add_argument(
"-v", "--verbose", action="store_true", help="Use verbose mode."
)
parser.add_argument(
"-g", "--grave", action="store_true", help="Use grave mode."
)
parser.add_argument(
"-l", "--level", type=int, nargs="?", default=0, help="Start from a level."
)
argv = parser.parse_args()
i = argv.level
index = 1 if argv.grave else 2 if argv.verbose else 0
def true(code):
return True
trues = (true, true, true)
def word():
return "".join([
choice("bcdfghjklmnpqrstvwxz") + choice("aeiouy")
for _ in range(randint(2, 5))
])[::choice([-1, 1])].replace("q", "qu")
def ensure(text, strings, expected, limits, validate=trues, inp=""):
global i
i += 1
print("\033[32;1m=== Level %i ===\033[0m" % i)
print(text % strings[index])
print("Print:")
print(expected)
if inp:
inputs = literal_eval(inp)
print("With the input%s:" % ("s" * (len(inputs) > 1)))
print("\n".join(literal_eval(inp)))
try:
while True:
code = ""
while not len(code):
code = input("\033[36;1mCharcoal> \033[0m")
if len(code) > limits[index]:
print("Try again. Your code was too long.")
continue
if not validate[index](code):
print("Try again. Please use the syntax given.")
continue
result = Run(code, inp, grave=argv.grave, verbose=argv.verbose)
if result == expected:
print("Congratulations on passing level %i!" % i)
return
if len(result):
print("Try again. Your output was:")
print(result)
else:
print("""\
Your code gave no output.
Did you make a mistake? Please read the level text \
to make sure you did not miss anything important.""")
except (KeyboardInterrupt, EOFError):
print()
sys.exit()
for generator in [
lambda: (
lambda expected: (
lambda expected, length: (
"""\
The most important part of Charcoal is its literals.
A string is %s.""",
(
"a run of non-commands, \
and/or commands preceded by the escape character '´'",
"a run of non-commands, \
and/or commands preceded by the escape character '```'",
"""an ordinary Python string.
In verbose mode, Print() is needed to print a value."""
),
expected,
(length, length, length + 11)
)
)(expected, len(expected))
)(word()),
lambda: (
lambda expected: (
"A number is %s.",
(
"a run of any character in '⁰¹²³⁴⁵⁶⁷⁸⁹·'",
"a run of '`0'-'`9' or '`.'",
"an ordinary number"
),
expected,
(1, 2, 10)
)
)("-" * randint(5, 9)),
lambda: (
lambda expected: (
lambda expected, length: (
"""\
Another important part of Charcoal is its directional printing.
Preceding a number with one of the directions %s \
prints a line with a character selected from '-|/\\' in that direction.""",
(
"'←↑→↓↖↗↘↙'",
"'``0'-'``9' according to numpad directions",
"""
:Up, :Down, :Left, :Right:UpLeft, :UpRight, :DownLeft and :DownRight"""
),
expected,
(2, 5, 22)
)
)(expected, len(expected))
)(Run(choice("←↑→↓↖↗↘↙") + choice("⁵⁶⁷⁸⁹"))),
lambda: (lambda expected: (
"""\
A language is never complete without arithmetic.
Charcoal has the basic arithmetic operators %s,
but the cast operator %s is needed to see the result as a number,
and a separator %s is needed to delimit two of the same literal type.%s""",
(
("'⁺⁻×÷'", "'I'", "'¦'", ""),
("'`+`-`*`/'", "'`I'", "'`:'", ""),
(
"""
Plus (+), Minus (-), Times (*) and Divide (/)""",
"Cast",
"',', ';' or ' '",
"""
In verbose mode, operators are called like functions:
e.g. Plus(1, 'a') or +(1, 'a')"""
)
),
expected,
(5, 10, 22),
(
lambda code: code[0] == "I" and code[1] in "⁺⁻×÷",
lambda code: code[:2] == "`I" and code[2:4] in "`+`-`*`/",
lambda code: "Cast" in code and any(
operator in code
for operator in [
"+", "-", "*", "/", "Plus", "Minus", "Times", "Divide"
]
)
)
))(Run(
"I" + choice("⁺⁻×÷") + choice("³⁴⁵⁶⁷⁸⁹") + "¦" + choice("³⁴⁵⁶⁷⁸⁹")
)),
lambda: (lambda expected: (
"""\
Operators work between most types.""",
((), (), ()),
expected,
(5, 10, 25)
))(chr(randint(33, 126)) * randint(4, 9)),
lambda: (lambda expected: (
"""\
Charcoal can take number and string input, \
which are %s and %s respectively.""",
(
("'N'", "'S'"),
("'`N'", "'`S'"),
("'InputNumber()'", "'InputString()'")
),
expected,
(1, 2, 25),
(
lambda code: code == "S",
lambda code: code == "`S",
lambda code: "InputString" in code
),
repr([expected])
))(word()),
lambda: (lambda expected: (
"""\
Unlike most other golfing languages, \
Charcoal uses a conventional memory model.
This means that in Charcoal you need to specify arguments, \
and assign to variables.
To assign to a variable, which is one of %s,
use the assignment command %s with the value and the variable name.
The input operators allow you to specify a variable name \
to assign the input to the variable.""",
(
("'αβγδεζηθικλμνξπρσςτυφχψω'", "'A'"),
("'`a`b`g`d`e`z`h`q`i`k`l`m`n`x`p`r`s`v`t`u`f`c`y`w'", "'`A'"),
("'abgdezhqiklmnxprsvtufcyw'", "Assign")
),
expected * 2,
(3, 6, 35),
(
lambda code: "S" in code,
lambda code: "`S" in code,
lambda code: "InputString" in code
),
repr([expected])
))(word())
# TODO: https://github.com/somebody1234/Charcoal/wiki/Tutorial
# Next up:
# Assign
# Some more commands
# loops (modulus for For, if (a) {}
][argv.level:]:
ensure(*generator())
print("Congratulations on finishing the Charcoal interactive tutorial!")