-
Notifications
You must be signed in to change notification settings - Fork 0
/
lclib.c
86 lines (79 loc) · 1.54 KB
/
lclib.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include"stdio.h"
#include"stdlib.h"
extern char *prog;
extern char token[80];
extern char token_type;
extern char tok;
enum tok_types {DELIMITER, IDENTIFIER, NUMBER, COMMAND, STRING,
QUOTE, VARIABLE, BLOCK, FUNCTION};
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};
int get_token(void);
void sntx_err(int error), eval_exp(int *result);
void putback(void);
int call_getche(void)
{
char ch;
ch = getchar();
while(*prog!=')') prog++;
prog++;
return ch;
}
int call_putch(void)
{
#ifdef DEBUG
printf("\nclclib.call_putch(void) entered");
#endif
int value;
eval_exp(&value);
printf("%c",value);
return value;
}
int call_puts(void)
{
get_token();
if(*token!='(') sntx_err(PAREN_EXPECTED);
get_token();
if(token_type!=QUOTE) 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;
}
int print(void)
{
int i;
get_token();
if(*token!='(') sntx_err(PAREN_EXPECTED);
get_token();
if(token_type==QUOTE) {
printf("%s ", token);
}
else {
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;
}
int getnum(void)
{
char s[80];
fgets(s, 80, stdin);
while(*prog!=')') prog++;
prog++;
return atoi(s);
}