ArkScript
A small, fast, functional and scripting language for video games
Common.hpp
Go to the documentation of this file.
1/**
2 * @file Common.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Common code for the compiler
5 * @date 2021-10-02
6 *
7 * @copyright Copyright (c) 2021-2025
8 *
9 */
10
11#ifndef ARK_COMPILER_COMMON_HPP
12#define ARK_COMPILER_COMMON_HPP
13
14#include <array>
15#include <string_view>
16#include <vector>
17#include <cinttypes>
18#include <Ark/Constants.hpp>
19
20namespace Ark
21{
22 using bytecode_t = std::vector<uint8_t>;
23}
24
25namespace Ark::internal
26{
27 namespace bytecode
28 {
29 constexpr std::array Magic = { 'a', 'r', 'k', '\0' };
30 constexpr std::array Version = {
31 ARK_VERSION_MAJOR & 0xff00,
32 ARK_VERSION_MAJOR & 0x00ff,
33 ARK_VERSION_MINOR & 0xff00,
34 ARK_VERSION_MINOR & 0x00ff,
35 ARK_VERSION_PATCH & 0xff00,
36 ARK_VERSION_PATCH & 0x00ff
37 };
38 constexpr std::size_t TimestampLength = 8;
39 constexpr std::size_t HeaderSize = Magic.size() + Version.size() + TimestampLength;
40 }
41
42 /// The different node types available
43 enum class NodeType
44 {
45 Symbol,
46 Capture,
47 Keyword,
48 String,
49 Number,
50 List,
51 Spread,
52 Field,
53 Macro,
55 Unused
56 };
57
58 /// Node types as string, in the same order as the enum NodeType
59 constexpr std::array<std::string_view, 11> nodeTypes = {
60 "Symbol",
61 "Capture",
62 "Keyword",
63 "String",
64 "Number",
65 "List",
66 "Spread",
67 "Field",
68 "Macro",
69 "Namespace",
70 "Unused"
71 };
72
73 /// The different keywords available
74 enum class Keyword
75 {
76 Fun,
77 Let,
78 Mut,
79 Set,
80 If,
81 While,
82 Begin,
83 Import,
84 Del
85 };
86
87 /// List of available keywords in ArkScript
88 constexpr std::array<std::string_view, 9> keywords = {
89 "fun",
90 "let",
91 "mut",
92 "set",
93 "if",
94 "while",
95 "begin",
96 "import",
97 "del"
98 };
99
100 namespace Language
101 {
102 constexpr std::string_view AppendInPlace = "append!";
103 constexpr std::string_view ConcatInPlace = "concat!";
104 constexpr std::string_view PopInPlace = "pop!";
105 constexpr std::string_view SetAtInPlace = "@=";
106 constexpr std::string_view SetAt2InPlace = "@@=";
107 /// All the builtins that modify in place a variable
108 constexpr std::array UpdateRef = {
111 };
112
113 // This list is related to include/Ark/Compiler/Instructions.hpp
114 // The order is very important
115 constexpr std::array<std::string_view, 9> listInstructions = {
116 "list",
117 "append",
118 "concat",
121 "pop",
125 };
126
127 constexpr std::string_view SysArgs = "sys:args";
128 constexpr std::string_view SysPlatform = "sys:platform";
129
130 constexpr std::string_view And = "and";
131 constexpr std::string_view Or = "or";
132
133 constexpr std::string_view Undef = "$undef";
134 constexpr std::string_view Symcat = "$symcat";
135 constexpr std::string_view Argcount = "$argcount";
136 constexpr std::string_view Repr = "$repr";
137 constexpr std::string_view AsIs = "$as-is";
138
139 constexpr std::array macros = {
140 Undef,
141 Symcat,
142 Argcount,
143 Repr,
144 AsIs
145 };
146
147 // This list is related to include/Ark/Compiler/Instructions.hpp
148 // from FIRST_OPERATOR, to LAST_OPERATOR
149 // The order is very important
150 constexpr std::array<std::string_view, 24> operators = {
151 "+", "-", "*", "/",
152 ">", "<", "<=", ">=", "!=", "=",
153 "len", "empty?", "tail", "head",
154 "nil?", "assert",
155 "toNumber", "toString",
156 "@", "@@", "mod",
157 "type", "hasField",
158 "not"
159 };
160 }
161}
162
163#endif
Constants used by ArkScript.
constexpr int ARK_VERSION_MAJOR
Definition Constants.hpp:17
constexpr int ARK_VERSION_PATCH
Definition Constants.hpp:19
constexpr int ARK_VERSION_MINOR
Definition Constants.hpp:18
constexpr std::array< std::string_view, 9 > listInstructions
Definition Common.hpp:115
constexpr std::string_view AppendInPlace
Definition Common.hpp:102
constexpr std::string_view AsIs
Definition Common.hpp:137
constexpr std::string_view Argcount
Definition Common.hpp:135
constexpr std::array< std::string_view, 24 > operators
Definition Common.hpp:150
constexpr std::string_view ConcatInPlace
Definition Common.hpp:103
constexpr std::string_view SysPlatform
Definition Common.hpp:128
constexpr std::string_view PopInPlace
Definition Common.hpp:104
constexpr std::array macros
Definition Common.hpp:139
constexpr std::string_view SetAt2InPlace
Definition Common.hpp:106
constexpr std::string_view SysArgs
Definition Common.hpp:127
constexpr std::string_view And
Definition Common.hpp:130
constexpr std::string_view Repr
Definition Common.hpp:136
constexpr std::array UpdateRef
All the builtins that modify in place a variable.
Definition Common.hpp:108
constexpr std::string_view Or
Definition Common.hpp:131
constexpr std::string_view Symcat
Definition Common.hpp:134
constexpr std::string_view Undef
Definition Common.hpp:133
constexpr std::string_view SetAtInPlace
Definition Common.hpp:105
constexpr std::array Magic
Definition Common.hpp:29
constexpr std::size_t HeaderSize
Definition Common.hpp:39
constexpr std::size_t TimestampLength
Definition Common.hpp:38
constexpr std::array< std::string_view, 11 > nodeTypes
Node types as string, in the same order as the enum NodeType.
Definition Common.hpp:59
NodeType
The different node types available.
Definition Common.hpp:44
Keyword
The different keywords available.
Definition Common.hpp:75
constexpr std::array< std::string_view, 9 > keywords
List of available keywords in ArkScript.
Definition Common.hpp:88
std::vector< uint8_t > bytecode_t
Definition Common.hpp:22