-
Notifications
You must be signed in to change notification settings - Fork 1
/
char_builtins.go
61 lines (52 loc) · 1.09 KB
/
char_builtins.go
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
package main
import (
"errors"
"fmt"
"math/big"
"unicode"
)
func FnIsChar(nargs int) error {
if nargs != 1 {
return errors.New("Wrong arg count to char?")
}
_, ok := stack.Pop().(Char)
stack.Push(Boolean(ok))
return nil
}
func FnInteger2Char(nargs int) error {
if nargs != 1 {
return errors.New("Wrong arg count to integer->char")
}
v := stack.Pop()
i, ok := v.(Integer)
if !ok {
return fmt.Errorf("Got non-char to integer->char (%T)", v)
}
bi := big.Int(i)
stack.Push(Char(rune(bi.Int64())))
return nil
}
func FnCharUpcase(nargs int) error {
if nargs != 1 {
return errors.New("char-upcase takes 1 argument")
}
cv := stack.Pop()
c, ok := cv.(Char)
if !ok {
return errors.New("char-upcase takes a character as the argument")
}
stack.Push(Char(unicode.ToUpper(rune(c))))
return nil
}
func FnCharDowncase(nargs int) error {
if nargs != 1 {
return errors.New("char-downcase takes 1 argument")
}
cv := stack.Pop()
c, ok := cv.(Char)
if !ok {
return errors.New("char-downcase takes a character as the argument")
}
stack.Push(Char(unicode.ToLower(rune(c))))
return nil
}