Skip to content

Commit

Permalink
template literal and tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Jul 2, 2022
1 parent ba0a041 commit c2feeb2
Show file tree
Hide file tree
Showing 19 changed files with 1,008 additions and 402 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ project(typescript)
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
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/instructions.h checker/compiler.h checker/types.h checker/utils.h checker/checks.h checker/debug.h checker/vm2.cpp)
# ${CMAKE_CURRENT_SOURCE_DIR}/../libs/tracy/TracyClient.cpp
Expand Down
99 changes: 99 additions & 0 deletions src/checker/check2.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,110 @@ namespace ts::vm2 {
inline unsigned int depth;
}

inline Type *findMember(TypeRef *start, uint64_t hash) {
auto current = start;
while (current) {
if (current->type->hash == hash) return current->type;
current = current->next;
}
return nullptr;
}

/**
* `left extends right ? true : false`
*/
bool extends(Type *left, Type *right) {
switch (right->kind) {
case TypeKind::TupleMember: {
if (left->kind != TypeKind::TupleMember) return false;
//todo: handle optional
if (!extends((Type *)left->type, (Type *)right->type)) return false;

return true;
}
case TypeKind::Tuple: {
switch (left->kind) {
case TypeKind::Tuple: {
//todo: comparing tuple is much more complex than that

auto rightCurrent = (TypeRef *) right->type;
auto leftCurrent = (TypeRef *) left->type;
while (rightCurrent) {
if (!extends(leftCurrent->type, rightCurrent->type)) return false;

rightCurrent = rightCurrent->next;
leftCurrent = leftCurrent->next;
if (rightCurrent && !leftCurrent) return false;
if (!rightCurrent && leftCurrent) return false;
}
return true;
}
case TypeKind::Array: {
auto elementType = (Type *) left->type;
if (elementType->kind == TypeKind::Any) return true;

auto current = (TypeRef *) right->type;
while (current) {
if (!extends(elementType, current->type)) return false;
current = current->next;
}
return true;
}
}

return false;
}
case TypeKind::Array: {
switch (left->kind) {
case TypeKind::Array: {
return extends((Type *) left->type, (Type *) right->type);
}
case TypeKind::Tuple: {
auto elementType = (Type *) right->type;
if (elementType->kind == TypeKind::Any) return true;

auto current = (TypeRef *) left->type;
while (current) {
//current->type is TupleMember
if (!extends((Type *) current->type->type, elementType)) return false;
current = current->next;
}
return true;
}
}
return false;
}
case TypeKind::PropertySignature: {
switch (left->kind) {
case TypeKind::PropertySignature: {
return true;
}
}
return false;
}
case TypeKind::ObjectLiteral: {
switch (left->kind) {
case TypeKind::ObjectLiteral: {
auto rightCurrent = (TypeRef *) right->type;
auto leftStart = (TypeRef *) left->type;

while (rightCurrent) {
switch (rightCurrent->type->kind) {
// case TypeKind::PropertySignature:
case TypeKind::PropertySignature: {
auto found = findMember(leftStart, rightCurrent->type->hash);
if (!found) return false;
if (!extends((Type *) found->type, (Type *) rightCurrent->type->type)) return false;
}
}
rightCurrent = rightCurrent->next;
}

return true;
}
}
return false;
}
case TypeKind::Union: {
auto current = (TypeRef *) right->type;
while (current) {
Expand Down
2 changes: 1 addition & 1 deletion src/checker/checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace ts::vm {
right = to<TypeParameter>(right)->type;
}
profiler.compare(left, right);
return true;
// return true;

switch (right->kind) {
case TypeKind::ObjectLiteral: {
Expand Down
7 changes: 4 additions & 3 deletions src/checker/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,15 +598,16 @@ namespace ts::checker {
case types::SyntaxKind::TypeParameter: {
const auto n = to<TypeParameterDeclaration>(node);
auto &symbol = program.pushSymbol(n->name->escapedText, SymbolType::TypeVariable, n);
program.pushOp(instructions::TypeArgument, n->name);
if (n->defaultType) {
program.pushSubroutineNameLess(n->defaultType);
handle(n->defaultType, program);
auto routine = program.popSubroutine();
program.pushOp(instructions::TypeArgumentDefault);
program.pushOp(instructions::TypeArgumentDefault, n->name);
program.pushAddress(routine->index);
} else {
program.pushOp(instructions::TypeArgument, n->name);
}
//todo constraints + default
//todo constraints
break;
}
case types::SyntaxKind::FunctionDeclaration: {
Expand Down
3 changes: 2 additions & 1 deletion src/checker/instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ namespace ts::instructions {
* ```
*/
TypeArgument,
TypeArgumentDefault, //expects an entry on the stack
TypeArgumentDefault, //one parameter with the address of the subroutine of the default value

TypeArgumentConstraint, //expects an entry on the stack


Expand Down
Loading

0 comments on commit c2feeb2

Please sign in to comment.