Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed May 24, 2022
0 parents commit 373a4ff
Show file tree
Hide file tree
Showing 27 changed files with 20,301 additions and 0 deletions.
Empty file added .gitmodules
Empty file.
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.22)
project(typescript)

set(CMAKE_CXX_STANDARD 20)

set(CMAKE_CXX_FLAGS "-Wall -Wextra -O2")
list(APPEND CMAKE_MODULE_PATH libs/)

#add_definitions(-DTRACY_ENABLE)
#include_directories(libs/tracy/)

add_subdirectory(src)

include_directories(libs)

add_executable(typescript_main main.cpp)

target_link_libraries(
typescript_main
typescript
)
Empty file added libs/.gitkeep
Empty file.
1,380 changes: 1,380 additions & 0 deletions libs/magic_enum.hpp

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
#include "src/utf.h"

int main() {
std::string text = "今天12";

for (int i = 0; i < text.length();) {
auto p = ts::charCodeAt(text, i);
std::cout << p.code << std::endl;
i += p.length;
}

return 0;
}
10 changes: 10 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.22)
project(typescript)

add_definitions(-DTRACY_ENABLE)

add_subdirectory(tests)

#add_library(typescript parser.cpp scanner.cpp utf.cpp syntax_cursor.cpp syntax_cursor.h ast.h parser2.h)
add_library(typescript scanner.cpp utf.cpp syntax_cursor.cpp syntax_cursor.h ast.h parser2.h types.cpp factory.h node_test.h)
#add_library(typescript scanner.cpp utf.cpp syntax_cursor.cpp syntax_cursor.h ast.h)
515 changes: 515 additions & 0 deletions src/ast.h

Large diffs are not rendered by default.

104 changes: 104 additions & 0 deletions src/core.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#pragma once

#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <functional>
#include <optional>

namespace ts {
using namespace std;

inline string trimStringStart(string s) {
while (s.compare(0, 1, " ") == 0) s.erase(s.begin());
return s;
}

inline string trimStringEnd(string s) {
while (s.size() > 0 && s.compare(s.size() - 1, 1, " ") == 0) s.erase(s.end() - 1);
return s;
}

template<typename T>
inline bool some(optional<vector<T>> array, optional<std::function<bool(typename decltype(array)::value_type::value_type)>> predicate) {
//inline bool some(vector<any> array, std::function<void(T)> predicate) {
//inline bool some(optional<vector<T>> array) {
if (array) {
if (predicate) {
for (auto &v: (*array)) {
if ((*predicate)(v)) {
return true;
}
}
} else {
return (*array).size() > 0;
}
}
return false;
};

//inline bool some(optional<T> array, std::function<void(typename decltype(data)::value_type::value_type)> predicate) {
template<typename T>
inline bool some(vector<T> array, std::function<bool(typename decltype(array)::value_type)> predicate) {
return some(optional(array), optional(predicate));
}

template<typename T>
class LogicalOrReturnLast {
protected:
T value;
public:
LogicalOrReturnLast(T value) : value(value) {}
operator T() { return value; }
LogicalOrReturnLast operator||(LogicalOrReturnLast other) {
if (value) return *this;

return other;
}
LogicalOrReturnLast operator||(T other) {
if (value) return *this;

return LogicalOrReturnLast(other);
}
};

//template<typename T>
//inline bool some(ts::NodeArray<T> array, std::function<bool(typename decltype(array)::value_type::value_type)> predicate) {
// return some(optional(array.list), optional(predicate));
//}

/**
* Filters vector by filter in-place.
*/
template<typename T, typename Func>
static void remove(vector<T> &vector, const Func &filter) {
auto new_end = remove_if(vector.begin(), vector.end(), filter);
vector.erase(new_end, vector.end());
};

template<typename T>
static void remove(vector<T> &vector, T item) {
vector.erase(remove(vector.begin(), vector.end(), item), vector.end());
};

template<typename T>
static bool has(vector<T> &vector, T item) {
return find(vector.begin(), vector.end(), item) != vector.end();
};

template<typename T>
static bool has(const vector<T> &vector, T item) {
return find(vector.begin(), vector.end(), item) != vector.end();
};

template<typename T, typename U>
static bool has(unordered_map<T, U> &map, T key) {
return map.find(key) != map.end();
};

template<typename T, typename U>
static bool has(map<T, U> &map, T key) {
return map.find(key) != map.end();
};
}
Loading

0 comments on commit 373a4ff

Please sign in to comment.