-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflufflepuff.l
71 lines (71 loc) · 1.21 KB
/
flufflepuff.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
%{
#define SIZE 32767
#include <iostream>
#include <string>
#include <stack>
#include <vector>
using namespace std;
#define YY_DECL extern "C" int yylex()
char* filename;
string program;
stack<int> stk;
vector<char> array (SIZE);
int ptr=0;
%}
%option noyywrap
%%
"pf" { program+='+'; }
"bl" { program+='-'; }
"b" { program+='>'; }
"t" { program+='<'; }
"!" { program+='.'; }
"?" { program+=','; }
"*gasp*" { program+='['; }
"*pomf*" { program+=']'; }
. ;
\n ;
%%
main(int argc, char** argv) {
FILE* input;
if(argc > 1)
{
filename = argv[1];
input = fopen(filename, "r");
}
else
{
filename = "stdin";
input = stdin;
}
if(!input)
{
cout << argv[0] << ": " << filename << ": Cannot open file" << endl;
return -1;
}
yyin = input;
yylex();
for(int i=0; i<program.length(); i++)
{
char instr = program[i];
if(instr == '+') array[ptr]++;
if(instr == '-') array[ptr]--;
if(instr == '>')
{
ptr++;
if(ptr > SIZE) ptr=0;
}
if(instr == '<')
{
ptr--;
if(ptr < 0) ptr=SIZE;
}
if(instr == '.') putchar(array[ptr]);
if(instr == ',') array[ptr]=getchar();
if(instr == '[') stk.push(i);
if(instr == ']' && !stk.empty())
{
if(array[ptr]) i = stk.top()-1;
stk.pop();
}
}
}