ArkScript
A small, lisp-inspired, functional scripting language
Common.hpp
Go to the documentation of this file.
1/**
2 * @file Common.hpp
3 * @author Lex Plateau (lexplt.dev@gmail.com)
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 MutArg,
47 RefArg,
48 Capture,
49 Keyword,
50 String,
51 Number,
52 List,
53 Spread,
54 Field,
55 Macro,
57 Unused
58 };
59
60 /// Node types as string, in the same order as the enum NodeType
61 constexpr std::array<std::string_view, 13> nodeTypes = {
62 "Symbol",
63 "MutArg",
64 "RefArg",
65 "Capture",
66 "Keyword",
67 "String",
68 "Number",
69 "List",
70 "Spread",
71 "Field",
72 "Macro",
73 "Namespace",
74 "Unused"
75 };
76
77 /// The different keywords available
78 enum class Keyword
79 {
80 Fun,
81 Let,
82 Mut,
83 Set,
84 If,
85 While,
86 Begin,
87 Import,
88 Del
89 };
90
91 /// List of available keywords in ArkScript
92 constexpr std::array<std::string_view, 9> keywords = {
93 "fun",
94 "let",
95 "mut",
96 "set",
97 "if",
98 "while",
99 "begin",
100 "import",
101 "del"
102 };
103
104 namespace Language
105 {
106 constexpr std::string_view AppendInPlace = "append!";
107 constexpr std::string_view ConcatInPlace = "concat!";
108 constexpr std::string_view PopInPlace = "pop!";
109 constexpr std::string_view SetAtInPlace = "@=";
110 constexpr std::string_view SetAt2InPlace = "@@=";
111 /// All the builtins that modify in place a variable
112 constexpr std::array UpdateRef = {
115 };
116
117 // This list is related to include/Ark/Compiler/Instructions.hpp
118 // The order is very important
119 constexpr std::array<std::string_view, 9> listInstructions = {
120 "list",
121 "append",
122 "concat",
125 "pop",
129 };
130
131 constexpr std::string_view SysArgs = "builtin__sys:args";
132 constexpr std::string_view SysProgramName = "builtin__sys:programName";
133
134 constexpr std::string_view And = "and";
135 constexpr std::string_view Or = "or";
136
137 constexpr std::string_view Undef = "$undef";
138 constexpr std::string_view Symcat = "$symcat";
139 constexpr std::string_view Argcount = "$argcount";
140 constexpr std::string_view Repr = "$repr";
141 constexpr std::string_view AsIs = "$as-is";
142 constexpr std::string_view Type = "$type";
143
144 constexpr std::array macros = {
145 Undef,
146 Symcat,
147 Argcount,
148 Repr,
149 AsIs,
150 Type
151 };
152
153 // This list is related to include/Ark/Compiler/Instructions.hpp
154 // from FIRST_OPERATOR, to LAST_OPERATOR
155 // The order is very important
156 constexpr std::array<std::string_view, 24> operators = {
157 "+", "-", "*", "/",
158 ">", "<", "<=", ">=", "!=", "=",
159 "len", "empty?", "tail", "head",
160 "nil?", "assert",
161 "toNumber", "toString",
162 "@", "@@", "mod",
163 "type", "hasField",
164 "not"
165 };
166 }
167}
168
169#endif
Constants used by ArkScript.
constexpr int ARK_VERSION_MAJOR
Definition Constants.hpp:18
constexpr int ARK_VERSION_PATCH
Definition Constants.hpp:20
constexpr int ARK_VERSION_MINOR
Definition Constants.hpp:19
constexpr std::array< std::string_view, 9 > listInstructions
Definition Common.hpp:119
constexpr std::string_view AppendInPlace
Definition Common.hpp:106
constexpr std::string_view AsIs
Definition Common.hpp:141
constexpr std::string_view Type
Definition Common.hpp:142
constexpr std::string_view Argcount
Definition Common.hpp:139
constexpr std::array< std::string_view, 24 > operators
Definition Common.hpp:156
constexpr std::string_view ConcatInPlace
Definition Common.hpp:107
constexpr std::string_view PopInPlace
Definition Common.hpp:108
constexpr std::array macros
Definition Common.hpp:144
constexpr std::string_view SetAt2InPlace
Definition Common.hpp:110
constexpr std::string_view SysArgs
Definition Common.hpp:131
constexpr std::string_view And
Definition Common.hpp:134
constexpr std::string_view Repr
Definition Common.hpp:140
constexpr std::array UpdateRef
All the builtins that modify in place a variable.
Definition Common.hpp:112
constexpr std::string_view Or
Definition Common.hpp:135
constexpr std::string_view Symcat
Definition Common.hpp:138
constexpr std::string_view Undef
Definition Common.hpp:137
constexpr std::string_view SysProgramName
Definition Common.hpp:132
constexpr std::string_view SetAtInPlace
Definition Common.hpp:109
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
NodeType
The different node types available.
Definition Common.hpp:44
Keyword
The different keywords available.
Definition Common.hpp:79
constexpr std::array< std::string_view, 13 > nodeTypes
Node types as string, in the same order as the enum NodeType.
Definition Common.hpp:61
constexpr std::array< std::string_view, 9 > keywords
List of available keywords in ArkScript.
Definition Common.hpp:92
std::vector< uint8_t > bytecode_t
Definition Common.hpp:22