Skip to content

Commit

Permalink
refactor: add kind
Browse files Browse the repository at this point in the history
  • Loading branch information
seven332 committed Dec 7, 2023
1 parent 7db3e8a commit 45ffee8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 38 deletions.
1 change: 1 addition & 0 deletions packages/wasm/c++/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(sksl-wasm-lib STATIC
kind.cpp
lexer.cpp
formatter.cpp
simple_codec.cpp
Expand Down
39 changes: 1 addition & 38 deletions packages/wasm/c++/src/action/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@
#include <cstdint>
#include <iostream>
#include <optional>
#include <regex>
#include <string>
#include <string_view>
#include <vector>

#include "data.h"
#include "hash.h"
#include "kind.h"
#include "lexer.h"
#include "module.h"
#include "token.h"
Expand Down Expand Up @@ -78,42 +77,6 @@ class SkSLDiagnosticReporter : public SkSL::ErrorReporter {
std::vector<SkSLDiagnostic> diagnostics_;
};

struct Kind {
std::string str;
std::uint32_t position;
std::uint32_t length;
};

std::optional<Kind> GetKind(const std::string& content) {
std::regex re(R"(^[ \t]*\/\/[ /\t]*kind[ \t]*[:=][ \t]*(\w+))", std::regex_constants::ECMAScript);
std::cmatch match;
if (!std::regex_search(content.data(), match, re)) {
return std::nullopt;
}
return Kind {
.str = match.str(1),
.position = static_cast<std::uint32_t>(match.position(1)),
.length = static_cast<std::uint32_t>(match.length(1)),
};
}

std::optional<SkSL::ProgramKind> ToSkSLProgramKind(const std::string& kind) {
switch (Hash(kind.c_str())) {
case Hash("shader"):
return SkSL::ProgramKind::kRuntimeShader;
case Hash("colorfilter"):
return SkSL::ProgramKind::kRuntimeColorFilter;
case Hash("blender"):
return SkSL::ProgramKind::kRuntimeBlender;
case Hash("meshfrag"):
return SkSL::ProgramKind::kMeshFragment;
case Hash("meshvert"):
return SkSL::ProgramKind::kMeshVertex;
default:
return std::nullopt;
}
}

static std::unique_ptr<SkSL::Program> CompileProgram(
SkSL::Compiler* compiler,
SkSL::ProgramKind kind,
Expand Down
37 changes: 37 additions & 0 deletions packages/wasm/c++/src/kind.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "kind.h"

#include <cstdint>
#include <regex>
#include <string>

#include "hash.h"

std::optional<Kind> GetKind(const std::string& content) {
std::regex re(R"(^[ \t]*\/\/[ /\t]*kind[ \t]*[:=][ \t]*(\w+))", std::regex_constants::ECMAScript);
std::cmatch match;
if (!std::regex_search(content.data(), match, re)) {
return std::nullopt;
}
return Kind {
.str = match.str(1),
.position = static_cast<std::uint32_t>(match.position(1)),
.length = static_cast<std::uint32_t>(match.length(1)),
};
}

std::optional<SkSL::ProgramKind> ToSkSLProgramKind(const std::string& kind) {
switch (Hash(kind.c_str())) {
case Hash("shader"):
return SkSL::ProgramKind::kRuntimeShader;
case Hash("colorfilter"):
return SkSL::ProgramKind::kRuntimeColorFilter;
case Hash("blender"):
return SkSL::ProgramKind::kRuntimeBlender;
case Hash("meshfrag"):
return SkSL::ProgramKind::kMeshFragment;
case Hash("meshvert"):
return SkSL::ProgramKind::kMeshVertex;
default:
return std::nullopt;
}
}
16 changes: 16 additions & 0 deletions packages/wasm/c++/src/kind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <src/sksl/SkSLProgramKind.h>

#include <cstdint>
#include <string>

struct Kind {
std::string str;
std::uint32_t position;
std::uint32_t length;
};

std::optional<Kind> GetKind(const std::string& content);

std::optional<SkSL::ProgramKind> ToSkSLProgramKind(const std::string& kind);

0 comments on commit 45ffee8

Please sign in to comment.