Skip to content

Commit 6fe6d91

Browse files
committed
Roll in Python version
1 parent 505ea02 commit 6fe6d91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1606
-645
lines changed

ecr/debug.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef DEBUG_H
2+
#define DEBUG_H
3+
4+
#include "ecr.h"
5+
6+
int debug(StringArray* codes, StringArray* keys) {
7+
// Decode the files and print for inspection
8+
for (int pc = 0; pc < codes->size; pc++) {
9+
printf("PC = %d, code = %s\n", pc, codes->array[pc]);
10+
StringArray* items = getLines(codes->array[pc], ',');
11+
for (int n = 0; n < items->size; n++) {
12+
char* item = items->array[n];
13+
int p = strpos(item, ':');
14+
if (p >= 0) {
15+
char* key = (char*)malloc(p + 1);
16+
strncpy(key, item, p);
17+
char* value = (char*)malloc(strlen(item) - p);
18+
strcpy(value, item + p + 1);
19+
if (value[0] == '[') {
20+
printf("%s:[\n", keys->array[atoi(key)]);
21+
}
22+
else if (value[0] == '{') {
23+
printf("%s:{\n", keys->array[atoi(key)]);
24+
} else {
25+
printf("%s:%s\n", keys->array[atoi(key)], keys->array[atoi(value)]);
26+
}
27+
}
28+
else if (item[0] == ']') {
29+
printf("]\n");
30+
}
31+
else if (item[0] == '}') {
32+
printf("}\n");
33+
} else if (item[0] == '#') {
34+
printf("%d\n", atoi(item + 1));
35+
} else {
36+
printf("%s\n", keys->array[atoi(item)]);
37+
}
38+
}
39+
}
40+
return 0;
41+
}
42+
43+
#endif

ecr/ecr

24.7 KB
Binary file not shown.

ecr/ecr.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "ecr.h"
2+
#include "debug.h"
3+
#include "run.h"
4+
5+
// Main program
6+
int main(void)
7+
{
8+
// Read the code file into memory as an array of lines
9+
StringArray* codes = readFile((char*)"test.code");
10+
if (codes == NULL) {
11+
return 1;
12+
}
13+
printf("Code lines: %d\n", codes->size);
14+
15+
// Read the key file into memory as an array of lines
16+
StringArray* keys = readFile((char*)"test.keys");
17+
if (keys == NULL) {
18+
return 1;
19+
}
20+
printf("Keys: %d\n", keys->size);
21+
22+
// debug(codes, keys);
23+
24+
int result = run(codes, keys);
25+
26+
return result;
27+
}

ecr/ecr.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#ifndef ECR_H
2+
#define ECR_H
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
#define NEWLINE (const char)'\n'
9+
10+
// A struct to hold an array of string buffers and the array size
11+
struct StringArray {
12+
char** array;
13+
int size;
14+
};
15+
16+
// Replace one character with another in situ
17+
void replaceChar(char* string, char from, char to) {
18+
int len = strlen(string);
19+
for (int n = 0; n < len; n++) {
20+
if (string[n] == from) {
21+
string[n] = to;
22+
}
23+
}
24+
}
25+
26+
// Find the position of a character inside a string
27+
int strpos(char* haystack, char needle) {
28+
int len = strlen(haystack);
29+
for (int n = 0; n < len; n++) {
30+
if (haystack[n] == needle) {
31+
return n;
32+
}
33+
}
34+
return -1;
35+
}
36+
37+
// Convert a string to an array of lines, using a specified delimiter.
38+
StringArray* getLines(char* string, char delimiter) {
39+
int length = strlen(string);
40+
int nlines = 0;
41+
int start = 0;
42+
int pos = 0;
43+
44+
// Count the number of lines.
45+
while (pos < length) {
46+
char c = string[pos];
47+
if (c == delimiter || c == '\0') {
48+
string[pos] = '\0';
49+
start = pos + 1;
50+
nlines++;
51+
}
52+
pos++;
53+
}
54+
if (start < pos) {
55+
++nlines;
56+
}
57+
58+
// Create an array of lines
59+
char** lines = (char**)malloc(nlines * sizeof(char*));
60+
pos = 0;
61+
int line = 0;
62+
while (pos < length) {
63+
int l = strlen(&string[pos]);
64+
lines[line] = (char*)malloc(l + 1);
65+
strcpy(lines[line++], &string[pos]);
66+
pos += l + 1;
67+
}
68+
69+
struct StringArray* mylist = (struct StringArray*)malloc(sizeof(struct StringArray));
70+
71+
mylist->array = lines;
72+
mylist->size = nlines;
73+
74+
return mylist;
75+
}
76+
77+
// Read a file and convert it into an array of strings
78+
StringArray* readFile(char* filename) {
79+
FILE *f = fopen(filename, "rb");
80+
if (f == NULL) {
81+
printf("Could not read file %s\n", filename);
82+
return NULL;
83+
};
84+
85+
fseek(f, 0, SEEK_END);
86+
long fsize = ftell(f);
87+
rewind(f);
88+
89+
char* string = (char*)malloc(fsize + 1);
90+
if (string != NULL) {
91+
fread(string, fsize, 1, f);
92+
fclose(f);
93+
string[fsize] = 0;
94+
} else {
95+
printf("Could not allocate memory for %s\n", filename);
96+
return NULL;
97+
}
98+
99+
// Convert the file to an array of lines
100+
StringArray* lines = getLines(string, NEWLINE);
101+
102+
// Dispose of the original file
103+
free(string);
104+
return lines;
105+
}
106+
107+
108+
#endif

ecr/out

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
Lines = 13
2+
Lines = 42
3+
PC = 0, code = 0:1,#4,2:3,4:5,6:7,8:9,10:11,12:13,14:15,16:[,18,],19:9
4+
Lines = 13
5+
domain:core
6+
4
7+
keyword:variable
8+
type:symbol
9+
debug:False
10+
valueHolder:True
11+
name:V
12+
elements:1
13+
index:0
14+
value:[
15+
None
16+
]
17+
used:True
18+
PC = 1, code = 0:1,#5,2:3,4:5,6:7,8:9,10:20,12:13,14:15,16:[,18,],19:9
19+
Lines = 13
20+
domain:core
21+
5
22+
keyword:variable
23+
type:symbol
24+
debug:False
25+
valueHolder:True
26+
name:Stack
27+
elements:1
28+
index:0
29+
value:[
30+
None
31+
]
32+
used:True
33+
PC = 2, code = 0:1,#7,2:21,4:18,6:9,16:{,0:1,4:22,23:{,4:24,23:25,},},26:20
34+
Lines = 14
35+
domain:core
36+
7
37+
keyword:put
38+
type:None
39+
debug:True
40+
value:{
41+
domain:core
42+
type:json
43+
content:{
44+
type:text
45+
content:[]
46+
}
47+
}
48+
target:Stack
49+
PC = 3, code = 0:1,#8,2:21,4:18,6:9,16:{,0:1,4:22,23:{,4:24,23:27,},},26:11
50+
Lines = 14
51+
domain:core
52+
8
53+
keyword:put
54+
type:None
55+
debug:True
56+
value:{
57+
domain:core
58+
type:json
59+
content:{
60+
type:text
61+
content:{}
62+
}
63+
}
64+
target:V
65+
PC = 4, code = 0:1,#9,2:21,4:18,6:9,16:{,4:24,23:28,},26:11
66+
Lines = 10
67+
domain:core
68+
9
69+
keyword:put
70+
type:None
71+
debug:True
72+
value:{
73+
type:text
74+
content:Hello, world!
75+
}
76+
target:V
77+
PC = 5, code = 0:1,#10,2:29,4:18,6:9,16:{,0:1,10:11,4:5,},30:20
78+
Lines = 11
79+
domain:core
80+
10
81+
keyword:push
82+
type:None
83+
debug:True
84+
value:{
85+
domain:core
86+
name:V
87+
type:symbol
88+
}
89+
to:Stack
90+
PC = 6, code = 0:1,#11,2:31,4:18,6:9,16:{,4:32,33:7,34:[,17:{,4:24,23:35,},17:{,0:1,10:11,4:5,},],}
91+
Lines = 20
92+
domain:core
93+
11
94+
keyword:print
95+
type:None
96+
debug:True
97+
value:{
98+
type:cat
99+
numeric:False
100+
parts:[
101+
None:{
102+
type:text
103+
content:Pushed
104+
}
105+
None:{
106+
domain:core
107+
name:V
108+
type:symbol
109+
}
110+
]
111+
}
112+
PC = 7, code = 0:1,#12,2:21,4:18,6:9,16:{,4:24,23:36,},26:11
113+
Lines = 10
114+
domain:core
115+
12
116+
keyword:put
117+
type:None
118+
debug:True
119+
value:{
120+
type:text
121+
content:Goodbye
122+
}
123+
target:V
124+
PC = 8, code = 0:1,#13,2:31,4:18,6:9,16:{,4:32,33:7,34:[,17:{,4:24,23:37,},17:{,0:1,10:11,4:5,},],}
125+
Lines = 20
126+
domain:core
127+
13
128+
keyword:print
129+
type:None
130+
debug:True
131+
value:{
132+
type:cat
133+
numeric:False
134+
parts:[
135+
None:{
136+
type:text
137+
content:V:
138+
}
139+
None:{
140+
domain:core
141+
name:V
142+
type:symbol
143+
}
144+
]
145+
}
146+
PC = 9, code = 0:1,#14,2:38,4:18,6:9,26:11,39:20
147+
Lines = 7
148+
domain:core
149+
14
150+
keyword:pop
151+
type:None
152+
debug:True
153+
target:V
154+
from:Stack
155+
PC = 10, code = 0:1,#15,2:31,4:18,6:9,16:{,4:32,33:7,34:[,17:{,4:24,23:40,},17:{,0:1,10:11,4:5,},],}
156+
Lines = 20
157+
domain:core
158+
15
159+
keyword:print
160+
type:None
161+
debug:True
162+
value:{
163+
type:cat
164+
numeric:False
165+
parts:[
166+
None:{
167+
type:text
168+
content:Popped
169+
}
170+
None:{
171+
domain:core
172+
name:V
173+
type:symbol
174+
}
175+
]
176+
}
177+
PC = 11, code = 0:1,#16,2:41,4:18,6:9
178+
Lines = 5
179+
domain:core
180+
16
181+
keyword:exit
182+
type:None
183+
debug:True
184+
PC = 12, code =
185+
Lines = 0

ecr/run

24.6 KB
Binary file not shown.

ecr/run.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef RUN_H
2+
#define RUN_H
3+
4+
#include "ecr.h"
5+
6+
char* command = (char*)malloc(1);
7+
8+
int runOneCommand(char* cmd, StringArray* keys){
9+
command = (char*)realloc(command, strlen(cmd));
10+
strcpy(command, cmd);
11+
printf("%s\n", command);
12+
StringArray* items = getLines(command, ',');
13+
printf("%d\n", items->size);
14+
return 0;
15+
}
16+
17+
int run(StringArray* codes, StringArray* keys) {
18+
printf("Run the program\n");
19+
char* command = codes->array[0];
20+
runOneCommand(command, keys);
21+
return 0;
22+
}
23+
24+
#endif

0 commit comments

Comments
 (0)