REMOVED USER_STD macros, code formating, csv changes, few file functions

This commit is contained in:
2021-02-15 23:20:20 +01:00
parent 125c73ac33
commit c1532e78b1
15 changed files with 10550 additions and 2101 deletions

204
ml.h
View File

@@ -1,11 +1,5 @@
#pragma once
// Comment this define out to drop support for standard library functions.
// This allows the program to run without a runtime.
#define USE_STD
// Comment this define out to drop support for libm functions
#define HAS_LIBM
#include <map>
#include <string>
@@ -14,7 +8,7 @@
#include <exception>
// // Forward declaration for MlEnvironment class definition
// Forward declaration for MlEnvironment class definition
class MlValue;
@@ -29,19 +23,22 @@ public:
// This is only used to determine which atoms to capture when
// creating a lambda function.
bool has(std::string name) const;
// Get the value associated with this name in this scope
MlValue get(const std::string& name) const;
MlValue get(const std::string &name) const;
// Set the value associated with this name in this scope
void set(std::string name, MlValue value);
void combine(MlEnvironment const &other);
void set_parent_scope(MlEnvironment *parent) {
parent_scope = parent;
parent_scope = parent;
}
// Output this scope in readable form to a stream.
friend std::ostream &operator<<(std::ostream &os, MlEnvironment const &v);
private:
// The definitions in the scope.
@@ -50,20 +47,21 @@ private:
};
// An exception thrown by the lisp
class MlError {
public:
// Create an error with the value that caused the error,
// the scope where the error was found, and the message.
MlError(const MlValue &v, MlEnvironment const &env, const char *msg);
// Copy constructor is needed to prevent double frees
MlError(MlError const &other);
~MlError();
// Get the printable error description.
std::string description();
private:
MlValue *cause;
MlEnvironment env;
@@ -71,142 +69,144 @@ private:
};
// The type for a builtin function, which takes a list of values,
// and the environment to run the function in.
typedef MlValue (*Builtin)(std::vector<MlValue>, MlEnvironment &);
class MlValue {
public:
class MlValue {
public:
// Constructs a unit value
MlValue();
// Constructs a unit value
MlValue();
// Constructs an integer
MlValue(int i);
// Constructs a floating point value
MlValue(double f);
// Constructs a list
MlValue(const std::vector<MlValue> &list);
// Constructs an integer
MlValue(int i);
// Construct a quoted value
static MlValue quote(const MlValue& quoted);
// Constructs a floating point value
MlValue(double f);
// Construct an atom
static MlValue atom(const std::string &s);
// Constructs a list
MlValue(const std::vector<MlValue> &list);
// Construct a string
static MlValue string(const std::string &s);
// Construct a quoted value
static MlValue quote(const MlValue &quoted);
// Construct a lambda function
MlValue(const std::vector<MlValue> &params, MlValue ret, MlEnvironment const &env);
// Construct an atom
static MlValue atom(const std::string &s);
// Construct a builtin function
MlValue(const std::string &name, Builtin b);
// Construct a string
static MlValue string(const std::string &s);
std::vector<std::string> get_used_atoms();
// Construct a lambda function
MlValue(const std::vector<MlValue> &params, MlValue ret, MlEnvironment const &env);
// Is this a builtin function?
bool is_builtin();
// Construct a builtin function
MlValue(const std::string &name, Builtin b);
// Apply this as a function to a list of arguments in a given environment.
MlValue apply(std::vector<MlValue> args, MlEnvironment &env);
// Evaluate this value as lisp code.
MlValue eval(MlEnvironment &env);
std::vector<std::string> get_used_atoms();
bool is_number() const;
// Is this a builtin function?
bool is_builtin();
// Get the "truthy" boolean value of this value.
bool as_bool() const;
// Apply this as a function to a list of arguments in a given environment.
MlValue apply(std::vector<MlValue> args, MlEnvironment &env);
// Get this item's integer value
int as_int() const;
// Evaluate this value as lisp code.
MlValue eval(MlEnvironment &env);
// Get this item's floating point value
double as_float() const;
bool is_number() const;
// Get this item's string value
std::string as_string() const;
// Get the "truthy" boolean value of this value.
bool as_bool() const;
// Get this item's atom value
std::string as_atom() const;
// Get this item's integer value
int as_int() const;
// Get this item's list value
std::vector<MlValue> as_list() const;
// Get this item's floating point value
double as_float() const;
// Push an item to the end of this list
void push(MlValue val);
// Get this item's string value
std::string as_string() const;
// Push an item from the end of this list
MlValue pop();
// Get this item's atom value
std::string as_atom() const;
// Get this item's list value
std::vector<MlValue> as_list() const;
// Push an item to the end of this list
void push(MlValue val);
// Push an item from the end of this list
MlValue pop();
// Cast this to an integer value
MlValue cast_to_int() const;
// Cast this to an integer value
MlValue cast_to_int() const;
// Cast this to a floating point value
MlValue cast_to_float() const;
// Cast this to a floating point value
MlValue cast_to_float() const;
bool operator==(MlValue other) const;
bool operator==(MlValue other) const;
bool operator!=(const MlValue &other) const;
bool operator!=(const MlValue &other) const;
bool operator>=(const MlValue &other) const;
bool operator>=(const MlValue &other) const;
bool operator<=(const MlValue &other) const;
bool operator<=(const MlValue &other) const;
bool operator>(const MlValue &other) const;
bool operator>(const MlValue &other) const;
bool operator<(const MlValue &other) const;
bool operator<(const MlValue &other) const;
// This function adds two lisp values, and returns the lisp value result.
MlValue operator+(const MlValue &other) const;
// This function adds two lisp values, and returns the lisp value result.
MlValue operator+(const MlValue &other) const;
// This function subtracts two lisp values, and returns the lisp value result.
MlValue operator-(const MlValue &other) const;
// This function subtracts two lisp values, and returns the lisp value result.
MlValue operator-(const MlValue &other) const;
// This function multiplies two lisp values, and returns the lisp value result.
MlValue operator*(const MlValue &other) const;
// This function multiplies two lisp values, and returns the lisp value result.
MlValue operator*(const MlValue &other) const;
// This function divides two lisp values, and returns the lisp value result.
MlValue operator/(const MlValue &other) const;
// This function divides two lisp values, and returns the lisp value result.
MlValue operator/(const MlValue &other) const;
// This function finds the remainder of two lisp values, and returns the lisp value result.
MlValue operator%(const MlValue &other) const;
// This function finds the remainder of two lisp values, and returns the lisp value result.
MlValue operator%(const MlValue &other) const;
// Get the name of the type of this value
std::string get_type_name();
// Get the name of the type of this value
std::string get_type_name();
std::string display() const;
std::string display() const;
std::string debug() const;
std::string debug() const;
friend std::ostream &operator<<(std::ostream &os, MlValue const &v);
friend std::ostream &operator<<(std::ostream &os, MlValue const &v);
private:
enum {
QUOTE,
ATOM,
INT,
FLOAT,
LIST,
STRING,
LAMBDA,
BUILTIN,
UNIT
} type;
private:
enum {
QUOTE,
ATOM,
INT,
FLOAT,
LIST,
STRING,
LAMBDA,
BUILTIN,
UNIT
} type;
union {
int i;
double f;
Builtin b;
} stack_data;
union {
int i;
double f;
Builtin b;
} stack_data;
std::string str;
std::vector<MlValue> list;
MlEnvironment lambda_scope;
};
std::string str;
std::vector<MlValue> list;
MlEnvironment lambda_scope;
};