fixed the spaceing t#34
This commit is contained in:
@ -4,6 +4,7 @@
|
|||||||
extern int yylex(void);
|
extern int yylex(void);
|
||||||
void yyerror(const char *err);
|
void yyerror(const char *err);
|
||||||
extern char* yytext;
|
extern char* yytext;
|
||||||
|
extern int yyleng;
|
||||||
extern int yychar;
|
extern int yychar;
|
||||||
extern SymbolTable * cur;
|
extern SymbolTable * cur;
|
||||||
//char* cur_value;
|
//char* cur_value;
|
||||||
@ -122,8 +123,8 @@ idlist:
|
|||||||
;
|
;
|
||||||
|
|
||||||
sblock:
|
sblock:
|
||||||
L_BRACE {cur = CreateScope(cur,2,2);} statement_list {cur = getParent(cur);} R_BRACE
|
L_BRACE {column_number += yyleng; cur = CreateScope(cur,line_number,column_number);} statement_list {cur = getParent(cur);} R_BRACE
|
||||||
| L_BRACE {cur = CreateScope(cur,2,2);} dblock statement_list {cur = getParent(cur);} R_BRACE
|
| L_BRACE {column_number += yyleng; cur = CreateScope(cur,line_number,column_number);} dblock statement_list {cur = getParent(cur);} R_BRACE
|
||||||
;
|
;
|
||||||
|
|
||||||
dblock:
|
dblock:
|
||||||
|
@ -7,9 +7,11 @@
|
|||||||
%{
|
%{
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "../tmp/grammar.tab.h"
|
#include "../tmp/grammar.tab.h"
|
||||||
|
#include "../src/symbol_table.h"
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
#define DEBUG 0
|
#define DEBUG 0
|
||||||
#endif
|
#endif
|
||||||
|
extern SymbolTable * cur;
|
||||||
|
|
||||||
int line_number = 1, column_number = 1;
|
int line_number = 1, column_number = 1;
|
||||||
%}
|
%}
|
||||||
|
294
src/runner.c
294
src/runner.c
@ -4,187 +4,187 @@
|
|||||||
#include "runner.h"
|
#include "runner.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
fprintf(stderr, INVALID);
|
fprintf(stderr, INVALID);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
else if (argc == 2) {
|
|
||||||
if (is_help(argv[1])) {
|
|
||||||
return 0;
|
|
||||||
} else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) {
|
|
||||||
no_flag = SET_FLAG;
|
|
||||||
alpha_file = fopen(argv[1], "r");
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, INVALID);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != 0) {
|
|
||||||
fprintf(stderr, INVALID);
|
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
for (int i = 1; i < argc - 1; i++) {
|
|
||||||
if (check_flag(argv[i], argv[argc - 1]) != 0) {
|
|
||||||
fprintf(stderr, "INVALID FLAG(S): Use -help to view valid inputs \n");
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
alpha_file = fopen(argv[argc - 1], "r");
|
else if (argc == 2) {
|
||||||
}
|
if (is_help(argv[1])) {
|
||||||
}
|
return 0;
|
||||||
return run(alpha_file);
|
} else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) {
|
||||||
|
no_flag = SET_FLAG;
|
||||||
|
alpha_file = fopen(argv[1], "r");
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, INVALID);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != 0) {
|
||||||
|
fprintf(stderr, INVALID);
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
for (int i = 1; i < argc - 1; i++) {
|
||||||
|
if (check_flag(argv[i], argv[argc - 1]) != 0) {
|
||||||
|
fprintf(stderr, "INVALID FLAG(S): Use -help to view valid inputs \n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
alpha_file = fopen(argv[argc - 1], "r");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return run(alpha_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
int check_flag(char *arg, char *alpha) {
|
int check_flag(char *arg, char *alpha) {
|
||||||
if (strcmp("-tok", arg) == 0) {
|
if (strcmp("-tok", arg) == 0) {
|
||||||
if (tok_flag == NULL) {
|
if (tok_flag == NULL) {
|
||||||
return new_file(arg, alpha);
|
return new_file(arg, alpha);
|
||||||
}
|
}
|
||||||
fprintf(stderr, "FLAGS REPEAT\n");
|
fprintf(stderr, "FLAGS REPEAT\n");
|
||||||
return -1;
|
return -1;
|
||||||
} else if (strcmp("-st", arg) == 0) {
|
} else if (strcmp("-st", arg) == 0) {
|
||||||
if (st_flag == NULL) {
|
if (st_flag == NULL) {
|
||||||
return new_file(arg, alpha);
|
return new_file(arg, alpha);
|
||||||
}
|
}
|
||||||
fprintf(stderr, "FLAGS REPEAT\n");
|
fprintf(stderr, "FLAGS REPEAT\n");
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "INVALID FLAG: Use -help for valid inputs\n");
|
fprintf(stderr, "INVALID FLAG: Use -help for valid inputs\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int run(FILE *alpha) {
|
int run(FILE *alpha) {
|
||||||
int token;
|
int token;
|
||||||
cur = CreateScope(NULL, 1, 1);
|
cur = CreateScope(NULL, 1, 1);
|
||||||
|
|
||||||
// If file is not found
|
// If file is not found
|
||||||
if (alpha == NULL) {
|
if (alpha == NULL) {
|
||||||
fprintf(stderr, "INPUT FILE NOT FOUND\n");
|
fprintf(stderr, "INPUT FILE NOT FOUND\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
yyin = alpha;
|
|
||||||
|
|
||||||
// TOK FLAG
|
|
||||||
if (tok_flag != NULL) {
|
|
||||||
while (0 != (token = yylex())) {
|
|
||||||
if (tok_flag != NULL) {
|
|
||||||
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number,
|
|
||||||
token, yytext);
|
|
||||||
}
|
|
||||||
if (token == COMMENT) {
|
|
||||||
for (int i = 0; i < yyleng; i++) {
|
|
||||||
if (yytext[i] == '\n') {
|
|
||||||
line_number++;
|
|
||||||
column_number = 0;
|
|
||||||
}
|
|
||||||
column_number++;
|
|
||||||
}
|
}
|
||||||
continue;
|
yyin = alpha;
|
||||||
}
|
|
||||||
if (token == 1999) {
|
|
||||||
printf(
|
|
||||||
"On line number %d and column number %d we have an invalid "
|
|
||||||
"character:%s\n",
|
|
||||||
line_number, column_number, yytext);
|
|
||||||
}
|
|
||||||
column_number += yyleng;
|
|
||||||
}
|
|
||||||
fclose(tok_flag);
|
|
||||||
|
|
||||||
if (yyin != NULL) {
|
// TOK FLAG
|
||||||
fclose(yyin);
|
if (tok_flag != NULL) {
|
||||||
}
|
while (0 != (token = yylex())) {
|
||||||
return 0;
|
if (tok_flag != NULL) {
|
||||||
}
|
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number,
|
||||||
|
token, yytext);
|
||||||
|
}
|
||||||
|
if (token == COMMENT) {
|
||||||
|
for (int i = 0; i < yyleng; i++) {
|
||||||
|
if (yytext[i] == '\n') {
|
||||||
|
line_number++;
|
||||||
|
column_number = 0;
|
||||||
|
}
|
||||||
|
column_number++;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (token == 1999) {
|
||||||
|
printf(
|
||||||
|
"On line number %d and column number %d we have an invalid "
|
||||||
|
"character:%s\n",
|
||||||
|
line_number, column_number, yytext);
|
||||||
|
}
|
||||||
|
column_number += yyleng;
|
||||||
|
}
|
||||||
|
fclose(tok_flag);
|
||||||
|
|
||||||
if (st_flag != NULL) {
|
if (yyin != NULL) {
|
||||||
// output symbol table, file pointer is
|
fclose(yyin);
|
||||||
// print_symbol_table(top,st_flag);
|
}
|
||||||
}
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
yyparse();
|
if (st_flag != NULL) {
|
||||||
FILE *f = fdopen(1, "w");
|
// output symbol table, file pointer is
|
||||||
print_symbol_table(getAncestor(cur), f);
|
// print_symbol_table(top,st_flag);
|
||||||
fclose(f);
|
}
|
||||||
|
|
||||||
if (yyin != NULL) {
|
yyparse();
|
||||||
fclose(yyin);
|
FILE *f = fdopen(1, "w");
|
||||||
}
|
print_symbol_table(getAncestor(cur), f);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
return 0;
|
if (yyin != NULL) {
|
||||||
|
fclose(yyin);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_help(char *input) {
|
bool is_help(char *input) {
|
||||||
if (strcmp(input, "-help") == 0) {
|
if (strcmp(input, "-help") == 0) {
|
||||||
printf("%s", HELP);
|
printf("%s", HELP);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int new_file(char *arg, char *alpha) {
|
int new_file(char *arg, char *alpha) {
|
||||||
int type_len;
|
int type_len;
|
||||||
const char *basename = alpha;
|
const char *basename = alpha;
|
||||||
const char *slash = strchr(alpha, '/');
|
const char *slash = strchr(alpha, '/');
|
||||||
|
|
||||||
while (slash != NULL) {
|
while (slash != NULL) {
|
||||||
basename = slash + 1;
|
basename = slash + 1;
|
||||||
slash = strchr(basename, '/');
|
slash = strchr(basename, '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
mkdir("./out", 0777);
|
mkdir("./out", 0777);
|
||||||
|
|
||||||
char *new_basename = calloc(strlen(basename) + 5, sizeof(char));
|
char *new_basename = calloc(strlen(basename) + 5, sizeof(char));
|
||||||
strcpy(new_basename, "./out/");
|
strcpy(new_basename, "./out/");
|
||||||
strcat(new_basename, basename);
|
strcat(new_basename, basename);
|
||||||
basename = new_basename;
|
basename = new_basename;
|
||||||
|
|
||||||
if (strcmp(arg, "-tok") == 0) {
|
if (strcmp(arg, "-tok") == 0) {
|
||||||
type_len = TOK_LEN;
|
type_len = TOK_LEN;
|
||||||
} else if (strcmp(arg, "-st") == 0) {
|
} else if (strcmp(arg, "-st") == 0) {
|
||||||
type_len = ST_LEN;
|
type_len = ST_LEN;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "INVALID FLAG: Use -help to view valid inputs\n");
|
fprintf(stderr, "INVALID FLAG: Use -help to view valid inputs\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculate lengths
|
// calculate lengths
|
||||||
int basename_len = strlen(basename);
|
int basename_len = strlen(basename);
|
||||||
char *file_name =
|
char *file_name =
|
||||||
calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char));
|
calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char));
|
||||||
|
|
||||||
// coy filename and add extension
|
// coy filename and add extension
|
||||||
strncpy(file_name, basename, basename_len - ALPHA_OFFSET);
|
strncpy(file_name, basename, basename_len - ALPHA_OFFSET);
|
||||||
strcat(file_name, ".");
|
strcat(file_name, ".");
|
||||||
strcat(file_name, arg + 1);
|
strcat(file_name, arg + 1);
|
||||||
|
|
||||||
if (strcmp(arg, "-tok") == 0) {
|
if (strcmp(arg, "-tok") == 0) {
|
||||||
tok_flag = fopen(file_name, "w");
|
tok_flag = fopen(file_name, "w");
|
||||||
} else if (strcmp(arg, "-st") == 0) {
|
} else if (strcmp(arg, "-st") == 0) {
|
||||||
st_flag = fopen(file_name, "w");
|
st_flag = fopen(file_name, "w");
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_alpha_file(char *alpha, int file_len) {
|
int is_alpha_file(char *alpha, int file_len) {
|
||||||
if (strcmp(".alpha", alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
|
if (strcmp(".alpha", alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
|
||||||
return -1; // not alpha file
|
return -1; // not alpha file
|
||||||
}
|
}
|
||||||
return 0; // is alpha file
|
return 0; // is alpha file
|
||||||
}
|
}
|
||||||
|
|
||||||
void enter_scope(int line, int column) {
|
void enter_scope(int line, int column) {
|
||||||
cur = CreateScope(cur, line, column);
|
cur = CreateScope(cur, line, column);
|
||||||
}
|
}
|
||||||
void exit_scope() {
|
void exit_scope() {
|
||||||
if (cur->Parent_Scope == NULL) {
|
if (cur->Parent_Scope == NULL) {
|
||||||
printf("Can't close top");
|
printf("Can't close top");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cur = cur->Parent_Scope;
|
cur = cur->Parent_Scope;
|
||||||
}
|
}
|
||||||
|
74
tests/sprint2/test/sp2_llnode.alpha
Normal file
74
tests/sprint2/test/sp2_llnode.alpha
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
type rec: [integer: x; integer: y]
|
||||||
|
type T1: integer -> integer
|
||||||
|
type T2: rec -> integer
|
||||||
|
|
||||||
|
type llnode: [address: prev; integer: val; address: next]
|
||||||
|
type list: integer -> llnode
|
||||||
|
|
||||||
|
function foo : T1
|
||||||
|
function bar1 : T2
|
||||||
|
function bar2 : T2
|
||||||
|
function make_list : list
|
||||||
|
make_list(a) := {
|
||||||
|
[integer:orig_a; address: ret; address: curr; address: temp]
|
||||||
|
if (a < 0 | a = 0) then {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
ret := reserve llnode;
|
||||||
|
ret.prev := null;
|
||||||
|
ret.next := null;
|
||||||
|
ret.val := a;
|
||||||
|
while (0 < a) {
|
||||||
|
temp := reserve llnode;
|
||||||
|
temp.prev := null;
|
||||||
|
temp.next := null;
|
||||||
|
temp.val := val;
|
||||||
|
if (a = orig_a) then {
|
||||||
|
ret.next := temp;
|
||||||
|
temp.prev := ret;
|
||||||
|
curr := temp;
|
||||||
|
} else {
|
||||||
|
curr.next := temp;
|
||||||
|
temp.prev := curr;
|
||||||
|
curr := temp;
|
||||||
|
}
|
||||||
|
a := a - 1;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
foo(x) := {
|
||||||
|
return x * x;
|
||||||
|
}
|
||||||
|
bar1(a) := {
|
||||||
|
return a.x * a.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
bar2 as (r,s) := {
|
||||||
|
if (r < s) then {
|
||||||
|
while (!(r < s)) {
|
||||||
|
r := r + 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
[integer: x]
|
||||||
|
x := 0;
|
||||||
|
while (x < 10) {
|
||||||
|
r := r + s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r * s;
|
||||||
|
}
|
||||||
|
entry(arg) := {
|
||||||
|
[ integer: result ; rec: w; llnode: li]
|
||||||
|
li := make_list(6);
|
||||||
|
result := foo(5);
|
||||||
|
w := reserve w;
|
||||||
|
w.x := 5;
|
||||||
|
w.y := 7;
|
||||||
|
result := bar1(w);
|
||||||
|
result := bar2(5,7);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user