Skip to content

Commit

Permalink
first debugger functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Jun 22, 2022
1 parent 6192879 commit bf2b724
Show file tree
Hide file tree
Showing 20 changed files with 924 additions and 190 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "libs/fmt"]
path = libs/fmt
url = [email protected]:fmtlib/fmt.git
[submodule "libs/imgui"]
path = libs/imgui
url = [email protected]:ocornut/imgui.git
1 change: 1 addition & 0 deletions libs/imgui
Submodule imgui added at 9aae45
33 changes: 28 additions & 5 deletions libs/imgui-texteditor-fork/TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ TextEditor::TextEditor()
SetPalette(GetDarkPalette());
SetLanguageDefinition(LanguageDefinition::HLSL());
mLines.push_back(Line());
inlineErrorHover = [](auto ...) {};
}

TextEditor::~TextEditor()
Expand Down Expand Up @@ -882,6 +883,7 @@ void TextEditor::Render()
auto scrollY = ImGui::GetScrollY();

auto lineNo = (int)floor(scrollY / mCharAdvance.y);
auto lineStart = (int)floor(scrollY / mCharAdvance.y);
auto globalLineMax = (int)mLines.size();
auto lineMax = std::max(0, std::min((int)mLines.size() - 1, lineNo + (int)floor((scrollY + contentSize.y) / mCharAdvance.y)));

Expand Down Expand Up @@ -1009,7 +1011,6 @@ void TextEditor::Render()
}
}


// Render colorized text
auto prevColor = line.empty() ? mPalette[(int)PaletteIndex::Default] : GetGlyphColor(line[0]);
ImVec2 bufferOffset;
Expand Down Expand Up @@ -1081,9 +1082,31 @@ void TextEditor::Render()
++lineNo;
}

// Draw a tooltip on known identifiers/preprocessor symbols
// Render inline errors
if (true) {
for (auto &&inlineError: inlineErrors) {
ImVec2 lineStartScreenPos = ImVec2(cursorScreenPos.x, cursorScreenPos.y + inlineError.line * mCharAdvance.y);
auto start = ImVec2(lineStartScreenPos.x + mTextStart + scrollX + (mCharAdvance.x * inlineError.charPos), lineStartScreenPos.y + mCharAdvance.y);
auto end = ImVec2(lineStartScreenPos.x + mTextStart + scrollX + (mCharAdvance.x * inlineError.charEnd), lineStartScreenPos.y + mCharAdvance.y);
drawList->AddLine(start, end, mPalette[(int) PaletteIndex::ErrorMarker], 2);
}
}

// Draw a tooltip on known identifiers/preprocessor symbols
if (ImGui::IsMousePosValid())
{
auto mousePos = ImGui::GetMousePos();
for (auto &&inlineError: inlineErrors) {
if (inlineError.line < lineStart || inlineError.line > lineMax) continue;

ImVec2 lineStartScreenPos = ImVec2(cursorScreenPos.x, cursorScreenPos.y + inlineError.line * mCharAdvance.y);
auto start = ImVec2(lineStartScreenPos.x + mTextStart + scrollX + (mCharAdvance.x * inlineError.charPos), lineStartScreenPos.y);
auto end = ImVec2(lineStartScreenPos.x + mTextStart + scrollX + (mCharAdvance.x * inlineError.charEnd), lineStartScreenPos.y + mCharAdvance.y);
if (ImGui::IsMouseHoveringRect(start, end)) {
inlineErrorHover(start, end, inlineError);
}
}

auto id = GetWordAt(ScreenPosToCoordinates(ImGui::GetMousePos()));
if (!id.empty())
{
Expand Down Expand Up @@ -2008,7 +2031,7 @@ const TextEditor::Palette & TextEditor::GetDarkPalette()
{
const static Palette p = { {
0xff7f7f7f, // Default
0xffd69c56, // Keyword
0xffd69c56, // Keyword
0xff00ff00, // Number
0xff7070e0, // String
0xff70a0e0, // Char literal
Expand Down Expand Up @@ -2036,7 +2059,7 @@ const TextEditor::Palette & TextEditor::GetLightPalette()
{
const static Palette p = { {
0xff7f7f7f, // None
0xffff0c06, // Keyword
0xffff0c06, // Keyword
0xff008000, // Number
0xff2020a0, // String
0xff304070, // Char literal
Expand Down Expand Up @@ -2064,7 +2087,7 @@ const TextEditor::Palette & TextEditor::GetRetroBluePalette()
{
const static Palette p = { {
0xff00ffff, // None
0xffffff00, // Keyword
0xffffff00, // Keyword
0xff00ff00, // Number
0xff808000, // String
0xff808000, // Char literal
Expand Down
9 changes: 9 additions & 0 deletions libs/imgui-texteditor-fork/TextEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ class TextEditor
typedef std::vector<Glyph> Line;
typedef std::vector<Line> Lines;

struct InlineError {
int line;
int charPos;
int charEnd;
void *data = nullptr;
};

struct LanguageDefinition
{
typedef std::pair<std::string, PaletteIndex> TokenRegexString;
Expand Down Expand Up @@ -266,6 +273,8 @@ class TextEditor
static const Palette& GetLightPalette();
static const Palette& GetRetroBluePalette();

std::vector<InlineError> inlineErrors;
std::function<void(ImVec2&, ImVec2&, InlineError&)> inlineErrorHover;
private:
typedef std::vector<std::pair<std::regex, PaletteIndex>> RegexList;

Expand Down
38 changes: 23 additions & 15 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ bool exists(const string &file)
return infile.good();
}

void run(const string &bytecode, const string &code, const string &fileName) {
vm::VM vm;
vm.run(bytecode, code, fileName);
vm.printErrors();
}

void compileAndRun(const string &code, const string &file, const string &fileName) {
auto bytecode = file + ".tsbytecode";
auto buffer = readFile(file);
checker::Compiler compiler;
Parser parser;
auto result = parser.parseSourceFile(file, buffer, ts::types::ScriptTarget::Latest, false, ScriptKind::TS, {});
auto program = compiler.compileSourceFile(result);
auto bin = program.build();
writeFile(bytecode, bin);
vm::VM vm;
vm.run(bin, code, fileName);
vm.printErrors();
}

int main(int argc, char *argv[]) {
std::string file = "/Users/marc/bude/typescript-cpp/tests/basicError1.ts";
auto cwd = std::filesystem::current_path();
Expand All @@ -41,25 +61,13 @@ int main(int argc, char *argv[]) {
}

auto code = readFile(file);
auto bytecode = file + ".tsb";
auto relative = std::filesystem::relative(file, cwd);

auto bytecode = file + ".tsbytecode";
if (exists(bytecode)) {
auto buffer = readFile(bytecode);
vm::VM vm;
vm.run(buffer, code, relative.string());
vm.printErrors();
run(readFile(bytecode), code, relative.string());
} else {
auto buffer = readFile(file);
checker::Compiler compiler;
Parser parser;
auto result = parser.parseSourceFile(file, buffer, ts::types::ScriptTarget::Latest, false, ScriptKind::TS, {});
auto program = compiler.compileSourceFile(result);
auto bin = program.build();
writeFile(bytecode, bin);
vm::VM vm;
vm.run(bin, code, relative.string());
vm.printErrors();
compileAndRun(code, file, relative.string());
}
return 0;
}
6 changes: 4 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ add_subdirectory(tests)
add_library(typescript utf.h utf.cpp core.h core.cpp utilities.h utilities.cpp node_test.h node_test.cpp
syntax_cursor.h syntax_cursor.cpp parser2.h parser2.cpp types.h types.cpp path.h path.cpp
factory.h factory.cpp parenthesizer.h parenthesizer.cpp scanner.h scanner.cpp
checker/vm.h checker/vm.cpp checker/instructions.h checker/compiler.h checker/type_objects.h checker/utils.h checker/checks.h checker/debug.h)
checker/vm.h checker/vm.cpp checker/instructions.h checker/compiler.h checker/types.h checker/utils.h checker/checks.h checker/debug.h)
# ${CMAKE_CURRENT_SOURCE_DIR}/../libs/tracy/TracyClient.cpp

target_link_libraries(typescript fmt)
target_link_libraries(typescript fmt)

add_subdirectory(gui)
48 changes: 39 additions & 9 deletions src/checker/checks.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "./type_objects.h"
#include "./types.h"
#include "../core.h"

namespace ts::vm {
Expand All @@ -17,10 +17,14 @@ namespace ts::vm {
sharedOpt<Type> findMember(const vector<shared<Type>> &members, const string_view &name) {
for (auto &&member: members) {
switch (member->kind) {
case TypeKind::MethodSignature: if (to<TypeMethodSignature>(member)->name == name) return member; break;
case TypeKind::Method: if (to<TypeMethod>(member)->name == name) return member; break;
case TypeKind::PropertySignature: if (to<TypePropertySignature>(member)->name == name) return member; break;
case TypeKind::Property: if (to<TypeProperty>(member)->name == name) return member; break;
case TypeKind::MethodSignature: if (to<TypeMethodSignature>(member)->name == name) return member;
break;
case TypeKind::Method: if (to<TypeMethod>(member)->name == name) return member;
break;
case TypeKind::PropertySignature: if (to<TypePropertySignature>(member)->name == name) return member;
break;
case TypeKind::Property: if (to<TypeProperty>(member)->name == name) return member;
break;
}
}
return nullptr;
Expand All @@ -32,8 +36,8 @@ namespace ts::vm {
}

struct StackEntry {
shared <Type> left;
shared <Type> right;
shared<Type> left;
shared<Type> right;
};

struct ExtendableStack {
Expand Down Expand Up @@ -184,7 +188,34 @@ namespace ts::vm {
return stack.failed();
}
case TypeKind::Literal: {
return to<TypeLiteral>(left)->type == to<TypeLiteral>(right)->type && to<TypeLiteral>(left)->text() == to<TypeLiteral>(right)->text() ? stack.valid() : stack.failed();
if (left->kind == TypeKind::Literal) {
return to<TypeLiteral>(left)->type == to<TypeLiteral>(right)->type && to<TypeLiteral>(left)->text() == to<TypeLiteral>(right)->text() ? stack.valid() : stack.failed();
}
return stack.failed();
}
case TypeKind::Union: {
if (left->kind != TypeKind::Union) {
for (auto &&l: to<TypeUnion>(right)->types) {
if (isExtendable(left, l, stack)) return stack.valid();
}
return false;
} else {
//e.g.: string|number = string|boolean
auto rightTypes = to<TypeUnion>(right)->types;
auto leftTypes = to<TypeUnion>(left)->types;

for (auto &&r: leftTypes) {
bool valid = false;
for (auto &&l: rightTypes) {
if (isExtendable(l, r, stack)) {
valid = true;
break;
};
}
if (!valid) return stack.failed();
}
return stack.valid();
}
}
}

Expand Down Expand Up @@ -240,5 +271,4 @@ namespace ts::vm {
ExtendableStack stack;
return isExtendable(left, right, stack);
}

}
Loading

0 comments on commit bf2b724

Please sign in to comment.