Skip to content

Commit 33a4057

Browse files
committed
Initial commit
0 parents  commit 33a4057

File tree

9 files changed

+275
-0
lines changed

9 files changed

+275
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Brainy0789
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# BScript
2+
3+
BScript is a coding language that is terrible.
4+
5+
[Syntax](docs/SYNTAX.md)
6+
7+
All you get is simple variables that can only be added or subtracted to (only one), basic loops, really nothings else. It's probably easier than Brainfuck, though.

docs/SYNTAX.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# BScript Syntax
2+
3+
## Printing<br>
4+
````
5+
print "At least we have support for basic strings";
6+
````
7+
<br>Or a variable:<br>
8+
````
9+
print a;
10+
````
11+
<br>You can even use the single string! (More on that later.)<br>
12+
````
13+
print str
14+
````
15+
16+
## Creating Variables:<br>
17+
````
18+
var a;
19+
````
20+
<br>(always starts at 0)
21+
22+
## Adding/subtracting to variables
23+
24+
````
25+
a +; //Addition by 1
26+
b -; //Subtraction by 1
27+
````
28+
(Do note that if a variables goes above the number 256, it resets to 0)
29+
30+
## Loops
31+
````
32+
(a,b) {
33+
//loops until the first variable is equal to the second.
34+
}
35+
````
36+
37+
## The Single String:
38+
In BScript, you get access to a single string, `str`. You can set it using:
39+
````
40+
str "something"
41+
````
42+
43+
## Inputs
44+
45+
This simply sets a user input to the Single String
46+
47+
````
48+
input
49+
````
50+
51+
# Advanced
52+
53+
These are advanced functions for speicial use cases.
54+
55+
## Single String to ASCII
56+
57+
Sets the the ASCII value of the index of the string to a variable.
58+
59+
````
60+
ascii 0,a
61+
````
62+
(Do note the indexes start at 0)
63+
64+
## ASCII to Single String
65+
66+
Adds the ASCII value to the String.
67+
68+
````
69+
revascii a
70+
````
71+
72+
(Do note the strings do not reset when you call this. You may have to call `string ""` if you want this to work correctly.)

main.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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)

output

32.5 KB
Binary file not shown.

output.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
int main() {
5+
char str[256] = "";
6+
unsigned char a = 0;
7+
unsigned char b = 0;
8+
strcpy(str, "A");
9+
a = (unsigned char)str[0];
10+
printf("%s", str);
11+
a = (a + 1) % 256;
12+
printf("%c", a);
13+
while (b != a) {
14+
a = (a - 1 + 256) % 256;
15+
}
16+
return 0;
17+
}

script.bs

Whitespace-only changes.

0 commit comments

Comments
 (0)