|
| 1 | +import re |
| 2 | + |
| 3 | +class BScriptCompiler: |
| 4 | + def __init__(self): |
| 5 | + self.vars = set() |
| 6 | + self.c_lines = [] |
| 7 | + self.loop_stack = [] |
| 8 | + self.indent_level = 1 |
| 9 | + self.str_declared = False |
| 10 | + |
| 11 | + def indent(self): |
| 12 | + return " " * self.indent_level |
| 13 | + |
| 14 | + def compile(self, code: str) -> str: |
| 15 | + lines = self.preprocess(code) |
| 16 | + self.c_lines = [ |
| 17 | + '#include <stdio.h>', |
| 18 | + '#include <string.h>', |
| 19 | + '', |
| 20 | + 'int main() {' |
| 21 | + ] |
| 22 | + # Declare single string buffer |
| 23 | + self.c_lines.append(' char str[256] = "";') |
| 24 | + self.str_declared = True |
| 25 | + |
| 26 | + i = 0 |
| 27 | + while i < len(lines): |
| 28 | + line = lines[i].strip() |
| 29 | + if not line or line.startswith('//'): |
| 30 | + i += 1 |
| 31 | + continue |
| 32 | + |
| 33 | + # var declaration |
| 34 | + m = re.match(r'var\s+([a-zA-Z_]\w*);', line) |
| 35 | + if m: |
| 36 | + var = m.group(1) |
| 37 | + if var not in self.vars: |
| 38 | + self.vars.add(var) |
| 39 | + self.c_lines.append(f'{self.indent()}unsigned char {var} = 0;') |
| 40 | + i += 1 |
| 41 | + continue |
| 42 | + |
| 43 | + # increment/decrement |
| 44 | + m = re.match(r'([a-zA-Z_]\w*)\s*([+-]);', line) |
| 45 | + if m: |
| 46 | + var, op = m.groups() |
| 47 | + if var not in self.vars: |
| 48 | + raise Exception(f"Variable '{var}' used before declaration") |
| 49 | + if op == '+': |
| 50 | + self.c_lines.append(f'{self.indent()}{var} = ({var} + 1) % 256;') |
| 51 | + else: |
| 52 | + self.c_lines.append(f'{self.indent()}{var} = ({var} - 1 + 256) % 256;') |
| 53 | + i += 1 |
| 54 | + continue |
| 55 | + |
| 56 | + # print statements |
| 57 | + m = re.match(r'print\s+(.*);', line) |
| 58 | + if m: |
| 59 | + expr = m.group(1).strip() |
| 60 | + if expr == 'str': |
| 61 | + self.c_lines.append(f'{self.indent()}printf("%s", str);') |
| 62 | + elif expr in self.vars: |
| 63 | + self.c_lines.append(f'{self.indent()}printf("%c", {expr});') |
| 64 | + else: |
| 65 | + # assume string literal with quotes |
| 66 | + literal = expr.strip('"').strip("'") |
| 67 | + self.c_lines.append(f'{self.indent()}printf("{literal}");') |
| 68 | + i += 1 |
| 69 | + continue |
| 70 | + |
| 71 | + # str assignment |
| 72 | + m = re.match(r'str\s+"([^"]*)"', line) |
| 73 | + if m: |
| 74 | + content = m.group(1) |
| 75 | + self.c_lines.append(f'{self.indent()}strcpy(str, "{content}");') |
| 76 | + i += 1 |
| 77 | + continue |
| 78 | + |
| 79 | + # input (just fgets from stdin) |
| 80 | + if line == 'input': |
| 81 | + self.c_lines.append(f'{self.indent()}fgets(str, sizeof(str), stdin);') |
| 82 | + i += 1 |
| 83 | + continue |
| 84 | + |
| 85 | + # ascii index to var: ascii 0,a |
| 86 | + m = re.match(r'ascii\s+(\d+),\s*([a-zA-Z_]\w*)', line) |
| 87 | + if m: |
| 88 | + idx, var = m.groups() |
| 89 | + if var not in self.vars: |
| 90 | + raise Exception(f"Variable '{var}' used before declaration") |
| 91 | + self.c_lines.append(f'{self.indent()}{var} = (unsigned char)str[{idx}];') |
| 92 | + i += 1 |
| 93 | + continue |
| 94 | + |
| 95 | + # revascii var |
| 96 | + m = re.match(r'revascii\s+([a-zA-Z_]\w*)', line) |
| 97 | + if m: |
| 98 | + var = m.group(1) |
| 99 | + if var not in self.vars: |
| 100 | + raise Exception(f"Variable '{var}' used before declaration") |
| 101 | + # Append char to str |
| 102 | + # We'll do strcat with single char string |
| 103 | + self.c_lines.append(f'{self.indent()}char tmp[2] = {{ (char){var}, \'\\0\' }};') |
| 104 | + self.c_lines.append(f'{self.indent()}strcat(str, tmp);') |
| 105 | + i += 1 |
| 106 | + continue |
| 107 | + |
| 108 | + # loops (a,b) { ... } |
| 109 | + m = re.match(r'\((\w+),\s*(\w+)\)\s*{', line) |
| 110 | + if m: |
| 111 | + var1, var2 = m.groups() |
| 112 | + if var1 not in self.vars or var2 not in self.vars: |
| 113 | + raise Exception(f"Loop variables must be declared before use") |
| 114 | + self.c_lines.append(f'{self.indent()}while ({var1} != {var2}) {{') |
| 115 | + self.loop_stack.append('}') |
| 116 | + self.indent_level += 1 |
| 117 | + i += 1 |
| 118 | + continue |
| 119 | + |
| 120 | + if line == '}': |
| 121 | + if not self.loop_stack: |
| 122 | + raise Exception("Unexpected closing brace '}'") |
| 123 | + self.indent_level -= 1 |
| 124 | + self.c_lines.append(self.indent() + self.loop_stack.pop()) |
| 125 | + i += 1 |
| 126 | + continue |
| 127 | + |
| 128 | + raise Exception(f"Unknown or unsupported command: {line}") |
| 129 | + |
| 130 | + # Close main |
| 131 | + self.c_lines.append(' return 0;') |
| 132 | + self.c_lines.append('}') |
| 133 | + |
| 134 | + return '\n'.join(self.c_lines) |
| 135 | + |
| 136 | + def preprocess(self, code: str): |
| 137 | + # Very simple preprocessing: split by lines, remove empty lines and comments |
| 138 | + raw_lines = code.split('\n') |
| 139 | + clean_lines = [] |
| 140 | + for line in raw_lines: |
| 141 | + line = line.strip() |
| 142 | + if not line or line.startswith('//'): |
| 143 | + continue |
| 144 | + clean_lines.append(line) |
| 145 | + return clean_lines |
| 146 | + |
| 147 | + |
| 148 | +# Example usage: |
| 149 | + |
| 150 | +code = ''' |
| 151 | +print "Hello"; |
| 152 | +''' |
| 153 | + |
| 154 | +compiler = BScriptCompiler() |
| 155 | +c_code = compiler.compile(code) |
| 156 | +print(c_code) |
0 commit comments