Skip to content

Commit 9cd77df

Browse files
committed
comments now generate in C Files
1 parent c551d28 commit 9cd77df

File tree

6 files changed

+80
-14
lines changed

6 files changed

+80
-14
lines changed
7.06 KB
Binary file not shown.

bscript.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import argparse
2+
from compiler import BScriptCompiler
3+
4+
def main():
5+
parser = argparse.ArgumentParser(description="BScript Compiler CLI")
6+
parser.add_argument("file", help="BScript source file (.bs)")
7+
parser.add_argument("--out", "-o", help="Output C file (optional)")
8+
args = parser.parse_args()
9+
10+
with open(args.file, "r") as f:
11+
code = f.read()
12+
13+
compiler = BScriptCompiler()
14+
c_code = compiler.compile(code)
15+
16+
out_file = args.out if args.out else args.file.rsplit(".", 1)[0] + ".c"
17+
with open(out_file, "w") as f:
18+
f.write(c_code)
19+
20+
print(f"C code written to {out_file}")
21+
22+
if __name__ == "__main__":
23+
main()

main.py renamed to compiler.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def indent(self):
1414
def compile(self, code: str) -> str:
1515
lines = self.preprocess(code)
1616
self.c_lines = [
17+
'// Generated C code from BScript',
1718
'#include <stdio.h>',
1819
'#include <string.h>',
1920
'',
@@ -26,10 +27,17 @@ def compile(self, code: str) -> str:
2627
i = 0
2728
while i < len(lines):
2829
line = lines[i].strip()
29-
if not line or line.startswith('//'):
30+
if line == "":
3031
i += 1
3132
continue
3233

34+
# Comment detection:
35+
if re.match(r'\s*//', line):
36+
self.c_lines.append(self.indent() + line.lstrip())
37+
i += 1
38+
continue
39+
40+
3341
# var declaration
3442
m = re.match(r'var\s+([a-zA-Z_]\w*);', line)
3543
if m:
@@ -139,18 +147,7 @@ def preprocess(self, code: str):
139147
clean_lines = []
140148
for line in raw_lines:
141149
line = line.strip()
142-
if not line or line.startswith('//'):
150+
if not line:
143151
continue
144152
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)
153+
return clean_lines

script.bs

Whitespace-only changes.

tests/script.bs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// GLOBAL STRING TEST
2+
3+
str "Hello, World!"
4+
5+
var a;
6+
print str;
7+
8+
// LOOP TEST
9+
10+
var a;
11+
var b;
12+
13+
b +;
14+
b +;
15+
b +;
16+
b +;
17+
18+
(a, b) {
19+
print a;
20+
a +;
21+
}
22+
23+
// COMMENTS SHOULD BE GENERATING INTO THE C FILE.

tests/script.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Generated C code from BScript
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
int main() {
6+
char str[256] = "";
7+
// GLOBAL STRING TEST
8+
strcpy(str, "Hello, World!");
9+
unsigned char a = 0;
10+
printf("%s", str);
11+
// LOOP TEST
12+
unsigned char b = 0;
13+
b = (b + 1) % 256;
14+
b = (b + 1) % 256;
15+
b = (b + 1) % 256;
16+
b = (b + 1) % 256;
17+
while (a != b) {
18+
printf("%c", a);
19+
a = (a + 1) % 256;
20+
}
21+
// COMMENTS SHOULD BE GENERATING INTO THE C FILE.
22+
return 0;
23+
}

0 commit comments

Comments
 (0)