Uniot Core
0.8.1
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
38#define getPrimitiveName() (__func__)
39
45#define exportPrimitiveNameTo(name) \
46 char name[sizeof(__func__)]; \
47 snprintf(name, sizeof(name), __func__)
48
49namespace uniot {
50
55namespace lisp {
61using Object = struct Obj *;
62
69using VarObject = struct Obj **;
70
76using Root = void *;
77} // namespace lisp
78
88class Lisp {
89 public:
94 enum Type : uint8_t {
95 Unknown = 0,
96 Int = 1,
97 Bool = 2,
98 BoolInt = 3,
99 Symbol = 4,
100 Cell = 5,
101
102 Any = 6
103 };
104
111 static inline bool correct(Lisp::Type type) {
112 return (type >= Type::Int && type <= Lisp::Type::Any);
113 }
114
120 static inline const char *str(Lisp::Type type) {
121 static const char *map[] = {
122 "Unknown",
123 "Int",
124 "Bool",
125 "Bool/Int",
126 "Symbol",
127 "Cell",
128
129 "Any"};
130 return correct(type) ? map[type] : map[Lisp::Type::Unknown];
131 }
132
138 static inline Lisp::Type getType(lisp::Object obj) {
139 if (obj == nullptr) {
140 return Lisp::Unknown;
141 }
142
143 switch (obj->type) {
144 case TINT:
145 return Lisp::Int;
146 case TNIL:
147 return Lisp::Bool;
148 case TTRUE:
149 return Lisp::Bool;
150 case TSYMBOL:
151 return Lisp::Symbol;
152 case TCELL:
153 return Lisp::Cell;
154 default:
155 return Lisp::Unknown;
156 }
157 }
158};
159
160} // namespace uniot
Utility class for working with Lisp objects in C++.
Definition LispHelper.h:88
Type
Enumeration of supported Lisp data types.
Definition LispHelper.h:94
@ BoolInt
Definition LispHelper.h:98
@ Symbol
Definition LispHelper.h:99
@ Cell
Definition LispHelper.h:100
@ Bool
Definition LispHelper.h:97
@ Int
Definition LispHelper.h:96
@ Any
Definition LispHelper.h:102
@ Unknown
Definition LispHelper.h:95
static bool correct(Lisp::Type type)
Checks if a type value is within the valid range.
Definition LispHelper.h:111
static Lisp::Type getType(lisp::Object obj)
Determines the Lisp::Type of a given Lisp object.
Definition LispHelper.h:138
static const char * str(Lisp::Type type)
Converts a type enumeration value to a human-readable string.
Definition LispHelper.h:120
struct Obj * Object
A pointer to a Lisp object structure.
Definition LispHelper.h:61
void * Root
A generic pointer representing the root of a Lisp environment.
Definition LispHelper.h:76
struct Obj ** VarObject
A pointer to a pointer to a Lisp object structure.
Definition LispHelper.h:69
Contains type definitions and utilities for interacting with the Lisp interpreter.
Contains all classes and functions related to the Uniot Core.