📄 chap29.lst
字号:
lvartemp = lvartos; /* save local var stack index */
get_args(); /* get function arguments */
temp = prog; /* save return location */
func_push(lvartemp); /* save local var stack index */
prog = loc; /* reset prog to start of function */
get_params(); /* load the function's parameters with
the values of the arguments */
interp_block(); /* interpret the function */
prog = temp; /* reset the program pointer */
lvartos = func_pop(); /* reset the local var stack */
}
}
/* Push the arguments to a function onto the local
variable stack. */
void get_args(void)
{
int value, count, temp[NUM_PARAMS];
struct var_type i;
count = 0;
get_token();
if(*token != '(') sntx_err(PAREN_EXPECTED);
/* process a comma-separated list of values */
do {
eval_exp(&value);
temp[count] = value; /* save temporarily */
get_token();
count++;
} while(*token == ',');
count--;
/* now, push on local_var_stack in reverse order */
for(; count>=0; count--) {
i.value = temp[count];
i.v_type = ARG;
local_push(i);
}
}
/* Get function parameters. */
void get_params(void)
{
struct var_type *p;
int i;
i = lvartos-1;
do { /* process comma-separated list of parameters */
get_token();
p = &local_var_stack[i];
if(*token != ')' ) {
if(tok != INT && tok != CHAR)
sntx_err(TYPE_EXPECTED);
p->v_type = token_type;
get_token();
/* link parameter name with argument already on
local var stack */
strcpy(p->var_name, token);
get_token();
i--;
}
else break;
} while(*token == ',');
if(*token != ')') sntx_err(PAREN_EXPECTED);
}
listing 12
/* Return from a function. */
void func_ret(void)
{
int value;
value = 0;
/* get return value, if any */
eval_exp(&value);
ret_value = value;
}
listing 13
/* Assign a value to a variable. */
void assign_var(char *var_name, int value)
{
register int i;
/* first, see if it's a local variable */
for(i=lvartos-1; i >= call_stack[functos-1]; i--) {
if(!strcmp(local_var_stack[i].var_name, var_name)) {
local_var_stack[i].value = value;
return;
}
}
if(i < call_stack[functos-1])
/* if not local, try global var table */
for(i=0; i < NUM_GLOBAL_VARS; i++)
if(!strcmp(global_vars[i].var_name, var_name)) {
global_vars[i].value = value;
return;
}
sntx_err(NOT_VAR); /* variable not found */
}
listing 14
/* Execute an if statement. */
void exec_if(void)
{
int cond;
eval_exp(&cond); /* get if expression */
if(cond) { /* is true so process target of IF */
interp_block();
}
else { /* otherwise skip around IF block and
process the ELSE, if present */
find_eob(); /* find start of next line */
get_token();
if(tok != ELSE) {
putback(); /* restore token if
no ELSE is present */
return;
}
interp_block();
}
}
listing 15
/* Execute a while loop. */
void exec_while(void)
{
int cond;
char *temp;
putback();
temp = prog; /* save location of top of while loop */
get_token();
eval_exp(&cond); /* check the conditional expression */
if(cond) interp_block(); /* if true, interpret */
else { /* otherwise, skip around loop */
find_eob();
return;
}
prog = temp; /* loop back to top */
}
listing 16
/* Execute a do loop. */
void exec_do(void)
{
int cond;
char *temp;
putback();
temp = prog; /* save location of top of do loop */
get_token(); /* get start of loop */
interp_block(); /* interpret loop */
get_token();
if(tok != WHILE) sntx_err(WHILE_EXPECTED);
eval_exp(&cond); /* check the loop condition */
if(cond) prog = temp; /* if true loop; otherwise,
continue on */
}
listing 17
/* Execute a for loop. */
void exec_for(void)
{
int cond;
char *temp, *temp2;
int brace ;
get_token();
eval_exp(&cond); /* initialization expression */
if(*token != ';') sntx_err(SEMI_EXPECTED);
prog++; /* get past the ; */
temp = prog;
for(;;) {
eval_exp(&cond); /* check the condition */
if(*token != ';') sntx_err(SEMI_EXPECTED);
prog++; /* get past the ; */
temp2 = prog;
/* find the start of the for block */
brace = 1;
while(brace) {
get_token();
if(*token == '(') brace++;
if(*token == ')') brace--;
}
if(cond) interp_block(); /* if true, interpret */
else { /* otherwise, skip around loop */
find_eob();
return;
}
prog = temp2;
eval_exp(&cond); /* do the increment */
prog = temp; /* loop back to top */
}
}
listing 18
int getche(void); /* read a character from keyboard and
return its value */
int putch(char ch); /* write a character to the screen */
int puts(char *s); /* write a string to the screen */
int getnum(void); /* read an integer from the keyboard and
return its value */
int print(char *s); /* write a string to the screen */
or
int print(int i); /* write an integer to the screen */
listing 19
/****** Internal Library Functions *******/
/* Add more of your own, here. */
#include <conio.h> /* if your compiler does not
support this header file,
remove it */
#include <stdio.h>
#include <stdlib.h>
extern char *prog; /* points to current location in program */
extern char token[80]; /* holds string representation of token */
extern char token_type; /* contains type of token */
extern char tok; /* holds the internal representation of token */
enum tok_types {DELIMITER, IDENTIFIER, NUMBER, KEYWORD,
TEMP, STRING, BLOCK};
/* These are the constants used to call sntx_err() when
a syntax error occurs. Add more if you like.
NOTE: SYNTAX is a generic error message used when
nothing else seems appropriate.
*/
enum error_msg
{SYNTAX, UNBAL_PARENS, NO_EXP, EQUALS_EXPECTED,
NOT_VAR, PARAM_ERR, SEMI_EXPECTED,
UNBAL_BRACES, FUNC_UNDEF, TYPE_EXPECTED,
NEST_FUNC, RET_NOCALL, PAREN_EXPECTED,
WHILE_EXPECTED, QUOTE_EXPECTED, NOT_STRING,
TOO_MANY_LVARS, DIV_BY_ZERO};
int get_token(void);
void sntx_err(int error), eval_exp(int *result);
void putback(void);
/* Get a character from console. (Use getchar() if
your compiler does not support _getche().) */
int call_getche()
{
char ch;
ch = _getche();
while(*prog!=')') prog++;
prog++; /* advance to end of line */
return ch;
}
/* Put a character to the display. */
int call_putch()
{
int value;
eval_exp(&value);
printf("%c", value);
return value;
}
/* Call puts(). */
int call_puts(void)
{
get_token();
if(*token!='(') sntx_err(PAREN_EXPECTED);
get_token();
if(token_type!=STRING) sntx_err(QUOTE_EXPECTED);
puts(token);
get_token();
if(*token!=')') sntx_err(PAREN_EXPECTED);
get_token();
if(*token!=';') sntx_err(SEMI_EXPECTED);
putback();
return 0;
}
/* A built-in console output function. */
int print(void)
{
int i;
get_token();
if(*token!='(') sntx_err(PAREN_EXPECTED);
get_token();
if(token_type==STRING) { /* output a string */
printf("%s ", token);
}
else { /* output a number */
putback();
eval_exp(&i);
printf("%d ", i);
}
get_token();
if(*token!=')') sntx_err(PAREN_EXPECTED);
get_token();
if(*token!=';') sntx_err(SEMI_EXPECTED);
putback();
return 0;
}
/* Read an integer from the keyboard. */
int getnum(void)
{
char s[80];
gets(s);
while(*prog != ')') prog++;
prog++; /* advance to end of line */
return atoi(s);
}
listing 20
/* Little C Demonstration Program #1.
This program demonstrates all features
of C that are recognized by Little C.
*/
int i, j; /* global vars */
char ch;
int main()
{
int i, j; /* local vars */
puts("Little C Demo Program.");
print_alpha();
do {
puts("enter a number (0 to quit): ");
i = getnum();
if(i < 0 ) {
puts("numbers must be positive, try again");
}
else {
for(j = 0; j < i; j=j+1) {
print(j);
print("summed is");
print(sum(j));
puts("");
}
}
} while(i!=0);
return 0;
}
/* Sum the values between 0 and num. */
int sum(int num)
{
int running_sum;
running_sum = 0;
while(num) {
running_sum = running_sum + num;
num = num - 1;
}
return running_sum;
}
/* Print the alphabet. */
int print_alpha()
{
for(ch = 'A'; ch<='Z'; ch = ch + 1) {
putch(ch);
}
puts("");
return 0;
}
listing 21
/* Nested loop example. */
int main()
{
int i, j, k;
for(i = 0; i < 5; i = i + 1) {
for(j = 0; j < 3; j = j + 1) {
for(k = 3; k ; k = k - 1) {
print(i);
print(j);
print(k);
puts("");
}
}
}
puts("done");
return 0;
}
listing 22
/* Assigments as operations. */
int main()
{
int a, b;
a = b = 10;
print(a); print(b);
while(a=a-1) {
print(a);
do {
print(b);
} while((b=b-1) > -10);
}
return 0;
}
listing 23
/* This program demonstrates recursive functions. */
/* return the factorial of i */
int factr(int i)
{
if(i<2) {
return 1;
}
else {
return i * factr(i-1);
}
}
int main()
{
print("Factorial of 4 is: ");
print(factr(4));
return 0;
}
listing 24
/* A more rigorous example of function arguments. */
int f1(int a, int b)
{
int count;
print("in f1");
count = a;
do {
print(count);
} while(count=count-1);
print(a); print(b);
print(a*b);
return a*b;
}
int f2(int a, int x, int y)
{
print(a); print(x);
print(x / a);
print(y*x);
return 0;
}
int main()
{
f2(10, f1(10, 20), 99);
return 0;
}
listing 25
/* The loop statements. */
int main()
{
int a;
char ch;
/* the while */
puts("Enter a number: ");
a = getnum();
while(a) {
print(a);
print(a*a);
puts("");
a = a - 1;
}
/* the do-while */
puts("enter characters, 'q' to quit");
do {
ch = getche();
} while(ch !='q');
/* the for */
for(a=0; a<10; a = a + 1) {
print(a);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -