-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.py
More file actions
186 lines (173 loc) · 5.12 KB
/
constants.py
File metadata and controls
186 lines (173 loc) · 5.12 KB
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
import sys
import os
import re
import time
import math
import random
import json
import threading
import concurrent.futures
from http.server import HTTPServer, BaseHTTPRequestHandler
from shell_lite.runtime import *
# Initialize Runtime Helpers
builtins_map = get_builtins()
globals().update(builtins_map)
class DotDict(dict):
def __getattr__(self, key):
try:
return self[key]
except KeyError:
raise AttributeError(key)
def __setattr__(self, key, value):
self[key] = value
STD_MODULES = get_std_modules()
# Wrap modules
for k, v in STD_MODULES.items():
if isinstance(v, dict): STD_MODULES[k] = DotDict(v)
# Async Executor
_executor = concurrent.futures.ThreadPoolExecutor()
# HTTP Server Support
GLOBAL_ROUTES = {}
GLOBAL_STATIC_ROUTES = {}
class ShellLiteHTTPHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.handle_req()
def do_POST(self):
self.handle_req()
def handle_req(self):
path = self.path
# Static Routes
for prefix, folder in GLOBAL_STATIC_ROUTES.items():
if path.startswith(prefix):
clean_path = path[len(prefix):]
if clean_path.startswith('/'):
clean_path = clean_path[1:]
if clean_path == '': clean_path = 'index.html'
file_path = os.path.join(folder, clean_path)
if os.path.exists(file_path) and \
os.path.isfile(file_path):
self.send_response(200)
# Simple mime type guessing
if file_path.endswith('.css'):
settings = 'text/css'
elif file_path.endswith('.js'):
settings = 'application/javascript'
elif file_path.endswith('.html'):
settings = 'text/html'
else:
settings = 'application/octet-stream'
self.send_header('Content-type', settings)
self.end_headers()
with open(file_path, 'rb') as f:
self.wfile.write(f.read())
return
handler = GLOBAL_ROUTES.get(path)
if handler:
try:
res = handler()
self.send_response(200)
self.end_headers()
if res: self.wfile.write(str(res).encode())
else: self.wfile.write(b'OK')
except Exception as e:
self.send_response(500)
self.wfile.write(str(e).encode())
else:
self.send_response(404)
self.wfile.write(b'Not Found')
# --- Web DSL Support ---
class Tag:
def __init__(self, name, attrs=None):
self.name = name
self.attrs = attrs or {}
self.children = []
def add(self, child):
self.children.append(child)
def __str__(self):
attr_str = ''.join([f' {k}="{v}"' for k,v in self.attrs.items()])
inner = ''.join([str(c) for c in self.children])
if self.name in ('img', 'br', 'hr', 'input', 'meta', 'link'):
return f'<{self.name}{attr_str} />'
return f'<{self.name}{attr_str}>{inner}</{self.name}>'
class WebBuilder:
def __init__(self): self.stack = []
def push(self, tag):
if self.stack: self.stack[-1].add(tag)
self.stack.append(tag)
def pop(self): return self.stack.pop() if self.stack else None
def add_text(self, text):
if self.stack: self.stack[-1].add(text)
else: pass # Top level text?
_web_builder = WebBuilder()
class BuilderContext:
def __init__(self, tag): self.tag = tag
def __enter__(self):
_web_builder.push(self.tag)
return self.tag
def __exit__(self, *args): _web_builder.pop()
def _make_tag_fn(name):
def fn(*args):
attrs = {}
content = []
for arg in args:
if isinstance(arg, dict):
attrs.update(arg)
elif isinstance(arg, str) and '=' in arg and ' ' not in arg:
k, v = arg.split('=', 1)
attrs[k] = v
else:
content.append(arg)
t = Tag(name, attrs)
for c in content:
t.add(c)
return t
return fn
for t in ['div', 'p', 'h1', 'h2', 'h3', 'h4', 'span', 'a',
'img', 'button', 'input', 'form', 'ul', 'li',
'html', 'head', 'body', 'title', 'meta', 'link',
'script', 'style', 'br', 'hr']:
globals()[t] = _make_tag_fn(t)
# --- User Script ---
EMPTY = 0
PAWN = 1
KNIGHT = 2
BISHOP = 3
ROOK = 4
QUEEN = 5
KING = 6
WHITE = 8
BLACK = 16
A8 = 0
B8 = 1
C8 = 2
D8 = 3
E8 = 4
F8 = 5
G8 = 6
H8 = 7
A1 = 56
B1 = 57
C1 = 58
D1 = 59
E1 = 60
F1 = 61
G1 = 62
H1 = 63
def get_piece_color(p):
_slang_ret = None
if (p == EMPTY):
return 0
if (p < BLACK):
return WHITE
else:
return BLACK
return _slang_ret
def get_piece_type(p):
_slang_ret = None
if (p == EMPTY):
return EMPTY
if (p < BLACK):
return (p - WHITE)
else:
return (p - BLACK)
return _slang_ret