repl.it
@sugarfi/

Photon

Python

An esoteric programming language.

fork
loading
Files
  • main.py
  • 99bottles.photon
  • branch.photon
  • fib.photon
  • goto.photon
  • hello.photon
  • help
  • loop.photon
  • square.photon
main.py
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
import re
from sys import exit
from os import listdir
import replit

replit.clear()

print('Photon v 1.0')
print('File Loader')
print()

f = listdir('.')
for file in f:
    if 'photon' in file.split('.'):
        print(file)
f = input('file or type help> ')
if f == 'help':
    f = open('help').read().split('\n')
    for i in range(0, 59, 30):
        replit.clear()
        for line in f[i:i+30]:
            print(line)
        input('Enter for more')
    exit(0)
if '.' not in f or f.split('.')[1] != 'photon':
    print('Invalid file')
    exit(1)
try:
    file = open(f).read()
except:
    print('Invalid file')
    exit(1)
line = re.compile(r'\[[#:].,[#:].>.\]')
lines = re.findall(line, file)

mem = {
    '$':0,
    '-':None,
}

def isnull(arg):
    return arg == None
def getab(args):
    if isnull(args[0]):
        a = mem.get(0, 0)
    else:
        a = args[0]
    if isnull(args[1]):
        b = mem.get(1, 0)
    else:
        b = args[1]
    try:
        a = int(a)
    except:
        pass
    try:
        b = int(b)
    except:
        pass
    return a, b
def vararg(arg, t):
    if t == ':' and arg not in ('$', '-'):
        return mem[arg]
    elif arg == '$':
        return mem[arg]
    else:
        return arg
def add(args, t):
    global mem
    a, b = getab(args) 
    a, b = vararg(a, t[0]), vararg(b, t[1])
    mem['$'] = a + b
def sub(args, t):
    global mem
    a, b = getab(args) 
    a, b = vararg(a, t[0]), vararg(b, t[1])
    mem['$'] = max(a - b, 0)
def mul(args, t):
    global mem
    a, b = getab(args) 
    a, b = vararg(a, t[0]), vararg(b, t[1])
    mem['$'] = a * b
def div(args, t):
    global mem
    a, b = getab(args) 
    a, b = vararg(a, t[0]), vararg(b, t[1])
    mem['$'] = a // b
def out(args, t):
    a = getab(args)[0]
    a = vararg(a, t[0])
    print(a, end='')
def clear(args, t):
    global mem
    mem = {'$':0, '-':None}
def ascii(args, t):
    a = getab(args)[0]
    a = vararg(a, t[0])
    print(chr(a), end='')
def new(args, t):
    print()
def var(args, t):
    global mem
    b = vararg(getab(args)[1], t[1])
    a = int(args[0])
    mem[a] = b
def get(args, t):
    global mem
    a = int(args[0])
    v = input('? ')[0]
    if v.isdigit():
        mem[a] = int(v) 
    else:
        mem[a] = ord(v)
def goto(args, t):
    global l
    a = vararg(getab(args)[0], t[0])
    l = a - 1
def branch(args, t):
    global l
    a, b = getab(args) 
    a, b = vararg(a, t[0]), vararg(b, t[1])
    if a != 0:
        l = b - 1    
funcs = {
    '+':add,
    '-':sub,
    '*':mul,
    '/':div,
    '_':out,
    '.':clear,
    '@':ascii,
    '~':new,
    '=':var,
    '?':get,
    '^':goto,
    '{':branch,
}

l = 0
while l < len(lines):
    line = lines[l]
    line = line.lstrip('[').rstrip(']')
    line = re.split('[,>]', line)
    args, func = line[:2], line[2]
    t = [args[0][0], args[1][0]]
    for i in range(len(args)):
        arg = args[i]
        if arg[0] == '#':
            arg = int(arg[1])
        elif arg[0] == ':':
            if arg[1] in ('$', '-'):
                t[i] = '#'
            arg = arg[1]
        args[i] = arg
    if func in funcs:
        funcs[func](args, t)
    l += 1
    
Fetching token
?