-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.pde
More file actions
120 lines (106 loc) · 2.94 KB
/
program.pde
File metadata and controls
120 lines (106 loc) · 2.94 KB
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
class Program{
String name;
String[] bname = new String[8];
String comment = "REbasic ver " + ver;
boolean archived = false;
String[] prgm;
ArrayList<String> bprgm;
int length;
Program(){
;
}
Program(String file){
String lines[] = loadStrings(file);
name = lines[0];
prgm = new String[lines.length - 1];
for(int i = 0; i < prgm.length; i ++){
prgm[i] = lines[i + 1];
}
encodeName();
encodeBody();
//printBody();
log.debug();
log.info("program length: " + length);
}
void tokenbytes(String hex){
if(hex.length() == 2){
bprgm.add(hex);
length ++;
}
else if(hex.length() == 4){
bprgm.add(hex.substring(0, 2));
bprgm.add(hex.substring(2, 4));
length += 2;
}
else{
log.debug();
log.warning("bad bprgm hex: " + hex);
log.debug();
}
}
void encodeName(){
log.info("encoding name: " + name);
printbar();
String namebytes = "";
for(int i = 0; i < 8; i ++){
//only alphanumeric characters and theta are allowed
if(i >= name.length() || !(isalphanum(name.charAt(i)) || name.charAt(i) == 'θ'))
bname[i] = "00";
else if(name.charAt(i) == 'θ')
bname[i] = "5B";
else
bname[i] = Integer.toHexString(name.charAt(i));
namebytes += (bname[i] + " ");
}
log.debug(namebytes);
log.debug();
}
void encodeBody(){
log.info("encoding body...");
printbar();
bprgm = new ArrayList<String>();
for(int i = 0; i < prgm.length; i ++){
if(i > 0){
tokenbytes("3F");
}
for(int j = 0; j < prgm[i].length(); j ++){
int currchar = prgm[i].charAt(j);
//alpha numeric char (capitals only)
if(isalphanum(prgm[i].charAt(j))){
tokenbytes(Integer.toHexString(prgm[i].charAt(j)));
}
//char for tokenizer
else if(currchar == '§'){
//tokenizing
String token;
token = prgm[i].substring(j + 1, prgm[i].indexOf('§', j + 1));
prgm[i] = prgm[i].substring(0, j) + prgm[i].substring(prgm[i].indexOf('§', j + 1) + 1, prgm[i].length());
log.info("encoding token " + token + " on line " + (i+1) + " to " + tokens.get(token));
//bprgmBytes also increments length
if(tokens.get(token) != null)
tokenbytes(tokens.get(token));
else
log.warning("bad token: " + token);
j --;
}
else{
//if the token is in the lookup table
if(currchar < ctokens.length && ctokens[currchar] != null){
tokenbytes(ctokens[currchar]);
}
}
}
}
}
void printBody(){
String line = "";
for(int i = 0; i < bprgm.size(); i ++){
line = line + bprgm.get(i) + " ";
if((i + 1) % 16 == 0){
log.debug(line);
line = "";
}
}
log.debug(line);
}
}