Skip to content

Commit b089d20

Browse files
committed
some window shit :o
1 parent 6614b78 commit b089d20

2 files changed

Lines changed: 80 additions & 8 deletions

File tree

2.28 KB
Binary file not shown.

compiler.py

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
import subprocess
3+
import shlex
34

45
class BScriptCompiler:
56
def __init__(self):
@@ -285,14 +286,17 @@ def indent():
285286

286287

287288
def transpile(self, code: str, lang: str = "c") -> str:
289+
windowed = False
288290
lines = self.preprocess(code)
289291
lang = lang.lower()
290292
if lang == "c":
291293
self.c_lines = [
292294
'// Generated C code from BScript',
295+
'',
293296
'#include <stdio.h>',
294297
'#include <string.h>',
295298
'char str[256] = "";',
299+
'#include <SDL.h>',
296300
''
297301
]
298302
self.global_vars.clear()
@@ -547,17 +551,66 @@ def transpile(self, code: str, lang: str = "c") -> str:
547551
i += 1
548552
continue
549553

554+
# Window
555+
if line == 'window;':
556+
if windowed:
557+
raise Exception("Window already created, cannot create another")
558+
continue
559+
560+
windowed = True
561+
sdl_code = [
562+
"int main(int argc, char* argv[]) {",
563+
" if (SDL_Init(SDL_INIT_VIDEO) != 0) {",
564+
" printf(\"SDL_Init Error: %s\\n\", SDL_GetError());",
565+
" return 1;",
566+
" }",
567+
" SDL_Window* window = SDL_CreateWindow(\"BScript Window\",",
568+
" SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,",
569+
" 640, 480, SDL_WINDOW_SHOWN);",
570+
" if (!window) {",
571+
" printf(\"SDL_CreateWindow Error: %s\\n\", SDL_GetError());",
572+
" SDL_Quit();",
573+
" return 1;",
574+
" }",
575+
576+
" SDL_Event e;",
577+
" int running = 1;",
578+
" while (running) {",
579+
" while (SDL_PollEvent(&e)) {",
580+
" if (e.type == SDL_QUIT) {",
581+
" running = 0;",
582+
" }",
583+
" }",
584+
" SDL_Delay(16);",
585+
" }",
586+
587+
" SDL_DestroyWindow(window);",
588+
" SDL_Quit();"
589+
]
590+
if self.in_function:
591+
self.c_lines.extend(f'{self.indent()}{stmt}' for stmt in sdl_code)
592+
else:
593+
self.main_code.extend(f'{self.indent()}{stmt}' for stmt in sdl_code)
594+
i += 1
595+
continue
596+
550597
raise Exception(f"Unknown or unsupported command: {line}")
551598

552599
# Compose full output
553600
# Globals first
554601
output = self.c_lines + self.global_var_decls
555602

556603
# Then main function
557-
output.append("int main() {")
558-
output.extend(self.main_code)
559-
output.append(" return 0;")
560-
output.append("}")
604+
605+
if not windowed:
606+
output.append("int main() {")
607+
output.extend(self.main_code)
608+
output.append(" return 0;")
609+
output.append("}")
610+
else:
611+
# If windowed, we already have the main function in sdl_code
612+
output.extend(self.main_code)
613+
output.append("}")
561614

562615
return '\n'.join(output)
563616
elif lang == "js":
@@ -571,11 +624,30 @@ def preprocess(self, code: str):
571624
def compile(self, source_file: str, output_name: str):
572625
if not output_name or not source_file:
573626
raise ValueError("Source file and output name must be provided")
574-
if not output_name.endswith('.exe') and not output_name.endswith('.out') and not output_name.endswith(''):
575-
# optional check, you can remove or adjust based on platform
576-
pass
627+
628+
# Read the source to check if it uses SDL
629+
with open(source_file, 'r', encoding='utf-8') as f:
630+
source_code = f.read()
631+
632+
# Determine if SDL is needed
633+
uses_sdl = 'SDL_CreateWindow' in source_code or 'window;' in source_code
634+
635+
# Set up compilation command
636+
compile_cmd = ["gcc", source_file, "-o", output_name]
637+
638+
if uses_sdl:
639+
try:
640+
# Get SDL compiler flags safely
641+
sdl_flags = subprocess.check_output(["sdl2-config", "--cflags", "--libs"])
642+
compile_cmd += shlex.split(sdl_flags.decode())
643+
except subprocess.CalledProcessError:
644+
raise RuntimeError("Failed to retrieve SDL2 compiler flags via sdl2-config")
645+
646+
# Run compilation
577647
try:
578-
subprocess.run(["gcc", source_file, "-o", output_name], check=True)
648+
subprocess.run(compile_cmd, check=True)
579649
except subprocess.CalledProcessError as e:
580650
raise RuntimeError(f"Compilation failed: {e}")
651+
581652
print(f"Compiled {source_file} to {output_name}")
653+

0 commit comments

Comments
 (0)