initial support for nil, make-list
unfinished, but usable.. still 0 is false in libs and code
This commit is contained in:
37
ml.cpp
37
ml.cpp
@@ -30,7 +30,6 @@
|
||||
#define INVALID_ORDER "cannot order expression"
|
||||
#define BAD_CAST "cannot cast"
|
||||
#define ATOM_NOT_DEFINED "atom not defined"
|
||||
#define EVAL_EMPTY_LIST "evaluated empty list"
|
||||
#define INTERNAL_ERROR "internal virtual machine error"
|
||||
#define INDEX_OUT_OF_RANGE "index out of range"
|
||||
#define MALFORMED_PROGRAM "malformed program"
|
||||
@@ -40,6 +39,7 @@
|
||||
#define INT_TYPE "int"
|
||||
#define FLOAT_TYPE "float"
|
||||
#define UNIT_TYPE "unit"
|
||||
#define NIL_TYPE "nil"
|
||||
#define FUNCTION_TYPE "function"
|
||||
#define ATOM_TYPE "atom"
|
||||
#define QUOTE_TYPE "quote"
|
||||
@@ -93,6 +93,12 @@ MlValue MlValue::string(const std::string &s) {
|
||||
return result;
|
||||
}
|
||||
|
||||
MlValue MlValue::nil() {
|
||||
MlValue result;
|
||||
result.type = NIL;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Construct a lambda function
|
||||
MlValue::MlValue(const std::vector<MlValue> ¶ms, MlValue ret, MlEnvironment const &env) : type(LAMBDA) {
|
||||
// We store the params and the result in the list member
|
||||
@@ -162,7 +168,7 @@ bool MlValue::is_number() const {
|
||||
|
||||
// Get the "truthy" boolean value of this value.
|
||||
bool MlValue::as_bool() const {
|
||||
return *this != MlValue(0);
|
||||
return type != NIL && *this != MlValue(0); // TODO remove 0 as false
|
||||
}
|
||||
|
||||
// Get this item's integer value
|
||||
@@ -281,6 +287,8 @@ bool MlValue::operator==(MlValue other) const {
|
||||
// The values for quotes are stored in the
|
||||
// first slot of the list member.
|
||||
return list[0] == other.list[0];
|
||||
case NIL:
|
||||
return other.type == NIL;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
@@ -516,6 +524,8 @@ std::string MlValue::get_type_name() {
|
||||
return FUNCTION_TYPE;
|
||||
case UNIT:
|
||||
return UNIT_TYPE;
|
||||
case NIL:
|
||||
return NIL_TYPE;
|
||||
default:
|
||||
// We don't know the name of this type.
|
||||
// This isn't the users fault, this is just unhandled.
|
||||
@@ -553,6 +563,8 @@ std::string MlValue::display() const {
|
||||
return "<" + str + " at " + to_string(long(stack_data.b)) + ">";
|
||||
case UNIT:
|
||||
return "@";
|
||||
case NIL:
|
||||
return "nil";
|
||||
default:
|
||||
// We don't know how to display whatever type this is.
|
||||
// This isn't the users fault, this is just unhandled.
|
||||
@@ -594,6 +606,8 @@ std::string MlValue::debug() const {
|
||||
return "<" + str + " at " + to_string(long(stack_data.b)) + ">";
|
||||
case UNIT:
|
||||
return "@";
|
||||
case NIL:
|
||||
return "nil";
|
||||
default:
|
||||
// We don't know how to debug whatever type this is.
|
||||
// This isn't the users fault, this is just unhandled.
|
||||
@@ -621,8 +635,7 @@ MlError::~MlError() {
|
||||
}
|
||||
|
||||
std::string MlError::description() {
|
||||
return "error: the expression `" + cause->debug() + "` failed in scope " + to_string(env) + " with message \"" +
|
||||
msg + "\"";
|
||||
return "error: the expression `" + cause->debug() + "` failed in scope " + to_string(env) + " with message \"" + msg + "\"";
|
||||
}
|
||||
|
||||
void MlEnvironment::combine(MlEnvironment const &other) {
|
||||
@@ -657,9 +670,7 @@ MlValue MlValue::apply(std::vector<MlValue> args, MlEnvironment &env) {
|
||||
// Get the list of parameter atoms
|
||||
params = list[0].list;
|
||||
if (params.size() != args.size())
|
||||
throw MlError(MlValue(args), env, args.size() > params.size() ?
|
||||
TOO_MANY_ARGS : TOO_FEW_ARGS
|
||||
);
|
||||
throw MlError(MlValue(args), env, args.size() > params.size() ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||
|
||||
// Get the captured scope from the lambda
|
||||
e = lambda_scope;
|
||||
@@ -701,7 +712,7 @@ MlValue MlValue::eval(MlEnvironment &env) {
|
||||
return env.get(str);
|
||||
case LIST:
|
||||
if (list.size() < 1)
|
||||
throw MlError(*this, env, EVAL_EMPTY_LIST);
|
||||
return MlValue::nil();
|
||||
|
||||
args = std::vector<MlValue>(list.begin() + 1, list.end());
|
||||
|
||||
@@ -747,6 +758,7 @@ MlValue parse(std::string &s, int &ptr) {
|
||||
|
||||
if (s == "") {
|
||||
return MlValue();
|
||||
|
||||
} else if (s[ptr] == '\'') {
|
||||
// If this is a quote
|
||||
ptr++;
|
||||
@@ -807,6 +819,7 @@ MlValue parse(std::string &s, int &ptr) {
|
||||
}
|
||||
|
||||
return MlValue::string(x);
|
||||
|
||||
} else if (s[ptr] == '@') {
|
||||
ptr++;
|
||||
skip_whitespace(s, ptr);
|
||||
@@ -822,7 +835,11 @@ MlValue parse(std::string &s, int &ptr) {
|
||||
std::string x = s.substr(ptr, n);
|
||||
ptr += n;
|
||||
skip_whitespace(s, ptr);
|
||||
return MlValue::atom(x);
|
||||
if (x == "nil")
|
||||
return MlValue::nil();
|
||||
else
|
||||
return MlValue::atom(x);
|
||||
|
||||
} else {
|
||||
throw std::runtime_error(MALFORMED_PROGRAM);
|
||||
}
|
||||
@@ -1765,7 +1782,7 @@ int main(int argc, const char **argv) {
|
||||
try {
|
||||
load_std_lib(env);
|
||||
// for xcode profiling
|
||||
// run(read_file_contents("/Users/vaclavt/Development/mlisp/tests/test.lsp"), env);
|
||||
run(read_file_contents("/Users/vaclavt/Development/mlisp/tests/test.lsp"), env);
|
||||
|
||||
if (argc == 1 || (argc == 2 && std::string(argv[1]) == "-i"))
|
||||
repl(env);
|
||||
|
||||
Reference in New Issue
Block a user