-
Notifications
You must be signed in to change notification settings - Fork 15
/
builtins.xl
216 lines (193 loc) · 8.78 KB
/
builtins.xl
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
// *****************************************************************************
// builtins.xl XL project
// *****************************************************************************
//
// File description:
//
// Built-in operations that are pre-loaded by the compiler without
// any explicit `use` statement.
//
//
//
//
//
// *****************************************************************************
// This software is licensed under the GNU General Public License v3+
// (C) 2017-2020, Christophe de Dinechin <[email protected]>
// *****************************************************************************
// This file is part of XL
//
// XL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// XL is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with XL, in a file named COPYING.
// If not, see <https://www.gnu.org/licenses/>.
// *****************************************************************************
// Binary operations on naturals
X:natural + Y:natural as natural is builtin Add
X:natural - Y:natural as natural is builtin Sub
X:natural * Y:natural as natural is builtin Mul
X:natural / Y:natural as natural is builtin UDiv
X:natural rem Y:natural as natural is builtin URem
X:natural mod Y:natural as natural is builtin UMod
X:natural and Y:natural as natural is builtin And
X:natural or Y:natural as natural is builtin Or
X:natural xor Y:natural as natural is builtin Xor
X:natural shl Y:natural as natural is builtin Shl
X:natural shr Y:natural as natural is builtin LShr
-X:natural as integer is builtin Neg
not X:natural as natural is builtin Not
// Binary operations on integers
X:integer + Y:integer as integer is builtin Add
X:integer - Y:integer as integer is builtin Sub
X:integer * Y:integer as integer is builtin Mul
X:integer / Y:integer as integer is builtin UDiv
X:integer rem Y:integer as integer is builtin URem
X:integer mod Y:integer as integer is builtin UMod
X:integer and Y:integer as integer is builtin And
X:integer or Y:integer as integer is builtin Or
X:integer xor Y:integer as integer is builtin Xor
X:integer shl Y:natural as integer is builtin Shl
X:integer shr Y:natural as integer is builtin AShr
-X:integer as integer is builtin Neg
not X:integer as integer is builtin Not
// Binary operation on real numbers
X:real + Y:real as real is builtin FAdd
X:real - Y:real as real is builtin FSub
X:real * Y:real as real is builtin FMul
X:real / Y:real as real is builtin FDiv
X:real rem Y:real as real is C fmod
X:real mod Y:real as real is C xl_modf
-X:real as real is builtin FNeg
// Comparison operators
X:integer = Y:integer as boolean is builtin ICmpEQ
X:integer <> Y:integer as boolean is builtin ICmpNE
X:integer > Y:integer as boolean is builtin ICmpSGT
X:integer >= Y:integer as boolean is builtin ICmpSGE
X:integer < Y:integer as boolean is builtin ICmpSLT
X:integer <= Y:integer as boolean is builtin ICmpSLE
X:natural = Y:natural as boolean is builtin ICmpEQ
X:natural <> Y:natural as boolean is builtin ICmpNE
X:natural > Y:natural as boolean is builtin ICmpUGT
X:natural >= Y:natural as boolean is builtin ICmpUGE
X:natural < Y:natural as boolean is builtin ICmpULT
X:natural <= Y:natural as boolean is builtin ICmpULE
X:real = Y:real as boolean is builtin FCmpOEQ
X:real <> Y:real as boolean is builtin FCmpONE
X:real > Y:real as boolean is builtin FCmpOGT
X:real >= Y:real as boolean is builtin FCmpOGE
X:real < Y:real as boolean is builtin FCmpOLT
X:real <= Y:real as boolean is builtin FCmpOLE
X:text = Y:text as boolean is C xl_text_eq
X:text <> Y:text as boolean is C xl_text_ne
X:text > Y:text as boolean is C xl_text_gt
X:text >= Y:text as boolean is C xl_text_ge
X:text < Y:text as boolean is C xl_text_lt
X:text <= Y:text as boolean is C xl_text_le
X:boolean = Y:boolean as boolean is builtin BCmpEQ
X:boolean <> Y:boolean as boolean is builtin BCmpNE
X:boolean > Y:boolean as boolean is builtin BCmpGT
X:boolean >= Y:boolean as boolean is builtin BCmpGE
X:boolean < Y:boolean as boolean is builtin BCmpLT
X:boolean <= Y:boolean as boolean is builtin BCmpLE
X:boolean and Y:boolean as boolean is builtin BAnd
X:boolean or Y:boolean as boolean is builtin BOr
X:boolean xor Y:boolean as boolean is builtin BXor
not X:boolean as boolean is builtin BNot
// Basic math functions
abs X:integer as natural is builtin IAbs
abs X:real as real is builtin FAbs
extern real sqrt(real);
extern real sin(real);
extern real cos(real);
extern real tan(real);
extern real asin(real);
extern real acos(real);
extern real atan(real);
extern real exp(real);
extern real expm1(real);
extern real log(real);
extern real log10(real);
extern real log2(real);
extern real log1p(real);
pi is 3.1415926535897932384626433
// If-then-else statement
if true then TrueBody else FalseBody is TrueBody
if false then TrueBody else FalseBody is FalseBody
if true then TrueBody is TrueBody
if false then TrueBody is false
// Console / debugging operations
write X:text as boolean is C xl_write_text
write X:integer as boolean is C xl_write_integer
write X:natural as boolean is C xl_write_natural
write X:real as boolean is C xl_write_real
write X:character as boolean is C xl_write_character
print as boolean is C xl_write_cr
write true is write "true"
write false is write "false"
write Head, Rest is { write Head; write Rest }
print Items is { write Items; print }
// Tree shape and attributes
kind X:tree as natural is builtin TreeKind
left X:prefix as tree is builtin TreeLeft
right X:prefix as tree is builtin TreeRight
left X:postfix as tree is builtin TreeLeft
right X:postfix as tree is builtin TreeRight
left X:infix as tree is builtin TreeLeft
right X:infix as tree is builtin TreeRight
name X:infix as text is builtin InfixName
opening X:text as text is builtin TextOpening
closing X:text as text is builtin TextClosing
child X:block as tree is builtin BlockChild
opening X:block as text is builtin BlockOpening
closing X:block as text is builtin BlockClosing
// Min and max functions
min X, Y is if X < min Y then X else min Y
min Y is Y
max X, Y is if X > max Y then X else max Y
max Y is Y
// Interval test
X:real in A:real..B:real is X >= A and X <= B
// Factorial function
0! is 1
N:natural! is N * (N-1)!
// Boolean to text conversion
text true is "true"
text false is "false"
// A few additional text notations
Source:text / From:text / To:text is text_replace Source, From, To
Pat:text in Src:text is text_index Src, Pat, 0
Pat:text in Src:text from Idx:natural is text_index Src, Pat, Idx
// A utility function for C-style testing
good false is false
good 0 is false
good 0.0 is false
good "" is false
good nil is false
good Other is true
bad X is not good X
// Loops
while Condition loop Body is
if Condition then
Body
while Condition loop Body
until Condition loop Body is { while not Condition loop Body }
loop Body is { Body; loop Body }
for Var in Low..High loop Body is
Var := Low
while Var < High loop
Body
Var := Var + 1
every Duration, Body is loop { Body; sleep Duration }
Duration:real h is Duration * 3600
Duration:real m is Duration * 60
Duration:real s is Duration
Duration:real ms is Duration * 0.001