Skip to content

Commit 68bf1e5

Browse files
author
robertDurst
committed
include std lib directly within the transpiled code output
1 parent cd0cabd commit 68bf1e5

File tree

3 files changed

+184
-8
lines changed

3 files changed

+184
-8
lines changed

out.c

+168-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,183 @@
22
* Do not alter! This code is generated by the sailfishc
33
* compiler and might/will break if anything changed.
44
*/
5-
#include "stdlib/stdlib.h"
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
// -------- STDLIB_START ------- //
10+
void
11+
print_bool(int i)
12+
{
13+
if (i == 1)
14+
printf("%s", "true");
15+
else
16+
printf("%s", "false")
17+
;
18+
}
19+
void
20+
print_flt(float f)
21+
{
22+
printf("%f", f);
23+
};
24+
void
25+
print_int(int i)
26+
{
27+
printf("%d", i);
28+
};
29+
void
30+
print_str(char* s)
31+
{
32+
printf("%s", s);
33+
};
34+
// -------- STDLIB_END ------- //
35+
36+
typedef struct _Node_ {
37+
struct _Node_* next;
38+
int data;
39+
} Node;
40+
41+
Node*
42+
construct_Node(Node* next,int data)
43+
{
44+
Node* a____struct___generated = (Node*)malloc(sizeof(Node));
45+
a____struct___generated->next = next; a____struct___generated->data = data;
46+
return a____struct___generated;
47+
}
648

749
int
8-
main()
50+
has_next(Node* own)
951
{
10-
if(1==0)
52+
int ret = (own->next!=NULL);
53+
return ret;
54+
}
55+
void
56+
set_next(Node* own, Node* node)
57+
{
58+
own->next = node;
59+
}
60+
Node*
61+
next(Node* own)
62+
{
63+
return own->next;
64+
}
65+
int
66+
data(Node* own)
67+
{
68+
return own->data;
69+
}
70+
typedef struct _Stack_ {
71+
struct _Node_* head;
72+
int size;
73+
} Stack;
74+
75+
Stack*
76+
construct_Stack(Node* head,int size)
77+
{
78+
Stack* a____struct___generated = (Stack*)malloc(sizeof(Stack));
79+
a____struct___generated->head = head; a____struct___generated->size = size;
80+
return a____struct___generated;
81+
}
82+
83+
int
84+
is_empty(Stack* own)
85+
{
86+
return (own->size==0);
87+
}
88+
int
89+
size(Stack* own)
90+
{
91+
return own->size;
92+
}
93+
int
94+
peek(Stack* own)
95+
{
96+
int i = 0;
97+
if(is_empty(own)==1)
98+
{
99+
100+
}
101+
else
102+
{
103+
i = data(own->head);
104+
}
105+
106+
return i;
107+
}
108+
void
109+
push(Stack* own, Node* node)
110+
{
111+
set_next(node, own->head);
112+
own->head = node;
113+
own->size = own->size+1;
114+
}
115+
void
116+
pop(Stack* own)
117+
{
118+
if(is_empty(own)==1)
11119
{
12120

121+
}
122+
else
123+
{
124+
own->head = next(own->head);
125+
own->size = own->size-1;
126+
}
127+
128+
}
129+
void
130+
print_(Stack* own, Node* node)
131+
{
132+
if(node!=NULL)
133+
{
134+
print_int(data(node));
135+
print_str(" ");
136+
print_(own, next(node));
13137
}
14138
else
15139
{
16140

17141
}
18142

19143
}
144+
void
145+
print(Stack* own)
146+
{
147+
print_str("Stack contents: ");
148+
print_(own, own->head);
149+
print_str("\n");
150+
}
151+
int
152+
main()
153+
{
154+
Node* a = construct_Node(NULL,10);
155+
Node* b = construct_Node(NULL,20);
156+
Node* c = construct_Node(NULL,30);
157+
Node* d = construct_Node(NULL,40);
158+
Node* e = construct_Node(NULL,50);
159+
Stack* s = construct_Stack(NULL,0);
160+
push(s, a);
161+
push(s, b);
162+
push(s, c);
163+
print(s);
164+
print_str("Size: ");
165+
print_int(size(s));
166+
print_str("\tTop: ");
167+
print_int(peek(s));
168+
print_str("\n");
169+
pop(s);
170+
pop(s);
171+
pop(s);
172+
pop(s);
173+
pop(s);
174+
print_str("Size: ");
175+
print_int(size(s));
176+
print_str("\tTop: ");
177+
print_int(peek(s));
178+
print_str("\n");
179+
push(s, d);
180+
push(s, e);
181+
push(s, c);
182+
push(s, b);
183+
print(s);
184+
}

src/main/CommandLine.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,4 @@ handleCommandLine(int argc, char* const* argv)
194194
"Try again or type sailfishc to see command options!\n";
195195
return 0;
196196
}
197-
}
197+
}

src/transpiler/Transpiler.h

+15-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,19 @@
1212

1313
const std::string OUTPUT_HEADER =
1414
"/**\n * Do not alter! This code is generated by the sailfishc\n * "
15-
"compiler and might/will break if anything changed.\n */\n#include "
16-
"\"stdlib/stdlib.h\"\n\n";
15+
"compiler and might/will break if anything changed.\n */\n\n";
16+
17+
const std::string STDLIB =
18+
"#include <stdio.h>\n#include <stdlib.h>\n\n// -------- STDLIB_START "
19+
" ------- "
20+
"//\nvoid\n"
21+
"print_bool(int i)\n"
22+
"{\nif (i == 1)\nprintf(\"%s\", \"true\");"
23+
"\nelse\nprintf(\"%s\", \"false\")\n;\n}"
24+
"\nvoid\nprint_flt(float f)\n{\nprintf(\"%f\", f);\n};"
25+
"\nvoid\nprint_int(int i)\n{\nprintf(\"%d\", i);\n};"
26+
"\nvoid\nprint_str(char* s)\n{\nprintf(\"%s\", s);\n};"
27+
"\n// -------- STDLIB_END ------- //\n\n";
1728

1829
class Transpiler : public Visitor
1930
{
@@ -36,7 +47,7 @@ class Transpiler : public Visitor
3647
symbolTable = s;
3748

3849
// set the file header here
39-
fileBuffer = OUTPUT_HEADER;
50+
fileBuffer = OUTPUT_HEADER + STDLIB;
4051
}
4152

4253
// destructor
@@ -109,4 +120,4 @@ class Transpiler : public Visitor
109120
void visit(ast::AttributeAccess*);
110121
void visit(ast::AttributeMethodAccess*);
111122
void visit(ast::MethodAccess*);
112-
};
123+
};

0 commit comments

Comments
 (0)