Skip to content

Commit

Permalink
feat: implement literal characters
Browse files Browse the repository at this point in the history
  • Loading branch information
hrzlgnm committed Aug 26, 2023
1 parent f1d9479 commit 1ab4818
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 77 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_library(
source/ast/builtin_function_expression.cpp
source/ast/callable_expression.cpp
source/ast/call_expression.cpp
source/ast/character_literal.cpp
source/ast/function_expression.cpp
source/ast/hash_literal_expression.cpp
source/ast/identifier.cpp
Expand Down
11 changes: 11 additions & 0 deletions source/ast/character_literal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "character_literal.hpp"

character_literal::character_literal(char val)
: value(val)
{
}

auto character_literal::string() const -> std::string
{
return {1, value};
}
13 changes: 13 additions & 0 deletions source/ast/character_literal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "expression.hpp"

struct character_literal : expression
{
explicit character_literal(char val);
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object override;
auto compile(compiler& comp) const -> void override;

char value;
};
6 changes: 6 additions & 0 deletions source/compiler/ast_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <code/code.hpp>
#include <eval/object.hpp>

#include "ast/character_literal.hpp"
#include "compiler.hpp"
#include "symbol_table.hpp"

Expand Down Expand Up @@ -219,3 +220,8 @@ auto call_expression::compile(compiler& comp) const -> void
}

auto builtin_function_expression::compile(compiler& /*comp*/) const -> void {}

auto character_literal::compile(compiler& comp) const -> void
{
comp.emit(opcodes::constant, comp.add_constant({value}));
}
20 changes: 19 additions & 1 deletion source/compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ auto check_program(std::string_view input) -> parsed_program
return {std::move(prgrm), std::move(prsr)};
}

using expected_value = std::variant<int64_t, std::string, std::vector<instructions>>;
using expected_value = std::variant<int64_t, char, std::string, std::vector<instructions>>;

struct ctc
{
Expand All @@ -240,6 +240,7 @@ auto check_constants(const std::vector<expected_value>& expecteds, const constan
std::visit(
overloaded {
[&](const int64_t val) { CHECK_EQ(val, actual.as<integer_type>()); },
[&](const char val) { CHECK_EQ(val, actual.as<char>()); },
[&](const std::string& val) { CHECK_EQ(val, actual.as<string_type>()); },
[&](const std::vector<instructions>& instrs)
{ check_instructions(instrs, actual.as<compiled_function>().instrs); },
Expand Down Expand Up @@ -485,6 +486,23 @@ TEST_CASE("stringExpression")
},
},
};
run(std::move(tests));
}

TEST_CASE("characterExpression")
{
using enum opcodes;
std::array tests {
ctc {
R"('m')",
{{'m'}},
{
make(constant, 0),
make(pop),
},
},
};
run(std::move(tests));
}

TEST_CASE("arrayLiterals")
Expand Down
Loading

0 comments on commit 1ab4818

Please sign in to comment.