Skip to content

Commit 43f564e

Browse files
test
1 parent 4e237cb commit 43f564e

8 files changed

Lines changed: 119 additions & 40 deletions

File tree

src/tensor-array/interp/open_file.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
#include <stdio.h>
1818
#include <stdlib.h>
1919
#include <string.h>
20+
#include "vm.h"
2021
#include "open_file.h"
2122

2223
char *src = NULL;
@@ -63,12 +64,17 @@ void read_file(const char* filename)
6364

6465
int i;
6566
interp_malloc();
67+
interp_memreset();
6668
i = fread(src, poolsize, 1, fptr);
6769
if (i < 0)
6870
{
6971
fprintf(stderr, "Error: Could not read file %s\n", filename);
7072
fclose(fptr);
7173
exit(1);
7274
}
73-
return 0; // Return 0 on success
75+
orig = text;
76+
text = text - 1;
77+
src[i] = '\0';
78+
fclose(fptr);
79+
7480
}

src/tensor-array/interp/parser.c

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121
#include "parser.h"
2222
#include "token.h"
2323
#include "open_file.h"
24+
#include "vm_type.h"
2425

2526
void emit(int size, ...)
2627
{
@@ -53,7 +54,7 @@ void match(long tk)
5354

5455
void expression(int level)
5556
{
56-
void* temp = NULL; // Temporary variable to hold intermediate values
57+
sym_data* temp = NULL; // Temporary variable to hold intermediate values
5758
int isArrRef = 0; // Flag to check if we are dealing with an array reference
5859
// This function would handle parsing and evaluating expressions
5960
// For now, it is a placeholder
@@ -67,15 +68,19 @@ void expression(int level)
6768
break;
6869
case TOKEN_ID:
6970
/* code */
70-
//temp = sym_cur;
71+
temp = sym_cur;
7172
match(TOKEN_ID);
72-
/*
73-
if (!temp->data)
73+
if (temp->type)
7474
{
75-
temp->data = new_Tensor();
75+
if (token == '(')
76+
{
77+
/* code */
78+
match('(');
79+
match(')');
80+
emit(2, CALL, temp->data)
81+
}
82+
7683
}
77-
*/
78-
if (0);
7984
else
8085
{
8186
emit(3, IMM, TYPE_PTR, tkn_val);
@@ -247,6 +252,37 @@ void statement()
247252
*a = text + 1; // Set the jump address to the next instruction
248253
}
249254
break;
255+
case TOKEN_FUNC:
256+
match(TOKEN_FUNC);
257+
if (tkn != TOKEN_ID)
258+
{
259+
fprintf(stderr, "Error: function name\n");
260+
exit(1);
261+
}
262+
cur->type = TYPE_FUNC;
263+
cur->data = malloc(1024*8);
264+
VM_INSTRUCTION *save = text;
265+
text = cur->data
266+
match(TOKEN_ID);
267+
match('(');
268+
match(')');
269+
statement();
270+
if (*text != RET) emit(1, RET);
271+
text = save;
272+
break;
273+
case: TOKEN_RETURN:
274+
match(TOKEN_RETURN);
275+
expression(TOKEN_ASSIGN);
276+
emit(1, RET);
277+
break;
278+
case '{':
279+
match('{');
280+
while (tkn != '}')
281+
statement();
282+
match('}');
283+
break;
284+
case '\0':
285+
return;
250286
default:
251287
expression(TOKEN_ASSIGN);
252288
if (tkn == ';')

src/tensor-array/interp/sym_map.cc

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
sym_data* sym_cur = NULL;
2222

23-
std::map<std::string, sym_data> sym_map;
23+
scope sym_map;
2424

2525
void sym_data_set(char* name, sym_data dat)
2626
{
@@ -36,13 +36,3 @@ int glob_data_find(char* name)
3636
{
3737
return sym_map.find(name) != sym_map.end();
3838
}
39-
40-
void* new_Tensor()
41-
{
42-
return new tensor_array::value::Tensor;
43-
}
44-
void delete_Tensor(void* t)
45-
{
46-
delete t;
47-
}
48-

src/tensor-array/interp/sym_map.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ extern "C"
2323
long tkn;
2424
long hash;
2525
long cls;
26+
long type;
2627
void* data; // Pointer to additional data if needed
2728
} sym_data;
2829
void sym_data_set(char* name, sym_data dat);
2930
sym_data* sym_data_get(char*);
3031
int glob_data_find(char* name);
3132
extern sym_data* sym_cur;
32-
void* new_Tensor();
3333
#ifdef __cplusplus
3434
}
3535

3636
#include <map>
3737
#include <string>
38-
39-
extern std::map<std::string, sym_data> sym_map;
38+
typedef std::map<std::string, sym_data> scope;
39+
extern scope sym_map;
4040
#endif

src/tensor-array/interp/token.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ void token_next()
236236
src++;
237237
tkn = TOKEN_MATMUL; // Store the token value
238238
return; // Exit after processing the token
239+
case '[':
240+
case ']':
241+
case '(':
242+
case ')':
243+
case '{':
244+
case '}':
245+
case ',':
246+
case ';':
247+
case ':':
248+
return;
239249
default:
240250
if (tkn >= '0' && tkn <= '9')
241251
{
@@ -294,7 +304,7 @@ void token_next()
294304
}
295305
else
296306
{
297-
/* code to handle other tokens */
307+
printf("invalid symbol %c", tkn)
298308
}
299309
break;
300310
}

src/tensor-array/interp/vm.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ typedef enum
2323

2424
typedef long VM_INSTRUCTION;
2525

26-
typedef enum
27-
{
28-
TYPE_STRING,
29-
TYPE_INT,
30-
TYPE_PTR
31-
} VM_TYPE;
32-
3326
void eval();
3427

3528
extern long any_value;

src/tensor-array/interp/vm_type.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifdef __cplusplus
2+
extern "C"
3+
{
4+
#endif
5+
typedef enum
6+
{
7+
TYPE_NONE,
8+
TYPE_STRING,
9+
TYPE_INT,
10+
TYPE_PTR,
11+
TYPE_FUNC
12+
} VM_TYPE;
13+
#ifdef __cplusplus
14+
}
15+
#endif

src/tensor-array/interp/vmop.cc

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@ limitations under the License.
2020
#include <cstring>
2121
#include "sym_map.h"
2222
#include "vmop.h"
23+
#include "vm_type.h"
24+
25+
typedef long VM_INSTRUCTION;
26+
extern VM_INSTRUCTION* pc;
2327

2428
std::stack<tensor_array::value::Tensor> tensor_stack;
25-
std::stack<std::string> ptr_stack;
29+
std::stack<std::string> var_stack;
30+
std::stack<std::pair<long, scope>> call_stack;
2631
tensor_array::value::Tensor ag;
2732
void* aptr;
2833
long any_value;
29-
long any_type;
34+
VM_TYPE any_type;
3035

3136
void new_int()
3237
{
@@ -51,10 +56,21 @@ void new_string()
5156

5257
void op_imm()
5358
{
54-
if (any_type == 0) new_string();
55-
else if (any_type == 1) new_int();
56-
else if (any_type == 2) new_ptr();
57-
else;
59+
switch (any_type)
60+
{
61+
case TYPE_INT:
62+
/* code */
63+
new_int();
64+
break;
65+
case TYPE_STRING:
66+
new_string();
67+
break;
68+
case TYPE_PTR:
69+
new_ptr();
70+
break;
71+
default:
72+
break;
73+
}
5874
}
5975

6076
void op_add()
@@ -212,6 +228,19 @@ void op_shr()
212228
// ag = ag >> bg;
213229
}
214230

231+
void op_call()
232+
{
233+
VM_INSTRUCTION pc1 = (VM_INSTRUCTION)*pc++;
234+
call_stack.push({std::move(pc), std::move(sym_map)});
235+
pc = pc1;
236+
}
237+
238+
void op_ret()
239+
{
240+
[pc, sym_map] = std::move(call_stack.top());
241+
call_stack.pop();
242+
}
243+
215244
void op_open()
216245
{
217246
// Implementation for opening a file or resource
@@ -260,7 +289,7 @@ void op_push()
260289

261290
void op_ptr_push()
262291
{
263-
ptr_stack.push(reinterpret_cast<char*>(aptr));
292+
var_stack.push(reinterpret_cast<char*>(aptr));
264293
std::free(aptr);
265294
}
266295

@@ -274,13 +303,13 @@ void op_get()
274303

275304
void op_set()
276305
{
277-
if (!ptr_stack.empty())
306+
if (!var_stack.empty())
278307
{
279-
std::string& var_name = ptr_stack.top();
308+
std::string& var_name = var_stack.top();
280309
sym_data& temp = sym_map[var_name];
281310
delete temp.data; // Set the top of the stack to ag
282311
temp.data = new tensor_array::value::Tensor(ag);
283-
ptr_stack.pop();
312+
var_stack.pop();
284313
}
285314
else
286315
{

0 commit comments

Comments
 (0)