Skip to content

Commit c94bd86

Browse files
committed
initial commit
nothing works
0 parents  commit c94bd86

File tree

8 files changed

+196
-0
lines changed

8 files changed

+196
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Machine (that is) Virtual (and made in) C - MVC
2+
3+
https://www.geeksforgeeks.org/basics-file-handling-c/
4+
5+
https://www.geeksforgeeks.org/c-program-to-read-contents-of-whole-file/

build/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
default:
2+
gcc ../src/*.c -I ../include/ -o cvm -O3

build/test.neo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ADD 0 RA 32 33
2+
PUT 0 RA

include/buffers.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef BUFFERS_H
2+
#define BUFFERS_H
3+
4+
#include "types.h"
5+
6+
typedef struct {
7+
i8 *buffer;
8+
u64 size;
9+
u64 index;
10+
} CharBuffer;
11+
12+
CharBuffer * newCharBuffer(u64 size);
13+
14+
inline void clearCharBuffer(CharBuffer *buffer);
15+
16+
inline void pushCharBuffer(CharBuffer *buffer, i8 ch);
17+
18+
#endif

include/machine.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef MACHINE_H
2+
#define MACHINE_H
3+
4+
#include "types.h"
5+
6+
// ==================== Constants ====================
7+
8+
#define INSTRUCTIONS_MAX_SIZE 1024
9+
#define STACK_MAX_SIZE 1024
10+
#define HEAP_MAX_SIZE 1024
11+
12+
13+
// ==================== Operations ====================
14+
15+
typedef enum {
16+
OP_PUSH,
17+
OP_POP,
18+
OP_MOV,
19+
OP_ADD,
20+
OP_SUB,
21+
OP_MUL,
22+
OP_DIV,
23+
OP_POW,
24+
OP_PUT,
25+
OP_GET,
26+
OP_JMPZ,
27+
OP_JMPL,
28+
OP_JMP,
29+
OP_ALC,
30+
OP_FREE,
31+
OP_HALT,
32+
} Operations; // 4 bits
33+
34+
typedef struct {
35+
Operations type;
36+
u8 mode;
37+
38+
// An instruction can have 3 values (e.g.: `ADD 0 RA, 2, 2` or `3 0 0 2 2` or ADD into Register A the sum of 2 and 2)
39+
u64 value1;
40+
u64 value2;
41+
u64 value3;
42+
} Instruction; // 4 bits + 4 bits + 3 * 8 bytes = 25 bytes
43+
44+
typedef struct {
45+
Instruction instruction_list[INSTRUCTIONS_MAX_SIZE]; // Instructions (.code or .text memory area)
46+
u64 stack[STACK_MAX_SIZE]; // stack memory area
47+
u64 heap[HEAP_MAX_SIZE]; // heap memory area
48+
49+
// Registers
50+
u64 stack_pointer;
51+
u64 current_instruction;
52+
u64 last_operation_value;
53+
u64 register_A;
54+
u64 register_B;
55+
u64 register_C;
56+
} Machine;
57+
58+
#endif

include/types.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef TYPES_H
2+
#define TYPES_H
3+
4+
#define i8 char
5+
#define i16 short
6+
#define i32 int
7+
#define i64 long
8+
9+
#define u8 unsigned char
10+
#define u16 unsigned short
11+
#define u32 unsigned int
12+
#define u64 unsigned long
13+
14+
#define f32 float
15+
#define f64 double
16+
17+
#define x32 unsigned float
18+
#define x64 unsigned double
19+
20+
#endif

src/buffers.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "buffers.h"
2+
3+
CharBuffer * newCharBuffer(u64 size) {
4+
CharBuffer *new = (CharBuffer*) malloc(sizeof(CharBuffer));
5+
new->buffer = (i8*) malloc(sizeof(i8) * size);
6+
new->size = size;
7+
new->index = 0;
8+
9+
return new;
10+
}
11+
12+
inline void clearCharBuffer(CharBuffer *buffer) {
13+
for (u64 i = 0; i < buffer->size; i++)
14+
buffer->buffer[i] = 0;
15+
buffer->index = 0;
16+
}
17+
18+
inline void pushCharBuffer(CharBuffer *buffer, i8 ch) {
19+
buffer->buffer[buffer->index++] = ch;
20+
}

src/main.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "types.h"
5+
#include "buffers.h"
6+
#include "machine.h"
7+
8+
9+
i32 compile(i8 *file_path)
10+
{
11+
FILE *in_file;
12+
FILE *out_file;
13+
14+
in_file = fopen(file_path, "r");
15+
out_file = fopen("main.ceo", "wb");
16+
17+
if (in_file == NULL || out_file == NULL)
18+
return 1;
19+
20+
//
21+
CharBuffer * buffer = newCharBuffer(256);
22+
23+
u64 current_stage = 0;
24+
i8 current_char = 0;
25+
26+
while ((current_char = fgetc(in_file)) != EOF) {
27+
switch(current_char) {
28+
case 10: // New line
29+
current_stage = 0;
30+
clearCharBuffer(buffer);
31+
break;
32+
33+
case 32: // Space
34+
switch (current_stage) {
35+
case 0: // Selecting the operation
36+
37+
current_stage++;
38+
break;
39+
40+
case 1:
41+
break;
42+
43+
default:
44+
break;
45+
}
46+
47+
clearCharBuffer(buffer);
48+
break;
49+
50+
default:
51+
pushCharBuffer(buffer, current_char);
52+
}
53+
}
54+
55+
//
56+
fclose(in_file);
57+
fclose(out_file);
58+
free(buffer);
59+
60+
return 0;
61+
}
62+
63+
64+
i32 main(i32 argc, i8 *argv[])
65+
{
66+
if (argc == 3)
67+
if (!strcmp(argv[1], "--compile") || !strcmp(argv[1], "-c"))
68+
return compile(argv[2]);
69+
70+
return 0;
71+
}

0 commit comments

Comments
 (0)