Skip to content

Commit 8aa31ec

Browse files
committed
some bug fixes
1 parent 2517768 commit 8aa31ec

5 files changed

Lines changed: 131 additions & 3 deletions

File tree

0 Bytes
Binary file not shown.

compiler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ def transpile(self, code: str, lang: str = "c") -> str:
292292
'// Generated C code from BScript',
293293
'#include <stdio.h>',
294294
'#include <string.h>',
295+
'char str[256] = "";',
295296
''
296297
]
297298
self.global_vars.clear()
@@ -554,7 +555,6 @@ def transpile(self, code: str, lang: str = "c") -> str:
554555

555556
# Then main function
556557
output.append("int main() {")
557-
output.append(" char str[256] = \"\";")
558558
output.extend(self.main_code)
559559
output.append(" return 0;")
560560
output.append("}")
@@ -574,7 +574,7 @@ def compile(self, output_name: str, source_file: str):
574574

575575
# Compile the C code using gcc
576576
try:
577-
subprocess.run(['gcc', '-o', output_name, source_file], check=True)
577+
subprocess.run(["gcc", source_file, "-o", output_name], check=True)
578578
except subprocess.CalledProcessError as e:
579579
raise RuntimeError(f"Compilation failed: {e}")
580-
print(f"Compiled {source_file} to {output_name}")
580+
print(f"Compiled {source_file} to {output_name}")

tests/something

32.5 KB
Binary file not shown.

tests/something.bs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
func add x, y {
2+
var w;
3+
4+
(w, y) {
5+
x +;
6+
w +;
7+
}
8+
9+
return x;
10+
}
11+
12+
func subtract x, y {
13+
var w;
14+
15+
(w, y) {
16+
x -;
17+
w +;
18+
}
19+
20+
return x;
21+
}
22+
23+
func toInt {
24+
var a;
25+
var b;
26+
var result;
27+
28+
ascii 0, a;
29+
ascii 1, b;
30+
31+
// Convert ASCII to digits
32+
33+
var FORTYEIGHT;
34+
35+
a = subtract a,FORTYEIGHT;
36+
b = subtract b,FORTYEIGHT;
37+
38+
// Multiply a by 10 (a * 10)
39+
a = add a,a; // *2
40+
a = add a,a; // *4
41+
a = add a,a; // *8
42+
var temp;
43+
a = add a,a; // now a * 16
44+
a = add a,a; // too much? reset
45+
46+
// Instead let's do:
47+
// a * 10 = (a * 8) + (a * 2)
48+
a = add a,a; // a*2
49+
a = add a,a;
50+
a = add a,a; // a*8
51+
a = add a,temp; // a*10
52+
53+
result = add a,b;
54+
return result;
55+
}
56+
57+
input;
58+
var i;
59+
i = toInt;
60+
input;
61+
var j;
62+
j = toInt;
63+
64+
var k;
65+
66+
k = add i,j;
67+
68+
print k;

tests/something.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Generated C code from BScript
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
char str[256] = "";
6+
7+
unsigned char add(unsigned char x, unsigned char y) {
8+
unsigned char w = 0;
9+
while (w != y) {
10+
x = (x + 1) % 256;
11+
w = (w + 1) % 256;
12+
}
13+
return x;
14+
}
15+
unsigned char subtract(unsigned char x, unsigned char y) {
16+
unsigned char w = 0;
17+
while (w != y) {
18+
x = (x - 1 + 256) % 256;
19+
w = (w + 1) % 256;
20+
}
21+
return x;
22+
}
23+
unsigned char toInt() {
24+
unsigned char a = 0;
25+
unsigned char b = 0;
26+
unsigned char result = 0;
27+
a = (unsigned char)str[0];
28+
b = (unsigned char)str[1];
29+
// Convert ASCII to digits
30+
unsigned char FORTYEIGHT = 0;
31+
a = subtract(a, FORTYEIGHT);
32+
b = subtract(b, FORTYEIGHT);
33+
// Multiply a by 10 (a * 10)
34+
a = add(a, a);
35+
a = add(a, a);
36+
a = add(a, a);
37+
unsigned char temp = 0;
38+
a = add(a, a);
39+
a = add(a, a);
40+
// Instead let's do:
41+
// a * 10 = (a * 8) + (a * 2)
42+
a = add(a, a);
43+
a = add(a, a);
44+
a = add(a, a);
45+
a = add(a, temp);
46+
result = add(a, b);
47+
return result;
48+
}
49+
unsigned char i = 0;
50+
unsigned char j = 0;
51+
unsigned char k = 0;
52+
int main() {
53+
fgets(str, sizeof(str), stdin);
54+
i = toInt;
55+
fgets(str, sizeof(str), stdin);
56+
j = toInt;
57+
k = add(i, j);
58+
printf("%d", k);
59+
return 0;
60+
}

0 commit comments

Comments
 (0)