Skip to content

[C++] Add Logo Programming Language #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ binaries: $(bt_stage1) $(bt_stage2) $(kernel_core) $(rm_static)
SECTOR_COUNT_BT_STAGE1 = 1
SECTOR_COUNT_SHARED_LIBRARY = 1
SECTOR_COUNT_BT_STAGE2 = 12
SECTOR_COUNT_KERNEL = 75
SECTOR_COUNT_KERNEL = 80

SECTOR_START_BT_STAGE1 = 0
SECTOR_START_SHARED_LIBRARY = $(shell expr $(SECTOR_START_BT_STAGE1) + $(SECTOR_COUNT_BT_STAGE1) )
Expand Down
8 changes: 4 additions & 4 deletions src/usr/include/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ void rectangle(int left, int top, int right, int bottom);
void bar(int left, int top, int right, int bottom);
void fillellipse(int xcenter, int ycenter, int x_radius, int y_radius);

int textheight(char *str);
int textwidth(char *str);
void outtext(char *str);
int outtextxy(int x, int y, char *str);
int textheight(const char *str);
int textwidth(const char *str);
void outtext(const char *str);
int outtextxy(int x, int y, const char *str);
void moveto(int x, int y);
int getx();
int gety();
Expand Down
9 changes: 8 additions & 1 deletion src/usr/include/iostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ostream {
public:
ostream& operator<<(const char c);
ostream& operator<<(const int num);
ostream& operator<<(const double num);
ostream& operator<<(const std::string &str);
ostream& operator<<(const char* str);
};
Expand All @@ -19,10 +20,16 @@ class istream {
public:
istream& operator>>(char &c);
istream& operator>>(char *str);
istream& operator>>(int &num);
istream& operator>>(std::string &str);
istream& operator>>(int &x);

istream& get(char &c);
istream& get_line(char *str, std::size_t n, char delim='\n');
};

extern ostream cout;
extern istream cin;

std::istream& get_line(std::istream &i, std::string &str);

} // namespace std end
9 changes: 9 additions & 0 deletions src/usr/include/limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#define LLONG_MAX 9223372036854775807LL
#define LLONG_MIN (-LLONG_MAX-1)
#define ULLONG_MAX ((unsigned long)(((unsigned long)LLONG_MAX)*2ULL+1))

#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX-1)
#define UINT_MAX ((unsigned int)(((unsigned int)INT_MAX)*2U+1))
21 changes: 21 additions & 0 deletions src/usr/include/math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#define M_PI 3.14159265358979323846

#ifdef __cplusplus
extern "C" {
namespace std {
#endif

int isnan(double x);
double floor(double x);
double round(double x);
double fmod(double x, double y);

double sin(double x);
double cos(double x);

#ifdef __cplusplus
} // namespace std end
} // extern C end
#endif
2 changes: 2 additions & 0 deletions src/usr/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ int atoi (const char *s);
// for others the result can be undefined.
// And only base 10 is considered as signed int.
void itoa(int num, char *s, int base);
void ftoa(double num, char *s);
int min(int, int);
int max(int, int);
int abs(int a);

void* malloc(size_t size);
void free(void* ptr);

void atexit(void (*)(void));
void exit(int status);

#ifdef __cplusplus
Expand Down
53 changes: 53 additions & 0 deletions src/usr/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,36 @@ class basic_string {
std::vector<CharT> _data;

public:
const static std::size_t npos = -1;

typedef CharT* iterator;
typedef const CharT* const_iterator;

basic_string();
basic_string(std::size_t n, CharT c);
basic_string(const CharT* str);
basic_string(const CharT* str, std::size_t n);
basic_string(const basic_string<CharT>& o);

iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;

void clear();
void pop_back();
CharT& back();
CharT& front();
CharT& at(std::size_t pos);
CharT& operator[](std::size_t pos);
const CharT *c_str() const;
bool empty() const;
std::size_t length() const;

std::size_t find(char c, std::size_t pos = 0) const;
basic_string<CharT> substr(int start, std::size_t len = npos);

basic_string<CharT>& operator=(const basic_string<CharT>& o);
basic_string<CharT>& operator+=(const CharT c);
basic_string<CharT>& operator+=(const CharT* o);
basic_string<CharT>& operator+=(const basic_string<CharT> &o);
Expand All @@ -63,6 +73,49 @@ using string = basic_string<char>;

} // namespace std end


template<typename CharT>
inline bool operator==(const std::basic_string<CharT> &a, const CharT* b) { return std::strcmp(a.c_str(), b) == 0; }
template<typename CharT>
inline bool operator==(const CharT* a, const std::basic_string<CharT> &b) { return std::strcmp(a, b.c_str()) == 0; }
template<typename CharT>
inline bool operator==(const std::basic_string<CharT> &a, const std::basic_string<CharT> &b) { return std::strcmp(a.c_str(), b.c_str()) == 0; }

template<typename CharT>
inline bool operator!=(const std::basic_string<CharT> &a, const CharT* b) { return !operator==(a,b); }
template<typename CharT>
inline bool operator!=(const CharT* a, const std::basic_string<CharT> &b) { return !operator==(a,b); }
template<typename CharT>
inline bool operator!=(const std::basic_string<CharT> &a, const std::basic_string<CharT> &b) { return !operator==(a,b); }

template<typename CharT>
inline bool operator<(const std::basic_string<CharT> &a, const CharT* b) { return std::strcmp(a.c_str(), b) < 0; }
template<typename CharT>
inline bool operator<(const CharT* a, const std::basic_string<CharT> &b) { return std::strcmp(a, b.c_str()) < 0; }
template<typename CharT>
inline bool operator<(const std::basic_string<CharT> &a, const std::basic_string<CharT> &b) { return std::strcmp(a.c_str(), b.c_str()) < 0; }

template<typename CharT>
inline bool operator>(const std::basic_string<CharT> &a, const CharT* b) { return std::strcmp(a.c_str(), b) > 0; }
template<typename CharT>
inline bool operator>(const CharT* a, const std::basic_string<CharT> &b) { return std::strcmp(a, b.c_str()) > 0; }
template<typename CharT>
inline bool operator>(const std::basic_string<CharT> &a, const std::basic_string<CharT> &b) { return std::strcmp(a.c_str(), b.c_str()) > 0; }

template<typename CharT>
inline bool operator<=(const std::basic_string<CharT> &a, const CharT* b) { return !operator>(a,b); }
template<typename CharT>
inline bool operator<=(const CharT* a, const std::basic_string<CharT> &b) { return !operator>(a,b); }
template<typename CharT>
inline bool operator<=(const std::basic_string<CharT> &a, const std::basic_string<CharT> &b) { return !operator>(a,b); }

template<typename CharT>
inline bool operator>=(const std::basic_string<CharT> &a, const CharT* b) { return !operator<(a,b); }
template<typename CharT>
inline bool operator>=(const CharT* a, const std::basic_string<CharT> &b) { return !operator<(a,b); }
template<typename CharT>
inline bool operator>=(const std::basic_string<CharT> &a, const std::basic_string<CharT> &b) { return !operator<(a,b); }

#include <string.tcc>

#endif
52 changes: 52 additions & 0 deletions src/usr/include/string.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ basic_string<CharT>::basic_string(const CharT* str, std::size_t n) : _data(n+1,
}
}

template <typename CharT>
basic_string<CharT>::basic_string(const basic_string<CharT> &o) : _data(o._data) {}

template <typename CharT>
typename basic_string<CharT>::iterator basic_string<CharT>::begin() { return this->_data.begin(); }

Expand All @@ -41,6 +44,12 @@ typename basic_string<CharT>::const_iterator basic_string<CharT>::begin() const
template <typename CharT>
typename basic_string<CharT>::const_iterator basic_string<CharT>::end() const { return this->_data.end() - 1; }

template <typename CharT>
void basic_string<CharT>::clear() {
this->_data.clear();
this->_data.push_back('\0');
}

template <typename CharT>
void basic_string<CharT>::pop_back() {
if(this->_data.size()>1) {
Expand Down Expand Up @@ -74,6 +83,49 @@ const CharT *basic_string<CharT>::c_str() const {
return this->_data.begin();
}

template <typename CharT>
std::size_t basic_string<CharT>::length() const {
std::size_t sz = this->_data.size();
if(sz == 0) {
return 0; // should not happen
}
return sz - 1;
}

template <typename CharT>
bool basic_string<CharT>::empty() const {
return (this->_data.size() <= 1);
}

template <typename CharT>
std::size_t basic_string<CharT>::find(char c, std::size_t pos) const {
std::size_t len = length();
while (pos < len) {
if (this->_data[pos] == c) {
return pos;
}
pos++;
}
return npos;
}

template <typename CharT>
basic_string<CharT> basic_string<CharT>::substr(int start, std::size_t len) {
std::string sub;
int index = start;
int str_length = length();
for(std::size_t i = 0; i < len && index < str_length; i++, index++) {
sub += this->at(index);
}
return sub;
}

template <typename CharT>
basic_string<CharT>& basic_string<CharT>::operator=(const basic_string<CharT> &o) {
this->_data = o._data;
return *this;
}

template <typename CharT>
basic_string<CharT>& basic_string<CharT>::operator+=(const CharT c) {
this->_data.back() = c; // replace null character
Expand Down
21 changes: 21 additions & 0 deletions src/usr/include/utility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

namespace std {

template<typename T1, typename T2>
class pair {
public:
T1 first;
T2 second;
pair();
pair(const T1 &first, const T2 &second);
};

template<typename T1, typename T2>
inline pair<T1,T2> make_pair(const T1 &a, const T2& b) {
return pair<T1,T2>(a, b);
}

} // namespace std end

#include <utility.tcc>
15 changes: 15 additions & 0 deletions src/usr/include/utility.tcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

// This is internal headerfile to be included by utility.h only

#include <utility.h>

namespace std {

template<typename T1, typename T2>
pair<T1,T2>::pair() {}

template<typename T1, typename T2>
pair<T1,T2>::pair(const T1 &first, const T2 &second) : first(first), second(second) {}

} // namespace std end
14 changes: 9 additions & 5 deletions src/usr/include/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,28 @@ class vector {
typedef T* iterator;
typedef const T* const_iterator;

vector<T>();
vector<T>(std::size_t size, const T &_default);
vector();
vector(std::size_t size, const T &_default);
vector(const vector<T> &o);
vector& operator=(const vector<T> &o);
~vector();

iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;

T *raw_data() const;
bool empty();
std::size_t size();
void clear();
bool empty() const;
std::size_t size() const;
void push_back(const T &val);
void pop_back();

T& back();
T& front();
T& at(std::size_t pos);
T& operator[](std::size_t pos);
const T& operator[](std::size_t pos) const;
};

} // namespace std end
Expand Down
38 changes: 34 additions & 4 deletions src/usr/include/vector.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ vector<T>::vector(std::size_t size, const T &_default): _size(size) {
}
}

template <typename T>
vector<T>::vector(const vector<T> &o) : _size(o._size) {
resize_capacity(o._capacity);
std::memcpy(_data, o._data, _capacity*sizeof(T));
}

template <typename T>
vector<T>& vector<T>::operator=(const vector<T> &o) {
resize_capacity(o._capacity);
_size = o._size;
std::memcpy(_data, o._data, _capacity*sizeof(T));
return *this;
}

template <typename T>
vector<T>::~vector() {
delete[] _data;
}

template <typename T>
typename vector<T>::iterator vector<T>::begin() { return this->_data; }

Expand All @@ -48,21 +67,27 @@ void vector<T>::resize_capacity(std::size_t capacity) {
// and will also make a copy of array and move _data pointer.
// assumes size<=current_capacity and size<=new_capacity
const std::size_t data_size = capacity*sizeof(T);
T *_new_data = (T*) std::malloc(data_size);
T *_new_data = new T[capacity];
if (!_new_data) return; // malloc failed
std::memcpy(_new_data, this->_data, data_size);
std::free(this->_data); // should be no-op if _data == NULL
delete[] this->_data; // should be no-op if _data == NULL
this->_data = _new_data;
this->_capacity = capacity;
}

template <typename T>
bool vector<T>::empty() {
void vector<T>::clear() {
// not reducing capacity for now
this->_size = 0;
}

template <typename T>
bool vector<T>::empty() const {
return this->_size == 0;
}

template <typename T>
std::size_t vector<T>::size() {
std::size_t vector<T>::size() const {
return this->_size;
}

Expand Down Expand Up @@ -102,4 +127,9 @@ T &vector<T>::operator[](std::size_t pos) {
return this->_data[pos];
}

template <typename T>
const T &vector<T>::operator[](std::size_t pos) const {
return this->_data[pos];
}

} // namespace std end
Loading