-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.c
98 lines (89 loc) · 1.92 KB
/
io.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
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include "allocator.h"
#include "expr.h"
#include "io.h"
#include "display.h"
static char* readString(){
char *ret = NULL;
char c = getchar();
int i = 0;
while(c != ' ' && c != '\n'){
i++;
ret = (char *)reallocate(ret, sizeof(char)*i);
ret[i - 1] = c;
c = getchar();
}
ret = (char *)reallocate(ret, sizeof(char) * (i + 1));
ret[i] = '\0';
return ret;
}
Literal getString(int line){
char *s = readString();
Literal ret = {line, LIT_STRING, {0}};
ret.sVal = s;
return ret;
}
static int isNum(char c){
return c>='0' && c<='9';
}
static int isSign(char c){
return c == '-'|| c == '+';
}
static int isInt(char *s){
int i = 0;
if(isSign(s[0]))
i++;
while(s[i] != '\0'){
if(!isNum(s[i]))
return 0;
i++;
}
if(isSign(s[0]) && i == 1)
return 0;
return 1;
}
Literal getInt(int line){
char *s = readString();
while(!isInt(s)){
printf(warning("[Input Error] Not an integer : %s!\n[Re-Input] "), s);
s = readString();
}
long l = 0;
sscanf(s, "%ld", &l);
memfree(s);
Literal lit = {line, LIT_INT, {0}};
lit.iVal = l;
return lit;
}
static int isNumber(char *s){
int i = 0, hasDot = 0;
if(isSign(s[0]))
i++;
while(s[i] != '\0'){
if(s[i] == '.'){
if(hasDot == 0)
hasDot = 1;
else
return 0;
}
else if(!isNum(s[i]))
return 0;
i++;
}
if(isSign(s[0]) && i == 1)
return 0;
return 1;
}
Literal getFloat(int line){
char *s = readString();
while(!isNumber(s)){
printf(warning("[Input Error] Not a number : %s!\n[Re-Input] "), s);
s = readString();
}
double d = 0;
sscanf(s, "%lf", &d);
memfree(s);
Literal lit = {line, LIT_DOUBLE, {0}};
lit.dVal = d;
return lit;
}