aboutsummaryrefslogtreecommitdiff
path: root/support/in2data.py
blob: 5f497a9f7996909d125746f65e79fb243d531c0b (plain)
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
import sys

def pascal_to_basic(line):
    if len(line.strip()) == 0:
        return "", 0
        
    ret = ""
    bytes = 0
    commands = line.strip().split("/")
    
    for c in commands:
        c = c.strip()
        if c.startswith("$"):
            if c == commands[0]:
                ret = "DATA "
            else:
                ret = ret + ", "
                
            cnum = c[1:]
            ret = ret + "&H" + cnum
            bytes = bytes + 1
        
        elif c.startswith("{"):
            if c != commands[0]:
                ret = ret + " : "
            ret = ret + "REM " + c.strip()[1:-1]
            
    return ret, bytes
        

def inline_to_basic(input_file, output_file, line_start):
    total_bytes = 0
    line_number = line_start
    for line in input_file:
        line = line.strip()
        
        basline, line_bytes = pascal_to_basic(line)
        if basline is not None and len(basline) > 0:
            output_file.write("{0} {1}\r\n".format(line_number, basline))
            total_bytes = total_bytes + line_bytes
            line_number = line_number + 1
            
    output_file.write("{0} REM Total Bytes in Data: {1}\r\n".format(line_number, total_bytes))
    
if __name__ == "__main__":
    fpin = open(sys.argv[1], "r")
    fpout = open(sys.argv[2], "w")
    
    inline_to_basic(fpin, fpout, int(sys.argv[3]))
    
    fpin.close()
    fpout.close()