Repository navigation
Expand file tree
/
Copy pathinput.fl
More file actions
65 lines (58 loc) · 2.5 KB
/
Copy pathinput.fl
File metadata and controls
65 lines (58 loc) · 2.5 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
61
62
63
64
65
# input.fl -- talking to the person running the script.
#
# run it two ways:
#
# echo "Ada" | ./flint examples/input.fl
# ./flint examples/input.fl < names.txt
#
# the second form is the honest one. input() is for reading data piped in as
# much as for prompting a human, and the example is written so the same lines
# work for both.
#
# what input() does, in one paragraph: it writes the prompt with no newline,
# reads one line, drops the newline, and hands it back as a normal string.
# end of file gives nil, not an empty string, so a script can tell "the user
# pressed enter" from "there is nothing left to read".
#
# pipe a name in and watch the types disagree:
#
# echo "" | ./flint examples/input.fl
# echo "x" | head -c 0 | ./flint examples/input.fl
# the prompt goes out exactly as written, with no newline added, so the
# typing starts where the prompt ends
let name = input("What is your name? ")
print("hello, " + name)
# no prompt, no prompt output. just the line.
let raw = input()
print("you wrote: " + raw)
# empty input is a string with nothing in it, because someone pressing enter
# on an empty line is an answer, and crashing on it would be a surprise
let blank = input()
print("empty is a string: " + str(type(blank)))
print("and it is this long: " + str(len(blank)))
# end of file is nil. this reads until there is nothing left, which is the
# only sane loop over stdin there is:
#
# while true {
# let line = input()
# if line == nil { break }
# print("got: " + line)
# }
#
# the loop above is commented out rather than deleted so the example ends at
# a fixed place. reading until EOF in one go is a real program shape; try it
# interactively by typing lines and ending with ctrl-D.
# a long line. the buffer doubles from 64 bytes, so nothing is truncated and
# a script that reads a file-like amount of text through stdin never notices.
let big = input()
print("long line was " + str(len(big)) + " bytes")
print(big)
# input() returns a string, always. to do arithmetic on what the user typed,
# convert it: `as number` asserts a type and will not convert one, so a string
# that looks like a number still needs num().
let answer = num(input("a number, please: "))
print("twice that is " + str(answer * 2))
# input() with the wrong shape is an ordinary runtime error, like everything
# else that is wrong-shaped in this language
# print(input("one", "two")) # error: expected 0 or 1 arguments
# print(input(42)) # error: argument to input() must be a string