-
Notifications
You must be signed in to change notification settings - Fork 1
/
edit.cpp
177 lines (170 loc) · 3.69 KB
/
edit.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "ec_conf.h"
#include "rpn.h"
static void cdel(char *buf)
{
for(;*buf;buf++)
*(buf)=*(buf+1);
}
static void cins(char *buf, char c)
{
for(;c;buf++){
c^=*buf;
*buf^=c;
c^=*buf;
}
*buf='\0';
}
static void set_exp_sign(char *buf, bool neg)
{
int8_t n = 0;
while(buf[n] && buf[n]!=EXPCHAR)n++; // string MUST contain an EXPCHAR
if(!buf[n])return;
n++;
if(neg && buf[n]=='-')return;
if(!neg && buf[n]!='-')return;
if(!neg)cdel(&buf[n]); //delete '-'
else cins(&buf[n],'-'); //insert '-'
}
void rpn::key_edit(char key)
{
static char edln[18];
static int8_t edpos=0,expos=0;
static bool neg,point,ex, sexp=0;
static int16_t exv=0;
f64 result;
if(!(altFn&alt_Edit)){
altFn |= alt_Edit;
edln[0]='-'; //it is easier to insert the sign first and remove it later
for(int8_t i=1;i<18;i++)edln[i]=0;
if(!(altFn&alt_Fix)){
PrDev->lcdclear(0);
show_flags();
}
edpos=1;
point=neg=false;
exv=0;
sexp=ex=false;
PrDev->cursor();
PrDev->blink();
}
switch(key){
case 0: //dummy: do nothing
return;
case 'a' ... 'f': //hex digits
if(!hex_en)goto def;
key+='A'-'a'; //substitute capital
//drop through
case '0' ... '9':
if(ex){
if(exv*10+(key-'0')>=307)break;
exv*=10;
exv+=key-'0';
}
edln[edpos++]=key;
PrDev->print(key);
if( (altFn&alt_Fix) && ((edpos>2) || (key>'1')) ){
goto xit;
}
break;
case '_': //sign
if(ex){
sexp^=1;
set_exp_sign(edln+expos,sexp);
edpos+=sexp?1:-1;
}
else{
neg^=1; //toggle sign
}
PrDev->lcdclear(0);
PrDev->print(&edln[!neg]);
break;
case '.':
if(!point){
if(edpos<2){
PrDev->print('0');
edln[edpos++]='0';
}
edln[edpos++]='.';
PrDev->print('.');
}
point=true;
break;
case 'E':
if(!ex){
if(edpos==1){ /* eex cannot be first */
PrDev->print('1');
edln[edpos++]='1';
}
expos=edpos;
edln[edpos++]=EXPCHAR;
PrDev->print((char)EXPCHAR);
}
ex=true;
exv=0;
sexp=false;
break;
case '\177': //pc raw backspace
case '\010': //terminal backspace
edpos--;
if(edln[edpos]=='.')point=false;
if(edln[edpos]=='e'){
ex=false;
}
if(ex && edln[edpos]=='-')sexp=false;
exv=exv/10;
edln[edpos]='\0';
if(edpos<2){ //exit edit mode
// altFn=alt_Norm;
// PrDev->lcdprint(stackx.toString());
stack_pull(); //okay, but destroys stack top
goto out;
}
else{
PrDev->lcdclear(0);
PrDev->lcdprint(&edln[!neg]);
}
break;
/*
case 'X': //toggle hex mode
hex_en ^= 1;
lastx.setBase(hex_en?16:10);
show_flags();
PrDev->lcdprint(&edln[!neg]);
break;
*/
case CR:
xit:
edln[edpos]='\0';
result = strtof64(&edln[!neg], NULL);
if(altFn&alt_Fix){
lastx.setDecs(result.ipart());
}
else stackx=result;
out:
altFn=alt_Norm;
//push_en=true;
show_stack();
return;
break;
case '?': //shift
altFn^=alt_Shift;
show_flags();
PrDev->setCursor(neg+edpos,1);
break;
case '!':
stack_pull();
goto out;
break;
case 'S':
case 'R':
default: /* do other functions immediately */
def:
altFn&=~alt_Edit; //don't do edit again
//push_en=true;
edln[edpos]='\0';
stackx = strtof64(&edln[!neg], NULL);
key_input(key,'.');
//altFn=alt_Norm; //return to normal
break;
}
}