8000 feat: binary file parser · m0dulo/LyxPythonVM@72a023e · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 72a023e

Browse files
author
m0dulo
committed
feat: binary file parser
1 parent cd01248 commit 72a023e

16 files changed

+495
-267
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/.vscode
2-
/build/
3-
/bin/
1+
/.vscode
2+
/build/
3+
/bin/
44
/test/*.pyc

CMakeLists.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
cmake_minimum_required(VERSION 3.0)
2-
project(LyxPythonVM)
3-
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
4-
if(CMAKE_BUILD_TYPE MATCHES Debug)
5-
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -w -msse3 -funroll-loops -std=c++11 -O0 -pg" )
6-
else()
7-
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -funroll-loops -w -std=c++11 -O2 -march=native" )
8-
endif()
9-
set(SRC ${PROJECT_SOURCE_DIR}/src/main.cc)
1+
cmake_minimum_required(VERSION 3.0)
2+
project(LyxPythonVM)
3+
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
4+
if(CMAKE_BUILD_TYPE MATCHES Debug)
5+
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -w -msse3 -funroll-loops -std=c++11 -O0 -pg" )
6+
else()
7+
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -funroll-loops -w -std=c++11 -O2 -march=native" )
8+
endif()
9+
set(SRC ${PROJECT_SOURCE_DIR}/src/main.cpp)
1010
add_executable(main ${SRC})

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LyxPythonVM
2-
🐍 Python Virtual Machine from Scratch
3-
👨‍💻 Currently in development
1+
# LyxPythonVM
2+
🐍 Python Virtual Machine from Scratch
3+
👨‍💻 Currently in development
44
If you have any question about LyxPythonVM, feel free to post an issue or send me an email: <yuhsin.lyx@gmail.com>

src/BufferedInputStream.hpp

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
#ifndef BUFFERED_INPUT_STREAM_HPP_
2-
#define BUFFERED_INPUT_STREAM_HPP_
3-
4-
#include <stdio.h>
5-
6-
#define BUFFER_LEN 256
7-
8-
class BufferedInputStraem {
9-
private:
10-
FILE *fp;
11-
char szBuffer[BUFFER_LEN];
12-
unsigned short index;
13-
14-
public:
15-
BufferedInputStraem(char const *filename) {
16-
fp = fopen(filename, "rb");
17-
fread(szBuffer, BUFFER_LEN * sizeof(char), 1, fp);
18-
index = 0;
19-
}
20-
21-
~BufferedInputStraem() { close(); }
22-
23-
char read() {
24-
if (index < BUFFER_LEN)
25-
return szBuffer[index++];
26-
else {
27-
index = 0;
28-
fread(szBuffer, BUFFER_LEN * sizeof(char), 1, fp);
29-
return szBuffer[index++];
30-
}
31-
}
32-
33-
int read_init() {
34-
int b1 = read() & 0xff;
35-
int b2 = read() & 0xff;
36-
int b3 = read() & 0xff;
37-
int b4 = read() & 0xff;
38-
39-
return b4 << 24 | b3 << 16 | b2 << 8 | b1;
40-
}
41-
42-
void unread() { index--; }
43-
44-
void close() {
45-
if (fp != NULL) {
46-
fclose(fp);
47-
fp = NULL;
48-
}
49-
}
50-
};
1+
#ifndef BUFFERED_INPUT_STREAM_HPP_
2+
#define BUFFERED_INPUT_STREAM_HPP_
3+
4+
#include <stdio.h>
5+
6+
#define BUFFER_LEN 256
7+
8+
class BufferedInputStream {
9+
private:
10+
FILE *fp;
11+
char szBuffer[BUFFER_LEN];
12+
unsigned short index;
13+
14+
public:
15+
BufferedInputStream(char const *filename) {
16+
fp = fopen(filename, "rb");
17+
fread(szBuffer, BUFFER_LEN * sizeof(char), 1, fp);
18+
index = 0;
19+
}
20+
21+
~BufferedInputStream() { close(); }
22+
23+
char read() {
24+
if (index < BUFFER_LEN)
25+
return szBuffer[index++];
26+
else {
27+
index = 0;
28+
fread(szBuffer, BUFFER_LEN * sizeof(char), 1, fp);
29+
return szBuffer[index++];
30+
}
31+
}
32+
33+
int read_init() {
34+
int b1 = read() & 0xff;
35+
int b2 = read() & 0xff;
36+
int b3 = read() & 0xff;
37+
int b4 = read() & 0xff;
38+
39+
return b4 << 24 | b3 << 16 | b2 << 8 | b1;
40+
}
41+
42+
void unread() { index--; }
43+
44+
void close() {
45+
if (fp != NULL) {
46+
fclose(fp);
47+
fp = NULL;
48+
}
49+
}
50+
};
5151
#endif // BUFFERED_INPUT_STREAM_HPP_

src/arrayList.cpp

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,61 @@
1-
#include "arrayList.hpp"
2-
#include <stdio.h>
3-
4-
template <typename T> ArrayList<T>::ArrayList(int n) {
5-
_length = n;
6-
_size = 0;
7-
_array = new T[n];
8-
}
9-
10-
template <typename T> void ArrayList<T>::add(T t) {
11-
if (_size >= _length)
12-
expand();
13-
14-
_array[_size++] = t;
15-
}
16-
17-
template <typename T> void ArrayList<T>::insert(int index, T t) {
18-
add(NULL);
19-
20-
for (int i = _size; i > index; i--) {
21-
_array[i] = _array[i - 1];
22-
}
23-
24-
_array[index] = t;
25-
}
26-
27-
template <typename T> void ArrayList<T>::expand() {
28-
T *new_array = new T[_length << 1];
29-
for (int i = 0; i < _length; i++) {
30-
new_array[i] = _array[i];
31-
}
32-
_array = new_array;
33-
delete[] _array;
34-
35-
_length <<= 1;
36-
printf("expand an array to %d, size is %d\n", _length, _size);
37-
}
38-
39-
template <typename T> int ArrayList<T>::size() { return _size; }
40-
41-
template <typename T> int ArrayList<T>::length() { return _length; }
42-
43-
template <typename T> T ArrayList<T>::get(int index) { return _array[index]; }
44-
45-
template <typename T> void ArrayList<T>::set(int index, T t) {
46-
if (_size <= index)
47-
_size = index + 1;
48-
49-
while (_size > _length)
50-
expand();
51-
52-
_array[index] = t;
53-
}
54-
55-
template <typename T> T ArrayList<T>::pop() { return _array[--_size]; }
56-
class LyxObject;
57-
template class ArrayList<LyxObject*>;
58-
59-
class LyxString;
60-
template class ArrayList<LyxString*>;
1+
#include "arrayList.hpp"
2+
#include <stdio.h>
3+
4+
template <typename T> ArrayList<T>::ArrayList(int n) {
5+
_length = n;
6+
_size = 0;
7+
_array = new T[n];
8+
}
9+
10+
template <typename T> void ArrayList<T>::add(T t) {
11+
if (_size >= _length)
12+
expand();
13+
14+
_array[_size++] = t;
15+
}
16+
17+
template <typename T> void ArrayList<T>::insert(int index, T t) {
18+
add(NULL);
19+
20+
for (int i = _size; i > index; i--) {
21+
_array[i] = _array[i - 1];
22+
}
23+
24+
_array[index] = t;
25+
}
26+
27+
template <typename T> void ArrayList<T>::expand() {
28+
T *new_array = new T[_length << 1];
29+
for (int i = 0; i < _length; i++) {
30+
new_array[i] = _array[i];
31+
}
32+
_array = new_array;
33+
delete[] _array;
34+
35+
_length <<= 1;
36+
printf("expand an array to %d, size is %d\n", _length, _size);
37+
}
38+
39+
template <typename T> int ArrayList<T>::size() { return _size; }
40+
41+
template <typename T> int ArrayList<T>::length() { return _length; }
42+
43+
template <typename T> T ArrayList<T>::get(int index) { return _array[index]; }
44+
45+
template <typename T> void ArrayList<T>::set(int index, T t) {
46+
if (_size <= index)
47+
_size = index + 1;
48+
49+
while (_size > _length)
50+
expand();
51+
52+
_array[index] = t;
53+
}
54+
55+
template <typename T> T ArrayList<T>::pop() { return _array[--_size]; }
56+
57+
class LyxObject;
58+
template class ArrayList<LyxObject *>;
59+
60+
class LyxString;
61+
template class ArrayList<LyxString *>;

src/arrayList.hpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
#ifndef ARRAY_LIST_HPP
2-
#define ARRAY_LIST_HPP
3-
4-
#include <stdio.h>
5-
6-
template <typename T> class ArrayList {
7-
private:
8-
int _length;
9-
T *_array;
10-
int _size;
11-
12-
void expand();
13-
14-
public:
15-
ArrayList(int n = 8);
16-
17-
void add(T t);
18-
void insert(int index, T t);
19-
T get(int index);
20-
void set(int index, T t);
21-
int size();
22-
int length();
23-
T pop();
24-
};
25-
1+
#ifndef ARRAY_LIST_HPP
2+
#define ARRAY_LIST_HPP
3+
4+
#include <stdio.h>
5+
6+
template <typename T> class ArrayList {
7+
private:
8+
int _length;
9+
T *_array;
10+
int _size;
11+
12+
void expand();
13+
14+
public:
15+
ArrayList(int n = 8);
16+
17+
void add(T t);
18+
void insert(int index, T t);
19+
T get(int index);
20+
void set(int index, T t);
21+
int size();
22+
int length();
23+
T pop();
24+
};
25+
2626
#endif // ARRAY_LIST_HPP

0 commit comments

Comments
 (0)
0