Uniot Core
0.9.0
Loading...
Searching...
No Matches
LispHelper.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2020 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
28
29#pragma once
30
31#include <libminilisp.h>
32
33#include <functional>
34
40#define getPrimitiveName() (__func__)
41
47#define exportPrimitiveNameTo(name) \
48 char name[sizeof(__func__)]; \
49 snprintf(name, sizeof(name), __func__)
50
51namespace uniot {
52
57namespace lisp {
63using Object = struct Obj *;
64
71using VarObject = struct Obj **;
72
78using Root = void *;
79} // namespace lisp
80
90class Lisp {
91 public:
96 enum Type : uint8_t {
97 Unknown = 0,
98 Int = 1,
99 Bool = 2,
101 Symbol = 4,
102 Cell = 5,
103
104 Any = 6
105 };
106
113 static inline bool correct(Lisp::Type type) {
114 return (type >= Type::Int && type <= Lisp::Type::Any);
115 }
116
122 static inline const char *str(Lisp::Type type) {
123 static const char *map[] = {
124 "Unknown",
125 "Int",
126 "Bool",
127 "Bool/Int",
128 "Symbol",
129 "Cell",
130
131 "Any"};
132 return correct(type) ? map[type] : map[Lisp::Type::Unknown];
133 }
134
140 static inline Lisp::Type getType(lisp::Object obj) {
141 if (obj == nullptr) {
142 return Lisp::Unknown;
143 }
144
145 switch (obj->type) {
146 case TINT:
147 return Lisp::Int;
148 case TNIL:
149 return Lisp::Bool;
150 case TTRUE:
151 return Lisp::Bool;
152 case TSYMBOL:
153 return Lisp::Symbol;
154 case TCELL:
155 return Lisp::Cell;
156 default:
157 return Lisp::Unknown;
158 }
159 }
160};
161
170
181
186using LispStartHook = std::function<void(LispStartReason)>;
187
192using LispStopHook = std::function<void(LispStopReason)>;
193
195} // namespace uniot
Utility class for working with Lisp objects in C++.
Definition LispHelper.h:90
Type
Enumeration of supported Lisp data types.
Definition LispHelper.h:96
@ BoolInt
Definition LispHelper.h:100
@ Symbol
Definition LispHelper.h:101
@ Cell
Definition LispHelper.h:102
@ Bool
Definition LispHelper.h:99
@ Int
Definition LispHelper.h:98
@ Any
Definition LispHelper.h:104
@ Unknown
Definition LispHelper.h:97
static bool correct(Lisp::Type type)
Checks if a type value is within the valid range.
Definition LispHelper.h:113
static Lisp::Type getType(lisp::Object obj)
Determines the Lisp::Type of a given Lisp object.
Definition LispHelper.h:140
static const char * str(Lisp::Type type)
Converts a type enumeration value to a human-readable string.
Definition LispHelper.h:122
LispStartReason
Why a script started running.
Definition LispHelper.h:166
struct Obj * Object
A pointer to a Lisp object structure.
Definition LispHelper.h:63
void * Root
A generic pointer representing the root of a Lisp environment.
Definition LispHelper.h:78
LispStopReason
Why a script stopped running.
Definition LispHelper.h:175
std::function< void(LispStartReason)> LispStartHook
Called after the interpreter is built and before the script is evaluated.
Definition LispHelper.h:186
struct Obj ** VarObject
A pointer to a pointer to a Lisp object structure.
Definition LispHelper.h:71
std::function< void(LispStopReason)> LispStopHook
Called before the interpreter is destroyed.
Definition LispHelper.h:192
@ Restored
A stored script, run from AppKit::attach() during boot.
Definition LispHelper.h:167
@ Received
A script that arrived over MQTT.
Definition LispHelper.h:168
@ Completed
The script ran to the end, or a finite task used up its passes.
Definition LispHelper.h:176
@ Cleared
An empty script arrived, which means "run nothing".
Definition LispHelper.h:178
@ Replaced
A new script arrived and took its place.
Definition LispHelper.h:177
@ Failed
A Lisp error tore the machine down.
Definition LispHelper.h:179
Contains type definitions and utilities for interacting with the Lisp interpreter.
Contains all classes and functions related to the Uniot Core.