File tree 11 files changed +146
-8
lines changed
11 files changed +146
-8
lines changed Original file line number Diff line number Diff line change 1
- CC = gcc-12
1
+ CC = clang
2
2
3
- CFLAGS = -g -Wall -Wextra
3
+ CFLAGS = -g -Wall
4
4
5
5
TARGET = cpplox
6
6
@@ -10,5 +10,10 @@ $(TARGET):
10
10
$(CC ) $(CFLAGS ) -o $(TARGET ) .out src/* .c
11
11
12
12
clean :
13
- $(RM ) -f * .out .DS_Store
13
+ $(RM ) -f .DS_Store
14
14
$(RM ) -rf * .dSYM/
15
+ veryclean :
16
+ $(RM ) -f * .out
17
+ $(RM ) -f .DS_Store
18
+ $(RM ) -rf * .dSYM/
19
+
Original file line number Diff line number Diff line change 1
1
# cpplox
2
2
Lox Language implementation in C, code for the bytecode virtual machine
3
+
4
+ ## (TODO)
5
+
6
+ * Mon 2023-03-06 23:29+0530*
7
+ 1 . Implement a run-length encoding algorithm for line checking.
Original file line number Diff line number Diff line change 1
1
#include <stdlib.h>
2
2
#include "chunk.h"
3
3
#include "memory.h"
4
+ /* #include "value.h" */
4
5
/* #include <stdio.h> */
6
+
5
7
void initChunk (Chunk * chunk ) {
6
8
//Initialise chunk
7
9
chunk -> count = 0 ;
Original file line number Diff line number Diff line change 2
2
#ifndef clox_chunk_h
3
3
#define clox_chunk_h
4
4
5
-
6
5
#include "common.h"
7
6
#include "value.h"
7
+
8
8
//Define opcode -> operation code
9
9
//return the kind of opertion that the interpeter is dealing with -> add, subtract etc.
10
10
typedef enum {
11
11
OP_RETURN ,
12
+ OP_NEGATE ,
12
13
OP_CONSTANT
13
14
} OpCode ;
14
15
Original file line number Diff line number Diff line change 4
4
#include <stdbool.h>
5
5
#include <stddef.h>
6
6
#include <stdint.h>
7
+
8
+ #ifdef DEBUG_TRACE_EXECUTION
9
+ printf (" " );
10
+ for (Val * slot = vm .stack ; slot < vm .stack_top ; slot ++ ) {
11
+ printf ("{ " );
12
+ print_val (* slot );
13
+ printf (" }" );
14
+ }
15
+ printf ("\n" );
16
+ disassembleInstruction (vm .chunk , (int )(vm .ip - vm .chunk -> code ));
7
17
//Use bool, size_t, uint8_t
8
18
#endif
19
+ #endif
Original file line number Diff line number Diff line change @@ -41,6 +41,8 @@ int disassembleInstruction(Chunk* chunk, int offset) {
41
41
return simpleInstruction ("OP_RETURN" , offset );
42
42
case OP_CONSTANT :
43
43
return const_instruction ("OP_CONSTANT" , chunk , offset );
44
+ case OP_NEGATE :
45
+ return simpleInstruction ("OP_NEGATE" , offset );
44
46
default :
45
47
printf ("Unknown opcode %d\n" , instruction );
46
48
return offset + 1 ;
Original file line number Diff line number Diff line change 3
3
4
4
#include "chunk.h"
5
5
6
- void disassembleChunk (Chunk * chunk , const char * name );
7
- int disassembleInstruction (Chunk * chunk , int offset );
6
+
7
+ void disassembleChunk (Chunk * chunk , const char * name );
8
+ int disassembleInstruction (Chunk * chunk , int offset );
8
9
9
10
#endif
Original file line number Diff line number Diff line change 1
1
#include "common.h"
2
2
#include "chunk.h"
3
3
#include "debug.h"
4
- int main (int argc , char * argv []) {
4
+ #include "vm.h"
5
+
6
+
7
+ int main () {
8
+ init_vm ();
5
9
Chunk chunk ;
6
10
initChunk (& chunk );
7
11
writeChunk (& chunk , OP_CONSTANT , 123 );
12
+
8
13
int constant = add_const (& chunk , 1.5 );
14
+
9
15
writeChunk (& chunk , constant , 123 );
16
+ writeChunk (& chunk , OP_NEGATE , 123 );
10
17
writeChunk (& chunk , OP_RETURN , 123 );
18
+
11
19
//Dissasemble the chunk!
12
20
disassembleChunk (& chunk , "test chunk" );
21
+
22
+ interpret (& chunk );
23
+ free_vm ();
13
24
freeChunk (& chunk );
14
25
return 0 ;
15
26
}
Original file line number Diff line number Diff line change 1
1
#include <stdlib.h>
2
2
#include "memory.h"
3
3
4
- void * reallocate (void * pointer , size_t oldSize , size_t newSize ){
4
+ void * reallocate (void * pointer , size_t oldSize , size_t newSize ){
5
5
if (newSize == 0 ) {
6
6
free (pointer );
7
7
return NULL ;
Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include "common.h"
3
+ #include "debug.h"
4
+ #include "vm.h"
5
+
6
+ /* Global decl */
7
+ VM vm ;
8
+
9
+ static void reset_stack () {
10
+ vm .stack_top = vm .stack ; //set stack_top to the beginning of the array to indecate it is empty
11
+ }
12
+
13
+ void push (Val value ) {
14
+ * vm .stack_top = value ;
15
+ vm .stack_top ++ ;
16
+ }
17
+
18
+ Val pop () {
19
+ vm .stack_top -- ;
20
+ return * vm .stack_top ;
21
+ }
22
+
23
+ /*
24
+ *The Run function is the core of the VM, the heart that pumps blood to the body
25
+ */
26
+
27
+ void init_vm () {
28
+ reset_stack ();
29
+ }
30
+
31
+
32
+ void free_vm () {
33
+
34
+ }
35
+
36
+
37
+ static interpreted_result run (void ) {
38
+ #define READ_BYTE () (*vm.ip++)
39
+ #define READ_CONSTANT () (vm.chunk->constants.values[READ_BYTE()])
40
+
41
+ for (;;) {
42
+ uint8_t instruction ;
43
+ switch (instruction = READ_BYTE ()) {
44
+ case OP_CONSTANT : {
45
+ Val constant = READ_CONSTANT ();
46
+ print_val (constant );
47
+ printf ("\n" );
48
+ push (constant );
49
+ break ;
50
+ }
51
+
52
+ case OP_NEGATE : push (- pop ()); break ;
53
+
54
+ case OP_RETURN : {
55
+ print_val (pop ());
56
+ printf ("\n" );
57
+ return INTERPRET_OK ;
58
+ }
59
+ }
60
+ }
61
+ #undef READ_BYTE
62
+ #undef READ_CONSTANT
63
+ }
64
+
65
+
66
+ interpreted_result interpret (Chunk * chunk ) {
67
+ vm .chunk = chunk ;
68
+ vm .ip = vm .chunk -> code ;
69
+ return run ();
70
+ }
71
+
Original file line number Diff line number Diff line change
1
+ #ifndef clox_vm_h
2
+ #define clox_vm_h
3
+
4
+ #include "chunk.h"
5
+ #include "value.h"
6
+
7
+ #define STACK_MAX 256 //Are 8 bytes enough?
8
+
9
+ typedef struct {
10
+ Chunk * chunk ;
11
+ uint8_t * ip ;
12
+ Val stack [STACK_MAX ]; //My stack based proglang!
13
+ Val * stack_top ;
14
+ } VM ;
15
+
16
+ typedef enum {
17
+ INTERPRET_OK ,
18
+ INTERPRET_COMPILE_ERROR ,
19
+ INTERPRET_RUNTIME_ERROR
20
+ } interpreted_result ;
21
+
22
+ void init_vm ();
23
+ void free_vm ();
24
+ interpreted_result interpret (Chunk * chunk );
25
+
26
+ void push (Val value );
27
+ Val pop ();
28
+
29
+ #endif
You can’t perform that action at this time.
0 commit comments