본문 바로가기

IT/C

tiny BASIC interpreter C source - 다차원 배열 추가

 

배열추가 완성본.c

내가 완성하지 못한 명령어를 포함한채로 제출 하지는 않았을테고........

분명히 최종 완성본이 있을텐데 어디있는지 모르겠음

몇년전에 만들어둔걸 올리다보니...

잘 돌아갈수도 있고....안 돌아가면 말고

-----------------------------------------------------------------------------------

 

/* A tiny BASIC interpreter */
#include <stdio.h>
#include <setjmp.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

//디버깅 하지 않을때는 주석처리한다
#define DEBUG //original code 용
#define DEBUG2 //추가 코드 용
//#define DEBUG3 //추가 코드 - 추가 함수용

#define NUM_LAB 100
#define LAB_LEN 10
#define FOR_NEST 25
#define SUB_NEST 25
#define PROG_SIZE 10000

#define DELIMITER 1
#define VARIABLE 2
#define NUMBER 3
#define COMMAND 4
#define STRING 5
#define QUOTE 6
#define ARRAY 7//(dim추가구분)

//tok
#define PRINT 1
#define INPUT 2
#define IF 3
#define THEN 4
#define FOR 5
#define NEXT 6
#define TO 7
#define GOTO 8
#define EOL 9
#define FINISHED 10
#define GOSUB 11
#define RETURN 12
#define DIM 13//(dim추가구분)
#define CLEAR 14//(clear추가구분)
#define END 15

//단일변수
char *prog; //다음에 실행할 basic 문자를 지정
jmp_buf e_buf; //PASS

char token[80]; //공백을 제외한 단어의 개수
char token_type, tok;

char *gstack[SUB_NEST]; /* stack for gosub */

int ftos; /* index to top of FOR stack */
int gtos; /* index to top of GOSUB stack */

int variables[26]={ //basic 변수 이름 부분
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0
};


//구조체
struct label
{
char name[LAB_LEN];
char *p; /* points to place to go in source file */
};
struct label label_table[NUM_LAB];

struct for_stack
{
int var; /* counter variable */
int target; /* target value */
char *loc;
} fstack[FOR_NEST]; /* stack for FOR/NEXT loop */
struct for_stack fpop();

/* keyword lookup table */
struct commands
{
char command[20];
char tok;
} table[] = { /* Commands must be entered lowercase */
{"print",PRINT}, /* in this table */
{"input",INPUT},
{"if",IF},
{"then",THEN},
{"goto",GOTO},
{"for",FOR},
{"next",NEXT},
{"to",TO},
{"gosub",GOSUB},
{"return",RETURN},
{"end",END},
{"clear", CLEAR}, //(clear추가구분)
{"dim", DIM}, //(dim추가구분)
{"",END} /* mark end of table */
};

//추가 구조체
struct arr_str //(dim추가구분)
{
int num; //배열 선언시 범위 저장
int arr_value[100]; //배열의 값 저장
int guard; //중복 선언을 막아줌
} arr_table[26]; //variable과 마찬가지로 26개 선언

//funtion - 매개변수 표시
void print();
void scan_labels();
void find_eol();
void exec_goto();
void exec_if();
void exec_for();
void next();
void fpush(struct for_stack i);
void input();
void gosub();
void greturn();
void gpush(char *s);
void label_init();

//function - 선언추가 및 매개변수 표시
int load_program(char *p, char *fname);
char get_token(void);
void putback(void);
void assignment(void);
int find_var(char *s);
int get_next_label(char *s);
int iswhite(char c);
int isdelim(char c);
int look_up(char *s);
void get_exp(int *result);
void serror(int error);
char *find_label(char *s);
char *gpop();

//추가 함수
void clear(); //변수값 초기화 함수(clear추가구분)
void assignment_arr(void); //token_type : array시 연산 (dim추가구분)
void dim(); //다차원 배열 선언(dim추가구분)

void main(int argc,char *argv[])
{
//char in[80]; no using
//int answer; no using
//char *t; no using

char *p_buf;

if(argc!=2) //프롬프트 에서 잘못 입력시
{
printf("usage: P/Gname <filename>\n");
exit(1);
}

/* allocate memory for the program */
if(!(p_buf=(char *) malloc(PROG_SIZE))) //방의 크기가 10000이 넘을때
{
printf("allocation failure");
exit(1);
}

/* load the program to execute */
if(!load_program(p_buf,argv[1])) //.bas의 문자열을 저장
{
exit(1);
}

/* initialize the long jump buffer */
if(setjmp(e_buf)) //PASS
{
exit(1);
}

prog=p_buf;

/* find the labels in the program */
scan_labels(); // 248 : L

/* initialize the FOR stack index */
ftos = 0; //전역변수

/* initialize the GOSUB stack index */
gtos = 0; //전역변수

do
{

token_type=get_token();

/* check for assignment statement */
if(token_type == VARIABLE) //변수일때
{
/*return the var to the input stream */
putback(); //전역변수 prog를 초기화

/* must be assignment statement */
assignment(); //변수 형태 오류검사 ex) A = 10
}

else if(token_type == ARRAY) //(dim추가구분)
{
putback(); //전역변수 prog를 초기화

assignment_arr(); //변수 형태 오류검사 ex) A = 10

}

/* is command */
else //변수가 아닐때 즉 명령어 일때
{
switch(tok)
{
case PRINT:
print();
break;

case GOTO:
exec_goto();
break;

case IF:
exec_if();
break;

case FOR:
exec_for();
break;

case NEXT:
next();
break;

case INPUT:
input();
break;

case GOSUB:
gosub();
break;

case RETURN:
greturn();
break;

case CLEAR:
clear();
break;

case DIM: //(dim추가구분)
dim();
break;

case END:
exit(0);

} //switch end
} //else end
} while (tok != FINISHED); //while end

} //main end

/* Load a program. */
int load_program(char *p, char *fname)
{
FILE *fp;
int i = 0;

if(!(fp=fopen(fname,"r")))
{
return 0;
}

i=0;

do
{
*p = getc(fp);
p++; i++;
} while(!feof(fp) && i<PROG_SIZE);

/* null terminate the program */
*(p-1)='\0';

fclose(fp);

return 1;
}

/* Assign a variable a value. */
void assignment(void)
{
int var, value;

/* get the variable a value */
get_token();

if(!isalpha(*token))
{
serror(4);
return;
}

var = toupper(*token)-'A';

/* get the equals sign */
get_token();

if(*token!='=')
{
serror(3);
return;
}

/* get the value to assign to var */
get_exp(&value);

/* assign the value */
variables[var] = value;
}

void assignment_arr(void) //(dim 추가구분)
{
int var, value;
int arr_temp = 0; //배열주소 (dim추가구분)

#ifdef DEBUG2
printf("assignment_arr 함수 진입 성공 \n");
#endif

/* get the variable a value */
get_token(); //배열 이름 받음

#ifdef DEBUG2
printf("assignment_arr - token(1) : %s \n",token);
#endif

if(!isalpha(*token)) //알파벳이 판단 함수
{
serror(4);
return;
}

var = toupper(*token)-'A'; //대문자로 변환(내장함수)

get_token(); //'(' 받음

#ifdef DEBUG2
printf("assignment_arr - token(2) : %s \n",token);
#endif

if(*token!='(')
{
serror(14);
return;
}

get_token(); //숫자 받음

if(token_type == NUMBER)
{
arr_temp = atoi(token); //c문법대로라면 현행 베이직 문법대로라면 + 1을 해야함

if(arr_table[var].num < arr_temp) //선언한 배열값을 넘을경우
{
serror(18);
return;
}
}

else //만약 숫자가 아닐경우
{
#ifdef DEBUG2
printf("assignment_arr - serror(17) - token : %s \n",token);
#endif

serror(17);
return;
}

get_token();

#ifdef DEBUG2
printf("assignment_arr - token(3) : %s \n",token);
#endif

if(*token!=')')
{
serror(15);
return;
}

get_token();

if(*token!='=')
{
serror(3);
return;
}

get_exp(&value);

arr_table[var].arr_value[arr_temp] = value;

#ifdef DEBUG2
printf("assignment_arr - var : %d \n",var);
printf("assignment_arr - arr_table[%d].arr_value[%d] : %d \n",var, arr_temp,
arr_table[var].arr_value[arr_temp]);
#endif

}


/* Execute a simple version of the BASIC PRINT statement */
void print()
{
int answer;
int len=0,spaces;
char last_delim;

do
{
/* get next list item */
get_token();

if(tok==EOL || tok==FINISHED)
{
break;
}

/* is string */
if(token_type==QUOTE)
{
printf(token);
len += strlen(token);
get_token();
}

/* is expression */
else
{
putback();

#ifdef DEBUG
printf("PRINT 내부 get_exp 함수 진입 \n");
#endif

get_exp(&answer);

get_token();
len += printf("%d",answer);
}

last_delim = *token;

if(*token==';')
{
/* compute number of spaces to move to next tab */
spaces = 8 - (len % 8);

/* add in the tabbing position */
len += spaces;

while(spaces)
{
printf(" ");
spaces--;
}
}

/* do nothing */
else if(*token==',');

else if(tok!=EOL && tok!=FINISHED)
{
#ifdef DEBUG
printf("print(1) - serror(0); \n");
#endif

serror(0);
}

} while (*token==';' || *token==','); //do-while end

if(tok==EOL || tok==FINISHED)
{
if(last_delim != ';' && last_delim != ',')
{
printf("\n");
}
}

/* error is not,or; */
else
{
#ifdef DEBUG
printf("print(1) - serror(0); \n");
#endif

serror(0);
}


} //function end

/* Find all labels. */
void scan_labels()
{
int addr;
char *temp;

/* zero all labels */
label_init();

/* save pointer to top of program */
temp = prog;

/* if the first token in the file is a label */
get_token();

if(token_type==NUMBER)
{
strcpy(label_table[0].name,token);
label_table[0].p=prog;
}

find_eol();

do
{
get_token();
if(token_type==NUMBER)
{
addr = get_next_label(token);

if(addr== -1 || addr== -2)
{
(addr== -1) ?serror(5):serror(6);
}

strcpy(label_table[addr].name,token);

/* current point in program */
label_table[addr].p=prog;

} //if end

/* if not on a blank line,find next line */
if(tok!=EOL)
{
find_eol();
}

} while(tok!=FINISHED); //do-while end

/* restore to original */
prog = temp;

} //function end

/* Find the start of next line */
void find_eol()
{
while(*prog!='\n' && *prog!='\0')
{
++prog;
}

if(*prog)
{
prog++;
}

}

/*
Return index of next free position in label array.
A -1 is returned if the array is full.
A -2 is returned when duplicate label is found.
*/
int get_next_label(char *s)
{
register int t;

for(t=0; t<NUM_LAB; ++t)
{
if(label_table[t].name[0]==0)
{
return t;
}

/* dup */
if(!strcmp(label_table[t].name,s))
{
return -2;
}
}

return -1;
}

/* Find location of given label. A null is returned if
label is not found; otherwise a pointer to the position
of the label is returned */
char *find_label(char *s)
{
register int t;

for(t=0; t<NUM_LAB; ++t)
{
if(!strcmp(label_table[t].name,s))
{
return label_table[t].p;
}
}

/* error condition */
return '\0';
}

/* Execute a GOTO statement. */
void exec_goto()
{
char *loc;

/* get label to go to */
get_token();

/* find the location of the label */
loc = find_label(token);

if(loc=='\0')
{
/* label not defined */
serror(7);
}

/* start program running at that loc */
else
{
prog=loc;
}
}

/* Initialize the array that holds the labels.
By convention,a null label name indicates that */
void label_init()
{
register int t;

for(t=0; t<NUM_LAB; ++t)
{
label_table[t].name[0]='\0';
}
}

/* Execute an IF statement. */
void exec_if()
{
int x,y,cond;
char op;

/* get left expression */
get_exp(&x);

/* get the operator */
get_token();

if(!strchr("=<>",*token))
{
#ifdef DEBUG
printf("exec_if - serror(0); \n");
#endif

/* not a legal operator */
serror(0);
return;
}

op = *token;

/* get right expression */
get_exp(&y);

/* determine the outcome */
cond = 0;

switch(op)
{
case '<':
if(x<y)
{
cond=1;
}
break;

case '>':
if(x>y)
{
cond=1;
}
break;

case '=':
if(x==y)
{
cond=1;
}
break;
}

/* is true so process target of IF */
if(cond)
{
get_token();

/* else program execution starts on next line */
if(tok!=THEN)
{
serror(8);
return;
}
}

/* find start of next line */
else
{
find_eol();
}

} //function end

/* Execute a for loop */
void exec_for()
{
struct for_stack i;
int value;

/* read the control variable */
get_token();

if(!isalpha(*token))
{
serror(4);
return;
}

/* save its index */
i.var=toupper(*token)-'A';

/* read the equal sign */
get_token();

if(*token!='=')
{
serror(3);
return;
}

/* get initial value */
get_exp(&value);

variables[i.var]=value;

get_token();

/* read and discard the TO */
if(tok!=TO)
{
serror(9);
}

/* get target value */
get_exp(&i.target);

/* if loop can execute at least once,push info on stack */
if(value>=variables[i.var])
{
i.loc = prog;
fpush(i);
}

/* otherwise,skip loop code altogether */
else
{
while(tok!=NEXT)
{
get_token();
}
}

}

/* Execute a NEXT statement. */
void next()
{
struct for_stack i;

/* read the info */
i=fpop();

/* increment control variable */
variables[i.var]++;

if(variables[i.var]>i.target)
{
/* all don */
return;
}

/* otherwise, restore the info */
fpush(i);

/* loop */
prog = i.loc;
}

/* Push function for the FOR stack */
void fpush(struct for_stack i)
{
if(ftos>FOR_NEST)
{
serror(10);
}

fstack[ftos]=i;
ftos++;
}

struct for_stack fpop()
{
ftos--;

if(ftos<0)
{
serror(11);
}

return(fstack[ftos]);
}

/* Execute a simple from of the BASIC INPUT command */
void input()
{
//char str[80]; no using***********************
char var;
int i;

/* see if prompt string is present */
get_token();

if(token_type==QUOTE)
{
/* if so,print it and check for comma */
printf(token);
get_token();

if(*token!=',')
{
serror(1);
}

get_token();
}

else
{
/* otherwise,prompt with / */
printf("? ");
}

/* get the input var */
var = toupper(*token)-'A';

/*read input */
scanf("%d",&i);

/* store it */
variables[var] = i;
}

/* Execute a GOSUB command. */
void gosub()
{
char *loc;

get_token();

/* find the label to call */
loc = find_label(token);

if(loc=='\0')
{
/* label not defined */
serror(7);
}

else
{
/* save place to return to */
gpush(prog);
/* start program running at that loc */
prog = loc;
}
}

/* Return from GOSUB */
void greturn()
{
prog = gpop();
}

/* GOSUB stack push function. */
void gpush(char *s)
{
gtos++;

if(gtos==SUB_NEST)
{
serror(12);
return;
}

gstack[gtos]=s;
}

/* GOSUB stack pop function. */
char *gpop()
{
if(gtos==0)
{
serror(13);
return 0;
}

return(gstack[gtos--]);
}

/* recursive descent parser for integer expressions
which may include variables */
#define DELIMITER 1
#define VARIABLE 2
#define NUMBER 3
#define COMMAND 4
#define STRING 5
#define QUOTE 6

#define EOL 9
#define FINISHED 10

void level2(),level3(),level4(),level5();
void level6(),primitive(),arith(),unary();

/* Entry point into parser. */
void get_exp(int *result)
{
get_token();

#ifdef DEBUG
printf("get_exp - token : %s \n",token);
#endif

if(! *token)
{
serror(2);
return;
}

level2(result);
putback();
}

/* Add or subtract two terms. */
void level2(int *result)
{
register char op;
int hold;

level3(result);

while((op = *token) == '+' || op == '-')
{
get_token();

#ifdef DEBUG
printf("level2 - token : %s \n",token);
#endif

level3(&hold);
arith(op,result,&hold);
}
}

/* Multiply or divide two factors. */
void level3(int *result)
{
register char op;
int hold;

level4(result);
while((op = *token) == '*' || op == '/' || op == '%')
{
get_token();

#ifdef DEBUG
printf("level3 - token : %s \n",token);
#endif

level4(&hold);
arith(op,result,&hold);
}
}

/* Process integer exponent. */
void level4(int *result)
{
int hold;

level5(result);

if(*token == '^')
{
get_token();

#ifdef DEBUG
printf("level4 - token : %s \n",token);
#endif

level4(&hold);
arith('^',result,&hold);
}

}

/* Is a unary + or -. */
void level5(int *result)
{
register char op;

op = 0;

if((token_type==DELIMITER) && *token == '+' || *token == '-')
{
op = *token;
get_token();

#ifdef DEBUG
printf("level5 - token : %s \n",token);
#endif

}

level6(result);

if(op)
{
unary(op,result);
}
}

/* Process parenthesized expression. */
void level6(int *result)
{
if((*token == '(') && (token_type == DELIMITER))
{
get_token();

#ifdef DEBUG
printf("level6 - token(1) : %s \n",token);
#endif

level2(result);

if(*token != ')')
{
serror(1);
}

get_token();

#ifdef DEBUG
printf("level6 - token(2) : %s \n",token);
#endif

}

else
{
primitive(result);
}
}

/* Find value of number or variable. */
void primitive(int *result)
{
int v_t=0;
int a_t=0;

switch(token_type)
{
case VARIABLE:

#ifdef DEBUG
printf("primitive case VARIABLE 진입 \n");
#endif

*result = find_var(token);
get_token();

#ifdef DEBUG
printf("primitive - token(1) : %s \n",token);
#endif

return;

case NUMBER:
*result = atoi(token);
get_token();

#ifdef DEBUG
printf("primitive - token(2) : %s \n",token);
#endif

return;

case ARRAY:
v_t = toupper(*token)-'A';
get_token(); //"(" get

get_token(); //배열주소 get
a_t = atoi(token);

get_token(); //")" get

*result = arr_table[v_t].arr_value[a_t];

get_token();

return;

default:
#ifdef DEBUG
printf("primitive - serror(0) token : %s \n",token);
#endif

serror(0);

}
}

/* Perform the specified arithmetic. */
void arith(char o, int *r, int *h)
{
register int t, ex;

switch(o)
{
case '-':
*r = *r - *h;
break;

case '+':
*r = *r + *h;
break;

case '*':
*r = *r * *h;
break;

case '/':
*r = (*r) / (*h);
break;

case '%':
t = (*r) / (*h);
*r = *r - (t*(*h));
break;

case '^':
ex = *r;

if(*h == 0)
{
*r = 1;
break;
}

for(t = *h-1; t>0; --t)
{
*r= (*r) * ex;
}

break;
}
}

/* Reverce the sign. */
void unary(char o, int *r)
{
if(o == '-')
{
*r = -(*r);
}
}

/* Find the value of a variable */
int find_var(char *s)
{
if(!isalpha(*s))
{
#ifdef DEBUG
printf("find_var - serror(4) \n");
#endif

/* not a variable */
serror(4);
return 0;
}

return variables[toupper(*token) - 'A'];
}

/* Display an error message */
void serror(int error)
{
static char *e[] = {
"syntex error", //0
"unbalenced parentheses", //1
"no expression present", //2
"equal sign expected", //3
"not a variable", //4
"label table full", //5
"duplicate label", //6
"undefined label", //7
"THEN expected", //8
"TO expected", //9
"too many nested FOR loops", //10
"NEXT without FOR", //11
"too many nested GOSUBs", //12
"RETURN without GOSUB", //13
"'(' is a requisite", //14 (dim추가구분)
"')' is a requisite", //15 (dim추가구분)
"array limited 99", //16 (dim추가구분)
"not a number", //17 (dim추가구분)
"over array size", //18 (dim추가구분)
"declare already" //19 (dim추가구분)
};

printf("%s\n",e[error]);

/* return to save point */
longjmp(e_buf,1);
}

/* Get a token. */
char get_token(void)
{
register char *temp;

token_type=0; tok=0;
temp = token;

/* end of file */
if(*prog == '\0')
{
*token = 0;
tok = FINISHED;
return(token_type=DELIMITER);
}

/* skip over white space */
while(iswhite(*prog))
{
++prog;
}

/* crLf */
if(*prog=='\n')
{
++prog;
tok = EOL; *token='\n';
token[1]='\0';
return(token_type = DELIMITER);
}

/* delimiter */
if(strchr("+-*^/%=;(),><",*prog))
{
*temp = *prog;
/* advanced to next position */
prog++;
temp++;
*temp=0;
return(token_type=DELIMITER);
}

/* quoted string */
if(*prog=='"')
{
prog++;

while(*prog!='"' && *prog!='\n')
{
*temp++ = *prog++;
}

if(*prog=='\n')
{
serror(1);
}

prog++; *temp=0;
return(token_type=QUOTE);
}

/* number */
if(isdigit(*prog))
{
while(!isdelim(*prog))
{
*temp++ = *prog++;
}

*temp='\0';
return(token_type=NUMBER);
}

/* var or command */
if(isalpha(*prog))
{
while(!isdelim(*prog))
{
*temp++ = *prog++;
}

token_type=STRING;
}

*temp='\0';

/* see if a string is a command or a variable */
if(token_type==STRING)
{
/* convert to internal rep */
tok=look_up(token);

if(*prog++ == '(') //(dim추가구분)
{
#ifdef DEBUG2
printf("token_type : ARRAY 부여 \n");
#endif

token_type=ARRAY;
*prog--;
}

else if(!tok)
{
#ifdef DEBUG
printf("token_type : VARIABLE 부여 \n");
#endif

token_type=VARIABLE;
}

/* is a command */
else
{
token_type=COMMAND;
}
}
return token_type;
}

/* Return a token to input stream. */
void putback(void)
{
char *t;

t = token;
for(; *t; t++)
{
prog--;
}
}

/* Look up a token's internal representation in the
token table */
int look_up(char *s)
{
register int i;//,j; no using************************
char *p;

/* convert to lowercase */
p = s;

while(*p)
{
*p = tolower(*p); p++;
}

/* see if token is in table */
for(i=0; *table[i].command; i++)
{
if(!strcmp(table[i].command,s))
{
return table[i].tok;
}
}

/* unknown command */
return 0;
}

/* Return true if c is a delimiter. */
int isdelim(char c)
{
if(strchr(" ;,+-<>/*%^=()",c) || c==9 || c=='\n' || c==0)
{
return 1;
}

return 0;
}

/* Return 1 if c is space or tab */
int iswhite(char c)
{
if(c==' ' || c=='\t')
{
return 1;
}

else return 0;
}

void clear(void)//보류
{
int count1, count2;

printf("clear all data & memory \n");

for(count1 = 0; count1<80; count1++)
{
token[count1] = '0';
}

//*prog = '0'; //다음에 실행할 basic 문자를 지정
//token_type = '0';
//tok = '0';

//char *gstack[SUB_NEST]; /* stack for gosub */

ftos=0; /* index to top of FOR stack */
gtos=0; /* index to top of GOSUB stack */
/*
for(count1 = 0; count1<26; count1++)
{
variables[count1] = 0;
}
*/

for(count1 = 0; count1<100; count1++)
{
//*label_table[count1].p = '0';
//label_table[count1].p++;

for(count2 = 0; count2<10; count2++)
{
label_table[count1].name[count2] = '0';
}

}

/*
for(count1 = 0; count1<25; count1++)
{
fstack[count1].var = 0;
fstack[count1].target = 0;
*fstack[count1].loc = '0';
}
*/

}


void dim(void) //(dim추가구분)
{
int var;

get_token(); //배열이름

if(!isalpha(*token)) //알파벳이 판단 함수
{
serror(4);
return;
}

var = toupper(*token)-'A';

if(arr_table[var].guard)
{
serror(18);
return;
}

else
{
arr_table[var].guard = 1;
}

get_token(); //'(' 받음

if(*token!='(')
{
serror(14);
return;
}

get_token(); //숫자 받음

if(token_type == NUMBER)
{
arr_table[var].num = atoi(token); //c문법대로라면 현행 베이직 문법대로라면 + 1을 해야함
}

else //만약 숫자가 아닐경우
{
#ifdef DEBUG2
printf("dim - serror(17) - token : %s \n",token);
#endif

serror(17);
return;
}

get_token(); //')'받음

if(*token!=')')
{
serror(15);
return;
}


}

/*
1. clear 명령어 완성못함

3. 동적 메모리 할당으로 배열길이 자유조절하기
*/