Source

snakes-on-a-moon / library.py

Full commit
  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
773
# basic library: holds lua standard library functions and variables

import sys
import math
import random
from luatypes import *

def lua_assert(args):
    v = args[0]
    message = args[1] if len(args) >= 2 else 'assertion failed!'
    assert v != nil or v != False, message
    return args

def lua_collectgarbage(args):
    raise AssertionError('collectgarbage not supported')

def lua_dofile(filename):
    print 'dofile NYI'

def lua_error(args):
    message = args[0]
    level = args[1] if len(args) > 1 else 1
    raise AssertionError(message)

_G = LuaTable(hash={ })
_G['_G'] = _G

def lua_getfenv(f=None):
    print 'getfenv NYI'

def lua_getmetatable(args):
    object = args[0]
    metatable = object.metatable
    if metatable and '__metatable' in metatable.hash:
        return [metatable['__metatable']]
    else:
        return [metatable]

def lua_ipairs(args):
    t = args[0]
    def iter_func(args):
        table = args[0]
        index = args[1]
        assert isinstance(index, (int, long, float))
        assert int(index) == index
        index = int(index)
        if index < len(table):
            nextindex = index + 1
            return [nextindex, table[nextindex]]
        else:
            return [None]
    return [iter_func, t, 0]

def lua_load(func, chunkname=None):
    print 'load NYI'

def lua_loadfile(filename=None):
    print 'loadfile NYI'

def lua_loadstring(string, chunkname=None):
    print 'loadstring NYI'

def lua_next(args):
    table = args[0]
    index = args[1] if len(args) > 1 else None
    if index:
        if isinstance(index, (int, long, float)) \
            and int(index) == index \
            and index > 0:
            # index is array index
            index = int(index)
            if index < len(table):
                nextindex = index + 1
                return [nextindex, table[nextindex]]
            elif index == len(table):
                # last array index, so return first index and element in hash
                key = table.hash.keys()[0]
                return [key, table[key]]
        # otherwise index is hash key
        keys = table.hash.keys()
        i = keys.index(index)
        if i < len(keys) - 1:
            nextkey = keys[i+1]
            return [nextkey, table[nextkey]]
    else:
        if table.array:
            return [1, table[1]]
        elif table.hash:
            key = table.hash.keys()[0]
            return [key, table[key]]
    return [None]

def lua_pairs(args):
    t = args[0]
    return [lua_next, t, None]

def lua_pcall(f, args):
    print 'pcall NYI'

def lua_print(args):
    for arg in args:
        if isinstance(arg, float) and int(arg) == arg:
            print int(arg),
        else:
            print arg,
        print '\t',
    print

def lua_rawequal(v1, v2):
    print 'rawequal NYI'

def lua_rawget(table, index):
    print 'rawget NYI'

def lua_rawset(table, index, value):
    print 'rawset NYI'

def lua_select(args):
    index = args[0]
    if isinstance(index, (int, long, float)):
        return args[index+1:]
    assert index == '#'
    return [len(args) - 1]

def lua_setfenv(f, table):
    print 'setfenv NYI'

def lua_setmetatable(args):
    table = args[0]
    metatable = args[1]
    if table.metatable and '__metatable' in table.metatable.hash:
        raise AssertionError('table has a "__metatable" field')
    table.metatable = metatable
    return [table]

def lua_tonumber(args):
    e = args[0]
    base = args[1] if len(args) > 1 else 10
    return [int(e, base)]

def lua_tostring(args):
    e = args[0]
    # TODO check __tostring metamethod
    return [str(e)]

def lua_type(args):
    v = args[0]
    if v == None:
        return ['nil']
    if isinstance(v, (int, long, float)):
        return ['number']
    if isinstance(v, str):
        return ['string']
    if isinstance(v, bool):
        return ['boolean']
    if isinstance(v, LuaTable):
        return ['table']
    if isinstance(v, LuaFunction) or hasattr(v, '__call__'):
        return ['function']
    # TODO check if thread or userdata
    return ['thread or userdata']

def lua_unpack(args):
    list = args[0]
    assert isinstance(list, LuaTable)
    i = args[1] if len(args) > 1 else 1
    j = args[2] if len(args) > 2 else len(list)
    return [t[k] for k in xrange(i, j+1)]

_VERSION = 'Lua 5.1'

def lua_xpcall(f, err):
    print 'xpcall NYI'

# coroutine library

def coroutine_create(f):
    print 'coroutine.create NYI'

def coroutine_resume(co, vals=None):
    print 'coroutine.resume NYI'

def coroutine_running():
    print 'coroutine.running NYI'

def coroutine_status(co):
    print 'coroutine.status NYI'

def coroutine_wrap(f):
    print 'coroutine.wrap NYI'

def coroutine_yield(f):
    print 'coroutine.yield NYI'

coroutine = LuaTable(hash={
    'create': coroutine_create,
    'resume': coroutine_resume,
    'running': coroutine_running,
    'status': coroutine_status,
    'wrap': coroutine_wrap,
    'yield': coroutine_yield,
    })

# module library

def lua_module(name, args=None):
    print 'module NYI'

def lua_require(modname):
    print 'require NYI'

package_cpath = ''

package_loaded = LuaTable()

package_loaders = LuaTable()

def package_loadlib(libname, funcname):
    print 'package.loadlib NYI'

package_path = ''

package_preload = LuaTable()

def package_seeall(module):
    print 'package.seeall NYI'

module = LuaTable(hash={
    'cpath': package_cpath,
    'loaded': package_loaded,
    'loaders': package_loaders,
    'loadlib': package_loadlib,
    'path': package_path,
    'preload': package_preload,
    'seeall': package_seeall,
    })

# string library

def string_byte(args):
    s = args[0]
    i = args[1] if len(args) > 1 else 1
    j = args[2] if len(args) > 2 else 1
    return [ord(s[k]) for k in xrange(i-1, j)]

def string_char(args):
    return [''.join([chr(i) for i in args])]

def string_dump(args):
    raise AssertionError('string.dump NYI')

def string_find(args):
    raise AssertionError('string.find NYI')

def string_format(args):
    formatstring = args[0]
    result = formatstring % tuple(args[1:])
    return [result]

def string_gmatch(args):
    raise AssertionError('string.gmatch NYI')

def string_gsub(args):
    raise AssertionError('string.gsub NYI')

def string_len(args):
    s = args[0]
    return [len(s)]

def string_lower(args):
    s = args[0]
    return [str.lower(s)]

def string_match(args):
    raise AssertionError('string.match NYI')

def string_rep(args):
    s = args[0]
    n = args[1]
    return [s * n]

def string_reverse(args):
    s = args[0]
    l = list(s)
    l.reverse()
    return [''.join(l)]

def string_sub(args):
    s = args[0]
    i = args[1]
    j = args[2] if len(args) > 2 else -1
    if i > 0:
        i = i - 1
    elif i == 0:
        i = 1
    if j == -1:
        j = len(s)
    return [s[i:j]]

def string_upper(args):
    s = args[0]
    return [str.upper(s)]

string = LuaTable(hash={
    'byte': string_byte,
    'char': string_char,
    'dump': string_dump,
    'find': string_find,
    'format': string_format,
    'gmatch': string_gmatch,
    'gsub': string_gsub,
    'len': string_len,
    'lower': string_lower,
    'match': string_match,
    'rep': string_rep,
    'reverse': string_reverse,
    'sub': string_sub,
    'upper': string_upper,
    })

# table library

def table_concat(args):
    lenargs = len(args)
    table = args[0]
    sep = args[1] if lenargs > 1 else ''
    i = args[2] if lenargs > 2 else 1
    j = args[3] if lenargs > 3 else len(table)
    return [sep.join([str(table[k]) for k in xrange(i, j+1)])]

def table_insert(args):
    table = args[0]
    pos = args[1] if len(args) > 2 else len(table)+1
    value = args[2] if len(args) > 2 else args[1]
    table.array.insert(pos-1, value)
    return []

def table_maxn(args):
    table = args[0]
    i = len(table)
    while i > 0:
        if table[i] != None:
            return [i]
        i -= 1
    return [0]

def table_remove(args):
    table = args[0]
    pos = args[1] if len(args) > 1 else len(table)
    del table[pos]
    return []

def table_sort(args):
    raise AssertionError('table.sort NYI')

table = LuaTable(hash={
    'concat': table_concat,
    'insert': table_insert,
    'maxn': table_maxn,
    'remove': table_remove,
    'sort': table_sort,
    })

# math library

def math_abs(args):
    x = args[0]
    return [abs(x)]

def math_acos(args):
    x = args[0]
    return [math.acos(x)]

def math_asin(args):
    x = args[0]
    return [math.asin(x)]

def math_atan(args):
    x = args[0]
    return [math.atan(x)]

def math_atan2(y, x):
    x, y = args[0], args[1]
    return [math.atan2(y, x)]

def math_ceil(args):
    x = args[0]
    return [math.ceil(x)]

def math_cos(args):
    x = args[0]
    return [math.cos(x)]

def math_cosh(args):
    x = args[0]
    return [math.cosh(x)]

def math_deg(args):
    x = args[0]
    return [math.degrees(x)]

def math_exp(args):
    x = args[0]
    return [math.exp(x)]

def math_floor(args):
    x = args[0]
    return [math.floor(x)]

def math_fmod(args):
    x, y = args[0], args[1]
    return [math.fmod(x, y)]

def math_frexp(args):
    x = args[0]
    return [math.frexp(x)]

math_huge = (1 << 32) - 1

def math_ldexp(args):
    m, e = args[0], args[1]
    return [math.ldexp(m, e)]

def math_log(args):
    x = args[0]
    return [math.log(x)]

def math_log10(args):
    x = args[0]
    return [math.log10(x)]

def math_max(args):
    assert len(args) > 0
    return [max(args)]

def math_min(args):
    assert len(args) > 0
    return [min(args)]

def math_modf(args):
    x = args[0]
    return [math.modf(x)]

math_pi = math.pi

def math_pow(args):
    x, y = args[0], args[1]
    return [math.pow(x, y)]

def math_rad(args):
    x = args[0]
    return [math.radians(x)]

def math_random(args):
    argc = len(args)
    if argc == 0:
        return [random.random()]
    if argc == 1:
        m = int(args[0])
        return [random.randint(1, m)]
    m, n = int(args[0]), int(args[1])
    return [random.randint(m, n)]

def math_randomseed(args):
    x = args[0]
    random.seed(x)

def math_sin(args):
    x = args[0]
    return [math.sin(x)]

def math_sinh(args):
    x = args[0]
    return [math.sinh(x)]

def math_sqrt(args):
    x = args[0]
    return [math.sqrt(x)]

def math_tan(args):
    x = args[0]
    return [math.tan(x)]

def math_tanh(args):
    x = args[0]
    return [math.tanh(x)]

math = LuaTable(hash={
    'abs': math_abs,
    'acos': math_acos,
    'asin': math_asin,
    'atan': math_atan,
    'atan2': math_atan2,
    'ceil': math_ceil,
    'cos': math_cos,
    'cosh': math_cosh,
    'deg': math_deg,
    'exp': math_exp,
    'floor': math_floor,
    'fmod': math_fmod,
    'frexp': math_frexp,
    'huge': math_huge,
    'ldexp': math_ldexp,
    'log': math_log,
    'log10': math_log10,
    'max': math_max,
    'min': math_min,
    'modf': math_modf,
    'pi': math_pi,
    'pow': math_pow,
    'rad': math_rad,
    'random': math_random,
    'randomseed': math_randomseed,
    'sin': math_sin,
    'sinh': math_sinh,
    'sqrt': math_sqrt,
    'tan': math_tan,
    'tanh': math_tanh,
    })

# io library

__default_input = sys.stdin
__default_output = sys.stdout

def io_close(args):
    file = args[0] if len(args) > 1 else __default_output
    if file != __default_output:
        file.close()
    return [None]

def io_flush(args):
    __default_output.flush()
    return [None]

def io_input(args):
    file = arg[0] if len(args) > 1 else None
    global __default_input
    if file:
        if isinstance(file, str):            
            __default_input = open(file)
        else:
            __default_input = file
    return [__default_input]

def io_lines(args):
    filename = args[0] if len(args) > 1 else __default_input
    f = open(filename)
    def read_line():
        try:
            line = f.next()
            return [line]
        except StopIteration:
            f.close()
            return [None]
    return [read_line]

def io_open(args):
    filename = args[0]
    mode = args[1] if len(args) > 1 else ''
    return [open(filename, mode)]

def io_output(args):
    file = args[0] if len(args) > 0 else None
    global __default_output
    if file:
        if isinstance(file, str):
            # filename, open it
            __default_output = open(file)
        else:
            # otherwise file is a file handle
            __default_output = file
    return [__default_output]

def io_popen(args):
    raise AssertionError('io.popen NYI')

def io_read(args):
    return file_read([__default_input] + args)

def io_tmpfile(args):
    return [os.tmpfile()]

def io_type(args):
    obj = args[0]
    if isinstance(obj, file):
        if obj.closed:
            return ['closed file']
        return ['file']
    return [None]

def io_write(args):
    return file_write([__default_output] + args)

io = LuaTable(hash={
    'close': io_close,
    'flush': io_flush,
    'input': io_input,
    'lines': io_lines,
    'open': io_open,
    'output': io_output,
    'popen': io_popen,
    'read': io_read,
    'tmpfile': io_tmpfile,
    'type': io_type,
    'write': io_write
    })

# file library

# The file functions are OO, so in lua all file functions implicitly
# take a file object i.e. "self" as a parameter.  Here the self
# parameter is explicitly bound as file_obj.

def file_close(args):
    file_obj = args[0]
    file_obj.close()
    return [None]

def file_flush(args):
    file_obj = args[0]
    file_obj.flush()
    return [None]

def file_lines(args):
    file_obj = args[0]
    def read_line():
        try:
            line = file_obj.next()
            return [line]
        except StopIteration:
            return [None]
    return [read_line]

def file_read(args):
    raise AssertionError('file.read NYI')

def file_seek(args):
    raise AssertionError('file.seek NYI')

def file_setvbuf(args):
    raise AssertionError('file.setvbuf NYI')

def file_write(args):
    file_obj = args[0]
    for arg in args[1:]:
        if isinstance(arg, (int, long, float)):
            if arg == int(arg):
                arg = str(int(arg))
            else:
                arg = str(arg)
        file_obj.write(arg)
    return [None]

file_ = LuaTable(hash={
    'close': file_close,
    'flush': file_flush,
    'lines': file_lines,
    'read': file_read,
    'seek': file_seek,
    'setvbuf': file_setvbuf,
    'write': file_write 
    })

# os library

def os_clock():
    print 'os.clock NYI'

def os_date(format=None, time=None):
    print 'os.date NYI'

def os_difftime(t2, t1):
    print 'os.difftime NYI'

def os_execute(command=None):
    print 'os.execute NYI'

def os_exit(code=None):
    print 'os.exit NYI'

def os_getenv(varname):
    print 'os.getenv NYI'

def os_remove(filename):
    print 'os.remove NYI'

def os_rename(oldname, newname):
    print 'os.rename NYI'

def os_setlocale(locale, category=None):
    print 'os.setlocale NYI'

def os_time(table=None):
    print 'os.time NYI'

def os_tmpname():
    print 'os.tmpname NYI'

os = LuaTable(hash={
    'clock': os_clock,
    'date': os_date,
    'difftime': os_difftime,
    'execute': os_execute,
    'exit': os_exit,
    'getenv': os_getenv,
    'remove': os_remove,
    'rename': os_rename,
    'setlocale': os_setlocale,
    'time': os_time,
    'tmpname': os_tmpname,
    })

# debug library

def debug_debug():
    print 'debug.debug NYI'

def debug_getfenv(o):
    print 'debug.getfenv NYI'

def debug_gethook(thread=None):
    print 'debug.gethook NYI'

def debug_getinfo(thread=None, function=None, what=None):
    print 'debug.getinfo NYI'

def debug_getlocal(thread=None, level=None, local=None):
    print 'debug.getlocal NYI'

def debug_getmetatable(object_):
    print 'debug.getmetatable NYI'

def debug_getregistry():
    print 'debug.getregistry NYI'

def debug_getupvalue(func, up):
    print 'debug.getupvalue NYI'

def debug_setfenv(object_, table):
    print 'debug.setfenv NYI'

def debug_sethook(thread=None, hook=None, mask=None, count=None):
    print 'debug.sethook NYI'

def debug_setlocal(thread=None, level=None, local=None, value=None):
    print 'debug.setlocal NYI'

def debug_setmetatable(object_, table):
    print 'debug.setmetatable NYI'

def debug_setupvalue(func, up, value):
    print 'debug.setupvalue NYI'

def debug_traceback(thread=None, message=None, level=None):
    print 'debug.traceback NYI'

debug = LuaTable(hash={
    'debug': debug_debug,
    'getfenv': debug_getfenv,
    'gethook': debug_gethook,
    'getinfo': debug_getinfo,
    'getlocal': debug_getlocal,
    'getmetatable': debug_getmetatable,
    'getregistry': debug_getregistry,
    'getupvalue': debug_getupvalue,
    'setfenv': debug_setfenv,
    'sethook': debug_sethook,
    'setlocal': debug_setlocal,
    'setmetatable': debug_setmetatable,
    'setupvalue': debug_setupvalue,
    'traceback': debug_traceback,
    })