Skip to content

Commit b5e09fe

Browse files
author
robertDurst
committed
add a few more options to cli, including ability to compile c and execute binary
1 parent 1b0fd68 commit b5e09fe

File tree

4 files changed

+131
-50
lines changed

4 files changed

+131
-50
lines changed

examples/stack.fish

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Cfn Node {
3131
<- void
3232
-> int
3333
{
34-
return own.data
34+
return own.data
3535
}
3636
}
3737

@@ -52,75 +52,81 @@ Cfn Stack {
5252
<- void
5353
-> int
5454
{
55-
return own.size
55+
return own.size
5656
}
5757

5858
fun peek
5959
<- void
6060
-> int
6161
{
62-
// super hacky way of accessing methods on attributes
63-
dec Node temp = new Node { next: void, data: 0 }
64-
temp = own.head
65-
return temp.data
62+
// since return is at end, must declare a return value here
63+
dec int i = 0
64+
if | own...is_empty() == true | {
65+
// do nothing
66+
} else {
67+
// super hacky way of accessing methods on attributes
68+
dec Node temp = new Node { next: void, data: 0 }
69+
temp = own.head
70+
i = temp...data()
71+
}
72+
73+
return i
6674
}
6775

6876

6977
fun push
7078
<- Node node
7179
-> void
7280
{
73-
if | is_empty == false | { own.head = node }
74-
else {
75-
node...set_next(own.head)
76-
own.head = node
77-
}
78-
79-
own.size = own.size + 1
81+
node...set_next(own.head)
82+
own.head = node
83+
own.size = own.size + 1
8084
}
8185

8286
fun pop
8387
<- void
8488
-> void
8589
{
86-
if | is_empty == true | { }
87-
else {
88-
dec Node temp = new Node { next: void, data: 0 }
89-
temp = own.head
90-
own.head = temp...next()
91-
92-
own.size = own.size - 1
93-
}
90+
if | own...is_empty() == true | { }
91+
else {
92+
dec Node temp = new Node { next: void, data: 0 }
93+
temp = own.head
94+
own.head = temp...next()
95+
96+
own.size = own.size - 1
97+
}
9498
}
9599

96100
fun print_
97101
<- Node node
98102
-> void
99103
{
100104
if | node!= void| {
101-
display_int(node...data())
102-
display_str(" ")
103-
own...print_(node...next())
104-
}
105+
display_int(node...data())
106+
display_str(" ")
107+
own...print_(node...next())
108+
}
105109

106-
else {}
110+
else {}
107111
}
108112

109113
fun print
110114
<- void
111115
-> void
112116
{
113117
display_str("Stack contents: ")
114-
own...print_(own.head)
115-
display_str("\n")
118+
own...print_(own.head)
119+
display_str("\n")
116120
}
117121
}
118122

119123

120124
start {
121-
dec Node a = new Node { next: void, data: 1 }
122-
dec Node b = new Node { next: void, data: 2 }
123-
dec Node c = new Node { next: void, data: 3 }
125+
dec Node a = new Node { next: void, data: 10 }
126+
dec Node b = new Node { next: void, data: 20 }
127+
dec Node c = new Node { next: void, data: 30 }
128+
dec Node d = new Node { next: void, data: 40 }
129+
dec Node e = new Node { next: void, data: 50 }
124130

125131
dec Stack s = new Stack { head: void, size: 0 }
126132

@@ -136,6 +142,9 @@ start {
136142
display_int(s...peek())
137143
display_str("\n")
138144

145+
s...pop()
146+
s...pop()
147+
s...pop()
139148
s...pop()
140149
s...pop()
141150

@@ -144,4 +153,11 @@ start {
144153
display_str("\tTop: ")
145154
display_int(s...peek())
146155
display_str("\n")
156+
157+
s...push(d)
158+
s...push(e)
159+
s...push(c)
160+
s...push(b)
161+
162+
s...print()
147163
}

out.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,32 @@ size(Stack* own)
6363
}
6464
int
6565
peek(Stack* own)
66+
{
67+
int i = 0;
68+
if(is_empty(own)==1)
69+
{
70+
71+
}
72+
else
6673
{
6774
Node*temp = construct_Node(NULL,0);
6875
temp = own->head;
69-
return temp->data;
76+
i = data(temp);
77+
}
78+
79+
return i;
7080
}
7181
void
7282
push(Stack* own, Node* node)
73-
{
74-
if(is_empty==0)
75-
{
76-
own->head = node;
77-
}
78-
else
7983
{
8084
set_next(node, own->head);
8185
own->head = node;
82-
}
83-
8486
own->size = own->size+1;
8587
}
8688
void
8789
pop(Stack* own)
8890
{
89-
if(is_empty==1)
91+
if(is_empty(own)==1)
9092
{
9193

9294
}
@@ -124,24 +126,34 @@ print(Stack* own)
124126
int
125127
main()
126128
{
127-
Node*a = construct_Node(NULL,1);
128-
Node*b = construct_Node(NULL,2);
129-
Node*c = construct_Node(NULL,3);
129+
Node*a = construct_Node(NULL,10);
130+
Node*b = construct_Node(NULL,20);
131+
Node*c = construct_Node(NULL,30);
132+
Node*d = construct_Node(NULL,40);
133+
Node*e = construct_Node(NULL,50);
130134
Stack*s = construct_Stack(NULL,0);
131135
push(s, a);
132136
push(s, b);
133137
push(s, c);
134138
print(s);
135139
print_str("Size: ");
136140
print_int(size(s));
137-
print_str("\tTop:");
141+
print_str("\tTop: ");
138142
print_int(peek(s));
139143
print_str("\n");
140144
pop(s);
141145
pop(s);
146+
pop(s);
147+
pop(s);
148+
pop(s);
142149
print_str("Size: ");
143150
print_int(size(s));
144-
print_str("\tTop:");
151+
print_str("\tTop: ");
145152
print_int(peek(s));
146153
print_str("\n");
154+
push(s, d);
155+
push(s, e);
156+
push(s, c);
157+
push(s, b);
158+
print(s);
147159
}

src/main/CommandLine.cpp

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ helpMessage()
1414
"\n\tsailfishc --help\n"
1515
"\n\tsailfishc --version\n"
1616
"\n\tsailfishc --lex_only [filename]\n"
17-
"\n\tsailfishc --parse_only [filename]\n\n"
18-
"\n\tsailfishc --semantic_analysis_only [filename]\n\n";
17+
"\n\tsailfishc --parse_only [filename]\n"
18+
"\n\tsailfishc --semantic_analysis_only [filename]\n"
19+
"\n\tsailfishc --compile_c [filename]\n"
20+
"\n\tsailfishc --compile_and_execute [filename]\n\n";
1921
}
2022

2123
void
@@ -24,6 +26,32 @@ versionInfo()
2426
std::cout << "sailfishc 0.0.1\n";
2527
}
2628

29+
bool
30+
compileC()
31+
{
32+
// check to see if we can use system
33+
if (!system(NULL))
34+
{
35+
std::cout << "System command processor doesn't exist. Please compile "
36+
"sailfishc generated C code yourself.\n";
37+
return false;
38+
}
39+
40+
std::cout << "EXECUTING: gcc out.c\n";
41+
system("gcc out.c");
42+
43+
std::cout << "gcc compiled out.c to: a.out\n";
44+
45+
return true;
46+
}
47+
48+
void
49+
executeBinary()
50+
{
51+
std::cout << "EXECUTING: ./a.out.\n";
52+
system("./a.out");
53+
}
54+
2755
void
2856
fullCompilation(std::string filename)
2957
{
@@ -43,7 +71,7 @@ fullCompilation(std::string filename)
4371
s->getSymbolTable());
4472
t->transpile();
4573

46-
std::cout << "Success. Compiled to: out.c\n";
74+
std::cout << "Success. sailfishc compiled " + filename + " to: out.c\n";
4775
}
4876
catch (const std::string msg)
4977
{
@@ -127,12 +155,36 @@ handleCommandLine(int argc, char* const* argv)
127155
std::cerr << msg;
128156
}
129157
}
158+
else if (std::string("--compile_c").compare(argv[1]) == 0)
159+
{
160+
// compile sailfish
161+
fullCompilation(argv[2]);
162+
163+
// compile c
164+
compileC();
165+
}
166+
else if (std::string("--compile_and_execute").compare(argv[1]) == 0)
167+
{
168+
// compile sailfish
169+
fullCompilation(argv[2]);
170+
171+
// compile c
172+
if (compileC())
173+
// execute gcc generated binary
174+
executeBinary();
175+
}
176+
else
177+
{
178+
std::cout
179+
<< "Unrecognized flag. "
180+
"Try again or type sailfishc to see command options!\n";
181+
}
130182

131183
return 0;
132184

133185
default:
134186
std::cout << "Unexpected number of arguments. "
135-
"Try again or type sailfishc to see command opetions!\n";
187+
"Try again or type sailfishc to see command options!\n";
136188
return 0;
137189
}
138190
}

src/main/CommandLine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../semantics/SemanticAnalyzer.h"
1010
#include "../transpiler/Transpiler.h"
1111
#include <iostream>
12+
#include <stdlib.h>
1213
#include <string>
1314

1415
int handleCommandLine(int, char* const* argv);

0 commit comments

Comments
 (0)