Add simple and naive string building functionality
This commit is contained in:
parent
0b62026d27
commit
5cb8921c42
7 changed files with 60 additions and 4 deletions
|
@ -58,6 +58,7 @@ set(CPP_TESTS
|
||||||
dict
|
dict
|
||||||
array
|
array
|
||||||
symbol
|
symbol
|
||||||
|
string
|
||||||
)
|
)
|
||||||
|
|
||||||
set(LISP_TESTS
|
set(LISP_TESTS
|
||||||
|
|
|
@ -215,9 +215,11 @@ Result<GcRoot<T>> GcRoot<T>::create(T* ptr) {
|
||||||
template <class T>
|
template <class T>
|
||||||
requires std::derived_from<T, PodObject>
|
requires std::derived_from<T, PodObject>
|
||||||
GcRoot<T>::GcRoot(GcRoot<T>&& rhs) {
|
GcRoot<T>::GcRoot(GcRoot<T>&& rhs) {
|
||||||
rhs._node->update(this);
|
|
||||||
_ptr = rhs._ptr;
|
_ptr = rhs._ptr;
|
||||||
|
if (rhs._node != 0) {
|
||||||
|
rhs._node->update(this);
|
||||||
_node = rhs._node;
|
_node = rhs._node;
|
||||||
|
}
|
||||||
rhs._ptr = 0;
|
rhs._ptr = 0;
|
||||||
rhs._node = 0;
|
rhs._node = 0;
|
||||||
}
|
}
|
||||||
|
@ -227,9 +229,11 @@ template <class T>
|
||||||
GcRoot<T>& GcRoot<T>::operator=(GcRoot<T>&& rhs) {
|
GcRoot<T>& GcRoot<T>::operator=(GcRoot<T>&& rhs) {
|
||||||
if (_node != 0) _node->remove();
|
if (_node != 0) _node->remove();
|
||||||
|
|
||||||
rhs._node->update(this);
|
|
||||||
_ptr = rhs._ptr;
|
_ptr = rhs._ptr;
|
||||||
|
if (rhs._node != 0) {
|
||||||
|
rhs._node->update(this);
|
||||||
_node = rhs._node;
|
_node = rhs._node;
|
||||||
|
}
|
||||||
rhs._ptr = 0;
|
rhs._ptr = 0;
|
||||||
rhs._node = 0;
|
rhs._node = 0;
|
||||||
|
|
||||||
|
|
|
@ -196,10 +196,14 @@ Result<Value> String::copy_value() const {
|
||||||
return Value(String(TRY(_value.copy())));
|
return Value(String(TRY(_value.copy())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result<String> String::copy() const { return String(TRY(_value.copy())); }
|
||||||
|
|
||||||
Result<Value> Symbol::copy_value() const {
|
Result<Value> Symbol::copy_value() const {
|
||||||
return Value(Symbol(TRY(_value.copy())));
|
return Value(Symbol(TRY(_value.copy())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result<Symbol> Symbol::copy() const { return Symbol(TRY(_value.copy())); }
|
||||||
|
|
||||||
Result<Value> Syntax::copy_value() const {
|
Result<Value> Syntax::copy_value() const {
|
||||||
return Value(Syntax(TRY(_value.copy())));
|
return Value(Syntax(TRY(_value.copy())));
|
||||||
}
|
}
|
||||||
|
@ -836,3 +840,13 @@ Result<Value> Syntax::rest() const {
|
||||||
auto val = TRY(expression());
|
auto val = TRY(expression());
|
||||||
return val.rest();
|
return val.rest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result<String> build_string(const Value& value) {
|
||||||
|
if (value.is<String>()) {
|
||||||
|
return value.to<String>()->copy();
|
||||||
|
}
|
||||||
|
|
||||||
|
return write_one(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<String> build_string(const char* value) { return String::create(value); }
|
||||||
|
|
|
@ -1299,3 +1299,15 @@ Result<bool> syntax_is_list(const Value& value);
|
||||||
Result<bool> syntax_is_nil(const Value& value);
|
Result<bool> syntax_is_nil(const Value& value);
|
||||||
Result<Value> syntax_unwrap(const Value& value);
|
Result<Value> syntax_unwrap(const Value& value);
|
||||||
Result<Value> syntax_unwrap_all(const Value& value);
|
Result<Value> syntax_unwrap_all(const Value& value);
|
||||||
|
|
||||||
|
Result<String> build_string(const Value& value);
|
||||||
|
Result<String> build_string(const char* value);
|
||||||
|
|
||||||
|
template <class T, class... Args>
|
||||||
|
Result<String> build_string(const T& value, Args&&... args) {
|
||||||
|
auto first = TRY(build_string(value));
|
||||||
|
auto rest = TRY(build_string(std::forward<Args>(args)...));
|
||||||
|
|
||||||
|
first = TRY(first.concat(rest));
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,18 @@
|
||||||
#include "error.hpp"
|
#include "error.hpp"
|
||||||
|
|
||||||
|
#include "common.hpp"
|
||||||
|
|
||||||
static const char* curerr = 0;
|
static const char* curerr = 0;
|
||||||
|
static Value errobj;
|
||||||
|
|
||||||
void seterr(const char* err) { curerr = err; }
|
void seterr(const char* err) { curerr = err; }
|
||||||
const char* geterr(void) { return curerr; }
|
const char* geterr(void) { return curerr; }
|
||||||
|
|
||||||
|
const Value& geterrobj(void) { return errobj; }
|
||||||
|
|
||||||
|
Result<void> seterrobj(const Value& val) {
|
||||||
|
errobj = TRY(val.copy());
|
||||||
|
return Result<void>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void reseterrobj() { errobj = Value(); }
|
||||||
|
|
|
@ -19,16 +19,27 @@ enum class ErrorCode {
|
||||||
Interrupt,
|
Interrupt,
|
||||||
AssertionFailed,
|
AssertionFailed,
|
||||||
DivisionByZero,
|
DivisionByZero,
|
||||||
|
SyntaxError,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Value;
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class Result;
|
||||||
|
|
||||||
void seterr(const char* err);
|
void seterr(const char* err);
|
||||||
const char* geterr(void);
|
const char* geterr(void);
|
||||||
|
|
||||||
|
const Value& geterrobj(void);
|
||||||
|
Result<void> seterrobj(const Value&);
|
||||||
|
void reseterrobj();
|
||||||
|
|
||||||
#define STRINGIZE_NESTED(A) #A
|
#define STRINGIZE_NESTED(A) #A
|
||||||
#define STRINGIZE(A) STRINGIZE_NESTED(A)
|
#define STRINGIZE(A) STRINGIZE_NESTED(A)
|
||||||
|
|
||||||
#define ERROR(code) \
|
#define ERROR(code) \
|
||||||
(({ \
|
(({ \
|
||||||
|
reseterrobj(); \
|
||||||
seterr("Error " STRINGIZE(code) " at " __FILE__ ":" STRINGIZE(__LINE__)); \
|
seterr("Error " STRINGIZE(code) " at " __FILE__ ":" STRINGIZE(__LINE__)); \
|
||||||
ErrorCode::code; \
|
ErrorCode::code; \
|
||||||
}))
|
}))
|
||||||
|
|
|
@ -48,6 +48,8 @@ Result<void> run_repl() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> run(int argc, const char* argv[]) {
|
Result<void> run(int argc, const char* argv[]) {
|
||||||
|
auto t = TRY(build_string("foo", "bar", Value(TRY(Nil::create()))));
|
||||||
|
|
||||||
String src = TRY(String::create(""));
|
String src = TRY(String::create(""));
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
if (stdin_isatty()) {
|
if (stdin_isatty()) {
|
||||||
|
|
Loading…
Reference in a new issue