-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhello.ugt
114 lines (92 loc) · 1.96 KB
/
hello.ugt
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
module Hello
infixr <| 0
infixl |> 0
infixl |>> 5
infixl <<| 5
let <| = f -> x -> f x
let |> = x -> f -> f x
let |>> = f -> g -> x -> g (f x)
let <<| = f -> g -> x -> f (g x)
let from_py = pyfunc -> arg -> pyfunc.(arg)
let to_py = extern "to_py_callable"
let print = from_py extern "print"
let failwith = extern "failwith"
let imp =
let imp = from_py extern "__import__"
let imp = imp "importlib"
in from_py <| imp.import_module
let io = imp "io"
let op = imp "operator"
let repr = from_py extern "repr"
// arithmetic operators
infixl + 10
infixl - 10
infixl * 20
infixl / 20
infixl \\ 20
infixr ^ 30
infixl !! 50
let _add = op.add
let _sub = op.sub
let _mul = op.mul
let _pow = op.pow
let _fdiv = op.truediv
let _sdiv = op.floordiv
let _item = op.getitem
let + = x -> y -> _add.(x, y)
let - = x -> y -> _sub.(x, y)
let * = x -> y -> _mul.(x, y)
let \\ = x -> y -> _sdiv.(x, y)
let / = x -> y -> _fdiv.(x, y)
let ^ = x -> y -> _pow.(x, y)
let !! = x -> y -> _item.(x, y)
let setattr = x -> y -> v -> (extern "setattr").(x, y, v)
// cmp operations
infixl < 8
infixl > 8
infixl >= 8
infixr <= 8
infixl == 8
let _lt = op.lt
let _gt = op.gt
let _eq = op.eq
let < = x -> y -> _lt.(x, y)
let > = x -> y -> _gt.(x, y)
let == = x -> y -> _eq.(x, y)
let >= =
let _ge = op.ge in
x -> y -> _ge.(x, y)
let <= =
let _le = op.le in
x -> y -> _le.(x, y)
// list
infixr :: 1
data Cons(_, _), Nil
let :: = Cons
// control flow
// TODO: use bytecode to write one that uses loops
rec each = f -> seq ->
seq match
[] => (),
hd :: tl =>
do f hd
in each f tl
rec for = seq -> f ->
seq match
[] => (),
hd :: tl =>
do f hd
in for tl f
let fori =
rec fori = i -> seq -> f ->
seq match
[] => (),
hd :: tl =>
do f i hd
in fori (i + 1) tl f
in fori
rec while = cond -> f ->
cond () ?
do f ()
in while cond f
else ()