-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.bmx
772 lines (634 loc) · 18 KB
/
config.bmx
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
' Copyright (c) 2013-2024 Bruce A Henderson
'
' Based on the public domain Monkey "trans" by Mark Sibly
'
' This software is provided 'as-is', without any express or implied
' warranty. In no event will the authors be held liable for any damages
' arising from the use of this software.
'
' Permission is granted to anyone to use this software for any purpose,
' including commercial applications, and to alter it and redistribute it
' freely, subject to the following restrictions:
'
' 1. The origin of this software must not be misrepresented; you must not
' claim that you wrote the original software. If you use this software
' in a product, an acknowledgment in the product documentation would be
' appreciated but is not required.
'
' 2. Altered source versions must be plainly marked as such, and must not be
' misrepresented as being the original software.
'
' 3. This notice may not be removed or altered from any source
' distribution.
'
SuperStrict
Import BRL.LinkedList
Import BRL.Map
Import BRL.FileSystem
Import BRL.Math
Import "options.bmx"
Import "base.stringhelper.bmx"
Import "base64.bmx"
Import "enums.c"
Import "hash.c"
Import "math.c"
Import "zlib/zlib.bmx"
' debugging help
Const DEBUG:Int = False
Const ABORT_ON_NULL:Int = True
Const PROFILER:Int = False
Const DEBUGSTOP_ON_ERROR:Int = False
Const SHOW_INTERNALERR_LOCATION:Int = True
Global ENV_LANG$
Global _errInfo$
Global _errStack:TList = New TList
' bytes offset to the first field
Global OBJECT_BASE_OFFSET:Int = 8
' 4 bytes on 32-bit, 8 bytes on 64-bit
Global POINTER_SIZE:Int = 4
Global _symbols$[]=[ "..","[]",":*",":/",":+",":-",":|",":&",":~~",":shr",":shl",":sar",":mod"]
Global _symbols_map$[]=[ "..","[]","*=","/=","+=","-=","|=","&=","^=",">>=", "<<=",">>=","%=" ]
Global _fileHasher:TFileHash
Function PushErr( errInfo$ )
_errStack.AddLast _errInfo
_errInfo=errInfo
End Function
Function PopErr()
_errInfo=String(_errStack.RemoveLast())
End Function
Function Err( err$ )
If DEBUGSTOP_ON_ERROR Then
DebugStop ' useful for debugging!
End If
Throw "Compile Error: "+err + "~n" + _errInfo + "~n"
End Function
Function Warn( err$ )
'If DEBUGSTOP_ON_ERROR Then
' DebugStop ' useful for debugging!
'End If
Print "Compile Warning: "+err + "~n" + _errInfo + "~n"
End Function
Function FormatError:String(path:String, line:Int, char:Int)
Return "[" + path + ";" + line + ";" + char + "]"
End Function
Function InternalErr(errorLocation:String)
If DEBUGSTOP_ON_ERROR Then
DebugStop ' useful for debugging!
End If
Local locationMsg:String
If SHOW_INTERNALERR_LOCATION And errorLocation Then locationMsg = " in " + errorLocation
Throw "Compile Error: Internal Error" + locationMsg + ".~nPlease report the issue, with an example if possible, to https://github.com/bmx-ng/bcc/issues/new~n" + _errInfo + "~n"
End Function
Function IsSpace:Int( ch:Int )
Return ch<=Asc(" ") Or ch=$A0 ' NO-BREAK SPACE (U+00A0)
End Function
Function IsDigit:Int( ch:Int )
Return ch>=Asc("0") And ch<=Asc("9")
End Function
Function IsAlpha:Int( ch:Int )
Return (ch>=Asc("A") And ch<=Asc("Z")) Or (ch>=Asc("a") And ch<=Asc("z"))
End Function
Function IsBinDigit:Int( ch:Int )
Return ch=Asc("0") Or ch=Asc("1")
End Function
Function IsHexDigit:Int( ch:Int )
Return IsDigit(ch) Or (ch>=Asc("A") And ch<=Asc("F")) Or (ch>=Asc("a") And ch<=Asc("f"))
End Function
Function Todo()
Err "TODO!"
End Function
Function StringToLong:Long(value:String)
Local Sign:Int = 1
Local i:Int
While i < value.length And (value[i] = Asc("+") Or value[i] = Asc("-"))
If value[i] = Asc("-") Then
Sign = -1
End If
i :+ 1
Wend
Local n:Long = 0
While i < value.length
Local c:Int = value[i]
If Not IsDigit(c) Exit
n = n * 10 + (c-Asc("0"))
i :+ 1
Wend
Return n
End Function
Function IsStandardFunc:Int(func:String)
func = func.ToLower()
Global funcs:String = ";isalnum;isalpha;isascii;isblank;iscntrl;isdigit;isgraph;islower;isprint;ispunct;isspace;isupper;isxdigit;" + ..
"strlen;_wgetenv;_wputenv;"
Return funcs.Find(func) > 0
End Function
Function mapSymbol:String(sym:String)
For Local i:Int = 0 Until _symbols.length
If sym = _symbols[i] Then
Return _symbols_map[i]
End If
Next
Return sym
End Function
'enquote depending on ENV_LANG
'
Function LangEnquote$( str$ )
str=EscapeString(str)
' str=str.Replace( "~0","\0" ) 'Fix me?
For Local i:Int=0 Until str.Length
If str[i]>=32 And str[i]<128 Continue
Local t$,n:Int=str[i]
While n
Local c:Int=(n&15)+48
If c>=58 c:+97-58
t=Chr( c )+t
n=(n Shr 4) & $0fffffff
Wend
If Not t t="0"
If ENV_LANG = "cpp" Then
'Case "cpp"
t="~q~q\x"+t+"~q~q"
Else
t="\u"+("0000"+t)[-4..]
End If
str=str[..i]+t+str[i+1..]
i:+t.Length-1
Next
str="~q"+str+"~q"
If ENV_LANG="cpp" str="L"+str
Return str
End Function
Function EscapeString$(str$)
str=str.Replace( "\","\\" )
str=str.Replace( "~q","\~q" )
str=str.Replace( "~n","\n" )
str=str.Replace( "~r","\r" )
str=str.Replace( "~t","\t" )
Return str
End Function
Function EscapeLines:String(str:String)
str=str.Replace("~n", "Newline")
Return str
End Function
Function BmxEnquote$( str$ )
str=str.Replace( "~~","~~~~" )
str=str.Replace( "~q","~~q" )
str=str.Replace( "~n","~~n" )
str=str.Replace( "~r","~~r" )
str=str.Replace( "~t","~~t" )
str=str.Replace( "~0","~~0" )
str="~q"+str+"~q"
Return str
End Function
Function BmxUnquote$( str$, unquoted:Int = False )
Local length:Int
Local i:Int
If Not unquoted Then
If str.length < 2 Or str[str.length - 1] <> Asc("~q") Then
Err "Expecting expression but encountered malformed string literal"
End If
length = str.length - 1
i = 1
Else
length = str.length
End If
Local sb:TStringBuffer = New TStringBuffer
While i < length
Local c:Int = str[i]
i :+ 1
If c <> Asc("~~") Then
sb.AppendChar(c)
Continue
End If
If i = length Err "Bad escape sequence in string"
c = str[i]
i :+ 1
Select c
Case Asc("~~")
sb.AppendChar(c)
Case Asc("0")
sb.AppendChar(0)
Case Asc("t")
sb.AppendChar(Asc("~t"))
Case Asc("r")
sb.AppendChar(Asc("~r"))
Case Asc("n")
sb.AppendChar(Asc("~n"))
Case Asc("q")
sb.AppendChar(Asc("~q"))
Case Asc("$") ' hex
c = str[i]
i :+ 1
Local n:Int
While True
Local v:Int
If c >= Asc("0") And c <= Asc("9") Then
v = c-Asc("0")
Else If c >= Asc("a") And c <= Asc("f") Then
v = c-Asc("a")+10
Else If c >= Asc("A") And c <= Asc("F") Then
v = c-Asc("A")+10
Else If c <> Asc("~~")
Err "Bad escape sequence in string"
Else
Exit
End If
n = (n Shl 4) | (v & $f)
If i = length Err "Bad escape sequence in string"
c = str[i]
i :+ 1
Wend
If c <> Asc("~~") Err "Bad escape sequence in string"
sb.AppendChar(n)
Case Asc("%") ' bin
c = str[i]
i :+ 1
Local n:Int
While c = Asc("1") Or c = Asc("0")
n :Shl 1
If c = Asc("1") Then
n :| 1
End If
If i = length Err "Bad escape sequence in string"
c = str[i]
i :+ 1
Wend
If c <> Asc("~~") Err "Bad escape sequence in string"
sb.AppendChar(n)
Default
If c >= Asc("1") And c <= Asc("9") Then
Local n:Int
While c >= Asc("0") And c <= Asc("9")
n = n * 10 + (c-Asc("0"))
If i = length Err "Bad escape sequence in string"
c = str[i]
i :+ 1
Wend
If c <> Asc("~~") Err "Bad escape sequence in string"
sb.AppendChar(n)
Else
Err "Bad escape sequence in string"
End If
End Select
Wend
Return sb.ToString()
End Function
Function BmxProcessMultiString:String( str:String )
Local valid:Int
If str.length < 7 Then
Err "Expecting expression but encountered malformed multiline string literal"
End If
For Local i:Int = 0 Until 3
If str[i] <> Asc("~q") Or str[str.length -1 -i] <> Asc("~q") Then
Err "Expecting expression but encountered malformed multiline string literal"
End If
Next
str = str[3..str.length - 3]
' normalise line endings
str = str.Replace("~r~n", "~n").Replace("~r", "~n")
If str[0] <> Asc("~n") Then
Err "Expecting EOL but encountered malformed multiline string literal"
End If
str = str[1..]
Local LINES:String[] = str.Split("~n")
Local lineCount:Int = LINES.length - 1
Local last:String = LINES[lineCount]
Local i:Int = last.length - 1
While i >= 0
If last[i] <> Asc(" ") And last[i] <> Asc("~t") Then
Err "Expecting trailing whitespace"
End If
i :- 1
Wend
Local trailingIndent:String = last
' strip indent
If trailingIndent Then
For i = 0 Until lineCount
Local line:String = LINES[i]
If line.StartsWith(trailingIndent) Then
line = line[trailingIndent.length..]
LINES[i] = line
End If
Next
End If
' right trim
For i = 0 Until lineCount
Local line:String = LINES[i]
Local index:Int = line.length
While index
index :- 1
If line[index] <> Asc(" ") And line[index] <> Asc("~t") Then
Exit
End If
Wend
If index < line.length - 1 Then
line = line[..index + 1]
LINES[i] = line
End If
Next
Local sb:TStringBuffer = New TStringBuffer
For i = 0 Until lineCount
Local line:String = LINES[i]
Local length:Int = line.length
Local softWrap:Int
If line And line[line.length-1] = Asc("\") Then
softWrap = True
length :- 1
End If
If line Then
sb.Append(line[..length])
End If
If Not softWrap And i < lineCount - 1 Then
sb.Append("~n")
End If
Next
Return BmxUnquote(sb.ToString(), True)
End Function
Type TStack Extends TList
Method Push(obj:Object)
AddFirst(obj)
End Method
Method Length:Int()
Return count()
End Method
Method Get:Object(index:Int)
Return ValueAtIndex(index)
End Method
Method Pop:Object()
Return RemoveFirst()
End Method
End Type
Type TStringList Extends TList
Method Join:String(s:String)
Local arr:String[] = New String[count()]
Local index:Int
For Local t:String = EachIn Self
arr[index] = t
index :+ 1
Next
Return s.Join(arr)
End Method
End Type
Type TKeyValue
Field key:Object
Field value:Object
Method Create:TKeyValue(key:Object,value:Object)
Self.key = key
Self.value = value
Return Self
End Method
Method Compare:Int(other:Object)
If Not TKeyValue(other) Return 0
Return key.Compare(TKeyValue(other).key)
End Method
End Type
Type TUnorderedMap
Field list:TList = New TList
Field map:TMap = New TMap
Field valuesList:TList = New TList
Method Insert( key:Object,value:Object )
list.AddLAst(New TKeyValue.Create(key, value))
valuesList.AddLast(value)
map.Insert(key, value)
End Method
Method Keys:TList()
Local klist:TList = New TList
For Local kv:TKeyValue = EachIn list
klist.AddLast(kv.key)
Next
Return klist
End Method
Method Values:TList()
'Local vlist:TList = New TList
'For Local kv:TKeyValue = EachIn list
' vlist.AddLast(kv.value)
'Next
Return valuesList
End Method
Method Contains:Int( key:Object )
Return map.Contains(key)
End Method
Method ValueForKey:Object( key:Object )
Return map.ValueForKey(key)
End Method
End Type
Function MakeKeywords:String()
Local keywords:String
keywords :+ "import brl.classes~n"
keywords :+ "Asc%(v$)=~qbrl_blitz_keywords_asc~q~n"
keywords :+ "Chr$(v%)=~qbrl_blitz_keywords_chr~q~n"
keywords :+ "Len%(v:Object)=~qbrl_blitz_keywords_len~q~n"
keywords :+ "IncbinPtr@*(v$)=~qbbIncbinPtr~q~n"
keywords :+ "IncbinLen%(v$)=~qbbIncbinLen~q~n"
Return keywords
End Function
Function FilePath:String(path:String)
Local baseDir:String = ExtractDir(path)
Local bmxDir:String = baseDir + "/.bmx"
If FileType(bmxDir) <> FILETYPE_DIR Then
Throw "Missing : " + bmxDir
End If
Return bmxDir
End Function
Function BuildHeaderName:String(path:String)
If opt_buildtype = BUILDTYPE_MODULE Then
path = opt_modulename + "_" + StripDir(path)
Else
Local dir:String = ExtractDir(path).ToLower().Replace("/.bmx","")
dir = dir[dir.findLast("/") + 1..]
If dir.EndsWith(".mod") Then
dir = dir.Replace(".mod", "")
End If
Local file:String = StripDir(path).ToLower()
path = dir + "_" + file
End If
Return TStringHelper.Sanitize(path, , True)
End Function
Rem
bbdoc: Get the header file name from a given module ident, optionally with include path.
End Rem
Function ModuleHeaderFromIdent:String(ident:String, includePath:Int = False)
Local ns:String = ident[..ident.find(".")]
Local name:String = ident[ident.find(".") + 1..]
Local file:String = name + ".bmx" + FileMung() + ".h"
If includePath Then
file = ns + ".mod/" + name + ".mod/.bmx/" + file
End If
Return file
End Function
Function HeaderFile:String(path:String, mung:String)
Local fileDir:String = FilePath(path)
Local file:String = StripDir(path)
Return fileDir + "/" + file + mung + ".h"
End Function
Function OutputFilePath:String(path:String, mung:String, suffix:String, bmxDir:Int = False)
Local fileDir:String = FilePath(path)
If bmxDir Then
fileDir :+ "/.bmx"
End If
Local file:String = StripDir(path)
Return fileDir + "/" + file + mung + "." + suffix
End Function
Function FileMung:String(makeApp:Int = False)
Local m:String = "."
If makeApp Then
Select opt_apptype
Case APPTYPE_CONSOLE
m :+ "console."
Case APPTYPE_GUI
m :+ "gui."
End Select
End If
If opt_release Then
m :+ "release"
Else
m :+ "debug"
End If
If opt_coverage Then
m :+ ".cov"
End If
' If opt_threaded Then
' m :+ ".mt"
' End If
m :+ "." + opt_platform
m :+ "." + opt_arch
Return m
End Function
Function HeaderComment:String()
' TODO
End Function
Global fileRegister:TMap = New TMap
Function GenHash:String(file:String)
Local Hash:String = bmx_gen_hash(file)
If Not fileRegister.Contains(Hash) Then
fileRegister.Insert(Hash, file)
End If
Return Hash
End Function
Type TTemplateRecord
Field start:Int
Field file:String
Field source:String
Method Create:TTemplateRecord(start:Int, file:String, source:String)
Self.start = start
Self.file = file
Self.source = source
Return Self
End Method
Method ToString:String()
Local s:Byte Ptr = source.ToUTF8String()
?Not bmxng
Local slen:Int = strlen_(s)
?bmxng
Local slen:ULongInt = strlen_(s)
?
?Not bmxng
Local dlen:Int = slen + 12
?bmxng
Local dlen:ULongInt = slen + 12
?
Local data:Byte[dlen]
compress2(data, dlen, s, slen, 9)
MemFree(s)
Local t:String = "{" + start +","+ slen +","+ LangEnquote(file) + ","
t :+ LangEnquote(TBase64.Encode(data, Int(dlen), 0, TBase64.DONT_BREAK_LINES))
Return t + "}"
End Method
Function Load:TTemplateRecord(start:Int, file:String, size:Int, source:String)
?Not bmxng
Local dlen:Int = size + 1
?bmxng
Local dlen:ULongInt = size + 1
?
Local data:Byte[dlen]
Local s:Byte[] = TBase64.Decode(source)
?Not bmxng
uncompress(data, dlen, s, s.length)
?bmxng
uncompress(data, dlen, s, ULongInt(s.length))
?
Return New TTemplateRecord.Create(start, file, String.FromUTF8String(data))
End Function
End Type
Type TCallback
Method Callback(obj:Object) Abstract
End Type
Type TFileHash
Field statePtr:Byte Ptr
Method Create:TFileHash()
statePtr = bmx_hash_createState()
Return Self
End Method
Method CalculateHash:String(stream:TStream)
Const BUFFER_SIZE:Int = 8192
bmx_hash_reset(statePtr)
Local data:Byte[BUFFER_SIZE]
While True
Local read:Int = stream.Read(data, BUFFER_SIZE)
bmx_hash_update(statePtr, data, read)
If read < BUFFER_SIZE Then
Exit
End If
Wend
Return bmx_hash_digest(statePtr)
End Method
End Type
Function CalculateFileHash:String(path:String)
If Not _fileHasher Then
_fileHasher = New TFileHash.Create()
End If
If FileType(path) = FILETYPE_FILE Then
Local stream:TStream = ReadStream(path)
Local fileHash:String = _fileHasher.CalculateHash(stream)
stream.Close()
Return fileHash
End If
Return Null
End Function
?Not bmxng
Const OP_MUL:Int = 0
Const OP_DIV:Int = 1
Const OP_MOD:Int = 2
Const OP_SHL:Int = 3
Const OP_SHR:Int = 4
Const OP_SAR:Int = 5
Const OP_ADD:Int = 6
Const OP_SUB:Int = 7
Const OP_AND:Int = 8
Const OP_XOR:Int = 9
Const OP_OR:Int = 10
Function OpToInt:Int(op:String)
Select op
Case "*" Return OP_MUL
Case "/" Return OP_DIV
Case "mod" Return OP_MOD
Case "shl" Return OP_SHL
Case "shr" Return OP_SHR
Case "sar" Return OP_SAR
Case "+" Return OP_ADD
Case "-" Return OP_SUB
Case "&" Return OP_AND
Case "~~" Return OP_XOR
Case "|" Return OP_OR
End Select
InternalErr "TBinaryMathExpr.Eval.OpToInt : " + op
End Function
?
Extern
Function strlen_:Int(s:Byte Ptr)="strlen"
Function bmx_enum_next_power(char:Int, val:Long Var, ret:Long Var)
Function bmx_gen_hash:String(txt:String)
Function bmx_hash_createState:Byte Ptr()
Function bmx_hash_reset(state:Byte Ptr)
Function bmx_hash_update(state:Byte Ptr, data:Byte Ptr, length:Int)
Function bmx_hash_digest:String(state:Byte Ptr)
Function bmx_bitwise_not_uint:String(value:String)
Function bmx_bitwise_not_sizet:String(value:String)
Function bmx_bitwise_not_ulong:String(value:String)
Function bmx_bitwise_not_longint:String(value:String, size:Int)
Function bmx_bitwise_not_ulongint:String(value:String, size:Int)
Function bmx_binarymathexpr_sizet:String(op:Int, lhs:String, rhs:String)
Function bmx_binarymathexpr_uint:String(op:Int, lhs:String, rhs:String)
Function bmx_binarymathexpr_ulong:String(op:Int, lhs:String, rhs:String)
Function bmx_binarymathexpr_longint:String(op:Int, lhs:String, rhs:String, size:Int)
Function bmx_binarymathexpr_ulongint:String(op:Int, lhs:String, rhs:String, size:Int)
End Extern