UNIT type removed

This commit is contained in:
Vaclav Tvrdik 2021-03-07 18:48:57 +01:00
parent 3d54ed9fb3
commit d54c2ee79b
4 changed files with 17 additions and 54 deletions

View File

@ -125,8 +125,7 @@ namespace json11 {
////////////////////////
static MlValue ivalualize(NullStruct) {
MlValue null;
return null;
return MlValue::nil();
}
static MlValue ivalualize(double value) {

View File

@ -1,8 +1,3 @@
;; (print
;; "ahoj"
;; ; this comment is problem
;; )
;; (print "lisp")
; (define l '(1 2 2 2 3 4 5 4 4 1 2 2 2 3 4 5 4 4 61 2 2 2 3 4 5 4 4 66)
(define json_list (parse-json "{\"k1\":\"v1\", \"k2\":42, \"k3\":[\"a\",123,true,false,null]}"))
(print json_list)
(for x json_list (print x))

54
ml.cpp
View File

@ -22,6 +22,7 @@
#define TOO_FEW_ARGS "too few arguments to function"
#define TOO_MANY_ARGS "too many arguments to function"
#define INVALID_ARGUMENT "invalid argument"
#define INVALID_ARGUMENT_NIL "invalid nil argument"
#define MISMATCHED_TYPES "mismatched types"
#define CALL_NON_FUNCTION "called non-function"
#define UNKNOWN_ERROR "unknown exception"
@ -38,7 +39,6 @@
#define STRING_TYPE "string"
#define INT_TYPE "int"
#define FLOAT_TYPE "float"
#define UNIT_TYPE "unit"
#define NIL_TYPE "nil"
#define FUNCTION_TYPE "function"
#define ATOM_TYPE "atom"
@ -57,7 +57,7 @@ bool is_symbol(char ch) {
}
MlValue::MlValue() : type(UNIT) {}
MlValue::MlValue() : type(NIL) {}
MlValue::MlValue(int i) : type(INT) { stack_data.i = i; }
@ -335,10 +335,8 @@ bool MlValue::operator<(const MlValue &other) const {
// This function adds two lisp values, and returns the lisp value result.
MlValue MlValue::operator+(const MlValue &other) const {
// If the other value's type is the unit type,
// don't even bother continuing.
// Unit types consume all arithmetic operations.
if (other.type == UNIT) return other;
if (other.type == NIL)
throw MlError(*this, MlEnvironment(), INVALID_ARGUMENT_NIL);
// Other type must be a float or an int
if ((is_number() || other.is_number()) &&
@ -374,8 +372,6 @@ MlValue MlValue::operator+(const MlValue &other) const {
return result;
} else throw MlError(*this, MlEnvironment(), INVALID_BIN_OP);
case UNIT:
return *this;
default:
throw MlError(*this, MlEnvironment(), INVALID_BIN_OP);
}
@ -383,10 +379,8 @@ MlValue MlValue::operator+(const MlValue &other) const {
// This function subtracts two lisp values, and returns the lisp value result.
MlValue MlValue::operator-(const MlValue &other) const {
// If the other value's type is the unit type,
// don't even bother continuing.
// Unit types consume all arithmetic operations.
if (other.type == UNIT) return other;
if (other.type == NIL)
throw MlError(*this, MlEnvironment(), INVALID_ARGUMENT_NIL);
// Other type must be a float or an int
if (other.type != FLOAT && other.type != INT)
@ -404,9 +398,6 @@ MlValue MlValue::operator-(const MlValue &other) const {
return MlValue(cast_to_float().stack_data.f - other.stack_data.f);
// Otherwise, do integer subtraction.
else return MlValue(stack_data.i - other.stack_data.i);
case UNIT:
// Unit types consume all arithmetic operations.
return *this;
default:
// This operation was done on an unsupported type
throw MlError(*this, MlEnvironment(), INVALID_BIN_OP);
@ -415,10 +406,8 @@ MlValue MlValue::operator-(const MlValue &other) const {
// This function multiplies two lisp values, and returns the lisp value result.
MlValue MlValue::operator*(const MlValue &other) const {
// If the other value's type is the unit type,
// don't even bother continuing.
// Unit types consume all arithmetic operations.
if (other.type == UNIT) return other;
if (other.type == NIL)
throw MlError(*this, MlEnvironment(), INVALID_ARGUMENT_NIL);
// Other type must be a float or an int
if (other.type != FLOAT && other.type != INT)
@ -434,9 +423,6 @@ MlValue MlValue::operator*(const MlValue &other) const {
return MlValue(cast_to_float().stack_data.f * other.stack_data.f);
// Otherwise, do integer multiplication.
else return MlValue(stack_data.i * other.stack_data.i);
case UNIT:
// Unit types consume all arithmetic operations.
return *this;
default:
// This operation was done on an unsupported type
throw MlError(*this, MlEnvironment(), INVALID_BIN_OP);
@ -445,10 +431,8 @@ MlValue MlValue::operator*(const MlValue &other) const {
// This function divides two lisp values, and returns the lisp value result.
MlValue MlValue::operator/(const MlValue &other) const {
// If the other value's type is the unit type,
// don't even bother continuing.
// Unit types consume all arithmetic operations.
if (other.type == UNIT) return other;
if (other.type == NIL)
throw MlError(*this, MlEnvironment(), INVALID_ARGUMENT_NIL);
// Other type must be a float or an int
if (other.type != FLOAT && other.type != INT)
@ -464,9 +448,6 @@ MlValue MlValue::operator/(const MlValue &other) const {
return MlValue(cast_to_float().stack_data.f / other.stack_data.f);
// Otherwise, do integer multiplication.
else return MlValue(stack_data.i / other.stack_data.i);
case UNIT:
// Unit types consume all arithmetic operations.
return *this;
default:
// This operation was done on an unsupported type
throw MlError(*this, MlEnvironment(), INVALID_BIN_OP);
@ -475,10 +456,8 @@ MlValue MlValue::operator/(const MlValue &other) const {
// This function finds the remainder of two lisp values, and returns the lisp value result.
MlValue MlValue::operator%(const MlValue &other) const {
// If the other value's type is the unit type,
// don't even bother continuing.
// Unit types consume all arithmetic operations.
if (other.type == UNIT) return other;
if (other.type == NIL)
throw MlError(*this, MlEnvironment(), INVALID_ARGUMENT_NIL);
// Other type must be a float or an int
if (other.type != FLOAT && other.type != INT)
@ -492,9 +471,6 @@ MlValue MlValue::operator%(const MlValue &other) const {
if (other.type == FLOAT)
return MlValue(fmod(cast_to_float().stack_data.f, other.stack_data.f));
else return MlValue(stack_data.i % other.stack_data.i);
case UNIT:
// Unit types consume all arithmetic operations.
return *this;
default:
// This operation was done on an unsupported type
throw MlError(*this, MlEnvironment(), INVALID_BIN_OP);
@ -522,8 +498,6 @@ std::string MlValue::get_type_name() {
// lambda and builtin types, we group them together.
// This is because they are both callable.
return FUNCTION_TYPE;
case UNIT:
return UNIT_TYPE;
case NIL:
return NIL_TYPE;
default:
@ -561,8 +535,6 @@ std::string MlValue::display() const {
return "(" + result + ")";
case BUILTIN:
return "<" + str + " at " + to_string(long(stack_data.b)) + ">";
case UNIT:
return "@";
case NIL:
return "nil";
default:
@ -604,8 +576,6 @@ std::string MlValue::debug() const {
return "(" + result + ")";
case BUILTIN:
return "<" + str + " at " + to_string(long(stack_data.b)) + ">";
case UNIT:
return "@";
case NIL:
return "nil";
default:

3
ml.h
View File

@ -77,7 +77,7 @@ typedef MlValue (*Builtin)(std::vector<MlValue>, MlEnvironment &);
class MlValue {
public:
// Constructs a unit value
// Constructs a nil value
MlValue();
// Constructs an integer
@ -200,7 +200,6 @@ private:
STRING,
LAMBDA,
BUILTIN,
UNIT,
NIL
} type;